Skip to content

Commit 657e21f

Browse files
committed
feat: refactor flow validation tests and update NodeFunctionIdWrapper to SubFlowValue
1 parent d3cd952 commit 657e21f

2 files changed

Lines changed: 41 additions & 41 deletions

File tree

src/utils.ts

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ import {
44
Flow,
55
FunctionDefinition,
66
NodeFunction,
7-
NodeFunctionIdWrapper,
7+
SubFlowValue,
88
NodeParameter,
9-
ReferenceValue
9+
ReferenceValue, Maybe
1010
} from "@code0-tech/sagittarius-graphql-types";
1111
import ts from "typescript";
1212
import {createSystem, createVirtualTypeScriptEnvironment, VirtualTypeScriptEnvironment} from "@typescript/vfs"
@@ -109,20 +109,20 @@ export function generateFlowSourceCode(
109109
): string {
110110
const nodes = flow?.nodes?.nodes || [];
111111
const funcMap = new Map(functions?.map(f => [f.identifier, f]));
112-
const visited = new Set<string>();
112+
const visited = new Set<NodeFunction['id'] | FunctionDefinition['id']>();
113113

114-
const generateNodeCode = (nodeId: string, indent: string = ""): string => {
115-
const node = nodes.find(n => n?.id === nodeId);
114+
const generateNodeCode = (id: NodeFunction['id'] | FunctionDefinition['id'], indent: string = ""): string => {
115+
const node = nodes.find(n => n?.id === id);
116116
if (!node || !node.functionDefinition) return "";
117-
visited.add(nodeId);
117+
visited.add(id);
118118

119119
const funcDef = funcMap.get(node.functionDefinition.identifier);
120120
if (!funcDef) return `${indent}// Error: Function ${node.functionDefinition.identifier} not found\n`;
121121

122122
const params = (node.parameters?.nodes as NodeParameter[]) || [];
123123
const args = params.map((p, index) => {
124124
const val = p.value;
125-
if (!val) return isForInference ? `/* @pos ${nodeId} ${index} */ {}` : `/* @pos ${nodeId} ${index} */ undefined`;
125+
if (!val) return isForInference ? `/* @pos ${id} ${index} */ {}` : `/* @pos ${id} ${index} */ undefined`;
126126
if (val.__typename === "ReferenceValue") {
127127
const ref = val as ReferenceValue;
128128
let refCode = typeof ref.inputIndex === "number"
@@ -131,19 +131,19 @@ export function generateFlowSourceCode(
131131
ref.referencePath?.forEach(pathObj => {
132132
refCode += `?.${pathObj.path}`;
133133
});
134-
return `/* @pos ${nodeId} ${index} */ ${refCode}`;
134+
return `/* @pos ${id} ${index} */ ${refCode}`;
135135
}
136136
if (val.__typename === "LiteralValue") {
137137
const jsonString = stringify(val?.value)
138-
return `/* @pos ${nodeId} ${index} */ ${jsonString}`;
138+
return `/* @pos ${id} ${index} */ ${jsonString}`;
139139
}
140-
if (val.__typename === "NodeFunctionIdWrapper") {
141-
const wrapper = val as NodeFunctionIdWrapper;
142-
const lambdaArgName = `p_${sanitizeId(nodeId)}_${index}`;
143-
const subTreeCode = generateNodeCode(wrapper.id!, indent + " ");
144-
return `/* @pos ${nodeId} ${index} */ (...${lambdaArgName}) => {\n${subTreeCode}${indent}}`;
140+
if (val.__typename === "SubFlowValue") {
141+
const wrapper = val as SubFlowValue;
142+
const lambdaArgName = `p_${sanitizeId(id as string)}_${index}`;
143+
const subTreeCode = generateNodeCode(wrapper.startingNodeId || wrapper.functionDefinition?.id!, indent + " ");
144+
return `/* @pos ${id} ${index} */ (...${lambdaArgName}) => {\n${subTreeCode}${indent}}`;
145145
}
146-
return isForInference ? `/* @pos ${nodeId} ${index} */ {}` : `/* @pos ${nodeId} ${index} */ undefined`;
146+
return isForInference ? `/* @pos ${id} ${index} */ {}` : `/* @pos ${id} ${index} */ undefined`;
147147
});
148148

149149
const varName = `node_${sanitizeId(node.id!)}`;
@@ -155,21 +155,21 @@ export function generateFlowSourceCode(
155155
let code = `${indent}`;
156156

157157
if (node.functionDefinition.identifier === "std::control::return") {
158-
code += `return /* @pos ${nodeId} null */ ${funcName}(${args.join(", ")})${needsAnyCast ? "" : ""} ;\n`
158+
code += `return /* @pos ${id} null */ ${funcName}(${args.join(", ")})${needsAnyCast ? "" : ""} ;\n`
159159
} else if (node.functionDefinition.identifier === "std::control::if") {
160-
code += `const ${varName} = /* @pos ${nodeId} null */ ${funcName}(${args.join(", ")})${needsAnyCast ? "" : ""} ;\n`
160+
code += `const ${varName} = /* @pos ${id} null */ ${funcName}(${args.join(", ")})${needsAnyCast ? "" : ""} ;\n`
161161
code += `if(${args[0]}) {
162-
${generateNodeCode((node.parameters?.nodes?.[1]?.value as NodeFunctionIdWrapper)?.id!, indent + " ")}
162+
${generateNodeCode(((node.parameters?.nodes?.[1]?.value as SubFlowValue)?.startingNodeId || (node.parameters?.nodes?.[1]?.value as SubFlowValue)?.functionDefinition?.id), indent + " ")}
163163
}`
164164
} else if (node.functionDefinition.identifier === "std::control::if_else") {
165-
code += `const ${varName} = /* @pos ${nodeId} null */ ${funcName}(${args.join(", ")})${needsAnyCast ? "" : ""} ;\n`
165+
code += `const ${varName} = /* @pos ${id} null */ ${funcName}(${args.join(", ")})${needsAnyCast ? "" : ""} ;\n`
166166
code += `if(${args[0]}) {
167-
${generateNodeCode((node.parameters?.nodes?.[1]?.value as NodeFunctionIdWrapper)?.id!, indent + " ")}
167+
${generateNodeCode(((node.parameters?.nodes?.[1]?.value as SubFlowValue)?.startingNodeId || (node.parameters?.nodes?.[1]?.value as SubFlowValue)?.functionDefinition?.id), indent + " ")}
168168
} else {
169-
${generateNodeCode((node.parameters?.nodes?.[2]?.value as NodeFunctionIdWrapper)?.id!, indent + " ")}
169+
${generateNodeCode(((node.parameters?.nodes?.[2]?.value as SubFlowValue)?.startingNodeId || (node.parameters?.nodes?.[2]?.value as SubFlowValue)?.functionDefinition?.id), indent + " ")}
170170
}`
171171
} else {
172-
code += `const ${varName} = /* @pos ${nodeId} null */ ${funcName}(${args.join(", ")})${needsAnyCast ? "" : ""} ;\n`
172+
code += `const ${varName} = /* @pos ${id} null */ ${funcName}(${args.join(", ")})${needsAnyCast ? "" : ""} ;\n`
173173
}
174174

175175
if (node.nextNodeId) code += generateNodeCode(node.nextNodeId, indent);
@@ -181,9 +181,9 @@ export function generateFlowSourceCode(
181181
const funcDeclarations = functions?.map(f => `declare function fn_${f.identifier?.replace(/::/g, '_')}${f.signature}`).join('\n');
182182

183183
const nextNodeIds = new Set(nodes.map(n => n?.nextNodeId).filter(id => !!id));
184-
const subTreeIds = new Set<string>();
185-
nodes.forEach(n => n?.parameters?.nodes?.forEach((p: any) => {
186-
if (p?.value?.__typename === "NodeFunctionIdWrapper" && p.value.id) subTreeIds.add(p.value.id);
184+
const subTreeIds = new Set<NodeFunction['id'] | FunctionDefinition['id']>();
185+
nodes.forEach(n => n?.parameters?.nodes?.forEach((p: Maybe<NodeParameter>) => {
186+
if (p?.value?.__typename === "SubFlowValue" && (p.value.startingNodeId || p.value.functionDefinition?.id)) subTreeIds.add(p.value.startingNodeId || p.value.functionDefinition?.id);
187187
}));
188188

189189
const flowCode = flow ? `const flow_${sanitizeId(flow.id ?? "")} = /* @pos null null */ flow(${flow.settings?.nodes?.map((setting, index) => `/* @pos null ${index} */ ${stringify(setting?.value)}`).join(", ") ?? ""});` : ""

test/flowValidation.test.ts

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ describe('getFlowValidation - Integrationstest', () => {
3535
},
3636
{
3737
value: {
38-
__typename: "NodeFunctionIdWrapper",
39-
id: "gid://sagittarius/NodeFunction/3"
38+
__typename: "SubFlowValue",
39+
startingNodeId: "gid://sagittarius/NodeFunction/3"
4040
}
4141
}
4242
]
@@ -234,8 +234,8 @@ describe('getFlowValidation - Integrationstest', () => {
234234
},
235235
{
236236
value: {
237-
__typename: "NodeFunctionIdWrapper",
238-
id: "gid://sagittarius/NodeFunction/1"
237+
__typename: "SubFlowValue",
238+
startingNodeId: "gid://sagittarius/NodeFunction/1"
239239
}
240240
}
241241
]
@@ -284,8 +284,8 @@ describe('getFlowValidation - Integrationstest', () => {
284284
"identifier": "value"
285285
},
286286
"value": {
287-
"id": "gid://sagittarius/NodeFunction/2",
288-
"__typename": "NodeFunctionIdWrapper"
287+
"startingNodeId": "gid://sagittarius/NodeFunction/2",
288+
"__typename": "SubFlowValue"
289289
}
290290
}
291291
]
@@ -434,8 +434,8 @@ describe('getFlowValidation - Integrationstest', () => {
434434
"identifier": "first"
435435
},
436436
"value": {
437-
"id": "gid://sagittarius/NodeFunction/4",
438-
"__typename": "NodeFunctionIdWrapper"
437+
"startingNodeId": "gid://sagittarius/NodeFunction/4",
438+
"__typename": "SubFlowValue"
439439
}
440440
},
441441
{
@@ -843,8 +843,8 @@ describe('getFlowValidation - Integrationstest', () => {
843843
"identifier": "runnable"
844844
},
845845
"value": {
846-
"id": "gid://sagittarius/NodeFunction/4",
847-
"__typename": "NodeFunctionIdWrapper"
846+
"startingNodeId": "gid://sagittarius/NodeFunction/4",
847+
"__typename": "SubFlowValue"
848848
}
849849
}
850850
]
@@ -1013,8 +1013,8 @@ describe('getFlowValidation - Integrationstest', () => {
10131013
"updatedAt": "2026-04-13T19:45:26Z"
10141014
},
10151015
"value": {
1016-
"__typename": "NodeFunctionIdWrapper",
1017-
"id": "gid://sagittarius/NodeFunction/19"
1016+
"__typename": "SubFlowValue",
1017+
"startingNodeId": "gid://sagittarius/NodeFunction/19"
10181018
}
10191019
},
10201020
{
@@ -1030,8 +1030,8 @@ describe('getFlowValidation - Integrationstest', () => {
10301030
"updatedAt": "2026-04-13T19:45:26Z"
10311031
},
10321032
"value": {
1033-
"__typename": "NodeFunctionIdWrapper",
1034-
"id": "gid://sagittarius/NodeFunction/17"
1033+
"__typename": "SubFlowValue",
1034+
"startingNodeId": "gid://sagittarius/NodeFunction/17"
10351035
}
10361036
}
10371037
],
@@ -1613,8 +1613,8 @@ describe('getFlowValidation - Integrationstest', () => {
16131613
},
16141614
{
16151615
value: {
1616-
__typename: "NodeFunctionIdWrapper",
1617-
id: "gid://sagittarius/NodeFunction/2"
1616+
__typename: "SubFlowValue",
1617+
startingNodeId: "gid://sagittarius/NodeFunction/2"
16181618
}
16191619
}
16201620
]

0 commit comments

Comments
 (0)