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 3x2 Command Post (rows y..y+1), a 4-cell L Ammo Depot with its elbow at * (x, y+4) (long arm up through row y+2, short arm at (x+1, y+4)), then three * 2-cell horizontal lines: Radar Site (row y+5), FOB (row y+6), and FOB II * (row y+7). */ 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 startedState(): VersusState { let state = createVersusState({ userId: 1, name: "A" }, { userId: 2, name: "B" }); state = expectState(placeFleet(state, 0, fleetAt(6, 0))); 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("lays out a 3x2 rect that flips between wide and tall", () => { const wide = [ { x: 0, y: 0 }, { x: 1, y: 0 }, { x: 2, y: 0 }, { x: 0, y: 1 }, { x: 1, y: 1 }, { x: 2, y: 1 }, ]; expect(cellsForPlacement({ x: 0, y: 0 }, 0, 6, 10, "rect", { width: 3, height: 2 })).toEqual( wide, ); const tall = [ { x: 0, y: 0 }, { x: 1, y: 0 }, { x: 0, y: 1 }, { x: 1, y: 1 }, { x: 0, y: 2 }, { x: 1, y: 2 }, ]; expect(cellsForPlacement({ x: 0, y: 0 }, 1, 6, 10, "rect", { width: 3, height: 2 })).toEqual( tall, ); expect(cellsForPlacement({ x: 8, y: 0 }, 0, 6, 10, "rect", { width: 3, height: 2 })).toBeNull(); // A rect without usable dimensions is rejected. expect(cellsForPlacement({ x: 0, y: 0 }, 0, 6, 10, "rect")).toBeNull(); }); it("grows the L with its size while keeping the elbow anchor", () => { const anchor = { x: 2, y: 4 }; // Orientation 0: long arm (size - 2 cells) up, short arm right. expect(cellsForPlacement(anchor, 0, 4, 10, "l")).toEqual([ { x: 2, y: 4 }, { x: 2, y: 3 }, { x: 2, y: 2 }, { x: 3, y: 4 }, ]); // Half-turn swings the long arm down and the short arm left; the elbow holds. expect(cellsForPlacement(anchor, 2, 4, 10, "l")).toEqual([ { x: 2, y: 4 }, { x: 2, y: 5 }, { x: 2, y: 6 }, { x: 1, y: 4 }, ]); for (const turn of [0, 1, 2, 3] as const) { expect(cellsForPlacement(anchor, turn, 4, 10, "l")).toContainEqual(anchor); } }); it("rotates the 3-cell L around its fixed elbow anchor", () => { const anchor = { x: 2, y: 2 }; expect(cellsForPlacement(anchor, 0, 3, 10, "l")).toEqual([ { x: 2, y: 2 }, { x: 2, y: 1 }, { x: 3, y: 2 }, ]); expect(cellsForPlacement(anchor, 1, 3, 10, "l")).toEqual([ { x: 2, y: 2 }, { x: 3, y: 2 }, { x: 2, y: 3 }, ]); expect(cellsForPlacement(anchor, 2, 3, 10, "l")).toEqual([ { x: 2, y: 2 }, { x: 2, y: 3 }, { x: 1, y: 2 }, ]); expect(cellsForPlacement(anchor, 3, 3, 10, "l")).toEqual([ { x: 2, y: 2 }, { x: 1, y: 2 }, { x: 2, y: 1 }, ]); // The elbow anchor stays put through every rotation. for (const turn of [0, 1, 2, 3] as const) { expect(cellsForPlacement(anchor, turn, 3, 10, "l")).toContainEqual(anchor); } // Orientation 0 with the elbow on the top edge runs the up-arm off board. expect(cellsForPlacement({ x: 0, y: 0 }, 0, 3, 10, "l")).toBeNull(); }); it("rejects missing, overlapping, and unknown emplacements", () => { expect(buildFleet([fleetAt(0, 0)[0]], 10)).toEqual({ error: "Missing structure: Ammo Depot." }); // The L's elbow at (2,2) runs its long arm up through (2,1), inside the // Command Post's 3x2 block. const overlapping: FobPlacement[] = [ { assetId: "command-post", origin: { x: 0, y: 0 }, orientation: 0 }, { assetId: "ammo-depot", origin: { x: 2, y: 2 }, orientation: 0 }, { assetId: "radar-site", origin: { x: 0, y: 4 }, orientation: 0 }, { assetId: "fob", origin: { x: 0, y: 5 }, orientation: 0 }, { assetId: "fob-2", origin: { x: 0, y: 6 }, orientation: 0 }, ]; expect(buildFleet(overlapping, 10)).toEqual({ error: "Structures 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, 1))); 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 all sixteen cells of player 1's fleetAt(0,0) // (Command Post, Ammo Depot L, Radar Site, FOB, FOB II); player 1 misses. let state = startedState(); const targets = [ { x: 0, y: 0 }, { x: 1, y: 0 }, { x: 2, y: 0 }, { x: 2, y: 1 }, { x: 1, y: 1 }, { x: 0, y: 1 }, { x: 0, y: 2 }, { x: 0, y: 3 }, { x: 0, y: 4 }, { x: 1, y: 4 }, { x: 1, y: 5 }, { x: 0, y: 5 }, { x: 0, y: 6 }, { x: 1, y: 6 }, { x: 1, y: 7 }, { x: 0, y: 7 }, ]; state = expectState(fireShot(state, 0, { kind: "grid", grid: targets[0] })); for (let i = 1; i < targets.length; i += 1) { // Player 1 keeps re-hitting the same empty corner between player 0's shots. state = expectState( fireShot( state, 1, i === 1 ? { kind: "grid", grid: { x: 9, y: 9 } } : { kind: "correction", rangeM: 0, deflectionM: 0 }, ), ); const previous = targets[i - 1]; const next = targets[i]; state = expectState( fireShot(state, 0, { kind: "correction", // One cell per 50m step; range is north-positive, deflection east-positive. rangeM: (previous.y - next.y) * 50, deflectionM: (next.x - previous.x) * 50, }), ); } expect(state.phase).toBe("complete"); expect(state.winner).toBe(0); expect(state.reason).toBe("eliminated"); expect(state.players[0].fobsDestroyed).toBe(5); expect(isFleetDestroyed(state.players[1].fleet ?? [])).toBe(true); }); it("stamps every shot with its impact instant", () => { const state = startedState(); const at = new Date("2026-10-19T12:00:00Z"); const first = fireShot(state, 0, { kind: "grid", grid: { x: 0, y: 0 } }, at); if (!("shot" in first)) throw new Error("expected the shot to resolve"); expect(first.shot.at).toBe(at.toISOString()); }); 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(5); }); 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); }); });