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

89 lines
2.5 KiB
TypeScript

import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
vi.mock("core-public/Schemas", async () => {
const actual = (await vi.importActual("core-public/Schemas")) as any;
return {
...actual,
GameStartInfoSchema: {
safeParse: (data: any) => ({ success: true, data: data }),
},
ServerPrestartMessageSchema: {
safeParse: (data: any) => ({ success: true, data: data }),
},
};
});
import { GameType } from "engine/game/Game";
import { GameServer } from "server/GameServer";
describe("GameLifecycle", () => {
let mockLogger: any;
beforeEach(() => {
vi.useFakeTimers();
mockLogger = {
child: vi.fn().mockReturnThis(),
info: vi.fn(),
warn: vi.fn(),
error: vi.fn(),
};
});
afterEach(() => {
vi.restoreAllMocks();
vi.clearAllTimers();
});
it("should not start turn interval if game has ended", async () => {
const game = new GameServer("test-game", mockLogger, Date.now(), {
gameType: GameType.Private,
} as any);
// Call end() first - this should set _hasEnded
await game.end();
// Now call start() - this should be a no-op due to our fix
game.start();
// Check if the interval ID is set (it shouldn't be)
expect((game as any).endTurnIntervalID).toBeUndefined();
// Check if _hasStarted remained false (or at least no interval was created)
expect(game.hasStarted()).toBe(false);
});
it("should clear turn interval and set _hasEnded on end()", async () => {
// We need to initialize the game such that start() can succeed
const game = new GameServer("test-game", mockLogger, Date.now(), {
gameType: GameType.Private,
gameMap: "plains",
gameMapSize: 100,
} as any);
// Manually trigger prestart to fulfill some internal checks if necessary
game.prestart();
// start() should create the interval
game.start();
expect((game as any).endTurnIntervalID).toBeDefined();
// end() should clear it
await game.end();
expect((game as any).endTurnIntervalID).toBeUndefined();
expect((game as any)._hasEnded).toBe(true);
});
it("should be resilient to multiple end() calls", async () => {
const game = new GameServer("test-game", mockLogger, Date.now(), {
gameType: GameType.Private,
} as any);
await game.end();
expect((game as any)._hasEnded).toBe(true);
// Should not throw or crash
await expect(game.end()).resolves.toBeUndefined();
expect((game as any)._hasEnded).toBe(true);
});
});