From ae71cd05e649ce8200651ffb362910a61bcc67f1 Mon Sep 17 00:00:00 2001 From: unsc9 <438590997@qq.com> Date: Thu, 29 Oct 2026 09:10:44 +0800 Subject: [PATCH] test(editor): cover the three no-prompt switch branches --- tests/editor-host.spec.tsx | 121 ++++++++++++++++++++++++++++++++----- 1 file changed, 105 insertions(+), 16 deletions(-) diff --git a/tests/editor-host.spec.tsx b/tests/editor-host.spec.tsx index 2b9dfe50..6d8dda3e 100644 --- a/tests/editor-host.spec.tsx +++ b/tests/editor-host.spec.tsx @@ -338,6 +338,25 @@ function typeAndCommit(input: HTMLInputElement, value: string): void { }) } +/** Register a viewer that reports an unsaved draft on mount; its report + * feeds the editor-dirty registry the switch/close guards read. */ +function registerDirtyViewer(service: ReturnType): void { + const DirtyViewer = (viewerProps: FileViewerProps): ReactNode => { + useEffect(() => { + viewerProps.onToolbarState?.({ modes: true, mode: 'edit', dirty: true, editable: true, saveState: 'idle' }) + // Mount-only: this instance's draft is the report above. + // eslint-disable-next-line react-hooks/exhaustive-deps + }, []) + return null + } + service.registerFileViewer({ + id: 'test:dirty', + exts: ['fake'], + fetchStrategy: 'none', + component: DirtyViewer, + }) +} + describe('EditorHost (files window)', () => { it('a path-less tab renders the empty-state hint with the tree panel open', () => { const { store, ctx, homeTab } = setup() @@ -411,22 +430,7 @@ describe('EditorHost (files window)', () => { it('in-place mode: switching files confirms before dropping an unsaved draft', () => { const { store, ctx } = setup() const service = ctx.betterSidebar - // A viewer holding a draft: its toolbar report feeds the host's dirty - // registry, which is what the switch guard reads. - const DirtyViewer = (viewerProps: FileViewerProps): ReactNode => { - useEffect(() => { - viewerProps.onToolbarState?.({ modes: true, mode: 'edit', dirty: true, editable: true, saveState: 'idle' }) - // Mount-only: this instance's draft is the report above. - // eslint-disable-next-line react-hooks/exhaustive-deps - }, []) - return null - } - service.registerFileViewer({ - id: 'test:dirty', - exts: ['fake'], - fetchStrategy: 'none', - component: DirtyViewer, - }) + registerDirtyViewer(service) const tabId = 'editor:/tmp/a.fake' service.openTab({ type: 'editor', title: 'a.fake', path: '/tmp/a.fake', id: tabId }) const fileTab = (): SidebarTab => @@ -453,6 +457,91 @@ describe('EditorHost (files window)', () => { } }) + it('in-place mode: re-opening the same path never asks to discard the draft', () => { + const { store, ctx } = setup() + registerDirtyViewer(ctx.betterSidebar) + const tabId = 'editor:/tmp/a.fake' + ctx.betterSidebar.openTab({ type: 'editor', title: 'a.fake', path: '/tmp/a.fake', id: tabId }) + const fileTab = (): SidebarTab => + allLeaves(store.getSnapshot().state!.bottomSplits).flatMap(leaf => leaf.tabs) + .find(tab => tab.id === tabId)! + store.setPrefs({ ...store.getPrefs(), editorExplorer: true }) + const { container, unmount } = mountHost(ctx, store, fileTab) + const confirm = vi.spyOn(window, 'confirm') + try { + typeAndCommit(container.querySelector('input')!, '/tmp/a.fake') + // The same path is not a switch: no prompt, same tab, same file. + expect(confirm).not.toHaveBeenCalled() + expect(fileTab().id).toBe(tabId) + expect(fileTab().path).toBe('/tmp/a.fake') + } finally { + confirm.mockRestore() + unmount() + } + }) + + it('in-place mode: a file another native type claims never asks about the draft', () => { + const { store, ctx } = setup() + const service = ctx.betterSidebar + registerDirtyViewer(service) + const resources: string[] = [] + const claimedCtx = { + betterSidebar: service, + get: (name: string) => { + if (name === 'betterSidebar') return service + if (name === 'sidebarRightTabs') { + return { candidates: (address: string) => address.endsWith('.drawio') ? [{ kind: 'drawio' }] : [{ kind: 'editor' }] } + } + if (name === 'sidebarRight') { + return { openResource: (address: string) => { resources.push(address) } } + } + return undefined + }, + sessions: ctx.sessions, + } as unknown as Context + service.openTab({ type: 'editor', title: 'a.fake', path: '/tmp/a.fake', id: 'editor:/tmp/a.fake', meta: { treeOpen: false } }) + const fileTab = (): SidebarTab => + allLeaves(store.getSnapshot().state!.bottomSplits).flatMap(leaf => leaf.tabs) + .find(tab => tab.path === '/tmp/a.fake')! + store.setPrefs({ ...store.getPrefs(), editorExplorer: true }) + const { container, unmount } = mountHost(claimedCtx, store, fileTab) + const confirm = vi.spyOn(window, 'confirm') + try { + typeAndCommit(container.querySelector('input[placeholder^="File path"]')!, '/tmp/board.drawio') + // The claiming type opens the file elsewhere; this tab keeps its draft, + // so there is nothing to confirm. + expect(confirm).not.toHaveBeenCalled() + expect(resources).toHaveLength(1) + expect(fileTab().path).toBe('/tmp/a.fake') + } finally { + confirm.mockRestore() + unmount() + } + }) + + it('split mode: opening another file never asks about the source draft', () => { + const { store, ctx } = setup() + registerDirtyViewer(ctx.betterSidebar) + ctx.betterSidebar.openTab({ type: 'editor', title: 'a.fake', path: '/tmp/a.fake', id: 'editor:/tmp/a.fake' }) + const fileTab = (): SidebarTab => + allLeaves(store.getSnapshot().state!.bottomSplits).flatMap(leaf => leaf.tabs) + .find(tab => tab.path === '/tmp/a.fake')! + store.setPrefs({ ...store.getPrefs(), editorExplorer: false }) + const { container, unmount } = mountHost(ctx, store, fileTab) + const confirm = vi.spyOn(window, 'confirm') + try { + typeAndCommit(container.querySelector('input[placeholder^="File path"]')!, '/tmp/b.fake') + const tabs = allLeaves(store.getSnapshot().state!.bottomSplits).flatMap(leaf => leaf.tabs) + // Split mode opens a per-path tab; the source tab and its draft stay put. + expect(confirm).not.toHaveBeenCalled() + expect(fileTab().path).toBe('/tmp/a.fake') + expect(tabs.some(tab => tab.path === '/tmp/b.fake')).toBe(true) + } finally { + confirm.mockRestore() + unmount() + } + }) + it('in-place mode: a file another native type claims opens THAT type\'s tab, not the editor (#695)', () => { const { store, ctx } = setup() const service = ctx.betterSidebar