Adds a dedicated Node 22 + ws arena server (src/arena) that owns live Ordnance Disposal matches: drift-corrected 20Hz tick loop with capped catch-up and tick-drift logging, per-socket snapshot fan-out, 30s jose ticket auth, and awards via the idempotent results callback route. Client gains a transport abstraction (transport.ts): WsTransport first when NEXT_PUBLIC_ARENA_URL is baked into the bundle, HttpTransport (today's actions + SSE + guarded poll, behavior unchanged) as automatic fallback after two connection failures or a 5s setup timeout; absent env var means pure HTTP (kill switch / rollback path). Next actions proxy the match lifecycle to the arena when ARENA_SECRET + ARENA_INTERNAL_URL are set; the in-process registry stays untouched as the default backend. Deploy wiring: runner-arena Dockerfile target, compose service, env docs, DEPLOYMENT.md checklist. Tests: arena module suite (19), live server WS suite (3), prediction harness ws-grade latency scenario.
275 lines
11 KiB
TypeScript
275 lines
11 KiB
TypeScript
import { afterEach, beforeEach, describe, expect, it } from "vitest";
|
|
import {
|
|
INTER_ROUND_COUNTDOWN_TICKS,
|
|
MAX_SEATS,
|
|
MIN_TO_START,
|
|
RECONNECT_WINDOW_MS,
|
|
} from "@/lib/minigames/bomperator/constants";
|
|
import type { BomperatorView } from "@/lib/minigames/bomperator/types";
|
|
import type { ServerMessage } from "@/arena/protocol";
|
|
import {
|
|
BomperatorGameModule,
|
|
type BomperatorFinalResult,
|
|
} from "@/arena/games/bomperator";
|
|
|
|
/**
|
|
* Arena game-module suite: the registry scenarios (tests/int/
|
|
* bomperator-registry.int.spec.ts) ported to the arena module with delivery
|
|
* and awards injected/stubbed. The Next-side registry keeps its own suite
|
|
* because the HTTP transport fallback still runs it.
|
|
*/
|
|
|
|
interface Delivered {
|
|
userId: number;
|
|
message: ServerMessage;
|
|
}
|
|
|
|
function makeModule(): {
|
|
mod: BomperatorGameModule;
|
|
delivered: Delivered[];
|
|
awards: Array<{ matchId: string; results: BomperatorFinalResult[] }>;
|
|
} {
|
|
const delivered: Delivered[] = [];
|
|
const awards: Array<{ matchId: string; results: BomperatorFinalResult[] }> = [];
|
|
const mod = new BomperatorGameModule({
|
|
deliver: (userId, message) => delivered.push({ userId, message }),
|
|
onResults: (matchId, results) => awards.push({ matchId, results }),
|
|
autoTick: false, // drive the sim manually; no wall-clock timers
|
|
});
|
|
return { mod, delivered, awards };
|
|
}
|
|
|
|
function viewOf(mod: BomperatorGameModule, matchId: string, userId: number): BomperatorView {
|
|
return mod.snapshotFor(matchId, userId);
|
|
}
|
|
|
|
/** Bring a freshly started match out of the countdown and into battle. */
|
|
function toBattle(mod: BomperatorGameModule, matchId: string): void {
|
|
mod.advance(matchId, INTER_ROUND_COUNTDOWN_TICKS + 1);
|
|
}
|
|
|
|
let ctx: ReturnType<typeof makeModule>;
|
|
beforeEach(() => {
|
|
ctx = makeModule();
|
|
});
|
|
afterEach(() => ctx.mod.clear());
|
|
|
|
describe("arena bomperator module: lobby", () => {
|
|
it("seats the host and reports a waiting match", () => {
|
|
const matchId = ctx.mod.createRoom(9001, "Host");
|
|
const summary = ctx.mod.listSummaries(9001).find((row) => row.matchId === matchId);
|
|
expect(summary?.phase).toBe("waiting");
|
|
expect(summary?.seatedCount).toBe(1);
|
|
expect(summary?.joined).toBe(true);
|
|
expect(ctx.mod.listSummaries(9002).find((row) => row.matchId === matchId)?.joined).toBe(false);
|
|
});
|
|
|
|
it("fills seats up to the cap and rejects an extra player", () => {
|
|
const matchId = ctx.mod.createRoom(9101, "Host");
|
|
for (let seat = 1; seat < MAX_SEATS; seat += 1) ctx.mod.join(matchId, 9101 + seat, `P${seat}`);
|
|
expect(() => ctx.mod.join(matchId, 9999, "Overflow")).toThrow("full");
|
|
});
|
|
|
|
it("stops a user seating at two matches at once", () => {
|
|
ctx.mod.createRoom(9201, "Host");
|
|
const second = ctx.mod.createRoom(9202, "Other");
|
|
expect(() => ctx.mod.join(second, 9201, "Host")).toThrow("already in another match");
|
|
});
|
|
|
|
it("lets a non-seated user read a spectator snapshot with no seat", () => {
|
|
const matchId = ctx.mod.createRoom(9301, "Host");
|
|
const view = viewOf(ctx.mod, matchId, 9302);
|
|
expect(view.yourSeat).toBeNull();
|
|
expect(view.players[0].name).toBe("Host");
|
|
});
|
|
});
|
|
|
|
describe("arena bomperator module: start gating", () => {
|
|
it("refuses to start below the minimum and starts at the minimum", () => {
|
|
const matchId = ctx.mod.createRoom(9401, "Host");
|
|
expect(() => ctx.mod.start(matchId, 9401)).toThrow(`at least ${MIN_TO_START}`);
|
|
ctx.mod.join(matchId, 9402, "Guest");
|
|
ctx.mod.start(matchId, 9401);
|
|
expect(viewOf(ctx.mod, matchId, 9401).phase).toBe("countdown");
|
|
});
|
|
|
|
it("rejects starting twice", () => {
|
|
const matchId = ctx.mod.createRoom(9501, "Host");
|
|
ctx.mod.join(matchId, 9502, "Guest");
|
|
ctx.mod.start(matchId, 9501);
|
|
expect(() => ctx.mod.start(matchId, 9501)).toThrow("already started");
|
|
});
|
|
});
|
|
|
|
describe("arena bomperator module: input + ticking", () => {
|
|
it("applies a queued movement input over subsequent ticks", () => {
|
|
const matchId = ctx.mod.createRoom(9601, "Host");
|
|
ctx.mod.join(matchId, 9602, "Guest");
|
|
ctx.mod.start(matchId, 9601);
|
|
toBattle(ctx.mod, matchId);
|
|
|
|
const before = viewOf(ctx.mod, matchId, 9601);
|
|
expect(before.phase).toBe("battle");
|
|
expect(before.players[before.yourSeat!].x).toBeCloseTo(1, 3);
|
|
|
|
ctx.mod.input(matchId, 9601, { dir: "right" });
|
|
ctx.mod.advance(matchId, 6);
|
|
const after = viewOf(ctx.mod, matchId, 9601);
|
|
expect(after.players[after.yourSeat!].x).toBeGreaterThan(1.5);
|
|
});
|
|
|
|
it("rejects inputs from a non-seated user", () => {
|
|
const matchId = ctx.mod.createRoom(9701, "Host");
|
|
ctx.mod.join(matchId, 9702, "Guest");
|
|
ctx.mod.start(matchId, 9701);
|
|
expect(() => ctx.mod.input(matchId, 9799, { dir: "left" })).toThrow("not in this match");
|
|
});
|
|
|
|
it("fans snapshots out through the deliver callback on every tick", () => {
|
|
const matchId = ctx.mod.createRoom(9711, "Host");
|
|
ctx.mod.join(matchId, 9712, "Guest");
|
|
ctx.mod.start(matchId, 9711);
|
|
ctx.delivered.length = 0;
|
|
ctx.mod.advance(matchId, 1);
|
|
const snapshots = ctx.delivered.filter((d) => d.message.t === "snapshot");
|
|
// Both seated technicians receive one snapshot per tick.
|
|
expect(new Set(snapshots.map((d) => d.userId))).toEqual(new Set([9711, 9712]));
|
|
});
|
|
});
|
|
|
|
describe("arena bomperator module: input sequencing", () => {
|
|
it("applies only the newest seq and ignores stale out-of-order frames", () => {
|
|
const matchId = ctx.mod.createRoom(9751, "Host");
|
|
ctx.mod.join(matchId, 9752, "Guest");
|
|
ctx.mod.start(matchId, 9751);
|
|
toBattle(ctx.mod, matchId);
|
|
|
|
ctx.mod.input(matchId, 9751, { dir: "right", seq: 1 });
|
|
ctx.mod.advance(matchId, 6);
|
|
const right = viewOf(ctx.mod, matchId, 9751);
|
|
const rightX = right.players[right.yourSeat!].x;
|
|
expect(rightX).toBeGreaterThan(1.5);
|
|
|
|
// A stale echo of an older intent must never resurrect it.
|
|
ctx.mod.input(matchId, 9751, { dir: "left", seq: 3 });
|
|
ctx.mod.input(matchId, 9751, { dir: "right", seq: 2 });
|
|
ctx.mod.advance(matchId, 6);
|
|
const after = viewOf(ctx.mod, matchId, 9751);
|
|
expect(after.players[after.yourSeat!].x).toBeLessThan(rightX);
|
|
});
|
|
|
|
it("echoes each seat's last applied input seq in the snapshot", () => {
|
|
const matchId = ctx.mod.createRoom(9761, "Host");
|
|
ctx.mod.join(matchId, 9762, "Guest");
|
|
ctx.mod.start(matchId, 9761);
|
|
toBattle(ctx.mod, matchId);
|
|
|
|
expect(viewOf(ctx.mod, matchId, 9761).players[0].lastInputSeq).toBe(0);
|
|
ctx.mod.input(matchId, 9761, { dir: "down", seq: 5 });
|
|
ctx.mod.input(matchId, 9761, { dir: "up", seq: 4 }); // stale: ignored
|
|
const view = viewOf(ctx.mod, matchId, 9761);
|
|
expect(view.players[0].lastInputSeq).toBe(5);
|
|
expect(view.players[1].lastInputSeq).toBe(0);
|
|
});
|
|
});
|
|
|
|
describe("arena bomperator module: disconnect and dissolve", () => {
|
|
it("holds a seat on socket detach and restores it on re-attach", () => {
|
|
const matchId = ctx.mod.createRoom(9801, "Host");
|
|
ctx.mod.join(matchId, 9802, "Guest");
|
|
ctx.mod.start(matchId, 9801);
|
|
toBattle(ctx.mod, matchId);
|
|
|
|
ctx.mod.detach(matchId, 9801); // socket dropped
|
|
expect(viewOf(ctx.mod, matchId, 9802).players[0].connected).toBe(false);
|
|
|
|
ctx.mod.attach(matchId, 9801); // reconnect within the window
|
|
expect(viewOf(ctx.mod, matchId, 9802).players[0].connected).toBe(true);
|
|
});
|
|
|
|
it("dissolves the match once a held seat's reconnect window lapses", () => {
|
|
const matchId = ctx.mod.createRoom(9901, "Host");
|
|
ctx.mod.join(matchId, 9902, "Guest");
|
|
ctx.mod.start(matchId, 9901);
|
|
toBattle(ctx.mod, matchId);
|
|
|
|
ctx.mod.leave(matchId, 9901); // host walks away
|
|
// Advance with a clock past the reconnect window: the seat expires and the
|
|
// match drops below two active technicians.
|
|
ctx.mod.advance(matchId, 1, Date.now() + RECONNECT_WINDOW_MS + 5_000);
|
|
const view = viewOf(ctx.mod, matchId, 9902);
|
|
expect(view.phase).toBe("complete");
|
|
expect(view.endReason).toBe("dissolved");
|
|
});
|
|
});
|
|
|
|
describe("arena bomperator module: exit paths", () => {
|
|
it("ends the match as soon as the last connected technician leaves", () => {
|
|
const matchId = ctx.mod.createRoom(9951, "Host");
|
|
ctx.mod.join(matchId, 9952, "Guest");
|
|
ctx.mod.start(matchId, 9951);
|
|
toBattle(ctx.mod, matchId);
|
|
|
|
ctx.mod.leave(matchId, 9951); // host walks; guest still connected -> alive
|
|
expect(() => viewOf(ctx.mod, matchId, 9952)).not.toThrow();
|
|
|
|
ctx.mod.leave(matchId, 9952); // last connected leaves -> dissolve + dispose now
|
|
expect(() => viewOf(ctx.mod, matchId, 9952)).toThrow("Match not found.");
|
|
});
|
|
|
|
it("lets a seated technician end the drill, but not a stranger", () => {
|
|
const matchId = ctx.mod.createRoom(9961, "Host");
|
|
ctx.mod.join(matchId, 9962, "Guest");
|
|
ctx.mod.start(matchId, 9961);
|
|
toBattle(ctx.mod, matchId);
|
|
|
|
expect(() => ctx.mod.end(matchId, 9999)).toThrow("Only a technician");
|
|
ctx.mod.end(matchId, 9962); // guest can end it
|
|
expect(() => viewOf(ctx.mod, matchId, 9961)).toThrow("Match not found.");
|
|
});
|
|
|
|
it("tells attached clients when the drill is ended by its host", () => {
|
|
const matchId = ctx.mod.createRoom(9971, "Host");
|
|
ctx.mod.join(matchId, 9972, "Guest");
|
|
ctx.mod.start(matchId, 9971);
|
|
toBattle(ctx.mod, matchId);
|
|
ctx.mod.attach(matchId, 9972);
|
|
|
|
ctx.mod.end(matchId, 9971); // host ends it
|
|
const frames = ctx.delivered.filter((d) => d.message.t === "terminated");
|
|
// The actor gets no frame; the guest does, naming the host.
|
|
expect(frames.map((f) => f.userId)).toEqual([9972]);
|
|
const frame = frames[0]!.message;
|
|
expect(frame.t === "terminated" && frame.reason).toBe("host-ended");
|
|
expect(frame.t === "terminated" && frame.hostName).toBe("Host");
|
|
});
|
|
});
|
|
|
|
describe("arena bomperator module: disposal + awards handoff", () => {
|
|
it("force-expiring removes the match", () => {
|
|
const matchId = ctx.mod.createRoom(9911, "Host");
|
|
ctx.mod.forceExpire(matchId);
|
|
expect(() => viewOf(ctx.mod, matchId, 9911)).toThrow("Match not found.");
|
|
});
|
|
|
|
it("clearing for tests isolates subsequent specs", () => {
|
|
ctx.mod.createRoom(9921, "Host");
|
|
ctx.mod.clear();
|
|
expect(ctx.mod.listSummaries()).toHaveLength(0);
|
|
});
|
|
|
|
it("hands final results to the awards callback exactly once per match", () => {
|
|
const matchId = ctx.mod.createRoom(9931, "Host");
|
|
ctx.mod.join(matchId, 9932, "Guest");
|
|
ctx.mod.start(matchId, 9931);
|
|
// Dissolve below two active seats by walking away with a lapsed window.
|
|
toBattle(ctx.mod, matchId);
|
|
ctx.mod.leave(matchId, 9931);
|
|
ctx.mod.advance(matchId, 1, Date.now() + RECONNECT_WINDOW_MS + 5_000);
|
|
// Completion fired the handoff; disposing again must not repeat it.
|
|
ctx.mod.forceExpire(matchId);
|
|
expect(ctx.awards).toHaveLength(1);
|
|
expect(ctx.awards[0]!.matchId).toBe(matchId);
|
|
expect(ctx.awards[0]!.results.map((r) => r.userId).sort()).toEqual([9931, 9932]);
|
|
});
|
|
});
|