import { describe, expect, test } from 'bun:test' import { readFileSync } from 'node:fs' import { resolve } from 'node:path' import { applyApprovedCandidateToDraft, ensureCompleteConfiguration, formatSourceOptionLabel, hasMixedShareSourceRow, normalizeDraftForPublish, reportIsUsed, sourceForObservation, type ComputeCandidateItem, type ComputeConfiguration, } from '../src/admin/pages/compute-comparison-model' const defaultsPath = resolve(import.meta.dir, '../../backend/app/services/compute_history_defaults.json') const bundledDefaults = JSON.parse(readFileSync(defaultsPath, 'utf8')) as ComputeConfiguration function makeCandidate(overrides: Partial = {}): ComputeCandidateItem { return { id: 42, status: 'approved', candidate_type: 'observation', proposed_payload: { year: 2025, metric: 'global_share', source_id: 'caict-preview-2025', values: { US: 45, CN: 22.5 }, }, source_payload: { id: 'caict-preview-2025', title: 'CAICT Preview 2025', url: 'https://www.caict.ac.cn/preview-2025', publisher: 'CAICT', report_year: 2025, locator: 'Table 1', }, confidence: 0.9, extractor_note: 'Extracted', reviewed_by: 'admin', reviewed_at: '2026-11-11T00:00:00Z', created_at: '2026-11-11T00:00:00Z', ...overrides, } } describe('compute-comparison-model', () => { test('round-trips bundled authoritative defaults without dropping structures, milestones, or 8 national hubs', () => { const hydrated = ensureCompleteConfiguration(bundledDefaults) const normalized = normalizeDraftForPublish(hydrated) expect(normalized.structures.length).toBeGreaterThanOrEqual(2) expect(normalized.milestones.length).toBeGreaterThanOrEqual(3) expect(normalized.national_hubs).toHaveLength(8) expect(normalized.observations.some(item => item.metric === 'intelligent_share')).toBe(true) expect(normalized.observations.some(item => item.metric === 'frontier_cluster_share')).toBe(true) expect(normalized.observations.some(item => item.metric === 'chip_inflow_share')).toBe(true) }) test('protects report sources referenced by structures, milestones, national hubs, or per-country value_sources', () => { const hydrated = ensureCompleteConfiguration(bundledDefaults) const hubSourceId = hydrated.national_hubs[0].source_id const milestoneSourceId = hydrated.milestones[0].source_id expect(reportIsUsed(hydrated, hubSourceId)).toBe(true) expect(reportIsUsed(hydrated, milestoneSourceId)).toBe(true) expect(reportIsUsed(hydrated, 'unused-source-id')).toBe(false) }) test('formats English source option labels without leaking CJK characters', () => { const hydrated = ensureCompleteConfiguration(bundledDefaults) for (const source of hydrated.sources) { const label = formatSourceOptionLabel(source, true) expect(/[\u3400-\u9fff]/.test(label)).toBe(false) } }) test('imports new observation candidate into draft and registers missing source', () => { const hydrated = ensureCompleteConfiguration(bundledDefaults) const result = applyApprovedCandidateToDraft(hydrated, makeCandidate()) expect(result.status).toBe('applied') expect(result.mixedShareRowWarning).toBe(false) expect(result.draft.sources.some(src => src.id === 'caict-preview-2025')).toBe(true) const obs2025 = result.draft.observations.find(obs => obs.year === 2025 && obs.metric === 'global_share') expect(obs2025?.values.US).toBe(45) expect(obs2025?.values.CN).toBe(22.5) expect(obs2025?.value_sources).toBeUndefined() }) test('China-only partial revision preserves US source via value_sources and triggers mixed-share warning', () => { const hydrated = ensureCompleteConfiguration(bundledDefaults) const cnOnlyCandidate = makeCandidate({ id: 77, proposed_payload: { year: 2025, metric: 'chip_inflow_share', source_id: 'epoch-chip-imports-2026-11', values: { CN: 9.9 }, }, source_payload: { id: 'epoch-chip-imports-2026-11', title: 'Epoch AI Chip Imports 2026-11-12', url: 'https://epoch.ai/data/ai-chip-imports-documentation/changelog', publisher: 'Epoch AI', report_year: 2026, published_at: '2026-11-12', locator: 'Changelog 2026-11-12', }, }) const result = applyApprovedCandidateToDraft(hydrated, cnOnlyCandidate) expect(result.status).toBe('applied') expect(result.mixedShareRowWarning).toBe(true) expect(hasMixedShareSourceRow(result.draft)).toBe(true) const row = result.draft.observations.find( obs => obs.metric === 'chip_inflow_share' && obs.year === 2025, )! expect(row.source_id).toBe('epoch-chip-imports-2026') expect(row.values).toEqual({ US: 72, CN: 9.9 }) expect(row.value_sources).toEqual({ CN: 'epoch-chip-imports-2026-11' }) expect(sourceForObservation(row, 'CN')).toBe('epoch-chip-imports-2026-11') expect(sourceForObservation(row, 'US')).toBe('epoch-chip-imports-2026') expect(reportIsUsed(result.draft, 'epoch-chip-imports-2026-11')).toBe(true) // Subsequent update bringing US onto the same new report collapses value_sources cleanly. const usFollowup = makeCandidate({ id: 78, proposed_payload: { year: 2025, metric: 'chip_inflow_share', source_id: 'epoch-chip-imports-2026-11', values: { US: 72.0 }, }, source_payload: { id: 'epoch-chip-imports-2026-11', report_year: 2026 }, }) const unified = applyApprovedCandidateToDraft(result.draft, usFollowup) expect(unified.status).toBe('applied') expect(unified.mixedShareRowWarning).toBe(false) const unifiedRow = unified.draft.observations.find( obs => obs.metric === 'chip_inflow_share' && obs.year === 2025, )! expect(unifiedRow.source_id).toBe('epoch-chip-imports-2026-11') expect(unifiedRow.value_sources).toBeUndefined() }) test('blocks period_label conflict until explicit replacement confirmation and reports discarded codes', () => { const hydrated = ensureCompleteConfiguration(bundledDefaults) // Add US to 2025-H1 absolute row so replacing with CN-only full-year drops US const withUs: ComputeConfiguration = { ...hydrated, observations: hydrated.observations.map(obs => ( obs.metric === 'absolute' && obs.year === 2025 ? { ...obs, values: { ...obs.values, US: 1800 } } : obs )), } const fullYearCandidate = makeCandidate({ id: 99, proposed_payload: { year: 2025, metric: 'absolute', source_id: 'caict-2026-full', values: { CN: 1120 }, period_label: null, }, source_payload: { id: 'caict-2026-full', title: 'CAICT 2026 Full Year', url: 'https://www.caict.ac.cn/2026', publisher: 'CAICT', report_year: 2026, locator: 'Page 14', }, }) const blocked = applyApprovedCandidateToDraft(withUs, fullYearCandidate) expect(blocked.status).toBe('period_conflict') expect(blocked.existingPeriodLabel).toBe('2025-H1') expect(blocked.candidatePeriodLabel).toBeNull() expect(blocked.discardedCodes).toEqual(['US']) // Original draft untouched when blocked expect(blocked.draft).toBe(withUs) const replaced = applyApprovedCandidateToDraft(withUs, fullYearCandidate, { replaceOnPeriodConflict: true, }) expect(replaced.status).toBe('applied') expect(replaced.discardedCodes).toEqual(['US']) const row2025 = replaced.draft.observations.find( obs => obs.metric === 'absolute' && obs.year === 2025, )! expect(row2025.period_label).toBeUndefined() expect(row2025.source_id).toBe('caict-2026-full') expect(row2025.values).toEqual({ CN: 1120 }) }) })