Files
OpenFrontIO/tests/server/GameLifecycle.test.ts
Evan Pelle 43d07ca85f refactor: rename core-public -> engine-public; fold helpers in; drop shared
Tighten the package model around the determinism invariant: everything the
engine imports must itself be deterministic.

- Rename core-public -> engine-public (package, dir, `engine-public/*` alias,
  all 118 import sites). It is the deterministic public surface the engine
  depends on and that client/server also use.
- Move the pure formatting helpers (renderNumber/renderTroops) from shared
  into engine-public/format — the engine uses them and they're deterministic.
- Drop the now-empty shared package (and its alias). `shared` was intended for
  client/server-shared, engine-forbidden code; there is none yet, so it is
  removed and can be re-added when such code appears.

Final graph: engine-public (leaf) <- engine <- {client, server};
client/server also -> engine-public.

Verified: tsc clean; 1364 + 65 tests pass; prod build succeeds; engine-public
is a leaf; engine has no client/server imports. Lockfile regenerated.

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

89 lines
2.5 KiB
TypeScript

import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
vi.mock("engine-public/Schemas", async () => {
const actual = (await vi.importActual("engine-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);
});
});