Files
hose-use/src/domains/datalink/datalink.test.ts
T

172 lines
6.8 KiB
TypeScript

import { describe, expect, test } from "bun:test";
import {
editRecord,
getPlatform,
getRecordDetail,
listPlatformEntities,
listPlatforms,
recomputeRecordLedger,
searchEntityRecords,
toggleRecordState,
} from "./api.js";
import { buildKeywordFilter, selectRecordFields } from "./services.js";
const platform = {
id: "platform-1",
name: "业务应用",
corporationId: "corp-1",
active: true,
adminIds: ["staff-1"],
};
describe("DataLink Domain - 应用、业务对象与记录查询", () => {
test("列出应用并为权限数组提供稳定默认值", async () => {
const client = {
get: async (url: string, options: any) => {
expect(url).toBe("api/v2/datalink/platform/search");
expect(options.searchParams).toEqual({ scope: "enable", corpId: "corp-1" });
return { json: async () => ({ items: [platform] }) };
},
} as any;
const result = await listPlatforms(client, "corp-1");
expect(result[0]?.name).toBe("业务应用");
expect(result[0]?.staffIds).toEqual([]);
expect(result[0]?.departmentIds).toEqual([]);
});
test("获取应用详情时请求管理员关联", async () => {
const client = {
get: async (url: string, options: any) => {
expect(url).toBe("api/v2/datalink/platform/$platform-1");
expect(options.searchParams).toBeInstanceOf(URLSearchParams);
expect(options.searchParams.get("id")).toBe("platform-1");
expect(options.searchParams.getAll("join")).toEqual([
"icon.fileId,fileId,/v1/attachment/attachments",
"adminIds,admin,/v1/organization/staffs",
]);
expect(options.searchParams.get("corpId")).toBe("corp-1");
return { json: async () => ({ value: { ...platform, staffIds: ["staff-1"], roleIds: ["role-1"], departmentIds: [], admin: [{ id: "staff-1", name: "管理员", code: "A-1", cellphone: "13800000000" }] } }) };
},
} as any;
const result = await getPlatform(client, "corp-1", "platform-1");
expect(result.adminIds).toEqual(["staff-1"]);
expect(result.roleIds).toEqual(["role-1"]);
expect(result.admin).toEqual([{ id: "staff-1", name: "管理员", code: "A-1", active: undefined }]);
expect(JSON.stringify(result)).not.toContain("13800000000");
});
test("获取对象字段元数据并保留扩展属性", async () => {
const client = {
post: async (url: string, options: any) => {
expect(url).toBe("api/v2/datalink/entity/platformId/$platform-1");
expect(options.searchParams.corpId).toBe("corp-1");
expect(options.json).toEqual({});
return { json: async () => ({ items: [{ id: "entity-1", name: "供应商", fields: [{ name: "code", label: "编码", type: "text", optional: false, customFlag: true }] }] }) };
},
} as any;
const result = await listPlatformEntities(client, "corp-1", "platform-1");
expect(result[0]?.fields[0]?.label).toBe("编码");
expect(result[0]?.fields[0]?.customFlag).toBe(true);
});
test("分页查询记录并生成标准化分页结果", async () => {
const client = {
post: async (url: string, options: any) => {
expect(url).toBe("api/v2/datalink/searchPlatformDataLinkWithChild/$entity-1");
expect(options.searchParams.corpId).toBe("corp-1");
expect(options.json).toEqual({
entityId: "entity-1",
query: { filterBy: 'code == "A-1"', limit: { start: 10, count: 5 } },
});
return { json: async () => ({ items: { total: 1, data: [{ id: "record-1", dataLink: { code: "A-1" } }] } }) };
},
} as any;
const result = await searchEntityRecords(client, "corp-1", "entity-1", { start: 10, count: 5, filterBy: 'code == "A-1"' });
expect(result.total).toBe(1);
expect(result.data[0]?.dataLink?.code).toBe("A-1");
});
test("关键词过滤和字段投影可用于通用 CLI 查询", () => {
expect(buildKeywordFilter('A"1')).toContain('A\\"1');
expect(selectRecordFields({ id: "r-1", dataLink: { code: "A-1" } }, ["编码"], [{ name: "code", label: "编码", type: "text" }])).toEqual({ id: "r-1", dataLink: { 编码: "A-1" } });
});
test("切换记录停启用状态调用 PUT contract state 接口", async () => {
let calledUrl = "";
let calledParams: any = null;
let calledJson: any = null;
const client = {
get: async () => ({
json: async () => ({ value: { id: "record-1", active: true } }),
}),
put: async (url: string, options: any) => {
calledUrl = url;
calledParams = options.searchParams;
calledJson = options.json;
return { status: 204 };
},
} as any;
const res = await toggleRecordState(client, "corp-1", "record-1");
expect(calledUrl).toBe("api/supplier/v3/contract/$record-1/state");
expect(calledParams).toEqual({ corpId: "corp-1" });
expect(calledJson).toEqual({ id: "record-1", state: false });
expect(res).toEqual({ recordId: "record-1", active: false });
});
test("获取记录详情调用 GET datalink detail 接口", async () => {
const client = {
get: async (url: string, options: any) => {
expect(url).toBe("api/v2/datalink/detail/$record-1/$DETAIL");
expect(options.searchParams).toEqual({
dataLinkId: "record-1",
type: "DETAIL",
corpId: "corp-1",
});
return { json: async () => ({ value: { id: "record-1", name: "记录1" } }) };
},
} as any;
const result = await getRecordDetail(client, "corp-1", "record-1");
expect((result as any).name).toBe("记录1");
});
test("编辑业务对象记录调用 POST entityEdit edit 接口", async () => {
const client = {
post: async (url: string, options: any) => {
expect(url).toBe("api/v2/datalink/entityEdit/edit");
expect(options.searchParams).toEqual({ corpId: "corp-1" });
expect(options.json.dataLinkEntityId).toBe("entity-1");
expect(options.json.dataLinkId).toBe("record-1");
expect(options.json.form).toEqual({ name: "新名称" });
return { json: async () => ({ value: { id: "record-1", name: "新名称" } }) };
},
} as any;
const result = await editRecord(client, "corp-1", {
dataLinkEntityId: "$entity-1",
dataLinkId: "$record-1",
form: { name: "新名称" },
});
expect((result as any).name).toBe("新名称");
});
test("重算台账调用 POST ledger recompute 接口", async () => {
const client = {
post: async (url: string, options: any) => {
expect(url).toBe("api/v2/ledger/recompute/$record-1");
expect(options.searchParams).toEqual({ corpId: "corp-1" });
return { json: async () => ({ id: "ok" }) };
},
} as any;
const result = await recomputeRecordLedger(client, "corp-1", "record-1");
expect(result.id).toBe("ok");
});
});