144 lines
4.8 KiB
TypeScript
144 lines
4.8 KiB
TypeScript
import { afterEach, beforeEach, describe, expect, test } from "bun:test";
|
|
import { mkdtempSync, mkdirSync, rmSync, writeFileSync, existsSync, readFileSync } from "node:fs";
|
|
import { tmpdir } from "node:os";
|
|
import { join } from "node:path";
|
|
import {
|
|
loadCorpConfigs,
|
|
saveCorpConfig,
|
|
resolveEnterprisesDirs,
|
|
resolveWritableEnterprisesDir,
|
|
loadGlobalConfig,
|
|
saveGlobalConfig,
|
|
resolveGlobalConfigFile,
|
|
} from "./loader.js";
|
|
import type { CorpConfig } from "./types.js";
|
|
|
|
describe("core/config/loader", () => {
|
|
let tempDir: string;
|
|
const originalEnv = { ...process.env };
|
|
|
|
beforeEach(() => {
|
|
tempDir = mkdtempSync(join(tmpdir(), "hose-config-test-"));
|
|
});
|
|
|
|
afterEach(() => {
|
|
process.env = { ...originalEnv };
|
|
try {
|
|
rmSync(tempDir, { recursive: true, force: true });
|
|
} catch {
|
|
// ignore
|
|
}
|
|
});
|
|
|
|
test("resolveEnterprisesDirs prioritizes HOSE_ENTERPRISES_DIR environment variable", () => {
|
|
const customDir = join(tempDir, "custom-enterprises");
|
|
mkdirSync(customDir, { recursive: true });
|
|
process.env.HOSE_ENTERPRISES_DIR = customDir;
|
|
|
|
const dirs = resolveEnterprisesDirs();
|
|
expect(dirs).toEqual([customDir]);
|
|
});
|
|
|
|
test("resolveWritableEnterprisesDir respects HOSE_ENTERPRISES_DIR", () => {
|
|
const customDir = join(tempDir, "writable-enterprises");
|
|
mkdirSync(customDir, { recursive: true });
|
|
process.env.HOSE_ENTERPRISES_DIR = customDir;
|
|
|
|
expect(resolveWritableEnterprisesDir()).toBe(customDir);
|
|
});
|
|
|
|
const createMockCorp = (id: string, name: string, overrides: Partial<CorpConfig> = {}): CorpConfig => ({
|
|
id,
|
|
name,
|
|
baseUrl: "https://app.ekuaibao.com",
|
|
source: "manual",
|
|
credentials: {
|
|
accessToken: `token-${id}`,
|
|
corpId: id,
|
|
staffId: `staff-${id}`,
|
|
},
|
|
...overrides,
|
|
});
|
|
|
|
test("loadCorpConfigs loads valid configs and skips example files", () => {
|
|
const dirA = join(tempDir, "dirA");
|
|
mkdirSync(dirA, { recursive: true });
|
|
|
|
const validCorp = createMockCorp("corp-1", "企业1");
|
|
|
|
// Save valid corp
|
|
writeFileSync(join(dirA, "corp-1.json"), JSON.stringify(validCorp));
|
|
// Save example corp
|
|
writeFileSync(
|
|
join(dirA, "example.json"),
|
|
JSON.stringify(createMockCorp("example-corp", "模板企业"))
|
|
);
|
|
|
|
const loaded = loadCorpConfigs([dirA]);
|
|
expect(loaded).toHaveLength(1);
|
|
expect(loaded[0]?.id).toBe("corp-1");
|
|
expect(loaded[0]?.name).toBe("企业1");
|
|
});
|
|
|
|
test("loadCorpConfigs merges multiple directories with earlier directory taking precedence", () => {
|
|
const dirA = join(tempDir, "dirA");
|
|
const dirB = join(tempDir, "dirB");
|
|
mkdirSync(dirA, { recursive: true });
|
|
mkdirSync(dirB, { recursive: true });
|
|
|
|
const corpInA = createMockCorp("corp-common", "本地优先企业", {
|
|
credentials: { accessToken: "token-a", corpId: "corp-common", staffId: "staff-a" },
|
|
});
|
|
|
|
const corpInB = createMockCorp("corp-common", "全局覆盖企业", {
|
|
credentials: { accessToken: "token-b", corpId: "corp-common", staffId: "staff-b" },
|
|
});
|
|
|
|
const corpOnlyB = createMockCorp("corp-b-only", "仅在B的企业", {
|
|
credentials: { accessToken: "token-b2", corpId: "corp-b-only", staffId: "staff-b2" },
|
|
});
|
|
|
|
writeFileSync(join(dirA, "corp-common.json"), JSON.stringify(corpInA));
|
|
writeFileSync(join(dirB, "corp-common.json"), JSON.stringify(corpInB));
|
|
writeFileSync(join(dirB, "corp-b-only.json"), JSON.stringify(corpOnlyB));
|
|
|
|
const loaded = loadCorpConfigs([dirA, dirB]);
|
|
expect(loaded).toHaveLength(2);
|
|
|
|
const common = loaded.find((c) => c.id === "corp-common");
|
|
expect(common?.name).toBe("本地优先企业");
|
|
|
|
const bOnly = loaded.find((c) => c.id === "corp-b-only");
|
|
expect(bOnly?.name).toBe("仅在B的企业");
|
|
});
|
|
|
|
test("saveCorpConfig writes validated JSON to target directory", () => {
|
|
const targetDir = join(tempDir, "save-target");
|
|
const corp = createMockCorp("corp-save-test", "保存测试企业", {
|
|
credentials: { accessToken: "tok-123", corpId: "corp-save-test", staffId: "stf-1" },
|
|
});
|
|
|
|
const filePath = saveCorpConfig(corp, targetDir);
|
|
expect(existsSync(filePath)).toBe(true);
|
|
|
|
const saved = JSON.parse(readFileSync(filePath, "utf-8"));
|
|
expect(saved.id).toBe("corp-save-test");
|
|
expect(saved.credentials.accessToken).toBe("tok-123");
|
|
});
|
|
|
|
test("loadGlobalConfig and saveGlobalConfig respect HOSE_CONFIG_FILE", () => {
|
|
const customConfigFile = join(tempDir, "config.json");
|
|
process.env.HOSE_CONFIG_FILE = customConfigFile;
|
|
|
|
expect(resolveGlobalConfigFile()).toBe(customConfigFile);
|
|
|
|
const initial = loadGlobalConfig();
|
|
expect(initial.stateful).toBe(true);
|
|
|
|
saveGlobalConfig({ stateful: false, defaultCorpId: "test-corp-123" });
|
|
const updated = loadGlobalConfig();
|
|
expect(updated.stateful).toBe(false);
|
|
expect(updated.defaultCorpId).toBe("test-corp-123");
|
|
});
|
|
});
|