fix: harden CLI parsing, MCP startup and release integrity
This commit is contained in:
1 parent
f0902f5f20
commit
103ead0fe5
53 files changed
+3713
-1945
No files matched your search
@@ -0,0 +1,226 @@
|
||||
import { describe, expect, it } from "bun:test";
|
||||
import {
|
||||
hasReturnStatement,
|
||||
lintJavaScriptCode,
|
||||
lintMvelCode,
|
||||
lintWorkflowScripts,
|
||||
executeJavaScriptSandbox,
|
||||
} from "./index.js";
|
||||
import type { WorkflowDetail } from "../../types.js";
|
||||
|
||||
describe("workflow-script - 本地静态语法校验与沙箱仿真引擎", () => {
|
||||
describe("hasReturnStatement", () => {
|
||||
it("identifies return statement correctly", () => {
|
||||
expect(hasReturnStatement("const x = 1;\nreturn x + 2;")).toBe(true);
|
||||
expect(hasReturnStatement("// return 123;\nconst x = 1;")).toBe(false);
|
||||
expect(hasReturnStatement("/*\n * return false;\n */\nconst y = 2;")).toBe(false);
|
||||
expect(hasReturnStatement("const x = 'returning';")).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe("lintJavaScriptCode", () => {
|
||||
it("reports error for empty script", () => {
|
||||
const res = lintJavaScriptCode(" ");
|
||||
expect(res.issues.some((i) => i.rule === "empty-script")).toBe(true);
|
||||
expect(res.hasReturn).toBe(false);
|
||||
});
|
||||
|
||||
it("warns about missing return statement", () => {
|
||||
const res = lintJavaScriptCode("const a = 1; const b = 2;");
|
||||
expect(res.issues.some((i) => i.rule === "missing-return")).toBe(true);
|
||||
expect(res.hasReturn).toBe(false);
|
||||
});
|
||||
|
||||
it("catches syntax errors with line numbers", () => {
|
||||
const res = lintJavaScriptCode("const a = 1;\nconst b = ;\nreturn a + b;");
|
||||
expect(res.issues.some((i) => i.rule === "syntax-error" && i.level === "error")).toBe(true);
|
||||
});
|
||||
|
||||
it("accepts valid JavaScript with declared bound variables", () => {
|
||||
const code = `
|
||||
const filtered = items.filter(x => x.amount > 0);
|
||||
return { count: filtered.length, items: filtered };
|
||||
`;
|
||||
const res = lintJavaScriptCode(code, ["items"]);
|
||||
expect(res.issues).toHaveLength(0);
|
||||
expect(res.hasReturn).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe("lintMvelCode", () => {
|
||||
it("detects unclosed brackets in MVEL code", () => {
|
||||
const code = "var a = HashMap();\na.put('key', 123;";
|
||||
const res = lintMvelCode(code);
|
||||
expect(res.issues.some((i) => i.rule === "unclosed-bracket")).toBe(true);
|
||||
});
|
||||
|
||||
it("detects mismatched brackets", () => {
|
||||
const code = "var a = (1 + 2]; return a;";
|
||||
const res = lintMvelCode(code);
|
||||
expect(res.issues.some((i) => i.rule === "unmatched-brackets")).toBe(true);
|
||||
});
|
||||
|
||||
it("flags dangling node references if node does not exist in workflow", () => {
|
||||
const code = `
|
||||
var upstream = action_non_existent;
|
||||
var r = new HashMap();
|
||||
r.put("data", upstream);
|
||||
return r;
|
||||
`;
|
||||
const existing = new Set(["trigger_webhook", "action_calc"]);
|
||||
const res = lintMvelCode(code, existing);
|
||||
expect(res.issues.some((i) => i.rule === "dangling-node-reference")).toBe(true);
|
||||
expect(res.referencedNodes).toHaveLength(1);
|
||||
expect(res.referencedNodes[0].name).toBe("action_non_existent");
|
||||
expect(res.referencedNodes[0].exists).toBe(false);
|
||||
});
|
||||
|
||||
it("passes clean MVEL code referencing existing nodes", () => {
|
||||
const code = `
|
||||
import java.util.HashMap;
|
||||
var upstream = action_calc;
|
||||
var result = new HashMap();
|
||||
result.put("val", upstream);
|
||||
return result;
|
||||
`;
|
||||
const existing = new Set(["trigger_webhook", "action_calc"]);
|
||||
const res = lintMvelCode(code, existing);
|
||||
expect(res.issues).toHaveLength(0);
|
||||
expect(res.referencedNodes[0].exists).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe("lintWorkflowScripts", () => {
|
||||
it("scans and reports issues across all workflow code nodes", () => {
|
||||
const mockWorkflow = {
|
||||
id: "pipe_lint_test",
|
||||
label: "测试流程",
|
||||
state: 0,
|
||||
tasks: [
|
||||
{
|
||||
name: "trigger_1",
|
||||
type: "webhook",
|
||||
label: "触发器",
|
||||
groupName: "trigger",
|
||||
orderIndex: 0,
|
||||
levelIndex: [0],
|
||||
status: "active",
|
||||
},
|
||||
{
|
||||
name: "action_js_bad",
|
||||
type: "code-js2",
|
||||
label: "语法错误JS",
|
||||
groupName: "action",
|
||||
orderIndex: 1,
|
||||
levelIndex: [1],
|
||||
status: "active",
|
||||
code: "const x = ; return x;",
|
||||
mapping: [],
|
||||
},
|
||||
{
|
||||
name: "action_mvel_good",
|
||||
type: "code",
|
||||
label: "正常MVEL",
|
||||
groupName: "action",
|
||||
orderIndex: 2,
|
||||
levelIndex: [2],
|
||||
status: "active",
|
||||
code: "var a = trigger_1; return a;",
|
||||
},
|
||||
],
|
||||
} as unknown as WorkflowDetail;
|
||||
|
||||
const report = lintWorkflowScripts(mockWorkflow);
|
||||
expect(report.totalCodeNodes).toBe(2);
|
||||
expect(report.validCount).toBe(1);
|
||||
expect(report.invalidCount).toBe(1);
|
||||
expect(report.allValid).toBe(false);
|
||||
|
||||
const badReport = report.reports.find((r) => r.nodeName === "action_js_bad");
|
||||
expect(badReport?.valid).toBe(false);
|
||||
expect(badReport?.issues.some((i) => i.rule === "syntax-error")).toBe(true);
|
||||
|
||||
const goodReport = report.reports.find((r) => r.nodeName === "action_mvel_good");
|
||||
expect(goodReport?.valid).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe("executeJavaScriptSandbox", () => {
|
||||
it("executes code in sandbox and returns computed values", async () => {
|
||||
const code = `
|
||||
console.log("Processing records:", items.length);
|
||||
const total = items.reduce((sum, item) => sum + item.amount, 0);
|
||||
return { total, count: items.length };
|
||||
`;
|
||||
const inputs = {
|
||||
items: [{ amount: 100 }, { amount: 250 }, { amount: 50 }],
|
||||
};
|
||||
|
||||
const res = await executeJavaScriptSandbox(code, inputs);
|
||||
expect(res.success).toBe(true);
|
||||
expect(res.returnValue).toEqual({ total: 400, count: 3 });
|
||||
expect(res.logs).toHaveLength(1);
|
||||
expect(res.logs[0].message).toContain("Processing records: 3");
|
||||
expect(res.durationMs).toBeGreaterThanOrEqual(0);
|
||||
});
|
||||
|
||||
it("catches runtime errors and reports line number", async () => {
|
||||
const code = `
|
||||
const a = 10;
|
||||
const b = undefined;
|
||||
return b.nonExistentField.subField;
|
||||
`;
|
||||
|
||||
const res = await executeJavaScriptSandbox(code, {});
|
||||
expect(res.success).toBe(false);
|
||||
expect(res.error).toBeDefined();
|
||||
expect(res.error?.name).toBe("TypeError");
|
||||
expect(res.error?.message).toContain("undefined");
|
||||
expect(res.error?.line).toBe(4);
|
||||
});
|
||||
|
||||
it("terminates on infinite loops via timeout", async () => {
|
||||
const code = "while (true) {}";
|
||||
const res = await executeJavaScriptSandbox(code, {}, { timeoutMs: 100 });
|
||||
expect(res.success).toBe(false);
|
||||
expect(res.error?.message).toContain("timed out");
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe("MVEL lexical boundaries", () => {
|
||||
for (const code of [
|
||||
'return "{";',
|
||||
'return wrap("https://example.test/path");',
|
||||
'/* { action_missing */\nreturn 1;',
|
||||
'return "action_missing";',
|
||||
"return 'escaped \\' { action_missing';",
|
||||
'/* comment with "quotes" and // */\r\nreturn [1, 2]; // { action_missing',
|
||||
]) {
|
||||
it(`ignores literals and comments in ${JSON.stringify(code)}`, () => {
|
||||
const result = lintMvelCode(code, new Set());
|
||||
expect(result.issues).toEqual([]);
|
||||
expect(result.referencedNodes).toEqual([]);
|
||||
expect(result.hasReturn).toBe(true);
|
||||
});
|
||||
}
|
||||
|
||||
it("does not count quoted or commented return as executable code", () => {
|
||||
const result = lintMvelCode('value = "return"; /* return 1; */ // return 2;');
|
||||
expect(result.hasReturn).toBe(false);
|
||||
expect(result.issues.map(i => i.rule)).toEqual(["missing-return"]);
|
||||
});
|
||||
|
||||
it("keeps real references and error coordinates while diagnosing unfinished literals", () => {
|
||||
const result = lintMvelCode('// ]\nreturn action_missing;]', new Set());
|
||||
expect(result.issues.find(i => i.rule === "unmatched-brackets")).toMatchObject({ line: 2, column: 23 });
|
||||
expect(result.issues.find(i => i.rule === "dangling-node-reference")?.level).toBe("error");
|
||||
expect(lintMvelCode('return "unfinished').issues.some(i => i.rule === "unterminated-string")).toBe(true);
|
||||
expect(lintMvelCode('return 1;\n /* unfinished').issues.find(i => i.rule === "unterminated-comment")).toMatchObject({ line: 2, column: 3 });
|
||||
});
|
||||
|
||||
it("does not invalidate an entire workflow for literal-only node names", () => {
|
||||
const result = lintWorkflowScripts({ id: "lexical", tasks: [{ name: "action_code", type: "code", code: 'return "action_missing {";' }] } as unknown as WorkflowDetail);
|
||||
expect(result.allValid).toBe(true);
|
||||
});
|
||||
});
|
||||
@@ -1,6 +1,62 @@
|
||||
import type { WorkflowTask } from "../../types.js";
|
||||
import type { ScriptIssue } from "./types.js";
|
||||
import { hasReturnStatement } from "./lint-js.js";
|
||||
|
||||
/** Mask literals/comments without moving offsets used by diagnostics. */
|
||||
function scanMvelCode(code: string, issues: ScriptIssue[]): string {
|
||||
const masked = code.split("");
|
||||
let mode: "code" | "line" | "block" | "'" | '"' = "code";
|
||||
let start = { line: 1, column: 1 };
|
||||
let line = 1;
|
||||
let column = 1;
|
||||
let escaped = false;
|
||||
|
||||
for (let i = 0; i < code.length; i++) {
|
||||
const ch = code[i];
|
||||
const next = code[i + 1];
|
||||
if (mode === "code") {
|
||||
if (ch === "'" || ch === '"') {
|
||||
mode = ch;
|
||||
start = { line, column };
|
||||
masked[i] = " ";
|
||||
} else if (ch === "/" && (next === "/" || next === "*")) {
|
||||
mode = next === "/" ? "line" : "block";
|
||||
start = { line, column };
|
||||
masked[i] = masked[i + 1] = " ";
|
||||
i++;
|
||||
column++;
|
||||
}
|
||||
} else {
|
||||
if (ch !== "\n" && ch !== "\r") masked[i] = " ";
|
||||
if (mode === "line") {
|
||||
if (ch === "\n" || ch === "\r") mode = "code";
|
||||
} else if (mode === "block") {
|
||||
if (ch === "*" && next === "/") {
|
||||
masked[++i] = " ";
|
||||
column++;
|
||||
mode = "code";
|
||||
}
|
||||
} else if (escaped) {
|
||||
escaped = false;
|
||||
} else if (ch === "\\") {
|
||||
escaped = true;
|
||||
} else if (ch === mode) {
|
||||
mode = "code";
|
||||
}
|
||||
}
|
||||
if (ch === "\n") { line++; column = 1; }
|
||||
else column++;
|
||||
}
|
||||
|
||||
if (mode === "block" || mode === "'" || mode === '"') {
|
||||
issues.push({
|
||||
level: "error",
|
||||
...start,
|
||||
rule: mode === "block" ? "unterminated-comment" : "unterminated-string",
|
||||
message: mode === "block" ? "未闭合的块注释" : `未闭合的字符串: ${mode}`,
|
||||
});
|
||||
}
|
||||
return masked.join("");
|
||||
}
|
||||
|
||||
/**
|
||||
* 静态检查 MVEL (code) 括号闭合与上游节点引用有效性
|
||||
@@ -26,7 +82,8 @@ export function lintMvelCode(
|
||||
return { issues, referencedNodes, hasReturn: false };
|
||||
}
|
||||
|
||||
const hasRet = hasReturnStatement(code);
|
||||
const executableCode = scanMvelCode(code, issues);
|
||||
const hasRet = /\breturn\b/.test(executableCode);
|
||||
if (!hasRet) {
|
||||
issues.push({
|
||||
level: "warning",
|
||||
@@ -37,12 +94,12 @@ export function lintMvelCode(
|
||||
|
||||
// 1. 括号与引号对称性平衡检查
|
||||
const stack: Array<{ char: string; line: number }> = [];
|
||||
const lines = code.split(/\r?\n/);
|
||||
const lines = executableCode.split(/\r?\n/);
|
||||
|
||||
for (let l = 0; l < lines.length; l++) {
|
||||
const lineText = lines[l];
|
||||
// 忽略单行注释
|
||||
const cleanLine = lineText.replace(/\/\/.*$/, "");
|
||||
// 忽略单行注释,以及扫描器已屏蔽的字符串与块注释。
|
||||
const cleanLine = lineText;
|
||||
|
||||
for (let c = 0; c < cleanLine.length; c++) {
|
||||
const ch = cleanLine[c];
|
||||
@@ -80,7 +137,7 @@ export function lintMvelCode(
|
||||
const foundRefs = new Set<string>();
|
||||
let match: RegExpExecArray | null;
|
||||
|
||||
while ((match = nodeRefRegex.exec(code)) !== null) {
|
||||
while ((match = nodeRefRegex.exec(executableCode)) !== null) {
|
||||
foundRefs.add(match[1]);
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user