import { describe, expect, it } from "vitest"; import { buildFleet, cellsForPlacement, createVersusState, fireShot, isFleetDestroyed, placeFleet, scoreOf, seatPlayer, skipTurn, type FobPlacement, type VersusState, } from "@/lib/minigames/call-for-fire-versus"; /** * A deterministic, non-overlapping fleet layout anchored at (x, y): * a 2x2 Command Post (rows y..y+1), an L Ammo Depot (box rows y+2..y+3), * a Radar Site line (row y+4) and a 1-cell FOB (row y+5). */ function fleetAt(x: number, y: number): FobPlacement[] { return [ { assetId: "command-post", origin: { x, y }, orientation: 0 }, { assetId: "ammo-depot", origin: { x, y: y + 2 }, orientation: 0 }, { assetId: "radar-site", origin: { x, y: y + 4 }, orientation: 0 }, { assetId: "fob", origin: { x, y: y + 5 }, orientation: 0 }, ]; } function startedState(): VersusState { let state = createVersusState({ userId: 1, name: "A" }, { userId: 2, name: "B" }); state = expectState(placeFleet(state, 0, fleetAt(4, 4))); state = expectState(placeFleet(state, 1, fleetAt(0, 0))); return state; } function expectState(result: { state: VersusState } | { error: string }): VersusState { if ("error" in result) throw new Error(result.error); return result.state; } describe("call for fire versus: fleet placement", () => { it("lays out straight-line emplacements and rejects off-board ones", () => { expect(cellsForPlacement({ x: 8, y: 0 }, 0, 3, 10)).toBeNull(); expect(cellsForPlacement({ x: 0, y: 0 }, 1, 3, 10)).toEqual([ { x: 0, y: 0 }, { x: 0, y: 1 }, { x: 0, y: 2 }, ]); }); it("lays out a 2x2 square emplacement regardless of orientation", () => { const square = [ { x: 0, y: 0 }, { x: 1, y: 0 }, { x: 0, y: 1 }, { x: 1, y: 1 }, ]; expect(cellsForPlacement({ x: 0, y: 0 }, 0, 4, 10, "square")).toEqual(square); expect(cellsForPlacement({ x: 0, y: 0 }, 1, 4, 10, "square")).toEqual(square); expect(cellsForPlacement({ x: 9, y: 0 }, 0, 4, 10, "square")).toBeNull(); }); it("rotates the 3-cell L through four distinct orientations", () => { expect(cellsForPlacement({ x: 0, y: 0 }, 0, 3, 10, "l")).toEqual([ { x: 0, y: 0 }, { x: 0, y: 1 }, { x: 1, y: 1 }, ]); expect(cellsForPlacement({ x: 0, y: 0 }, 1, 3, 10, "l")).toEqual([ { x: 1, y: 0 }, { x: 0, y: 0 }, { x: 0, y: 1 }, ]); expect(cellsForPlacement({ x: 0, y: 0 }, 2, 3, 10, "l")).toEqual([ { x: 1, y: 1 }, { x: 1, y: 0 }, { x: 0, y: 0 }, ]); expect(cellsForPlacement({ x: 0, y: 0 }, 3, 3, 10, "l")).toEqual([ { x: 0, y: 1 }, { x: 1, y: 1 }, { x: 1, y: 0 }, ]); expect(cellsForPlacement({ x: 9, y: 0 }, 0, 3, 10, "l")).toBeNull(); }); it("rejects missing, overlapping, and unknown emplacements", () => { expect(buildFleet([fleetAt(0, 0)[0]], 10)).toEqual({ error: "Missing emplacement: Ammo Depot." }); const overlapping: FobPlacement[] = [ { assetId: "command-post", origin: { x: 0, y: 0 }, orientation: 0 }, { assetId: "ammo-depot", origin: { x: 1, y: 0 }, orientation: 0 }, { assetId: "radar-site", origin: { x: 0, y: 4 }, orientation: 0 }, { assetId: "fob", origin: { x: 0, y: 5 }, orientation: 0 }, ]; expect(buildFleet(overlapping, 10)).toEqual({ error: "Emplacements cannot overlap." }); const unknown: FobPlacement[] = [ ...fleetAt(0, 0), { assetId: "nuke", origin: { x: 9, y: 9 }, orientation: 0 }, ]; expect("error" in buildFleet(unknown, 10)).toBe(true); }); it("starts the battle once both fleets are placed, host first", () => { let state = createVersusState({ userId: 1, name: "A" }, { userId: 2, name: "B" }); state = expectState(placeFleet(state, 0, fleetAt(0, 0))); expect(state.phase).toBe("placing"); state = expectState(placeFleet(state, 1, fleetAt(0, 4))); expect(state.phase).toBe("battle"); expect(state.turn).toBe(0); expect(state.startedAt).not.toBeNull(); }); }); describe("call for fire versus: firing", () => { it("requires a grid reference first, then corrections only", () => { const state = startedState(); expect("error" in fireShot(state, 0, { kind: "correction", rangeM: 0, deflectionM: 0 })).toBe(true); const first = fireShot(state, 0, { kind: "grid", grid: { x: 0, y: 0 } }); expect("state" in first).toBe(true); if (!("state" in first)) return; expect(first.shot.outcome).toBe("hit"); expect(first.state.turn).toBe(1); // Player 1's own first shot may also be a grid designation. const guestFirst = fireShot(first.state, 1, { kind: "grid", grid: { x: 9, y: 9 } }); expect("state" in guestFirst).toBe(true); if (!("state" in guestFirst)) return; // Back to player 0, who has already fired: only a correction is legal now. const second = fireShot(guestFirst.state, 0, { kind: "grid", grid: { x: 5, y: 5 } }); expect("error" in second).toBe(true); }); it("rejects shots out of turn and once the match is over", () => { const state = startedState(); expect("error" in fireShot(state, 1, { kind: "grid", grid: { x: 5, y: 5 } })).toBe(true); }); it("reports misses, hits, and destroyed emplacements", () => { let state = startedState(); // Miss. state = expectState(fireShot(state, 0, { kind: "grid", grid: { x: 7, y: 7 } })); expect(state.players[0].shots.at(-1)?.outcome).toBe("miss"); // Hit the command post's first cell. state = expectState(fireShot(state, 1, { kind: "grid", grid: { x: 9, y: 9 } })); state = expectState(fireShot(state, 0, { kind: "correction", rangeM: 350, deflectionM: -350 })); const last = state.players[0].shots.at(-1); expect(last?.outcome).toBe("hit"); expect(state.players[0].cellsHit).toBe(1); }); it("ends the match when every emplacement is reduced", () => { // Player 0 walks fire across player 1's whole fleet; player 1 always misses. let state = startedState(); const moves: Array<{ i: 0 | 1; input: Parameters[2] }> = [ { i: 0, input: { kind: "grid", grid: { x: 0, y: 0 } } }, { i: 1, input: { kind: "grid", grid: { x: 9, y: 9 } } }, { i: 0, input: { kind: "correction", rangeM: 0, deflectionM: 50 } }, { i: 1, input: { kind: "correction", rangeM: 0, deflectionM: 0 } }, { i: 0, input: { kind: "correction", rangeM: -50, deflectionM: -50 } }, { i: 1, input: { kind: "correction", rangeM: 0, deflectionM: 0 } }, { i: 0, input: { kind: "correction", rangeM: 0, deflectionM: 50 } }, { i: 1, input: { kind: "correction", rangeM: 0, deflectionM: 0 } }, { i: 0, input: { kind: "correction", rangeM: -50, deflectionM: -50 } }, { i: 1, input: { kind: "correction", rangeM: 0, deflectionM: 0 } }, { i: 0, input: { kind: "correction", rangeM: -50, deflectionM: 0 } }, { i: 1, input: { kind: "correction", rangeM: 0, deflectionM: 0 } }, { i: 0, input: { kind: "correction", rangeM: 0, deflectionM: 50 } }, { i: 1, input: { kind: "correction", rangeM: 0, deflectionM: 0 } }, { i: 0, input: { kind: "correction", rangeM: -50, deflectionM: -50 } }, { i: 1, input: { kind: "correction", rangeM: 0, deflectionM: 0 } }, { i: 0, input: { kind: "correction", rangeM: 0, deflectionM: 50 } }, { i: 1, input: { kind: "correction", rangeM: 0, deflectionM: 0 } }, { i: 0, input: { kind: "correction", rangeM: -50, deflectionM: -50 } }, ]; for (const move of moves) { state = expectState(fireShot(state, move.i, move.input)); } expect(state.phase).toBe("complete"); expect(state.winner).toBe(0); expect(state.reason).toBe("eliminated"); expect(state.players[0].fobsDestroyed).toBe(4); expect(isFleetDestroyed(state.players[1].fleet ?? [])).toBe(true); }); it("passes the turn on a timeout", () => { const state = startedState(); const skipped = skipTurn(state); expect(skipped.turn).toBe(1); }); }); describe("call for fire versus: views", () => { it("never exposes the opponent's fleet layout", async () => { const { buildVersusView } = await import("@/lib/minigames/call-for-fire-versus/views"); const state = startedState(); const view = buildVersusView(state, 0); const serialized = JSON.stringify(view); // Player 1's fleet includes (0,0) and (1,0); player 0 has fired nothing, // so none of those cells may appear in player 0's view. expect(serialized).not.toContain('{"x":0,"y":0}'); expect(serialized).not.toContain('{"x":1,"y":0}'); expect(serialized).not.toContain('"fobsDestroyed":4'); // The viewer's own fleet is present. expect(view.you.fleet).toHaveLength(4); }); it("reports the outcome to the shooter only after a shot", async () => { const { buildVersusView } = await import("@/lib/minigames/call-for-fire-versus/views"); let state = startedState(); state = expectState(fireShot(state, 0, { kind: "grid", grid: { x: 0, y: 0 } })); const shooter = buildVersusView(state, 0); const target = buildVersusView(state, 1); expect(shooter.you.shots).toHaveLength(1); expect(shooter.you.shots[0].outcome).toBe("hit"); // The defender sees the incoming round on their own board. expect(target.you.incoming).toHaveLength(1); expect(target.you.incoming[0].grid).toEqual({ x: 0, y: 0 }); }); }); describe("call for fire versus: helpers", () => { it("seats a player and scores by emplacements then cells", () => { const state = createVersusState({ userId: 1, name: "A" }, { userId: 0, name: "Waiting" }); const seated = seatPlayer(state, 1, { userId: 2, name: "B" }); expect(seated.players[1].userId).toBe(2); expect(seated.players[1].name).toBe("B"); expect(scoreOf(seated.players[0])).toBe(0); }); });