Files
planet/frontend/public/earth/js/compute-topic-model.test.js
T
rayd1o 5bfc790dec
ci / backend (push) Has been cancelled
ci / frontend (push) Has been cancelled
release / images (push) Has been cancelled
ci / delivery (push) Has been cancelled
release: bump version to 0.75.0
2026-09-23 21:20:12 +08:00

69 lines
2.6 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { expect, test } from "bun:test";
import {
computeShareMeterValue,
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 only accepts real 0–100 share observations", () => {
expect(computeShareMeterValue(31, "global_share")).toBe(31);
expect(computeShareMeterValue(61.2, "bilateral_share")).toBe(61.2);
expect(computeShareMeterValue(null, "global_share")).toBeNull();
expect(computeShareMeterValue(0, "absolute")).toBeNull();
expect(computeShareMeterValue(101, "global_share")).toBeNull();
});