Files
planet/frontend/public/earth/js/compute-topic-model.test.js
linkong 842cba4dc7
ci / backend (push) Has been cancelled
ci / frontend (push) Has been cancelled
release / images (push) Has been cancelled
release / client_release (push) Has been cancelled
ci / delivery (push) Has been cancelled
release: bump version to 0.77.0
2026-11-20 16:59:38 +08:00

207 lines
7.3 KiB
JavaScript

import { expect, test } from "bun:test";
import {
buildBilateralDuelSummary,
buildDenominatorAttributionSummary,
buildDivergenceChartGeometry,
buildInnovationConvergenceGeometry,
computeShareMeterValue,
formatStructureSegments,
resolveObservationWindow,
selectComputeParticipant,
summarizeComputeTrend,
} from "./compute-topic-model.js";
const catalog = [
{ code: "CN", kind: "country", members: ["CN"] },
{ code: "US", kind: "country", members: ["US"] },
{ code: "EUROPE", kind: "region", members: ["DE", "FR", "GB"] },
{ code: "APAC", kind: "region", members: ["JP", "KR"] },
{ code: "OTHER", kind: "region", members: ["BR"] },
{ code: "DE", kind: "country", parent_group_code: "EUROPE", members: ["DE"] },
{ code: "FR", kind: "country", parent_group_code: "EUROPE", members: ["FR"] },
{ code: "JP", kind: "country", parent_group_code: "APAC", members: ["JP"] },
{ code: "IN", kind: "country", members: ["IN"] },
];
test("choosing a country replaces its selected parent region", () => {
const result = selectComputeParticipant(
["CN", "US", "EUROPE", "APAC", "OTHER"],
"DE",
catalog,
);
expect(result.codes).toEqual(["CN", "US", "DE", "APAC", "OTHER"]);
expect(result.replaced).toEqual(["EUROPE"]);
});
test("choosing a region replaces selected member countries", () => {
const result = selectComputeParticipant(["CN", "DE", "FR", "APAC"], "EUROPE", catalog);
expect(result.codes).toEqual(["CN", "EUROPE", "APAC"]);
expect(result.replaced).toEqual(["DE", "FR"]);
});
test("a sixth non-overlapping participant is allowed and a seventh is rejected", () => {
const six = selectComputeParticipant(["CN", "US", "DE", "APAC", "OTHER"], "FR", catalog);
expect(six.codes).toHaveLength(6);
const seven = selectComputeParticipant(six.codes, "IN", catalog);
expect(seven.codes).toEqual(six.codes);
expect(seven.limitReached).toBe(true);
});
test("share trend reports actual percentage-point change with both observation years", () => {
expect(summarizeComputeTrend([
{ year: 2022, value: 34 },
{ year: 2023, value: 41 },
], "global_share")).toEqual({
fromYear: 2022,
toYear: 2023,
change: 7,
unit: "percentage_points",
});
});
test("trend summary never invents a change from one or zero observations", () => {
expect(summarizeComputeTrend([{ year: 2023, value: 31 }], "global_share")).toBeNull();
expect(summarizeComputeTrend([], "absolute")).toBeNull();
});
test("share meter accepts verified share modes and rejects absolute or out-of-range values", () => {
expect(computeShareMeterValue(31, "global_share")).toBe(31);
expect(computeShareMeterValue(21, "intelligent_share")).toBe(21);
expect(computeShareMeterValue(61.2, "bilateral_share")).toBe(61.2);
expect(computeShareMeterValue(52.3, "bilateral_rpi")).toBe(52.3);
expect(computeShareMeterValue(null, "global_share")).toBeNull();
expect(computeShareMeterValue(0, "absolute")).toBeNull();
expect(computeShareMeterValue(101, "global_share")).toBeNull();
});
test("bilateral duel summary computes CN-US RPI bar ratios and hides when bilateral scope missing", () => {
const duel = buildBilateralDuelSummary({
mode: "global_share",
bilateral_rpi: {
latest_total: {
year: 2024,
cn_value: 23,
us_value: 44,
rpi_percent: 52.3,
gap_pp: -21,
yoy_cn_pp: -8,
evidence_id: "caict-2025",
},
latest_intelligent: {
year: 2024,
cn_value: 21,
us_value: 46,
rpi_percent: 45.7,
evidence_id: "caict-2025",
},
},
});
expect(duel).not.toBeNull();
expect(duel.cnValue).toBe(23);
expect(duel.usValue).toBe(44);
expect(duel.rpiPercent).toBe(52.3);
expect(duel.cnBarWidthPct + duel.usBarWidthPct).toBeCloseTo(100, 1);
const aiDuel = buildBilateralDuelSummary({
mode: "intelligent_share",
bilateral_rpi: {
latest_intelligent: {
year: 2024,
cn_value: 21,
us_value: 46,
rpi_percent: 45.7,
evidence_id: "caict-2025",
},
},
});
expect(aiDuel.rpiPercent).toBe(45.7);
expect(buildBilateralDuelSummary({ mode: "global_share", bilateral_rpi: null })).toBeNull();
});
test("structure segments render only for countries with verified domestic observations", () => {
const structures = {
CN: {
year: 2025,
period_label: "2025-H1",
intelligent_share: 81,
general_and_super_share: 19,
source_id: "caict-2025",
},
GLOBAL: {
year: 2025,
period_label: "2025-H1",
intelligent_share: 85,
general_and_super_share: 15,
source_id: "caict-2025",
},
};
const cnStruct = formatStructureSegments(structures, "CN");
expect(cnStruct).not.toBeNull();
expect(cnStruct.intelligentShare).toBe(81);
expect(cnStruct.globalIntelligentShare).toBe(85);
expect(formatStructureSegments(structures, "JP")).toBeNull();
});
test("denominator attribution and observation window surface reference period gaps without fabricating future data", () => {
const attr = buildDenominatorAttributionSummary({
denominator_attribution: {
CN: {
latest_year: 2025,
latest_period: "2025-H1",
latest_value: 962,
latest_global_total: 4495,
country_from: 435,
country_to: 962,
country_growth_pct: 121.1,
global_from: 1397,
global_to: 4495,
global_growth_pct: 221.8,
diluted_by_global_expansion: true,
evidence_ids: ["caict-2024", "caict-2025"],
},
},
}, "CN");
expect(attr.latestValue).toBe(962);
expect(attr.dilutedByGlobalExpansion).toBe(true);
const win = resolveObservationWindow([
{ year: 2023, periodLabel: "2023", value: 31 },
{ year: 2024, periodLabel: "2024", value: 23 },
{ year: 2026, periodLabel: null, value: null },
], 2026);
expect(win.isAfterLatestReference).toBe(true);
expect(win.activePoint.year).toBe(2024);
expect(win.latestReferenceLabel).toBe("2024");
});
test("divergence and innovation SVG geometry builders preserve gaps and never invent single-country scores", () => {
const div = buildDivergenceChartGeometry(
[
{ year: 2020, values: { CN: 31, US: null }, evidence_id: "caict-2021" },
{ year: 2021, values: { CN: 33, US: 34 }, evidence_id: "caict-2022" },
{ year: 2022, values: { CN: 33, US: 34 }, evidence_id: "caict-2023" },
{ year: 2023, values: { CN: 31, US: 41 }, evidence_id: "caict-2024" },
{ year: 2024, values: { CN: 23, US: 44 }, evidence_id: "caict-2025" },
{ year: 2026, values: { CN: null, US: null }, evidence_id: null },
],
[{ id: "ms-2022", year: 2022, title: "管制收紧", source_id: "bis-export-controls-2022" }],
);
expect(div).not.toBeNull();
expect(div.cnPoints).toHaveLength(5);
expect(div.usPoints).toHaveLength(4);
expect(div.corridorPaths).toHaveLength(1);
expect(div.milestones).toHaveLength(1);
const innov = buildInnovationConvergenceGeometry([
{ year: 2023, value: 22.02, evidence_id: "innovation-2026" },
{ year: 2024, value: 19.96, evidence_id: "innovation-2026" },
{ year: 2025, value: 17.95, evidence_id: "innovation-2026" },
{ year: 2026, value: null, evidence_id: null },
]);
expect(innov).not.toBeNull();
expect(innov.points).toHaveLength(3);
expect(innov.delta).toBe(-4.07);
expect(innov.areaPath).toContain("Z");
});