import test from 'node:test'; import assert from 'node:assert/strict'; import { readFileSync } from 'node:fs'; import ts from 'typescript'; import { analysisPeriodLabel, isValidAnalysisPeriodContext, parseAnalysisPeriod } from '../src/analysisPeriodModel.ts'; import { analysisScopeText } from '../src/analysisScope.ts'; import { analysisMemberListPeriod, parseAnalysisMemberList } from '../src/analysisMembersModel.ts'; import { buildAnalysisNarrative } from '../src/analysisReportModel.ts'; import { parseAnalysisVisuals, renderVisualTableHtml, visualPeriodSummary } from '../src/analysisVisualsModel.ts'; const snapshotSource = readFileSync(new URL('../src/analysisSnapshot.ts', import.meta.url), 'utf8').replace(/^import .*;$/gm, '').replace(/^export /gm, ''); const snapshotIdentity = new Function(ts.transpileModule(snapshotSource, { compilerOptions: { target: ts.ScriptTarget.ES2022 }, }).outputText + '\nreturn { reportSnapshotIdentity, reportSnapshotDifferences };')(); const range = { year: 2026, monthStart: 1, monthEnd: 9 }; const visual = (period = range, overrides = {}) => ({ id: 'range-chart', type: 'line', title: '连续月份趋势', subtitle: '正式指标', unit: 'ratio', categories: [{ key: '2026-01', label: '1月' }, { key: '2026-09', label: '9月' }], series: [{ key: 'overall', label: '综合', values: [0.95, 0.94] }], evidenceRefs: ['Q1234ABCD'], requiredFields: ['composite_rate'], scope: { ...range, releaseId: 'release-a' }, period, metric: 'composite_rate', coverage: { totalRows: 2, returnedRows: 2, truncated: false }, originTurnId: 'turn-a', ...overrides, }); test('month ranges validate as a paired period and keep old year/month/quarter forms', () => { assert.deepEqual(parseAnalysisPeriod(range), range); assert.equal(analysisPeriodLabel(range), '2026年1—9月'); assert.equal(analysisPeriodLabel({ year: 2026, month: 9 }), '2026年9月'); assert.equal(analysisPeriodLabel({ year: 2026, quarter: 3 }), '2026年第3季度'); assert.equal(analysisPeriodLabel({ year: 2026 }), '2026年'); for (const invalid of [ { year: 2026, monthStart: 1 }, { year: 2026, monthEnd: 9 }, { year: 2026, monthStart: 10, monthEnd: 9 }, { year: 2026, monthStart: 0, monthEnd: 9 }, { year: 2026, monthStart: 1, monthEnd: 13 }, { year: 2026, month: 9, monthStart: 1, monthEnd: 9 }, { year: 2026, quarter: 3, monthStart: 1, monthEnd: 9 }, ]) { assert.equal(isValidAnalysisPeriodContext(invalid), false, JSON.stringify(invalid)); assert.equal(analysisPeriodLabel(invalid), ''); } }); test('scope and visual labels retain a month range through report narrative and table export', () => { const summary = parseAnalysisVisuals([visual(range, { comparisonPeriod: { year: 2025, monthStart: 1, monthEnd: 9 } })]); assert.equal(summary.visuals.length, 1); const chart = summary.visuals[0]; assert.equal(visualPeriodSummary(chart), '2025年1—9月 → 2026年1—9月'); assert.match(renderVisualTableHtml(chart), /2025年1—9月 → 2026年1—9月/); const narrative = buildAnalysisNarrative('', [chart]); assert.equal(narrative.period, '2025年1—9月 → 2026年1—9月'); assert.equal(analysisScopeText({ plan: { context: range } }), '2026年1—9月'); assert.equal(parseAnalysisVisuals([visual({ ...range, monthEnd: 0 })]).visuals.length, 0); assert.equal(parseAnalysisVisuals([visual(range, { scope: { ...range, month: 9 } })]).visuals.length, 0); }); test('exclusion and source-column filters remain visible in the bound scope summary', () => { const scope = { ...range, exclusions: { product: ['MIMO-V14'], group: ['海外组'] }, columnFilters: [{ field: 'customerCode', stages: ['black'], operator: 'not_in', values: ['000123'] }], columnLabels: { customerCode: '客户代码' }, }; const chart = parseAnalysisVisuals([visual(range, { scope, evidenceRefs: ['Q1234ABCD'] })]).visuals[0]; const turn = { id: 'turn-a', question: '看MIMO-V14趋势', plan: { context: scope }, visuals: [chart] }; const text = analysisScopeText(turn); assert.match(text, /排除产品:MIMO-V14/); assert.match(text, /排除业务分组:海外组/); assert.match(text, /黑片 客户代码排除000123/); }); test('member-list snapshots accept and label ranges while keeping single-month snapshots compatible', () => { const base = { schemaVersion: 1, releaseId: 'release-a', period: range, fields: [{ key: 'product', label: '产品' }], rows: [{ values: { product: 'A' }, stages: ['black'] }], totalCount: 1, rowsReturned: 1, queryComplete: true, }; const parsed = parseAnalysisMemberList(base); assert.ok(parsed); assert.equal(analysisMemberListPeriod(parsed.period), '2026年1—9月'); assert.equal(parseAnalysisMemberList({ ...base, period: { year: 2026, month: 9 } }).period.month, 9); assert.equal(parseAnalysisMemberList({ ...base, period: { year: 2026, monthStart: 10, monthEnd: 9 } }), null); }); test('report snapshot identity retains ranges in visuals and normalizes nested versus flat turn fields', () => { const chart = visual(range, { comparisonPeriod: { year: 2025, monthStart: 1, monthEnd: 9 } }); const turn = { id: 'turn-a', question: '总结', result: { answer: '报告正文', visuals: [chart], results: [], memberList: null } }; const flattened = { id: 'turn-a', question: '总结', ...turn.result }; assert.equal(snapshotIdentity.reportSnapshotIdentity(turn), snapshotIdentity.reportSnapshotIdentity(flattened)); const changed = { ...turn, result: { ...turn.result, visuals: [visual({ ...range, monthEnd: 8 })] } }; assert.notEqual(snapshotIdentity.reportSnapshotIdentity(turn), snapshotIdentity.reportSnapshotIdentity(changed)); assert.deepEqual(snapshotIdentity.reportSnapshotDifferences(changed, turn), ['visuals']); const scoped = visual(range, { scope: { ...range, exclusions: { product: ['MIMO-V14'] }, columnFilters: [{ field: 'customerCode', stages: ['black'], operator: 'not_in', values: ['000123'] }] } }); const withScope = { ...turn, result: { ...turn.result, visuals: [scoped] } }; const changedExclusion = { ...withScope, result: { ...withScope.result, visuals: [visual(range, { scope: { ...scoped.scope, exclusions: { product: ['MIMO-V13'] } } })] } }; assert.notEqual(snapshotIdentity.reportSnapshotIdentity(withScope), snapshotIdentity.reportSnapshotIdentity(changedExclusion)); assert.deepEqual(snapshotIdentity.reportSnapshotDifferences(withScope, changedExclusion), ['visuals']); const changedColumnRule = { ...withScope, result: { ...withScope.result, visuals: [visual(range, { scope: { ...scoped.scope, columnFilters: [{ ...scoped.scope.columnFilters[0], values: ['000999'] }] } })] } }; assert.notEqual(snapshotIdentity.reportSnapshotIdentity(withScope), snapshotIdentity.reportSnapshotIdentity(changedColumnRule)); assert.deepEqual(snapshotIdentity.reportSnapshotDifferences(withScope, changedColumnRule), ['visuals']); });