|
| 1 | +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; |
| 2 | +import { registerWallHandle, resetWallHandles, stubWallHandle } from 'dormouse-lib/components/wall/wall-handles'; |
| 3 | +import { closeWorkspace, createWorkspace, moveWorkspace, renameWorkspace, resetWorkspaces, setActiveWorkspace } from 'dormouse-lib/lib/workspace-store'; |
| 4 | +import { recordToolDirty, resetToolDirty } from 'dormouse-lib/lib/tool-dirty-store'; |
| 5 | +import { cancelEditorClose, decideEditorClose, getEditorClosePrompt } from 'dormouse-lib/lib/tool-editor'; |
| 6 | +import { createTeardownFlow, _resetTeardownArbiterForTesting, type TeardownFlow } from './teardown-flow'; |
| 7 | +import { _resetQuitConfirmForTesting } from './quit-confirm-store'; |
| 8 | + |
| 9 | +const invoke = vi.hoisted(() => vi.fn(async (_command: string) => {})); |
| 10 | +vi.mock('@tauri-apps/api/core', () => ({ invoke })); |
| 11 | + |
| 12 | +const settle = () => new Promise(resolve => setTimeout(resolve, 0)); |
| 13 | + |
| 14 | +// A native teardown owns its scope from the first consent question, including |
| 15 | +// the unsaved-editor gate that comes before the running-work dialog. |
| 16 | +describe.each(['quit', 'close-window'] as const)('%s unsaved-editor consent', kind => { |
| 17 | + let flow: TeardownFlow; |
| 18 | + const proceed = vi.fn(); |
| 19 | + const gate = vi.fn(); |
| 20 | + beforeEach(() => { |
| 21 | + vi.clearAllMocks(); |
| 22 | + resetWorkspaces(); |
| 23 | + resetWallHandles(); |
| 24 | + _resetTeardownArbiterForTesting(); |
| 25 | + _resetQuitConfirmForTesting(); |
| 26 | + createWorkspace({ id: 'second', name: 'Second' }); |
| 27 | + recordToolDirty('editor', true); |
| 28 | + registerWallHandle(stubWallHandle('workspace:1', { dirtyToolIds: () => ['editor'] })); |
| 29 | + flow = createTeardownFlow({ kind, ack: 'ack', cancelCommand: 'cancel', gate: () => gate, |
| 30 | + mustConfirm: () => false, proceed }); |
| 31 | + }); |
| 32 | + afterEach(() => { |
| 33 | + flow.reset(); |
| 34 | + cancelEditorClose(); |
| 35 | + resetToolDirty(); |
| 36 | + resetWallHandles(); |
| 37 | + _resetQuitConfirmForTesting(); |
| 38 | + }); |
| 39 | + |
| 40 | + it.each(['arrival', 'departure'] as const)('cancels on a Workspace %s before discard can authorize a changed scope', async change => { |
| 41 | + flow.request({ kind }); |
| 42 | + expect(getEditorClosePrompt()).not.toBeNull(); |
| 43 | + if (change === 'arrival') createWorkspace({ id: 'third' }); |
| 44 | + else closeWorkspace('second'); |
| 45 | + await decideEditorClose('discard'); |
| 46 | + await settle(); |
| 47 | + expect(invoke).toHaveBeenCalledWith('cancel'); |
| 48 | + expect(getEditorClosePrompt()).toBeNull(); |
| 49 | + expect(proceed).not.toHaveBeenCalled(); |
| 50 | + expect(gate).not.toHaveBeenCalled(); |
| 51 | + createWorkspace({ id: 'fourth' }); |
| 52 | + expect(invoke.mock.calls.filter(([command]) => command === 'cancel')).toHaveLength(1); |
| 53 | + }); |
| 54 | + |
| 55 | + it('keeps consent through rename, focus, and reorder, then stops watching after commitment', async () => { |
| 56 | + flow.request({ kind }); |
| 57 | + const prompt = getEditorClosePrompt(); |
| 58 | + renameWorkspace('second', 'Renamed'); |
| 59 | + setActiveWorkspace('workspace:1'); |
| 60 | + moveWorkspace('second', 0); |
| 61 | + expect(getEditorClosePrompt()).toBe(prompt); |
| 62 | + await decideEditorClose('discard'); |
| 63 | + await settle(); |
| 64 | + expect(proceed).toHaveBeenCalledOnce(); |
| 65 | + createWorkspace({ id: 'third' }); |
| 66 | + expect(invoke).not.toHaveBeenCalledWith('cancel'); |
| 67 | + }); |
| 68 | + |
| 69 | + it('releases the scope watch when another window cancels', async () => { |
| 70 | + flow.request({ kind }); |
| 71 | + flow.reset(); |
| 72 | + createWorkspace({ id: 'third' }); |
| 73 | + await settle(); |
| 74 | + expect(getEditorClosePrompt()).toBeNull(); |
| 75 | + expect(invoke).not.toHaveBeenCalledWith('cancel'); |
| 76 | + expect(proceed).not.toHaveBeenCalled(); |
| 77 | + }); |
| 78 | +}); |
0 commit comments