election-game/backend/packages/scenarios/test/registry.test.ts

98 lines
5.4 KiB
TypeScript
Raw Permalink 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 { existsSync } from "node:fs";
import { join } from "node:path";
import { DEFAULT_PACKS_ROOT, DEFAULT_SCENARIOS_ROOT } from "@election2026/pack-loader";
import { describe, expect, it } from "vitest";
import { loadScenarioRegistry } from "../src/index.ts";
const built = existsSync(join(DEFAULT_SCENARIOS_ROOT, "2022-local", "63000.json"));
describe.skipIf(!built)("全台劇本目錄", () => {
it("每個有資料的縣市都有歷史劇本,資料包也都存在", async () => {
const reg = await loadScenarioRegistry();
const counties = new Set(reg.all.map((s) => s.region.countyCode));
expect(counties.size).toBeGreaterThanOrEqual(21);
for (const s of reg.all) expect(existsSync(join(DEFAULT_PACKS_ROOT, s.packId, "manifest.json")), s.id).toBe(true);
});
it("候選人 id、號次不重複;真實結果的候選人都在名單上;沒有人被設成奧步型", async () => {
const reg = await loadScenarioRegistry();
for (const s of reg.all) {
const ids = new Set(s.candidates.map((c) => c.id));
expect(ids.size, s.id).toBe(s.candidates.length);
for (const row of Object.values(s.actual?.byVillage ?? {})) for (const id of Object.keys(row)) expect(ids.has(id), `${s.id} ${id}`).toBe(true);
for (const c of s.candidates) expect(c.aiStyle, `${s.id} ${c.name}`).not.toBe("dirty");
}
// 讀全部劇本、逐里比對:整套測試平行跑時會超過預設的 5 秒
}, 20_000);
it("臺北市有 12 位候選人,黃珊珊用民眾黨支持的槽位", async () => {
const reg = await loadScenarioRegistry();
const taipei = reg.find("local2022-63000")!;
expect(taipei.candidates).toHaveLength(12);
expect(taipei.candidates.find((c) => c.name === "黃珊珊")?.slot).toBe("c");
expect(taipei.spendingCap).toBe(84_589_000);
});
it("2024 總統與 73 個區域立委選區", async () => {
const reg = await loadScenarioRegistry();
const in2024 = reg.all.filter((s) => s.electionDate.startsWith("2024"));
const pres = in2024.filter((s) => s.region.kind === "president" && !s.hypothetical);
expect(pres).toHaveLength(1);
expect(pres[0]!.candidates.map((c) => c.name)).toEqual(["柯文哲", "賴清德", "侯友宜"]);
expect(pres[0]!.region.regionLevel).toBe("county");
const leg = in2024.filter((s) => s.region.kind === "legislator" && !s.hypothetical);
expect(leg).toHaveLength(73);
expect(leg.every((s) => (s.villages?.length ?? 0) > 0)).toBe(true);
});
it.skipIf(!existsSync(join(DEFAULT_SCENARIOS_ROOT, "2018-local", "63000.json")))("2018 縣市長(含嘉義市)、2020 總統與立委", async () => {
const reg = await loadScenarioRegistry();
const history = (year: string, kind: string) => reg.all.filter((s) => s.electionDate.startsWith(year) && s.region.kind === kind && !s.hypothetical);
expect(history("2018", "mayor")).toHaveLength(22);
expect(reg.find("local2018-63000")?.candidates.map((c) => c.name)).toContain("柯文哲");
expect(reg.find("local2018-63000")?.spendingCap).toBe(87_475_000);
const pres = history("2020", "president");
expect(pres.map((s) => s.id)).toEqual(["pres2020"]);
expect(pres[0]!.spendingCap).toBe(430_297_000);
expect(pres[0]!.publicHistoryElection).toBe("2016-president");
expect(history("2020", "legislator")).toHaveLength(73);
// 不同年份的候選人 id 不會撞在一起
const ids = reg.all.flatMap((s) => (s.hypothetical ? [] : s.candidates.map((c) => c.id)));
expect(new Set(ids).size).toBe(ids.length);
});
it.skipIf(!existsSync(join(DEFAULT_SCENARIOS_ROOT, "2014-local", "63000.json")))("經典之戰:2014 縣市長", async () => {
const reg = await loadScenarioRegistry();
const mayors = reg.all.filter((s) => s.electionDate === "2014-11-29" && s.region.kind === "mayor" && !s.hypothetical);
expect(mayors).toHaveLength(22);
const taipei = reg.find("local2014-63000")!;
expect(taipei.candidates.map((c) => c.name)).toEqual(expect.arrayContaining(["連勝文", "柯文哲"]));
expect(taipei.spendingCap).toBe(87_690_000);
expect(taipei.publicHistoryElection).toBe("2012-president");
expect(reg.find("local2014-68000")?.region.office).toBe("市長");
// 沒有政治獻金資料:備註要說清楚
expect(taipei.notes.some((n) => n.includes("沒有 2014 年這場選舉的候選人申報資料"))).toBe(true);
});
it.skipIf(!existsSync(join(DEFAULT_SCENARIOS_ROOT, "1996-president", "00000.json")))("經典之戰:1996 年起歷屆總統", async () => {
const reg = await loadScenarioRegistry();
const pres = reg.all.filter((s) => s.region.kind === "president" && !s.hypothetical);
expect(pres.map((s) => s.electionDate.slice(0, 4)).sort()).toEqual(["1996", "2000", "2004", "2008", "2012", "2016", "2020", "2024"]);
const first = reg.find("pres1996")!;
expect(first.candidates.map((c) => [c.name, c.runningMate])).toEqual([
["陳履安", "王清峰"],
["李登輝", "連戰"],
["彭明敏", "謝長廷"],
["林洋港", "郝柏村"],
]);
// 第一次直選之前沒有總統選舉,地圖的「上次選舉」用 1995 年立委
expect(first.publicHistoryElection).toBe("1995-legislator");
expect(reg.find("pres2012")?.publicHistoryElection).toBe("2008-president");
for (const s of pres) {
expect(s.region.regionLevel).toBe("county");
// 舊縣市都併進現在的 22 個縣市
expect(new Set(Object.keys(s.actual?.byVillage ?? {}).map((c) => c.slice(0, 5))).size).toBe(22);
}
});
});