import { describe, it, expect, vi, beforeEach, afterEach } from "vitest"; describe("loadConfig", () => { const originalEnv = { ...process.env }; beforeEach(() => { vi.resetModules(); }); afterEach(() => { process.env = { ...originalEnv }; }); async function loadConfig() { const mod = await import("../config.js"); return mod.loadConfig(); } it("returns default port 3001 when WEB_PORT is undefined", async () => { delete process.env.WEB_PORT; const config = await loadConfig(); expect(config.port).toBe(3001); }); it("parses WEB_PORT as an integer", async () => { process.env.WEB_PORT = "8080"; const config = await loadConfig(); expect(config.port).toBe(8080); expect(typeof config.port).toBe("number"); }); it("returns default appview URL when APPVIEW_URL is undefined", async () => { delete process.env.APPVIEW_URL; const config = await loadConfig(); expect(config.appviewUrl).toBe("http://localhost:3000"); }); it("uses provided environment variables", async () => { process.env.WEB_PORT = "9000"; process.env.APPVIEW_URL = "https://api.atbb.space"; const config = await loadConfig(); expect(config.port).toBe(9000); expect(config.appviewUrl).toBe("https://api.atbb.space"); }); it("returns NaN for port when WEB_PORT is empty string (?? does not catch empty strings)", async () => { process.env.WEB_PORT = ""; const config = await loadConfig(); // Documents a gap: ?? only catches null/undefined, not "" expect(config.port).toBeNaN(); }); it("returns empty string for appviewUrl when APPVIEW_URL is empty string", async () => { process.env.APPVIEW_URL = ""; const config = await loadConfig(); // Documents a gap: ?? only catches null/undefined, not "" expect(config.appviewUrl).toBe(""); }); });