import type { RogueView } from "@election2026/protocol"; import { beforeEach, describe, expect, it } from "vitest"; import { dailyPick, loadProgress, recordRun, todayKey } from "../src/game/rogue/progress.ts"; /** 測試用的 localStorage(vitest 跑在 Node,沒有瀏覽器的儲存空間) */ function fakeStorage(): Storage { const m = new Map(); return { get length() { return m.size; }, clear: () => m.clear(), getItem: (k) => m.get(k) ?? null, key: (i) => [...m.keys()][i] ?? null, removeItem: (k) => void m.delete(k), setItem: (k, v) => void m.set(k, String(v)), }; } const finished = (runId: string, won: boolean, ascension = 0, feats: string[] = ["win"], you = { id: "chang-san-cheng", name: "張善政" }): RogueView => ({ runId, scenarioId: "taoyuan-2022-mayor-even", you, options: { ascension, tutorial: false, daily: null, even: true, career: null, }, result: { won, you: 1, rival: 0, others: 0, youShare: won ? 0.5 : 0.4, rivalShare: 0.45, grade: won ? "B" : "C", byTown: [], feats: won ? feats : [], }, }) as unknown as RogueView; describe("選戰卡牌的進度", () => { beforeEach(() => { Object.defineProperty(globalThis, "localStorage", { value: fakeStorage(), configurable: true, }); }); it("贏了解鎖下一級逆風、記成就;同一局重新整理不會重複算", () => { expect(loadProgress().ascension).toBe(0); const first = recordRun(finished("r1", true)); expect(first.unlocked).toBe(1); expect(first.newFeats).toEqual(["win"]); expect(loadProgress().wins).toBe(1); const again = recordRun(finished("r1", true)); expect(again.unlocked).toBeNull(); expect(loadProgress().runs).toBe(1); // 打比已解鎖還低的等級贏了,不會再解鎖 expect(recordRun(finished("r2", true, 0)).unlocked).toBeNull(); // 輸了不解鎖、不算勝場 const lost = recordRun(finished("r3", false, 1)); expect(lost.unlocked).toBeNull(); expect(loadProgress()).toMatchObject({ runs: 3, wins: 2, ascension: 1 }); }); it("記下玩過的真實參選人(參選人卡冊上色用);自創角色不記", () => { recordRun(finished("r1", true)); recordRun(finished("r2", false, 0)); recordRun(finished("r3", true, 0, ["win"], { id: "custom", name: "林小美" })); expect(loadProgress().played).toEqual({ 張善政: { runs: 2, wins: 1, grade: "B", scenarios: ["taoyuan-2022-mayor-even"], }, }); }); it("每日一戰:同一天挑到同一個題目", () => { const items = ["a", "b", "c", "d", "e"]; expect(dailyPick(items, "2026-10-26")).toBe(dailyPick(items, "2026-10-26")); expect(todayKey(new Date(2026, 9, 26))).toBe("2026-10-26"); }); });