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 家"); }); });