fix(workflow): 收敛并发/守卫/return 三处判定,修复漏告警与误判

并发保护此前只存在于 CLI 调用点(node.ts 自己重读比对),SDK 出口 updateWorkflow 完全没有,
任何绕过 CLI 的调用方都会静默覆盖他人的修改:

- updateWorkflow 第三参数支持 `{ platformId, expectedVersion }` 对象形式(字符串写法向后兼容),
  提供 expectedVersion 时提交前重读远端比对,不一致抛 ValidationError(WORKFLOW_VERSION_CONFLICT);
- node.ts saveNodeChanges 改为复用该能力,删除重复实现,CLI 与 SDK 共用一条路径;
- 测试改为伪造 Piper 客户端而非 spy updates,覆盖:期望版本一致/不一致、旧字符串写法、
  无期望值时不额外读取、chooses 部分失败。

状态守卫判定(UNSAFE_SKIP_WITHOUT_STATUS_GUARD)此前把条件树 JSON.stringify 后用子串包含判断,
只要任意位置出现节点名且出现 $status 即判为已守卫,短节点名会普遍误命中,
规则因此静默失效:

- 改为收集条件内的字符串,取 ${a.b} / a.b 形式的引用目标后整体比对 `<节点名>.$status|status|result|data`;
- 同时统一了 combineFilters/filters/conditions/inputs 的取值入口,消除各处顺序不一致;
- 新增四条回归用例,含"节点名子串不得误判为守卫"。

return 识别此前只跳过整行注释,行尾 `// return`、块注释内部行与字符串里的 "return"
都被当真,missing-return 告警永不触发:新增 stripCommentsAndStrings 处理行注释、
块注释、单/双引号与模板串(含转义),新增专项测试。

同步更新 management、script 与 workflow 三处模块 README。
This commit is contained in:
gitea committed 2026-11-01 21:55:45 +08:00
1 parent 1ee46fd79f
commit a0058c1c66
10 files changed
+380 -40

No files matched your search

@@ -0,0 +1,64 @@
import { describe, expect, it } from "bun:test";
import { hasReturnStatement, stripCommentsAndStrings, lintJavaScriptCode } from "./lint-js.js";
/**
* return 检测曾经只跳过整行注释,行尾注释、块注释内部行和字符串里的 "return"
* 都会让 missing-return 告警彻底失效。以下用例固定住这一修复。
*/
describe("JavaScript return detection", () => {
it("finds real return statements", () => {
expect(hasReturnStatement("return a + b;")).toBe(true);
expect(hasReturnStatement("const x = 1;\nif (x) { return x; }\nreturn 0;")).toBe(true);
});
it("ignores returns in trailing comments", () => {
expect(hasReturnStatement("const total = sum;\n// 记得 return 结果给下游")).toBe(false);
});
it("ignores returns inside block comments", () => {
expect(hasReturnStatement("/*\nreturn 1;\n*/\nprepare();")).toBe(false);
expect(hasReturnStatement("/* return 1 */ prepare();")).toBe(false);
});
it("ignores returns inside string literals", () => {
expect(hasReturnStatement('log("return value: ");')).toBe(false);
expect(hasReturnStatement("log('return value');")).toBe(false);
expect(hasReturnStatement("log(`return ${x}`);")).toBe(false);
});
it("does not treat identifiers containing return as a return", () => {
expect(hasReturnStatement("returnValue = 1;")).toBe(false);
expect(hasReturnStatement("const return_of_day = 3;")).toBe(false);
});
it("still detects a return that follows commented-out code", () => {
expect(hasReturnStatement("// return 1;\nreturn 2;")).toBe(true);
});
});
describe("stripCommentsAndStrings", () => {
it("keeps code characters and blanks out comment bodies", () => {
expect(stripCommentsAndStrings("// c\nkeep")).toBe(`${" ".repeat(4)}\nkeep`);
const blockStripped = stripCommentsAndStrings("/* x */ keep");
expect(blockStripped).toMatch(/^\s+keep$/);
});
it("handles escaped quotes inside strings", () => {
const stripped = stripCommentsAndStrings('"a\\"b" + x');
expect(stripped).not.toContain('"');
expect(stripped).toMatch(/\+\s*x$/);
});
});
describe("lintJavaScriptCode missing-return reporting", () => {
it("warns when only comments mention return", () => {
const { issues } = lintJavaScriptCode("// 记得 return 值\nnormalize(data);");
expect(issues.map((i) => i.rule)).toContain("missing-return");
});
it("stays quiet when a real return exists", () => {
const { issues, hasReturn } = lintJavaScriptCode("normalize(data);\nreturn data;");
expect(hasReturn).toBe(true);
expect(issues.map((i) => i.rule)).not.toContain("missing-return");
});
});