| 1 | import { describe, expect, it, vi } from 'vitest' |
| 2 | |
| 3 | vi.mock('electron', () => ({ ipcMain: { handle: vi.fn() } })) |
| 4 | vi.mock('electron-log/main.js', () => ({ default: { error: vi.fn(), warn: vi.fn() } })) |
| 5 | vi.mock('../../../src/main/generation/edit-deck-allpage-flow', () => ({ |
| 6 | executeDeckAllPageEditGeneration: vi.fn() |
| 7 | })) |
| 8 | vi.mock('../../../src/main/generation/edit-flow', () => ({ resolveEditContext: vi.fn() })) |
| 9 | vi.mock('../../../src/main/generation/generation-utils', () => ({ |
| 10 | createEmitAssistantMessage: vi.fn() |
| 11 | })) |
| 12 | vi.mock('../../../src/main/edit-jobs/edit-job-finalization', () => ({ |
| 13 | settleEditJobFailure: vi.fn(), |
| 14 | settleEditJobSuccess: vi.fn() |
| 15 | })) |
| 16 | |
| 17 | import { DeckEditJobService } from '../../../src/main/edit-jobs/deck-edit-job-service' |
| 18 | import { JobCoordinator } from '../../../src/main/agent-runtime' |
| 19 | |
| 20 | describe('DeckEditJobService state recovery', () => { |
| 21 | it('restores the retry payload and failed page count from a finished partial job', async () => { |
| 22 | const sessionId = 'session-1' |
| 23 | const runId = 'deck-edit-run-1' |
| 24 | const retryPayload = { |
| 25 | sessionId, |
| 26 | modelConfigId: 'model-1', |
| 27 | userMessage: 'Unify the title style', |
| 28 | type: 'page' as const, |
| 29 | chatType: 'main' as const, |
| 30 | selectPageIds: ['page-1', 'page-2'] |
| 31 | } |
| 32 | const ctx = { |
| 33 | sessionRunStates: new Map(), |
| 34 | db: { |
| 35 | getLatestSessionJob: vi.fn().mockResolvedValue({ |
| 36 | id: runId, |
| 37 | status: 'finished', |
| 38 | total_pages: 2, |
| 39 | created_at: 100, |
| 40 | updated_at: 120, |
| 41 | abort_reason: null |
| 42 | }), |
| 43 | getGenerationRun: vi.fn().mockResolvedValue({ |
| 44 | id: runId, |
| 45 | status: 'partial', |
| 46 | total_pages: 2, |
| 47 | error: 'page-2 failed', |
| 48 | metadata: JSON.stringify({ retryPayload }) |
| 49 | }), |
| 50 | listGenerationPages: vi.fn().mockResolvedValue([ |
| 51 | { page_id: 'page-1', status: 'completed' }, |
| 52 | { page_id: 'page-2', status: 'failed' } |
| 53 | ]) |
| 54 | } |
| 55 | } |
| 56 | const service = new DeckEditJobService(ctx as never, new JobCoordinator()) |
| 57 | |
| 58 | await expect(service.getState(sessionId)).resolves.toMatchObject({ |
| 59 | sessionId, |
| 60 | runId, |
| 61 | status: 'failed', |
| 62 | hasActiveRun: false, |
| 63 | completedPageCount: 1, |
| 64 | failedPageCount: 1, |
| 65 | retryPayload |
| 66 | }) |
| 67 | }) |
| 68 | }) |
| 69 |