/** * Dev-console admin command specs (unit-level, pure engine). * * Pins the contract the Minigame Developer Console relies on: applyAdminCommand * for Ordnance Disposal and Call for Fire Versus is pure (input state never * mutates), phase-gated (stale commands are no-ops), and produces the state * transitions the dock buttons advertise. */ import { describe, expect, it } from "vitest"; import { MAX_BOMBS, MAX_RANGE, applyAdminCommand, createMatchState, startRound, tick, type BomperatorAdminCommand, type BomperatorState, } from "@/lib/minigames/bomperator"; import { VERSUS_BOARD_SIZE, type FobPlacement, } from "@/lib/minigames/call-for-fire-versus/constants"; import { applyAdminCommand as applyVersusAdminCommand, createVersusState, fireShot, placeFleet, } from "@/lib/minigames/call-for-fire-versus/engine"; import type { VersusState } from "@/lib/minigames/call-for-fire-versus/types"; import { buildVersusView } from "@/lib/minigames/call-for-fire-versus/views"; /* ---------------------------------------------------------------- bomperator */ function battleState(playerCount = 3): BomperatorState { const seats = Array.from({ length: playerCount }, (_, seat) => ({ seat, userId: 7000 + seat, name: `P${seat}`, })); const state = createMatchState(seats, 3, 1234); return startRound(state); } /** Advance from countdown into battle. */ function inBattle(state: BomperatorState): BomperatorState { let next = state; for (let i = 0; i < 80 && next.phase !== "battle"; i += 1) next = tick(next); expect(next.phase).toBe("battle"); return next; } function run(cmd: BomperatorAdminCommand, state: BomperatorState): BomperatorState { const before = JSON.stringify(state); const after = applyAdminCommand(state, cmd); // Purity: the input state object is never mutated. expect(JSON.stringify(state)).toBe(before); return after; } describe("bomperator applyAdminCommand", () => { it("destroyCrates clears every crate and exposes the hidden power-ups", () => { const state = inBattle(battleState()); const cratesBefore = state.grid.filter((cell) => cell === "crate").length; expect(cratesBefore).toBeGreaterThan(0); const next = run({ kind: "destroyCrates" }, state); expect(next.grid.some((cell) => cell === "crate")).toBe(false); expect(next.drops.length).toBe(state.drops.length + cratesBefore); expect(next.events.some((event) => event.type === "crateBroken")).toBe(true); }); it("destroyCrates is ignored outside an active round", () => { const lobby = createMatchState([{ seat: 0, userId: 1, name: "A" }], 3, 7); const next = run({ kind: "destroyCrates" }, lobby); expect(next).toBe(lobby); }); it("grantPickup applies the power-up to a live seat and emits a pickup event", () => { const state = inBattle(battleState()); const before = state.players[1]; const bomb = run({ kind: "grantPickup", seat: 1, pickup: "bomb" }, state); expect(bomb.players[1].bombsMax).toBe(before.bombsMax + 1); expect(bomb.events.at(-1)).toMatchObject({ type: "pickup", seat: 1, kind: "bomb" }); const range = run({ kind: "grantPickup", seat: 1, pickup: "range" }, state); expect(range.players[1].range).toBe(before.range + 1); const kick = run({ kind: "grantPickup", seat: 1, pickup: "kick" }, state); expect(kick.players[1].kick).toBe(true); }); it("grantPickup respects the stat caps and ignores dead or unknown seats", () => { const state = inBattle(battleState()); let maxed = state; for (let i = 0; i < MAX_BOMBS + 3; i += 1) { maxed = applyAdminCommand(maxed, { kind: "grantPickup", seat: 0, pickup: "bomb" }); } expect(maxed.players[0].bombsMax).toBe(MAX_BOMBS); for (let i = 0; i < MAX_RANGE + 3; i += 1) { maxed = applyAdminCommand(maxed, { kind: "grantPickup", seat: 0, pickup: "range" }); } expect(maxed.players[0].range).toBe(MAX_RANGE); expect(run({ kind: "grantPickup", seat: 99, pickup: "speed" }, state)).toBe(state); const dead = applyAdminCommand(state, { kind: "eliminate", seat: 2 }); expect(run({ kind: "grantPickup", seat: 2, pickup: "speed" }, dead)).toBe(dead); }); it("eliminate kills a live seat, credits no kill, and emits a death event", () => { const state = inBattle(battleState()); const next = run({ kind: "eliminate", seat: 1 }, state); expect(next.players[1].alive).toBe(false); expect(next.players[1].deaths).toBe(state.players[1].deaths + 1); expect(next.players.every((p) => p.kills === state.players[p.seat].kills)).toBe(true); expect(next.events.at(-1)).toMatchObject({ type: "death", seat: 1, killerSeat: null }); }); it("freeze holds the tick loop and unfreeze resumes it", () => { const state = inBattle(battleState()); const frozen = run({ kind: "freeze", frozen: true }, state); expect(frozen.frozen).toBe(true); expect(tick(frozen)).toBe(frozen); const thawed = run({ kind: "freeze", frozen: false }, frozen); expect(thawed.frozen).toBe(false); expect(tick(thawed).tickCount).toBe(thawed.tickCount + 1); }); it("suddenDeathNow arms sudden death during battle only", () => { const state = inBattle(battleState()); const next = run({ kind: "suddenDeathNow" }, state); expect(next.suddenDeath).toBe(true); expect(next.events.at(-1)?.type).toBe("suddenDeath"); const already = { ...state, suddenDeath: true }; expect(run({ kind: "suddenDeathNow" }, already)).toBe(already); const countdown = battleState(); expect(run({ kind: "suddenDeathNow" }, countdown)).toBe(countdown); }); it("setRoundTimer clamps to zero and rejects non-finite values", () => { const state = inBattle(battleState()); expect(run({ kind: "setRoundTimer", ticks: 40 }, state).roundTimerTicks).toBe(40); expect(run({ kind: "setRoundTimer", ticks: -5 }, state).roundTimerTicks).toBe(0); expect(run({ kind: "setRoundTimer", ticks: Number.NaN }, state)).toBe(state); }); it("endRound awards the round and completes the match at the target", () => { let state = inBattle(battleState()); state = { ...state, players: state.players.map((p) => ({ ...p, roundsWon: 2 })) }; const next = run({ kind: "endRound", winnerSeat: 0 }, state); expect(next.players[0].roundsWon).toBe(3); expect(next.phase).toBe("complete"); expect(next.matchWinnerSeat).toBe(0); expect(next.endReason).toBe("rounds"); }); it("endRound with a null winner is a draw and rolls to the next round", () => { const state = inBattle(battleState()); const next = run({ kind: "endRound", winnerSeat: null }, state); expect(next.roundWinnerSeat).toBeNull(); expect(next.phase).toBe("roundEnd"); }); }); /* --------------------------------------------------------------- cff versus */ /** Mirrors the engine spec layout: 3x2 CP, 4-cell L depot, three 2-cell lines. */ function fleetAt(x: number, y: number): FobPlacement[] { return [ { assetId: "command-post", origin: { x, y }, orientation: 0 }, { assetId: "ammo-depot", origin: { x, y: y + 4 }, orientation: 0 }, { assetId: "radar-site", origin: { x, y: y + 5 }, orientation: 0 }, { assetId: "fob", origin: { x, y: y + 6 }, orientation: 0 }, { assetId: "fob-2", origin: { x, y: y + 7 }, orientation: 0 }, ]; } function versusBattle(): VersusState { let state = createVersusState({ userId: 8001, name: "A" }, { userId: 8002, name: "B" }); const placedA = placeFleet(state, 0, fleetAt(0, 0)); if ("error" in placedA) throw new Error(placedA.error); state = placedA.state; const placedB = placeFleet(state, 1, fleetAt(6, 0)); if ("error" in placedB) throw new Error(placedB.error); state = placedB.state; expect(state.phase).toBe("battle"); return state; } describe("cff-versus applyAdminCommand", () => { it("revealFleets serves both fleets to every viewer while on", () => { const state = versusBattle(); expect(buildVersusView(state, 0).opponentFleet).toBeUndefined(); const revealed = applyVersusAdminCommand(state, { kind: "revealFleets", on: true }); const viewA = buildVersusView(revealed, 0); const viewB = buildVersusView(revealed, 1); expect(viewA.opponentFleet?.length).toBeGreaterThan(0); expect(viewB.opponentFleet?.length).toBeGreaterThan(0); // What each viewer sees as the opponent fleet is the other's real fleet. expect(viewA.opponentFleet?.map((fob) => fob.id)).toEqual( revealed.players[1].fleet?.map((fob) => fob.id), ); const hidden = applyVersusAdminCommand(revealed, { kind: "revealFleets", on: false }); expect(hidden.revealFleets).toBe(false); expect(buildVersusView(hidden, 0).opponentFleet).toBeUndefined(); }); it("forceShotResult hit snaps an errant round onto the nearest un-struck cell", () => { const base = versusBattle(); const armed = applyVersusAdminCommand(base, { kind: "forceShotResult", outcome: "hit" }); expect(armed.forcedShotOutcome).toBe("hit"); // Fire far away from any structure: without the override this is a miss. const farCell = { x: VERSUS_BOARD_SIZE - 1, y: VERSUS_BOARD_SIZE - 1 }; const shot = fireShot(armed, 0, { kind: "grid", grid: farCell }); if ("error" in shot) throw new Error(shot.error); expect(shot.shot.outcome).not.toBe("miss"); expect(shot.state.players[0].cellsHit).toBe(1); // The one-shot override is consumed. expect(shot.state.forcedShotOutcome).toBeNull(); }); it("forceShotResult hit leaves an already-landing shot untouched", () => { const base = versusBattle(); const enemyCell = base.players[1].fleet?.[0].cells[0]; if (!enemyCell) throw new Error("no enemy cell"); const armed = applyVersusAdminCommand(base, { kind: "forceShotResult", outcome: "hit" }); const shot = fireShot(armed, 0, { kind: "grid", grid: enemyCell }); if ("error" in shot) throw new Error(shot.error); expect(shot.shot.grid).toEqual(enemyCell); expect(shot.shot.outcome).not.toBe("miss"); }); it("forceShotResult miss suppresses damage even on a struck cell", () => { const base = versusBattle(); const enemyCell = base.players[1].fleet?.[0].cells[0]; if (!enemyCell) throw new Error("no enemy cell"); const armed = applyVersusAdminCommand(base, { kind: "forceShotResult", outcome: "miss" }); const shot = fireShot(armed, 0, { kind: "grid", grid: enemyCell }); if ("error" in shot) throw new Error(shot.error); expect(shot.shot.outcome).toBe("miss"); expect(shot.shot.assetId).toBeNull(); expect(shot.state.players[0].cellsHit).toBe(0); expect(shot.state.forcedShotOutcome).toBeNull(); }); it("skipTurn passes the clock and clearing the override is always allowed", () => { const state = versusBattle(); const skipped = applyVersusAdminCommand(state, { kind: "skipTurn" }); expect(skipped.turn).toBe(1); const cleared = applyVersusAdminCommand( { ...state, forcedShotOutcome: "hit" }, { kind: "forceShotResult", outcome: null }, ); expect(cleared.forcedShotOutcome).toBeNull(); }); it("is pure: input states are never mutated", () => { const state = versusBattle(); const before = JSON.stringify(state); applyVersusAdminCommand(state, { kind: "revealFleets", on: true }); applyVersusAdminCommand(state, { kind: "forceShotResult", outcome: "hit" }); applyVersusAdminCommand(state, { kind: "skipTurn" }); expect(JSON.stringify(state)).toBe(before); }); });