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

43 lines
2.0 KiB
TypeScript
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 { existsSync } from "node:fs";
import { join } from "node:path";
import { autoplay, createMatch, DEFAULT_PARAMS, playWeek, viewFor, type World } from "@election2026/engine";
import { DEFAULT_PACKS_ROOT, loadPack } from "@election2026/pack-loader";
import { PlayerView } from "@election2026/protocol";
import { describe, expect, it } from "vitest";
import { CONTENT, SCENARIOS } from "../src/index.ts";
/** 用真實資料包整場跑一次。資料包是管線產物,沒有建置時略過 */
for (const scenario of SCENARIOS) {
const built = existsSync(join(DEFAULT_PACKS_ROOT, scenario.packId, "manifest.json"));
describe.skipIf(!built)(`劇本 ${scenario.id}`, () => {
const world = async (): Promise<World> => ({ scenario, pack: await loadPack(scenario.packId), content: CONTENT, params: DEFAULT_PARAMS });
it("每位候選人都能玩,每週的視角都符合協定,AI 指令全部合法", async () => {
const w = await world();
for (const cand of scenario.candidates) {
let state = createMatch(w, "m", 1, cand.id);
while (state.phase !== "finished") {
const view = viewFor(state, w, cand.id);
const parsed = PlayerView.safeParse(view);
expect(parsed.success, parsed.success ? "" : parsed.error.message).toBe(true);
const out = playWeek(state, w);
expect(out.errors).toEqual({});
state = out.state;
}
expect(PlayerView.safeParse(viewFor(state, w, cand.id)).success).toBe(true);
}
});
it("開票有效票與各里加總一致,歷史劇本附上真實結果", async () => {
const w = await world();
const r = autoplay(w, "m", 2).result!;
expect(Object.values(r.votes).reduce((a, b) => a + b, 0)).toBe(r.valid);
expect(Object.keys(r.byVillage)).toHaveLength(w.pack.villages.length);
if (scenario.actual) {
const total = Object.values(r.actualShares ?? {}).reduce((a, b) => a + b, 0);
expect(total).toBeCloseTo(1, 6);
}
});
});
}