File size: 6,496 Bytes
1302ebc
67057ab
1302ebc
67057ab
1302ebc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f608a7c
1302ebc
 
 
 
cac73cf
 
 
 
 
 
 
 
 
1302ebc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
67057ab
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { describe, it, expect } from "vitest";
import { createGame, step, summarizeRun, extremeDeckPool, RULE_VERSION, CARDS_HASH, type Action, type GameState } from "@wcg/battle-core";
import { validateRunReport, processRunReport, type RunReport } from "../src/runService.js";
import { stageModeKey } from "../src/userRepository.js";

/** 純挨打到死:戰士不出攻擊牌、只 END_TURN,最終被怪物打死 → GAME_OVER(確定性,收集真實 actions)。 */
function deathRun(seed: number): { actions: Action[]; final: GameState } {
  let s = createGame({ classType: "WARRIOR", seed });
  const actions: Action[] = [];
  let guard = 0;
  while (s.phase === "PLAYER_TURN" && guard++ < 3000) {
    const a: Action = { type: "END_TURN" };
    actions.push(a);
    s = step(s, a).state;
  }
  return { actions, final: s };
}

const valid = (): RunReport => ({
  seed: 7, actions: [{ type: "END_TURN" }], classType: "WARRIOR", campaign: "slime",
  maxLevel: 11, ruleVersion: RULE_VERSION, cardsHash: CARDS_HASH,
});

describe("validateRunReport", () => {
  it("合法 → null", () => { expect(validateRunReport(valid())).toBeNull(); });
  it("非物件 → 錯誤", () => { expect(validateRunReport(null)).toBeTruthy(); });
  it("缺/非整數 seed → 錯誤", () => { expect(validateRunReport({ ...valid(), seed: 1.5 })).toBeTruthy(); });
  it("classType 非法 → 錯誤", () => { expect(validateRunReport({ ...valid(), classType: "WIZARD" })).toBeTruthy(); });
  it("campaign 非法 → 錯誤", () => { expect(validateRunReport({ ...valid(), campaign: "x" })).toBeTruthy(); });
  it("maxLevel 超界 → 錯誤", () => { expect(validateRunReport({ ...valid(), maxLevel: 0 })).toBeTruthy(); });
  it("actions 非陣列 → 錯誤", () => { expect(validateRunReport({ ...valid(), actions: "x" })).toBeTruthy(); });
  it("缺 ruleVersion → 錯誤", () => { expect(validateRunReport({ ...valid(), ruleVersion: "" })).toBeTruthy(); });
  it("action type 非法 → 錯誤", () => { expect(validateRunReport({ ...valid(), actions: [{ type: "BOOM" } as never] })).toBeTruthy(); });
  it("elapsedSec 合法且遞增 → null", () => {
    expect(validateRunReport({ ...valid(), actions: [{ type: "END_TURN", elapsedSec: 0 }, { type: "END_TURN", elapsedSec: 12 }] })).toBeNull();
  });
  it("elapsedSec 非整數/負值/倒退 → 錯誤", () => {
    expect(validateRunReport({ ...valid(), actions: [{ type: "END_TURN", elapsedSec: -1 }] })).toBeTruthy();
    expect(validateRunReport({ ...valid(), actions: [{ type: "PLAY_CARD", handIndex: 0, elapsedSec: 1.5 }] })).toBeTruthy();
    expect(validateRunReport({ ...valid(), actions: [{ type: "END_TURN", elapsedSec: 10 }, { type: "END_TURN", elapsedSec: 9 }] })).toBeTruthy();
  });
});

describe("processRunReport(後端權威重播)", () => {
  it("版本不符 → version-mismatch(不聚合)", () => {
    expect(processRunReport({ ...valid(), ruleVersion: "0.0.0" }).status).toBe("version-mismatch");
  });

  it("重播後尚未結束(空動作)→ invalid", () => {
    expect(processRunReport({ ...valid(), actions: [] }).status).toBe("invalid");
  });

  it("真實戰敗局 → ok、outcome=GAME_OVER、cleared=false,且與 summarizeRun 一致", () => {
    const { actions, final } = deathRun(7);
    expect(final.phase).toBe("GAME_OVER");
    const r = processRunReport({ ...valid(), actions });
    expect(r.status).toBe("ok");
    if (r.status === "ok") {
      expect(r.summary.outcome).toBe("GAME_OVER");
      expect(r.summary.cleared).toBe(false);
      expect(r.summary).toEqual(summarizeRun(final)); // 權威值=直接 summarize 重播結果
    }
  });
});

// ===== 極難(insane):可後端權威重播驗證,且須附自組 30 張牌 =====

/** 合法的 30 張戰士極難牌組:5 傳說(各1) + 10 史詩(5種各2) + 15 普通(5種各3)。 */
function insaneDeck(): number[] {
  const pool = extremeDeckPool("WARRIOR");
  const legend = pool.legend.slice(0, 5);
  const epic: number[] = []; for (const id of pool.epic.slice(0, 5)) epic.push(id, id);
  const normal: number[] = []; for (const id of pool.normal.slice(0, 5)) normal.push(id, id, id);
  return [...legend, ...epic, ...normal];
}

/** 極難純挨打到死(只 END_TURN):slime insane 攻擊+20% 真傷 → 幾回合內 GAME_OVER。 */
function insaneDeathRun(seed: number, deck: number[]): { actions: Action[]; final: GameState } {
  let s = createGame({ classType: "WARRIOR", seed, campaign: "slime", extreme: true, deckOverride: deck });
  const actions: Action[] = [];
  let guard = 0;
  while (s.phase === "PLAYER_TURN" && guard++ < 3000) {
    const a: Action = { type: "END_TURN" };
    actions.push(a);
    s = step(s, a).state;
  }
  return { actions, final: s };
}

describe("極難(insane) 回報驗證與重播", () => {
  const base = (): RunReport => {
    const deck = insaneDeck();
    return { seed: 5, actions: [{ type: "SURRENDER" }], classType: "WARRIOR", campaign: "slime", maxLevel: 11, hardMode: true, extreme: true, deckOverride: deck, ruleVersion: RULE_VERSION, cardsHash: CARDS_HASH };
  };

  it("合法 insane 回報(附 30 張牌)→ null", () => { expect(validateRunReport(base())).toBeNull(); });
  it("extreme 但缺牌組 → 錯誤", () => { expect(validateRunReport({ ...base(), deckOverride: undefined })).toBeTruthy(); });
  it("extreme 但牌組張數不符 → 錯誤", () => { expect(validateRunReport({ ...base(), deckOverride: insaneDeck().slice(0, 29) })).toBeTruthy(); });
  it("非極難卻帶 deckOverride → 錯誤", () => { expect(validateRunReport({ ...base(), extreme: false })).toBeTruthy(); });

  it("真實 insane 戰敗局 → ok、extreme=true、與 summarizeRun 一致", () => {
    const deck = insaneDeck();
    const { actions, final } = insaneDeathRun(321, deck);
    expect(final.phase).toBe("GAME_OVER");
    expect(final.extreme).toBe(true);
    const r = processRunReport({ ...base(), seed: 321, actions, deckOverride: deck });
    expect(r.status).toBe("ok");
    if (r.status === "ok") {
      expect(r.summary.extreme).toBe(true);
      expect(r.summary.outcome).toBe("GAME_OVER");
      expect(r.summary).toEqual(summarizeRun(final)); // 權威重播一致
    }
  });

  it("難度 key:insane → campaign:extreme", () => {
    expect(stageModeKey("slime", true, true)).toBe("slime:extreme");
    expect(stageModeKey("sanctus", true, false)).toBe("sanctus:hard");
    expect(stageModeKey("slime", false, false)).toBe("slime");
  });
});