fix: harden CLI parsing, MCP startup and release integrity

This commit is contained in:
gitea committed 2026-10-17 20:32:36 +08:00
1 parent f0902f5f20
commit 103ead0fe5
53 files changed
+3713 -1945

No files matched your search

@@ -0,0 +1,256 @@
import { describe, expect, it } from "bun:test";
import {
flattenWorkflowTaskTree,
findWorkflowTask,
reindexTasks,
addWorkflowTask,
editWorkflowTask,
removeWorkflowTask,
buildWorkflowTask,
summarizeTask,
} from "./index.js";
import type { WorkflowDetail, WorkflowTask } from "../../types.js";
const mockWorkflow: WorkflowDetail = {
id: "wf_test_100",
label: "发票风险处理流水线",
state: 0,
debugState: 0,
runTimes: 0,
errorTimes: 0,
updateTime: "2026-10-15",
version: "26.10.1",
dataLimit: 20,
source: 0,
queueJobs: false,
tasksVersion: 0,
tasks: [
{
name: "trigger_webhook_1",
type: "webhook",
label: "发票数据接入",
groupName: "trigger",
orderIndex: 0,
levelIndex: [0],
status: "active",
urlPath: "test-uuid-1",
params: [],
},
{
name: "control_every_1",
type: "every",
label: "循环发票明细",
desc: "依次遍历每张发票",
groupName: "control",
mode: "sequence",
batch: false,
list: "@{choose('body.invoices')}",
filters: [],
tasksMaxLevel: 1,
orderIndex: 1,
levelIndex: [1],
status: "active",
tasks: [
{
name: "action_js_1",
type: "code-js2",
label: "数据清洗",
groupName: "action",
orderIndex: 2,
levelIndex: [1, 0],
status: "active",
code: "return item.amount > 0;",
},
{
name: "control_cond_1",
type: "condition",
label: "超额判定",
groupName: "control",
combineFilters: { condition: "or", filters: [] },
tasksMaxLevel: 1,
orderIndex: 3,
levelIndex: [1, 1],
status: "active",
tasks: [
{
name: "action_stop_1",
type: "stop",
label: "阻断超额",
groupName: "control",
orderIndex: 4,
levelIndex: [1, 1, 0],
status: "active",
},
],
},
],
},
{
name: "action_http_1",
type: "http",
label: "通知外部系统",
groupName: "action",
method: "POST",
url: "https://api.example.com/notify",
orderIndex: 5,
levelIndex: [2],
status: "active",
},
],
};
describe("workflow-nodes - 任务节点声明式编排纯函数", () => {
it("flattenWorkflowTaskTree correctly flattens nested tree with depth and summary", () => {
const flattened = flattenWorkflowTaskTree(mockWorkflow.tasks);
expect(flattened).toHaveLength(6);
// 1. Webhook trigger
expect(flattened[0].hierarchicalIndex).toBe("1");
expect(flattened[0].task.name).toBe("trigger_webhook_1");
expect(flattened[0].depth).toBe(0);
expect(flattened[0].summary).toContain("urlPath: test-uuid-1");
// 2. Every container
expect(flattened[1].hierarchicalIndex).toBe("2");
expect(flattened[1].task.name).toBe("control_every_1");
expect(flattened[1].depth).toBe(0);
expect(flattened[1].summary).toContain("循环遍历");
// 3. Child in every container (code-js2)
expect(flattened[2].hierarchicalIndex).toBe("2.1");
expect(flattened[2].task.name).toBe("action_js_1");
expect(flattened[2].depth).toBe(1);
expect(flattened[2].parentName).toBe("control_every_1");
expect(flattened[2].summary).toContain("脚本 (23 字符)");
// 4. Condition in every container
expect(flattened[3].hierarchicalIndex).toBe("2.2");
expect(flattened[3].task.name).toBe("control_cond_1");
expect(flattened[3].depth).toBe(1);
// 5. Stop inside condition inside every
expect(flattened[4].hierarchicalIndex).toBe("2.2.1");
expect(flattened[4].task.name).toBe("action_stop_1");
expect(flattened[4].depth).toBe(2);
expect(flattened[4].summary).toBe("阻断终止流程");
// 6. Http top-level task
expect(flattened[5].hierarchicalIndex).toBe("3");
expect(flattened[5].task.name).toBe("action_http_1");
expect(flattened[5].depth).toBe(0);
expect(flattened[5].summary).toContain("POST https://api.example.com/notify");
});
it("findWorkflowTask finds nodes by name, hierarchical index, or global number", () => {
// By name
const byName = findWorkflowTask(mockWorkflow.tasks, "action_js_1");
expect(byName).not.toBeNull();
expect(byName?.task.label).toBe("数据清洗");
expect(byName?.hierarchicalIndex).toBe("2.1");
expect(byName?.path).toBe("tasks.1.tasks.0");
// By hierarchical index
const byHierarchical = findWorkflowTask(mockWorkflow.tasks, "2.2.1");
expect(byHierarchical).not.toBeNull();
expect(byHierarchical?.task.name).toBe("action_stop_1");
// By global sequential index number
const byGlobalIndex = findWorkflowTask(mockWorkflow.tasks, 3);
expect(byGlobalIndex).not.toBeNull();
expect(byGlobalIndex?.task.name).toBe("action_js_1");
// Non-existent target
const notFound = findWorkflowTask(mockWorkflow.tasks, "non_existent_node");
expect(notFound).toBeNull();
});
it("reindexTasks recalculates orderIndex and levelIndex consistently", () => {
const clonedTasks: WorkflowTask[] = JSON.parse(JSON.stringify(mockWorkflow.tasks));
// Mess up indices
clonedTasks[0].orderIndex = 99;
clonedTasks[1].levelIndex = [9, 9];
reindexTasks(clonedTasks);
expect(clonedTasks[0].orderIndex).toBe(0);
expect(clonedTasks[0].levelIndex).toEqual([0]);
expect(clonedTasks[1].orderIndex).toBe(1);
expect(clonedTasks[1].levelIndex).toEqual([1]);
expect(clonedTasks[1].tasks![0].orderIndex).toBe(2);
expect(clonedTasks[1].tasks![0].levelIndex).toEqual([1, 0]);
expect(clonedTasks[1].tasks![1].tasks![0].orderIndex).toBe(4);
expect(clonedTasks[1].tasks![1].tasks![0].levelIndex).toEqual([1, 1, 0]);
});
it("addWorkflowTask appends to root, inserts after node, and inserts into parent container", () => {
// 1. Append to root
const { workflow: wfAppended, newTask: appendedTask } = addWorkflowTask(mockWorkflow, {
type: "code-js2",
label: "末尾数据汇总",
code: "return { total: 100 };",
});
expect(wfAppended.tasks).toHaveLength(4);
expect(appendedTask.label).toBe("末尾数据汇总");
expect(wfAppended.tasks[3].name).toBe(appendedTask.name);
expect(wfAppended.tasks[3].levelIndex).toEqual([3]);
// 2. Insert into parent container
const { workflow: wfParent, newTask: parentTask } = addWorkflowTask(mockWorkflow, {
type: "stop",
label: "循环中终止",
parent: "control_every_1",
});
const everyNode = wfParent.tasks[1];
expect(everyNode.tasks).toHaveLength(3);
expect(everyNode.tasks![2].name).toBe(parentTask.name);
expect(everyNode.tasks![2].levelIndex).toEqual([1, 2]);
// 3. Insert after specific node
const { workflow: wfAfter, newTask: afterTask } = addWorkflowTask(mockWorkflow, {
type: "code",
label: "MVEL计算节点",
after: "action_js_1",
code: "return 1 + 1;",
});
const everyTasks = wfAfter.tasks[1].tasks!;
expect(everyTasks).toHaveLength(3);
expect(everyTasks[0].name).toBe("action_js_1");
expect(everyTasks[1].name).toBe(afterTask.name); // Inserted right after
expect(everyTasks[1].label).toBe("MVEL计算节点");
expect(everyTasks[1].levelIndex).toEqual([1, 1]);
});
it("editWorkflowTask updates label, code, and custom properties", () => {
const { workflow: wfEdited, editedTask } = editWorkflowTask(mockWorkflow, "action_js_1", {
label: "数据清洗升级版",
code: "return item.amount > 500;",
desc: "只保留大于500的发票",
set: { customTag: "finance-audit" },
});
expect(editedTask.label).toBe("数据清洗升级版");
expect((editedTask as any).code).toBe("return item.amount > 500;");
expect(editedTask.desc).toBe("只保留大于500的发票");
expect((editedTask as any).customTag).toBe("finance-audit");
// Original workflow must not be mutated
expect(mockWorkflow.tasks[1].tasks![0].label).toBe("数据清洗");
});
it("removeWorkflowTask safely removes task and guards trigger node", () => {
// 1. Remove child task in container
const { workflow: wfRemoved, removedTask } = removeWorkflowTask(mockWorkflow, "action_js_1");
expect(removedTask.name).toBe("action_js_1");
expect(wfRemoved.tasks[1].tasks).toHaveLength(1);
expect(wfRemoved.tasks[1].tasks![0].name).toBe("control_cond_1");
expect(wfRemoved.tasks[1].tasks![0].levelIndex).toEqual([1, 0]); // Re-indexed to 0
// 2. Trigger protection: removing primary trigger when other tasks exist throws ValidationError
expect(() => {
removeWorkflowTask(mockWorkflow, "trigger_webhook_1");
}).toThrow("触发源节点");
});
});