151 lines
8.3 KiB
TypeScript
151 lines
8.3 KiB
TypeScript
|
|
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<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 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": {}, "<script>": {}, "Bad Id": { at: 1 } },
|
|||
|
|
log: [{ at: "2026-10-01", free: 3, paid: 0, reason: "當選" }, { reason: 5 }, null, { at: "x", free: "1e9", paid: 0, reason: "怪" }],
|
|||
|
|
});
|
|||
|
|
expect(s.coins).toEqual({ free: 0, paid: 3 });
|
|||
|
|
expect(Object.keys(s.owned).sort()).toEqual(["hat-bucket", "hat-from-next-version"]);
|
|||
|
|
expect(s.log).toHaveLength(2);
|
|||
|
|
expect(s.log[1]!.free).toBe(0);
|
|||
|
|
localStorage.setItem(WARDROBE_KEY, "{not json");
|
|||
|
|
expect(loadWardrobe().coins).toEqual({ free: 0, paid: 0 });
|
|||
|
|
});
|
|||
|
|
});
|
|||
|
|
|
|||
|
|
describe("部件拿到了沒", () => {
|
|||
|
|
const progress = { feats: { win: "2026-10-20" }, wins: 1, runs: 1 };
|
|||
|
|
it("免費的一開始就有;成就、累積成績解鎖;商店的要買", () => {
|
|||
|
|
expect(itemState(wearItem("held-megaphone")!, progress, { owned: {} }).owned).toBe(true);
|
|||
|
|
expect(itemState(wearItem("chest-corsage")!, progress, { owned: {} }).owned).toBe(true);
|
|||
|
|
expect(itemState(wearItem("held-mic")!, progress, { owned: {} })).toMatchObject({ owned: false, how: "拿到成就「辯論三戰全勝」" });
|
|||
|
|
// 謝票帶:落選也打完一局(局數 - 勝場)
|
|||
|
|
expect(itemState(wearItem("sash-thanks")!, progress, { owned: {} }).owned).toBe(false);
|
|||
|
|
expect(itemState(wearItem("sash-thanks")!, { ...progress, runs: 2 }, { owned: {} }).owned).toBe(true);
|
|||
|
|
expect(itemState(wearItem("hat-bamboo")!, progress, { owned: {} })).toMatchObject({ owned: false, price: 20 });
|
|||
|
|
expect(itemState(wearItem("hat-bamboo")!, progress, { owned: { "hat-bamboo": { at: "" } } }).owned).toBe(true);
|
|||
|
|
});
|
|||
|
|
|
|||
|
|
it("穿上、脫掉、換色都產生新的一份(不改舊的),換色跟著部件走", () => {
|
|||
|
|
const base = { ...legacyAvatar };
|
|||
|
|
const on = wear(base, "hat", "hat-campaign-cap");
|
|||
|
|
expect(base).toEqual(legacyAvatar);
|
|||
|
|
expect(on.wear).toEqual({ hat: "hat-campaign-cap" });
|
|||
|
|
expect(on.v).toBe(2);
|
|||
|
|
const red = setTint(on, "hat", "main", "#d61f2c");
|
|||
|
|
expect(red.tint).toEqual({ "hat.main": "#d61f2c" });
|
|||
|
|
const off = wear(red, "hat", null);
|
|||
|
|
expect(off.wear).toBeUndefined();
|
|||
|
|
expect(off.tint).toBeUndefined();
|
|||
|
|
});
|
|||
|
|
});
|
|||
|
|
|
|||
|
|
describe("讀存檔時清洗頭像", () => {
|
|||
|
|
it("自創角色:舊存檔原封不動;看不懂的代號換成預設;壞掉的角色丟掉", async () => {
|
|||
|
|
const odd = { ...legacyAvatar, hair: "mohawk-2099", wear: { hat: "hat-campaign-cap", pet: "pet-dragon" } };
|
|||
|
|
localStorage.setItem(
|
|||
|
|
"tongsuan.characters",
|
|||
|
|
JSON.stringify([
|
|||
|
|
{ id: "a", name: "舊角色", party: null, background: "律師", stats: { fame: 5, eloquence: 5, strategy: 5, wealth: 5, network: 5 }, avatar: legacyAvatar },
|
|||
|
|
{ id: "b", name: "怪角色", party: null, background: "律師", stats: { fame: 5, eloquence: 5, strategy: 5, wealth: 5, network: 5 }, avatar: odd },
|
|||
|
|
"壞掉",
|
|||
|
|
{ name: "沒有 id" },
|
|||
|
|
]),
|
|||
|
|
);
|
|||
|
|
const { loadCharacters } = await import("../src/game/creator/characters.ts");
|
|||
|
|
const list = loadCharacters();
|
|||
|
|
expect(list.map((c) => c.id)).toEqual(["a", "b"]);
|
|||
|
|
expect(list[0]!.avatar).toEqual(legacyAvatar);
|
|||
|
|
expect(list[1]!.avatar.hair).toBe("short");
|
|||
|
|
expect(list[1]!.avatar.wear).toEqual({ hat: "hat-campaign-cap" });
|
|||
|
|
});
|
|||
|
|
|
|||
|
|
it("真實人物改過的頭像:realPersonOk:false 的部件和商店部件拿掉", async () => {
|
|||
|
|
localStorage.setItem("tongsuan.avatars", JSON.stringify({ 測試政治人物: { ...legacyAvatar, wear: { hat: "hat-victory-band", held: "held-megaphone", sash: "sash-red", outer: "outer-campaign-vest" } } }));
|
|||
|
|
vi.resetModules();
|
|||
|
|
const { candidateAvatar } = await import("../src/game/art/candidateAvatar.ts");
|
|||
|
|
const spec = candidateAvatar({ name: "測試政治人物" });
|
|||
|
|
expect(spec.wear).toEqual({ sash: "sash-red", outer: "outer-campaign-vest" });
|
|||
|
|
});
|
|||
|
|
});
|