@mindlogic-ai/logician-ui 4.0.0-alpha.36 → 4.0.0-alpha.38

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (98) hide show
  1. package/USAGE.md +17 -0
  2. package/dist/components/Form/FormSection/CollapsibleFormSection.js +1 -0
  3. package/dist/components/Form/FormSection/CollapsibleFormSection.js.map +1 -1
  4. package/dist/components/Form/FormSection/CollapsibleFormSection.mjs +1 -0
  5. package/dist/components/Form/FormSection/CollapsibleFormSection.mjs.map +1 -1
  6. package/dist/components/Toast/Toast.d.ts +7 -3
  7. package/dist/components/Toast/Toast.d.ts.map +1 -1
  8. package/dist/components/Toast/Toast.js +7 -3
  9. package/dist/components/Toast/Toast.js.map +1 -1
  10. package/dist/components/Toast/Toast.mjs +8 -4
  11. package/dist/components/Toast/Toast.mjs.map +1 -1
  12. package/dist/components/Toast/Toast.styles.d.ts +8 -0
  13. package/dist/components/Toast/Toast.styles.d.ts.map +1 -1
  14. package/dist/components/Toast/Toast.styles.js +59 -0
  15. package/dist/components/Toast/Toast.styles.js.map +1 -1
  16. package/dist/components/Toast/Toast.styles.mjs +57 -1
  17. package/dist/components/Toast/Toast.styles.mjs.map +1 -1
  18. package/dist/components/Toast/Toast.types.d.ts +25 -1
  19. package/dist/components/Toast/Toast.types.d.ts.map +1 -1
  20. package/dist/components/Toast/Toaster.d.ts.map +1 -1
  21. package/dist/components/Toast/Toaster.js +9 -1
  22. package/dist/components/Toast/Toaster.js.map +1 -1
  23. package/dist/components/Toast/Toaster.mjs +9 -1
  24. package/dist/components/Toast/Toaster.mjs.map +1 -1
  25. package/dist/components/Toast/useToast.d.ts +1 -1
  26. package/dist/components/Toast/useToast.d.ts.map +1 -1
  27. package/dist/components/Toast/useToast.js +9 -1
  28. package/dist/components/Toast/useToast.js.map +1 -1
  29. package/dist/components/Toast/useToast.mjs +10 -2
  30. package/dist/components/Toast/useToast.mjs.map +1 -1
  31. package/dist/components/Workflow/Workflow.d.ts +1 -1
  32. package/dist/components/Workflow/Workflow.d.ts.map +1 -1
  33. package/dist/components/Workflow/Workflow.js +2 -2
  34. package/dist/components/Workflow/Workflow.js.map +1 -1
  35. package/dist/components/Workflow/Workflow.mjs +2 -2
  36. package/dist/components/Workflow/Workflow.mjs.map +1 -1
  37. package/dist/components/Workflow/Workflow.types.d.ts +9 -0
  38. package/dist/components/Workflow/Workflow.types.d.ts.map +1 -1
  39. package/dist/components/Workflow/Workflow.types.js.map +1 -1
  40. package/dist/components/Workflow/Workflow.types.mjs.map +1 -1
  41. package/dist/components/Workflow/canvas/Canvas/Canvas.d.ts +6 -2
  42. package/dist/components/Workflow/canvas/Canvas/Canvas.d.ts.map +1 -1
  43. package/dist/components/Workflow/canvas/Canvas/Canvas.js +29 -23
  44. package/dist/components/Workflow/canvas/Canvas/Canvas.js.map +1 -1
  45. package/dist/components/Workflow/canvas/Canvas/Canvas.mjs +31 -25
  46. package/dist/components/Workflow/canvas/Canvas/Canvas.mjs.map +1 -1
  47. package/dist/components/Workflow/createNode.d.ts +25 -1
  48. package/dist/components/Workflow/createNode.d.ts.map +1 -1
  49. package/dist/components/Workflow/createNode.js +39 -1
  50. package/dist/components/Workflow/createNode.js.map +1 -1
  51. package/dist/components/Workflow/createNode.mjs +38 -2
  52. package/dist/components/Workflow/createNode.mjs.map +1 -1
  53. package/dist/components/Workflow/index.d.ts +2 -0
  54. package/dist/components/Workflow/index.d.ts.map +1 -1
  55. package/dist/components/Workflow/layout/autoLayout.d.ts +10 -0
  56. package/dist/components/Workflow/layout/autoLayout.d.ts.map +1 -1
  57. package/dist/components/Workflow/layout/autoLayout.js +4 -0
  58. package/dist/components/Workflow/layout/autoLayout.js.map +1 -1
  59. package/dist/components/Workflow/layout/autoLayout.mjs +1 -1
  60. package/dist/components/Workflow/layout/autoLayout.mjs.map +1 -1
  61. package/dist/components/Workflow/placement.d.ts +65 -0
  62. package/dist/components/Workflow/placement.d.ts.map +1 -0
  63. package/dist/components/Workflow/placement.js +100 -0
  64. package/dist/components/Workflow/placement.js.map +1 -0
  65. package/dist/components/Workflow/placement.mjs +96 -0
  66. package/dist/components/Workflow/placement.mjs.map +1 -0
  67. package/dist/components/Workflow/useWorkflowActions.d.ts +54 -0
  68. package/dist/components/Workflow/useWorkflowActions.d.ts.map +1 -0
  69. package/dist/components/Workflow/useWorkflowActions.js +105 -0
  70. package/dist/components/Workflow/useWorkflowActions.js.map +1 -0
  71. package/dist/components/Workflow/useWorkflowActions.mjs +103 -0
  72. package/dist/components/Workflow/useWorkflowActions.mjs.map +1 -0
  73. package/dist/index.js +5 -0
  74. package/dist/index.js.map +1 -1
  75. package/dist/index.mjs +2 -1
  76. package/dist/index.mjs.map +1 -1
  77. package/package.json +1 -1
  78. package/src/components/Toast/Toast.action.test.tsx +135 -0
  79. package/src/components/Toast/Toast.stories.tsx +111 -0
  80. package/src/components/Toast/Toast.styles.ts +60 -0
  81. package/src/components/Toast/Toast.tsx +16 -3
  82. package/src/components/Toast/Toast.types.ts +26 -1
  83. package/src/components/Toast/Toaster.tsx +11 -0
  84. package/src/components/Toast/useToast.tsx +17 -2
  85. package/src/components/Workflow/README.md +64 -0
  86. package/src/components/Workflow/Workflow.tsx +2 -0
  87. package/src/components/Workflow/Workflow.types.ts +10 -0
  88. package/src/components/Workflow/canvas/Canvas/Canvas.tsx +57 -53
  89. package/src/components/Workflow/createNode.test.ts +40 -2
  90. package/src/components/Workflow/createNode.ts +70 -2
  91. package/src/components/Workflow/index.ts +8 -0
  92. package/src/components/Workflow/layout/autoLayout.ts +4 -4
  93. package/src/components/Workflow/placement.test.ts +156 -0
  94. package/src/components/Workflow/placement.ts +172 -0
  95. package/src/components/Workflow/stories/Workflow.stories.tsx +79 -0
  96. package/src/components/Workflow/useWorkflowActions.test.tsx +345 -0
  97. package/src/components/Workflow/useWorkflowActions.ts +196 -0
  98. package/src/guidelines/FeedbackActions.mdx +133 -0
@@ -0,0 +1,172 @@
1
+ import { isValidConnection } from './connectionRules';
2
+ import {
3
+ DEFAULT_NODE_GAP,
4
+ DEFAULT_RANK_GAP,
5
+ FALLBACK_HEIGHT,
6
+ FALLBACK_WIDTH,
7
+ } from './layout/autoLayout';
8
+ import type {
9
+ Graph,
10
+ GraphEdge,
11
+ GraphNode,
12
+ NodeTypeDef,
13
+ Position,
14
+ } from './Workflow.types';
15
+
16
+ type GetNodeType = (kind: string) => NodeTypeDef | undefined;
17
+
18
+ export type NodeSize = { width: number; height: number };
19
+
20
+ /**
21
+ * Size assumed for a node React Flow has not measured yet — including the node
22
+ * being added. Same fallback auto-arrange uses.
23
+ */
24
+ export const FALLBACK_NODE_SIZE: NodeSize = {
25
+ width: FALLBACK_WIDTH,
26
+ height: FALLBACK_HEIGHT,
27
+ };
28
+
29
+ /**
30
+ * Where `addNode` puts a new node.
31
+ *
32
+ * - `{ after: nodeId }` — to the right of that node, wired from its first free
33
+ * exit. Falls back to `{ at: 'viewportCenter' }` when the node is unknown or
34
+ * has no free exit (End, Note, every exit already holds its one edge).
35
+ * - `{ at: 'viewportCenter' }` — unconnected, centred in the visible canvas.
36
+ */
37
+ export type AddNodeTarget = { after: string } | { at: 'viewportCenter' };
38
+
39
+ /**
40
+ * A node's first exit with no outbound edge, or `null` when the node is
41
+ * unknown, has no exits (End, Note) or every exit already holds its one edge.
42
+ * An edge without a `sourceHandle` sits on the first exit (see
43
+ * `existingEdgeFromHandle`).
44
+ */
45
+ export function findFreeExit(
46
+ graph: Graph,
47
+ getNodeType: GetNodeType,
48
+ nodeId: string | null | undefined
49
+ ): { node: GraphNode; sourceHandle: string } | null {
50
+ if (!nodeId) return null;
51
+ const node = graph.nodes.find((n) => n.id === nodeId);
52
+ if (!node) return null;
53
+ const outputs = getNodeType(node.kind)?.handles(node.config).outputs ?? [];
54
+ const free = outputs.find(
55
+ (handle) =>
56
+ !graph.edges.some(
57
+ (edge) =>
58
+ edge.source === node.id &&
59
+ (edge.sourceHandle ?? outputs[0].id) === handle.id
60
+ )
61
+ );
62
+ return free ? { node, sourceHandle: free.id } : null;
63
+ }
64
+
65
+ const overlaps = (a: Position, aSize: NodeSize, b: Position, bSize: NodeSize) =>
66
+ a.x < b.x + bSize.width &&
67
+ a.x + aSize.width > b.x &&
68
+ a.y < b.y + bSize.height &&
69
+ a.y + aSize.height > b.y;
70
+
71
+ /** Moves `start` down until a fallback-sized card there covers no node. */
72
+ function nudgeToFreeSpot(
73
+ start: Position,
74
+ nodes: ReadonlyArray<GraphNode>,
75
+ getNodeSize: (id: string) => NodeSize | undefined
76
+ ): Position {
77
+ const position = { ...start };
78
+ // Each pass clears at least one node, so this ends within nodes.length passes.
79
+ for (let pass = 0; pass <= nodes.length; pass += 1) {
80
+ const blocker = nodes.find((n) =>
81
+ overlaps(
82
+ position,
83
+ FALLBACK_NODE_SIZE,
84
+ n.position,
85
+ getNodeSize(n.id) ?? FALLBACK_NODE_SIZE
86
+ )
87
+ );
88
+ if (!blocker) break;
89
+ const blockerHeight = (getNodeSize(blocker.id) ?? FALLBACK_NODE_SIZE)
90
+ .height;
91
+ position.y = blocker.position.y + blockerHeight + DEFAULT_NODE_GAP;
92
+ }
93
+ return position;
94
+ }
95
+
96
+ export type PlanNodePlacementArgs = {
97
+ graph: Graph;
98
+ getNodeType: GetNodeType;
99
+ /** The node being added; its `position` is ignored and replaced. */
100
+ node: GraphNode;
101
+ target: AddNodeTarget;
102
+ /** Flow coordinates of the visible canvas centre. */
103
+ viewportCenter: Position;
104
+ /** Id for the auto-connect edge, used only when one is created. */
105
+ edgeId: string;
106
+ /** Measured size of an existing node, when React Flow has one. */
107
+ getNodeSize?: (id: string) => NodeSize | undefined;
108
+ };
109
+
110
+ /**
111
+ * Where an added node goes and which edge, if any, wires it in. Pure — the
112
+ * `addNode` action feeds it the live graph, viewport and measured sizes.
113
+ *
114
+ * With `{ after }` on a node that has a free exit, the new node goes to that
115
+ * node's right and an edge runs from the exit into the new node's first entry.
116
+ * The edge is dropped (the node still goes to the right) when the new kind has
117
+ * no entry (Note) or the connection fails `isValidConnection` — which runs both
118
+ * node types' `canConnect` hooks, the same rules a manual drag obeys. Every
119
+ * other case lands unconnected at the viewport centre. Either way the node
120
+ * moves down past any node already in that spot.
121
+ */
122
+ export function planNodePlacement({
123
+ graph,
124
+ getNodeType,
125
+ node,
126
+ target,
127
+ viewportCenter,
128
+ edgeId,
129
+ getNodeSize = () => undefined,
130
+ }: PlanNodePlacementArgs): { position: Position; edge: GraphEdge | null } {
131
+ const source =
132
+ 'after' in target ? findFreeExit(graph, getNodeType, target.after) : null;
133
+
134
+ if (!source) {
135
+ const centered = {
136
+ x: viewportCenter.x - FALLBACK_NODE_SIZE.width / 2,
137
+ y: viewportCenter.y - FALLBACK_NODE_SIZE.height / 2,
138
+ };
139
+ return {
140
+ position: nudgeToFreeSpot(centered, graph.nodes, getNodeSize),
141
+ edge: null,
142
+ };
143
+ }
144
+
145
+ const sourceSize = getNodeSize(source.node.id) ?? FALLBACK_NODE_SIZE;
146
+ const position = nudgeToFreeSpot(
147
+ {
148
+ x: source.node.position.x + sourceSize.width + DEFAULT_RANK_GAP,
149
+ y: source.node.position.y,
150
+ },
151
+ graph.nodes,
152
+ getNodeSize
153
+ );
154
+
155
+ const placed: GraphNode = { ...node, position };
156
+ const targetHandle = getNodeType(node.kind)?.handles(node.config).inputs[0]
157
+ ?.id;
158
+ if (!targetHandle) return { position, edge: null };
159
+
160
+ const connection = {
161
+ source: source.node.id,
162
+ target: node.id,
163
+ sourceHandle: source.sourceHandle,
164
+ targetHandle,
165
+ };
166
+ const withNode: Graph = { ...graph, nodes: [...graph.nodes, placed] };
167
+ if (!isValidConnection(connection, withNode, getNodeType)) {
168
+ return { position, edge: null };
169
+ }
170
+
171
+ return { position, edge: { id: edgeId, ...connection } };
172
+ }
@@ -2,11 +2,16 @@ import { Box, HStack, VStack } from '@chakra-ui/react';
2
2
  import type { Meta, StoryFn } from '@storybook/react';
3
3
  import { useState } from 'react';
4
4
 
5
+ import { Button } from '@/components/Button';
6
+ import { PlusIcon } from '@/components/Icon';
7
+ import { IconButton } from '@/components/IconButton';
5
8
  import { Subtext, Subtitle } from '@/components/Typography';
6
9
 
7
10
  import { NodeInspector } from '../canvas/DrawerShell';
11
+ import { useWorkflowActions } from '../useWorkflowActions';
8
12
  import { Workflow } from '../Workflow';
9
13
  import type { Graph, WorkflowSelection } from '../Workflow.types';
14
+ import { useWorkflow } from '../WorkflowContext';
10
15
  import { toyGraph, toyNodeTypes } from './toyNodeTypes';
11
16
 
12
17
  /**
@@ -109,3 +114,77 @@ export const LeftDockedInspector: StoryFn<typeof Workflow> = () => (
109
114
  <NodeInspector dock="left" />
110
115
  </Workflow>
111
116
  );
117
+
118
+ /** "+" beside the selected node: adds an Add node wired after it. */
119
+ function AddAfterButton({ nodeId }: { nodeId: string }) {
120
+ const { addNode, getAddAfterSource } = useWorkflowActions();
121
+ const source = getAddAfterSource(nodeId);
122
+ if (!source) return null;
123
+ return (
124
+ <IconButton
125
+ aria-label={`Add after ${source.label}`}
126
+ size="sm"
127
+ variant="solid"
128
+ colorPalette="primary"
129
+ borderRadius="full"
130
+ onClick={() => addNode('math.add', { after: nodeId })}
131
+ >
132
+ <PlusIcon boxSize="xs" aria-hidden />
133
+ </IconButton>
134
+ );
135
+ }
136
+
137
+ /** Click-to-add list — what a touch palette does instead of drag-and-drop. */
138
+ function TapPalette() {
139
+ const { nodeTypes, editor } = useWorkflow();
140
+ const { addNode, getAddAfterSource } = useWorkflowActions();
141
+ const source = getAddAfterSource(editor.selectedNodeId);
142
+ return (
143
+ <VStack
144
+ w="240px"
145
+ p={4}
146
+ align="stretch"
147
+ borderLeftWidth="1px"
148
+ borderColor="border.default"
149
+ bg="bg.surface"
150
+ >
151
+ <Subtitle>Tap to add</Subtitle>
152
+ <Subtext color="slate.500" data-testid="add-target">
153
+ {source ? `Connect after ${source.label}` : 'Add at the centre'}
154
+ </Subtext>
155
+ {Object.values(nodeTypes).map((def) => (
156
+ <Button
157
+ key={def.kind}
158
+ size="sm"
159
+ variant="outline"
160
+ onClick={() =>
161
+ addNode(
162
+ def.kind,
163
+ source ? { after: source.nodeId } : { at: 'viewportCenter' }
164
+ )
165
+ }
166
+ >
167
+ {def.label}
168
+ </Button>
169
+ ))}
170
+ </VStack>
171
+ );
172
+ }
173
+
174
+ /**
175
+ * `useWorkflowActions().addNode` + `renderSelectedNodeToolbar`: add nodes
176
+ * without drag-and-drop. Select a node and press its "+" (or a button in the
177
+ * side list) to add a node wired from its first free exit; with nothing
178
+ * selected — or a node with no free exit — the node lands at the view centre.
179
+ * One undo (⌘Z) reverts node and edge together.
180
+ */
181
+ export const AddNodeWithoutDrag: StoryFn<typeof Workflow> = () => (
182
+ <Workflow
183
+ nodeTypes={toyNodeTypes}
184
+ defaultGraph={toyGraph as Graph}
185
+ showPalette={false}
186
+ renderSelectedNodeToolbar={(node) => <AddAfterButton nodeId={node.id} />}
187
+ >
188
+ <TapPalette />
189
+ </Workflow>
190
+ );
@@ -0,0 +1,345 @@
1
+ import { ChakraProvider } from '@chakra-ui/react';
2
+ import { act, fireEvent, render, screen } from '@testing-library/react';
3
+ import { describe, expect, it } from 'vitest';
4
+
5
+ import type { CreatedIcon } from '@/components/Icon';
6
+
7
+ import { system } from '../../theme';
8
+ import { DRAG_MIME } from './canvas/NodePalette';
9
+ import { useWorkflowActions, type WorkflowActions } from './useWorkflowActions';
10
+ import { Workflow } from './Workflow';
11
+ import { defineNodeType, type Graph, type NodeTypeDef } from './Workflow.types';
12
+ import { useWorkflow } from './WorkflowContext';
13
+
14
+ /**
15
+ * `useWorkflowActions` end to end: the real `<Workflow>` (reducer + history +
16
+ * React Flow provider) with a harness child, the way a host's tap-to-add
17
+ * palette calls it.
18
+ */
19
+ // The SVG icons resolve to data URLs under vitest, which jsdom can't mount as
20
+ // elements; the canvas only needs something renderable.
21
+ const StubIcon = (() => null) as unknown as CreatedIcon;
22
+
23
+ const startType = defineNodeType<Record<string, never>>({
24
+ kind: 'start',
25
+ label: 'Start',
26
+ category: 'trigger',
27
+ icon: StubIcon,
28
+ defaultConfig: () => ({}),
29
+ placement: { pinned: true, role: 'start' },
30
+ handles: () => ({ inputs: [], outputs: [{ id: 'out' }] }),
31
+ });
32
+ const agentType = defineNodeType<{ name: string; model: string }>({
33
+ kind: 'agent',
34
+ label: 'Agent',
35
+ category: 'ai',
36
+ icon: StubIcon,
37
+ defaultConfig: () => ({ name: 'Agent', model: 'static' }),
38
+ localizeDefaults: (t) => ({ name: t('agent_default_name') }),
39
+ hostDefaults: (bridge) => ({ model: (bridge as { model: string }).model }),
40
+ getInstanceTitle: (cfg) => cfg.name,
41
+ handles: () => ({ inputs: [{ id: 'in' }], outputs: [{ id: 'out' }] }),
42
+ });
43
+ const endType = defineNodeType<Record<string, never>>({
44
+ kind: 'end',
45
+ label: 'End',
46
+ category: 'output',
47
+ icon: StubIcon,
48
+ defaultConfig: () => ({}),
49
+ handles: () => ({ inputs: [{ id: 'in' }], outputs: [] }),
50
+ });
51
+ const noteType = defineNodeType<Record<string, never>>({
52
+ kind: 'note',
53
+ label: 'Note',
54
+ category: 'note',
55
+ icon: StubIcon,
56
+ defaultConfig: () => ({}),
57
+ handles: () => ({ inputs: [], outputs: [] }),
58
+ });
59
+ /** Refuses to be wired after Start, like a host `canConnect` rule. */
60
+ const guardType = defineNodeType<Record<string, never>>({
61
+ kind: 'guard',
62
+ label: 'Guard',
63
+ category: 'safety',
64
+ icon: StubIcon,
65
+ defaultConfig: () => ({}),
66
+ handles: () => ({ inputs: [{ id: 'in' }], outputs: [{ id: 'out' }] }),
67
+ canConnect: ({ source }) => source.kind !== 'start' || 'not after start',
68
+ });
69
+
70
+ const nodeTypes: NodeTypeDef[] = [
71
+ startType,
72
+ agentType,
73
+ endType,
74
+ noteType,
75
+ guardType,
76
+ ];
77
+
78
+ const seed: Graph = {
79
+ nodes: [{ id: 'start', kind: 'start', position: { x: 0, y: 0 }, config: {} }],
80
+ edges: [],
81
+ };
82
+
83
+ type Handle = {
84
+ actions: WorkflowActions;
85
+ ctx: ReturnType<typeof useWorkflow>;
86
+ };
87
+
88
+ function setup(graph: Graph = seed) {
89
+ const ref: { current: Handle | null } = { current: null };
90
+ function Harness() {
91
+ const actions = useWorkflowActions();
92
+ const ctx = useWorkflow();
93
+ ref.current = { actions, ctx };
94
+ return null;
95
+ }
96
+ const view = render(
97
+ <ChakraProvider value={system}>
98
+ <div style={{ width: 800, height: 600 }}>
99
+ <Workflow
100
+ nodeTypes={nodeTypes}
101
+ defaultGraph={graph}
102
+ translate={(key) => `t:${key}`}
103
+ hostBridge={{ model: 'live-model' }}
104
+ showPalette={false}
105
+ renderSelectedNodeToolbar={(node) => (
106
+ <span data-testid="toolbar">{node.id}</span>
107
+ )}
108
+ >
109
+ <Harness />
110
+ </Workflow>
111
+ </div>
112
+ </ChakraProvider>
113
+ );
114
+ const get = () => {
115
+ if (!ref.current) throw new Error('harness not mounted');
116
+ return ref.current;
117
+ };
118
+ return Object.assign(get, { container: view.container });
119
+ }
120
+
121
+ describe('useWorkflowActions().addNode', () => {
122
+ it('adds after a node: {kind}_{N} id, drop config, auto-edge, selection', () => {
123
+ const get = setup();
124
+ let result: ReturnType<WorkflowActions['addNode']> = null;
125
+ act(() => {
126
+ result = get().actions.addNode('agent', { after: 'start' });
127
+ });
128
+
129
+ expect(result).toEqual({ nodeId: 'agent_1', edgeId: expect.any(String) });
130
+ const { graph, editor } = get().ctx;
131
+ const added = graph.nodes.find((n) => n.id === 'agent_1');
132
+ // Same layering as a palette drop.
133
+ expect(added?.config).toEqual({
134
+ name: 't:agent_default_name',
135
+ model: 'live-model',
136
+ });
137
+ expect(added?.position).toEqual({ x: 254, y: 0 });
138
+ expect(graph.edges).toEqual([
139
+ {
140
+ id: result!.edgeId,
141
+ source: 'start',
142
+ target: 'agent_1',
143
+ sourceHandle: 'out',
144
+ targetHandle: 'in',
145
+ },
146
+ ]);
147
+ expect(result!.edgeId).toMatch(/^e_/);
148
+ expect(editor.selectedNodeId).toBe('agent_1');
149
+ });
150
+
151
+ it('chains: the next add goes after the newly selected node', () => {
152
+ const get = setup();
153
+ act(() => {
154
+ get().actions.addNode('agent', { after: 'start' });
155
+ });
156
+ act(() => {
157
+ const selected = get().ctx.editor.selectedNodeId!;
158
+ get().actions.addNode('agent', { after: selected });
159
+ });
160
+ const { graph } = get().ctx;
161
+ expect(graph.nodes.map((n) => n.id)).toEqual([
162
+ 'start',
163
+ 'agent_1',
164
+ 'agent_2',
165
+ ]);
166
+ expect(graph.edges.map((e) => [e.source, e.target])).toEqual([
167
+ ['start', 'agent_1'],
168
+ ['agent_1', 'agent_2'],
169
+ ]);
170
+ });
171
+
172
+ it('adds unconnected at the viewport centre', () => {
173
+ const get = setup();
174
+ let result: ReturnType<WorkflowActions['addNode']> = null;
175
+ act(() => {
176
+ result = get().actions.addNode('agent', { at: 'viewportCenter' });
177
+ });
178
+ expect(result).toEqual({ nodeId: 'agent_1', edgeId: null });
179
+ expect(get().ctx.graph.edges).toEqual([]);
180
+ });
181
+
182
+ it('adds no edge from an occupied exit or from End', () => {
183
+ const get = setup({
184
+ nodes: [
185
+ ...seed.nodes,
186
+ { id: 'end_1', kind: 'end', position: { x: 400, y: 0 }, config: {} },
187
+ ],
188
+ edges: [{ id: 'e_1', source: 'start', target: 'end_1' }],
189
+ });
190
+ expect(get().actions.canAddAfter('start')).toBe(false);
191
+ expect(get().actions.canAddAfter('end_1')).toBe(false);
192
+
193
+ act(() => {
194
+ expect(get().actions.addNode('agent', { after: 'start' })?.edgeId).toBe(
195
+ null
196
+ );
197
+ });
198
+ act(() => {
199
+ expect(get().actions.addNode('agent', { after: 'end_1' })?.edgeId).toBe(
200
+ null
201
+ );
202
+ });
203
+ expect(get().ctx.graph.edges).toHaveLength(1);
204
+ });
205
+
206
+ it('adds no edge into a Note', () => {
207
+ const get = setup();
208
+ act(() => {
209
+ expect(get().actions.addNode('note', { after: 'start' })).toEqual({
210
+ nodeId: 'note_1',
211
+ edgeId: null,
212
+ });
213
+ });
214
+ expect(get().ctx.graph.edges).toEqual([]);
215
+ });
216
+
217
+ it('respects canConnect', () => {
218
+ const get = setup();
219
+ act(() => {
220
+ expect(get().actions.addNode('guard', { after: 'start' })?.edgeId).toBe(
221
+ null
222
+ );
223
+ });
224
+ expect(get().ctx.graph.nodes.map((n) => n.id)).toContain('guard_1');
225
+ expect(get().ctx.graph.edges).toEqual([]);
226
+ });
227
+
228
+ it('is reverted by a single undo (node and edge together)', () => {
229
+ const get = setup();
230
+ act(() => {
231
+ get().actions.addNode('agent', { after: 'start' });
232
+ });
233
+ expect(get().ctx.canUndo).toBe(true);
234
+ act(() => {
235
+ get().ctx.undo();
236
+ });
237
+ expect(get().ctx.graph.nodes.map((n) => n.id)).toEqual(['start']);
238
+ expect(get().ctx.graph.edges).toEqual([]);
239
+ expect(get().ctx.canUndo).toBe(false);
240
+ });
241
+
242
+ it('returns null for an unknown kind or a taken singleton id', () => {
243
+ const get = setup();
244
+ act(() => {
245
+ expect(get().actions.addNode('nope', { at: 'viewportCenter' })).toBe(
246
+ null
247
+ );
248
+ expect(get().actions.addNode('start', { at: 'viewportCenter' })).toBe(
249
+ null
250
+ );
251
+ });
252
+ expect(get().ctx.canUndo).toBe(false);
253
+ });
254
+
255
+ it('controls the inspector per the option', () => {
256
+ const get = setup();
257
+ act(() => {
258
+ get().actions.addNode(
259
+ 'agent',
260
+ { at: 'viewportCenter' },
261
+ { inspector: 'open' }
262
+ );
263
+ });
264
+ expect(get().ctx.editor.drawerTarget).toEqual({
265
+ type: 'node',
266
+ id: 'agent_1',
267
+ });
268
+ // 'follow' keeps an open inspector on the new selection.
269
+ act(() => {
270
+ get().actions.addNode('agent', { at: 'viewportCenter' });
271
+ });
272
+ expect(get().ctx.editor.drawerTarget).toEqual({
273
+ type: 'node',
274
+ id: 'agent_2',
275
+ });
276
+ act(() => {
277
+ get().actions.addNode(
278
+ 'agent',
279
+ { at: 'viewportCenter' },
280
+ { inspector: 'close' }
281
+ );
282
+ });
283
+ expect(get().ctx.editor.drawerTarget).toBeNull();
284
+ expect(get().ctx.editor.selectedNodeId).toBe('agent_3');
285
+ });
286
+ });
287
+
288
+ describe('useWorkflowActions().getAddAfterSource', () => {
289
+ it('reports the exit and label an add-after would use', () => {
290
+ const get = setup({
291
+ nodes: [
292
+ ...seed.nodes,
293
+ {
294
+ id: 'agent_1',
295
+ kind: 'agent',
296
+ position: { x: 300, y: 0 },
297
+ config: { name: 'Triage', model: 'm' },
298
+ },
299
+ ],
300
+ edges: [],
301
+ });
302
+ expect(get().actions.getAddAfterSource('agent_1')).toEqual({
303
+ nodeId: 'agent_1',
304
+ sourceHandle: 'out',
305
+ label: 'Triage',
306
+ });
307
+ // No instance title → type label.
308
+ expect(get().actions.getAddAfterSource('start')?.label).toBe('Start');
309
+ expect(get().actions.getAddAfterSource(null)).toBeNull();
310
+ });
311
+ });
312
+
313
+ describe('renderSelectedNodeToolbar', () => {
314
+ it('renders beside the selected node only', () => {
315
+ const get = setup();
316
+ expect(screen.queryByTestId('toolbar')).toBeNull();
317
+ act(() => {
318
+ get().actions.addNode('agent', { after: 'start' });
319
+ });
320
+ expect(screen.getByTestId('toolbar')).toHaveTextContent('agent_1');
321
+ });
322
+ });
323
+
324
+ describe('palette drop', () => {
325
+ it('builds the node through the same helper as addNode', () => {
326
+ const get = setup();
327
+ const pane = get.container.querySelector('.react-flow')?.parentElement;
328
+ if (!pane) throw new Error('canvas not mounted');
329
+ act(() => {
330
+ fireEvent.drop(pane, {
331
+ clientX: 0,
332
+ clientY: 0,
333
+ dataTransfer: { types: [DRAG_MIME], getData: () => 'agent' },
334
+ });
335
+ });
336
+ const dropped = get().ctx.graph.nodes.find((n) => n.kind === 'agent');
337
+ expect(dropped?.id).toBe('agent_1');
338
+ expect(dropped?.config).toEqual({
339
+ name: 't:agent_default_name',
340
+ model: 'live-model',
341
+ });
342
+ // Drops stay unconnected.
343
+ expect(get().ctx.graph.edges).toEqual([]);
344
+ });
345
+ });