Files
apes-sidebar/tests/editor-conflict-reload.spec.tsx
T
Menghuan1918 44007061bb fix(editor): 重新载入后不再死锁在 fs-conflict(#1059)
根因:保存基线 mtimeRef 只在 [content] effect 里刷新;重新载入返回同样
文本时 content 不变,effect 不跑,下一次保存继续发送旧 expectedMtimeMs。

修复:
- props.mtimeMs 变化时也更新 mtimeRef(只动基线,模式/草稿/脏标记的
  文件切换重置语义保持不动);
- 冲突横幅「Reload from disk」先探测磁盘文本:与上次载入/保存的字节
  一致时说明只有 mtime 变了,就地采用新基线、保留草稿、不经过脏确认;
  文本确实变化时才交回宿主的带确认重载(脏保护不变);
- 记录最近一次成功保存的文本作为比较点:编辑态保存不会重新载入,单靠
  props.content 会滞后于磁盘。

tests/text-editor-conflict.spec.tsx 的横幅回调断言跟随异步探测调整:
探测失败时仍在下一微任务回落宿主重载。
2026-10-26 15:19:12 +08:00

267 lines
11 KiB
TypeScript

/**
* "Reload from disk" after a save conflict, with the host's dirty guard in
* play (#1059). Opening a file, editing it, and then touching the file (same
* bytes, new mtime) makes the next save fail with `fs-conflict`; the banner
* promises a way out. The host's reload route stops first at `window.confirm`
* whenever the draft is dirty, and some desktop shells answer that dialog with
* no UI at all (`false`) — so a reload that would only carry a new mtime must
* NOT need the confirm: it is resolved in place, baseline updated, draft kept.
*
* Real disk edits keep the old dirty protection: when the reload returns a
* different document the host's confirm still decides, and a declined confirm
* replaces nothing.
*
* The comparison is against the bytes the editor was LOADED from, not the live
* draft: after the user's edit the draft legitimately differs from disk, and
* comparing against it would classify every touch-only reload as "changed".
*/
// @vitest-environment jsdom
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import { createElement } from 'react'
import { act } from 'react-dom/test-utils'
import { EditorView } from '@codemirror/view'
import { renderRoot, setupReactAct } from './test-utils.ts'
import type { Context } from '../src/context-types.ts'
import { EditorHost } from '../src/client/EditorHost.tsx'
import { TextEditor } from '../src/client/TextEditor.tsx'
import { allLeaves, createSidebarStore, type SidebarTab } from '../src/client/state.ts'
import { createBetterSidebarService, type FileViewerProps } from '../src/client/service.ts'
setupReactAct()
vi.mock('../src/client/api.ts', async (importOriginal) => {
// Keep the real module: TextEditor narrows the save failure with
// instanceof SidebarApiError.
const actual = await importOriginal<typeof import('../src/client/api.ts')>()
return {
...actual,
api: { ...actual.api, fsRead: vi.fn(), fsWrite: vi.fn() },
}
})
import { SidebarApiError, api } from '../src/client/api.ts'
const fsRead = vi.mocked(api.fsRead)
const fsWrite = vi.mocked(api.fsWrite)
const CONTENT = 'const a = 1\n'
/** The user's unsaved edit (what the reload must NOT throw away). */
const DRAFT_PREFIX = '// draft\n'
const SECOND_PREFIX = '// second\n'
const CHANGED_CONTENT = 'const a = 2\n'
interface ViewerSpec {
id: string
ext: string
path: string
}
const CODE_VIEWER: ViewerSpec = { id: 'code', ext: 'ts', path: '/p/a.ts' }
const MARKDOWN_VIEWER: ViewerSpec = { id: 'markdown', ext: 'md', path: '/p/a.md' }
const readText = (content: string, mtimeMs: number): { kind: 'text'; content: string; size: number; mtimeMs: number } =>
({ kind: 'text', content, size: content.length, mtimeMs })
interface Harness {
container: HTMLDivElement
unmount: () => void
}
function setup(viewer: ViewerSpec = CODE_VIEWER): { ctx: Context; store: ReturnType<typeof createSidebarStore>; tab: () => SidebarTab } {
const store = createSidebarStore()
const service = createBetterSidebarService(store)
service.registerTab({ id: 'editor', title: 'Editor', dedupeKey: (tab) => tab.path, component: () => null })
service.registerFileViewer({
id: viewer.id,
exts: [viewer.ext],
priority: 0,
fetchStrategy: 'fsRead',
component: (props: FileViewerProps) => createElement(TextEditor, props),
})
store.setSession('s1')
const ctx = {
betterSidebar: service,
sessions: { list: { subscribe: () => () => {}, getSnapshot: () => ({ byId: { s1: { cwd: '/p' } }, current: 's1' }) } },
get: (name: string) => name === 'betterSidebar' ? service : undefined,
} as unknown as Context
ctx.betterSidebar.openTab({ type: 'editor', title: viewer.path.split('/').pop()!, path: viewer.path, id: `editor:${viewer.path}` })
const tab = (): SidebarTab => allLeaves(store.getSnapshot().state!.bottomSplits).flatMap(leaf => leaf.tabs)
.find(candidate => candidate.path === viewer.path)!
return { ctx, store, tab }
}
function mount(viewer: ViewerSpec = CODE_VIEWER): Harness {
const { ctx, store, tab } = setup(viewer)
const root = renderRoot(createElement(EditorHost, {
ctx,
store: store as never,
scope: { sessionId: 's1' },
tab: tab(),
expanded: [],
revealed: [],
onToggleDir: () => {},
onReferenceFile: () => {},
}))
return { container: root.container, unmount: root.unmount }
}
/** The editor's live CodeMirror view (the real document, draft included). */
function viewOf(container: HTMLElement): EditorView {
const host = container.querySelector('.cm-editor')
if (host === null) throw new Error('the mounted editor has no .cm-editor')
const view = EditorView.findFromDOM(host as HTMLElement)
if (view === null) throw new Error('the mounted editor has no CodeMirror view')
return view
}
/** Type through CodeMirror's own doc-change path (a keystroke). */
function typeText(container: HTMLElement, text: string): void {
act(() => {
viewOf(container).dispatch({ changes: { from: 0, insert: text } })
})
}
/** The user's first unsaved edit. */
function typeDraft(container: HTMLElement): void {
typeText(container, DRAFT_PREFIX)
}
function click(element: Element): void {
act(() => { element.dispatchEvent(new MouseEvent('click', { bubbles: true })) })
}
function saveButton(container: HTMLElement): HTMLButtonElement {
const button = container.querySelector('button[aria-label="Save"]')
if (button === null) throw new Error('the host header has no save button')
return button as HTMLButtonElement
}
/** One of the host header's mode-toggle buttons, by label. */
function modeButton(container: HTMLElement, label: string): HTMLButtonElement {
const button = Array.from(container.querySelectorAll('button'))
.find(candidate => candidate.textContent === label)
if (button === undefined) throw new Error(`no "${label}" mode button`)
return button as HTMLButtonElement
}
/** Save once and assert the host refused it as a conflict. */
async function saveIntoConflict(container: HTMLElement): Promise<void> {
fsWrite.mockRejectedValueOnce(new SidebarApiError('fs-conflict', 'changed on disk'))
click(saveButton(container))
await settle()
expect(container.textContent ?? '').toContain('changed on disk since it was loaded')
}
function reloadButton(container: HTMLElement): HTMLButtonElement {
const button = Array.from(container.querySelectorAll('button'))
.find(candidate => candidate.textContent === 'Reload from disk')
if (button === undefined) throw new Error('the conflict banner has no reload button')
return button as HTMLButtonElement
}
async function settle(): Promise<void> {
await act(async () => { await Promise.resolve() })
await act(async () => { await Promise.resolve() })
}
beforeEach(() => {
fsRead.mockReset()
fsWrite.mockReset()
fsWrite.mockResolvedValue({ ok: true, mtimeMs: 300 })
fsRead.mockImplementation(() => Promise.resolve(readText(CONTENT, 100)))
})
afterEach(() => {
vi.restoreAllMocks()
document.body.innerHTML = ''
})
describe('save-conflict banner: reload with a dirty draft (#1059)', () => {
it('adopts a touch-only reload in place: no confirm, draft kept, next save uses the new mtime', async () => {
const confirm = vi.spyOn(window, 'confirm').mockReturnValue(false)
const { container, unmount } = mount()
try {
await settle()
typeDraft(container)
await saveIntoConflict(container)
const before = viewOf(container)
expect(before.state.doc.toString()).toBe(`${DRAFT_PREFIX}${CONTENT}`)
// Disk still holds the loaded bytes; only the mtime moved.
fsRead.mockImplementation(() => Promise.resolve(readText(CONTENT, 200)))
click(reloadButton(container))
await settle()
// No dirty-confirm was asked, the banner cleared, and the draft is intact.
expect(confirm).not.toHaveBeenCalled()
expect(container.textContent ?? '').not.toContain('changed on disk since it was loaded')
expect(viewOf(container), 'the same editor instance stays mounted').toBe(before)
expect(viewOf(container).state.doc.toString()).toBe(`${DRAFT_PREFIX}${CONTENT}`)
// The next save carries the fresh baseline (200) and is accepted.
click(saveButton(container))
await settle()
expect(fsWrite).toHaveBeenCalledTimes(2)
expect(fsWrite.mock.calls[1]![2]).toBe(`${DRAFT_PREFIX}${CONTENT}`)
expect(fsWrite.mock.calls[1]![3]).toBe(200)
} finally {
unmount()
}
})
it('still protects a dirty draft when the reloaded bytes really changed', async () => {
const confirm = vi.spyOn(window, 'confirm').mockReturnValue(false)
const { container, unmount } = mount()
try {
await settle()
typeDraft(container)
await saveIntoConflict(container)
const before = viewOf(container)
// The file was really rewritten, not merely touched.
fsRead.mockImplementation(() => Promise.resolve(readText(CHANGED_CONTENT, 200)))
click(reloadButton(container))
await settle()
// The host's dirty guard still applies: a declined confirm replaces
// nothing, and the conflict banner stays up.
expect(confirm).toHaveBeenCalledTimes(1)
expect(viewOf(container), 'no remount on a declined reload').toBe(before)
expect(viewOf(container).state.doc.toString()).toBe(`${DRAFT_PREFIX}${CONTENT}`)
expect(container.textContent ?? '').toContain('changed on disk since it was loaded')
} finally {
unmount()
}
})
it('compares against the last SAVED bytes when an edit-mode save did not reload', async () => {
const confirm = vi.spyOn(window, 'confirm').mockReturnValue(false)
fsRead.mockImplementation(() => Promise.resolve(readText(CONTENT, 100)))
const { container, unmount } = mount(MARKDOWN_VIEWER)
try {
await settle()
// Markdown opens in preview; the host header's Edit toggle shows source.
click(modeButton(container, 'Edit'))
typeDraft(container)
// A successful save in EDIT mode does not reload (the host keeps the
// caret), so `content` stays at the bytes loaded when the tab opened.
click(saveButton(container))
await settle()
expect(fsWrite).toHaveBeenCalledTimes(1)
expect(fsWrite.mock.calls[0]![2]).toBe(`${DRAFT_PREFIX}${CONTENT}`)
// The tool touches the file: same bytes as the last SAVE, new mtime.
fsRead.mockImplementation(() => Promise.resolve(readText(`${DRAFT_PREFIX}${CONTENT}`, 200)))
typeText(container, SECOND_PREFIX)
await saveIntoConflict(container)
click(reloadButton(container))
await settle()
// In-place: no confirm, the second draft survives, the comparison point
// was the saved bytes (not the stale `content` prop).
expect(confirm).not.toHaveBeenCalled()
expect(container.textContent ?? '').not.toContain('changed on disk since it was loaded')
expect(viewOf(container).state.doc.toString()).toBe(`${SECOND_PREFIX}${DRAFT_PREFIX}${CONTENT}`)
click(saveButton(container))
await settle()
expect(fsWrite).toHaveBeenCalledTimes(3)
expect(fsWrite.mock.calls[2]![2]).toBe(`${SECOND_PREFIX}${DRAFT_PREFIX}${CONTENT}`)
expect(fsWrite.mock.calls[2]![3]).toBe(200)
} finally {
unmount()
}
})
})