Files
seal-use/src/core/config/loader.test.ts
T

176 lines
6.1 KiB
TypeScript

import { afterEach, describe, expect, test } from "bun:test";
import { mkdtempSync, mkdirSync, rmSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import {
loadCorpConfigs,
loadGlobalConfig,
resolveEnterprisesDir,
resolveGlobalConfigFile,
saveGlobalConfig
} 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
}
});
});
});
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`
}
}));
}