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

122 lines
3.9 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_ENV = process.env.SEAL_HOME_ENTERPRISES_DIR;
const ORIGINAL_USER_DIR_ENV = process.env.SEAL_HOME_USER_ENTERPRISES_DIR;
const ORIGINAL_CONFIG_FILE_ENV = process.env.SEAL_HOME_CONFIG_FILE;
const ORIGINAL_CWD = process.cwd();
const tempDirs: string[] = [];
afterEach(() => {
process.chdir(ORIGINAL_CWD);
if (ORIGINAL_ENV === undefined) {
delete process.env.SEAL_HOME_ENTERPRISES_DIR;
} else {
process.env.SEAL_HOME_ENTERPRISES_DIR = ORIGINAL_ENV;
}
if (ORIGINAL_USER_DIR_ENV === undefined) {
delete process.env.SEAL_HOME_USER_ENTERPRISES_DIR;
} else {
process.env.SEAL_HOME_USER_ENTERPRISES_DIR = ORIGINAL_USER_DIR_ENV;
}
if (ORIGINAL_CONFIG_FILE_ENV === undefined) {
delete process.env.SEAL_HOME_CONFIG_FILE;
} else {
process.env.SEAL_HOME_CONFIG_FILE = ORIGINAL_CONFIG_FILE_ENV;
}
for (const dir of tempDirs.splice(0)) {
rmSync(dir, { recursive: true, force: true });
}
});
describe("loadCorpConfigs", () => {
test("loads configs from 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"]);
});
test("returns no configs when explicit env directory does not exist", () => {
process.env.SEAL_HOME_ENTERPRISES_DIR = join(makeTempDir(), "missing");
expect(resolveEnterprisesDir()).toBeUndefined();
expect(loadCorpConfigs()).toEqual([]);
});
test("merges local and user enterprise directories with user configs taking precedence", () => {
const workspace = makeTempDir();
const workspaceEnterprises = join(workspace, "enterprises");
const userDir = makeTempDir();
mkdirSync(workspaceEnterprises, { recursive: true });
writeCorpConfig(join(workspaceEnterprises, "shared.json"), "shared", "Workspace");
writeCorpConfig(join(userDir, "shared.json"), "shared", "User");
writeCorpConfig(join(userDir, "user-only.json"), "user-only", "User Only");
process.chdir(workspace);
process.env.SEAL_HOME_USER_ENTERPRISES_DIR = userDir;
expect(loadCorpConfigs().map((corp) => [corp.id, corp.name])).toEqual([
["shared", "User"],
["user-only", "User Only"]
]);
});
test("defaults global config to stateless", () => {
process.env.SEAL_HOME_CONFIG_FILE = join(makeTempDir(), "config.json");
expect(loadGlobalConfig()).toEqual({ stateful: false });
});
test("saves and loads the global state mode", () => {
const file = join(makeTempDir(), "nested", "config.json");
process.env.SEAL_HOME_CONFIG_FILE = file;
expect(saveGlobalConfig({ stateful: true, defaultCorpId: "corp-a" })).toEqual({
stateful: true,
defaultCorpId: "corp-a"
});
expect(resolveGlobalConfigFile()).toBe(file);
expect(loadGlobalConfig()).toEqual({ stateful: true, defaultCorpId: "corp-a" });
});
});
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`
}
}));
}