mirror of
https://github.com/openfrontio/OpenFrontIO.git
synced 2026-08-18 09:14:09 +00:00
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>
167 lines
4.9 KiB
TypeScript
167 lines
4.9 KiB
TypeScript
import { beforeEach, describe, expect, it, vi } from "vitest";
|
|
|
|
vi.mock("client/Api", () => ({
|
|
getApiBase: vi.fn(() => "http://localhost:3000"),
|
|
}));
|
|
|
|
vi.mock("client/Auth", () => ({
|
|
getAuthHeader: vi.fn(async () => "Bearer test-token"),
|
|
}));
|
|
|
|
import {
|
|
banClanMember,
|
|
fetchClanBans,
|
|
unbanClanMember,
|
|
} from "client/ClanApi";
|
|
|
|
const okJson = (data: unknown, status = 200) => ({
|
|
ok: true,
|
|
status,
|
|
json: async () => data,
|
|
});
|
|
|
|
const failRes = (status: number, data: unknown = {}) => ({
|
|
ok: false,
|
|
status,
|
|
json: async () => data,
|
|
});
|
|
|
|
const mockFetch = (impl: (...args: unknown[]) => unknown) =>
|
|
vi.stubGlobal("fetch", vi.fn(impl));
|
|
|
|
beforeEach(() => {
|
|
vi.unstubAllGlobals();
|
|
vi.clearAllMocks();
|
|
});
|
|
|
|
describe("banClanMember", () => {
|
|
it("returns true on 204 success", async () => {
|
|
mockFetch(() => ({ ok: true, status: 204, json: async () => ({}) }));
|
|
const result = await banClanMember("TEST", "player-1");
|
|
expect(result).toBe(true);
|
|
});
|
|
|
|
it("sends reason in request body when provided", async () => {
|
|
const fetchSpy = vi.fn(
|
|
(_input: string | URL | Request, _init?: RequestInit) =>
|
|
Promise.resolve({ ok: true, status: 204, json: async () => ({}) }),
|
|
);
|
|
vi.stubGlobal("fetch", fetchSpy);
|
|
|
|
await banClanMember("TEST", "player-1", "spamming");
|
|
|
|
const body = JSON.parse(fetchSpy.mock.calls[0]![1]?.body as string);
|
|
expect(body).toEqual({ targetPublicId: "player-1", reason: "spamming" });
|
|
});
|
|
|
|
it("omits reason from request body when not provided", async () => {
|
|
const fetchSpy = vi.fn(
|
|
(_input: string | URL | Request, _init?: RequestInit) =>
|
|
Promise.resolve({ ok: true, status: 204, json: async () => ({}) }),
|
|
);
|
|
vi.stubGlobal("fetch", fetchSpy);
|
|
|
|
await banClanMember("TEST", "player-1");
|
|
|
|
const body = JSON.parse(fetchSpy.mock.calls[0]![1]?.body as string);
|
|
expect(body).toEqual({ targetPublicId: "player-1" });
|
|
expect(body).not.toHaveProperty("reason");
|
|
});
|
|
|
|
it("returns error object on failure", async () => {
|
|
mockFetch(() => failRes(403, { message: "insufficient permissions" }));
|
|
const result = await banClanMember("TEST", "player-1");
|
|
expect(result).toEqual({ error: "clan_modal.error_failed" });
|
|
});
|
|
|
|
it("returns network error on fetch failure", async () => {
|
|
vi.stubGlobal(
|
|
"fetch",
|
|
vi.fn(() => Promise.reject(new Error("offline"))),
|
|
);
|
|
const result = await banClanMember("TEST", "player-1");
|
|
expect(result).toEqual({ error: "clan_modal.error_network" });
|
|
});
|
|
});
|
|
|
|
describe("unbanClanMember", () => {
|
|
it("returns true on success", async () => {
|
|
mockFetch(() => ({ ok: true, status: 204, json: async () => ({}) }));
|
|
const result = await unbanClanMember("TEST", "player-1");
|
|
expect(result).toBe(true);
|
|
});
|
|
|
|
it("returns error object on failure", async () => {
|
|
mockFetch(() => failRes(409, { message: "Player not currently banned" }));
|
|
const result = await unbanClanMember("TEST", "player-1");
|
|
expect(result).toEqual({ error: "clan_modal.error_failed" });
|
|
});
|
|
|
|
it("returns network error on fetch failure", async () => {
|
|
vi.stubGlobal(
|
|
"fetch",
|
|
vi.fn(() => Promise.reject(new Error("offline"))),
|
|
);
|
|
const result = await unbanClanMember("TEST", "player-1");
|
|
expect(result).toEqual({ error: "clan_modal.error_network" });
|
|
});
|
|
});
|
|
|
|
describe("fetchClanBans", () => {
|
|
const bansResponse = {
|
|
results: [
|
|
{
|
|
publicId: "banned-1",
|
|
bannedBy: "officer-1",
|
|
reason: "toxic",
|
|
createdAt: "2024-06-01T00:00:00.000Z",
|
|
},
|
|
],
|
|
total: 1,
|
|
page: 1,
|
|
limit: 20,
|
|
};
|
|
|
|
it("returns parsed data on success", async () => {
|
|
mockFetch(() => okJson(bansResponse));
|
|
const result = await fetchClanBans("TEST");
|
|
expect(result).toEqual(bansResponse);
|
|
});
|
|
|
|
it("passes page and limit as query params", async () => {
|
|
const fetchSpy = vi.fn(
|
|
(_input: string | URL | Request, _init?: RequestInit) =>
|
|
Promise.resolve(okJson(bansResponse)),
|
|
);
|
|
vi.stubGlobal("fetch", fetchSpy);
|
|
|
|
await fetchClanBans("TEST", 2, 10);
|
|
|
|
const calledUrl = fetchSpy.mock.calls[0]![0] as string;
|
|
const url = new URL(calledUrl);
|
|
expect(url.searchParams.get("page")).toBe("2");
|
|
expect(url.searchParams.get("limit")).toBe("10");
|
|
});
|
|
|
|
it("returns false on non-ok response", async () => {
|
|
mockFetch(() => failRes(403));
|
|
const result = await fetchClanBans("TEST");
|
|
expect(result).toBe(false);
|
|
});
|
|
|
|
it("returns false when Zod validation fails", async () => {
|
|
mockFetch(() => okJson({ results: "not-an-array", total: 0 }));
|
|
const result = await fetchClanBans("TEST");
|
|
expect(result).toBe(false);
|
|
});
|
|
|
|
it("returns false on network error", async () => {
|
|
vi.stubGlobal(
|
|
"fetch",
|
|
vi.fn(() => Promise.reject(new Error("offline"))),
|
|
);
|
|
const result = await fetchClanBans("TEST");
|
|
expect(result).toBe(false);
|
|
});
|
|
});
|