1
0
Fork 0
polaris-task-force/tests/int/call-for-fire-versus-registry.int.spec.ts
Z8MB1E 3ce9677fd0 fix(minigames): square versus grid cells and make the Command Post a 2x2 block
The placement and battle grids inherited a 32px line-height, so any cell
holding the emplacement glyph grew taller than its square width (29x34),
making the row gaps look inconsistent once emplacements were placed. Set
leading-none on the grids so cells stay square.

Add a shape to emplacements ("line" | "square") and lay out the Command
Post as a 2x2 block (size 4) instead of a 3-cell line. cellsForFob resolves
cells per definition; the placement editor, preview, and tests use it.
2026-10-11 22:09:09 -04:00

148 lines
5.3 KiB
TypeScript

import { afterAll, beforeEach, describe, expect, it } from "vitest";
import type { FobPlacement } from "@/lib/minigames/call-for-fire-versus/constants";
import {
clearMatchesForTests,
createMatch,
fireShotFor,
getSnapshotFor,
joinMatch,
leaveMatch,
listMatchSummaries,
placeFleetFor,
} from "@/lib/minigames/call-for-fire-versus/registry";
import { computeVersusStatsDelta, readVersusStats, xpForVersus } from "@/lib/minigames/call-for-fire-versus/stats";
function fleetAt(x: number, y: number): FobPlacement[] {
return [
{ assetId: "command-post", origin: { x, y }, orientation: "h" },
{ assetId: "ammo-depot", origin: { x, y: y + 2 }, orientation: "h" },
{ assetId: "radar-site", origin: { x, y: y + 3 }, orientation: "h" },
{ assetId: "fob", origin: { x, y: y + 4 }, orientation: "h" },
];
}
beforeEach(() => {
clearMatchesForTests();
});
afterAll(() => {
clearMatchesForTests();
});
describe("call for fire versus registry: lobby", () => {
it("seats the host and reports a waiting match", () => {
const matchId = createMatch(9001, "Host");
const summary = listMatchSummaries(9001).find((row) => row.matchId === matchId);
expect(summary?.phase).toBe("waiting");
expect(summary?.seatedCount).toBe(1);
expect(summary?.joined).toBe(true);
// A different viewer is not joined.
expect(listMatchSummaries(9002).find((row) => row.matchId === matchId)?.joined).toBe(false);
});
it("seats a guest and rejects a third player", () => {
const matchId = createMatch(9101, "Host");
const view = joinMatch(matchId, 9102, "Guest");
expect(view.yourIndex).toBe(1);
expect(view.you.opponentName).toBe("Host");
expect(() => joinMatch(matchId, 9103, "Third")).toThrow("already full");
});
it("stops a user seating at two matches at once", () => {
const first = createMatch(9201, "Host");
createMatch(9202, "Other");
expect(() => joinMatch(first, 9202, "Other")).toThrow("already in another match");
});
it("rejects snapshots for non-members and unknown matches", () => {
const matchId = createMatch(9301, "Host");
expect(() => getSnapshotFor(matchId, 9302)).toThrow("You are not in this match.");
expect(() => getSnapshotFor("does-not-exist", 9301)).toThrow("Match not found.");
});
});
describe("call for fire versus registry: battle", () => {
it("allows pre-placement while waiting, but does not start the battle", () => {
const matchId = createMatch(9401, "Host");
const view = placeFleetFor(matchId, 9401, fleetAt(0, 0));
expect(view.phase).toBe("placing");
expect(view.you.ready).toBe(true);
expect(view.turnUserId).toBeNull();
});
it("starts the battle after both fleets, host firing first", () => {
const matchId = createMatch(9501, "Host");
joinMatch(matchId, 9502, "Guest");
placeFleetFor(matchId, 9501, fleetAt(4, 4));
const view = placeFleetFor(matchId, 9502, fleetAt(0, 0));
expect(view.phase).toBe("battle");
expect(view.turnUserId).toBe(9501);
expect(view.turnDeadline).not.toBeNull();
});
it("resolves a shot and passes the turn", () => {
const matchId = createMatch(9601, "Host");
joinMatch(matchId, 9602, "Guest");
placeFleetFor(matchId, 9601, fleetAt(4, 4));
placeFleetFor(matchId, 9602, fleetAt(0, 0));
const afterShot = fireShotFor(matchId, 9601, { kind: "grid", grid: { x: 0, y: 0 } });
expect(afterShot.you.shots).toHaveLength(1);
expect(afterShot.you.shots[0].outcome).toBe("hit");
expect(afterShot.turnUserId).toBe(9602);
// The defender sees the incoming round on their own board.
const defender = getSnapshotFor(matchId, 9602);
expect(defender.you.incoming[0].grid).toEqual({ x: 0, y: 0 });
});
it("awards the win to the opponent when a player leaves mid-battle", () => {
const matchId = createMatch(9701, "Host");
joinMatch(matchId, 9702, "Guest");
placeFleetFor(matchId, 9701, fleetAt(4, 4));
placeFleetFor(matchId, 9702, fleetAt(0, 0));
leaveMatch(matchId, 9701);
const guest = getSnapshotFor(matchId, 9702);
expect(guest.phase).toBe("complete");
expect(guest.result?.winnerUserId).toBe(9702);
expect(guest.result?.reason).toBe("resigned");
// The leaver is no longer a member.
expect(() => getSnapshotFor(matchId, 9701)).toThrow("You are not in this match.");
});
it("disposes a match once the last player leaves", () => {
const matchId = createMatch(9801, "Host");
leaveMatch(matchId, 9801);
expect(() => getSnapshotFor(matchId, 9801)).toThrow("Match not found.");
});
});
describe("call for fire versus: stat math", () => {
it("reads defaults and accumulates a finished match", () => {
const current = readVersusStats({});
expect(current).toEqual({ gamesPlayed: 0, wins: 0, losses: 0, bestScore: 0, xpEarned: 0 });
const win = computeVersusStatsDelta(current, {
userId: 1,
won: true,
fobsDestroyed: 4,
cellsHit: 8,
});
expect(win.gamesPlayed).toBe(1);
expect(win.wins).toBe(1);
expect(win.losses).toBe(0);
expect(win.bestScore).toBe(408);
expect(win.xpEarned).toBe(xpForVersus(true, 4));
const loss = computeVersusStatsDelta(win, {
userId: 1,
won: false,
fobsDestroyed: 1,
cellsHit: 3,
});
expect(loss.wins).toBe(1);
expect(loss.losses).toBe(1);
expect(loss.bestScore).toBe(408);
});
});