Files
hose-use/src/cli/commands/corps-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

200 lines
5.8 KiB
TypeScript

import { describe, expect, it } from "bun:test";
import { handleCorpsCommand } from "./corps/index.js";
import { handleContextCommand } from "./context.js";
import { CorpContext } from "../../core/config/manager.js";
import type { CorpConfig } from "../../core/config/types.js";
import type { CliRuntime } from "../types.js";
const sampleCorps: CorpConfig[] = [
{
id: "corp_prod",
name: "新转转集团",
env: "prod",
isTest: false,
baseUrl: "https://app.ekuaibao.com",
source: "manual",
credentials: { corpId: "corp_prod", staffId: "staff_1", accessToken: "t1" },
},
{
id: "corp_test",
name: "HAB对公测试环境",
env: "test",
isTest: true,
baseUrl: "https://app.ekuaibao.com",
source: "manual",
credentials: { corpId: "corp_test", staffId: "staff_2", accessToken: "t2" },
},
{
id: "corp_demo",
name: "营销中心演示企业",
env: "demo",
isTest: true,
baseUrl: "https://app.ekuaibao.com",
source: "manual",
credentials: { corpId: "corp_demo", staffId: "staff_3", accessToken: "t3" },
},
];
function createMockRuntime(corps: CorpConfig[] = sampleCorps): CliRuntime {
const context = new CorpContext(corps, { stateful: true, defaultCorpId: "corp_prod" });
return {
corpContext: context,
selectedCorp: context.getCurrent()!,
jsonMode: false,
outputOptions: {},
};
}
describe("CLI - corps environment attributes & filtering", () => {
it("corps list displays environment column with emoji labels", async () => {
const runtime = createMockRuntime();
const res = await handleCorpsCommand(
{
area: "corps",
action: "list",
args: [],
options: {},
raw: ["corps", "list"],
},
runtime
);
expect(res.exitCode).toBe(0);
expect(res.stdout).toContain("环境");
expect(res.stdout).toContain("🏭 正式");
expect(res.stdout).toContain("🧪 测试");
expect(res.stdout).toContain("🎪 演示");
expect(res.stdout).toContain("共 3 家企业 (🏭 正式 1 家, 🧪 测试 1 家, 🎪 演示 1 家)");
});
it("corps list --test filters only test and demo enterprises", async () => {
const runtime = createMockRuntime();
const res = await handleCorpsCommand(
{
area: "corps",
action: "list",
args: [],
options: { test: true },
raw: ["corps", "list", "--test"],
},
runtime
);
expect(res.exitCode).toBe(0);
expect(res.stdout).toContain("HAB对公测试环境");
expect(res.stdout).toContain("营销中心演示企业");
expect(res.stdout).not.toContain("新转转集团");
expect(res.stdout).toContain("已筛选显示 2 家");
});
it("corps list --prod filters only production enterprises", async () => {
const runtime = createMockRuntime();
const res = await handleCorpsCommand(
{
area: "corps",
action: "list",
args: [],
options: { prod: true },
raw: ["corps", "list", "--prod"],
},
runtime
);
expect(res.exitCode).toBe(0);
expect(res.stdout).toContain("新转转集团");
expect(res.stdout).not.toContain("HAB对公测试环境");
expect(res.stdout).not.toContain("营销中心演示企业");
expect(res.stdout).toContain("已筛选显示 1 家");
});
it("corps list --env=demo filters precisely demo enterprises", async () => {
const runtime = createMockRuntime();
const res = await handleCorpsCommand(
{
area: "corps",
action: "list",
args: [],
options: { env: "demo" },
raw: ["corps", "list", "--env=demo"],
},
runtime
);
expect(res.exitCode).toBe(0);
expect(res.stdout).toContain("营销中心演示企业");
expect(res.stdout).not.toContain("新转转集团");
expect(res.stdout).not.toContain("HAB对公测试环境");
});
it("corps tag requires mutation confirmation unless confirmed or dry-run", async () => {
const runtime = createMockRuntime();
await expect(
handleCorpsCommand(
{
area: "corps",
action: "tag",
args: ["corp_prod"],
options: { test: true },
raw: ["corps", "tag", "corp_prod", "--test"],
},
runtime
)
).rejects.toThrow("拒绝执行设置企业环境属性");
});
it("corps tag previews change under --dry-run", async () => {
const runtime = createMockRuntime();
const res = await handleCorpsCommand(
{
area: "corps",
action: "tag",
args: ["corp_prod"],
options: { test: true, "dry-run": true },
raw: ["corps", "tag", "corp_prod", "--test", "--dry-run"],
},
runtime
);
expect(res.exitCode).toBe(0);
expect(res.stdout).toContain("[DRY-RUN]");
expect(res.stdout).toContain("设置企业环境属性");
expect(res.stdout).toContain("newEnv: test");
});
it("corps current displays environment attribute label", async () => {
const runtime = createMockRuntime();
const res = await handleCorpsCommand(
{
area: "corps",
action: "current",
args: [],
options: {},
raw: ["corps", "current"],
},
runtime
);
expect(res.exitCode).toBe(0);
expect(res.stdout).toContain("环境属性");
expect(res.stdout).toContain("🏭 正式");
});
it("context command reports environment counts and current corp env tag", async () => {
const runtime = createMockRuntime();
const res = await handleContextCommand(
{
area: "context",
action: "",
args: [],
options: {},
raw: ["context"],
},
runtime
);
expect(res.exitCode).toBe(0);
expect(res.stdout).toContain("新转转集团 (corp_prod) [🏭 正式]");
expect(res.stdout).toContain("🏭 正式: 1 家, 🧪 测试: 1 家, 🎪 演示: 1 家");
});
});