1
0
Fork 0
polaris-task-force/tests/int/call-for-fire-versus.int.spec.ts
Z8MB1E 49782365c2 chore(call-for-fire-versus): finish place/structure wording in help and errors
Completes the terminology pass: the "How to place" help block and the
engine's user-facing error strings ("Missing structure", "Structures cannot
overlap", "Placement is closed", "The opponent has not placed their
structures") now match the place/structure/target vocabulary used elsewhere
in the duel. Also splits a comment that had been merged onto the scoreOf
declaration. Strings only; no behavior change.
2026-10-12 00:01:34 -04:00

239 lines
9.9 KiB
TypeScript

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 with its elbow at
* (x, y+3) (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 + 3 }, 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 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." });
const overlapping: FobPlacement[] = [
{ assetId: "command-post", origin: { x: 0, y: 0 }, orientation: 0 },
{ assetId: "ammo-depot", origin: { x: 1, y: 1 }, 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: "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, 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<typeof fireShot>[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);
});
});