Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions src/lib/__tests__/graphUtils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ describe("graph traversal helpers", () => {
expect(affectedEdges).toHaveLength(4);
});

it("detects cycles and marks nodes with errors", async () => {
it("detects cycles and returns true without mutating input nodes", async () => {
const { checkForCyclesAndMarkErrors } = await loadGraphUtils();
const nodes = [createNode("x"), createNode("y")];
nodes.forEach((n) => (n.data.dirty = false));
Expand All @@ -217,9 +217,10 @@ describe("graph traversal helpers", () => {
const hasCycle = checkForCyclesAndMarkErrors(nodes, edges);

expect(hasCycle).toBe(true);
// input nodes must not be mutated – caller owns state updates
nodes.forEach((node) => {
expect(node.data.error).toBe(true);
expect(node.data.extendedError).toMatch(/Cycle detected/);
expect(node.data.error).toBeUndefined();
expect(node.data.extendedError).toBeUndefined();
});
});
});
Expand Down
14 changes: 3 additions & 11 deletions src/lib/graphUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -448,7 +448,7 @@ export function mergePartialResultsIntoFullGraph(
* Flags cycles instantly so the user doesn’t have to wait for the API
* to complain.
*
* @returns `true` if a cycle was found (and nodes were marked with errors)
* @returns `true` if a cycle was found
*/
export function checkForCyclesAndMarkErrors(
affectedNodes: Node<CalculationNodeData>[],
Expand Down Expand Up @@ -479,14 +479,6 @@ export function checkForCyclesAndMarkErrors(
}
}

/* --- mark error if not all processed --------------------------------------- */
const hasCycle = processed !== affectedNodes.length;
if (hasCycle) {
affectedNodes.forEach((n) => {
n.data.error = true;
n.data.extendedError =
"Cycle detected in this sub-graph – calculation aborted.";
});
}
return hasCycle;
/* --- detect cycle if not all nodes were processed -------------------------- */
return processed !== affectedNodes.length;
}