import { readFileSync } from "node:fs";
import { describe, expect, it } from "vitest";
import { ART_OUT, CATALOG_OUT, buildAvatarCatalog } from "../scripts/build-avatar-catalog.ts";
import {
AVATAR_CATALOG,
AvatarSpecSchema,
CustomCandidate,
checkAvatar,
cleanAvatar,
lintCatalog,
onSale,
parseAvatarLoose,
shopShelf,
shopWeek,
slotItems,
stampAvatarVersion,
wearItem,
wearSlots,
type AvatarSpecData,
} from "../src/index.ts";
import { convertSvgArt, SvgArtError } from "../src/avatar/svg.ts";
/** 改版前(寫死選項那一版)存下來的頭像:每個舊選項都要照樣合法、清洗後一個字都不變 */
const legacy: AvatarSpecData = {
skin: "#f6d3b7",
hair: "bobSide",
hairColor: "#2f2520",
outfit: "blouse",
suit: "#5a2d34",
shirt: "#f3e6d8",
tie: "#2c5aa0",
glasses: true,
beard: false,
bg: "#efe0e6",
face: "heart",
faceW: 0.95,
faceH: 1.06,
ears: "small",
brows: "thin",
browAngle: -0.3,
eyes: "narrow",
glassesStyle: "halfRim",
glassesColor: "#a8874f",
nose: "small",
mouth: "teeth",
lines: 1,
part: "right",
mustache: false,
};
const OLD_OPTIONS = {
hair: ["short", "side", "buzz", "bob", "long", "bun", "curly", "bald", "receding", "slick", "bobSide", "shortBob", "perm", "crew", "volume"],
outfit: ["suit", "blouse", "vest", "polo"],
face: ["oval", "round", "long", "square", "heart"],
ears: ["small", "normal", "big"],
brows: ["thin", "normal", "thick"],
eyes: ["round", "narrow", "dot", "smile"],
glassesStyle: ["metal", "rimless", "black", "round", "halfRim"],
nose: ["small", "normal", "big"],
mouth: ["smile", "grin", "closed", "teeth"],
} as const;
describe("頭像資料:舊存檔照樣過", () => {
it("改版前的完整頭像:格式合法、目錄檢查沒問題、清洗後是同一個物件", () => {
const parsed = AvatarSpecSchema.parse(legacy);
expect(parsed).toEqual(legacy);
expect(checkAvatar(parsed)).toEqual([]);
expect(cleanAvatar(parsed).spec).toBe(parsed);
});
it("每一個舊選項都在目錄裡(代號沿用)", () => {
for (const [field, values] of Object.entries(OLD_OPTIONS)) {
for (const v of values) {
const spec = { ...legacy, [field]: v };
expect(checkAvatar(AvatarSpecSchema.parse(spec)), `${field}=${v}`).toEqual([]);
}
}
});
it("只有必填欄位的最舊存檔也合法;自創角色照樣能開局", () => {
const minimal = { skin: "#eebd98", hair: "short", hairColor: "#1f1b18", outfit: "suit", suit: "#24324a", shirt: "#f7f5f0", tie: "#b3262d", glasses: false, beard: false, bg: "#dfe8f0" };
expect(cleanAvatar(AvatarSpecSchema.parse(minimal)).notes).toEqual([]);
const custom = CustomCandidate.parse({ name: "測試人", party: null, background: "律師", stats: { fame: 5, eloquence: 5, strategy: 5, wealth: 5, network: 5 }, avatar: legacy, replace: null });
expect(custom.avatar).toEqual(legacy);
});
it("新欄位(畫風、新部位、換色)也合法", () => {
const spec = AvatarSpecSchema.parse({ ...legacy, v: 2, style: "chibi", wear: { hat: "hat-campaign-cap", sash: "sash-red" }, tint: { "hat.main": "#c0392b" } });
expect(checkAvatar(spec)).toEqual([]);
expect(cleanAvatar(spec).spec).toBe(spec);
});
});
describe("頭像資料:看不懂的代號換預設、格式壞掉才擋", () => {
it("看不懂的髮型、服裝換成預設值;看不懂的五官拿掉(畫的時候用預設)", () => {
const spec = AvatarSpecSchema.parse({ ...legacy, hair: "mohawk-2099", outfit: "spacesuit", eyes: "laser", glassesStyle: "monocle" });
const { spec: out, notes } = cleanAvatar(spec);
expect(out.hair).toBe("short");
expect(out.outfit).toBe("suit");
expect(out.eyes).toBeUndefined();
expect(out.glassesStyle).toBeUndefined();
expect(notes.length).toBe(4);
expect(checkAvatar(out)).toEqual([]);
});
it("看不懂的部件、部位、畫風拿掉;穿錯部位的拿掉;沒穿的部件的換色也拿掉", () => {
const spec = AvatarSpecSchema.parse({
...legacy,
style: "oil-painting",
wear: { hat: "hat-from-the-future", pet: "pet-cat", sash: "hat-campaign-cap", outer: "outer-campaign-vest" },
tint: { "hat.main": "#123456", "outer.main": "#654321", "outer.glitter": "#ffffff" },
});
const { spec: out } = cleanAvatar(spec);
expect(out.style).toBeUndefined();
expect(out.wear).toEqual({ outer: "outer-campaign-vest" });
expect(out.tint).toEqual({ "outer.main": "#654321" });
});
it("格式壞掉才回錯誤:代號有怪字元、穿太多件、顏色不是 #RRGGBB", () => {
expect(AvatarSpecSchema.safeParse({ ...legacy, hair: "")],
["事件屬性", bad('')],
["外部連結", bad('')],
["use 連結", bad('')],
["style", bad("")],
["style 屬性", bad('')],
["url() 填色", bad('')],
["文字", bad("凍蒜")],
["超出畫布", bad('')],
["transform 移出畫布", bad('')],
["顏色名稱", bad('')],
["DOCTYPE", `]>${ok}`],
["viewBox 不對", ok.replace("0 0 200 220", "0 0 400 400")],
["foreignObject", bad("")],
])("擋掉:%s", (_name, src) => {
expect(() => convertSvgArt(src)).toThrow(SvgArtError);
});
});
describe("服裝店每週上架", () => {
const monday = new Date("2026-10-26T00:00:00+08:00");
it("台灣時間星期一換週;同一週所有人看到一樣", () => {
expect(shopWeek(new Date("2026-10-25T23:59:00+08:00"))).toBe(shopWeek(monday) - 1);
expect(shopShelf(monday).weekly.map((i) => i.id)).toEqual(shopShelf(new Date("2026-11-01T20:00:00+08:00")).weekly.map((i) => i.id));
});
it("每週 6 件;基本款常駐;本週和常駐不重複", () => {
const s = shopShelf(monday);
expect(s.weekly).toHaveLength(6);
expect(s.always.map((i) => i.id).sort()).toEqual(["hat-bucket", "held-tissue", "mask-number", "outer-volunteer-vest"]);
expect(s.weekly.some((i) => s.always.includes(i))).toBe(false);
});
it("每件輪替的部件一年內一定會再上架", () => {
const rotating = AVATAR_CATALOG.items.filter((i) => i.get.type === "shop" && i.get.shelf === "rotate" && !i.available);
const last = new Map();
let maxGap = 0;
const w0 = shopWeek(monday);
for (let w = w0; w < w0 + 156; w++) {
for (const it of shopShelf(new Date(+monday + (w - w0) * 7 * 86_400_000)).weekly) {
const prev = last.get(it.id);
if (prev !== undefined) maxGap = Math.max(maxGap, w - prev);
last.set(it.id, w);
}
}
for (const it of rotating) expect(last.has(it.id), it.id).toBe(true);
expect(maxGap).toBeLessThanOrEqual(52);
});
it("限定品只在每年選舉季(11、12 月)上架,隔年同一個時候回來", () => {
const neon = wearItem("frame-neon")!;
expect(onSale(neon, "2026-11-15")).toBe(true);
expect(onSale(neon, "2027-01-02")).toBe(false);
expect(onSale(neon, "2027-12-01")).toBe(true);
expect(shopShelf(new Date("2026-11-16T12:00:00+08:00")).limited.map((i) => i.id)).toEqual(["frame-neon"]);
expect(shopShelf(new Date("2027-03-16T12:00:00+08:00")).limited).toEqual([]);
});
});