Files
hose-use/src/domains/platform/workflow/analysis/condition-null-safety.test.ts
T
gitea bb60053072 feat(analysis): 新增条件节点空值安全规则 CONDITION_NULL_UNSAFE_COMPARE
## 背景

条件节点对「取值链要先穿过列表再往下取字段」的字段做大于/小于类比较时,上游
列表为空会让字段取到 `null`。MVEL 里 `null < 0` 返回的是 **null 而不是 false**,
引擎对 condition 做强 Boolean 校验,于是直接抛:

```
if 节点(xxx)的 condition 参数的解析结果必须为 Boolean 类型
```

流程在这里**中断**,而不是安静地走 false 分支。真实案例中一条链路跑到第 9 步
才炸,报错信息完全指不出是哪次取值拿到了 null。

## 关键实测结论:AND 不短路

直觉写法是 `AND(列表非空, 额度 < 0)`,指望布尔短路跳过后面的比较。实测证明
这条路走不通——合思引擎**逐叶子求值再合并**:

| 输入 | 条件 | 结果 |
|---|---|---|
| 列表为空 | `AND(列表 notNull, 额度 lt 0)` | FAILED,仍是同一个类型错误 |
| 额度 = -5 | 同上 | COMPLETED |

也就是说同一个条件节点里加「不为空」判断**完全无效**,null 比较照样会被算出来。
唯一可靠的是流程控制短路:外层套一个条件节点判「列表不为空」,把原节点放进它的
满足分支——列表为空时外层为 false,内层根本不被求值。

同时确认了判空本身的可用性:`notNull` 对空数组 `[]` 返回 false、对非空数组返回
true,且不需要 `value` 字段;条件为 false 时节点 COMPLETED、子节点跳过、流程继续。

## 实现

新增 `analysis/condition-null-safety.ts`:

- `readChoosePath()` 解析 `@{choose('{...}')}` 描述符,兼容前端多一个右花括号的形态,
  取出取值链的根节点与 `properties` 层级;
- `goesThroughList()` 判定取值链是否「穿过 `DATA_LINK_LIST` 之后还有层级」——
  单层字段取值和「只判列表本身」都不算风险;
- `hasAncestorListGuard()` 沿祖先链找对同一列表做过 `notNull` 判断的条件节点;
- 命中「有序比较(`gt/gte/lt/lte`)+ 穿过列表 + 无祖先守卫」时产出
  `CONDITION_NULL_UNSAFE_COMPARE` 软提示,消息里明确否掉「用 AND 兜住」这个直觉解法,
  并给出守卫上提的正确写法。

`analyzer.ts` 的遍历新增祖先链参数,在条件节点诊断段之后调用该检查。

## 为什么是软建议而不是硬门禁

静态分析无法确定上游列表一定为空(例如上游刚创建、或业务上保证有数据),硬阻断会
让合法流程无法发布。因此只提示、不改写配置、不推断数据非空。`eq`/`neq` 也不纳入
检查——null 与值做相等比较返回的是布尔,不会中断。

## 测试

新增 14 个用例:路径解析(含多花括号形态)、穿过列表判定、无守卫告警、同节点内
AND 组合仍告警、外层守卫不告警、守卫判断的是别的列表仍告警、单层取值不告警、
只判列表不告警、`gt/gte/lte` 覆盖、`eq` 不告警、同一路径去重。

另外用真实流程做了前后对照:改造前(无守卫)命中 2 条,加上守卫后命中 0 条。

`bun test` 1082 pass / 0 fail;`bun run check` 零错误;`check:readme` 通过。
2026-11-04 17:10:02 +08:00

144 lines
6.1 KiB
TypeScript

import { describe, expect, it } from "bun:test";
import { analyzeWorkflow } from "./index.js";
import { goesThroughList, readChoosePath } from "./condition-null-safety.js";
const LIST_LEAF =
"@{choose('{\"key\":\"action_find\",\"properties\":[{\"key\":\"$this\",\"label\":\"结果列表\",\"type\":\"DATA_LINK_LIST\"},{\"key\":\"0\",\"label\":\"首条数据\",\"type\":\"DATA_LINK\"},{\"key\":\"E_x\",\"label\":\"差旅费剩余额度\",\"type\":\"MONEY\"}],\"label\":\"查找业务数据.结果列表.首条数据.差旅费剩余额度\"}')}";
const LIST_SELF =
"@{choose('{\"key\":\"action_find\",\"properties\":[{\"key\":\"$this\",\"label\":\"结果列表\",\"type\":\"DATA_LINK_LIST\"}],\"label\":\"查找业务数据.结果列表\"}')}";
const OTHER_LIST_LEAF =
"@{choose('{\"key\":\"action_other\",\"properties\":[{\"key\":\"$this\",\"label\":\"结果列表\",\"type\":\"DATA_LINK_LIST\"},{\"key\":\"0\",\"label\":\"首条数据\",\"type\":\"DATA_LINK\"}],\"label\":\"别的查询.结果列表.首条数据\"}')}";
const FLAT_MONEY =
"@{choose('{\"key\":\"trigger_1\",\"properties\":[{\"key\":\"amount\",\"label\":\"金额\",\"type\":\"MONEY\"}],\"label\":\"金额\"}')}";
function leaf(key: string, compare: string, value?: unknown) {
return value === undefined ? { key, compare } : { key, compare, value };
}
function group(filters: unknown[], condition = "and") {
return { condition, filters };
}
function condition(name: string, filters: unknown, children: unknown[] = []) {
return { name, type: "condition", label: "条件节点", combineFilters: filters, tasks: children };
}
function wf(tasks: unknown[]) {
return { id: "wf-null", label: "空值安全", state: 1, version: "1", tasks } as any;
}
describe("choose 路径解析", () => {
it("识别穿过列表的取值链", () => {
const parsed = readChoosePath(LIST_LEAF);
expect(parsed).not.toBeNull();
expect(parsed!.root).toBe("action_find");
expect(goesThroughList(parsed!)).toBe(true);
});
it("列表位于最后一层时不算穿过", () => {
expect(goesThroughList(readChoosePath(LIST_SELF)!)).toBe(false);
});
it("单层字段取值不算穿过", () => {
expect(goesThroughList(readChoosePath(FLAT_MONEY)!)).toBe(false);
});
it("兼容前端多一个右花括号的形态", () => {
const tail = "}')}";
const doubled = LIST_LEAF.slice(0, LIST_LEAF.lastIndexOf(tail)) + "}}" + "')}";
const parsed = readChoosePath(doubled);
expect(parsed?.root).toBe("action_find");
expect(goesThroughList(parsed!)).toBe(true);
});
it("非 choose 表达式一律忽略", () => {
expect(readChoosePath("trigger_1.amount")).toBeNull();
expect(readChoosePath(undefined)).toBeNull();
});
});
describe("CONDITION_NULL_UNSAFE_COMPARE", () => {
it("列表取数 + 数值比较 + 无守卫 → 告警", () => {
const result = analyzeWorkflow(wf([condition("control_a", group([group([leaf(LIST_LEAF, "lt", 0)])]))]));
const diag = result.diagnostics.find((d) => d.code === "CONDITION_NULL_UNSAFE_COMPARE");
expect(diag).toBeDefined();
expect(diag?.severity).toBe("warning");
expect(diag?.category).toBe("soft_advice");
expect(diag?.message).toContain("差旅费剩余额度");
expect(diag?.message).toContain("Boolean");
// 必须明确否掉「AND 兜住」这个直觉解法
expect(diag?.message).toContain("不做短路");
});
it("同一节点内用 AND 组合了列表非空判断 → 仍然告警(引擎不短路)", () => {
const result = analyzeWorkflow(
wf([
condition(
"control_a",
group([group([leaf(LIST_SELF, "notNull")]), group([leaf(LIST_LEAF, "lt", 0)])])
),
])
);
const diags = result.diagnostics.filter((d) => d.code === "CONDITION_NULL_UNSAFE_COMPARE");
expect(diags).toHaveLength(1);
});
it("外层套了列表非空守卫 → 不告警", () => {
const inner = condition("control_b", group([group([leaf(LIST_LEAF, "lt", 0)])]));
const outer = condition("control_guard", group([group([leaf(LIST_SELF, "notNull")])]), [inner]);
const result = analyzeWorkflow(wf([outer]));
expect(result.diagnostics.some((d) => d.code === "CONDITION_NULL_UNSAFE_COMPARE")).toBe(false);
});
it("守卫判断的是另一个列表 → 仍然告警", () => {
const inner = condition("control_b", group([group([leaf(LIST_LEAF, "lt", 0)])]));
const outer = condition(
"control_guard",
group([group([leaf(OTHER_LIST_LEAF.replace("action_other", "action_zzz"), "notNull")])]),
[inner]
);
const result = analyzeWorkflow(wf([outer]));
expect(result.diagnostics.some((d) => d.code === "CONDITION_NULL_UNSAFE_COMPARE")).toBe(true);
});
it("单层字段比较 → 不告警", () => {
const result = analyzeWorkflow(wf([condition("control_a", group([group([leaf(FLAT_MONEY, "lt", 0)])]))]));
expect(result.diagnostics.some((d) => d.code === "CONDITION_NULL_UNSAFE_COMPARE")).toBe(false);
});
it("只判列表本身 → 不告警", () => {
const result = analyzeWorkflow(wf([condition("control_a", group([group([leaf(LIST_SELF, "notNull")])]))]));
expect(result.diagnostics.some((d) => d.code === "CONDITION_NULL_UNSAFE_COMPARE")).toBe(false);
});
it("gte / lte / gt 同样覆盖", () => {
for (const compare of ["gt", "gte", "lte"]) {
const result = analyzeWorkflow(wf([condition("control_a", group([group([leaf(LIST_LEAF, compare, 0)])]))]));
expect(result.diagnostics.some((d) => d.code === "CONDITION_NULL_UNSAFE_COMPARE")).toBe(true);
}
});
it("eq 不属于有序比较 → 不告警", () => {
const result = analyzeWorkflow(wf([condition("control_a", group([group([leaf(LIST_LEAF, "eq", 0)])]))]));
expect(result.diagnostics.some((d) => d.code === "CONDITION_NULL_UNSAFE_COMPARE")).toBe(false);
});
it("同一路径重复出现只报一次", () => {
const result = analyzeWorkflow(
wf([
condition(
"control_a",
group([
group([leaf(LIST_LEAF, "lt", 0)]),
group([leaf(LIST_LEAF, "gt", 100)]),
])
),
])
);
expect(result.diagnostics.filter((d) => d.code === "CONDITION_NULL_UNSAFE_COMPARE")).toHaveLength(1);
});
});