import { describe, expect, it, spyOn, afterEach } from "bun:test"; import { handlePlatformCommand } from "./platform/index.js"; import * as api from "../../domains/datalink/api.js"; import * as services from "../../domains/datalink/services.js"; import * as helpers from "../helpers.js"; import type { CliRuntime } from "../types.js"; import type { CorpConfig } from "../../core/config/types.js"; describe("CLI - platform 命令与 record 动作", () => { let spies: ReturnType[] = []; afterEach(() => { for (const spy of spies) { spy.mockRestore(); } spies = []; }); const mockCorp: CorpConfig = { id: "corp-1", name: "测试企业", source: "account", baseUrl: "https://app.ekuaibao.com", credentials: { accessToken: "mock-token", corpId: "corp-1", staffId: "staff-1", }, }; const dummyRuntime: CliRuntime = { corpContext: { listCorps: () => [mockCorp], getCurrent: () => mockCorp, switchCorp: () => true, resolve: () => mockCorp, } as any, selectedCorp: mockCorp, jsonMode: false, }; const mockPlatform = { id: "platform-1", name: "项目台账", code: "PJ", active: true, adminIds: ["staff-1"], staffIds: [], roleIds: [], departmentIds: [], departmentsIncludeChildren: false, }; const mockEntity = { id: "entity-1", name: "项目", code: "PROJECT", type: "DIMENSION", active: true, fields: [ { name: "code", label: "编码", type: "text" }, { name: "name", label: "名称", type: "text" }, { name: "customField", label: "自定义字段", type: "text" }, ], actions: [], importMethod: [], }; it("未选中企业时报错并返回 exitCode 1", async () => { const res = await handlePlatformCommand( { area: "platform", action: "list", args: [], options: {}, raw: [] }, { ...dummyRuntime, selectedCorp: null } ); expect(res.exitCode).toBe(1); expect(res.stderr).toContain("未选中企业"); }); it("records 正确解析嵌套 dataLink 的停启用状态和更新时间", async () => { spies.push(spyOn(helpers, "createClientsForCorp").mockResolvedValue({ ekbClient: {} } as any)); spies.push(spyOn(services, "resolvePlatform").mockResolvedValue(mockPlatform as any)); spies.push(spyOn(services, "resolveEntity").mockResolvedValue(mockEntity as any)); spies.push(spyOn(api, "searchEntityRecords").mockResolvedValue({ total: 2, data: [ { id: "r-1", dataLink: { id: "r-1", name: "项目1", code: "P1", active: false, updateTime: "2026-09-09" }, }, { id: "r-2", dataLink: { id: "r-2", name: "项目2", code: "P2", active: true, updateTime: "2026-09-09" }, }, ], actions: [], components: [], path: null, template: null, })); const res = await handlePlatformCommand( { area: "platform", action: "records", args: ["platform-1", "entity-1"], options: {}, raw: [] }, dummyRuntime ); expect(res.exitCode).toBe(0); expect(res.stdout).toContain("停用"); expect(res.stdout).toContain("启用"); }); it("record get 获取记录详情", async () => { spies.push(spyOn(helpers, "createClientsForCorp").mockResolvedValue({ ekbClient: {} } as any)); spies.push(spyOn(services, "resolvePlatform").mockResolvedValue(mockPlatform as any)); spies.push(spyOn(services, "resolveEntity").mockResolvedValue(mockEntity as any)); spies.push(spyOn(api, "getRecordDetail").mockResolvedValue({ dataLink: { id: "r-1", name: "测试项目", code: "P-100", active: true, createTime: 12345678, updateTime: 12345679, form: { customField: "测试值" }, }, })); const res = await handlePlatformCommand( { area: "platform", action: "record", args: ["get", "platform-1", "entity-1", "r-1"], options: {}, raw: [] }, dummyRuntime ); expect(res.exitCode).toBe(0); expect(res.stdout).toContain("测试项目"); expect(res.stdout).toContain("P-100"); expect(res.stdout).toContain("测试值"); }); it("record toggle 切换停启用状态", async () => { spies.push(spyOn(helpers, "createClientsForCorp").mockResolvedValue({ ekbClient: {} } as any)); spies.push(spyOn(services, "resolvePlatform").mockResolvedValue(mockPlatform as any)); spies.push(spyOn(services, "resolveEntity").mockResolvedValue(mockEntity as any)); const toggleSpy = spyOn(api, "toggleRecordState").mockResolvedValue({ recordId: "r-1", active: true }); spies.push(toggleSpy); const res = await handlePlatformCommand( { area: "platform", action: "record", args: ["toggle", "platform-1", "entity-1", "r-1"], options: { yes: true }, raw: ["platform", "record", "toggle", "platform-1", "entity-1", "r-1", "--yes"] }, dummyRuntime ); expect(res.exitCode).toBe(0); expect(res.stdout).toContain("状态已成功切换为"); expect(toggleSpy).toHaveBeenCalledWith(expect.anything(), "corp-1", "r-1", undefined); }); it("record edit 编辑字段", async () => { spies.push(spyOn(helpers, "createClientsForCorp").mockResolvedValue({ ekbClient: {} } as any)); spies.push(spyOn(services, "resolvePlatform").mockResolvedValue(mockPlatform as any)); spies.push(spyOn(services, "resolveEntity").mockResolvedValue(mockEntity as any)); const editSpy = spyOn(api, "editRecord").mockResolvedValue({ id: "r-1" } as any); spies.push(editSpy); const res = await handlePlatformCommand( { area: "platform", action: "record", args: ["edit", "platform-1", "entity-1", "r-1"], options: { data: '{"name":"更新后的名称"}', yes: true }, raw: ["platform", "record", "edit", "platform-1", "entity-1", "r-1", "--yes"], }, dummyRuntime ); expect(res.exitCode).toBe(0); expect(res.stdout).toContain("字段更新成功"); expect(editSpy).toHaveBeenCalledWith( expect.anything(), "corp-1", expect.objectContaining({ dataLinkEntityId: "entity-1", dataLinkId: "r-1", form: { name: "更新后的名称" }, }) ); }); it("record recompute 触发台账重算", async () => { spies.push(spyOn(helpers, "createClientsForCorp").mockResolvedValue({ ekbClient: {} } as any)); spies.push(spyOn(services, "resolvePlatform").mockResolvedValue(mockPlatform as any)); spies.push(spyOn(services, "resolveEntity").mockResolvedValue(mockEntity as any)); const recomputeSpy = spyOn(api, "recomputeRecordLedger").mockResolvedValue({ id: "ok" }); spies.push(recomputeSpy); const res = await handlePlatformCommand( { area: "platform", action: "record", args: ["recompute", "platform-1", "entity-1", "r-1"], options: { yes: true }, raw: ["platform", "record", "recompute", "platform-1", "entity-1", "r-1", "--yes"] }, dummyRuntime ); expect(res.exitCode).toBe(0); expect(res.stdout).toContain("台账重算任务已成功触发"); expect(recomputeSpy).toHaveBeenCalledWith(expect.anything(), "corp-1", "r-1"); }); it("record get 支持仅传 免传应用与业务对象", async () => { spies.push(spyOn(helpers, "createClientsForCorp").mockResolvedValue({ ekbClient: {} } as any)); spies.push( spyOn(api, "getRecordDetail").mockResolvedValue({ dataLink: { id: "ID01DirectRecord", name: "直查记录", code: "DR-01", entityId: "entity-1", active: true, }, }) ); spies.push(spyOn(api, "listPlatforms").mockResolvedValue([mockPlatform] as any)); spies.push(spyOn(api, "listPlatformEntities").mockResolvedValue([mockEntity] as any)); const res = await handlePlatformCommand( { area: "platform", action: "record", args: ["get", "ID01DirectRecord"], options: {}, raw: [] }, dummyRuntime ); expect(res.exitCode).toBe(0); expect(res.stdout).toContain("直查记录"); expect(res.stdout).toContain("DR-01"); }); it("record batch-edit --dry-run 生成安全合并预览且不发送真实写请求", async () => { spies.push(spyOn(helpers, "createClientsForCorp").mockResolvedValue({ ekbClient: {} } as any)); spies.push(spyOn(services, "resolvePlatform").mockResolvedValue(mockPlatform as any)); spies.push(spyOn(services, "resolveEntity").mockResolvedValue(mockEntity as any)); spies.push( spyOn(api, "getRecordDetail").mockResolvedValue({ dataLink: { id: "r-1", active: true, name: "旧名称", code: "P-1", }, }) ); const editSpy = spyOn(api, "editRecord").mockResolvedValue({ id: "r-1" } as any); spies.push(editSpy); const res = await handlePlatformCommand( { area: "platform", action: "record", args: ["batch-edit", "platform-1", "entity-1"], options: { ids: "r-1", data: '{"名称":"新名称"}', "dry-run": true }, raw: ["platform", "record", "batch-edit", "platform-1", "entity-1", "--ids=r-1", "--dry-run"], }, dummyRuntime ); expect(res.exitCode).toBe(0); expect(res.stdout).toContain("Dry-Run 预览"); expect(editSpy).not.toHaveBeenCalled(); }); it("platform records 支持 --sum 数学核算与 --group-by 分组统计", async () => { spies.push(spyOn(helpers, "createClientsForCorp").mockResolvedValue({ ekbClient: {} } as any)); spies.push(spyOn(services, "resolvePlatform").mockResolvedValue(mockPlatform as any)); spies.push(spyOn(services, "resolveEntity").mockResolvedValue({ ...mockEntity, fields: [ { name: "fee", label: "租赁费用", type: "money" }, { name: "status", label: "状态", type: "text" }, ], } as any)); spies.push( spyOn(services, "fetchAllEntityRecords").mockResolvedValue({ data: [ { id: "1", fee: 1000, status: "A" }, { id: "2", fee: 2000, status: "A" }, { id: "3", fee: 3000, status: "B" }, ], total: 3, actions: [], components: [], path: null, template: null, }) ); const res = await handlePlatformCommand( { area: "platform", action: "records", args: ["platform-1", "entity-1"], options: { sum: "租赁费用", "group-by": "状态" }, raw: ["platform", "records", "platform-1", "entity-1", "--sum=租赁费用", "--group-by=状态"], }, dummyRuntime ); expect(res.exitCode).toBe(0); expect(res.stdout).toContain("台账数学聚合核算"); expect(res.stdout).toContain("总合计 (SUM)"); expect(res.stdout).toContain("6000"); expect(res.stdout).toContain("按 [状态] 分组统计"); }); it("platform audit 支持 --check-continuous 与 --expect 基线离群诊断", async () => { spies.push(spyOn(helpers, "createClientsForCorp").mockResolvedValue({ ekbClient: {} } as any)); spies.push(spyOn(services, "resolvePlatform").mockResolvedValue(mockPlatform as any)); spies.push(spyOn(services, "resolveEntity").mockResolvedValue({ ...mockEntity, fields: [ { name: "seq", label: "摊销序号", type: "number" }, { name: "fee", label: "租赁费用", type: "money" }, ], } as any)); spies.push( spyOn(services, "fetchAllEntityRecords").mockResolvedValue({ data: [ { id: "1", seq: 1, fee: 78787.55 }, { id: "2", seq: 3, fee: 0 }, ], total: 2, actions: [], components: [], path: null, template: null, }) ); const res = await handlePlatformCommand( { area: "platform", action: "audit", args: ["platform-1", "entity-1"], options: { "check-continuous": "摊销序号", expect: "租赁费用 ~= 78787.55" }, raw: ["platform", "audit", "platform-1", "entity-1", "--check-continuous=摊销序号", "--expect=租赁费用 ~= 78787.55"], }, dummyRuntime ); expect(res.exitCode).toBe(0); expect(res.stdout).toContain("台账序号连续性体检"); expect(res.stdout).toContain("断层缺失期数 (MISSING)"); expect(res.stdout).toContain("缺少 [2]"); expect(res.stdout).toContain("基线离群异常核对"); expect(res.stdout).toContain("偏离差额"); }); it("platform update 支持 --where 声明式筛选与 --set 原地修改(含 --dry-run 与 --yes)", async () => { spies.push(spyOn(helpers, "createClientsForCorp").mockResolvedValue({ ekbClient: {} } as any)); spies.push(spyOn(services, "resolvePlatform").mockResolvedValue(mockPlatform as any)); spies.push(spyOn(services, "resolveEntity").mockResolvedValue({ ...mockEntity, fields: [ { name: "fee", label: "租赁费用", type: "money" }, { name: "status", label: "状态", type: "text" }, ], } as any)); spies.push( spyOn(services, "fetchAllEntityRecords").mockResolvedValue({ data: [ { id: "r-1", fee: 0, status: "PENDING" }, { id: "r-2", fee: 78787.55, status: "DONE" }, ], total: 2, actions: [], components: [], path: null, template: null, }) ); spies.push( spyOn(api, "getRecordDetail").mockResolvedValue({ dataLink: { id: "r-1", active: true, fee: 0, status: "PENDING" }, }) ); const editSpy = spyOn(api, "editRecord").mockResolvedValue({ id: "r-1" } as any); spies.push(editSpy); // 1. Dry-run 模式测试 const dryRes = await handlePlatformCommand( { area: "platform", action: "update", args: ["platform-1", "entity-1"], options: { where: "租赁费用 == 0", set: "租赁费用 = 78787.55", "dry-run": true }, raw: ["platform", "update", "platform-1", "entity-1", "--where=租赁费用 == 0", "--set=租赁费用 = 78787.55", "--dry-run"], }, dummyRuntime ); expect(dryRes.exitCode).toBe(0); expect(dryRes.stdout).toContain("Dry-Run 预览"); expect(dryRes.stdout).toContain("r-1"); expect(editSpy).not.toHaveBeenCalled(); // 2. 真实确认执行测试 (--yes) const execRes = await handlePlatformCommand( { area: "platform", action: "update", args: ["platform-1", "entity-1"], options: { where: "租赁费用 == 0", set: "租赁费用 = 78787.55", yes: true }, raw: ["platform", "update", "platform-1", "entity-1", "--where=租赁费用 == 0", "--set=租赁费用 = 78787.55", "--yes"], }, dummyRuntime ); expect(execRes.exitCode).toBe(0); expect(execRes.stdout).toContain("已执行"); expect(editSpy).toHaveBeenCalled(); }); it("platform records 支持 --stats 画像统计(计算状态、重名与编码聚类)", async () => { spies.push(spyOn(helpers, "createClientsForCorp").mockResolvedValue({ ekbClient: {} } as any)); spies.push(spyOn(services, "resolvePlatform").mockResolvedValue(mockPlatform as any)); spies.push(spyOn(services, "resolveEntity").mockResolvedValue(mockEntity as any)); spies.push( spyOn(api, "searchEntityRecords").mockResolvedValue({ total: 3, data: [ { id: "r-1", dataLink: { id: "r-1", name: "场地A", code: "0-DT-2026-month-1", active: true } }, { id: "r-2", dataLink: { id: "r-2", name: "场地A", code: "0-DT-2026-period-1", active: false } }, { id: "r-3", dataLink: { id: "r-3", name: "场地B", code: "0-DT-2026-month-2", active: true } }, ], actions: [], components: [], path: null, template: null, }) ); // 1. 命令行表格输出测试 const textRes = await handlePlatformCommand( { area: "platform", action: "records", args: ["platform-1", "entity-1"], options: { stats: true }, raw: ["platform", "records", "platform-1", "entity-1", "--stats"], }, dummyRuntime ); expect(textRes.exitCode).toBe(0); expect(textRes.stdout).toContain("宏观状态与数据画像"); expect(textRes.stdout).toContain("启用 2 条"); expect(textRes.stdout).toContain("停用 1 条"); expect(textRes.stdout).toContain("N-DT-N-month-N"); // 2. JSON 结构化输出测试 const jsonRes = await handlePlatformCommand( { area: "platform", action: "records", args: ["platform-1", "entity-1"], options: { stats: true }, raw: ["platform", "records", "platform-1", "entity-1", "--stats"], }, { ...dummyRuntime, jsonMode: true } ); expect(jsonRes.exitCode).toBe(0); const parsed = JSON.parse(jsonRes.stdout); expect(parsed.total).toBe(3); expect(parsed.activeCount).toBe(2); expect(parsed.inactiveCount).toBe(1); expect(parsed.duplicateNamesCount).toBe(1); }); it("platform records 支持 --ndjson 流式逐行下发与 --flat 归一化", async () => { spies.push(spyOn(helpers, "createClientsForCorp").mockResolvedValue({ ekbClient: {} } as any)); spies.push(spyOn(services, "resolvePlatform").mockResolvedValue(mockPlatform as any)); spies.push(spyOn(services, "resolveEntity").mockResolvedValue(mockEntity as any)); spies.push( spyOn(api, "searchEntityRecords").mockResolvedValue({ total: 2, data: [ { id: "r-1", dataLink: { id: "r-1", name: "项目1", code: "P1", active: true, customField: "测试值" }, }, { id: "r-2", dataLink: { id: "r-2", name: "项目2", code: "P2", active: false, customField: "停用值" }, }, ], actions: [], components: [], path: null, template: null, }) ); const streamedLines: string[] = []; const writeSpy = spyOn(process.stdout, "write").mockImplementation((str: any) => { streamedLines.push(String(str)); return true; }); spies.push(writeSpy); const streamRes = await handlePlatformCommand( { area: "platform", action: "records", args: ["platform-1", "entity-1"], options: { ndjson: true }, raw: ["platform", "records", "platform-1", "entity-1", "--ndjson"], }, dummyRuntime ); expect(streamRes.exitCode).toBe(0); expect(streamedLines.length).toBe(2); const row1 = JSON.parse(streamedLines[0].trim()); expect(row1.id).toBe("r-1"); expect(row1.name).toBe("项目1"); expect(row1.code).toBe("P1"); expect(row1.active).toBe(true); expect(row1.fields["自定义字段"]).toBe("测试值"); }); });