91 lines
3.9 KiB
TypeScript
91 lines
3.9 KiB
TypeScript
import { describe, expect, it } from "vitest";
|
||
import { runBot } from "../src/bots.ts";
|
||
import { PLEDGE_OFFER, PLEDGE_SHARE, pledgeProgress, settlePledge } from "../src/pledges.ts";
|
||
import { choose, createRun, endWeek } from "../src/rules.ts";
|
||
import { totals, townLead, type RunState } from "../src/state.ts";
|
||
import { tutorialSetup } from "../src/tutorial.ts";
|
||
import { toView } from "../src/view.ts";
|
||
import { SETUP, advance, must, toPlan } from "./fixtures.ts";
|
||
|
||
/** docs/27:政見支票 */
|
||
|
||
describe("政見支票", () => {
|
||
it("第 1 週開跑記者會:三張支票+不開,畫面上是事件牌,不擋出牌", () => {
|
||
const s = createRun(SETUP, "r", 5);
|
||
expect(s.phase).toBe("plan");
|
||
expect(s.scene?.kind).toBe("pledge");
|
||
const v = toView(s);
|
||
expect(v.scene).toMatchObject({ kind: "press", title: "開跑記者會" });
|
||
const choices = v.scene?.kind === "press" ? v.scene.choices : [];
|
||
expect(choices).toHaveLength(PLEDGE_OFFER + 1);
|
||
expect(choices.at(-1)!.label).toBe("不開支票");
|
||
expect(v.hand.some((c) => !c.blocked)).toBe(true);
|
||
});
|
||
|
||
it("新手教學沒有記者會;支票不動到局裡的亂數", () => {
|
||
expect(createRun(tutorialSetup(), "t", 1, { tutorial: true }).scene).toBeNull();
|
||
const s = createRun(SETUP, "r", 5);
|
||
const declined = must(choose(s, PLEDGE_OFFER));
|
||
expect(declined.scene).toBeNull();
|
||
expect(declined.pledge ?? null).toBeNull();
|
||
expect(declined.rng).toBe(s.rng);
|
||
});
|
||
|
||
it("開了支票:狀態列看得到進度;沒回答就結束這週當作不開", () => {
|
||
const s = createRun(SETUP, "r", 5);
|
||
const took = must(choose(s, 0));
|
||
expect(took.pledge).toBeTruthy();
|
||
expect(toView(took).pledge).toMatchObject({ need: took.pledge!.need });
|
||
const skipped = must(endWeek(s));
|
||
expect(skipped.pledge ?? null).toBeNull();
|
||
});
|
||
|
||
it("跳過(skip)是不開支票;模擬玩家連什麼都不做的也不開", () => {
|
||
const s = createRun(SETUP, "r", 5);
|
||
const skipped = must(choose(s, "skip"));
|
||
expect(skipped.scene).toBeNull();
|
||
expect(skipped.pledge ?? null).toBeNull();
|
||
for (const seed of [1, 2, 3]) expect(runBot(SETUP, seed, "idle").result?.pledge ?? null).toBeNull();
|
||
});
|
||
|
||
it("開票夜:狀態列的兌現與否和開票結果一致(本命區領先剛好在門檻附近、對手週末衝票)", () => {
|
||
const setup = { ...SETUP, towns: SETUP.towns.map((t, i) => (i === 0 ? { ...t, lean: { you: 0.55, rival: 0.4, others: 0.05 } } : t)) };
|
||
let base = createRun(setup, "r", 7);
|
||
base = toPlan(advance(base, base.totalWeeks));
|
||
for (let lead = 0.15; lead <= 0.2; lead += 0.0025) {
|
||
const s = structuredClone(base) as RunState;
|
||
const home = s.towns.find((t) => t.home === "you")!;
|
||
const shift = ((lead - townLead(home)) * (home.you + home.rival + home.others)) / 2;
|
||
home.you += shift;
|
||
home.rival -= shift;
|
||
s.pledge = { id: "home", need: 15 };
|
||
s.momentum = 0;
|
||
s.rivalMomentum = 4;
|
||
const done = must(endWeek(s));
|
||
expect(done.phase).toBe("finished");
|
||
expect(toView(done).pledge?.kept).toBe(done.result!.pledge!.kept);
|
||
}
|
||
});
|
||
|
||
it("開票夜:兌現全市加票、跳票全市掉票", () => {
|
||
const base = createRun(SETUP, "r", 5);
|
||
const kept = structuredClone(base) as RunState;
|
||
kept.pledge = { id: "clean", need: 0 };
|
||
kept.stats.attacks = 0;
|
||
expect(pledgeProgress(kept)?.kept).toBe(true);
|
||
const before = totals(kept.towns).you;
|
||
const r = settlePledge(kept)!;
|
||
expect(r.kept).toBe(true);
|
||
expect(r.votes).toBeGreaterThan(0);
|
||
const t = totals(base.towns);
|
||
expect(totals(kept.towns).you - before).toBeCloseTo(PLEDGE_SHARE * (t.you + t.rival + t.others), -2);
|
||
|
||
const broken = structuredClone(base) as RunState;
|
||
broken.pledge = { id: "clean", need: 0 };
|
||
broken.stats.attacks = 2;
|
||
const b = settlePledge(broken)!;
|
||
expect(b.kept).toBe(false);
|
||
expect(b.votes).toBeLessThan(0);
|
||
});
|
||
});
|