import { describe, expect, it } from "vitest"; import { createTable, joinTable, startGame } from "@/lib/minigames/poker/tables"; /** * Regression tests for the registry's game-start path. Guards against the * seat-map aliasing bug that once spawned phantom "Player null" engine seats * when a table started with fewer than four players. */ describe("poker table registry: startGame", () => { it("starts a heads-up game with exactly the two seated players", () => { const tableId = createTable(9001, "Host"); joinTable(tableId, 9002, "Guest"); const snapshot = startGame(tableId, 9001); const seatedIds = snapshot.seats.filter((id) => id !== null); expect(seatedIds).toHaveLength(2); const players = snapshot.you?.players ?? []; expect(players).toHaveLength(2); for (const player of players) { expect(player.name).not.toBe("Player null"); expect(player.name.length).toBeGreaterThan(0); expect(player.eliminated).toBe(false); } }); it("starts a three-handed game with exactly the three seated players", () => { const tableId = createTable(9101, "Host"); joinTable(tableId, 9102, "Second"); joinTable(tableId, 9103, "Third"); const snapshot = startGame(tableId, 9101); const seatedIds = snapshot.seats.filter((id) => id !== null); expect(seatedIds).toHaveLength(3); const players = snapshot.you?.players ?? []; expect(players).toHaveLength(3); const names = players.map((player) => player.name); expect(names).not.toContain("Player null"); expect(new Set(names).size).toBe(3); }); it("keeps every engine player backed by a real seated user", () => { const tableId = createTable(9201, "Host"); joinTable(tableId, 9202, "Second"); const snapshot = startGame(tableId, 9201); const players = snapshot.you?.players ?? []; for (const player of players) { const userId = snapshot.seats[player.seatId]; expect(userId).not.toBeNull(); expect(typeof userId).toBe("number"); } }); });