81 lines
3.3 KiB
TypeScript
81 lines
3.3 KiB
TypeScript
import { describe, expect, test } from 'bun:test'
|
|
import { readFileSync } from 'node:fs'
|
|
import { resolve } from 'node:path'
|
|
import {
|
|
applyApprovedCandidateToDraft,
|
|
ensureCompleteConfiguration,
|
|
formatSourceOptionLabel,
|
|
normalizeDraftForPublish,
|
|
reportIsUsed,
|
|
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
|
|
|
|
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, or national hubs', () => {
|
|
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 approved candidate into draft and registers missing source', () => {
|
|
const hydrated = ensureCompleteConfiguration(bundledDefaults)
|
|
const candidate: ComputeCandidateItem = {
|
|
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',
|
|
}
|
|
|
|
const updated = applyApprovedCandidateToDraft(hydrated, candidate)
|
|
expect(updated.sources.some(src => src.id === 'caict-preview-2025')).toBe(true)
|
|
const obs2025 = updated.observations.find(obs => obs.year === 2025 && obs.metric === 'global_share')
|
|
expect(obs2025?.values.US).toBe(45)
|
|
expect(obs2025?.values.CN).toBe(22.5)
|
|
})
|
|
})
|