fix: fix the tests by simulating user actions rather than the function, and avoid the cyclic dependancy which was present

ref: N25B-371
This commit is contained in:
Björn Otgaar
2025-12-04 12:33:27 +01:00
parent c167144b4d
commit 95397ceccc
5 changed files with 111 additions and 51 deletions

View File

@@ -79,7 +79,6 @@ export const NodeConnects = {
export const NodeDeletes = {
start: () => false,
end: () => false,
test: () => false, // Used for coverage of universal/ undefined nodes
}
/**
@@ -92,5 +91,4 @@ export const NodesInPhase = {
start: () => false,
end: () => false,
phase: () => false,
test: () => false, // Used for coverage of universal/ undefined nodes
}

View File

@@ -1,9 +1,9 @@
import { useDraggable } from '@neodrag/react';
import { useReactFlow, type XYPosition } from '@xyflow/react';
import { type ReactNode, useCallback, useRef, useState } from 'react';
import useFlowStore from '../VisProgStores';
import styles from '../../VisProg.module.css';
import { NodeDefaults, type NodeTypes } from '../NodeRegistry'
import addNode from '../utils/AddNode';
/**
* Props for a draggable node within the drag-and-drop toolbar.
@@ -57,6 +57,46 @@ function DraggableNode({ className, children, nodeType, onDrop }: DraggableNodeP
);
}
/**
* Adds a new node to the flow graph.
*
* Handles:
* - Automatic node ID generation based on existing nodes of the same type.
* - Loading of default data from the `NodeDefaults` registry.
* - Integration with the flow store to update global node state.
*
* @param nodeType - The type of node to create (from `NodeTypes`).
* @param position - The XY position in the flow canvas where the node will appear.
*/
function addNode(nodeType: keyof typeof NodeTypes, position: XYPosition) {
const { nodes, setNodes } = useFlowStore.getState();
// Load any predefined data for this node type.
const defaultData = NodeDefaults[nodeType] ?? {}
// Currently, we find out what the Id is by checking the last node and adding one.
const sameTypeNodes = nodes.filter((node) => node.type === nodeType);
const nextNumber =
sameTypeNodes.length > 0
? (() => {
const lastNode = sameTypeNodes[sameTypeNodes.length - 1];
const parts = lastNode.id.split('-');
const lastNum = Number(parts[1]);
return Number.isNaN(lastNum) ? sameTypeNodes.length + 1 : lastNum + 1;
})()
: 1;
const id = `${nodeType}-${nextNumber}`;
// Create new node
const newNode = {
id: id,
type: nodeType,
position,
data: JSON.parse(JSON.stringify(defaultData))
}
setNodes([...nodes, newNode]);
}
/**
* The drag-and-drop toolbar component for the visual programming interface.
*

View File

@@ -110,7 +110,6 @@ export function PhaseReduce(node: Node, nodes: Node[]) {
* @param isThisSource whether this instance of the node was the source in the connection, true = yes.
*/
export function PhaseConnects(thisNode: Node, otherNode: Node, isThisSource: boolean) {
console.log("Connect functionality called.")
const node = thisNode as PhaseNode
const data = node.data as PhaseNodeData
if (!isThisSource)

View File

@@ -1,39 +0,0 @@
import type { XYPosition } from "@xyflow/react";
import { NodeDefaults, type NodeTypes } from "../NodeRegistry";
import useFlowStore from "../VisProgStores";
/**
* addNode — adds a new node to the flow using the unified class-based system.
* Keeps numbering logic for phase/norm nodes.
*/
export default function addNode(nodeType: keyof typeof NodeTypes, position: XYPosition) {
const { nodes, setNodes } = useFlowStore.getState();
// Find out if there's any default data about our ndoe
const defaultData = NodeDefaults[nodeType] ?? {}
// Currently, we find out what the Id is by checking the last node and adding one
const sameTypeNodes = nodes.filter((node) => node.type === nodeType);
const nextNumber =
sameTypeNodes.length > 0
? (() => {
const lastNode = sameTypeNodes[sameTypeNodes.length - 1];
const parts = lastNode.id.split('-');
const lastNum = Number(parts[1]);
return Number.isNaN(lastNum) ? sameTypeNodes.length + 1 : lastNum + 1;
})()
: 1;
const id = `${nodeType}-${nextNumber}`;
// Create new node
const newNode = {
id: id,
type: nodeType,
position,
// Deep copy using JSON because thats how things work:
// Ref: https://developer.mozilla.org/en-US/docs/Glossary/Deep_copy
data: JSON.parse(JSON.stringify(defaultData))
}
setNodes([...nodes, newNode]);
}