mirror of
https://github.com/openfrontio/OpenFrontIO.git
synced 2026-06-21 13:50:15 +00:00
275fd0dccc
## Description: This is a refactor to simplify config handling. Replaces the per-environment DevConfig/PreprodConfig/ProdConfig class hierarchy with two static classes: ClientEnv (browser main thread, reads from window.BOOTSTRAP_CONFIG) and ServerEnv (Node server, reads from process.env). The four config classes are deleted, the abstract DefaultServerConfig is gone, and DefaultConfig is renamed to Config. The values that flow server → client (gameEnv, numWorkers, turnstileSiteKey, jwtAudience, instanceId) used to be baked into the hardcoded per-env classes. They're now real env vars on the server, embedded into a single window.BOOTSTRAP_CONFIG object in index.html at request time (alongside the existing gitCommit/assetManifest/cdnBase globals, which moved into the same object), and read back by ClientEnv on the client. The dev defaults previously hidden inside DevServerConfig are now explicit in start:server-dev (NUM_WORKERS=2, TURNSTILE_SITE_KEY=1x..., JWT_AUDIENCE=localhost, etc.) and in vite.config.ts's html plugin inject.data. Production deploys plumb NUM_WORKERS and TURNSTILE_SITE_KEY through deploy.yml (GitHub vars) into the remote env file; JWT_AUDIENCE is derived from DOMAIN in deploy.sh. The dynamic /api/instance endpoint is gone — INSTANCE_ID rides along in BOOTSTRAP_CONFIG now. ServerEnv is the only thing server code touches; ClientEnv is browser-only. The two classes have intentional overlap (env, numWorkers, jwtIssuer, gameCreationRate, workerIndex, etc.) since they derive identical logic from different sources — there's a TODO in each to consolidate via a shared helper later. The game-logic Config no longer stores a ServerConfig/ClientEnv reference and its serverConfig() getter is gone; the one caller (MultiTabModal) now reads ClientEnv.env() directly. Worker init no longer carries server-config values since nothing in the worker actually reads them. ## Please complete the following: - [x] I have added screenshots for all UI updates - [x] I process any text displayed to the user through translateText() and I've added it to the en.json file - [x] I have added relevant tests to the test directory - [x] I confirm I have thoroughly tested these changes and take full responsibility for any bugs introduced ## Please put your Discord username so you can be contacted if a bug or regression is found: evan
119 lines
3.3 KiB
TypeScript
119 lines
3.3 KiB
TypeScript
import EventEmitter from "events";
|
|
import { describe, expect, it, vi } from "vitest";
|
|
import { MasterLobbyService } from "../../src/server/MasterLobbyService";
|
|
import { ServerEnv } from "../../src/server/ServerEnv";
|
|
|
|
vi.mock("../../src/server/Logger", () => ({
|
|
logger: {
|
|
child: () => ({
|
|
error: vi.fn(),
|
|
info: vi.fn(),
|
|
}),
|
|
},
|
|
}));
|
|
|
|
vi.mock("../../src/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);
|
|
});
|
|
});
|