200 lines
7.1 KiB
TypeScript
200 lines
7.1 KiB
TypeScript
import { describe, expect, it, spyOn, afterEach } from "bun:test";
|
|
import { handlePlatformCommand } from "./platform.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<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(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");
|
|
});
|
|
});
|