Files
hose-use/src/cli/commands/platform.test.ts
T
gitea 235e4b806d docs(agent): 补齐 platform records --all 的流式导出约束与 JSON 错误测试
- platform records 的 JSON 结构化错误补充 exitCode 与 nextAction,与 runner
  的统一错误形状对齐;非 JSON 路径同步携带 nextAction;
- 新增 --json 模式下 DATASET_TOO_LARGE 单测,覆盖分页与流式导出建议;
- Skill 与 workflow 参考文档中的 --all 示例统一改为 --ndjson -o,并补充
  DATASET_TOO_LARGE 的恢复说明,修正 bbcf96c 声称完成但遗漏的 skill guidelines;
- datalink README 补充 --all 的流式导出约束与结构化错误字段。

校验:bun test 1156 pass / 0 fail;bun run check 通过;
check:readme 的 specs/ 与 src/core/db/ 缺失为仓库既有问题,与本次改动无关。
2026-11-18 15:25:33 +08:00

684 lines
27 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import * as servicesApplications from "../../domains/platform/applications/services.js";
import * as apiApplications from "../../domains/platform/applications/api.js";
import { describe, expect, it, spyOn, afterEach } from "bun:test";
import { handlePlatformCommand } from "./platform/index.js";
import * as api from "../../domains/platform/datalink/api.js";
import * as services from "../../domains/platform/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<typeof spyOn>[] = [];
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(servicesApplications, "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(servicesApplications, "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(servicesApplications, "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(servicesApplications, "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(servicesApplications, "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 支持仅传 <recordId> 免传应用与业务对象", 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(apiApplications, "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(servicesApplications, "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(servicesApplications, "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(servicesApplications, "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(servicesApplications, "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(servicesApplications, "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(servicesApplications, "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) => {
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, writeStdout: async (chunk) => { streamedLines.push(chunk); } }
);
expect(streamRes.exitCode).toBe(0);
expect(streamedLines.length).toBe(2);
expect(writeSpy).not.toHaveBeenCalled();
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("测试值");
});
it("platform records --all 拒绝直接全量输出,要求分页或流式文件", async () => {
spies.push(spyOn(helpers, "createClientsForCorp").mockResolvedValue({ ekbClient: {} } as any));
spies.push(spyOn(servicesApplications, "resolvePlatform").mockResolvedValue(mockPlatform as any));
spies.push(spyOn(services, "resolveEntity").mockResolvedValue(mockEntity as any));
await expect(
handlePlatformCommand(
{ area: "platform", action: "records", args: ["platform-1", "entity-1"], options: { all: true }, raw: ["platform", "records", "platform-1", "entity-1", "--all"] },
dummyRuntime
)
).rejects.toThrow("--all 不再直接返回全量");
});
it("platform records --all --json 返回 DATASET_TOO_LARGE 结构化错误", async () => {
spies.push(spyOn(helpers, "createClientsForCorp").mockResolvedValue({ ekbClient: {} } as any));
spies.push(spyOn(servicesApplications, "resolvePlatform").mockResolvedValue(mockPlatform as any));
spies.push(spyOn(services, "resolveEntity").mockResolvedValue(mockEntity as any));
const result = await handlePlatformCommand(
{
area: "platform",
action: "records",
args: ["platform-1", "entity-1"],
options: { all: true, json: true },
raw: ["platform", "records", "platform-1", "entity-1", "--all", "--json"],
},
{ ...dummyRuntime, jsonMode: true }
);
expect(result.exitCode).toBe(2);
const parsed = JSON.parse(result.stdout);
expect(parsed.error).toBe(true);
expect(parsed.code).toBe("DATASET_TOO_LARGE");
expect(parsed.exitCode).toBe(2);
expect(parsed.nextAction).toContain("--ndjson");
expect(parsed.suggestions).toEqual([
{ type: "paginate", command: "--page=1 --pageSize=500" },
{ type: "stream_export", command: "--ndjson --output-file=<path>" },
]);
});
it("rejects malformed batch predicates before fetching all records or calling the writer", async () => {
spies.push(spyOn(helpers, "createClientsForCorp").mockResolvedValue({ ekbClient: {} } as any));
spies.push(spyOn(servicesApplications, "resolvePlatform").mockResolvedValue(mockPlatform as any));
spies.push(spyOn(services, "resolveEntity").mockResolvedValue(mockEntity as any));
const fetch = spyOn(services, "fetchAllEntityRecords").mockResolvedValue({ data: [], total: 0 } as any);
const write = spyOn(services, "batchSafeEditRecords").mockResolvedValue([]);
spies.push(fetch, write);
await expect(handlePlatformCommand({ area: "platform", action: "batch-edit", args: ["platform-1", "entity-1"], options: { where: "序号 in [1]", set: "active=true", yes: true }, raw: [] }, dummyRuntime)).rejects.toThrow("不支持的筛选条件");
expect(fetch).not.toHaveBeenCalled();
expect(write).not.toHaveBeenCalled();
});
it("platform gen-props 生成 MVEL 和 JS 格式的字段模板", async () => {
spies.push(spyOn(helpers, "createClientsForCorp").mockResolvedValue({ ekbClient: {} } as any));
spies.push(spyOn(servicesApplications, "resolvePlatform").mockResolvedValue(mockPlatform as any));
spies.push(spyOn(services, "resolveEntity").mockResolvedValue({
id: "E_contract",
name: "合同",
code: "CONTRACT",
fields: [
{ name: "E_contract_name", label: "合同名称", type: "text", optional: false },
{ name: "E_contract_amount", label: "合同金额", type: "money", optional: true },
],
} as any));
// MVEL 模板 (默认)
const mvelRes = await handlePlatformCommand(
{ area: "platform", action: "gen-props", args: ["platform-1", "contract"], options: {}, raw: [] },
dummyRuntime
);
expect(mvelRes.exitCode).toBe(0);
expect(mvelRes.stdout).toContain('String TARGET_ENTITY_ID = "E_contract";');
expect(mvelRes.stdout).toContain('String FIELD_合同名称 = FIELD_PREFIX + "name";');
expect(mvelRes.stdout).toContain('row.put(FIELD_合同名称, "");');
// JS 模板
const jsRes = await handlePlatformCommand(
{ area: "platform", action: "gen-props", args: ["platform-1", "contract"], options: { lang: "js" }, raw: [] },
dummyRuntime
);
expect(jsRes.exitCode).toBe(0);
expect(jsRes.stdout).toContain('const ENTITY_ID = "E_contract";');
expect(jsRes.stdout).toContain('"合同名称": FIELD_PREFIX + "name"');
});
it("platform records 支持仅传单参数业务对象名称,全局反查其所属应用", async () => {
spies.push(spyOn(helpers, "createClientsForCorp").mockResolvedValue({ ekbClient: {} } as any));
// 按应用名称查未找到
spies.push(spyOn(servicesApplications, "resolvePlatform").mockResolvedValue(undefined));
// 列出所有平台
spies.push(spyOn(servicesApplications, "listPlatforms").mockResolvedValue([
{ id: "platform-lease", name: "租赁摊销业务" } as any,
{ id: "platform-other", name: "其他应用" } as any,
]));
// 模拟应用详情与对象列表
spies.push(spyOn(servicesApplications, "getPlatform").mockResolvedValue({
id: "platform-lease",
name: "租赁摊销业务",
} as any));
spies.push(spyOn(services, "listPlatformEntities").mockImplementation(async (_, __, platformId) => {
if (platformId === "platform-lease") {
return [
{ id: "ent-track", name: "付款发票跟踪", code: "TRACK", fields: [{ name: "name", label: "名称", type: "text" }] } as any,
];
}
return [];
}));
spies.push(spyOn(api, "searchEntityRecords").mockResolvedValue({
total: 1,
data: [
{
id: "rec-1",
dataLink: { id: "rec-1", name: "记录1", code: "R1", active: true, updateTime: "2026-10-22" },
},
],
actions: [],
components: [],
path: null,
template: null,
}));
const res = await handlePlatformCommand(
{ area: "platform", action: "records", args: ["付款发票跟踪"], options: { count: 1 }, raw: [] },
dummyRuntime
);
expect(res.exitCode).toBe(0);
expect(res.stdout).toContain("记录1");
});
it("platform records 单参数业务对象在多个应用重名时抛出明确指导错误", async () => {
spies.push(spyOn(helpers, "createClientsForCorp").mockResolvedValue({ ekbClient: {} } as any));
spies.push(spyOn(servicesApplications, "resolvePlatform").mockResolvedValue(undefined));
spies.push(spyOn(servicesApplications, "listPlatforms").mockResolvedValue([
{ id: "app-1", name: "应用一" } as any,
{ id: "app-2", name: "应用二" } as any,
]));
spies.push(spyOn(services, "listPlatformEntities").mockImplementation(async (_, __, platformId) => {
return [{ id: `ent-${platformId}`, name: "重名对象", code: "DUP" } as any];
}));
await expect(
handlePlatformCommand(
{ area: "platform", action: "records", args: ["重名对象"], options: {}, raw: [] },
dummyRuntime
)
).rejects.toThrow("在多个自建应用中找到同名业务对象 [重名对象],请显式指定应用名称");
});
it("platform records 单参数既不是应用也不是业务对象时返回明确 NOT FOUND 错误", async () => {
spies.push(spyOn(helpers, "createClientsForCorp").mockResolvedValue({ ekbClient: {} } as any));
spies.push(spyOn(servicesApplications, "resolvePlatform").mockResolvedValue(undefined));
spies.push(spyOn(servicesApplications, "listPlatforms").mockResolvedValue([
{ id: "app-1", name: "应用一" } as any,
]));
spies.push(spyOn(services, "listPlatformEntities").mockResolvedValue([]));
await expect(
handlePlatformCommand(
{ area: "platform", action: "records", args: ["未知目标"], options: {}, raw: [] },
dummyRuntime
)
).rejects.toThrow("[NOT FOUND] 当前企业下未找到名为 [未知目标] 的自建应用或业务对象");
});
});