182 lines
8.5 KiB
TypeScript
182 lines
8.5 KiB
TypeScript
import { existsSync, readFileSync } from "node:fs";
|
||
import { actionInfo, STAFF_ROLES } from "@election2026/engine";
|
||
import { ActionInfo } from "@election2026/protocol";
|
||
import { describe, expect, it } from "vitest";
|
||
import { TAOYUAN_2022_FINANCE, financeFromIncome } from "../src/finance.ts";
|
||
import { ACTIONS, ENCOUNTERS, EVENTS, ISSUES, SCENARIOS } from "../src/index.ts";
|
||
|
||
describe("行動目錄", () => {
|
||
it("每個行動都符合協定", () => {
|
||
for (const a of ACTIONS) expect(ActionInfo.safeParse(actionInfo(a)).success, a.id).toBe(true);
|
||
});
|
||
|
||
it("幕僚的專長牌都存在、幕僚能做、是競選期間的牌", () => {
|
||
const byId = new Map(ACTIONS.map((a) => [a.id, a]));
|
||
for (const role of STAFF_ROLES) {
|
||
for (const id of role.deck) {
|
||
const a = byId.get(id);
|
||
expect(a, `${role.name}:${id}`).toBeDefined();
|
||
expect(a!.actors, `${role.name}:${id}`).toContain("team");
|
||
expect(a!.phase, `${role.name}:${id}`).toBe("campaign");
|
||
}
|
||
}
|
||
});
|
||
|
||
it("id 不重複", () => {
|
||
expect(new Set(ACTIONS.map((a) => a.id)).size).toBe(ACTIONS.length);
|
||
});
|
||
|
||
it("規模的預設值落在範圍內、對齊級距", () => {
|
||
for (const a of ACTIONS.filter((x) => x.amount)) {
|
||
const { min, max, step, default: d } = a.amount!;
|
||
expect(min <= d && d <= max, a.id).toBe(true);
|
||
expect((d - min) % step, a.id).toBe(0);
|
||
expect((max - min) % step, a.id).toBe(0);
|
||
}
|
||
});
|
||
|
||
it("黑暗面在午夜偷偷做;公開演出的黑暗面(苦肉計)一定留下祕密紀錄;午夜只能做祕密行動", () => {
|
||
for (const a of ACTIONS) {
|
||
if (a.category === "dark" && a.visibility === "secret") expect(a.slots, a.id).toEqual(["midnight"]);
|
||
if (a.category === "dark" && a.visibility !== "secret") expect(a.secret, a.id).toBeDefined();
|
||
if (a.slots.includes("midnight")) expect(a.visibility, a.id).toBe("secret");
|
||
}
|
||
});
|
||
|
||
it("新行動:id 不重複、擲骰機率合理、要指定對手的行動都有特殊處理或對手效果", () => {
|
||
const ids = ACTIONS.map((a) => a.id);
|
||
expect(new Set(ids).size).toBe(ids.length);
|
||
expect(ACTIONS.length).toBeGreaterThanOrEqual(75);
|
||
for (const a of ACTIONS) {
|
||
if (a.gamble) expect(a.gamble.chance, a.id).toBeGreaterThan(0), expect(a.gamble.chance, a.id).toBeLessThan(1);
|
||
if (a.target === "opponent") {
|
||
const hitsOpponent = [a.effects, a.outcome?.effects, a.gamble?.success.effects].some((list) => list?.some((e) => e.onOpponent));
|
||
expect(a.special !== undefined || hitsOpponent, a.id).toBe(true);
|
||
}
|
||
}
|
||
});
|
||
|
||
it("兩個時段的行動不能排在每段的第一個時段", () => {
|
||
for (const a of ACTIONS.filter((x) => x.duration === 2)) {
|
||
expect(a.slots.some((s) => s === "weekdayMorning" || s === "weekendMorning" || s === "midnight"), a.id).toBe(false);
|
||
}
|
||
});
|
||
|
||
it("AI 用到的行動都存在,而且由正確的角色執行", () => {
|
||
const source = readFileSync(new URL("../../engine/src/ai.ts", import.meta.url), "utf-8");
|
||
const byCandidate = [...source.matchAll(/add\(p, CANDIDATE, (?:style === "ground" \? "\w+" : )?"(\w+)"/g)].map((m) => ["candidate", m[1]]);
|
||
// 團隊事務寫成 addTeam(p, staff, "x") 或 team("x")
|
||
const byTeam = [...source.matchAll(/(?:addTeam\(p, staff, |\bteam\()"(\w+)"/g)].map((m) => ["team", m[1]]);
|
||
const ground = source.slice(source.indexOf("const STAFF_GROUND"), source.indexOf("};", source.indexOf("const STAFF_GROUND")));
|
||
const byStaff = [...ground.matchAll(/"(\w+)"/g)].map((m) => ["team", m[1]]);
|
||
const used = [...byCandidate, ...byTeam, ...byStaff];
|
||
expect(byCandidate.length).toBeGreaterThan(8);
|
||
expect(byTeam.length).toBeGreaterThan(10);
|
||
expect(byStaff.length).toBeGreaterThan(5);
|
||
for (const [actor, id] of used) {
|
||
const a = ACTIONS.find((x) => x.id === id);
|
||
expect(a, id).toBeDefined();
|
||
expect(a!.actors, id).toContain(actor);
|
||
}
|
||
});
|
||
});
|
||
|
||
describe("歷史劇本的錢", () => {
|
||
it("和資料管線產生的政治獻金資料一致", () => {
|
||
const file = new URL("../../../data/build/finance/111年直轄市市長選舉-桃園市.json", import.meta.url);
|
||
if (!existsSync(file)) return; // 資料是管線產物,沒有建置時略過
|
||
const built = JSON.parse(readFileSync(file, "utf-8")) as { candidates: { name: string; income: number; spending: number }[] };
|
||
for (const [name, real] of Object.entries(TAOYUAN_2022_FINANCE)) {
|
||
const row = built.candidates.find((c) => c.name === name);
|
||
expect(row, name).toBeDefined();
|
||
expect({ income: row!.income, spending: row!.spending }, name).toEqual(real);
|
||
}
|
||
});
|
||
|
||
it("起始資金加上十週的募款能力等於真實收入", () => {
|
||
for (const real of Object.values(TAOYUAN_2022_FINANCE)) {
|
||
const f = financeFromIncome(real.income, 10);
|
||
expect(Math.abs(f.startingMoney + f.fundraising * 10 - real.income)).toBeLessThan(60_000);
|
||
}
|
||
});
|
||
|
||
it("劇本的錢都由真實資料推算,經費上限是中選會公告的數字", () => {
|
||
const s = SCENARIOS.find((x) => x.id === "taoyuan-2022-mayor")!;
|
||
expect(s.spendingCap).toBe(81_679_000);
|
||
for (const c of s.candidates) {
|
||
expect(c.realFinance, c.name).toEqual(TAOYUAN_2022_FINANCE[c.name]);
|
||
expect(c.startingMoney).toBe(financeFromIncome(c.realFinance!.income, s.weeks).startingMoney);
|
||
expect(c.realFinance!.spending).toBeLessThanOrEqual(s.spendingCap);
|
||
}
|
||
});
|
||
});
|
||
|
||
describe("議題", () => {
|
||
it("id 不重複,在意程度介於 0 到 1", () => {
|
||
expect(new Set(ISSUES.map((i) => i.id)).size).toBe(ISSUES.length);
|
||
for (const i of ISSUES) for (const v of Object.values(i.appeal)) expect(v >= 0 && v <= 1).toBe(true);
|
||
});
|
||
});
|
||
|
||
describe("劇本", () => {
|
||
it("候選人 id 與號次不重複,真實結果對照到存在的候選人", () => {
|
||
for (const s of SCENARIOS) {
|
||
const ids = s.candidates.map((c) => c.id);
|
||
expect(new Set(ids).size, s.id).toBe(ids.length);
|
||
expect(new Set(s.candidates.map((c) => c.number)).size, s.id).toBe(ids.length);
|
||
for (const id of Object.values(s.actual?.partyToCandidate ?? {})) expect(ids, s.id).toContain(id);
|
||
expect(s.blackoutFromWeek).toBeLessThanOrEqual(s.weeks);
|
||
}
|
||
});
|
||
|
||
it("真實人物不會被設定成會買票、放網軍的 AI(docs/14)", () => {
|
||
for (const s of SCENARIOS) {
|
||
for (const c of s.candidates) {
|
||
if (c.personId !== null || s.actual !== null) expect(c.aiStyle, `${s.id} ${c.name}`).not.toBe("dirty");
|
||
}
|
||
}
|
||
});
|
||
});
|
||
|
||
describe("表態事件", () => {
|
||
it("格式正確:id 不重複、預設選項存在、至少兩個選項、幅度有上限", () => {
|
||
const ids = new Set<string>();
|
||
for (const e of EVENTS) {
|
||
expect(ids.has(e.id), e.id).toBe(false);
|
||
ids.add(e.id);
|
||
expect(e.options.length, e.id).toBeGreaterThanOrEqual(2);
|
||
expect(e.options.some((o) => o.id === e.fallback), e.id).toBe(true);
|
||
const outcomes = e.options.flatMap((o) => [o.outcome, ...(o.roll ? [o.roll.success, o.roll.failure] : [])]);
|
||
for (const out of outcomes) {
|
||
for (const f of out.effects ?? []) expect(Math.abs(f.base), e.id).toBeLessThanOrEqual(0.03);
|
||
expect(Math.abs(out.momentum ?? 0), e.id).toBeLessThanOrEqual(0.06);
|
||
}
|
||
// 擲骰的選項成功、失敗都要有文字
|
||
for (const o of e.options) if (o.roll) expect(o.roll.success.text && o.roll.failure.text, `${e.id}/${o.id}`).toBeTruthy();
|
||
}
|
||
});
|
||
|
||
it("不寫真實人物:事件文字裡沒有任何劇本候選人的名字", () => {
|
||
const names = SCENARIOS.flatMap((s) => s.candidates.map((c) => c.name));
|
||
const text = JSON.stringify(EVENTS);
|
||
for (const n of names) expect(text.includes(n), n).toBe(false);
|
||
});
|
||
});
|
||
|
||
describe("拜票副本", () => {
|
||
it("遭遇都掛在存在的行動上,分數在 -2 到 +2,每個行動至少有三個遭遇可抽", () => {
|
||
const ids = new Set(ACTIONS.map((a) => a.id));
|
||
const seen = new Set<string>();
|
||
for (const e of ENCOUNTERS) {
|
||
expect(seen.has(e.id), e.id).toBe(false);
|
||
seen.add(e.id);
|
||
for (const a of e.activities) expect(a === "*" || ids.has(a), `${e.id}:${a}`).toBe(true);
|
||
for (const o of e.options) {
|
||
for (const r of [o, ...(o.roll ? [o.roll.success, o.roll.failure] : [])]) expect(Math.abs(r.score), `${e.id}/${o.id}`).toBeLessThanOrEqual(2);
|
||
}
|
||
}
|
||
const activities = new Set(ENCOUNTERS.flatMap((e) => e.activities).filter((a) => a !== "*"));
|
||
for (const a of activities) expect(ENCOUNTERS.filter((e) => e.activities.includes(a) || e.activities.includes("*")).length, a).toBeGreaterThanOrEqual(3);
|
||
});
|
||
});
|