election-game/backend/packages/engine/test/events.test.ts

82 lines
3.7 KiB
TypeScript

import { describe, expect, it } from "vitest";
import { createMatch, playWeek, viewFor, type EventDef, type World } from "../src/index.ts";
import { tinyWorld } from "./fixtures.ts";
const EVENTS: EventDef[] = [
{
id: "rain",
title: "下雨",
body: "要不要出門?",
art: "x",
weight: 1,
fallback: "stay",
options: [
{ id: "go", label: "出門", outcome: { text: "淋濕了但很有誠意。", effects: [{ stat: "favor", base: 0.05, scope: "city" }], momentum: 0.05, news: { title: "{name}冒雨拜票", tone: "positive" } } },
{ id: "stay", label: "在家", outcome: { text: "沒事。" } },
],
},
{ id: "two", title: "第二個", body: "", art: "x", weight: 1, fallback: "a", options: [{ id: "a", label: "甲", outcome: { text: "甲。" } }, { id: "b", label: "乙", outcome: { text: "乙。", money: -10_000 } }] },
{ id: "three", title: "第三個", body: "", art: "x", weight: 1, fallback: "a", options: [{ id: "a", label: "甲", outcome: { text: "甲。" } }] },
];
const world = (): World => {
const w = tinyWorld();
return { ...w, content: { ...w.content, events: EVENTS } };
};
describe("表態事件", () => {
it("開局就替每位候選人抽一張,玩家視角只看得到選項名稱", () => {
const w = world();
const state = createMatch(w, "m", 1, "alice");
expect(state.candidates.every((c) => c.event !== null)).toBe(true);
const view = viewFor(state, w, "alice");
expect(view.event?.options.length).toBeGreaterThan(0);
expect(JSON.stringify(view.event)).not.toContain("effects");
});
it("玩家的選擇會生效並寫進戰報;沒選就套用預設", () => {
const w = world();
let state = createMatch(w, "m", 2, "alice");
// 讓玩家這週拿到「下雨」
state = { ...state, candidates: state.candidates.map((c) => (c.id === "alice" ? { ...c, event: "rain" } : c)) };
const go = playWeek(state, w, { alice: [] }, { alice: "go" }).state;
const stay = playWeek(state, w, { alice: [] }, {}).state;
const report = (s: typeof go) => s.reports.alice!.find((r) => r.kind === "event")?.text ?? "";
expect(report(go)).toContain("你選了「出門」");
expect(report(stay)).toContain("你選了「在家」");
expect(go.momentum[0]!).toBeGreaterThan(stay.momentum[0]!);
expect(go.news.some((n) => n.title.endsWith("冒雨拜票"))).toBe(true);
});
it("AI 候選人的表態不上新聞(不替真人捏造立場),但效果照算", () => {
const w = world();
let state = createMatch(w, "m", 5, "alice");
state = { ...state, candidates: state.candidates.map((c) => ({ ...c, event: "rain" })) };
const out = playWeek(state, w, {}, { alice: "stay" }).state;
const others = state.candidates.filter((c) => c.id !== "alice").map((c) => c.id);
expect(out.news.some((n) => n.title.endsWith("冒雨拜票"))).toBe(false);
expect(others.every((id) => out.reports[id]!.some((r) => r.kind === "event"))).toBe(true);
});
it("同一場比賽不會抽到重複的事件;抽完就不再有事件", () => {
const w = world();
let state = createMatch(w, "m", 3, "alice");
const seen: string[] = [];
while (state.phase === "campaign") {
const id = state.candidates[0]!.event;
if (id) seen.push(id);
state = playWeek(state, w).state;
}
expect(new Set(seen).size).toBe(seen.length);
expect(seen.length).toBe(EVENTS.length);
});
it("不修改輸入的狀態", () => {
const w = world();
const state = createMatch(w, "m", 4, "alice");
const before = JSON.stringify(state.candidates);
playWeek(state, w, {}, { alice: "go" });
expect(JSON.stringify(state.candidates)).toBe(before);
});
});