gpui-ui-kit 0.5.10

A reusable UI component library for GPUI applications
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
//! Integration tests for the workflow canvas module

use super::bezier::{connection_path, flatten_cubic_bezier, horizontal_bezier};
use super::history::{
    AddConnectionCommand, AddNodeCommand, Command, HistoryManager, MoveNodesCommand,
    RemoveNodeCommand,
};
use super::hit_test::{HitTestResult, HitTester};
use super::state::{
    Connection, NodeId, Position, SelectionState, ViewportState, WorkflowGraph, WorkflowNodeData,
};

// ============================================================================
// Position Tests
// ============================================================================

#[test]
fn test_position_new() {
    let pos = Position::new(10.0, 20.0);
    assert_eq!(pos.x, 10.0);
    assert_eq!(pos.y, 20.0);
}

#[test]
fn test_position_distance() {
    let p1 = Position::new(0.0, 0.0);
    let p2 = Position::new(3.0, 4.0);
    assert_eq!(p1.distance(&p2), 5.0);
}

#[test]
fn test_position_default() {
    let pos = Position::default();
    assert_eq!(pos.x, 0.0);
    assert_eq!(pos.y, 0.0);
}

// ============================================================================
// WorkflowNodeData Tests
// ============================================================================

#[test]
fn test_node_creation() {
    let node = WorkflowNodeData::new("Test Node", Position::new(100.0, 200.0));
    assert_eq!(node.title, "Test Node");
    assert_eq!(node.position.x, 100.0);
    assert_eq!(node.position.y, 200.0);
    assert_eq!(node.input_count, 1);
    assert_eq!(node.output_count, 1);
}

#[test]
fn test_node_with_ports() {
    let node = WorkflowNodeData::new("Node", Position::new(0.0, 0.0)).with_ports(3, 2);
    assert_eq!(node.input_count, 3);
    assert_eq!(node.output_count, 2);
}

#[test]
fn test_node_with_size() {
    let node = WorkflowNodeData::new("Node", Position::new(0.0, 0.0)).with_size(200.0, 100.0);
    assert_eq!(node.width, 200.0);
    assert_eq!(node.height, 100.0);
}

#[test]
fn test_node_center() {
    let node = WorkflowNodeData::new("Node", Position::new(100.0, 100.0)).with_size(200.0, 100.0);
    let center = node.center();
    assert_eq!(center.x, 200.0); // 100 + 200/2
    assert_eq!(center.y, 150.0); // 100 + 100/2
}

#[test]
fn test_node_port_positions() {
    let node = WorkflowNodeData::new("Node", Position::new(0.0, 0.0))
        .with_ports(2, 2)
        .with_size(160.0, 90.0);

    // Input ports on left side
    let input0 = node.input_port_position(0);
    let input1 = node.input_port_position(1);
    assert_eq!(input0.x, 0.0);
    assert_eq!(input1.x, 0.0);
    assert!(input0.y < input1.y); // Port 0 is above port 1

    // Output ports on right side
    let output0 = node.output_port_position(0);
    let output1 = node.output_port_position(1);
    assert_eq!(output0.x, 160.0); // Right edge
    assert_eq!(output1.x, 160.0);
    assert!(output0.y < output1.y);
}

// ============================================================================
// Connection Tests
// ============================================================================

#[test]
fn test_connection_creation() {
    let from_node = NodeId::new_v4();
    let to_node = NodeId::new_v4();
    let conn = Connection::new(from_node, 0, to_node, 1);

    assert_eq!(conn.from_node, from_node);
    assert_eq!(conn.from_port, 0);
    assert_eq!(conn.to_node, to_node);
    assert_eq!(conn.to_port, 1);
}

// ============================================================================
// WorkflowGraph Tests
// ============================================================================

#[test]
fn test_graph_add_node() {
    let mut graph = WorkflowGraph::new();
    let node = WorkflowNodeData::new("Node 1", Position::new(0.0, 0.0));
    let id = node.id;

    let returned_id = graph.add_node(node);
    assert_eq!(id, returned_id);
    assert!(graph.nodes.contains_key(&id));
    assert_eq!(graph.nodes.len(), 1);
}

#[test]
fn test_graph_remove_node() {
    let mut graph = WorkflowGraph::new();
    let node = WorkflowNodeData::new("Node", Position::new(0.0, 0.0));
    let id = node.id;
    graph.add_node(node);

    let removed = graph.remove_node(id);
    assert!(removed.is_some());
    assert!(!graph.nodes.contains_key(&id));
}

#[test]
fn test_graph_remove_node_removes_connections() {
    let mut graph = WorkflowGraph::new();

    let node1 = WorkflowNodeData::new("Node 1", Position::new(0.0, 0.0));
    let node2 = WorkflowNodeData::new("Node 2", Position::new(200.0, 0.0));
    let id1 = node1.id;
    let id2 = node2.id;

    graph.add_node(node1);
    graph.add_node(node2);
    graph.add_connection(id1, 0, id2, 0).unwrap();

    assert_eq!(graph.connections.len(), 1);

    graph.remove_node(id1);

    // Connection should be removed
    assert_eq!(graph.connections.len(), 0);
}

#[test]
fn test_graph_add_connection() {
    let mut graph = WorkflowGraph::new();

    let node1 = WorkflowNodeData::new("Node 1", Position::new(0.0, 0.0));
    let node2 = WorkflowNodeData::new("Node 2", Position::new(200.0, 0.0));
    let id1 = node1.id;
    let id2 = node2.id;

    graph.add_node(node1);
    graph.add_node(node2);

    let result = graph.add_connection(id1, 0, id2, 0);
    assert!(result.is_ok());
    assert_eq!(graph.connections.len(), 1);
}

#[test]
fn test_graph_add_connection_invalid_node() {
    let mut graph = WorkflowGraph::new();

    let node1 = WorkflowNodeData::new("Node 1", Position::new(0.0, 0.0));
    let id1 = node1.id;
    let fake_id = NodeId::new_v4();

    graph.add_node(node1);

    let result = graph.add_connection(id1, 0, fake_id, 0);
    assert!(result.is_err());
}

#[test]
fn test_graph_add_connection_self_loop_prevented() {
    let mut graph = WorkflowGraph::new();

    let node = WorkflowNodeData::new("Node", Position::new(0.0, 0.0)).with_ports(1, 1);
    let id = node.id;
    graph.add_node(node);

    let result = graph.add_connection(id, 0, id, 0);
    assert!(result.is_err());
}

#[test]
fn test_graph_remove_connection() {
    let mut graph = WorkflowGraph::new();

    let node1 = WorkflowNodeData::new("Node 1", Position::new(0.0, 0.0));
    let node2 = WorkflowNodeData::new("Node 2", Position::new(200.0, 0.0));
    let id1 = node1.id;
    let id2 = node2.id;

    graph.add_node(node1);
    graph.add_node(node2);
    graph.add_connection(id1, 0, id2, 0).unwrap();
    assert_eq!(graph.connections.len(), 1);

    let conn_id = graph.connections[0].id;
    graph.remove_connection(conn_id);
    assert_eq!(graph.connections.len(), 0);
}

// ============================================================================
// SelectionState Tests
// ============================================================================

#[test]
fn test_selection_empty() {
    let selection = SelectionState::default();
    assert!(selection.is_empty());
}

#[test]
fn test_selection_select_node() {
    let mut selection = SelectionState::default();
    let node_id = NodeId::new_v4();

    selection.select_node(node_id, false);
    assert!(selection.is_node_selected(node_id));
    assert!(!selection.is_empty());
}

#[test]
fn test_selection_multi_select() {
    let mut selection = SelectionState::default();
    let id1 = NodeId::new_v4();
    let id2 = NodeId::new_v4();

    selection.select_node(id1, false);
    selection.select_node(id2, true); // Add to selection

    assert!(selection.is_node_selected(id1));
    assert!(selection.is_node_selected(id2));
    assert_eq!(selection.selected_nodes.len(), 2);
}

#[test]
fn test_selection_clear() {
    let mut selection = SelectionState::default();
    let node_id = NodeId::new_v4();

    selection.select_node(node_id, false);
    selection.clear();

    assert!(selection.is_empty());
}

#[test]
fn test_selection_toggle_node() {
    let mut selection = SelectionState::default();
    let node_id = NodeId::new_v4();

    selection.toggle_node(node_id);
    assert!(selection.is_node_selected(node_id));

    selection.toggle_node(node_id);
    assert!(!selection.is_node_selected(node_id));
}

// ============================================================================
// ViewportState Tests
// ============================================================================

#[test]
fn test_viewport_default() {
    let viewport = ViewportState::default();
    assert_eq!(viewport.zoom, 1.0);
    assert_eq!(viewport.offset.x, 0.0);
    assert_eq!(viewport.offset.y, 0.0);
}

#[test]
fn test_viewport_screen_to_canvas() {
    let viewport = ViewportState::default();
    let canvas_pos = viewport.screen_to_canvas(100.0, 100.0);
    assert_eq!(canvas_pos.x, 100.0);
    assert_eq!(canvas_pos.y, 100.0);
}

#[test]
fn test_viewport_screen_to_canvas_with_offset() {
    let mut viewport = ViewportState::default();
    viewport.offset = Position::new(50.0, 50.0);

    let canvas_pos = viewport.screen_to_canvas(100.0, 100.0);
    assert_eq!(canvas_pos.x, 50.0);
    assert_eq!(canvas_pos.y, 50.0);
}

#[test]
fn test_viewport_screen_to_canvas_with_zoom() {
    let mut viewport = ViewportState::default();
    viewport.zoom = 2.0;

    let canvas_pos = viewport.screen_to_canvas(200.0, 200.0);
    assert_eq!(canvas_pos.x, 100.0);
    assert_eq!(canvas_pos.y, 100.0);
}

#[test]
fn test_viewport_canvas_to_screen() {
    let viewport = ViewportState::default();
    let pos = Position::new(100.0, 100.0);
    let screen_pos = viewport.canvas_to_screen(&pos);
    assert_eq!(screen_pos.x, 100.0);
    assert_eq!(screen_pos.y, 100.0);
}

#[test]
fn test_viewport_roundtrip() {
    let mut viewport = ViewportState::default();
    viewport.offset = Position::new(30.0, 40.0);
    viewport.zoom = 1.5;

    let original = Position::new(100.0, 200.0);
    let screen = viewport.canvas_to_screen(&original);
    let back = viewport.screen_to_canvas(screen.x, screen.y);

    assert!((original.x - back.x).abs() < 0.001);
    assert!((original.y - back.y).abs() < 0.001);
}

// ============================================================================
// HitTester Tests
// ============================================================================

#[test]
fn test_hit_test_empty_graph() {
    let hit_tester = HitTester::new();
    let graph = WorkflowGraph::new();
    let result = hit_tester.hit_test(Position::new(0.0, 0.0), &graph);
    assert_eq!(result, HitTestResult::Canvas);
}

#[test]
fn test_hit_test_node() {
    let hit_tester = HitTester::new();
    let mut graph = WorkflowGraph::new();

    let node = WorkflowNodeData::new("Node", Position::new(100.0, 100.0)).with_size(160.0, 80.0);
    let id = node.id;
    graph.add_node(node);

    // Hit inside node
    let result = hit_tester.hit_test(Position::new(150.0, 140.0), &graph);
    assert_eq!(result, HitTestResult::Node(id));

    // Miss outside node - hits canvas background
    let result = hit_tester.hit_test(Position::new(0.0, 0.0), &graph);
    assert_eq!(result, HitTestResult::Canvas);
}

#[test]
fn test_hit_test_input_port() {
    let hit_tester = HitTester::new();
    let mut graph = WorkflowGraph::new();

    let node = WorkflowNodeData::new("Node", Position::new(100.0, 100.0))
        .with_size(160.0, 80.0)
        .with_ports(1, 1);
    let id = node.id;
    let port_pos = node.input_port_position(0);
    graph.add_node(node);

    let result = hit_tester.hit_test(port_pos, &graph);
    assert_eq!(result, HitTestResult::InputPort(id, 0));
}

#[test]
fn test_hit_test_output_port() {
    let hit_tester = HitTester::new();
    let mut graph = WorkflowGraph::new();

    let node = WorkflowNodeData::new("Node", Position::new(100.0, 100.0))
        .with_size(160.0, 80.0)
        .with_ports(1, 1);
    let id = node.id;
    let port_pos = node.output_port_position(0);
    graph.add_node(node);

    let result = hit_tester.hit_test(port_pos, &graph);
    assert_eq!(result, HitTestResult::OutputPort(id, 0));
}

// ============================================================================
// History (Command Pattern) Tests
// ============================================================================

#[test]
fn test_history_add_node_command() {
    let mut graph = WorkflowGraph::new();
    let node = WorkflowNodeData::new("Test", Position::new(0.0, 0.0));
    let id = node.id;

    let cmd = AddNodeCommand { node: node.clone() };

    // Execute
    cmd.execute(&mut graph);
    assert!(graph.nodes.contains_key(&id));

    // Undo
    cmd.undo(&mut graph);
    assert!(!graph.nodes.contains_key(&id));
}

#[test]
fn test_history_remove_node_command() {
    let mut graph = WorkflowGraph::new();
    let node = WorkflowNodeData::new("Test", Position::new(0.0, 0.0));
    let id = node.id;
    graph.add_node(node.clone());

    let cmd = RemoveNodeCommand {
        node: node.clone(),
        connections: vec![],
    };

    // Execute
    cmd.execute(&mut graph);
    assert!(!graph.nodes.contains_key(&id));

    // Undo
    cmd.undo(&mut graph);
    assert!(graph.nodes.contains_key(&id));
}

#[test]
fn test_history_move_nodes_command() {
    let mut graph = WorkflowGraph::new();
    let node = WorkflowNodeData::new("Test", Position::new(100.0, 100.0));
    let id = node.id;
    graph.add_node(node);

    let old_pos = Position::new(100.0, 100.0);
    let new_pos = Position::new(200.0, 200.0);

    let cmd = MoveNodesCommand {
        moves: vec![(id, old_pos, new_pos)],
    };

    // Execute
    cmd.execute(&mut graph);
    assert_eq!(graph.nodes.get(&id).unwrap().position.x, 200.0);
    assert_eq!(graph.nodes.get(&id).unwrap().position.y, 200.0);

    // Undo
    cmd.undo(&mut graph);
    assert_eq!(graph.nodes.get(&id).unwrap().position.x, 100.0);
    assert_eq!(graph.nodes.get(&id).unwrap().position.y, 100.0);
}

#[test]
fn test_history_add_connection_command() {
    let mut graph = WorkflowGraph::new();

    let node1 = WorkflowNodeData::new("N1", Position::new(0.0, 0.0));
    let node2 = WorkflowNodeData::new("N2", Position::new(200.0, 0.0));
    let id1 = node1.id;
    let id2 = node2.id;
    graph.add_node(node1);
    graph.add_node(node2);

    let conn = Connection::new(id1, 0, id2, 0);

    let cmd = AddConnectionCommand {
        connection: conn.clone(),
    };

    // Execute
    cmd.execute(&mut graph);
    assert_eq!(graph.connections.len(), 1);

    // Undo
    cmd.undo(&mut graph);
    assert_eq!(graph.connections.len(), 0);
}

#[test]
fn test_history_manager_undo_redo() {
    let mut graph = WorkflowGraph::new();
    let mut history = HistoryManager::new();

    let node = WorkflowNodeData::new("Test", Position::new(0.0, 0.0));
    let id = node.id;

    // Execute add command
    history.execute(Box::new(AddNodeCommand { node: node.clone() }), &mut graph);
    assert!(graph.nodes.contains_key(&id));
    assert!(history.can_undo());
    assert!(!history.can_redo());

    // Undo
    assert!(history.undo(&mut graph));
    assert!(!graph.nodes.contains_key(&id));
    assert!(!history.can_undo());
    assert!(history.can_redo());

    // Redo
    assert!(history.redo(&mut graph));
    assert!(graph.nodes.contains_key(&id));
    assert!(history.can_undo());
    assert!(!history.can_redo());
}

#[test]
fn test_history_manager_clear_redo_on_new_action() {
    let mut graph = WorkflowGraph::new();
    let mut history = HistoryManager::new();

    let node1 = WorkflowNodeData::new("N1", Position::new(0.0, 0.0));
    let node2 = WorkflowNodeData::new("N2", Position::new(100.0, 0.0));

    history.execute(
        Box::new(AddNodeCommand {
            node: node1.clone(),
        }),
        &mut graph,
    );
    history.undo(&mut graph);
    assert!(history.can_redo());

    // New action clears redo stack
    history.execute(
        Box::new(AddNodeCommand {
            node: node2.clone(),
        }),
        &mut graph,
    );
    assert!(!history.can_redo());
}

// ============================================================================
// Bezier Curve Tests
// ============================================================================

#[test]
fn test_horizontal_bezier() {
    let from = Position::new(0.0, 0.0);
    let to = Position::new(100.0, 0.0);

    let (p0, p1, p2, p3) = horizontal_bezier(from, to);

    assert_eq!(p0.x, 0.0);
    assert_eq!(p3.x, 100.0);
    // Control points should be between start and end
    assert!(p1.x > p0.x && p1.x < p3.x);
    assert!(p2.x > p0.x && p2.x < p3.x);
}

#[test]
fn test_flatten_cubic_bezier() {
    let p0 = Position::new(0.0, 0.0);
    let p1 = Position::new(33.0, 0.0);
    let p2 = Position::new(66.0, 0.0);
    let p3 = Position::new(100.0, 0.0);

    let points = flatten_cubic_bezier(p0, p1, p2, p3, 1.0);

    // Should have at least start and end points
    assert!(points.len() >= 2);
    assert_eq!(points[0].x, 0.0);
    assert_eq!(points[points.len() - 1].x, 100.0);
}

#[test]
fn test_connection_path() {
    let from = Position::new(0.0, 0.0);
    let to = Position::new(100.0, 50.0);

    let points = connection_path(from, to, 1.0);

    assert!(points.len() >= 2);
    assert_eq!(points[0].x, 0.0);
    assert_eq!(points[0].y, 0.0);
}

// ============================================================================
// Integration Tests
// ============================================================================

#[test]
fn test_full_workflow_scenario() {
    let mut graph = WorkflowGraph::new();
    let mut history = HistoryManager::new();

    // Add three nodes
    let node1 = WorkflowNodeData::new("Input", Position::new(0.0, 50.0)).with_ports(0, 2);
    let node2 = WorkflowNodeData::new("Process", Position::new(200.0, 0.0)).with_ports(1, 1);
    let node3 = WorkflowNodeData::new("Output", Position::new(400.0, 50.0)).with_ports(2, 0);

    let id1 = node1.id;
    let id2 = node2.id;
    let id3 = node3.id;

    history.execute(Box::new(AddNodeCommand { node: node1 }), &mut graph);
    history.execute(Box::new(AddNodeCommand { node: node2 }), &mut graph);
    history.execute(Box::new(AddNodeCommand { node: node3 }), &mut graph);

    assert_eq!(graph.nodes.len(), 3);

    // Add connections
    let conn1 = Connection::new(id1, 0, id2, 0);
    let conn2 = Connection::new(id2, 0, id3, 0);
    history.execute(
        Box::new(AddConnectionCommand { connection: conn1 }),
        &mut graph,
    );
    history.execute(
        Box::new(AddConnectionCommand { connection: conn2 }),
        &mut graph,
    );

    assert_eq!(graph.connections.len(), 2);

    // Move a node
    let old_pos = graph.nodes.get(&id2).unwrap().position;
    let new_pos = Position::new(250.0, 50.0);
    history.execute(
        Box::new(MoveNodesCommand {
            moves: vec![(id2, old_pos, new_pos)],
        }),
        &mut graph,
    );

    assert_eq!(graph.nodes.get(&id2).unwrap().position.x, 250.0);

    // Undo the move
    history.undo(&mut graph);
    assert_eq!(graph.nodes.get(&id2).unwrap().position.x, 200.0);

    // Undo a connection
    history.undo(&mut graph);
    assert_eq!(graph.connections.len(), 1);

    // Undo another connection
    history.undo(&mut graph);
    assert_eq!(graph.connections.len(), 0);

    // Redo connections
    history.redo(&mut graph);
    history.redo(&mut graph);
    assert_eq!(graph.connections.len(), 2);
}

#[test]
fn test_node_removal_cascade() {
    let mut graph = WorkflowGraph::new();

    // Create nodes
    let node1 = WorkflowNodeData::new("A", Position::new(0.0, 0.0));
    let node2 = WorkflowNodeData::new("B", Position::new(200.0, 0.0));
    let node3 = WorkflowNodeData::new("C", Position::new(400.0, 0.0));

    let id1 = node1.id;
    let id2 = node2.id;
    let id3 = node3.id;

    graph.add_node(node1);
    graph.add_node(node2);
    graph.add_node(node3);

    // Create chain: A -> B -> C
    graph.add_connection(id1, 0, id2, 0).unwrap();
    graph.add_connection(id2, 0, id3, 0).unwrap();

    assert_eq!(graph.connections.len(), 2);

    // Remove middle node - should remove both connections
    graph.remove_node(id2);

    assert_eq!(graph.nodes.len(), 2);
    assert_eq!(graph.connections.len(), 0);
}

#[test]
fn test_serialization_roundtrip() {
    let mut graph = WorkflowGraph::new();

    let node1 = WorkflowNodeData::new("Node 1", Position::new(100.0, 200.0))
        .with_ports(1, 2)
        .with_size(180.0, 90.0);
    let node2 = WorkflowNodeData::new("Node 2", Position::new(300.0, 200.0)).with_ports(2, 1);

    let id1 = node1.id;
    let id2 = node2.id;

    graph.add_node(node1);
    graph.add_node(node2);
    graph.add_connection(id1, 0, id2, 0).unwrap();

    // Serialize
    let json = serde_json::to_string(&graph).expect("Serialization failed");

    // Deserialize
    let restored: WorkflowGraph = serde_json::from_str(&json).expect("Deserialization failed");

    assert_eq!(restored.nodes.len(), 2);
    assert_eq!(restored.connections.len(), 1);
    assert!(restored.nodes.contains_key(&id1));
    assert!(restored.nodes.contains_key(&id2));

    let restored_node1 = restored.nodes.get(&id1).unwrap();
    assert_eq!(restored_node1.title, "Node 1");
    assert_eq!(restored_node1.position.x, 100.0);
}

#[test]
fn test_hit_test_priority() {
    // Ports should have higher priority than nodes
    let hit_tester = HitTester::new();
    let mut graph = WorkflowGraph::new();

    let node = WorkflowNodeData::new("Node", Position::new(100.0, 100.0))
        .with_size(160.0, 80.0)
        .with_ports(1, 1);
    let id = node.id;
    let input_port = node.input_port_position(0);
    graph.add_node(node);

    // Click exactly on input port should return port, not node
    let result = hit_tester.hit_test(input_port, &graph);
    match result {
        HitTestResult::InputPort(node_id, port) => {
            assert_eq!(node_id, id);
            assert_eq!(port, 0);
        }
        _ => panic!("Expected InputPort hit result"),
    }
}