import { describe, expect, test } from "bun:test"; // config.ts reads process.env once at import time, so these run the same // parsing logic against a fresh module instance per case. async function loadConfig(env: Record) { const saved: Record = {}; for (const k of Object.keys(env)) { saved[k] = process.env[k]; process.env[k] = env[k]; } try { // Cache-bust so the module body re-runs against the patched env. const mod = await import(`../src/config.ts?t=${Math.random()}`); return mod.default as typeof import("../src/config.ts").default; } finally { for (const [k, v] of Object.entries(saved)) { if (v === undefined) delete process.env[k]; else process.env[k] = v; } } } describe("config env parsing", () => { test("unset falls back to the default", async () => { const c = await loadConfig({}); expect(c.MAX_TITLE_BYTES).toBe(500); expect(c.CI_MAX_HISTORY).toBe(50); }); test("empty string falls back to the default", async () => { const c = await loadConfig({ MAX_TITLE_BYTES: "" }); expect(c.MAX_TITLE_BYTES).toBe(500); }); test("garbage falls back to the default", async () => { const c = await loadConfig({ MAX_TITLE_BYTES: "abc" }); expect(c.MAX_TITLE_BYTES).toBe(500); }); test("a valid number is used", async () => { const c = await loadConfig({ MAX_TITLE_BYTES: "120" }); expect(c.MAX_TITLE_BYTES).toBe(120); }); test("explicit 0 is honoured, not replaced by the default", async () => { expect((await loadConfig({ MAX_RENDER_BYTES: "0" })).MAX_RENDER_BYTES).toBe(0); expect((await loadConfig({ INLINE_MAX_BYTES: "0" })).INLINE_MAX_BYTES).toBe(0); expect((await loadConfig({ CI_MAX_HISTORY: "0" })).CI_MAX_HISTORY).toBe(0); }); test("BASE_URL default tracks PORT", async () => { const c = await loadConfig({ PORT: "8080" }); expect(c.PORT).toBe(8080); expect(c.BASE_URL).toBe("http://localhost:8080"); expect(c.PUBLIC_ORIGIN).toBe("http://localhost:8080"); expect(c.PUBLIC_HTTPS).toBe(false); }); test("an https BASE_URL sets the derived origin fields", async () => { const c = await loadConfig({ BASE_URL: "https://forge.example.com" }); expect(c.PUBLIC_HTTPS).toBe(true); expect(c.PUBLIC_ORIGIN).toBe("https://forge.example.com"); expect(c.COMMITTER_EMAIL).toContain("@forge.example.com"); }); });