import { afterEach, describe, expect, test } from "bun:test"; import { existsSync, mkdirSync, mkdtempSync, rmSync, utimesSync, writeFileSync } from "node:fs"; import { tmpdir } from "node:os"; import { join } from "node:path"; import { findCorpConfigFile, loadCorpConfigs, loadGlobalConfig, resolveEnterprisesDir, resolveGlobalConfigFile, saveGlobalConfig, syncLegacyEnterprisesDir, updateCorpConfig } from "./loader.js"; const ORIGINAL_USE_ENV = process.env.SEAL_USE_ENTERPRISES_DIR; const ORIGINAL_HOME_ENV = process.env.SEAL_HOME_ENTERPRISES_DIR; const ORIGINAL_USER_DIR_USE_ENV = process.env.SEAL_USE_USER_ENTERPRISES_DIR; const ORIGINAL_USER_DIR_HOME_ENV = process.env.SEAL_HOME_USER_ENTERPRISES_DIR; const ORIGINAL_CONFIG_FILE_USE_ENV = process.env.SEAL_USE_CONFIG_FILE; const ORIGINAL_CONFIG_FILE_HOME_ENV = process.env.SEAL_HOME_CONFIG_FILE; const ORIGINAL_CWD = process.cwd(); const tempDirs: string[] = []; afterEach(() => { process.chdir(ORIGINAL_CWD); if (ORIGINAL_USE_ENV === undefined) delete process.env.SEAL_USE_ENTERPRISES_DIR; else process.env.SEAL_USE_ENTERPRISES_DIR = ORIGINAL_USE_ENV; if (ORIGINAL_HOME_ENV === undefined) delete process.env.SEAL_HOME_ENTERPRISES_DIR; else process.env.SEAL_HOME_ENTERPRISES_DIR = ORIGINAL_HOME_ENV; if (ORIGINAL_USER_DIR_USE_ENV === undefined) delete process.env.SEAL_USE_USER_ENTERPRISES_DIR; else process.env.SEAL_USE_USER_ENTERPRISES_DIR = ORIGINAL_USER_DIR_USE_ENV; if (ORIGINAL_USER_DIR_HOME_ENV === undefined) delete process.env.SEAL_HOME_USER_ENTERPRISES_DIR; else process.env.SEAL_HOME_USER_ENTERPRISES_DIR = ORIGINAL_USER_DIR_HOME_ENV; if (ORIGINAL_CONFIG_FILE_USE_ENV === undefined) delete process.env.SEAL_USE_CONFIG_FILE; else process.env.SEAL_USE_CONFIG_FILE = ORIGINAL_CONFIG_FILE_USE_ENV; if (ORIGINAL_CONFIG_FILE_HOME_ENV === undefined) delete process.env.SEAL_HOME_CONFIG_FILE; else process.env.SEAL_HOME_CONFIG_FILE = ORIGINAL_CONFIG_FILE_HOME_ENV; for (const dir of tempDirs.splice(0)) { rmSync(dir, { recursive: true, force: true }); } }); describe("loadCorpConfigs", () => { test("loads configs from SEAL_USE_ENTERPRISES_DIR and falls back to SEAL_HOME_ENTERPRISES_DIR", () => { const dir = makeTempDir(); writeFileSync(join(dir, "example.hose.json"), "{}"); writeFileSync(join(dir, "local.json"), JSON.stringify({ id: "direct", name: "Direct", source: { type: "direct", token: "seal-token", sealUrl: "https://direct.sealai.cc" } })); process.env.SEAL_HOME_ENTERPRISES_DIR = dir; expect(resolveEnterprisesDir()).toBe(dir); expect(loadCorpConfigs().map((corp) => corp.id)).toEqual(["direct"]); const newDir = makeTempDir(); writeFileSync(join(newDir, "local-use.json"), JSON.stringify({ id: "direct-use", name: "Direct Use", source: { type: "direct", token: "seal-token", sealUrl: "https://direct.sealai.cc" } })); process.env.SEAL_USE_ENTERPRISES_DIR = newDir; expect(resolveEnterprisesDir()).toBe(newDir); expect(loadCorpConfigs().map((corp) => corp.id)).toEqual(["direct-use"]); }); test("returns no configs when explicit env directory does not exist", () => { process.env.SEAL_USE_ENTERPRISES_DIR = join(makeTempDir(), "missing"); expect(resolveEnterprisesDir()).toBeUndefined(); expect(loadCorpConfigs()).toEqual([]); }); test("does not load current directory enterprises by default, only reading user directory", () => { const workspace = makeTempDir(); const workspaceEnterprises = join(workspace, "enterprises"); const userDir = makeTempDir(); mkdirSync(workspaceEnterprises, { recursive: true }); writeCorpConfig(join(workspaceEnterprises, "cwd-only.json"), "cwd-only", "CWD Enterprise"); writeCorpConfig(join(userDir, "user-only.json"), "user-only", "User Only"); process.chdir(workspace); process.env.SEAL_USE_USER_ENTERPRISES_DIR = userDir; // cwd enterprises/ should NOT be loaded by default expect(loadCorpConfigs().map((corp) => corp.id)).toEqual(["user-only"]); }); test("loads testEnterprisesDir from global config when specified", () => { const testDir = makeTempDir(); const userDir = makeTempDir(); writeCorpConfig(join(testDir, "test-corp.json"), "test-corp", "Test Corp"); writeCorpConfig(join(userDir, "user-corp.json"), "user-corp", "User Corp"); const configFile = join(makeTempDir(), "config.json"); process.env.SEAL_USE_CONFIG_FILE = configFile; process.env.SEAL_USE_USER_ENTERPRISES_DIR = userDir; saveGlobalConfig({ testEnterprisesDir: testDir }); const corps = loadCorpConfigs(); expect(corps.map((corp) => corp.id).sort()).toEqual(["test-corp", "user-corp"].sort()); }); test("defaults global config to stateless", () => { process.env.SEAL_USE_CONFIG_FILE = join(makeTempDir(), "config.json"); expect(loadGlobalConfig()).toEqual({ stateful: false, lint: { enabled: false, activeLinter: "builtin", minScore: 0 } }); }); test("saves and loads the global state mode and testEnterprisesDir", () => { const file = join(makeTempDir(), "nested", "config.json"); process.env.SEAL_USE_CONFIG_FILE = file; expect(saveGlobalConfig({ stateful: true, defaultCorpId: "corp-a", testEnterprisesDir: "/tmp/test-corps" })).toEqual({ stateful: true, defaultCorpId: "corp-a", testEnterprisesDir: "/tmp/test-corps", lint: { enabled: false, activeLinter: "builtin", minScore: 0 } }); expect(resolveGlobalConfigFile()).toBe(file); expect(loadGlobalConfig()).toEqual({ stateful: true, defaultCorpId: "corp-a", testEnterprisesDir: "/tmp/test-corps", lint: { enabled: false, activeLinter: "builtin", minScore: 0 } }); }); test("finds and updates an enterprise config file in-place", () => { const dir = makeTempDir(); process.env.SEAL_USE_ENTERPRISES_DIR = dir; writeCorpConfig(join(dir, "alpha.json"), "alpha", "Alpha Corp"); expect(findCorpConfigFile("alpha")).toBe(join(dir, "alpha.json")); expect(findCorpConfigFile("nonexistent")).toBeUndefined(); const updated = updateCorpConfig("alpha", { environment: "production", testCorpId: "alpha-test" }); expect(updated.id).toBe("alpha"); expect(updated.environment).toBe("production"); expect(updated.testCorpId).toBe("alpha-test"); // Check on-disk persistence const reloaded = loadCorpConfigs().find((c) => c.id === "alpha"); expect(reloaded?.testCorpId).toBe("alpha-test"); // Unsetting / clearing testCorpId const cleared = updateCorpConfig("alpha", { testCorpId: undefined }); expect(cleared.testCorpId).toBeUndefined(); const reloadedAfterClear = loadCorpConfigs().find((c) => c.id === "alpha"); expect(reloadedAfterClear?.testCorpId).toBeUndefined(); }); }); describe("syncLegacyEnterprisesDir", () => { test("migrates files from legacy to primary and deletes legacy when > 7 days old", () => { const primary = makeTempDir(); const legacy = makeTempDir(); writeCorpConfig(join(legacy, "old-corp.json"), "old-corp", "Old Corp"); const tenDaysAgo = (Date.now() - 10 * 24 * 60 * 60 * 1000) / 1000; utimesSync(join(legacy, "old-corp.json"), tenDaysAgo, tenDaysAgo); utimesSync(legacy, tenDaysAgo, tenDaysAgo); const result = syncLegacyEnterprisesDir(primary, legacy); expect(result.copied).toEqual(["old-corp.json"]); expect(result.deletedLegacyDir).toBe(true); expect(existsSync(join(primary, "old-corp.json"))).toBe(true); expect(existsSync(legacy)).toBe(false); }); test("preserves legacy directory when modified within 7 days", () => { const primary = makeTempDir(); const legacy = makeTempDir(); writeCorpConfig(join(legacy, "recent-corp.json"), "recent-corp", "Recent Corp"); const result = syncLegacyEnterprisesDir(primary, legacy); expect(result.copied).toEqual(["recent-corp.json"]); expect(result.deletedLegacyDir).toBe(false); expect(existsSync(join(primary, "recent-corp.json"))).toBe(true); expect(existsSync(legacy)).toBe(true); }); }); function makeTempDir(): string { const dir = mkdtempSync(join(tmpdir(), "seal-home-loader-")); mkdirSync(dir, { recursive: true }); tempDirs.push(dir); return dir; } function writeCorpConfig(path: string, id: string, name: string) { writeFileSync(path, JSON.stringify({ id, name, source: { type: "direct", token: "seal-token", sealUrl: `https://${id}.sealai.cc` } })); }