import { describe, it, expect } from "vitest"; import { createGame, step, buildGhostRecord, enterGhostBattle, RULE_VERSION, CARDS_HASH, type Action, type GameState, type GhostRecord } from "@wcg/battle-core"; import { buildServer } from "../src/server.js"; import { InMemoryGhostRepository } from "../src/repository.js"; function makeApp(requireVerified = false) { const repo = new InMemoryGhostRepository(() => 0); // 固定 random → 取第 0 筆 return { app: buildServer({ repo, requireVerified }), repo }; } const baseRecord: GhostRecord = { playerMaxHp: 100, turns: [{ atk: 5, def: 0 }, { atk: 3, def: 2 }], seed: 123, ruleVersion: RULE_VERSION, cardsHash: CARDS_HASH, }; /** 跑一小段遊戲,收集真實 actions(多半 GAME_OVER),用來測重播路徑能執行。 */ function shortRun(seed: number): { record: GhostRecord } { let s: GameState = createGame({ classType: "WARRIOR", seed }); const actions: Action[] = []; const apply = (a: Action) => { actions.push(a); s = step(s, a).state; }; for (let i = 0; i < 60 && s.phase === "PLAYER_TURN"; i++) { const p = s.player; // 還能出牌(連擊中或有硬幣)就出第 0 張;否則結束回合,確保每場會結算數個回合、turnLog 有資料。 if (p.hand.length > 0 && (p.combo > 0 || p.coin > 0)) apply({ type: "PLAY_CARD", handIndex: 0 }); else apply({ type: "END_TURN" }); if (s.phase === "REWARD_SELECT") apply({ type: "PICK_REWARD", choiceIndex: 0 }); } return { record: buildGhostRecord(s, { seed, actions }) }; } describe("POST /api/ghosts", () => { it("形態錯誤 → 400", async () => { const { app } = makeApp(); const res = await app.inject({ method: "POST", url: "/api/ghosts", payload: { foo: 1 } }); expect(res.statusCode).toBe(400); }); it("合法但版本不符 → 201、verified=false", async () => { const { app, repo } = makeApp(); const res = await app.inject({ method: "POST", url: "/api/ghosts", payload: { ...baseRecord, ruleVersion: "0.0.0" }, }); expect(res.statusCode).toBe(201); expect(res.json().verified).toBe(false); expect(repo.count()).toBe(1); }); it("無 actions → 入庫但 verified=false(無法重播)", async () => { const { app } = makeApp(); const res = await app.inject({ method: "POST", url: "/api/ghosts", payload: baseRecord }); expect(res.statusCode).toBe(201); expect(res.json().verified).toBe(false); }); it("有 actions 但未破關 → verified=false(重播路徑可執行、不崩潰)", async () => { const { app } = makeApp(); const { record } = shortRun(42); const res = await app.inject({ method: "POST", url: "/api/ghosts", payload: record }); expect(res.statusCode).toBe(201); expect(res.json().verified).toBe(false); // 多半 GAME_OVER → 非 VICTORY }); it("requireVerified=true 時,無法驗證的鬼影 → 422", async () => { const { app } = makeApp(true); const res = await app.inject({ method: "POST", url: "/api/ghosts", payload: baseRecord }); expect(res.statusCode).toBe(422); }); it("turns 過長 → 400(防 DoS)", async () => { const { app } = makeApp(); const huge = { ...baseRecord, turns: Array.from({ length: 2001 }, () => ({ atk: 1, def: 0 })) }; const res = await app.inject({ method: "POST", url: "/api/ghosts", payload: huge }); expect(res.statusCode).toBe(400); }); }); describe("GET /api/ghosts/random", () => { it("無存檔 → 204", async () => { const { app } = makeApp(); const res = await app.inject({ method: "GET", url: "/api/ghosts/random" }); expect(res.statusCode).toBe(204); }); it("入庫(已驗證)後 → 回傳鬼影", async () => { const { app, repo } = makeApp(); repo.add(baseRecord, true); // random 只外送 verified;直接放一筆已驗證鬼影 const res = await app.inject({ method: "GET", url: "/api/ghosts/random" }); expect(res.statusCode).toBe(200); expect(res.json().playerMaxHp).toBe(100); expect(res.json().turns.length).toBe(2); }); it("未驗證鬼影不外送(防作弊鬼影被抽中)→ 204", async () => { const { app, repo } = makeApp(); repo.add(baseRecord, false); // 未驗證 const res = await app.inject({ method: "GET", url: "/api/ghosts/random" }); expect(res.statusCode).toBe(204); }); }); describe("GET /api/ghosts/:id", () => { it("上傳→以回傳 id 取回同一筆", async () => { const { app } = makeApp(); const post = await app.inject({ method: "POST", url: "/api/ghosts", payload: baseRecord }); const id = post.json().id as string; const res = await app.inject({ method: "GET", url: `/api/ghosts/${id}` }); expect(res.statusCode).toBe(200); expect(res.json().id).toBe(id); }); it("不存在 → 404", async () => { const { app } = makeApp(); const res = await app.inject({ method: "GET", url: "/api/ghosts/nope" }); expect(res.statusCode).toBe(404); }); }); describe("M3↔M4 端到端:上傳鬼影 → 取回 → 重建第 12 關對手", () => { it("另一 client 取回的鬼影能正確重建數值", async () => { const { app, repo } = makeApp(); const { record } = shortRun(7); // 真實一段對局產生的存檔(含 turns) expect(record.turns.length).toBeGreaterThan(0); const post = await app.inject({ method: "POST", url: "/api/ghosts", payload: record }); expect(post.statusCode).toBe(201); repo.add(record, true); // random 只外送 verified;放一筆已驗證副本供取回(上傳路徑由 POST 測試另行覆蓋) const got = (await app.inject({ method: "GET", url: "/api/ghosts/random" })).json() as GhostRecord; expect(got.playerMaxHp).toBe(record.playerMaxHp); // 重建:把取回的鬼影接成第 12 關對手 const ghostGame = enterGhostBattle(createGame({ classType: "WARRIOR", seed: 999 }), got); expect(ghostGame.level).toBe(12); expect(ghostGame.opponent.kind).toBe("ghost"); expect(ghostGame.opponent.hp).toBe(got.playerMaxHp); // 鬼影血量 = 該玩家 maxHp expect(ghostGame.opponent.atk).toBe(got.turns[0]!.atk); // 第 1 回合攻防照抄 expect(ghostGame.opponent.def).toBe(got.turns[0]!.def); }); }); describe("健康檢查", () => { it("GET /api/health", async () => { const { app } = makeApp(); const res = await app.inject({ method: "GET", url: "/api/health" }); expect(res.statusCode).toBe(200); expect(res.json().ok).toBe(true); }); });