mirror of
https://github.com/openfrontio/OpenFrontIO.git
synced 2026-06-21 16:36:36 +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
88 lines
2.7 KiB
TypeScript
88 lines
2.7 KiB
TypeScript
import { ClientEnv } from "src/client/ClientEnv";
|
|
import { beforeEach, describe, expect, test, vi } from "vitest";
|
|
import { GameEnv, parseGameEnv } from "../../../src/core/configuration/Config";
|
|
|
|
describe("parseGameEnv", () => {
|
|
test("maps 'dev' to GameEnv.Dev", () => {
|
|
expect(parseGameEnv("dev")).toBe(GameEnv.Dev);
|
|
});
|
|
test("maps 'staging' to GameEnv.Preprod", () => {
|
|
expect(parseGameEnv("staging")).toBe(GameEnv.Preprod);
|
|
});
|
|
test("maps 'prod' to GameEnv.Prod", () => {
|
|
expect(parseGameEnv("prod")).toBe(GameEnv.Prod);
|
|
});
|
|
test("throws on undefined", () => {
|
|
expect(() => parseGameEnv(undefined)).toThrow(/unsupported game env/);
|
|
});
|
|
test("throws on unknown value", () => {
|
|
expect(() => parseGameEnv("production")).toThrow(/unsupported game env/);
|
|
});
|
|
});
|
|
|
|
describe("ClientEnv", () => {
|
|
beforeEach(() => {
|
|
vi.restoreAllMocks();
|
|
window.BOOTSTRAP_CONFIG = undefined;
|
|
ClientEnv.reset();
|
|
});
|
|
|
|
test("reads from window.BOOTSTRAP_CONFIG without fetching", () => {
|
|
window.BOOTSTRAP_CONFIG = {
|
|
gameEnv: "staging",
|
|
numWorkers: 4,
|
|
turnstileSiteKey: "test-key",
|
|
jwtAudience: "openfront.dev",
|
|
instanceId: "TEST_ID",
|
|
gitCommit: "abc123",
|
|
};
|
|
const fetchSpy = vi.spyOn(globalThis, "fetch");
|
|
|
|
expect(ClientEnv.env()).toBe(GameEnv.Preprod);
|
|
expect(ClientEnv.numWorkers()).toBe(4);
|
|
expect(ClientEnv.turnstileSiteKey()).toBe("test-key");
|
|
expect(ClientEnv.jwtAudience()).toBe("openfront.dev");
|
|
expect(ClientEnv.instanceId()).toBe("TEST_ID");
|
|
expect(fetchSpy).not.toHaveBeenCalled();
|
|
});
|
|
|
|
test("throws when BOOTSTRAP_CONFIG is undefined", () => {
|
|
expect(() => ClientEnv.env()).toThrow(/Missing BOOTSTRAP_CONFIG/);
|
|
});
|
|
|
|
test("throws when a required field is missing", () => {
|
|
window.BOOTSTRAP_CONFIG = {
|
|
gameEnv: "dev",
|
|
numWorkers: 1,
|
|
turnstileSiteKey: "k",
|
|
jwtAudience: "localhost",
|
|
// instanceId missing
|
|
};
|
|
expect(() => ClientEnv.instanceId()).toThrow(/Missing BOOTSTRAP_CONFIG/);
|
|
});
|
|
|
|
test("jwtIssuer maps 'localhost' to http://localhost:8787", () => {
|
|
window.BOOTSTRAP_CONFIG = {
|
|
gameEnv: "dev",
|
|
numWorkers: 1,
|
|
turnstileSiteKey: "k",
|
|
jwtAudience: "localhost",
|
|
instanceId: "x",
|
|
gitCommit: "DEV",
|
|
};
|
|
expect(ClientEnv.jwtIssuer()).toBe("http://localhost:8787");
|
|
});
|
|
|
|
test("jwtIssuer derives api.<audience> for non-localhost", () => {
|
|
window.BOOTSTRAP_CONFIG = {
|
|
gameEnv: "prod",
|
|
numWorkers: 1,
|
|
turnstileSiteKey: "k",
|
|
jwtAudience: "openfront.io",
|
|
instanceId: "x",
|
|
gitCommit: "abc123",
|
|
};
|
|
expect(ClientEnv.jwtIssuer()).toBe("https://api.openfront.io");
|
|
});
|
|
});
|