Files
hose-use/src/domains/platform/analysis.test.ts
T

115 lines
4.5 KiB
TypeScript

import { describe, expect, it } from "bun:test";
import { analyzeWorkflow } from "./analysis.js";
import { PlatformSchema } from "./types.js";
describe("Workflow static analysis", () => {
it("accepts nullable app center metadata", () => {
const platform = PlatformSchema.parse({ id: "app-1", name: "测试应用", corporationId: "corp-1", adminIds: null, importMethod: null });
expect(platform.adminIds).toEqual([]);
expect(platform.importMethod).toEqual([]);
});
it("builds a recursive AST and extracts code references", () => {
const result = analyzeWorkflow({ id: "wf-1", label: "审批", state: 1, version: "3", tasks: [{ name: "条件", type: "condition", label: "条件", groupName: "", orderIndex: 0, levelIndex: [0], status: "", tasks: [{ name: "脚本", type: "code-js2", label: "脚本", groupName: "", orderIndex: 1, levelIndex: [0, 0], status: "", code: "return {{bill.amount}} + ctx.user.id;" }] }] } as any);
expect(result.ast[0]?.children[0]?.path).toBe("0.0"); expect(result.dependencies[0]?.references).toEqual(["bill.amount", "user.id"]); expect(result.diagnostics).toHaveLength(0);
});
it("reports empty scripts, branches, and disabled workflow state", () => {
const result = analyzeWorkflow({ id: "wf-2", label: "异常", state: 0, version: "", tasks: [{ name: "脚本", type: "code", label: "", groupName: "", orderIndex: 0, levelIndex: [0], status: "", code: "" }, { name: "条件", type: "condition", label: "", groupName: "", orderIndex: 1, levelIndex: [1], status: "", tasks: [] }] } as any);
expect(result.diagnostics.map((item) => item.code)).toEqual(["WORKFLOW_DISABLED", "EMPTY_CODE", "EMPTY_BRANCH"]);
});
it("detects UNSAFE_SKIP_WITHOUT_STATUS_GUARD and LINKAGE_PRIORITY_OVERRIDE", () => {
const wf = {
id: "886351399301758976",
label: "【生产】每30分钟创建采购申请单",
state: 1,
version: "10",
tasks: [
{
name: "every_0",
type: "every",
label: "循环每条支付计划",
groupName: "control",
orderIndex: 1,
levelIndex: [0],
status: "",
tasks: [
{
name: "ekuaibao_flow_create_task_0",
type: "flow-create-task",
label: "创建预付申请单",
groupName: "ekuaibao",
orderIndex: 0,
levelIndex: [0, 0],
status: "",
onError: { defaultAction: "SKIP" },
inputs: {
specificationId: "ID01TxgE3fhD6n",
form: {
u_关联合同流程: "{{every_0.item.E_contract}}",
u_我方签约主体: "{{every_0.item.E_company}}",
},
},
},
{
name: "ekuaibao_datalink_edit_0",
type: "ekuaibao-datalink-edit",
label: "更新支付计划状态为创建成功",
groupName: "ekuaibao",
orderIndex: 1,
levelIndex: [0, 1],
status: "",
inputs: {
dataLinkId: "{{every_0.item.id}}",
form: {
状态: "创建成功",
},
},
},
],
},
],
};
const specifications = {
ID01TxgE3fhD6n: {
id: "ID01TxgE3fhD6n",
name: "预付申请单",
form: [
{
id: "f1",
name: "u_关联合同流程",
label: "关联合同流程",
type: "dataLink",
optional: false,
entityId: "e23d0d5451b1ccabfd7a",
assignmentRule: "ID01TRWvYFAulh",
isLinkageAssignment: false,
priorityList: [
{ type: "LINKAGE_ASSIGNMENT", priority: 10 },
{ type: "MANUALLY_FILL_IN", priority: 5 },
],
},
{
id: "f2",
name: "u_我方签约主体",
label: "我方签约主体",
type: "dimension",
optional: false,
isLinkageAssignment: true,
priorityList: [
{ type: "LINKAGE_ASSIGNMENT", priority: 10 },
{ type: "MANUALLY_FILL_IN", priority: 5 },
],
},
],
},
};
const result = analyzeWorkflow(wf as any, { specifications: specifications as any });
const codes = result.diagnostics.map((d) => d.code);
expect(codes).toContain("UNSAFE_SKIP_WITHOUT_STATUS_GUARD");
expect(codes).toContain("LINKAGE_PRIORITY_OVERRIDE");
});
});