Files
OpenFrontIO/tests/LobbySocket.test.ts
T
Evan Pelle a7f992e9b0 refactor: convert to npm-workspaces monorepo (engine/core-public/shared/client/server)
Restructure the single src/ tree into an npm-workspaces monorepo under
packages/, rename core -> engine, extract a types-only core-public layer,
and break the pre-existing engine -> client dependency cycle.

Structure (packages/):
  core-public  public API/wire schemas + shared enums (clean leaf)
  shared       framework-agnostic helpers (clean leaf)
  engine       deterministic simulation (was src/core)
  client       rendering/UI (was src/client)
  server       coordination (was src/server)

Dependency DAG: engine -> {core-public, shared}; client -> {core-public,
shared, engine}; server -> {core-public, engine}.

- npm workspaces: root package.json workspaces + per-package package.json;
  tsconfig.base.json holds shared options + path aliases
  (core-public/* shared/* engine/* client/* server/*) resolved uniformly by
  tsc, Vite (resolve.tsconfigPaths), Vitest, and tsx. Lockfile regenerated.
- core-public: moved Schemas/ApiSchemas/CosmeticSchemas/StatsSchemas/
  ClanApiSchemas/WorkerSchemas/Base64/PatternDecoder; extracted the enums
  (GameTypes), GameEvent type, emoji table, and GraphicsOverrides schema.
  Engine re-exports the moved enums/types so existing imports keep working.
- Broke engine -> client cycle:
  - renderNumber/renderTroops -> shared/format
  - NameBoxCalculator moved into engine
  - username validation returns translation key + params; client translates
  - applyStateUpdate moved to client (operates on the render-only PlayerState)
  - Config/UnitGrid/execution-Util/GameImpl now use structural read
    interfaces (engine/game/ReadViews: PlayerLike/UnitLike/GameLike) instead
    of importing client view classes; client imports view classes from a new
    client/view barrel; deleted the engine/game/GameView re-export shim.
- Build/deploy updated: vite.config, index.html, eslint, Dockerfile
  (copies packages/ + tsconfig.base.json before npm ci), .vscode, tests.

Verified: tsc --noEmit clean; 1364 + 65 tests pass; production vite build
succeeds; engine has zero client/server imports; core-public and shared are
dependency leaves.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-10 16:51:03 +00:00

139 lines
4.5 KiB
TypeScript

import { beforeEach, describe, expect, it, vi } from "vitest";
import { PublicLobbySocket } from "client/LobbySocket";
import {
PublicGameInfo,
PublicGames,
PublicGameType,
} from "core-public/Schemas";
function lobby(
gameID: string,
numClients: number,
publicGameType: PublicGameType = "ffa",
): PublicGameInfo {
return { gameID, numClients, publicGameType };
}
function fullMessage(
serverTime: number,
games: Partial<Record<PublicGameType, PublicGameInfo[]>>,
) {
return JSON.stringify({
type: "full",
serverTime,
games: { ffa: [], team: [], special: [], ...games },
});
}
function countsMessage(serverTime: number, counts: Record<string, number>) {
return JSON.stringify({ type: "counts", serverTime, counts });
}
function makeSocket() {
const callback = vi.fn<(g: PublicGames) => void>();
const socket = new PublicLobbySocket(callback);
const dispatch = (data: string) => {
(socket as any).handleMessage({ data } as MessageEvent);
};
return { socket, callback, dispatch };
}
describe("PublicLobbySocket.handleMessage", () => {
beforeEach(() => {
vi.spyOn(console, "error").mockImplementation(() => {});
});
it("delivers a full snapshot to the callback", () => {
const { callback, dispatch } = makeSocket();
dispatch(
fullMessage(1000, {
ffa: [lobby("g1", 3)],
team: [lobby("g2", 5, "team")],
}),
);
expect(callback).toHaveBeenCalledTimes(1);
const arg = callback.mock.calls[0][0];
expect(arg.serverTime).toBe(1000);
expect(arg.games.ffa).toEqual([lobby("g1", 3)]);
expect(arg.games.team).toEqual([lobby("g2", 5, "team")]);
});
it("patches numClients onto the last full snapshot when counts arrives", () => {
const { callback, dispatch } = makeSocket();
dispatch(fullMessage(1000, { ffa: [lobby("g1", 3), lobby("g2", 4)] }));
callback.mockClear();
dispatch(countsMessage(1500, { g1: 7, g2: 4 }));
expect(callback).toHaveBeenCalledTimes(1);
const arg = callback.mock.calls[0][0];
expect(arg.serverTime).toBe(1500);
expect(arg.games.ffa).toEqual([lobby("g1", 7), lobby("g2", 4)]);
// Static fields (gameConfig, startsAt, publicGameType) survive the patch.
expect(arg.games.ffa?.[0].publicGameType).toBe("ffa");
});
it("ignores counts arriving before any full snapshot", () => {
const { callback, dispatch } = makeSocket();
dispatch(countsMessage(1000, { g1: 5 }));
expect(callback).not.toHaveBeenCalled();
});
it("leaves lobbies whose gameID is absent from counts unchanged", () => {
const { callback, dispatch } = makeSocket();
dispatch(fullMessage(1000, { ffa: [lobby("g1", 3), lobby("g2", 4)] }));
callback.mockClear();
dispatch(countsMessage(1500, { g1: 9 }));
const arg = callback.mock.calls[0][0];
expect(arg.games.ffa).toEqual([lobby("g1", 9), lobby("g2", 4)]);
});
it("applies consecutive counts deltas on top of the merged state", () => {
const { callback, dispatch } = makeSocket();
dispatch(fullMessage(1000, { ffa: [lobby("g1", 1)] }));
dispatch(countsMessage(1500, { g1: 2 }));
dispatch(countsMessage(2000, { g1: 3 }));
expect(callback).toHaveBeenCalledTimes(3);
expect(callback.mock.calls[2][0].games.ffa).toEqual([lobby("g1", 3)]);
expect(callback.mock.calls[2][0].serverTime).toBe(2000);
});
it("replaces lobby set when a fresh full snapshot arrives", () => {
const { callback, dispatch } = makeSocket();
dispatch(fullMessage(1000, { ffa: [lobby("g1", 3)] }));
dispatch(fullMessage(2000, { ffa: [lobby("g2", 5)] }));
const arg = callback.mock.calls[1][0];
expect(arg.games.ffa).toEqual([lobby("g2", 5)]);
expect(arg.serverTime).toBe(2000);
});
it("does not call the callback on malformed JSON", () => {
const { callback, dispatch } = makeSocket();
dispatch("not json");
expect(callback).not.toHaveBeenCalled();
});
it("does not call the callback on schema-invalid messages", () => {
const { callback, dispatch } = makeSocket();
dispatch(JSON.stringify({ type: "bogus", serverTime: 1 }));
expect(callback).not.toHaveBeenCalled();
});
it("does not mutate the previously-delivered snapshot when applying counts", () => {
const { callback, dispatch } = makeSocket();
dispatch(fullMessage(1000, { ffa: [lobby("g1", 3)] }));
const prevSnapshot = callback.mock.calls[0][0];
const prevFfa = prevSnapshot.games.ffa;
dispatch(countsMessage(1500, { g1: 99 }));
expect(prevSnapshot.serverTime).toBe(1000);
expect(prevFfa).toEqual([lobby("g1", 3)]);
});
});