147 lines
7.2 KiB
TypeScript
147 lines
7.2 KiB
TypeScript
import { describe, expect, it } from "vitest";
|
||
import { createRun, endWeek, REMATCH_MOMENTUM } from "../src/rules.ts";
|
||
import { bigRallyWeek, intentsThisWeek, rivalCatchUp } from "../src/rival.ts";
|
||
import { growthWeeks } from "../src/mechanics.ts";
|
||
import { shiftTwoParty } from "../src/setup.ts";
|
||
import { COMBINED_CAP, MEASURED_TIDES, TIDE_CAP, applyHistory, expectedMargin, outlookLabel, tideKey } from "../src/tides.ts";
|
||
import { totals, type RogueSetup, type RunState } from "../src/state.ts";
|
||
import { toView } from "../src/view.ts";
|
||
import { SETUP, advance, must, toPlan } from "./fixtures.ts";
|
||
|
||
/** docs/26:可選局數、歷史形勢(選前形勢、全國風向、對手輕敵、現任包袱、捲土重來) */
|
||
|
||
describe("可選局數", () => {
|
||
for (const weeks of [6, 10, 12] as const) {
|
||
it(`${weeks} 週:辯論、民調、黑函不重疊,都在對手大型造勢之前;最後一週的對手和 8 週一樣強`, () => {
|
||
for (let seed = 1; seed <= 12; seed++) {
|
||
const s = createRun(SETUP, "r", seed, { weeks });
|
||
const sch = s.schedule!;
|
||
expect(sch).toBeTruthy();
|
||
const big = bigRallyWeek(s);
|
||
expect(big).toBe(weeks - 1);
|
||
const fixed = [sch.debate, sch.poll, sch.crisis];
|
||
expect(new Set(fixed).size).toBe(3);
|
||
for (const w of fixed) expect(w).toBeLessThan(big);
|
||
for (const l of sch.local ?? []) {
|
||
expect(fixed).not.toContain(l.week);
|
||
expect(l.week).toBeGreaterThanOrEqual(2);
|
||
expect(l.week).toBeLessThan(big);
|
||
}
|
||
expect(growthWeeks({ ...s, week: weeks })).toBeCloseTo(7);
|
||
}
|
||
});
|
||
}
|
||
|
||
it("8 週照原本的週次;政治生涯的短局不受影響", () => {
|
||
const s = createRun(SETUP, "r", 3);
|
||
expect(bigRallyWeek(s)).toBe(7);
|
||
expect(growthWeeks({ ...s, week: 8 })).toBe(7);
|
||
const career = createRun(SETUP, "r", 3, { weeks: 6, career: { stage: 2, total: 5 } });
|
||
expect(career.schedule).toBeUndefined();
|
||
expect(bigRallyWeek(career)).toBe(7);
|
||
});
|
||
|
||
it("12 週的一局打得完", () => {
|
||
let s = createRun(SETUP, "r", 4, { weeks: 12 });
|
||
s = advance(s, 12);
|
||
s = must(endWeek(toPlan(s)));
|
||
expect(s.phase).toBe("finished");
|
||
expect(s.history.length).toBe(13);
|
||
});
|
||
});
|
||
|
||
/** 選前形勢落後 gap、風向往你推 tide 的假開局 */
|
||
function historic(gap: number, measured: number, party: string | null = "民主進步黨", rivalParty: string | null = "中國國民黨"): RogueSetup {
|
||
return {
|
||
...SETUP,
|
||
you: { ...SETUP.you, party },
|
||
rival: { ...SETUP.rival, party: rivalParty },
|
||
outlook: { gap, rawGap: gap, basis: "測試" },
|
||
};
|
||
}
|
||
|
||
describe("歷史形勢", () => {
|
||
it("風向表的鍵:年份+選舉種類", () => {
|
||
expect(tideKey("local2014-68000", "2014-11-29")).toBe("2014-local");
|
||
expect(tideKey("leg2024-10007-01-even", "2024-01-13")).toBe("2024-leg");
|
||
expect(tideKey("pres2016", "2016-01-16")).toBe("2016-pres");
|
||
expect(tideKey("taoyuan-2022-mayor", "2022-11-26")).toBe("2022-local");
|
||
// 實測的風向:2014 偏綠、2018 偏藍
|
||
expect(MEASURED_TIDES["2014-local"]).toBeGreaterThan(0.05);
|
||
expect(MEASURED_TIDES["2018-local"]).toBeLessThan(-0.05);
|
||
});
|
||
|
||
it("風向照黨別換成對你有利或不利,最多 TIDE_CAP;難度超過 COMBINED_CAP 就把開局收回來", () => {
|
||
const green = applyHistory(historic(-0.12, 0), "local2014-68000", "2014-11-29", shiftTwoParty)!;
|
||
expect(green.info.tide).toBeGreaterThan(0);
|
||
expect(green.info.tide).toBeLessThanOrEqual(TIDE_CAP);
|
||
const blue = applyHistory({ ...historic(0.12, 0, "中國國民黨", "民主進步黨") }, "local2014-68000", "2014-11-29", shiftTwoParty)!;
|
||
expect(blue.info.tide).toBeCloseTo(-green.info.tide);
|
||
// 2018 偏藍:綠營候選人開局領先 12、風向不利;藍營落後 12、風向有利
|
||
const hard = applyHistory(historic(-0.12, 0, "民主進步黨", "中國國民黨"), "local2018-64000", "2018-11-24", shiftTwoParty)!;
|
||
expect(hard.info.expected).toBeGreaterThanOrEqual(-COMBINED_CAP - 1e-9);
|
||
expect(hard.info.expected).toBeCloseTo(expectedMargin(hard.info.gap, hard.info.tide, null, false));
|
||
// 現任、捲土重來算進難度
|
||
const local = applyHistory(historic(-0.02, 0), "local2014-68000", "2014-11-29", shiftTwoParty, { incumbent: "rival", rematch: true })!;
|
||
expect(local.info.expected).toBeGreaterThan(applyHistory(historic(-0.02, 0), "local2014-68000", "2014-11-29", shiftTwoParty)!.info.expected);
|
||
// 無黨籍對上藍營算綠營
|
||
const ko = applyHistory(historic(-0.05, 0, null, "中國國民黨"), "local2014-63000", "2014-11-29", shiftTwoParty)!;
|
||
expect(ko.info.tide).toBeGreaterThan(0);
|
||
expect(applyHistory({ ...SETUP }, "x", "2014-11-29", shiftTwoParty)).toBeNull();
|
||
});
|
||
|
||
it("難度的說法跟著「雙方打得一樣時開出來大約差多少」", () => {
|
||
expect(outlookLabel(0.05).level).toBe(1);
|
||
expect(outlookLabel(0.02).level).toBe(2);
|
||
expect(outlookLabel(0).level).toBe(3);
|
||
expect(outlookLabel(-0.03).level).toBe(4);
|
||
expect(outlookLabel(-0.05).level).toBe(5);
|
||
expect(outlookLabel(-0.08).level).toBe(6);
|
||
});
|
||
|
||
const options = (over: Partial<NonNullable<RunState["options"]["history"]>> = {}) => ({
|
||
history: { tide: 0.06, measured: 0.18, label: "測試風向", gap: -0.05, basis: "測試", incumbent: null, rematch: false, ...over },
|
||
});
|
||
|
||
it("週末風向把差距往你這邊推", () => {
|
||
const plain = must(endWeek(toPlan(createRun(SETUP, "r", 5))));
|
||
const windy = must(endWeek(toPlan(createRun(SETUP, "r", 5, options()))));
|
||
const gap = (s: RunState) => totals(s.towns).you - totals(s.towns).rival;
|
||
expect(gap(windy) - gap(plain)).toBeGreaterThan(0);
|
||
expect(windy.log.some((l) => l.text.startsWith("全國風向對你有利"))).toBe(true);
|
||
const against = must(endWeek(toPlan(createRun(SETUP, "r", 5, options({ tide: -0.06 })))));
|
||
expect(gap(against)).toBeLessThan(gap(plain));
|
||
});
|
||
|
||
it("現任包袱:現任每週流失支持者;你批評現任 +25%", () => {
|
||
const s = must(endWeek(toPlan(createRun(SETUP, "r", 5, options({ tide: 0, incumbent: "rival" })))));
|
||
expect(s.log.some((l) => l.text.includes("施政包袱"))).toBe(true);
|
||
expect(toView(createRun(SETUP, "r", 5, options({ incumbent: "rival" }))).situation?.incumbent).toBe("rival");
|
||
});
|
||
|
||
it("捲土重來:開局聲勢多", () => {
|
||
expect(createRun(SETUP, "r", 5, options({ rematch: true })).momentum).toBe(REMATCH_MOMENTUM);
|
||
expect(createRun(SETUP, "r", 5, options()).momentum).toBe(0);
|
||
});
|
||
|
||
it("對手輕敵:選戰前半、對手領先 4 個百分點以上,少一個行動;勢均力敵沒有", () => {
|
||
const s = advance(createRun(SETUP, "r", 5, options()), 2);
|
||
const behind = structuredClone(s) as RunState;
|
||
for (const t of behind.towns) {
|
||
t.rival += t.you * 0.3;
|
||
t.you *= 0.7;
|
||
}
|
||
const n = intentsThisWeek(behind);
|
||
rivalCatchUp(behind);
|
||
expect(behind.rivalEase?.less).toBe(1);
|
||
expect(intentsThisWeek(behind)).toBe(n - 1);
|
||
const even = structuredClone(behind) as RunState;
|
||
even.options = { ...even.options, history: null };
|
||
rivalCatchUp(even);
|
||
expect(even.rivalEase).toBeNull();
|
||
const late = { ...(structuredClone(behind) as RunState), week: 7 };
|
||
rivalCatchUp(late);
|
||
expect(late.rivalEase).toBeNull();
|
||
});
|
||
});
|