import { describe, it, expect } from "vitest"; import { loadConfig, parseAllowedUserIds, isUserAllowed } from "../src/config/config.js"; const baseEnv = { BOT_TOKEN: "123:abc", LLM_API_KEY: "litellm-master", POSTGRES_PASSWORD: "pw", }; describe("loadConfig", () => { it("loads a valid config with defaults", () => { const cfg = loadConfig(baseEnv); expect(cfg.botToken).toBe("123:abc"); expect(cfg.llmModel).toBe("mem0-openai"); expect(cfg.appEnv).toBe("development"); expect(cfg.botDbName).toBe("bot"); expect(cfg.postgresHost).toBe("mem0-postgres"); }); it("throws when BOT_TOKEN is missing", () => { expect(() => loadConfig({ LLM_API_KEY: "x" })).toThrow(/botToken/); }); it("throws when LLM_API_KEY is missing", () => { expect(() => loadConfig({ BOT_TOKEN: "123:abc" })).toThrow(/llmApiKey/); }); }); describe("summaryEnabled", () => { it("defaults to true when SUMMARY_ENABLED is unset", () => { expect(loadConfig(baseEnv).summaryEnabled).toBe(true); expect(loadConfig({ ...baseEnv, SUMMARY_ENABLED: "" }).summaryEnabled).toBe(true); }); it("honours true", () => { expect(loadConfig({ ...baseEnv, SUMMARY_ENABLED: "true" }).summaryEnabled).toBe(true); expect(loadConfig({ ...baseEnv, SUMMARY_ENABLED: "1" }).summaryEnabled).toBe(true); }); it("honours false / 0", () => { expect(loadConfig({ ...baseEnv, SUMMARY_ENABLED: "false" }).summaryEnabled).toBe(false); expect(loadConfig({ ...baseEnv, SUMMARY_ENABLED: "0" }).summaryEnabled).toBe(false); }); it("defaults summaryModel to empty (falls back to main model)", () => { expect(loadConfig(baseEnv).summaryModel).toBe(""); }); }); describe("parseAllowedUserIds", () => { it("splits and trims", () => { expect(parseAllowedUserIds("1, 2 ,3")).toEqual(["1", "2", "3"]); expect(parseAllowedUserIds("")).toEqual([]); expect(parseAllowedUserIds(" ")).toEqual([]); }); }); describe("isUserAllowed", () => { it("allows everyone when the allowlist is empty", () => { expect(isUserAllowed([], "999")).toBe(true); }); it("restricts to allowlisted ids", () => { expect(isUserAllowed(["1", "2"], "2")).toBe(true); expect(isUserAllowed(["1", "2"], "3")).toBe(false); }); });