election-game/web/test/rogue-progress.test.ts

124 lines
4.9 KiB
TypeScript
Raw 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 { RogueView } from "@election2026/protocol";
import { beforeEach, describe, expect, it } from "vitest";
import { completion, dailyPick, loadProgress, PRESIDENT_CODE, recordRun, TERRITORIES, todayKey } from "../src/game/rogue/progress.ts";
/** 測試用的 localStorage(vitest 跑在 Node,沒有瀏覽器的儲存空間) */
function fakeStorage(): Storage {
const m = new Map<string, string>();
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");
});
});
describe("完成度:全台制霸、類型逆風挑戰(docs/25)", () => {
beforeEach(() => {
Object.defineProperty(globalThis, "localStorage", {
value: fakeStorage(),
configurable: true,
});
});
const inCounty = (view: RogueView, countyCode: string, archetype: string | null): RogueView =>
({ ...view, countyCode, identity: archetype ? { archetype, name: "媒體型", skill: "", weakness: "", basis: null, home: "", rivalHome: "" } : null }) as unknown as RogueView;
it("贏了才插旗;同一個縣市只算第一次;總統大選是另一塊地盤", () => {
expect(recordRun(inCounty(finished("a", false), "68000", null)).conquered).toBeNull();
expect(recordRun(inCounty(finished("b", true), "68000", null)).conquered).toBe("桃園市");
expect(recordRun(inCounty(finished("c", true), "68000", null)).conquered).toBeNull();
expect(recordRun(inCounty(finished("d", true), PRESIDENT_CODE, null)).conquered).toBe("總統大選");
expect(Object.keys(loadProgress().conquered ?? {}).sort()).toEqual([PRESIDENT_CODE, "68000"].sort());
});
it("類型各自記最高贏到的逆風;完成度是四項的平均", () => {
expect(recordRun(inCounty(finished("a", true, 0), "68000", "media")).typeRecord).toEqual({ archetype: "media", level: 0 });
expect(recordRun(inCounty(finished("b", true, 1), "68000", "media")).typeRecord).toEqual({ archetype: "media", level: 1 });
expect(recordRun(inCounty(finished("c", true, 0), "68000", "media")).typeRecord).toBeNull();
expect(recordRun(inCounty(finished("d", false, 3), "68000", "finance")).typeRecord).toBeNull();
const { parts, percent } = completion(loadProgress(), 22);
const by = Object.fromEntries(parts.map((p) => [p.id, p]));
expect(by.map).toMatchObject({ done: 1, total: TERRITORIES.length });
expect(by.types).toMatchObject({ done: 1, total: 6 });
expect(by.climb).toMatchObject({ done: 2, total: 66 });
expect(by.feats).toMatchObject({ done: 1, total: 22 });
expect(percent).toBeCloseTo((1 / 23 + 1 / 6 + 2 / 66 + 1 / 22) / 4);
});
});