Files
hose-use/src/core/config/env.test.ts
T
gitea af1e11c018 feat(corps): 企业配置增加环境属性与生产安全保护机制
- Schema 扩展: CorpConfigSchema 增加 env ('prod' | 'test' | 'demo') 与 isTest 字段,未知企业保守兜底为 prod
- 智能初判与归一化: inferCorpEnvFromName 依据名称初判环境,normalizeCorpConfig 确保配置持久化时环境对齐
- CLI 视图升级: corps list 与 search 增加「环境」列 (🏭 正式 / 🧪 测试 / 🎪 演示),支持 --test/--prod/--env 筛选
- 环境打标与迁移: 新增 hose-use corps tag 命令,支持单个企业标记与 --migrate-all 批量显式升级持久化
- 生产安全阻断: workflow test 等敏感调试在正式生产环境 (prod) 阻断执行,需显式附加 --force 确认生产风险
- 上下文与探针增强: context、probe、corps current 全面输出企业环境属性
- 测试与文档补全: 覆盖 489 项全量测试并通过 tsc 类型检查
2026-10-16 12:30:08 +08:00

135 lines
4.5 KiB
TypeScript

import { describe, expect, it } from "bun:test";
import {
CorpConfig,
CorpConfigSchema,
getCorpEnv,
inferCorpEnvFromName,
isCorpProd,
isCorpTest,
normalizeCorpConfig,
} from "./types.js";
describe("CorpConfig Environment Attributes & Deduction", () => {
it("inferCorpEnvFromName deduces environment conservatively based on name and id", () => {
// 包含“测试”或“test”
expect(inferCorpEnvFromName("HAB对公测试环境", "ID01uzxB4x07fh")).toBe("test");
expect(inferCorpEnvFromName("Finance Test Corp", "ID_TEST_01")).toBe("test");
expect(inferCorpEnvFromName(undefined, "test-corp-id")).toBe("test");
// 包含“演示”或“POC”或“demo”
expect(inferCorpEnvFromName("合思POC演示企业", "ID01demo")).toBe("demo");
expect(inferCorpEnvFromName("Demo Corporation", "ID01")).toBe("demo");
// 真实生产企业一律保守识别为正式环境 prod
expect(inferCorpEnvFromName("新转转集团", "ID01PH4Q5CJboX")).toBe("prod");
expect(inferCorpEnvFromName("美年大健康产业控股股份有限公司", "ID01MCcOgJl1td")).toBe("prod");
expect(inferCorpEnvFromName("北京某科技有限公司", "ID01abc")).toBe("prod");
expect(inferCorpEnvFromName("", "")).toBe("prod");
expect(inferCorpEnvFromName(undefined, undefined)).toBe("prod");
});
it("getCorpEnv respects explicit env, falls back to isTest, and finally infers", () => {
const explicitTest = {
id: "c1",
name: "普通企业名",
env: "test" as const,
credentials: { corpId: "c1", staffId: "s1", accessToken: "t1" },
};
expect(getCorpEnv(explicitTest as CorpConfig)).toBe("test");
const explicitProd = {
id: "c2",
name: "虽然名字叫测试但显式指定了正式",
env: "prod" as const,
credentials: { corpId: "c2", staffId: "s2", accessToken: "t2" },
};
expect(getCorpEnv(explicitProd as CorpConfig)).toBe("prod");
const legacyIsTest = {
id: "c3",
name: "旧企业无env",
isTest: true,
credentials: { corpId: "c3", staffId: "s3", accessToken: "t3" },
};
expect(getCorpEnv(legacyIsTest as CorpConfig)).toBe("test");
const legacyNoEnv = {
id: "c4",
name: "新转转集团",
credentials: { corpId: "c4", staffId: "s4", accessToken: "t4" },
};
expect(getCorpEnv(legacyNoEnv as CorpConfig)).toBe("prod");
});
it("isCorpTest and isCorpProd maintain consistent boundaries", () => {
const testCorp: CorpConfig = {
id: "t1",
name: "测试企业",
env: "test",
isTest: true,
baseUrl: "https://app.ekuaibao.com",
source: "manual",
credentials: { corpId: "t1", staffId: "s1", accessToken: "token" },
};
expect(isCorpTest(testCorp)).toBe(true);
expect(isCorpProd(testCorp)).toBe(false);
const demoCorp: CorpConfig = {
id: "d1",
name: "演示企业",
env: "demo",
isTest: true,
baseUrl: "https://app.ekuaibao.com",
source: "manual",
credentials: { corpId: "d1", staffId: "s1", accessToken: "token" },
};
expect(isCorpTest(demoCorp)).toBe(true);
expect(isCorpProd(demoCorp)).toBe(false);
const prodCorp: CorpConfig = {
id: "p1",
name: "新转转集团",
env: "prod",
isTest: false,
baseUrl: "https://app.ekuaibao.com",
source: "manual",
credentials: { corpId: "p1", staffId: "s1", accessToken: "token" },
};
expect(isCorpTest(prodCorp)).toBe(false);
expect(isCorpProd(prodCorp)).toBe(true);
});
it("normalizeCorpConfig injects env and isTest into legacy configs seamlessly", () => {
const legacyRaw = {
id: "ID01PH4Q5CJboX",
name: "新转转集团",
credentials: {
accessToken: "token_123",
corpId: "ID01PH4Q5CJboX",
staffId: "s_123",
},
};
const parsed = CorpConfigSchema.parse(legacyRaw);
const normalized = normalizeCorpConfig(parsed);
expect(normalized.env).toBe("prod");
expect(normalized.isTest).toBe(false);
expect(isCorpProd(normalized)).toBe(true);
const legacyTestRaw = {
id: "ID01uzxB4x07fh",
name: "HAB对公测试环境",
credentials: {
accessToken: "token_test",
corpId: "ID01uzxB4x07fh",
staffId: "s_test",
},
};
const normalizedTest = normalizeCorpConfig(CorpConfigSchema.parse(legacyTestRaw));
expect(normalizedTest.env).toBe("test");
expect(normalizedTest.isTest).toBe(true);
expect(isCorpTest(normalizedTest)).toBe(true);
});
});