feat(workflow & platform): optimize complex workflow orchestration, code node debug summary, MVEL simulation and entity/feetype helpers
- feat(feetype): add --subtree and --ids-only flags to listFeeTypes for direct array export - feat(platform): add platform gen-props command to generate MVEL/JS entity field constants and templates - feat(workflow): show code node return value summaries in workflow test and job first screen - feat(workflow): add workflow node map sub-command and --mapping-file for deep declarative field mutations - feat(workflow): add executeMvelSandbox for local offline simulation of MVEL code nodes - test: add comprehensive test cases across feetype, platform gen-props, node map, and MVEL sandbox Closes #140
This commit is contained in:
1 parent
1739daacf2
commit
ad771e92e0
18 files changed
+591
-29
No files matched your search
@@ -1,5 +1,5 @@
|
||||
import { describe, it, expect } from "bun:test";
|
||||
import { flattenFeeTypeTree } from "./tools.js";
|
||||
import { flattenFeeTypeTree, collectSubtreeNodes } from "./tools.js";
|
||||
import { FeeTypeNode } from "./types.js";
|
||||
|
||||
describe("domains/feetype > tools & helpers", () => {
|
||||
@@ -71,4 +71,15 @@ describe("domains/feetype > tools & helpers", () => {
|
||||
expect(flat[3].parentName).toBe("");
|
||||
expect(flat[3].active).toBe(false);
|
||||
});
|
||||
|
||||
it("collectSubtreeNodes 正确收集指定子树及其全部后代", () => {
|
||||
const subtree = collectSubtreeNodes(sampleTree, "node_1");
|
||||
expect(subtree.map((s) => s.id)).toEqual(["node_1", "node_1_1", "node_1_1_1"]);
|
||||
|
||||
const leafSubtree = collectSubtreeNodes(sampleTree, "node_1_1_1");
|
||||
expect(leafSubtree.map((s) => s.id)).toEqual(["node_1_1_1"]);
|
||||
|
||||
const missingSubtree = collectSubtreeNodes(sampleTree, "not_exist");
|
||||
expect(missingSubtree).toEqual([]);
|
||||
});
|
||||
});
|
||||
@@ -60,6 +60,41 @@ export function flattenFeeTypeTree(
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 递归收集 parentId 节点及其所有子孙节点的 FlatFeeTypeItem
|
||||
*/
|
||||
export function collectSubtreeNodes(
|
||||
nodes: FeeTypeNode[],
|
||||
parentId: string,
|
||||
parentPath = "",
|
||||
parentName = ""
|
||||
): FlatFeeTypeItem[] {
|
||||
for (const node of nodes) {
|
||||
const item = node.feeType;
|
||||
if (!item || !item.id) continue;
|
||||
const currentPath = parentPath ? `${parentPath} / ${item.name}` : item.name;
|
||||
const children = Array.isArray(node.children) ? node.children : [];
|
||||
if (item.id === parentId || item.name === parentId || item.code.toLowerCase() === parentId.toLowerCase()) {
|
||||
const result: FlatFeeTypeItem[] = [];
|
||||
result.push({
|
||||
...item,
|
||||
path: currentPath,
|
||||
parentName,
|
||||
childrenCount: children.length,
|
||||
});
|
||||
if (children.length > 0) {
|
||||
result.push(...flattenFeeTypeTree(children, currentPath, item.name));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
if (children.length > 0) {
|
||||
const found = collectSubtreeNodes(children, parentId, currentPath, item.name);
|
||||
if (found.length > 0) return found;
|
||||
}
|
||||
}
|
||||
return [];
|
||||
}
|
||||
|
||||
/**
|
||||
* 清理组件字段 (参照易快报前端规范:删除内部状态与只读引用)
|
||||
*/
|
||||
@@ -127,12 +162,15 @@ export async function listFeeTypes(
|
||||
total: number;
|
||||
items: FlatFeeTypeItem[] | FeeTypeNode[];
|
||||
rawTree: FeeTypeNode[];
|
||||
ids: string[];
|
||||
}> {
|
||||
const rawTree = options.all
|
||||
? await getFullFeeTypeTree(client, corpId)
|
||||
: await getActiveFeeTypeTree(client, corpId);
|
||||
|
||||
let flat = flattenFeeTypeTree(rawTree);
|
||||
let flat = options.subtreeParentId
|
||||
? collectSubtreeNodes(rawTree, options.subtreeParentId)
|
||||
: flattenFeeTypeTree(rawTree);
|
||||
|
||||
if (options.keyword?.trim()) {
|
||||
const kw = options.keyword.trim().toLowerCase();
|
||||
@@ -145,10 +183,14 @@ export async function listFeeTypes(
|
||||
);
|
||||
}
|
||||
|
||||
const ids = flat.map((item) => item.id);
|
||||
const useFlat = options.flat || Boolean(options.subtreeParentId) || Boolean(options.idsOnly);
|
||||
|
||||
return {
|
||||
total: flat.length,
|
||||
items: options.flat ? flat : (options.keyword ? flat : rawTree),
|
||||
items: useFlat ? flat : (options.keyword ? flat : rawTree),
|
||||
rawTree,
|
||||
ids,
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -151,6 +151,8 @@ export interface FeeTypeQueryOptions {
|
||||
all?: boolean;
|
||||
keyword?: string;
|
||||
flat?: boolean;
|
||||
subtreeParentId?: string;
|
||||
idsOnly?: boolean;
|
||||
}
|
||||
|
||||
export interface FeeTypeView {
|
||||
|
||||
@@ -310,3 +310,45 @@ export function removeWorkflowTask(
|
||||
reindexTasks(cloned.tasks);
|
||||
return { workflow: cloned, removedTask };
|
||||
}
|
||||
|
||||
export type MappingContent = Array<Record<string, any>> | Record<string, any>;
|
||||
|
||||
/**
|
||||
* 批量映射节点字段配置 (支持数组格式替换 fields,或对象格式深度设置属性)
|
||||
*/
|
||||
export function applyNodeMapping(
|
||||
workflow: WorkflowDetail,
|
||||
targetKey: string | number,
|
||||
mapping: MappingContent
|
||||
): { workflow: WorkflowDetail; editedTask: WorkflowTask; changedKeys: string[] } {
|
||||
const cloned = JSON.parse(JSON.stringify(workflow)) as WorkflowDetail;
|
||||
if (!cloned.tasks) cloned.tasks = [];
|
||||
|
||||
const target = findWorkflowTask(cloned.tasks, targetKey);
|
||||
if (!target) {
|
||||
throw new NotFoundError(`未找到目标工作流任务节点: ${targetKey}`);
|
||||
}
|
||||
|
||||
const { task } = target;
|
||||
const changedKeys: string[] = [];
|
||||
|
||||
if (Array.isArray(mapping)) {
|
||||
// 数组格式直接更新/替换 fields 数组
|
||||
(task as any).fields = mapping;
|
||||
changedKeys.push(`fields (${mapping.length} 项)`);
|
||||
} else if (mapping && typeof mapping === "object") {
|
||||
// 对象格式:合并/设置每个顶层键
|
||||
for (const [key, value] of Object.entries(mapping)) {
|
||||
(task as any)[key] = value;
|
||||
if (Array.isArray(value)) {
|
||||
changedKeys.push(`${key} (${value.length} 项)`);
|
||||
} else {
|
||||
changedKeys.push(key);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
throw new ValidationError("映射内容必须为 JSON 数组或 JSON 对象");
|
||||
}
|
||||
|
||||
return { workflow: cloned, editedTask: task, changedKeys };
|
||||
}
|
||||
@@ -5,6 +5,7 @@ import {
|
||||
lintMvelCode,
|
||||
lintWorkflowScripts,
|
||||
executeJavaScriptSandbox,
|
||||
executeMvelSandbox,
|
||||
} from "./index.js";
|
||||
import type { WorkflowDetail } from "../../types.js";
|
||||
|
||||
@@ -275,4 +276,30 @@ describe("isolated script runtime boundaries", () => {
|
||||
expect((await executeJavaScriptSandbox('return Promise.resolve(1);')).success).toBe(false);
|
||||
expect((await executeJavaScriptSandbox('return 1;')).returnValue).toBe(1);
|
||||
});
|
||||
|
||||
describe("executeMvelSandbox", () => {
|
||||
it("executes basic MVEL variable assignments, collections and return statements", async () => {
|
||||
const mvel = [
|
||||
'String planCode = "AMORT-001";',
|
||||
'int periods = 3;',
|
||||
'Map row = new HashMap();',
|
||||
'row.put("code", planCode);',
|
||||
'row.put("periods", periods);',
|
||||
'return row;',
|
||||
].join("\n");
|
||||
const result = await executeMvelSandbox(mvel);
|
||||
expect(result.success).toBe(true);
|
||||
expect(result.returnValue).toEqual({ code: "AMORT-001", periods: 3 });
|
||||
});
|
||||
|
||||
it("accesses inputs in MVEL code", async () => {
|
||||
const mvel = [
|
||||
'int total = amount * 2;',
|
||||
'return total;',
|
||||
].join("\n");
|
||||
const result = await executeMvelSandbox(mvel, { amount: 50 });
|
||||
expect(result.success).toBe(true);
|
||||
expect(result.returnValue).toBe(100);
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -122,3 +122,54 @@ export async function executeJavaScriptSandbox(
|
||||
runtime?.dispose();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 转换 MVEL 常用语法为 QuickJS 可执行的 JavaScript,实现轻量仿真
|
||||
*/
|
||||
export function transpileMvelToJs(mvelCode: string): string {
|
||||
let js = mvelCode;
|
||||
|
||||
// 1. 移除 Java 类型声明:如 String x =, int y =, List z =, Map m =, boolean b =, double d =, long l =
|
||||
js = js.replace(/\b(String|int|Integer|long|Long|double|Double|float|Float|boolean|Boolean|List|Map|HashMap|ArrayList|Object)\s+([a-zA-Z_$][a-zA-Z0-9_$]*\s*=)/g, "var $2");
|
||||
js = js.replace(/\b(String|int|Integer|long|Long|double|Double|float|Float|boolean|Boolean|List|Map|HashMap|ArrayList|Object)\s+([a-zA-Z_$][a-zA-Z0-9_$]*\s*;)/g, "var $2");
|
||||
|
||||
// 2. 转换 new HashMap() / new ArrayList()
|
||||
js = js.replace(/new\s+(?:HashMap|Map)\s*(?:\([^)]*\)|<[^>]*>\s*\([^)]*\))/g, "({})");
|
||||
js = js.replace(/new\s+(?:ArrayList|List)\s*(?:\([^)]*\)|<[^>]*>\s*\([^)]*\))/g, "([])");
|
||||
|
||||
// 3. 常见 Map/List 方法调用模拟:
|
||||
// .put(k, v) -> [k] = v (包装为安全赋值辅助)
|
||||
// 注入轻量 helper
|
||||
const preamble = [
|
||||
"function _mvel_put(obj, key, val) { if (obj && typeof obj === 'object') { obj[key] = val; } return val; }",
|
||||
"function _mvel_get(obj, key) { return obj ? obj[key] : undefined; }",
|
||||
"function _mvel_size(obj) { if (!obj) return 0; if (Array.isArray(obj)) return obj.length; if (typeof obj === 'object') return Object.keys(obj).length; return 0; }",
|
||||
"function _mvel_add(arr, item) { if (Array.isArray(arr)) arr.push(item); return arr; }",
|
||||
"",
|
||||
].join("\n");
|
||||
|
||||
js = js.replace(/\.put\s*\(\s*([^,]+)\s*,\s*([^)]+)\s*\)/g, ""); // 避免直接正则冲突,使用下面逐行或更精确替换
|
||||
// 恢复并使用 helper
|
||||
js = mvelCode;
|
||||
js = js.replace(/\b(String|int|Integer|long|Long|double|Double|float|Float|boolean|Boolean|List|Map|HashMap|ArrayList|Object)\s+([a-zA-Z_$][a-zA-Z0-9_$]*\s*=)/g, "var $2");
|
||||
js = js.replace(/\b(String|int|Integer|long|Long|double|Double|float|Float|boolean|Boolean|List|Map|HashMap|ArrayList|Object)\s+([a-zA-Z_$][a-zA-Z0-9_$]*\s*;)/g, "var $2");
|
||||
js = js.replace(/new\s+(?:HashMap|Map)\s*(?:\([^)]*\)|<[^>]*>\s*\([^)]*\))/g, "({})");
|
||||
js = js.replace(/new\s+(?:ArrayList|List)\s*(?:\([^)]*\)|<[^>]*>\s*\([^)]*\))/g, "([])");
|
||||
js = js.replace(/\.put\s*\(\s*([^,]+)\s*,\s*([^)]+)\s*\)/g, "[$1] = $2");
|
||||
js = js.replace(/\.size\s*\(\s*\)/g, ".length");
|
||||
|
||||
return `${preamble}${js}`;
|
||||
}
|
||||
|
||||
/**
|
||||
* 轻量仿真执行 MVEL 节点
|
||||
*/
|
||||
export async function executeMvelSandbox(
|
||||
code: string,
|
||||
inputs: Record<string, unknown> = {},
|
||||
options: { timeoutMs?: number } = {}
|
||||
): Promise<ScriptEvalResult> {
|
||||
const transpiled = transpileMvelToJs(code);
|
||||
const result = await executeJavaScriptSandbox(transpiled, inputs, options);
|
||||
return result;
|
||||
}
|
||||
Reference in new issue
Block a user