Files
OpenFrontIO/tests/server/ServerEnv.test.ts
T
Evan 275fd0dccc refactor: collapse per-env Configs into ClientEnv + ServerEnv (#3906)
## 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
2026-05-11 19:24:01 -07:00

110 lines
2.9 KiB
TypeScript

import { afterEach, describe, expect, test, vi } from "vitest";
import { ServerEnv } from "../../src/server/ServerEnv";
describe("ServerEnv.numWorkers", () => {
afterEach(() => {
vi.unstubAllEnvs();
});
test("returns parsed value when valid", () => {
vi.stubEnv("NUM_WORKERS", "4");
expect(ServerEnv.numWorkers()).toBe(4);
});
test("throws when unset", () => {
vi.stubEnv("NUM_WORKERS", "");
expect(() => ServerEnv.numWorkers()).toThrow(/NUM_WORKERS not set/);
});
test("throws on non-numeric", () => {
vi.stubEnv("NUM_WORKERS", "abc");
expect(() => ServerEnv.numWorkers()).toThrow(/Invalid NUM_WORKERS/);
});
test("throws on zero", () => {
vi.stubEnv("NUM_WORKERS", "0");
expect(() => ServerEnv.numWorkers()).toThrow(/Invalid NUM_WORKERS/);
});
test("throws on negative", () => {
vi.stubEnv("NUM_WORKERS", "-2");
expect(() => ServerEnv.numWorkers()).toThrow(/Invalid NUM_WORKERS/);
});
});
describe("ServerEnv.turnstileSiteKey", () => {
afterEach(() => {
vi.unstubAllEnvs();
});
test("returns value when set", () => {
vi.stubEnv("TURNSTILE_SITE_KEY", "site-key");
expect(ServerEnv.turnstileSiteKey()).toBe("site-key");
});
test("throws when unset", () => {
vi.stubEnv("TURNSTILE_SITE_KEY", "");
expect(() => ServerEnv.turnstileSiteKey()).toThrow(
/TURNSTILE_SITE_KEY not set/,
);
});
});
describe("ServerEnv.jwtAudience", () => {
afterEach(() => {
vi.unstubAllEnvs();
});
test("returns DOMAIN when set", () => {
vi.stubEnv("DOMAIN", "openfront.io");
expect(ServerEnv.jwtAudience()).toBe("openfront.io");
});
test("throws when DOMAIN unset", () => {
vi.stubEnv("DOMAIN", "");
expect(() => ServerEnv.jwtAudience()).toThrow(/DOMAIN not set/);
});
});
describe("ServerEnv.jwtIssuer", () => {
afterEach(() => {
vi.unstubAllEnvs();
});
test("maps 'localhost' to http://localhost:8787", () => {
vi.stubEnv("DOMAIN", "localhost");
expect(ServerEnv.jwtIssuer()).toBe("http://localhost:8787");
});
test("derives api.<audience> for non-localhost", () => {
vi.stubEnv("DOMAIN", "openfront.io");
expect(ServerEnv.jwtIssuer()).toBe("https://api.openfront.io");
});
});
describe("ServerEnv.allowedFlares", () => {
afterEach(() => {
vi.unstubAllEnvs();
});
test("returns undefined when unset", () => {
vi.stubEnv("ALLOWED_FLARES", "");
expect(ServerEnv.allowedFlares()).toBeUndefined();
});
test("parses a single value", () => {
vi.stubEnv("ALLOWED_FLARES", "admin");
expect(ServerEnv.allowedFlares()).toEqual(["admin"]);
});
test("parses CSV", () => {
vi.stubEnv("ALLOWED_FLARES", "admin,beta,internal");
expect(ServerEnv.allowedFlares()).toEqual(["admin", "beta", "internal"]);
});
test("trims whitespace and drops empties", () => {
vi.stubEnv("ALLOWED_FLARES", " admin , , beta ");
expect(ServerEnv.allowedFlares()).toEqual(["admin", "beta"]);
});
});