Files
hose-use/src/domains/platform/workflow/analysis/choose-references.test.ts
T
gitea 71d1a59934 fix(workflow): 扩展字段引用扫描并收紧 choose 描述符门禁
- 字段引用检查覆盖 combineFilters、filters、conditions、flowCode、list、details 及嵌套结构
- choose 描述符 JSON 损坏或缺少有效 key 时新增 INVALID_CHOOSE_DESCRIPTOR 硬门禁
- DataLink 使用 gte/lte 时新增 DATALINK_FILTER_COMPARISON_COMPATIBILITY 软提示,不自动改写边界
- 重新生成 dist/mcp/server.js 并同步领域测试与 README 说明
2026-10-27 17:38:26 +08:00

156 lines
8.6 KiB
TypeScript

import { describe, expect, test } from "bun:test";
import { analyzeWorkflow } from "./analyzer.js";
const descriptor = (key: string) => `@{choose('${JSON.stringify({ key, properties: [{ key: "data" }] })}')}`;
function analyze(config: Record<string, unknown>, sources: Array<Record<string, unknown>> = [{ name: "trigger_actual", type: "webhook" }]) {
return analyzeWorkflow({ id: "wf", label: "test", state: 1, version: "1", tasks: [
...sources, { name: "action_write", type: "http", ...config },
] } as any);
}
describe("choose reference containers and source classification", () => {
test("finds missing JSON node references in direct and nested form containers", () => {
const tag = descriptor("missing_node");
for (const config of [
{ fields: [{ key: "title", value: tag }] },
{ mapping: [{ key: "title", value: tag }] },
{ mapping: { title: { nested: [tag] } } },
{ form: [{ key: "title", value: tag }] },
{ form: { title: tag } },
{ inputs: { form: { title: tag } } },
{ inputs: [{ key: "form", value: { title: tag } }] },
{ inputs: [{ key: "form", value: [{ key: "title", value: tag }] }] },
]) {
const result = analyze(config);
expect(result.summary.passed).toBe(false);
expect(result.diagnostics.filter(item => item.code === "BROKEN_CHOOSE_REFERENCE")).toHaveLength(1);
expect(result.diagnostics.find(item => item.code === "BROKEN_CHOOSE_REFERENCE")?.message).toContain("title");
}
});
test("does not inspect script, descriptions or descriptor label/type metadata", () => {
const tag = descriptor("missing_node");
const result = analyze({ code: `return ${JSON.stringify(tag)};`, desc: tag, label: tag, fields: [{ key: "title", label: tag, type: tag, value: "ordinary value" }], inputs: { form: { nested: { key: "title", value: "ordinary value", label: tag, type: tag } } } });
expect(result.diagnostics).toEqual([]);
// A raw form object may legitimately have a business field named label.
expect(analyze({ form: { label: tag } }).summary.passed).toBe(false);
expect(analyze({ form: { key: "ordinary field", value: "ordinary value", label: tag } }).summary.passed).toBe(false);
});
test.each([
"@{choose(action_missing.data)}",
"@{choose('action_missing.data')}",
'@{choose("action_missing.data")}',
descriptor("action_missing"),
])("blocks a clearly identified missing node in %s", tag => {
const result = analyze({ mapping: [{ key: "value", value: tag }] });
expect(result.summary.passed).toBe(false);
expect(result.diagnostics.map(item => item.code)).toContain("BROKEN_CHOOSE_REFERENCE");
});
test("handles multiple descriptors, escaped quotes and parentheses inside labels", () => {
const single = `@{choose('${JSON.stringify({ key: "action_first", label: 'text (with) "quotes"', properties: [] })}')}`;
const double = `@{choose(${JSON.stringify(JSON.stringify({ key: "action_second", properties: [] }))})}`;
const result = analyze({ inputs: { value: `prefix ${single} / ${double} suffix` } });
expect(result.diagnostics.filter(item => item.code === "BROKEN_CHOOSE_REFERENCE")).toHaveLength(2);
});
test("recognizes real custom node names while preserving body and other context roots", () => {
for (const tag of ["@{choose(source.data)}", "@{choose('source.data')}", descriptor("source"), "@{choose('body.invoices')}", "@{choose(input.value)}"]) {
const result = analyze({ inputs: { value: tag } }, [{ name: "source", type: "webhook" }]);
expect(result.summary.passed).toBe(true);
expect(result.diagnostics).toEqual([]);
}
});
test("requires explicit descriptor node keys even when they resemble context roots", () => {
for (const key of ["body", "context"]) {
expect(analyze({ form: { title: descriptor(key) } }).summary.passed).toBe(false);
expect(analyze({ form: { title: descriptor(key) } }, [{ name: key, type: "webhook" }]).summary.passed).toBe(true);
}
});
test("warns for unknown bare roots and unsupported or incomplete expressions without blocking", () => {
for (const tag of ["@{choose(missing_node.data)}", "@{choose('missing_node.data')}", "@{choose(action_x.data + 1)}", "@{choose('action_missing.data')"]) {
const result = analyze({ form: { title: tag } });
expect(result.summary.passed).toBe(true);
expect(result.diagnostics.map(item => item.code)).toContain("UNRESOLVED_CHOOSE_REFERENCE");
expect(result.diagnostics.every(item => item.category === "soft_advice")).toBe(true);
}
});
test.each([
"@{choose('{bad json}')}",
`@{choose('${JSON.stringify({ key: "trigger_actual", properties: [{ key: "startTime", type: "NUMBER" }] })}}')}`,
`@{choose('${JSON.stringify({ properties: [] })}')}`,
])("blocks an explicitly damaged JSON descriptor in %s", tag => {
const result = analyze({ combineFilters: { condition: "and", filters: [{ key: "日期", compare: "gt", value: tag }] } });
expect(result.summary.passed).toBe(false);
expect(result.diagnostics).toMatchObject([{ code: "INVALID_CHOOSE_DESCRIPTOR", category: "hard_block", severity: "error" }]);
expect(result.diagnostics[0].message).toContain("日期");
});
test("checks runtime expressions in filters, lookup codes, loops and nested details", () => {
const missing = descriptor("action_missing");
for (const config of [
{ combineFilters: { filters: [{ filters: [{ key: "日期", value: missing, compare: "gt" }], condition: "and" }], condition: "or" } },
{ filters: [{ key: "日期", value: missing }] },
{ conditions: { value: missing } },
{ flowCode: missing },
{ list: missing },
{ details: { fixed: [{ amount: missing }] } },
]) {
const result = analyze(config);
expect(result.diagnostics.map(item => item.code)).toContain("BROKEN_CHOOSE_REFERENCE");
expect(result.summary.passed).toBe(false);
}
});
test("warns about DataLink inclusive comparison compatibility without rewriting boundaries", () => {
const config = { type: "ekuaibao-datalink-filter", combineFilters: { filters: [
{ key: "日期", compare: "gte", value: descriptor("trigger_actual") },
{ key: "日期", compare: "lte", value: descriptor("trigger_actual") },
] } };
const original = JSON.stringify(config);
const result = analyze(config);
expect(result.diagnostics).toHaveLength(2);
expect(result.diagnostics.every(item => item.code === "DATALINK_FILTER_COMPARISON_COMPATIBILITY" && item.category === "soft_advice")).toBe(true);
expect(result.diagnostics[0].message).toContain("边界");
expect(JSON.stringify(config)).toBe(original);
expect(analyze({ type: "ekuaibao-datalink-filter", filters: config.combineFilters.filters }).diagnostics).toHaveLength(2);
for (const compare of ["gt", "lt"]) {
expect(analyze({ ...config, combineFilters: { filters: [{ key: "日期", compare, value: descriptor("trigger_actual") }] } }).diagnostics).toEqual([]);
}
expect(analyze({ ...config, type: "condition", tasks: [{ name: "child", type: "http" }] }).diagnostics).toEqual([]);
});
test.each(["@{choose(control_loop.$item)}", "@{choose('control_loop.$item.data')}", descriptor("control_loop.$item")])("blocks old loop item paths in %s", tag => {
const result = analyze({ fields: [{ key: "value", value: tag }] });
expect(result.summary.passed).toBe(false);
expect(result.diagnostics.map(item => item.code)).toContain("BROKEN_LOOP_ITEM_REFERENCE");
});
test("accepts the new loop descriptor with $item in properties", () => {
const tag = `@{choose('${JSON.stringify({ key: "control_loop", properties: [{ key: "$item" }, { key: "data" }] })}')}`;
const result = analyze({ fields: [{ key: "value", value: tag }] }, [{ name: "control_loop", type: "every", tasks: [{ name: "inner", type: "http" }] }]);
expect(result.summary.passed).toBe(true);
expect(result.diagnostics).toEqual([]);
});
test("never aliases missing trigger_1 to an actual trigger with another name", () => {
for (const tag of [descriptor("trigger_1"), "@{choose(trigger_1.data)}"]) {
expect(analyze({ inputs: { value: tag } }).summary.passed).toBe(false);
const real = analyze({ inputs: { value: tag } }, [{ name: "trigger_1", type: "webhook" }]);
expect(real.summary.passed).toBe(true);
}
});
test("finds actual nodes in nested task containers", () => {
const result = analyze({ fields: [{ key: "value", value: descriptor("action_nested") }] }, [
{ name: "condition", type: "condition", tasks: [{ name: "action_nested", type: "http" }] },
]);
expect(result.summary.passed).toBe(true);
expect(result.diagnostics).toEqual([]);
});
});