| 1 | import { describe, expect, it } from 'vitest' |
| 2 | import { |
| 3 | migrateLegacyPageOutlinesToSourceSkeletons, |
| 4 | resolveOutlinesForPages, |
| 5 | resolvePageContentOutline |
| 6 | } from '../../../src/main/session/page-outline-utils' |
| 7 | import type { |
| 8 | GenerationPageRecord, |
| 9 | PPTDatabase, |
| 10 | Session, |
| 11 | SessionPageRecord, |
| 12 | SourcePageSkeletonRecord |
| 13 | } from '../../../src/main/db/database' |
| 14 | |
| 15 | const makeSnapshot = ( |
| 16 | pageId: string, |
| 17 | pageNumber: number, |
| 18 | contentOutline: string |
| 19 | ): GenerationPageRecord => ({ |
| 20 | id: `run:${pageId}`, |
| 21 | run_id: 'run', |
| 22 | session_id: 'session', |
| 23 | page_id: pageId, |
| 24 | page_number: pageNumber, |
| 25 | title: `P${pageNumber}`, |
| 26 | content_outline: contentOutline, |
| 27 | layout_intent: null, |
| 28 | html_path: null, |
| 29 | status: 'completed', |
| 30 | error: null, |
| 31 | retry_count: 0, |
| 32 | created_at: 1, |
| 33 | updated_at: 1 |
| 34 | }) |
| 35 | |
| 36 | const makeSessionPage = ( |
| 37 | id: string, |
| 38 | fileSlug: string, |
| 39 | pageNumber: number |
| 40 | ): Pick<SessionPageRecord, 'id' | 'file_slug' | 'legacy_page_id' | 'page_number'> => ({ |
| 41 | id, |
| 42 | file_slug: fileSlug, |
| 43 | legacy_page_id: null, |
| 44 | page_number: pageNumber |
| 45 | }) |
| 46 | |
| 47 | const makeSourceSkeleton = ( |
| 48 | pageNumber: number, |
| 49 | sourceHeading: string, |
| 50 | reason: string | null = null |
| 51 | ): SourcePageSkeletonRecord => ({ |
| 52 | id: `session:${pageNumber}`, |
| 53 | session_id: 'session', |
| 54 | page_number: pageNumber, |
| 55 | title: `P${pageNumber}`, |
| 56 | role: 'content', |
| 57 | source_document_path: '/source.md', |
| 58 | source_document_name: 'source.md', |
| 59 | source_heading: sourceHeading, |
| 60 | heading_level: 1, |
| 61 | line_start: pageNumber, |
| 62 | line_end: pageNumber, |
| 63 | reason, |
| 64 | confidence: 'high', |
| 65 | created_at: 1, |
| 66 | updated_at: 1 |
| 67 | }) |
| 68 | |
| 69 | describe('resolvePageContentOutline', () => { |
| 70 | it('extracts explicit Chinese page sections', () => { |
| 71 | const outline = [ |
| 72 | '第 1 页:项目背景', |
| 73 | '- 市场变化', |
| 74 | '第 2 页:方案策略', |
| 75 | '- 分阶段落地', |
| 76 | '- 明确负责人', |
| 77 | '第 3 页:总结' |
| 78 | ].join('\n') |
| 79 | |
| 80 | expect(resolvePageContentOutline(outline, 2)).toBe('方案策略 分阶段落地 明确负责人') |
| 81 | }) |
| 82 | |
| 83 | it('extracts explicit English page sections without leaking the page number', () => { |
| 84 | const outline = ['Page 1: Context', 'Page 2', '- Delivery plan', '- Owner mapping'].join('\n') |
| 85 | |
| 86 | expect(resolvePageContentOutline(outline, 2)).toBe('Delivery plan Owner mapping') |
| 87 | }) |
| 88 | |
| 89 | it('extracts Chinese markdown page sections from the new thinking outline format', () => { |
| 90 | const outline = [ |
| 91 | '## 第 1 页:封面', |
| 92 | '- 页面角色:封面', |
| 93 | '- 页面目的:建立主题', |
| 94 | '', |
| 95 | '用一句话说明汇报对象。', |
| 96 | '- 项目名称', |
| 97 | '- 汇报日期', |
| 98 | '', |
| 99 | '## 第 2 页:技术演进', |
| 100 | '- Role: content', |
| 101 | '- Objective: 说明关键趋势', |
| 102 | '', |
| 103 | '梳理核心技术变化和工程影响。', |
| 104 | '- 架构趋势', |
| 105 | '- 部署趋势' |
| 106 | ].join('\n') |
| 107 | |
| 108 | expect(resolvePageContentOutline(outline, 2)).toBe( |
| 109 | '技术演进 说明关键趋势 梳理核心技术变化和工程影响。 架构趋势 部署趋势' |
| 110 | ) |
| 111 | }) |
| 112 | |
| 113 | it('stops explicit page extraction before following global brief sections', () => { |
| 114 | const outline = [ |
| 115 | '每页要点:', |
| 116 | '第 1 页:背景与目标', |
| 117 | '页面角色:内容页', |
| 118 | '页面目的:解释项目背景', |
| 119 | '必须保留:', |
| 120 | '- 预算数字', |
| 121 | '风格:', |
| 122 | '- 克制' |
| 123 | ].join('\n') |
| 124 | |
| 125 | expect(resolvePageContentOutline(outline, 1)).toBe('背景与目标 解释项目背景') |
| 126 | }) |
| 127 | |
| 128 | it('extracts numbered items inside an outline section', () => { |
| 129 | const outline = [ |
| 130 | '推荐大纲', |
| 131 | '1. 封面:一句话说明主题', |
| 132 | '2. 现状分析:痛点、数据、机会', |
| 133 | ' - 补充关键指标', |
| 134 | '3. 行动计划:节奏和责任' |
| 135 | ].join('\n') |
| 136 | |
| 137 | expect(resolvePageContentOutline(outline, 2)).toBe('现状分析:痛点、数据、机会 补充关键指标') |
| 138 | }) |
| 139 | |
| 140 | it('does not split ordinary per-page numbered bullets by default', () => { |
| 141 | const outline = ['1. 保留核心指标', '2. 加一张趋势图', '3. 结尾突出风险'].join('\n') |
| 142 | |
| 143 | expect(resolvePageContentOutline(outline, 2)).toBe( |
| 144 | '1. 保留核心指标 2. 加一张趋势图 3. 结尾突出风险' |
| 145 | ) |
| 146 | }) |
| 147 | |
| 148 | it('can split a shared unheaded numbered deck outline', () => { |
| 149 | const outline = ['1. 背景与目标', '2. 方案设计', '3. 里程碑'].join('\n') |
| 150 | |
| 151 | expect(resolvePageContentOutline(outline, 2, { allowUnheadedNumberedOutline: true })).toBe( |
| 152 | '方案设计' |
| 153 | ) |
| 154 | }) |
| 155 | }) |
| 156 | |
| 157 | describe('resolveOutlinesForPages', () => { |
| 158 | it('uses source page skeletons as the page outline source', async () => { |
| 159 | const db = { |
| 160 | listSourcePageSkeletons: async () => [ |
| 161 | makeSourceSkeleton(1, '背景与目标'), |
| 162 | makeSourceSkeleton(2, '方案设计', '明确交付节奏'), |
| 163 | makeSourceSkeleton(3, '里程碑') |
| 164 | ] |
| 165 | } as Pick<PPTDatabase, 'listSourcePageSkeletons'> as PPTDatabase |
| 166 | |
| 167 | const result = await resolveOutlinesForPages(db, 'session', [ |
| 168 | makeSessionPage('session-a', 'page-a', 1), |
| 169 | makeSessionPage('session-b', 'page-b', 2), |
| 170 | makeSessionPage('session-c', 'page-c', 3) |
| 171 | ]) |
| 172 | |
| 173 | expect(result.get('session-a')).toBe('背景与目标') |
| 174 | expect(result.get('session-b')).toBe('方案设计 明确交付节奏') |
| 175 | expect(result.get('session-c')).toBe('里程碑') |
| 176 | }) |
| 177 | }) |
| 178 | |
| 179 | describe('migrateLegacyPageOutlinesToSourceSkeletons', () => { |
| 180 | it('migrates shared generation outlines into source page skeleton rows', async () => { |
| 181 | const deckOutline = ['1. 背景与目标', '2. 方案设计', '3. 里程碑'].join('\n') |
| 182 | const replaced: Parameters<PPTDatabase['replaceSourcePageSkeletons']>[0][] = [] |
| 183 | const db = { |
| 184 | listSourcePageSkeletons: async () => [], |
| 185 | getSession: async () => |
| 186 | ({ |
| 187 | id: 'session', |
| 188 | title: '迁移测试', |
| 189 | reference_document_path: '/docs/source.md', |
| 190 | referenceDocumentPath: '/docs/source.md' |
| 191 | }) as Session, |
| 192 | listSessionPages: async () => [ |
| 193 | makeSessionPage('session-a', 'page-a', 1) as SessionPageRecord, |
| 194 | makeSessionPage('session-b', 'page-b', 2) as SessionPageRecord, |
| 195 | makeSessionPage('session-c', 'page-c', 3) as SessionPageRecord |
| 196 | ], |
| 197 | listLatestGenerationPageSnapshot: async () => [ |
| 198 | makeSnapshot('page-a', 1, deckOutline), |
| 199 | makeSnapshot('page-b', 2, deckOutline), |
| 200 | makeSnapshot('page-c', 3, deckOutline) |
| 201 | ], |
| 202 | replaceSourcePageSkeletons: async (args) => { |
| 203 | replaced.push(args) |
| 204 | } |
| 205 | } as Pick< |
| 206 | PPTDatabase, |
| 207 | | 'listSourcePageSkeletons' |
| 208 | | 'getSession' |
| 209 | | 'listSessionPages' |
| 210 | | 'listLatestGenerationPageSnapshot' |
| 211 | | 'replaceSourcePageSkeletons' |
| 212 | > as PPTDatabase |
| 213 | |
| 214 | const result = await migrateLegacyPageOutlinesToSourceSkeletons(db, 'session') |
| 215 | |
| 216 | expect(result).toEqual({ migrated: true, migratedCount: 3, existingCount: 0 }) |
| 217 | expect(replaced[0]?.sourceDocumentPath).toBe('/docs/source.md') |
| 218 | expect(replaced[0]?.items.map((item) => item.sourceHeading)).toEqual([ |
| 219 | '背景与目标', |
| 220 | '方案设计', |
| 221 | '里程碑' |
| 222 | ]) |
| 223 | }) |
| 224 | |
| 225 | it('does not overwrite existing source page skeletons', async () => { |
| 226 | const db = { |
| 227 | listSourcePageSkeletons: async () => [makeSourceSkeleton(1, '已有大纲')] |
| 228 | } as Pick<PPTDatabase, 'listSourcePageSkeletons'> as PPTDatabase |
| 229 | |
| 230 | await expect(migrateLegacyPageOutlinesToSourceSkeletons(db, 'session')).resolves.toEqual({ |
| 231 | migrated: false, |
| 232 | migratedCount: 0, |
| 233 | existingCount: 1 |
| 234 | }) |
| 235 | }) |
| 236 | }) |
| 237 |