mirror of
https://github.com/openfrontio/OpenFrontIO.git
synced 2026-07-10 23:44:13 +00:00
a7f992e9b0
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>
175 lines
4.4 KiB
TypeScript
175 lines
4.4 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 }),
|
|
},
|
|
ClientMessageSchema: {
|
|
safeParse: (data: any) => ({ success: true, data: data }),
|
|
},
|
|
};
|
|
});
|
|
|
|
import { GameType } from "engine/game/Game";
|
|
import { Client } from "server/Client";
|
|
import { GameServer } from "server/GameServer";
|
|
|
|
function makeMockWs() {
|
|
const handlers: Record<string, (...args: any[]) => any> = {};
|
|
return {
|
|
on: (event: string, handler: (...args: any[]) => any) => {
|
|
handlers[event] = handler;
|
|
},
|
|
removeAllListeners: (_event: string) => {},
|
|
send: vi.fn(),
|
|
close: vi.fn(),
|
|
readyState: 1,
|
|
trigger: (event: string, ...args: any[]) => handlers[event]?.(...args),
|
|
};
|
|
}
|
|
|
|
function makeClient(
|
|
clientID: string,
|
|
persistentID: string,
|
|
role?: string,
|
|
): { client: Client; ws: ReturnType<typeof makeMockWs> } {
|
|
const ws = makeMockWs();
|
|
const client = new Client(
|
|
clientID,
|
|
persistentID,
|
|
null,
|
|
role ?? null,
|
|
undefined,
|
|
"127.0.0.1",
|
|
"TestUser",
|
|
null,
|
|
ws as any,
|
|
undefined,
|
|
undefined,
|
|
[],
|
|
);
|
|
return { client, ws };
|
|
}
|
|
|
|
describe("GameServer - kick_player authorization", () => {
|
|
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();
|
|
});
|
|
|
|
function makeGame(creatorPersistentID?: string) {
|
|
return new GameServer(
|
|
"test-game",
|
|
mockLogger,
|
|
Date.now(),
|
|
{ gameType: GameType.Private } as any,
|
|
creatorPersistentID,
|
|
);
|
|
}
|
|
|
|
async function sendKickMessage(
|
|
ws: ReturnType<typeof makeMockWs>,
|
|
target: string,
|
|
) {
|
|
await ws.trigger(
|
|
"message",
|
|
JSON.stringify({
|
|
type: "intent",
|
|
intent: { type: "kick_player", target },
|
|
}),
|
|
);
|
|
}
|
|
|
|
it("lobby creator can kick another player with lobby_creator reason", async () => {
|
|
const game = makeGame("creator-pid");
|
|
const kickSpy = vi.spyOn(game, "kickClient");
|
|
|
|
const { client: creator, ws: creatorWs } = makeClient(
|
|
"creator-cid",
|
|
"creator-pid",
|
|
);
|
|
const { client: target } = makeClient("target-cid", "target-pid");
|
|
|
|
game.joinClient(creator);
|
|
game.joinClient(target);
|
|
|
|
await sendKickMessage(creatorWs, "target-cid");
|
|
|
|
expect(kickSpy).toHaveBeenCalledOnce();
|
|
expect(kickSpy).toHaveBeenCalledWith(
|
|
"target-cid",
|
|
"kick_reason.lobby_creator",
|
|
);
|
|
});
|
|
|
|
it("admin-flared player can kick another player with admin reason", async () => {
|
|
const game = makeGame();
|
|
const kickSpy = vi.spyOn(game, "kickClient");
|
|
|
|
const { client: admin, ws: adminWs } = makeClient(
|
|
"admin-cid",
|
|
"admin-pid",
|
|
"admin",
|
|
);
|
|
const { client: target } = makeClient("target-cid", "target-pid");
|
|
|
|
game.joinClient(admin);
|
|
game.joinClient(target);
|
|
|
|
await sendKickMessage(adminWs, "target-cid");
|
|
|
|
expect(kickSpy).toHaveBeenCalledOnce();
|
|
expect(kickSpy).toHaveBeenCalledWith("target-cid", "kick_reason.admin");
|
|
});
|
|
|
|
it("non-creator non-admin cannot kick", async () => {
|
|
const game = makeGame("creator-pid");
|
|
const kickSpy = vi.spyOn(game, "kickClient");
|
|
|
|
const { client: creator } = makeClient("creator-cid", "creator-pid");
|
|
const { client: rando, ws: randoWs } = makeClient("rando-cid", "rando-pid");
|
|
const { client: target } = makeClient("target-cid", "target-pid");
|
|
|
|
game.joinClient(creator);
|
|
game.joinClient(rando);
|
|
game.joinClient(target);
|
|
|
|
await sendKickMessage(randoWs, "target-cid");
|
|
|
|
expect(kickSpy).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("cannot kick yourself even as lobby creator", async () => {
|
|
const game = makeGame("creator-pid");
|
|
const kickSpy = vi.spyOn(game, "kickClient");
|
|
|
|
const { client: creator, ws: creatorWs } = makeClient(
|
|
"creator-cid",
|
|
"creator-pid",
|
|
);
|
|
game.joinClient(creator);
|
|
|
|
await sendKickMessage(creatorWs, "creator-cid");
|
|
|
|
expect(kickSpy).not.toHaveBeenCalled();
|
|
});
|
|
});
|