File size: 6,524 Bytes
9f0c00b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f84ef6e
 
 
 
9f0c00b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5f82f13
 
 
9f0c00b
 
 
 
 
5f82f13
 
 
 
 
 
 
9f0c00b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5f82f13
9f0c00b
 
 
 
 
 
5f82f13
9f0c00b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
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);
  });
});