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

268 lines
9.4 KiB
TypeScript

import { describe, expect, it } from "bun:test";
import { analyzeWorkflow } from "./index.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");
});
it("detects DEADLOCK_TRIGGER_STATE_FILTER when trigger specifies state APPROVING", () => {
const wf = {
id: "wf-trigger-deadlock",
label: "资金调拨自动审批",
state: 1,
version: "1",
tasks: [
{
name: "trigger_1",
type: "ekuaibao-flowform-trigger",
label: "单据审批事件触发器",
combineFilters: {
filters: [
{
filters: [
{ key: "formSpecification", value: "ID01RUq5zrhZTh", compare: "eq" },
{ key: "state", value: "APPROVING", compare: "eq" },
],
condition: "and",
},
],
condition: "or",
},
},
],
};
const result = analyzeWorkflow(wf as any);
const codes = result.diagnostics.map((d) => d.code);
expect(codes).toContain("DEADLOCK_TRIGGER_STATE_FILTER");
expect(result.diagnostics.find((d) => d.code === "DEADLOCK_TRIGGER_STATE_FILTER")?.severity).toBe("error");
});
it("detects MISSING_BACKLOG_DATA_BRACKETS on backlog openapi http task", () => {
const wf = {
id: "wf-http-no-brackets",
label: "自动审批",
state: 1,
version: "1",
tasks: [
{
name: "action_1",
type: "http",
label: "审批接口调用",
url: "https://app.ekuaibao.com/api/openapi/v1/backlog/data/ID01X4mbLfjvpt",
},
],
};
const result = analyzeWorkflow(wf as any);
const codes = result.diagnostics.map((d) => d.code);
expect(codes).toContain("MISSING_BACKLOG_DATA_BRACKETS");
expect(result.diagnostics.find((d) => d.code === "MISSING_BACKLOG_DATA_BRACKETS")?.severity).toBe("warning");
});
it("detects PERMISSION_MISSING_NODE when workflow uses nodes not granted to the enterprise", () => {
const wf = {
id: "wf-missing-perm",
label: "新代码节点测试",
state: 1,
version: "1",
tasks: [
{
name: "action_1",
type: "code-js2",
label: "新版JS节点",
code: "return 1;",
},
],
};
// 假设企业只开通了 code 和 http,没有开通 code-js2
const result = analyzeWorkflow(wf as any, {
availableNodeTypes: ["code", "http"],
});
const missingNodeDiag = result.diagnostics.find(
(d) => d.code === "PERMISSION_MISSING_NODE"
);
expect(missingNodeDiag).toBeDefined();
expect(missingNodeDiag?.severity).toBe("error");
expect(missingNodeDiag?.message).toContain("code-js2");
});
it("detects MISPLACED_FORMULA_IN_DATALINK_MAPPING when formulas are placed in DataLink fields", () => {
const wf = {
id: "wf-datalink-formula",
label: "台账摊销工作流",
state: 1,
version: "1",
tasks: [
{
name: "action_create",
type: "ekuaibao-datalink-create",
label: "新增摊销台账",
mapping: [
{
key: "amortizationMonth",
type: "date",
value: "AddMonths(date, 1)",
},
],
},
],
};
const result = analyzeWorkflow(wf as any);
const formulaDiag = result.diagnostics.find(
(d) => d.code === "MISPLACED_FORMULA_IN_DATALINK_MAPPING"
);
expect(formulaDiag).toBeDefined();
expect(formulaDiag?.severity).toBe("error");
expect(formulaDiag?.message).toContain("AddMonths");
expect(formulaDiag?.message).toContain("代码节点");
});
it("calculates summary metrics and classifies diagnostics into hard_block and soft_advice", () => {
const wf = {
id: "wf-summary-test",
label: "门禁分类测试",
state: 0, // -> warning (soft_advice): WORKFLOW_DISABLED
version: "1",
tasks: [
{
name: "empty_code_node",
type: "code",
label: "空代码",
code: "", // -> error (hard_block): EMPTY_CODE
},
{
name: "empty_branch_node",
type: "condition",
label: "空分支",
tasks: [], // -> warning (soft_advice): EMPTY_BRANCH
},
],
};
const result = analyzeWorkflow(wf as any);
expect(result.summary).toBeDefined();
expect(result.summary.total).toBe(3);
expect(result.summary.hardBlocks).toBe(1);
expect(result.summary.softAdvices).toBe(2);
expect(result.summary.passed).toBe(false);
const emptyCode = result.diagnostics.find((d) => d.code === "EMPTY_CODE");
expect(emptyCode?.category).toBe("hard_block");
expect(emptyCode?.severity).toBe("error");
const disabled = result.diagnostics.find((d) => d.code === "WORKFLOW_DISABLED");
expect(disabled?.category).toBe("soft_advice");
expect(disabled?.severity).toBe("warning");
const emptyBranch = result.diagnostics.find((d) => d.code === "EMPTY_BRANCH");
expect(emptyBranch?.category).toBe("soft_advice");
expect(emptyBranch?.severity).toBe("warning");
});
});