// Run inside the 3miz repository: node --test --import tsx games/chess/tests/game.test.ts import { test } from "node:test"; import assert from "node:assert/strict"; import { applyInput, initTable, type CreateTable, type Input } from "@3miz/game-server-sdk"; import { chess, isCheckmate, isKingInCheck, isStalemate, hasInsufficientMaterial, type State } from "../server/game.ts"; export const squareIndex = (name: string): number => { const f = name.charCodeAt(0) - 97; const r = parseInt(name[1], 10) - 1; return r * 8 + f; }; const create = (bots: boolean): CreateTable => ({ v: 1, table_id: "0192f0c1-1111-7aaa-8bbb-000000000099", game_id: "chess", game_version: "1.0.0", modes: ["classic"], options: {}, customization: { schema_version: 1, values: {} }, packs: [], seed: Buffer.alloc(32, 7).toString("base64"), created_at_ms: 1_700_000_000_000, observers_allowed: true, context: { rated: false, source: "manual" }, participants: [ { user_id: "u0", kind: "user", seat: 0, display: { name: "A" } }, bots ? { user_id: "b1", kind: "bot", seat: 1, display: { name: "Bot" }, bot_level: "hard" } : { user_id: "u1", kind: "user", seat: 1, display: { name: "B" } }, ], }); const whiteSeatOf = (s: State) => s.players.find((p) => p.color === "w")!.seat; const blackSeatOf = (s: State) => s.players.find((p) => p.color === "b")!.seat; const userOf = (s: State, seat: number) => s.players.find((p) => p.seat === seat)!.user_id; test("chess init: white back rank on rank 1, black on rank 8, white to move", () => { const { w } = initTable(chess, create(false), 1); const s = w.game as State; assert.equal(s.phase, "play"); assert.equal(s.board.length, 64); // White home rank (a1..h1) and black home rank (a8..h8) assert.deepEqual(s.board[squareIndex("a1")], { type: "r", color: "w" }); assert.deepEqual(s.board[squareIndex("h1")], { type: "r", color: "w" }); assert.deepEqual(s.board[squareIndex("e1")], { type: "k", color: "w" }); assert.deepEqual(s.board[squareIndex("a8")], { type: "r", color: "b" }); assert.deepEqual(s.board[squareIndex("h8")], { type: "r", color: "b" }); assert.deepEqual(s.board[squareIndex("e8")], { type: "k", color: "b" }); // White moves first, whoever holds the white seat assert.equal(s.players[s.turn].color, "w"); }); test("white pawn advances toward rank 8 (e2 -> e4)", () => { const { w } = initTable(chess, create(false), 1); const s0 = w.game as State; const seat = whiteSeatOf(s0); assert.equal(s0.turn, seat); const result = applyInput(chess, w, { v: 1, type: "move", payload: { from: "e2", to: "e4" }, uid: userOf(s0, seat), ci: "c0", cid: 1, role: "player", seat, ts_ms: 1_700_000_001_000, } as Input, 1); assert.equal(result.status, "accepted"); const s = w.game as State; assert.equal(s.board[squareIndex("e2")], null); assert.deepEqual(s.board[squareIndex("e4")], { type: "p", color: "w" }); assert.equal(s.turn, 1 - seat); }); test("white pawn cannot move backwards (e2 -> e1)", () => { const { w } = initTable(chess, create(false), 1); const s0 = w.game as State; const seat = whiteSeatOf(s0); const result = applyInput(chess, w, { v: 1, type: "move", payload: { from: "e2", to: "e1" }, uid: userOf(s0, seat), ci: "c0", cid: 1, role: "player", seat, ts_ms: 1_700_000_001_000, } as Input, 1); assert.equal(result.status, "rejected"); }); test("black pawn advances toward rank 1 after white moves", () => { const { w } = initTable(chess, create(false), 1); const s0 = w.game as State; const ws = whiteSeatOf(s0), bs = blackSeatOf(s0); let ts = 1_700_000_001_000, cid = 0; const move = (seat: number, from: string, to: string) => applyInput(chess, w, { v: 1, type: "move", payload: { from, to }, uid: userOf(w.game as State, seat), ci: `c${seat}`, cid: ++cid, role: "player", seat, ts_ms: (ts += 100), } as Input, 1); assert.equal(move(ws, "e2", "e4").status, "accepted"); assert.equal(move(bs, "e7", "e5").status, "accepted"); const s = w.game as State; assert.deepEqual(s.board[squareIndex("e5")], { type: "p", color: "b" }); // Black pawn cannot drift backwards either assert.equal(move(ws, "d2", "d4").status, "accepted"); assert.equal(move(bs, "e5", "e6").status, "rejected"); }); test("knight jumps (white Ng1 -> f3)", () => { const { w } = initTable(chess, create(false), 1); const s0 = w.game as State; const seat = whiteSeatOf(s0); const result = applyInput(chess, w, { v: 1, type: "move", payload: { from: "g1", to: "f3" }, uid: userOf(s0, seat), ci: "c0", cid: 1, role: "player", seat, ts_ms: 1_700_000_001_000, } as Input, 1); assert.equal(result.status, "accepted"); assert.deepEqual((w.game as State).board[squareIndex("f3")], { type: "n", color: "w" }); }); test("chess rejects invalid square", () => { const { w } = initTable(chess, create(false), 1); const s0 = w.game as State; const seat = whiteSeatOf(s0); const result = applyInput(chess, w, { v: 1, type: "move", payload: { from: "z9", to: "e2" }, uid: userOf(s0, seat), ci: "c0", cid: 1, role: "player", seat, ts_ms: 1_700_000_001_000, } as Input, 1); assert.equal(result.status, "rejected"); }); function driver(w: { game: unknown }) { let cid = 0, ts = 1_700_000_001_000; return (seat: number, from: string, to: string, promotion?: string) => applyInput(chess, w as never, { v: 1, type: "move", payload: promotion ? { from, to, promotion } : { from, to }, uid: userOf(w.game as State, seat), ci: `c${seat}`, cid: ++cid, role: "player", seat, ts_ms: (ts += 100), } as Input, 1); } test("white castles kingside once the path is clear", () => { const { w } = initTable(chess, create(false), 1); const s0 = w.game as State; const ws = whiteSeatOf(s0), bs = blackSeatOf(s0); const send = driver(w); assert.equal(send(ws, "g1", "f3").status, "accepted"); assert.equal(send(bs, "g8", "f6").status, "accepted"); assert.equal(send(ws, "e2", "e3").status, "accepted"); assert.equal(send(bs, "e7", "e6").status, "accepted"); assert.equal(send(ws, "f1", "e2").status, "accepted"); assert.equal(send(bs, "b7", "b6").status, "accepted"); assert.equal(send(ws, "e1", "g1").status, "accepted"); // O-O const s = w.game as State; assert.deepEqual(s.board[squareIndex("g1")], { type: "k", color: "w" }); assert.deepEqual(s.board[squareIndex("f1")], { type: "r", color: "w" }); assert.equal(s.board[squareIndex("e1")], null); assert.equal(s.board[squareIndex("h1")], null); // Rights are spent and stay spent. assert.equal(s.rights.wk, false); assert.equal(s.rights.wq, false); assert.equal(s.rights.bk, true); }); test("en passant capture works only on the next move", () => { const { w } = initTable(chess, create(false), 1); const s0 = w.game as State; const ws = whiteSeatOf(s0), bs = blackSeatOf(s0); const send = driver(w); assert.equal(send(ws, "e2", "e4").status, "accepted"); assert.equal(send(bs, "a7", "a6").status, "accepted"); assert.equal(send(ws, "e4", "e5").status, "accepted"); assert.equal(send(bs, "d7", "d5").status, "accepted"); assert.equal((w.game as State).ep, squareIndex("d6")); // window opens behind the double-stepper assert.equal(send(ws, "e5", "d6").status, "accepted"); // exd6 e.p. const s = w.game as State; assert.deepEqual(s.board[squareIndex("d6")], { type: "p", color: "w" }); assert.equal(s.board[squareIndex("d5")], null); // bypassed pawn is lifted assert.equal(s.board[squareIndex("e5")], null); assert.equal(s.captures, 1); assert.equal(s.ep, null); // window closes after use }); test("promotion: pawn on the last rank becomes the chosen piece", () => { const { w } = initTable(chess, create(false), 1); const s0 = w.game as State; const ws = whiteSeatOf(s0), bs = blackSeatOf(s0); const send = driver(w); const line: [number, string, string][] = [ [ws, "a2", "a4"], [bs, "g8", "f6"], [ws, "a4", "a5"], [bs, "f6", "h5"], [ws, "a5", "a6"], [bs, "e7", "e6"], [ws, "a6", "b7"], [bs, "d7", "d6"], ]; for (const [seat, from, to] of line) assert.equal(send(seat, from, to).status, "accepted", `${from}->${to}`); assert.equal(send(ws, "b7", "a8", "q").status, "accepted"); const s = w.game as State; assert.deepEqual(s.board[squareIndex("a8")], { type: "q", color: "w" }); assert.equal(s.moves_played, 9); }); test("promotion: invalid piece is rejected, knight is accepted", () => { const setup = () => { const { w } = initTable(chess, create(false), 1); const s0 = w.game as State; const ws = whiteSeatOf(s0), bs = blackSeatOf(s0); const send = driver(w); const line: [number, string, string][] = [ [ws, "a2", "a4"], [bs, "g8", "f6"], [ws, "a4", "a5"], [bs, "f6", "h5"], [ws, "a5", "a6"], [bs, "e7", "e6"], [ws, "a6", "b7"], [bs, "d7", "d6"], ]; for (const [seat, from, to] of line) assert.equal(send(seat, from, to).status, "accepted"); return { w, ws, send }; }; { const { ws, send } = setup(); assert.equal(send(ws, "b7", "a8", "k").status, "rejected"); } { const { w, ws, send } = setup(); assert.equal(send(ws, "b7", "a8", "n").status, "accepted"); assert.deepEqual((w.game as State).board[squareIndex("a8")], { type: "n", color: "w" }); } }); test("fool's mate is detected and ends the game", () => { const { w } = initTable(chess, create(false), 1); const s0 = w.game as State; const ws = whiteSeatOf(s0), bs = blackSeatOf(s0); const send = driver(w); assert.equal(send(ws, "f2", "f3").status, "accepted"); assert.equal(send(bs, "e7", "e5").status, "accepted"); assert.equal(send(ws, "g2", "g4").status, "accepted"); assert.equal(send(bs, "d8", "h4").status, "accepted"); // Qh4# assert.equal(w.status, "finished"); const s = w.game as State; assert.equal(s.phase, "over"); assert.equal(s.winner, bs); assert.equal(s.result_reason, "checkmate"); assert.equal(w.result!.standings.find((r) => r.seat === bs)!.rank, 1); }); test("check must be answered: sidelines rejected, capture accepted", () => { const { w } = initTable(chess, create(false), 1); const s0 = w.game as State; const ws = whiteSeatOf(s0), bs = blackSeatOf(s0); const send = driver(w); assert.equal(send(ws, "e2", "e4").status, "accepted"); assert.equal(send(bs, "e7", "e5").status, "accepted"); assert.equal(send(ws, "d1", "h5").status, "accepted"); assert.equal(send(bs, "b8", "c6").status, "accepted"); assert.equal(send(ws, "h5", "f7").status, "accepted"); // Qxf7+ assert.equal((w.game as State).phase, "play"); // check, not mate assert.equal(isKingInCheck(w.game as State, "b"), true); assert.equal(isCheckmate(w.game as State, "b"), false); assert.equal(send(bs, "a7", "a6").status, "rejected"); // ignores the check assert.equal(send(bs, "e8", "f7").status, "accepted"); // king takes the checker assert.deepEqual((w.game as State).board[squareIndex("f7")], { type: "k", color: "b" }); }); test("stalemate: lone king with no flight squares is a draw", () => { const mk = (seat: number, user_id: string, color: "w" | "b"): State["players"][number] => ({ seat, user_id, name: user_id, color, bot: false, level: "basic", conns: [], left: false }); const s: State = { players: [mk(0, "u0", "w"), mk(1, "u1", "b")], board: Array(64).fill(null) as State["board"], turn: 1, phase: "play", winner: null, result_reason: "", turn_ms: 60000, timer_n: 0, timer_id: null, deadline: null, bot_timer: null, moves_played: 30, captures: 10, rights: { wk: false, wq: false, bk: false, bq: false }, ep: null, }; s.board[squareIndex("h8")] = { type: "k", color: "b" }; s.board[squareIndex("f7")] = { type: "k", color: "w" }; s.board[squareIndex("g6")] = { type: "q", color: "w" }; assert.equal(isKingInCheck(s, "b"), false); assert.equal(isCheckmate(s, "b"), false); assert.equal(isStalemate(s, "b"), true); }); test("insufficient material: K vs K, KB vs K, KN vs K, KB vs KB, KN vs KN are recognized", () => { const mk = (seat: number, user_id: string, color: "w" | "b"): State["players"][number] => ({ seat, user_id, name: user_id, color, bot: false, level: "basic", conns: [], left: false }); const makeState = (pieces: { sq: string; type: "k" | "p" | "r" | "n" | "b" | "q"; color: "w" | "b" }[]): State => { const s: State = { players: [mk(0, "u0", "w"), mk(1, "u1", "b")], board: Array(64).fill(null) as State["board"], turn: 0, phase: "play", winner: null, result_reason: "", turn_ms: 60000, timer_n: 0, timer_id: null, deadline: null, bot_timer: null, bot_n: 0, moves_played: 40, captures: 28, rights: { wk: false, wq: false, bk: false, bq: false }, ep: null, }; for (const p of pieces) { s.board[squareIndex(p.sq)] = { type: p.type, color: p.color }; } return s; }; // 1. King vs King const kVsK = makeState([ { sq: "e1", type: "k", color: "w" }, { sq: "e8", type: "k", color: "b" }, ]); assert.equal(hasInsufficientMaterial(kVsK), true); // 2. King + Bishop vs King const kbVsK = makeState([ { sq: "e1", type: "k", color: "w" }, { sq: "c4", type: "b", color: "w" }, { sq: "e8", type: "k", color: "b" }, ]); assert.equal(hasInsufficientMaterial(kbVsK), true); // 3. King + Knight vs King const knVsK = makeState([ { sq: "e1", type: "k", color: "w" }, { sq: "e8", type: "k", color: "b" }, { sq: "f6", type: "n", color: "b" }, ]); assert.equal(hasInsufficientMaterial(knVsK), true); // 4. King + Bishop vs King + Bishop const kbVsKb = makeState([ { sq: "e1", type: "k", color: "w" }, { sq: "c1", type: "b", color: "w" }, { sq: "e8", type: "k", color: "b" }, { sq: "c8", type: "b", color: "b" }, ]); assert.equal(hasInsufficientMaterial(kbVsKb), true); // 5. King + Knight vs King + Knight const knVsKn = makeState([ { sq: "e1", type: "k", color: "w" }, { sq: "b1", type: "n", color: "w" }, { sq: "e8", type: "k", color: "b" }, { sq: "b8", type: "n", color: "b" }, ]); assert.equal(hasInsufficientMaterial(knVsKn), true); // 6. Pawn exists -> NOT insufficient (can promote) const withPawn = makeState([ { sq: "e1", type: "k", color: "w" }, { sq: "a2", type: "p", color: "w" }, { sq: "e8", type: "k", color: "b" }, ]); assert.equal(hasInsufficientMaterial(withPawn), false); // 7. Queen or Rook exists -> NOT insufficient const withRook = makeState([ { sq: "e1", type: "k", color: "w" }, { sq: "a1", type: "r", color: "w" }, { sq: "e8", type: "k", color: "b" }, ]); assert.equal(hasInsufficientMaterial(withRook), false); }); test("insufficient material: capturing last pawn triggers draw immediately", () => { const mk = (seat: number, user_id: string, color: "w" | "b"): State["players"][number] => ({ seat, user_id, name: user_id, color, bot: false, level: "basic", conns: [], left: false }); const s0: State = { players: [mk(0, "u0", "w"), mk(1, "u1", "b")], board: Array(64).fill(null) as State["board"], turn: 0, phase: "play", winner: null, result_reason: "", turn_ms: 60000, timer_n: 0, timer_id: null, deadline: null, bot_timer: null, bot_n: 0, moves_played: 50, captures: 29, rights: { wk: false, wq: false, bk: false, bq: false }, ep: null, }; s0.board[squareIndex("d4")] = { type: "k", color: "w" }; s0.board[squareIndex("d5")] = { type: "p", color: "b" }; s0.board[squareIndex("f8")] = { type: "k", color: "b" }; const w = { create: create(false), game: s0, status: "running", seq: 1, vseq: {}, sseq: 1, timers: {}, last_cid: {}, snap_hash: {}, sys_hash: "", result: null, rng: [1, 2, 3, 4], last_ts_ms: 1700000000000, }; // White king captures the black pawn on d5 -> only Kings left! const res = applyInput(chess, w as any, { v: 1, type: "move", payload: { from: "d4", to: "d5" }, uid: "u0", ci: "c0", cid: 1, role: "player", seat: 0, ts_ms: 1700000001000, } as Input, 1); assert.equal(res.status, "accepted"); assert.equal((w.game as State).phase, "over"); assert.equal((w.game as State).winner, null); assert.equal((w.game as State).result_reason, "insufficient_material"); assert.equal(w.status, "finished"); });