76 lines
4.3 KiB
TypeScript
76 lines
4.3 KiB
TypeScript
|
|
import type { Order } from "@election2026/protocol";
|
||
|
|
import { describe, expect, it } from "vitest";
|
||
|
|
import { comboBonus, COMBO_MAX, COMBO_STEP, COUNTER_BONUS, lastWeekVisits, mainRival, planTactics } from "../src/tactics.ts";
|
||
|
|
import type { ActionDef, MatchState, Scenario } from "../src/types.ts";
|
||
|
|
|
||
|
|
const scenario = { candidates: [{ id: "me", name: "我", slot: "a" }, { id: "rival", name: "對手", slot: "b" }, { id: "minor", name: "小黨", slot: "c" }] } as unknown as Scenario;
|
||
|
|
const action = (id: string, category: ActionDef["category"], visibility: ActionDef["visibility"] = "public") => ({ id, category, visibility }) as ActionDef;
|
||
|
|
const actions = new Map(
|
||
|
|
[action("street_canvass", "ground"), action("market_visit", "ground"), action("temple_visit", "ritual"), action("tv_ad", "media"), action("vote_buying", "dark", "secret")].map((a) => [a.id, a]),
|
||
|
|
);
|
||
|
|
const order = (actionId: string, target: string | null, o: Partial<Order> = {}): Order => ({
|
||
|
|
actor: "candidate",
|
||
|
|
staffId: null,
|
||
|
|
slot: "weekdayMorning",
|
||
|
|
actionId,
|
||
|
|
target,
|
||
|
|
amount: null,
|
||
|
|
opponentId: null,
|
||
|
|
issueIds: null,
|
||
|
|
...o,
|
||
|
|
});
|
||
|
|
const state = (lastWeek: Record<string, Order[]>, week = 3) =>
|
||
|
|
({ week, polls: [], log: [{ week: week - 1, orders: lastWeek }], candidates: [{ withdrawn: false }, { withdrawn: false }, { withdrawn: false }] }) as unknown as MatchState;
|
||
|
|
|
||
|
|
describe("組合拳", () => {
|
||
|
|
it("同一區不同的行程越多,加成越大,有上限", () => {
|
||
|
|
expect(comboBonus(1)).toBe(0);
|
||
|
|
expect(comboBonus(2)).toBeCloseTo(COMBO_STEP);
|
||
|
|
expect(comboBonus(9)).toBeCloseTo(COMBO_MAX);
|
||
|
|
});
|
||
|
|
|
||
|
|
it("只算公開、打在同一區的不同行程;同一張牌打兩次不算", () => {
|
||
|
|
const mine = [order("street_canvass", "A"), order("street_canvass", "A"), order("market_visit", "A"), order("tv_ad", null), order("street_canvass", "B")];
|
||
|
|
const t = planTactics(state({}), scenario, actions, [mine, [], []]);
|
||
|
|
expect(t.multiplier(0, mine[0]!)).toBeCloseTo(1 + COMBO_STEP);
|
||
|
|
expect(t.multiplier(0, mine[4]!)).toBe(1);
|
||
|
|
expect(t.multiplier(0, mine[3]!)).toBe(1);
|
||
|
|
expect(t.notes).toEqual([{ ci: 0, kind: "combo", townCode: "A", bonus: comboBonus(2), kinds: 2 }]);
|
||
|
|
});
|
||
|
|
});
|
||
|
|
|
||
|
|
describe("打對台", () => {
|
||
|
|
it("到對手上週公開去過的區跑地面行程才有加成", () => {
|
||
|
|
const last = { rival: [order("street_canvass", "A"), order("vote_buying", "B", { actor: "team", staffId: "staff-1", slot: "midnight" })] };
|
||
|
|
const mine = [order("market_visit", "A"), order("market_visit", "B"), order("tv_ad", null)];
|
||
|
|
const t = planTactics(state(last), scenario, actions, [mine, [], []]);
|
||
|
|
expect(t.multiplier(0, mine[0]!)).toBeCloseTo(1 + COUNTER_BONUS);
|
||
|
|
// 對手在 B 是午夜祕密行動,不算公開去過
|
||
|
|
expect(t.multiplier(0, mine[1]!)).toBe(1);
|
||
|
|
expect(t.notes.filter((n) => n.kind === "counter")).toEqual([{ ci: 0, kind: "counter", townCode: "A", bonus: COUNTER_BONUS, rival: 1, byCandidate: true }]);
|
||
|
|
});
|
||
|
|
|
||
|
|
it("自己上週去過的區不算打對台", () => {
|
||
|
|
const mine = [order("market_visit", "A")];
|
||
|
|
const t = planTactics(state({ me: [order("street_canvass", "A")] }), scenario, actions, [mine, [], []]);
|
||
|
|
expect(t.multiplier(0, mine[0]!)).toBe(1);
|
||
|
|
});
|
||
|
|
|
||
|
|
it("上週足跡:只算候選人本人,每人每區一筆", () => {
|
||
|
|
const last = { rival: [order("street_canvass", "A", { actor: "team", staffId: "staff-1" }), order("temple_visit", "A"), order("market_visit", "A")] };
|
||
|
|
expect(lastWeekVisits(state(last), scenario, actions)).toEqual([{ ci: 1, townCode: "A", actionId: "temple_visit" }]);
|
||
|
|
// 只看上一週:兩週前的足跡不算
|
||
|
|
expect(lastWeekVisits({ ...state(last), week: 4 }, scenario, actions)).toEqual([]);
|
||
|
|
});
|
||
|
|
|
||
|
|
it("只對主要對手打對台:沒有民調時看兩大黨,有民調時看民調最高的對手", () => {
|
||
|
|
const last = { minor: [order("street_canvass", "A")] };
|
||
|
|
const mine = [order("market_visit", "A")];
|
||
|
|
expect(mainRival(state(last), scenario, 0)).toBe(1);
|
||
|
|
expect(planTactics(state(last), scenario, actions, [mine, [], []]).multiplier(0, mine[0]!)).toBe(1);
|
||
|
|
const polled = { ...state(last), polls: [{ kind: "public", shares: { me: 0.4, rival: 0.1, minor: 0.3 } }] } as unknown as MatchState;
|
||
|
|
expect(mainRival(polled, scenario, 0)).toBe(2);
|
||
|
|
expect(planTactics(polled, scenario, actions, [mine, [], []]).multiplier(0, mine[0]!)).toBeCloseTo(1 + COUNTER_BONUS);
|
||
|
|
});
|
||
|
|
});
|