| 1 | import { describe, expect, it, vi } from 'vitest' |
| 2 | import { |
| 3 | revealGenerationWindow, |
| 4 | shouldRevealGenerationWindow |
| 5 | } from '../../../src/main/generation/generation-window-policy' |
| 6 | import type { GenerateChunkEvent } from '../../../src/shared/generation' |
| 7 | |
| 8 | const terminalEvent = ( |
| 9 | type: 'run_completed' | 'run_error', |
| 10 | options: { |
| 11 | cancelled?: boolean |
| 12 | activityKind?: 'edit' | 'style-switch' | 'single-page-retry' | 'addPage' |
| 13 | } = {} |
| 14 | ): GenerateChunkEvent => |
| 15 | ({ |
| 16 | type, |
| 17 | payload: { |
| 18 | runId: 'run-1', |
| 19 | ...(type === 'run_completed' |
| 20 | ? { totalPages: 3 } |
| 21 | : { message: 'Generation failed', cancelled: options.cancelled }), |
| 22 | activityKind: options.activityKind |
| 23 | } |
| 24 | }) as GenerateChunkEvent |
| 25 | |
| 26 | describe('generation window policy', () => { |
| 27 | it('reveals the app only for terminal deck generation states', () => { |
| 28 | expect( |
| 29 | shouldRevealGenerationWindow(terminalEvent('run_completed'), { |
| 30 | runId: 'run-1', |
| 31 | mode: 'generate' |
| 32 | }) |
| 33 | ).toBe(true) |
| 34 | expect( |
| 35 | shouldRevealGenerationWindow(terminalEvent('run_error'), { |
| 36 | runId: 'run-1', |
| 37 | mode: 'retry' |
| 38 | }) |
| 39 | ).toBe(true) |
| 40 | expect( |
| 41 | shouldRevealGenerationWindow(terminalEvent('run_completed'), { |
| 42 | runId: 'run-1', |
| 43 | mode: 'edit' |
| 44 | }) |
| 45 | ).toBe(false) |
| 46 | expect( |
| 47 | shouldRevealGenerationWindow(terminalEvent('run_error', { cancelled: true }), { |
| 48 | runId: 'run-1', |
| 49 | mode: 'generate' |
| 50 | }) |
| 51 | ).toBe(false) |
| 52 | expect( |
| 53 | shouldRevealGenerationWindow(terminalEvent('run_completed'), { |
| 54 | runId: 'newer-run', |
| 55 | mode: 'generate' |
| 56 | }) |
| 57 | ).toBe(false) |
| 58 | }) |
| 59 | |
| 60 | it('does not reveal the app for page progress or unrelated activity', () => { |
| 61 | expect( |
| 62 | shouldRevealGenerationWindow({ |
| 63 | type: 'page_generated', |
| 64 | payload: { |
| 65 | runId: 'run-1', |
| 66 | pageNumber: 1, |
| 67 | pageId: 'page-1', |
| 68 | title: 'Page 1', |
| 69 | html: '', |
| 70 | stage: 'rendering', |
| 71 | label: 'Generated' |
| 72 | } |
| 73 | } as GenerateChunkEvent) |
| 74 | ).toBe(false) |
| 75 | expect( |
| 76 | shouldRevealGenerationWindow( |
| 77 | terminalEvent('run_completed', { activityKind: 'style-switch' }) |
| 78 | ) |
| 79 | ).toBe(false) |
| 80 | }) |
| 81 | |
| 82 | it('restores, shows, and focuses the main window when needed', () => { |
| 83 | const window = { |
| 84 | isDestroyed: vi.fn(() => false), |
| 85 | isMinimized: vi.fn(() => true), |
| 86 | isVisible: vi.fn(() => false), |
| 87 | restore: vi.fn(), |
| 88 | show: vi.fn(), |
| 89 | focus: vi.fn() |
| 90 | } |
| 91 | |
| 92 | revealGenerationWindow(window as never) |
| 93 | |
| 94 | expect(window.restore).toHaveBeenCalledOnce() |
| 95 | expect(window.show).toHaveBeenCalledOnce() |
| 96 | expect(window.focus).toHaveBeenCalledOnce() |
| 97 | }) |
| 98 | }) |
| 99 |