Files
OpenFrontIO/tests/server/MasterLobbyServiceHealth.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

119 lines
3.3 KiB
TypeScript

import EventEmitter from "events";
import { describe, expect, it, vi } from "vitest";
import { MasterLobbyService } from "server/MasterLobbyService";
import { ServerEnv } from "server/ServerEnv";
vi.mock("server/Logger", () => ({
logger: {
child: () => ({
error: vi.fn(),
info: vi.fn(),
}),
},
}));
vi.mock("server/PollingLoop", () => ({
startPolling: vi.fn(),
}));
function createMockWorker(): EventEmitter {
const emitter = new EventEmitter();
(emitter as any).send = vi.fn();
return emitter;
}
function sendWorkerReady(worker: EventEmitter, workerId: number) {
worker.emit("message", { type: "workerReady", workerId });
}
function createService(numWorkers: number): MasterLobbyService {
vi.spyOn(ServerEnv, "numWorkers").mockReturnValue(numWorkers);
const log = { info: vi.fn(), error: vi.fn() } as any;
return new MasterLobbyService({} as any, log);
}
function startAllWorkers(
service: MasterLobbyService,
count: number,
): { id: number; w: EventEmitter }[] {
const workers = Array.from({ length: count }, (_, i) => {
const id = i + 1;
const w = createMockWorker();
service.registerWorker(id, w as any);
return { id, w };
});
for (const { w, id } of workers) {
sendWorkerReady(w, id);
}
return workers;
}
describe("MasterLobbyService.isHealthy", () => {
it("unhealthy before any workers register", () => {
const service = createService(4);
expect(service.isHealthy()).toBe(false);
});
it("unhealthy when workers registered but not ready", () => {
const service = createService(2);
service.registerWorker(1, createMockWorker() as any);
expect(service.isHealthy()).toBe(false);
});
it("unhealthy when only some workers are ready (server not started)", () => {
const service = createService(4);
// 1 of 4 ready -- not enough to flip `started`
const w1 = createMockWorker();
service.registerWorker(1, w1 as any);
sendWorkerReady(w1, 1);
expect(service.isHealthy()).toBe(false);
});
it("healthy once all workers are ready", () => {
const service = createService(2);
startAllWorkers(service, 2);
expect(service.isHealthy()).toBe(true);
});
it("stays healthy after a single worker crash", () => {
const service = createService(4);
startAllWorkers(service, 4);
service.removeWorker(4); // 3 of 4 left, threshold is 2
expect(service.isHealthy()).toBe(true);
});
it("goes unhealthy when too many workers crash", () => {
const service = createService(4);
startAllWorkers(service, 4);
service.removeWorker(2);
service.removeWorker(3);
service.removeWorker(4); // 1 of 4 left, threshold is 2
expect(service.isHealthy()).toBe(false);
});
it("single-worker setup goes unhealthy on crash", () => {
const service = createService(1);
startAllWorkers(service, 1);
expect(service.isHealthy()).toBe(true);
service.removeWorker(1);
expect(service.isHealthy()).toBe(false);
});
it("odd worker count: threshold rounds up (3 workers)", () => {
const service = createService(3);
startAllWorkers(service, 3);
// min = 3/2 = 1.5, so 2 ready is enough, 1 is not
service.removeWorker(3);
expect(service.isHealthy()).toBe(true);
service.removeWorker(2);
expect(service.isHealthy()).toBe(false);
});
});