import { it, expect } from 'vitest' import { parseQADashboard } from './qaDashboardData' import { dashboardFixture } from './qaDashboard.fixture' import { historicalTrend } from './qaHistoricalTrend' it('rejects a closed-week cumulative point silently cut short before its required cutoff', () => { const broken = structuredClone(live) broken[1].cutoff_at = '2026-10-11T23:59:59+08:00' expect(() => parseQADashboard({ ...dashboardFixture, cumulative_weeks: broken })).toThrow() }) const rates = (n: number, d: number) => ({ numerator: n, denominator: d, percent: d ? (100 * n) / d : null }) const live = [ { week: '2026WK40', week_start: '2026-09-28T00:00:00+08:00', week_end: '2026-10-05T00:00:00+08:00', cutoff_at: '2026-10-05T00:00:00+08:00', calculated_at: '2026-10-18T10:00:00Z', records: 2, coverage: rates(1, 2), correctness: rates(1, 2), source: 'master_live_cumulative_v1', policy_version: 'weekly-strict-yes-over-yes-no-v1', }, { week: '2026WK41', week_start: '2026-10-05T00:00:00+08:00', week_end: '2026-10-12T00:00:00+08:00', cutoff_at: '2026-10-12T00:00:00+08:00', calculated_at: '2026-10-18T10:00:00Z', records: 3, coverage: rates(1, 3), correctness: rates(2, 3), source: 'master_live_cumulative_v1', policy_version: 'weekly-strict-yes-over-yes-no-v1', }, ] it('prioritizes live cumulative weeks over an overlapping report without mutating historical evidence', () => { const wire = { ...dashboardFixture, cumulative_weeks: live, reported_weeks: [ { week: '2026WK40', week_start: live[0].week_start, week_end: live[0].week_end, coverage_percent: 99, correctness_percent: 99, source: 'user_provided_history', }, ], } const before = JSON.stringify(wire), parsed = parseQADashboard(wire) expect(historicalTrend(parsed, false)[0].source).toBe('master_live_cumulative_v1') expect(historicalTrend(parsed, false)[0].value).toBe(50) expect(parsed.reported_weeks[0].coverage_percent).toBe(99) expect(JSON.stringify(wire)).toBe(before) }) it('shows current cumulative counts instead of substituting old frozen-week values', () => { const wire = { ...dashboardFixture, cumulative_weeks: live } const before = JSON.stringify(wire) const parsed = parseQADashboard(wire) expect((parsed as unknown as { cumulative_weeks: unknown[] }).cumulative_weeks).toHaveLength(2) expect(historicalTrend(parsed, false).map((p) => p.value)).toEqual([50, 100 / 3]) expect(historicalTrend(parsed, true).map((p) => p.value)).toEqual([50, 200 / 3]) expect(historicalTrend(parsed, true)[1].source).toBe('master_live_cumulative_v1') expect(historicalTrend(parsed, true)[1].hint).toContain('2/3') expect(JSON.stringify(wire)).toBe(before) })