import { shopShelf, wearItem, type AvatarSpecData } from "@election2026/protocol"; import { beforeEach, describe, expect, it, vi } from "vitest"; import { itemState, setTint, wear } from "../src/game/shop/unlocks.ts"; import { WARDROBE_KEY, buyItem, coinsForRun, earnCoins, loadWardrobe, sanitizeWardrobe, totalCoins } from "../src/game/shop/wardrobe.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 legacyAvatar: AvatarSpecData = { skin: "#f6d3b7", hair: "bobSide", hairColor: "#2f2520", outfit: "blouse", suit: "#5a2d34", shirt: "#f3e6d8", tie: "#2c5aa0", glasses: true, beard: false, bg: "#efe0e6", face: "heart", eyes: "narrow", glassesStyle: "halfRim", part: "right" }; beforeEach(() => { vi.stubGlobal("localStorage", fakeStorage()); }); describe("小鎮幣怎麼發", () => { it("當選 3、落選 2、S 評價 +2、新成就每個 +1、每日一戰 +2、政治生涯過關 +2", () => { expect(coinsForRun({ won: true, grade: "A" }).coins).toBe(3); expect(coinsForRun({ won: false, grade: "C" }).coins).toBe(2); expect(coinsForRun({ won: true, grade: "S", newFeats: 2, daily: true, career: true })).toEqual({ coins: 11, reason: "當選 +3、S 評價 +2、新成就 +2、每日一戰 +2、政治生涯過關 +2" }); // 政治生涯落選不算過關 expect(coinsForRun({ won: false, career: true }).coins).toBe(2); }); }); describe("小鎮幣記帳", () => { it("發的記在「免費取得」那本;同一局只發一次;每筆都留紀錄", () => { expect(earnCoins(5, "當選 +3、S 評價 +2", "run-1")).toBe(true); expect(earnCoins(5, "重新整理又算一次", "run-1")).toBe(false); expect(earnCoins(2, "落選也打完 +2", "run-2")).toBe(true); const s = loadWardrobe(); expect(s.coins).toEqual({ free: 7, paid: 0 }); expect(s.log.map((e) => [e.free, e.paid, e.runId])).toEqual([ [5, 0, "run-1"], [2, 0, "run-2"], ]); expect(earnCoins(0, "沒有")).toBe(false); expect(earnCoins(-3, "負的")).toBe(false); }); it("買東西先扣免費的、不夠再扣付費的;買過不能再買;錢不夠不能買", () => { const now = new Date("2026-10-28T12:00:00+08:00"); localStorage.setItem(WARDROBE_KEY, JSON.stringify({ coins: { free: 5, paid: 10 }, owned: {}, log: [] })); // 漁夫帽是基本款,一直都買得到(8 枚) expect(buyItem("hat-bucket", now)).toEqual({ ok: true }); const s = loadWardrobe(); expect(s.coins).toEqual({ free: 0, paid: 7 }); expect(s.owned["hat-bucket"]).toBeDefined(); expect(s.log.at(-1)).toMatchObject({ free: -5, paid: -3, itemId: "hat-bucket", reason: "買「漁夫帽」" }); expect(buyItem("hat-bucket", now)).toEqual({ ok: false, reason: "owned" }); expect(buyItem("outer-volunteer-vest", now)).toEqual({ ok: false, reason: "poor" }); expect(totalCoins(loadWardrobe())).toBe(7); }); it("這週沒上架的、免費的、解鎖的、不存在的都不能用小鎮幣買", () => { const now = new Date("2026-10-28T12:00:00+08:00"); localStorage.setItem(WARDROBE_KEY, JSON.stringify({ coins: { free: 999, paid: 0 }, owned: {}, log: [] })); const shelf = shopShelf(now); const offShelf = ["hat-victory-band", "hat-bamboo", "hat-helmet", "outer-hivis", "held-fan", "held-thermos", "frame-billboard", "frame-newspaper"].find((id) => !shelf.weekly.some((i) => i.id === id))!; expect(buyItem(offShelf, now)).toEqual({ ok: false, reason: "notForSale" }); // 限定品只在 11、12 月 expect(buyItem("frame-neon", now)).toEqual({ ok: false, reason: "notForSale" }); expect(buyItem("frame-neon", new Date("2026-11-20T12:00:00+08:00"))).toEqual({ ok: true }); expect(buyItem("sash-red", now)).toEqual({ ok: false, reason: "unknown" }); expect(buyItem("chest-corsage", now)).toEqual({ ok: false, reason: "unknown" }); expect(buyItem("hat-from-the-future", now)).toEqual({ ok: false, reason: "unknown" }); }); it("清洗:壞掉的存檔不會當機;負數、小數、怪代號、壞紀錄都丟掉;看不懂但格式對的部件代號照留", () => { expect(sanitizeWardrobe("garbage")).toEqual({ coins: { free: 0, paid: 0 }, owned: {}, log: [] }); const s = sanitizeWardrobe({ coins: { free: -4, paid: 3.7 }, owned: { "hat-bucket": { at: "2026-10-01" }, "hat-from-next-version": {}, "