244 lines
9.1 KiB
TypeScript
244 lines
9.1 KiB
TypeScript
import { afterEach, beforeEach, describe, expect, test } from "bun:test";
|
|
import { mkdtempSync, mkdirSync, rmSync, writeFileSync, existsSync, readFileSync, statSync, utimesSync } from "node:fs";
|
|
import { tmpdir } from "node:os";
|
|
import { join } from "node:path";
|
|
import {
|
|
loadCorpConfigs,
|
|
saveCorpConfig,
|
|
resolveEnterprisesDirs,
|
|
resolveWritableEnterprisesDir,
|
|
getEnterprisesDirCandidates,
|
|
loadGlobalConfig,
|
|
saveGlobalConfig,
|
|
resolveGlobalConfigFile,
|
|
syncLegacyEnterprisesDir,
|
|
syncLegacyGlobalConfig,
|
|
} from "./loader.js";
|
|
import { saveAccount, loadAccountStore, resolveAccountCachePath } from "../auth/account-store.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("resolveEnterprisesDirs does not include relative project enterprises dir", () => {
|
|
delete process.env.HOSE_ENTERPRISES_DIR;
|
|
delete process.env.HOSE_USER_ENTERPRISES_DIR;
|
|
const customConfigFile = join(tempDir, "config.json");
|
|
process.env.HOSE_CONFIG_FILE = customConfigFile;
|
|
|
|
const dirs = resolveEnterprisesDirs();
|
|
// Must never contain relative "enterprises"
|
|
expect(dirs).not.toContain("enterprises");
|
|
});
|
|
|
|
test("resolveEnterprisesDirs prioritizes testEnterprisesDir from global config", () => {
|
|
delete process.env.HOSE_ENTERPRISES_DIR;
|
|
delete process.env.HOSE_USER_ENTERPRISES_DIR;
|
|
const customConfigFile = join(tempDir, "config.json");
|
|
process.env.HOSE_CONFIG_FILE = customConfigFile;
|
|
|
|
const customTestDir = join(tempDir, "my-test-enterprises");
|
|
mkdirSync(customTestDir, { recursive: true });
|
|
|
|
saveGlobalConfig({ testEnterprisesDir: customTestDir });
|
|
const dirs = resolveEnterprisesDirs();
|
|
expect(dirs[0]).toBe(customTestDir);
|
|
expect(resolveWritableEnterprisesDir()).toBe(customTestDir);
|
|
|
|
const candidates = getEnterprisesDirCandidates();
|
|
expect(candidates).toContain(`testEnterprisesDir: ${customTestDir}`);
|
|
});
|
|
|
|
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");
|
|
});
|
|
|
|
test("syncLegacyEnterprisesDir copies legacy credentials and removes stale legacy directory", () => {
|
|
const primary = join(tempDir, "primary");
|
|
const legacy = join(tempDir, "legacy");
|
|
mkdirSync(legacy, { recursive: true });
|
|
writeFileSync(join(legacy, "corp-old.json"), JSON.stringify(createMockCorp("corp-old", "旧企业")));
|
|
const old = new Date(Date.now() - 10 * 24 * 60 * 60 * 1000);
|
|
utimesSync(join(legacy, "corp-old.json"), old, old);
|
|
utimesSync(legacy, old, old);
|
|
|
|
const result = syncLegacyEnterprisesDir(primary, legacy);
|
|
expect(result.copied).toEqual(["corp-old.json"]);
|
|
expect(result.deletedLegacyDir).toBe(true);
|
|
expect(existsSync(join(primary, "corp-old.json"))).toBe(true);
|
|
expect(existsSync(legacy)).toBe(false);
|
|
});
|
|
|
|
test("syncLegacyEnterprisesDir preserves a recently modified legacy directory", () => {
|
|
const primary = join(tempDir, "primary-recent");
|
|
const legacy = join(tempDir, "legacy-recent");
|
|
mkdirSync(legacy, { recursive: true });
|
|
writeFileSync(join(legacy, "corp-recent.json"), JSON.stringify(createMockCorp("corp-recent", "近期企业")));
|
|
|
|
const result = syncLegacyEnterprisesDir(primary, legacy);
|
|
expect(result.deletedLegacyDir).toBe(false);
|
|
expect(existsSync(legacy)).toBe(true);
|
|
});
|
|
|
|
test("syncLegacyGlobalConfig migrates and removes a stale legacy config", () => {
|
|
const primary = join(tempDir, "config", "config.json");
|
|
const legacy = join(tempDir, "legacy-config.json");
|
|
writeFileSync(legacy, JSON.stringify({ stateful: false }));
|
|
const old = new Date(Date.now() - 10 * 24 * 60 * 60 * 1000);
|
|
utimesSync(legacy, old, old);
|
|
|
|
const result = syncLegacyGlobalConfig(primary, legacy);
|
|
expect(result.copied).toBe(true);
|
|
expect(result.deletedLegacyFile).toBe(true);
|
|
expect(JSON.parse(readFileSync(primary, "utf8")).stateful).toBe(false);
|
|
expect(existsSync(legacy)).toBe(false);
|
|
});
|
|
|
|
test("saveCorpConfig and saveAccount enforce 0o600 file permissions and respect HOSE_ACCOUNTS_FILE", () => {
|
|
const targetDir = join(tempDir, "secure-corps");
|
|
const corpFile = saveCorpConfig(createMockCorp("sec-1", "安全测试企业"), targetDir);
|
|
if (process.platform !== "win32") {
|
|
expect(statSync(corpFile).mode & 0o777).toBe(0o600);
|
|
}
|
|
|
|
const customAccountsFile = join(tempDir, "secure-accounts", "accounts.json");
|
|
process.env.HOSE_ACCOUNTS_FILE = customAccountsFile;
|
|
expect(resolveAccountCachePath()).toBe(customAccountsFile);
|
|
|
|
saveAccount({
|
|
accountId: "86-13800138000",
|
|
cloudAuthToken: "secret-cloud-token",
|
|
createdAt: Date.now(),
|
|
updatedAt: Date.now(),
|
|
corporations: [],
|
|
});
|
|
|
|
expect(existsSync(customAccountsFile)).toBe(true);
|
|
if (process.platform !== "win32") {
|
|
expect(statSync(customAccountsFile).mode & 0o777).toBe(0o600);
|
|
}
|
|
expect(loadAccountStore().accounts["86-13800138000"]?.cloudAuthToken).toBe("secret-cloud-token");
|
|
});
|
|
});
|