| 1 | import { describe, expect, it } from 'vitest' |
| 2 | import { |
| 3 | buildGenerationActivityLogContent, |
| 4 | resolveGenerationActivityStatus |
| 5 | } from '../../../src/renderer/src/components/session-detail/modal/generationActivityLog' |
| 6 | |
| 7 | const labels = { |
| 8 | processing: '处理中', |
| 9 | completed: '已完成', |
| 10 | runFailed: '任务失败', |
| 11 | pageFailed: (page: number, title: string) => `第 ${page} 页失败:${title}`, |
| 12 | pageContext: (page: number, title: string) => `第 ${page} 页:${title}`, |
| 13 | partialCompleted: (count: number) => `仍有 ${count} 页失败`, |
| 14 | unknownError: '未知错误' |
| 15 | } |
| 16 | |
| 17 | describe('generation activity log', () => { |
| 18 | it('keeps every non-terminal event in the generating state', () => { |
| 19 | expect( |
| 20 | resolveGenerationActivityStatus( |
| 21 | { |
| 22 | type: 'llm_status', |
| 23 | payload: { |
| 24 | runId: 'run-1', |
| 25 | stage: 'editing', |
| 26 | label: '已完成', |
| 27 | progress: 80 |
| 28 | } |
| 29 | }, |
| 30 | 0 |
| 31 | ) |
| 32 | ).toBe('running') |
| 33 | expect( |
| 34 | resolveGenerationActivityStatus( |
| 35 | { |
| 36 | type: 'page_updated', |
| 37 | payload: { |
| 38 | runId: 'run-1', |
| 39 | stage: 'editing', |
| 40 | label: 'P3 编辑完成', |
| 41 | progress: 80, |
| 42 | id: 'id-3', |
| 43 | pageNumber: 3, |
| 44 | title: '第三页', |
| 45 | html: '<div />', |
| 46 | pageId: 'page-3', |
| 47 | htmlPath: '/tmp/page-3.html' |
| 48 | } |
| 49 | }, |
| 50 | 0 |
| 51 | ) |
| 52 | ).toBe('running') |
| 53 | }) |
| 54 | |
| 55 | it('changes status only for run-level terminal events', () => { |
| 56 | expect( |
| 57 | resolveGenerationActivityStatus( |
| 58 | { type: 'run_completed', payload: { runId: 'run-1', totalPages: 3 } }, |
| 59 | 0 |
| 60 | ) |
| 61 | ).toBe('completed') |
| 62 | expect( |
| 63 | resolveGenerationActivityStatus( |
| 64 | { type: 'run_completed', payload: { runId: 'run-1', totalPages: 3 } }, |
| 65 | 1 |
| 66 | ) |
| 67 | ).toBe('failed') |
| 68 | expect( |
| 69 | resolveGenerationActivityStatus( |
| 70 | { type: 'run_error', payload: { runId: 'run-1', message: 'cancelled', cancelled: true } }, |
| 71 | 0 |
| 72 | ) |
| 73 | ).toBe('cancelled') |
| 74 | }) |
| 75 | |
| 76 | it('shows the failed page and its concrete error', () => { |
| 77 | expect( |
| 78 | buildGenerationActivityLogContent( |
| 79 | { |
| 80 | type: 'page_failed', |
| 81 | payload: { |
| 82 | runId: 'run-1', |
| 83 | stage: 'editing', |
| 84 | label: '失败', |
| 85 | pageNumber: 3, |
| 86 | pageId: 'page-3', |
| 87 | title: '市场分析', |
| 88 | error: '模型请求超时' |
| 89 | } |
| 90 | }, |
| 91 | labels |
| 92 | ) |
| 93 | ).toEqual({ label: '第 3 页失败:市场分析', detail: '模型请求超时' }) |
| 94 | }) |
| 95 | |
| 96 | it('shows the full run failure reason', () => { |
| 97 | expect( |
| 98 | buildGenerationActivityLogContent( |
| 99 | { |
| 100 | type: 'run_error', |
| 101 | payload: { runId: 'run-1', message: 'API rate limit: 429' } |
| 102 | }, |
| 103 | labels |
| 104 | ) |
| 105 | ).toEqual({ label: '任务失败', detail: 'API rate limit: 429' }) |
| 106 | }) |
| 107 | |
| 108 | it('adds page context to progress events without a detail', () => { |
| 109 | expect( |
| 110 | buildGenerationActivityLogContent( |
| 111 | { |
| 112 | type: 'page_started', |
| 113 | payload: { |
| 114 | runId: 'run-1', |
| 115 | stage: 'editing', |
| 116 | label: '正在生成页面', |
| 117 | pageNumber: 2, |
| 118 | pageId: 'page-2', |
| 119 | title: '产品方案' |
| 120 | } |
| 121 | }, |
| 122 | labels |
| 123 | ) |
| 124 | ).toEqual({ label: '正在生成页面', detail: '第 2 页:产品方案' }) |
| 125 | }) |
| 126 | }) |
| 127 |