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

33 lines
2.5 KiB
TypeScript
Raw Permalink 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 type { NewsItem, Poll } from "@election2026/protocol";
import { describe, expect, it } from "vitest";
import { drawPress, pressHits, type PressDef } from "../src/press.ts";
import type { MatchState, Scenario } from "../src/types.ts";
const scenario = { weeks: 10, candidates: [{ id: "me", name: "我" }, { id: "rival", name: "對手" }] } as unknown as Scenario;
const state = { seed: 1, week: 3, candidates: [{ id: "me", withdrawn: false }, { id: "rival", withdrawn: false }] } as unknown as MatchState;
const news = (n: Partial<NewsItem>): NewsItem => ({ id: "n", week: 3, tier: "main", title: "標題", candidateId: null, townCode: null, tone: "neutral", topic: null, targetId: null, art: null, ...n });
const poll = (shares: Record<string, number>): Poll => ({ id: "p", week: 3, kind: "public", pollster: "x", sampleSize: 1000, marginOfError: 3, shares, undecided: 0.1, byTown: null });
const def = (id: string, trigger: PressDef["trigger"]): PressDef => ({ id, trigger, question: "{opponent}?", fallback: "a", options: [{ id: "a", label: "A", hint: "", outcome: { text: "" } }] });
describe("記者堵麥", () => {
it("依新聞判斷記者會追問什麼", () => {
const hits = pressHits(scenario, state, "me", [
news({ candidateId: "rival", targetId: "me", title: "對手攻擊我" }),
news({ candidateId: "me", tone: "negative", title: "我出包" }),
news({ candidateId: "rival", tone: "negative", title: "對手出包" }),
news({ candidateId: "me", tone: "positive", tier: "headline", title: "我造勢成功" }),
news({ candidateId: "me", tone: "negative", topic: "press:attacked", title: "我回應記者被批" }),
], poll({ me: 0.3, rival: 0.5 }));
expect(hits.map((h) => h.trigger)).toEqual(["attacked", "own_scandal", "opp_scandal", "my_win", "poll_behind"]);
expect(hits[0]!.opponent).toBe("rival");
});
it("最多兩題、依優先順序;沒事就問平靜週的題目", () => {
const defs = [def("x", "attacked"), def("y", "own_scandal"), def("z", "my_win"), def("q", "quiet")];
const busy = drawPress(defs, scenario, state, "me", [news({ candidateId: "me", tone: "positive", tier: "headline" }), news({ candidateId: "rival", targetId: "me" }), news({ candidateId: "me", tone: "negative" })], null);
expect(busy.map((q) => q.trigger)).toEqual(["own_scandal", "attacked"]);
expect(busy[1]!.vars.opponent).toBe("對手");
expect(drawPress(defs, scenario, state, "me", [], null).map((q) => q.trigger)).toEqual(["quiet"]);
});
});