iced_nodegraph 0.4.0

High-performance node graph editor widget for Iced with SDF-based rendering
Documentation
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
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
//! High-level interaction tests driving NodeGraph through `iced_test::Simulator`.
//!
//! Unlike the recording-renderer tests in `src/coordinate_tests.rs` and
//! `src/clipping_tests.rs` (which assert on render geometry via a fake
//! renderer), these tests exercise the widget end-to-end through the real iced
//! event pipeline: layout -> update -> message emission. These tests validate
//! interaction logic and the Messages the event callbacks publish; the one
//! snapshot test additionally rasterizes (see its backend note).
//!
//! Coordinate model: the graph fills the 1024x768 root with the default camera
//! (zoom 1, no pan, origin (0,0)), so world coordinates equal screen pixels.
//! A node pushed at world `p` with content size `w x h` has a body spanning
//! `p .. p + (w, h)`.

use iced::widget::{container, text};
use iced::{Element, Length, Point, Theme, Vector};
use iced::{keyboard, mouse};
use iced_nodegraph::{NodeGraph, PinRef, edge, node, pin};
use iced_test::Simulator;

type Renderer = iced::Renderer;
type Graph = NodeGraph<'static, usize, usize, (), Msg, Theme, Renderer>;
type Pin = PinRef<usize, usize>;

/// Captures every interaction callback the graph can emit.
#[derive(Debug, Clone, PartialEq)]
enum Msg {
    Select(Vec<usize>),
    Move(Vector, Vec<usize>),
    Clone(Vec<usize>),
    Delete(Vec<usize>),
    Connect(Pin, Pin),
    Disconnect(Pin, Pin),
    Camera(Point, f32),
    Button,
    Input(String),
}

const NODE_W: f32 = 60.0;
const NODE_H: f32 = 30.0;

/// Builds a graph with one fixed-size node body per `(id, world-position)`,
/// every interaction callback wired into `Msg`.
fn graph_with(nodes: &[(usize, Point)]) -> Element<'static, Msg, Theme, Renderer> {
    let mut ng: Graph = NodeGraph::default()
        .width(Length::Fill)
        .height(Length::Fill)
        .on_select(Msg::Select)
        .on_move(Msg::Move)
        .on_clone(Msg::Clone)
        .on_delete(Msg::Delete);
    for &(id, pos) in nodes {
        let body = container(iced::widget::text("n"))
            .width(Length::Fixed(NODE_W))
            .height(Length::Fixed(NODE_H));
        ng.push_node(node(id, pos, body));
    }
    ng.into()
}

/// Screen center of a node body whose top-left world position is `p`.
fn center(p: Point) -> Point {
    Point::new(p.x + NODE_W / 2.0, p.y + NODE_H / 2.0)
}

fn moved(p: Point) -> iced::Event {
    iced::Event::Mouse(mouse::Event::CursorMoved { position: p })
}
fn press() -> iced::Event {
    iced::Event::Mouse(mouse::Event::ButtonPressed(mouse::Button::Left))
}
fn release() -> iced::Event {
    iced::Event::Mouse(mouse::Event::ButtonReleased(mouse::Button::Left))
}

/// A full left-button drag from `from` to `to` (press, move, release).
fn drag(ui: &mut Simulator<'_, Msg, Theme, Renderer>, from: Point, to: Point) {
    ui.point_at(from);
    ui.simulate([moved(from), press()]);
    ui.point_at(to);
    ui.simulate([moved(to), release()]);
}

/// A left click at `at` (press and release in place).
fn click(ui: &mut Simulator<'_, Msg, Theme, Renderer>, at: Point) {
    ui.point_at(at);
    ui.simulate([moved(at), press(), release()]);
}

/// A key press carrying `modifiers` (Simulator's `tap_key` cannot set them).
fn key_pressed(key: keyboard::Key, modifiers: keyboard::Modifiers) -> iced::Event {
    iced::Event::Keyboard(keyboard::Event::KeyPressed {
        key: key.clone(),
        modified_key: key,
        physical_key: keyboard::key::Physical::Unidentified(
            keyboard::key::NativeCode::Unidentified,
        ),
        location: keyboard::Location::Standard,
        modifiers,
        text: None,
        repeat: false,
    })
}

/// Mirrors iced's `Modifiers::command()`: Cmd on macOS, Ctrl elsewhere. The
/// graph's shortcuts gate on `command()`, so tests must send the platform's
/// command modifier or they pass on one OS and fail on the other.
fn cmd() -> keyboard::Modifiers {
    #[cfg(target_os = "macos")]
    {
        keyboard::Modifiers::LOGO
    }
    #[cfg(not(target_os = "macos"))]
    {
        keyboard::Modifiers::CTRL
    }
}

fn messages(ui: Simulator<'_, Msg, Theme, Renderer>) -> Vec<Msg> {
    ui.into_messages().collect()
}

/// Selection order comes from a HashSet, so normalize before comparing.
fn sorted(mut v: Vec<usize>) -> Vec<usize> {
    v.sort_unstable();
    v
}

/// Last selection the graph reported, sorted.
fn last_selection(msgs: &[Msg]) -> Option<Vec<usize>> {
    msgs.iter().rev().find_map(|m| match m {
        Msg::Select(ids) => Some(sorted(ids.clone())),
        _ => None,
    })
}

// ---------------------------------------------------------------------------
// Selection
// ---------------------------------------------------------------------------

#[test]
fn click_selects_node() {
    let mut ui = Simulator::new(graph_with(&[(0, Point::new(100.0, 100.0))]));
    click(&mut ui, center(Point::new(100.0, 100.0)));
    assert_eq!(last_selection(&messages(ui)), Some(vec![0]));
}

#[test]
fn click_unselected_node_replaces_selection() {
    let mut ui = Simulator::new(graph_with(&[
        (0, Point::new(100.0, 100.0)),
        (1, Point::new(400.0, 100.0)),
    ]));
    click(&mut ui, center(Point::new(100.0, 100.0)));
    click(&mut ui, center(Point::new(400.0, 100.0)));
    // Plain click on a different node clears the old selection.
    assert_eq!(last_selection(&messages(ui)), Some(vec![1]));
}

#[test]
fn shift_click_adds_to_selection() {
    let mut ui = Simulator::new(graph_with(&[
        (0, Point::new(100.0, 100.0)),
        (1, Point::new(400.0, 100.0)),
    ]));
    click(&mut ui, center(Point::new(100.0, 100.0)));

    let shift = keyboard::Modifiers::SHIFT;
    let a = center(Point::new(400.0, 100.0));
    ui.point_at(a);
    ui.simulate([iced::Event::Keyboard(keyboard::Event::ModifiersChanged(
        shift,
    ))]);
    ui.simulate([moved(a), press(), release()]);

    assert_eq!(last_selection(&messages(ui)), Some(vec![0, 1]));
}

#[test]
fn click_empty_space_clears_selection() {
    let mut ui = Simulator::new(graph_with(&[(0, Point::new(100.0, 100.0))]));
    click(&mut ui, center(Point::new(100.0, 100.0)));
    // Press+release far from any node performs an empty box select -> clears.
    click(&mut ui, Point::new(700.0, 600.0));
    assert_eq!(last_selection(&messages(ui)), Some(vec![]));
}

#[test]
fn ctrl_a_selects_all() {
    let mut ui = Simulator::new(graph_with(&[
        (0, Point::new(100.0, 100.0)),
        (1, Point::new(400.0, 100.0)),
        (2, Point::new(700.0, 100.0)),
    ]));
    ui.point_at(Point::new(500.0, 400.0));
    ui.simulate([key_pressed(keyboard::Key::Character("a".into()), cmd())]);
    assert_eq!(last_selection(&messages(ui)), Some(vec![0, 1, 2]));
}

#[test]
fn escape_clears_selection() {
    let mut ui = Simulator::new(graph_with(&[(0, Point::new(100.0, 100.0))]));
    click(&mut ui, center(Point::new(100.0, 100.0)));
    ui.simulate([key_pressed(
        keyboard::Key::Named(keyboard::key::Named::Escape),
        keyboard::Modifiers::default(),
    )]);
    assert_eq!(last_selection(&messages(ui)), Some(vec![]));
}

#[test]
fn box_select_grabs_enclosed_nodes() {
    let mut ui = Simulator::new(graph_with(&[
        (0, Point::new(100.0, 100.0)),
        (1, Point::new(300.0, 100.0)),
        (2, Point::new(700.0, 500.0)), // outside the box
    ]));
    // Drag a box over nodes 0 and 1 only, starting on empty space.
    drag(&mut ui, Point::new(50.0, 50.0), Point::new(400.0, 200.0));
    assert_eq!(last_selection(&messages(ui)), Some(vec![0, 1]));
}

// ---------------------------------------------------------------------------
// Movement
// ---------------------------------------------------------------------------

#[test]
fn drag_node_emits_move_with_delta() {
    let start = Point::new(100.0, 100.0);
    let mut ui = Simulator::new(graph_with(&[(0, start)]));
    // Drag the body center by (+50, +20).
    drag(
        &mut ui,
        center(start),
        center(start) + Vector::new(50.0, 20.0),
    );

    let msgs = messages(ui);
    let moved = msgs.iter().find_map(|m| match m {
        Msg::Move(delta, ids) => Some((*delta, sorted(ids.clone()))),
        _ => None,
    });
    let (delta, ids) = moved.expect("dragging a node must emit Move");
    assert_eq!(ids, vec![0]);
    assert!(
        (delta.x - 50.0).abs() < 0.5 && (delta.y - 20.0).abs() < 0.5,
        "node should move by (50, 20), got {delta:?}",
    );
}

#[test]
fn group_move_emits_move_with_delta_and_all_ids() {
    let mut ui = Simulator::new(graph_with(&[
        (0, Point::new(100.0, 100.0)),
        (1, Point::new(400.0, 100.0)),
    ]));
    // Select both, then drag one of them: the move reports the whole group.
    ui.point_at(Point::new(500.0, 400.0));
    ui.simulate([key_pressed(keyboard::Key::Character("a".into()), cmd())]);
    let from = center(Point::new(100.0, 100.0));
    drag(&mut ui, from, from + Vector::new(30.0, -10.0));

    let msgs = messages(ui);
    let group = msgs.iter().find_map(|m| match m {
        Msg::Move(delta, ids) => Some((*delta, sorted(ids.clone()))),
        _ => None,
    });
    let (delta, ids) = group.expect("dragging a multi-selection must emit Move");
    assert_eq!(ids, vec![0, 1]);
    assert!(
        (delta.x - 30.0).abs() < 0.5 && (delta.y + 10.0).abs() < 0.5,
        "group delta should be (30, -10), got {delta:?}",
    );
}

// ---------------------------------------------------------------------------
// Keyboard commands
// ---------------------------------------------------------------------------

#[test]
fn delete_key_requests_delete_of_selection() {
    let mut ui = Simulator::new(graph_with(&[(0, Point::new(100.0, 100.0))]));
    click(&mut ui, center(Point::new(100.0, 100.0)));
    ui.simulate([key_pressed(
        keyboard::Key::Named(keyboard::key::Named::Delete),
        keyboard::Modifiers::default(),
    )]);

    let msgs = messages(ui);
    assert!(
        msgs.contains(&Msg::Delete(vec![0])),
        "Delete key must request deletion of the selection: {msgs:?}",
    );
}

#[test]
fn ctrl_d_requests_clone_of_selection() {
    let mut ui = Simulator::new(graph_with(&[(0, Point::new(100.0, 100.0))]));
    click(&mut ui, center(Point::new(100.0, 100.0)));
    ui.simulate([key_pressed(keyboard::Key::Character("d".into()), cmd())]);

    let msgs = messages(ui);
    assert!(
        msgs.contains(&Msg::Clone(vec![0])),
        "Ctrl+D must request cloning of the selection: {msgs:?}",
    );
}

#[test]
fn ctrl_d_without_selection_does_nothing() {
    let mut ui = Simulator::new(graph_with(&[(0, Point::new(100.0, 100.0))]));
    ui.point_at(Point::new(500.0, 400.0));
    ui.simulate([key_pressed(keyboard::Key::Character("d".into()), cmd())]);

    let msgs = messages(ui);
    assert!(
        !msgs.iter().any(|m| matches!(m, Msg::Clone(_))),
        "Ctrl+D with no selection must not request a clone: {msgs:?}",
    );
}

#[test]
fn click_without_motion_does_not_emit_move() {
    // Regression: a press+release in place is a selection click, not a drag.
    // It must not emit a NodeMoved (which would dirty host undo history /
    // sync state on every click).
    let mut ui = Simulator::new(graph_with(&[(0, Point::new(100.0, 100.0))]));
    click(&mut ui, center(Point::new(100.0, 100.0)));
    let msgs = messages(ui);
    assert!(
        !msgs.iter().any(|m| matches!(m, Msg::Move(..))),
        "a click without motion must not emit Move: {msgs:?}",
    );
}

// ---------------------------------------------------------------------------
// Edge connect / disconnect (pin drag)
//
// Each node here holds exactly one fixed-size pin, so the connection anchor is
// predictable: a Right pin anchors at the node's right edge, a Left pin at its
// left edge, both at the node's vertical center. With NODE_W x NODE_H content
// at world top-left `p`, the anchors are:
//   output (Right): (p.x + NODE_W, p.y + NODE_H/2)
//   input  (Left) : (p.x,          p.y + NODE_H/2)
// ---------------------------------------------------------------------------

const OUT_POS: Point = Point::new(100.0, 100.0);
const IN_POS: Point = Point::new(300.0, 100.0);

fn out_anchor() -> Point {
    Point::new(OUT_POS.x + NODE_W, OUT_POS.y + NODE_H / 2.0)
}
fn in_anchor() -> Point {
    Point::new(IN_POS.x, IN_POS.y + NODE_H / 2.0)
}

fn pin_body() -> iced::widget::Container<'static, Msg, Theme, Renderer> {
    container(text("p"))
        .width(Length::Fixed(NODE_W))
        .height(Length::Fixed(NODE_H))
}

/// Two single-pin nodes: node 0 has a Right/Output pin, node 1 a Left/Input pin.
/// `connect_ok` drives `can_connect`; `seed_edge` pre-pushes edge 0:0 -> 1:0.
fn pin_graph(connect_ok: bool, seed_edge: bool) -> Element<'static, Msg, Theme, Renderer> {
    let mut ng: Graph = NodeGraph::default()
        .width(Length::Fill)
        .height(Length::Fill)
        .on_connect(Msg::Connect)
        .on_disconnect(Msg::Disconnect)
        .can_connect(move |_, _| connect_ok);
    ng.push_node(node(
        0usize,
        OUT_POS,
        pin!(Right, 0usize, pin_body(), Output),
    ));
    ng.push_node(node(1usize, IN_POS, pin!(Left, 0usize, pin_body(), Input)));
    if seed_edge {
        ng.push_edge(edge!(PinRef::new(0, 0), PinRef::new(1, 0)));
    }
    ng.into()
}

#[test]
fn drag_output_to_input_connects() {
    let mut ui = Simulator::new(pin_graph(true, false));
    drag(&mut ui, out_anchor(), in_anchor());

    let msgs = messages(ui);
    assert!(
        msgs.contains(&Msg::Connect(PinRef::new(0, 0), PinRef::new(1, 0))),
        "dragging output -> input must connect them: {msgs:?}",
    );
}

#[test]
fn drag_input_to_output_reports_output_first() {
    // Drag starts on the INPUT pin; the reported pair must still be
    // output-first (orient_connection), matching the rendered data-flow.
    let mut ui = Simulator::new(pin_graph(true, false));
    drag(&mut ui, in_anchor(), out_anchor());

    let msgs = messages(ui);
    assert!(
        msgs.contains(&Msg::Connect(PinRef::new(0, 0), PinRef::new(1, 0))),
        "connection must be normalized output-first regardless of drag direction: {msgs:?}",
    );
}

#[test]
fn drag_to_empty_space_does_not_connect() {
    let mut ui = Simulator::new(pin_graph(true, false));
    drag(&mut ui, out_anchor(), Point::new(600.0, 500.0));

    let msgs = messages(ui);
    assert!(
        !msgs.iter().any(|m| matches!(m, Msg::Connect(_, _))),
        "releasing over empty space must not connect: {msgs:?}",
    );
}

#[test]
fn can_connect_false_blocks_connection() {
    let mut ui = Simulator::new(pin_graph(false, false));
    drag(&mut ui, out_anchor(), in_anchor());

    let msgs = messages(ui);
    assert!(
        !msgs.iter().any(|m| matches!(m, Msg::Connect(_, _))),
        "can_connect returning false must block the snap/connect: {msgs:?}",
    );
}

#[test]
fn ctrl_click_on_edge_disconnects() {
    // Ctrl+click on the edge line (Fruit Ninja cut) disconnects it.
    let mut ui = Simulator::new(pin_graph(true, true));
    let mid = Point::new((out_anchor().x + in_anchor().x) / 2.0, out_anchor().y);
    ui.point_at(mid);
    // ModifiersChanged + a CursorMoved so pins compute their anchors, then a
    // ctrl-held press on the edge.
    ui.simulate([
        iced::Event::Keyboard(keyboard::Event::ModifiersChanged(cmd())),
        moved(mid),
    ]);
    ui.simulate([press(), release()]);

    let msgs = messages(ui);
    assert!(
        msgs.contains(&Msg::Disconnect(PinRef::new(0, 0), PinRef::new(1, 0))),
        "ctrl+click on an edge must disconnect it: {msgs:?}",
    );
}

// ---------------------------------------------------------------------------
// Camera: right-drag pan and wheel zoom
// ---------------------------------------------------------------------------

fn camera_graph() -> Element<'static, Msg, Theme, Renderer> {
    let mut ng: Graph = NodeGraph::default()
        .width(Length::Fill)
        .height(Length::Fill)
        .on_pan(Msg::Camera);
    ng.push_node(node(
        0usize,
        Point::new(100.0, 100.0),
        container(text("n"))
            .width(Length::Fixed(NODE_W))
            .height(Length::Fixed(NODE_H)),
    ));
    ng.into()
}

fn last_camera(msgs: &[Msg]) -> Option<(Point, f32)> {
    msgs.iter().rev().find_map(|m| match m {
        Msg::Camera(pos, zoom) => Some((*pos, *zoom)),
        _ => None,
    })
}

fn right_press() -> iced::Event {
    iced::Event::Mouse(mouse::Event::ButtonPressed(mouse::Button::Right))
}
fn right_release() -> iced::Event {
    iced::Event::Mouse(mouse::Event::ButtonReleased(mouse::Button::Right))
}

#[test]
fn right_drag_pans_camera() {
    let mut ui = Simulator::new(camera_graph());
    let from = Point::new(400.0, 400.0);
    let to = Point::new(460.0, 430.0); // +60, +30 screen
    ui.point_at(from);
    ui.simulate([moved(from), right_press()]);
    ui.point_at(to);
    ui.simulate([moved(to), right_release()]);

    let msgs = messages(ui);
    let (pos, zoom) = last_camera(&msgs).expect("right-drag must change the camera");
    // At zoom 1, panning by (+60,+30) screen shifts the camera position by the
    // same world amount.
    assert!(
        (zoom - 1.0).abs() < 1e-3,
        "pan must not change zoom: {zoom}"
    );
    assert!(
        (pos.x - 60.0).abs() < 1.0 && (pos.y - 30.0).abs() < 1.0,
        "camera should pan by (60, 30), got {pos:?}",
    );
}

#[test]
fn wheel_scroll_zooms_camera() {
    let mut ui = Simulator::new(camera_graph());
    let at = Point::new(400.0, 400.0);
    ui.point_at(at);
    ui.simulate([
        moved(at),
        iced::Event::Mouse(mouse::Event::WheelScrolled {
            delta: mouse::ScrollDelta::Lines { x: 0.0, y: 3.0 },
        }),
    ]);

    let msgs = messages(ui);
    let (_pos, zoom) = last_camera(&msgs).expect("wheel scroll must change the camera");
    assert!(
        zoom > 1.0,
        "scrolling up must zoom in (zoom > 1), got {zoom}",
    );
}

// ---------------------------------------------------------------------------
// Magnetic-plug grab: hysteresis + re-wiring
//
// Grabbing a CONNECTED pin does not disconnect on contact. The edge stays
// snapped (EdgeOver) until the cursor leaves the grabbed pin by more than
// UNSNAP_THRESHOLD (15px); only then does on_disconnect fire. The grabbed end
// can then be dropped on another compatible pin to re-wire.
// ---------------------------------------------------------------------------

fn last_msgs_after_grab(to: Point) -> Vec<Msg> {
    // Seeded edge 0:0 (output) -> 1:0 (input). Grab the input pin and drag to
    // `to`, then release.
    let mut ui = Simulator::new(pin_graph(true, true));
    let from = in_anchor();
    ui.point_at(from);
    ui.simulate([moved(from), press()]);
    ui.point_at(to);
    ui.simulate([moved(to), release()]);
    messages(ui)
}

#[test]
fn grabbing_connected_pin_in_place_keeps_connection() {
    // Press + release on a connected pin without moving: still connected.
    let msgs = last_msgs_after_grab(in_anchor());
    assert!(
        !msgs.iter().any(|m| matches!(m, Msg::Disconnect(_, _))),
        "grabbing a connected pin in place must not disconnect: {msgs:?}",
    );
}

#[test]
fn dragging_connected_pin_within_hysteresis_keeps_connection() {
    // Move 10px (< UNSNAP_THRESHOLD 15): magnetically stays connected.
    let near = Point::new(in_anchor().x + 10.0, in_anchor().y);
    let msgs = last_msgs_after_grab(near);
    assert!(
        !msgs.iter().any(|m| matches!(m, Msg::Disconnect(_, _))),
        "a sub-threshold drag must not unplug the connection: {msgs:?}",
    );
}

#[test]
fn dragging_connected_pin_past_hysteresis_disconnects() {
    // Move 30px (> UNSNAP_THRESHOLD 15): the plug pops out.
    let far = Point::new(in_anchor().x + 30.0, in_anchor().y);
    let msgs = last_msgs_after_grab(far);
    assert!(
        msgs.contains(&Msg::Disconnect(PinRef::new(0, 0), PinRef::new(1, 0))),
        "dragging past the hysteresis threshold must disconnect: {msgs:?}",
    );
}

// Three nodes: output 0:0 -> input 1:0 (seeded), plus a spare input 2:0.
fn rewire_graph() -> Element<'static, Msg, Theme, Renderer> {
    let mut ng: Graph = NodeGraph::default()
        .width(Length::Fill)
        .height(Length::Fill)
        .on_connect(Msg::Connect)
        .on_disconnect(Msg::Disconnect);
    ng.push_node(node(
        0usize,
        OUT_POS,
        pin!(Right, 0usize, pin_body(), Output),
    ));
    ng.push_node(node(1usize, IN_POS, pin!(Left, 0usize, pin_body(), Input)));
    ng.push_node(node(
        2usize,
        Point::new(IN_POS.x, 300.0),
        pin!(Left, 0usize, pin_body(), Input),
    ));
    ng.push_edge(edge!(PinRef::new(0, 0), PinRef::new(1, 0)));
    ng.into()
}

#[test]
fn rewire_grabbed_pin_to_another_pin() {
    // Grab the input end of 0:0 -> 1:0, pull it past the threshold (pop), then
    // drop it on input 2:0. Expect the old edge to disconnect and a new edge to
    // 2:0 to connect. The pop and the re-snap need separate cursor moves.
    let mut ui = Simulator::new(rewire_graph());
    let grab = in_anchor(); // node 1 input
    let target = Point::new(IN_POS.x, 315.0); // node 2 input anchor

    ui.point_at(grab);
    ui.simulate([moved(grab), press()]);
    // Pull straight down, clearing node 1 pin by more than UNSNAP_THRESHOLD.
    let midway = Point::new(grab.x, 220.0);
    ui.point_at(midway);
    ui.simulate([moved(midway)]);
    // Now snap onto node 2 input.
    ui.point_at(target);
    ui.simulate([moved(target), release()]);

    let msgs = messages(ui);
    assert!(
        msgs.contains(&Msg::Disconnect(PinRef::new(0, 0), PinRef::new(1, 0))),
        "re-wiring must disconnect the original edge: {msgs:?}",
    );
    assert!(
        msgs.contains(&Msg::Connect(PinRef::new(0, 0), PinRef::new(2, 0))),
        "re-wiring must connect the grabbed end to the new pin: {msgs:?}",
    );
}

#[test]
fn rewire_back_to_own_input_reconnects() {
    // Grab the input end of 0:0 -> 1:0, pull it past the threshold (disconnect),
    // then drop it back on the SAME input 1:0. Under the default
    // (input_not_occupied), this only works because the edge being dragged is
    // excluded from the occupancy check, so its own input stays a valid target.
    let mut ui = Simulator::new(rewire_graph());
    let grab = in_anchor();

    ui.point_at(grab);
    ui.simulate([moved(grab), press()]);
    let midway = Point::new(grab.x, 220.0); // clear node 1 pin past UNSNAP_THRESHOLD
    ui.point_at(midway);
    ui.simulate([moved(midway)]);
    ui.point_at(grab); // back onto the original input
    ui.simulate([moved(grab), release()]);

    let msgs = messages(ui);
    assert!(
        msgs.iter().any(|m| matches!(m, Msg::Disconnect(_, _))),
        "popping the edge off its input must disconnect first: {msgs:?}",
    );
    assert!(
        msgs.contains(&Msg::Connect(PinRef::new(0, 0), PinRef::new(1, 0))),
        "dropping back on the original input must reconnect it: {msgs:?}",
    );
}

#[test]
fn default_rejects_second_edge_to_occupied_input() {
    // No can_connect: the built-in default enforces one-edge-per-input. Input 1:0
    // is already wired from 0:0, so dragging a second output (2:0) onto it must not
    // connect.
    let mut ng: Graph = NodeGraph::default()
        .width(Length::Fill)
        .height(Length::Fill)
        .on_connect(Msg::Connect);
    ng.push_node(node(
        0usize,
        OUT_POS,
        pin!(Right, 0usize, pin_body(), Output),
    ));
    ng.push_node(node(1usize, IN_POS, pin!(Left, 0usize, pin_body(), Input)));
    ng.push_node(node(
        2usize,
        Point::new(OUT_POS.x, 300.0),
        pin!(Right, 0usize, pin_body(), Output),
    ));
    ng.push_edge(edge!(PinRef::new(0, 0), PinRef::new(1, 0)));
    let mut ui = Simulator::new(Element::from(ng));

    let from = Point::new(OUT_POS.x + NODE_W, 300.0 + NODE_H / 2.0); // node 2 right pin
    drag(&mut ui, from, in_anchor());

    let msgs = messages(ui);
    assert!(
        !msgs.contains(&Msg::Connect(PinRef::new(2, 0), PinRef::new(1, 0))),
        "default one-edge-per-input must reject a second edge to an occupied input: {msgs:?}",
    );
}

// ---------------------------------------------------------------------------
// Occluded interactions: a node body on top covering another node pin.
//
// Node 2 is a plain body (no pin) placed on top, covering node 1 input anchor.
// Expected: you can DROP a connection onto the covered pin (snap sees all pins
// regardless of cover), but you cannot START an edge drag from it (the covering
// body intercepts the press).
// ---------------------------------------------------------------------------

fn occlusion_graph() -> Element<'static, Msg, Theme, Renderer> {
    let mut ng: Graph = NodeGraph::default()
        .width(Length::Fill)
        .height(Length::Fill)
        .on_connect(Msg::Connect)
        .on_disconnect(Msg::Disconnect)
        .on_select(Msg::Select)
        .on_move(Msg::Move);
    ng.push_node(node(
        0usize,
        OUT_POS,
        pin!(Right, 0usize, pin_body(), Output),
    ));
    ng.push_node(node(1usize, IN_POS, pin!(Left, 0usize, pin_body(), Input)));
    // Cover node 1 input anchor (IN_POS.x, IN_POS.y + H/2) with a plain body.
    ng.push_node(node(
        2usize,
        Point::new(IN_POS.x - NODE_W / 2.0, IN_POS.y),
        container(text("cover"))
            .width(Length::Fixed(NODE_W))
            .height(Length::Fixed(NODE_H)),
    ));
    ng.into()
}

#[test]
fn drop_connect_through_covering_node_is_possible() {
    // Drag from the visible output and drop on the covered input: snap reaches
    // the pin under the cover, so the connection forms.
    let mut ui = Simulator::new(occlusion_graph());
    drag(&mut ui, out_anchor(), in_anchor());

    let msgs = messages(ui);
    assert!(
        msgs.contains(&Msg::Connect(PinRef::new(0, 0), PinRef::new(1, 0))),
        "dropping onto a covered pin must still connect: {msgs:?}",
    );
}

#[test]
fn drag_start_on_covered_pin_is_blocked() {
    // Press on the covered input pin: the covering body (node 2) takes the
    // press, so no edge drag starts. Dragging to the output therefore connects
    // nothing.
    let mut ui = Simulator::new(occlusion_graph());
    drag(&mut ui, in_anchor(), out_anchor());

    let msgs = messages(ui);
    assert!(
        !msgs.iter().any(|m| matches!(m, Msg::Connect(_, _))),
        "a covered pin must not start an edge drag: {msgs:?}",
    );
    // The covering node is what actually got grabbed.
    assert!(
        msgs.iter()
            .any(|m| matches!(m, Msg::Select(ids) if ids.contains(&2))),
        "the covering node should receive the press: {msgs:?}",
    );
}

// ---------------------------------------------------------------------------
// Occluded zoom: an opaque overlay on top of the graph swallows the wheel, so
// the covered graph must not zoom.
// ---------------------------------------------------------------------------

fn overlaid_graph() -> Element<'static, Msg, Theme, Renderer> {
    let mut ng: Graph = NodeGraph::default()
        .width(Length::Fill)
        .height(Length::Fill)
        .on_pan(Msg::Camera);
    ng.push_node(node(
        0usize,
        Point::new(100.0, 100.0),
        container(text("n"))
            .width(Length::Fixed(NODE_W))
            .height(Length::Fixed(NODE_H)),
    ));
    let graph: Element<'static, Msg, Theme, Renderer> = ng.into();
    let overlay =
        iced::widget::opaque(container(text("")).width(Length::Fill).height(Length::Fill));
    iced::widget::stack![graph, overlay].into()
}

#[test]
fn wheel_over_opaque_overlay_does_not_zoom_graph() {
    let mut ui = Simulator::new(overlaid_graph());
    let at = Point::new(400.0, 400.0);
    ui.point_at(at);
    ui.simulate([
        moved(at),
        iced::Event::Mouse(mouse::Event::WheelScrolled {
            delta: mouse::ScrollDelta::Lines { x: 0.0, y: 3.0 },
        }),
    ]);

    let msgs = messages(ui);
    assert!(
        !msgs.iter().any(|m| matches!(m, Msg::Camera(_, _))),
        "a covered graph must not zoom under the overlay: {msgs:?}",
    );
}

// ---------------------------------------------------------------------------
// Child-widget interaction: events route to widgets INSIDE a node first.
// ---------------------------------------------------------------------------

#[test]
fn click_on_button_in_node_routes_to_button_not_node() {
    // A button inside a node must consume the click; the node must NOT select.
    let mut ng: Graph = NodeGraph::default()
        .width(Length::Fill)
        .height(Length::Fill)
        .on_select(Msg::Select);
    ng.push_node(node(
        0usize,
        Point::new(100.0, 100.0),
        iced::widget::button(text("go"))
            .width(Length::Fixed(80.0))
            .height(Length::Fixed(30.0))
            .on_press(Msg::Button),
    ));
    let mut ui = Simulator::new(Element::from(ng));

    click(&mut ui, Point::new(140.0, 115.0)); // inside the button

    let msgs = messages(ui);
    assert!(
        msgs.contains(&Msg::Button),
        "the button inside the node must receive the click: {msgs:?}",
    );
    assert!(
        !msgs.iter().any(|m| matches!(m, Msg::Select(_))),
        "clicking a child button must not select the node: {msgs:?}",
    );
}

#[test]
fn backspace_in_focused_text_input_does_not_delete_node() {
    // With a text_input inside a node focused, Backspace edits the text; the
    // node (even when selected) must survive, because the input consumes the
    // key before the graph's delete handler runs.
    let mut ng: Graph = NodeGraph::default()
        .width(Length::Fill)
        .height(Length::Fill)
        .on_select(Msg::Select)
        .on_delete(Msg::Delete);
    ng.push_node(node(
        0usize,
        Point::new(100.0, 100.0),
        iced::widget::text_input("", "abc")
            .width(Length::Fixed(120.0))
            .on_input(Msg::Input),
    ));
    let mut ui = Simulator::new(Element::from(ng));

    // Focus the input, then select the node via Ctrl+A (handled by the graph
    // before children), then Backspace.
    click(&mut ui, Point::new(150.0, 115.0));
    ui.simulate([key_pressed(keyboard::Key::Character("a".into()), cmd())]);
    ui.simulate([key_pressed(
        keyboard::Key::Named(keyboard::key::Named::Backspace),
        keyboard::Modifiers::default(),
    )]);

    let msgs = messages(ui);
    assert!(
        !msgs.iter().any(|m| matches!(m, Msg::Delete(_))),
        "Backspace in a focused text_input must not delete the node: {msgs:?}",
    );
    assert!(
        msgs.iter().any(|m| matches!(m, Msg::Input(s) if s == "ab")),
        "the focused text_input should have consumed Backspace (abc -> ab): {msgs:?}",
    );
}

// ---------------------------------------------------------------------------
// Duplicate node id: debug-build guard (node_index resolves to the first match,
// so a duplicate id renders/behaves undefined). Edge ids are intentionally NOT
// guarded - `edge!` defaults them to `()` and edges are addressed by index.
// ---------------------------------------------------------------------------

#[cfg(debug_assertions)]
#[test]
#[should_panic(expected = "duplicate node id")]
fn push_node_rejects_duplicate_id_in_debug() {
    let mut ng: Graph = NodeGraph::default();
    let body = || {
        container(text("n"))
            .width(Length::Fixed(NODE_W))
            .height(Length::Fixed(NODE_H))
    };
    ng.push_node(node(7usize, Point::new(0.0, 0.0), body()));
    ng.push_node(node(7usize, Point::new(50.0, 50.0), body()));
}

// ---------------------------------------------------------------------------
// Connection validation (no can_connect): direction + self-pin rules.
//
// Duplicate-edge rejection is intentionally NOT a widget guarantee - it is the
// host's job via can_connect - so it is not asserted here.
// ---------------------------------------------------------------------------

#[test]
fn output_to_output_does_not_connect() {
    let mut ng: Graph = NodeGraph::default()
        .width(Length::Fill)
        .height(Length::Fill)
        .on_connect(Msg::Connect);
    ng.push_node(node(
        0usize,
        OUT_POS,
        pin!(Right, 0usize, pin_body(), Output),
    ));
    // Second output pin, anchored at IN_POS left edge for an easy drag target.
    ng.push_node(node(1usize, IN_POS, pin!(Left, 0usize, pin_body(), Output)));
    let mut ui = Simulator::new(Element::from(ng));

    drag(&mut ui, out_anchor(), in_anchor());

    let msgs = messages(ui);
    assert!(
        !msgs.iter().any(|m| matches!(m, Msg::Connect(_, _))),
        "two output pins must not connect (direction rule): {msgs:?}",
    );
}

#[test]
fn cannot_connect_pin_to_itself() {
    // Dragging a pin and releasing back on itself must not self-connect (the
    // source pin is excluded from valid targets).
    let mut ng: Graph = NodeGraph::default()
        .width(Length::Fill)
        .height(Length::Fill)
        .on_connect(Msg::Connect);
    ng.push_node(node(
        0usize,
        OUT_POS,
        pin!(Right, 0usize, pin_body(), Output),
    ));
    let mut ui = Simulator::new(Element::from(ng));

    let a = out_anchor();
    ui.point_at(a);
    ui.simulate([moved(a), press()]);
    let nudge = Point::new(a.x + 3.0, a.y); // small move, still on the pin
    ui.point_at(nudge);
    ui.simulate([moved(nudge), release()]);

    let msgs = messages(ui);
    assert!(
        !msgs.iter().any(|m| matches!(m, Msg::Connect(_, _))),
        "a pin must not connect to itself: {msgs:?}",
    );
}

// ---------------------------------------------------------------------------
// Zoom-at-cursor stability: the world point under the cursor stays fixed.
// ---------------------------------------------------------------------------

#[test]
fn wheel_zoom_keeps_world_point_under_cursor() {
    let mut ui = Simulator::new(camera_graph());
    let at = Point::new(400.0, 300.0);
    // Default camera (zoom 1, pos 0): world under the cursor == screen point.
    ui.point_at(at);
    ui.simulate([
        moved(at),
        iced::Event::Mouse(mouse::Event::WheelScrolled {
            delta: mouse::ScrollDelta::Lines { x: 0.0, y: 4.0 },
        }),
    ]);

    let msgs = messages(ui);
    let (pos, zoom) = last_camera(&msgs).expect("wheel must change the camera");
    assert!(zoom > 1.0, "scroll up should zoom in: {zoom}");
    // screen_to_world: world = screen/zoom - position. The world point under the
    // cursor must be unchanged (== `at`).
    let wx = at.x / zoom - pos.x;
    let wy = at.y / zoom - pos.y;
    assert!(
        (wx - at.x).abs() < 0.5 && (wy - at.y).abs() < 0.5,
        "world point under cursor drifted after zoom: was {at:?}, now ({wx}, {wy})",
    );
}

// ---------------------------------------------------------------------------
// Hit detection under zoom + pan: the real widget pipeline must locate pins and
// edges when the camera is NOT at the default (zoom 1, no pan), so world pixels
// differ from screen pixels. The other tests run at zoom 1 (world == screen),
// which never exercises the screen<->world transform in hit detection.
//
// World->screen with camera (position, zoom): screen = (world + position) * zoom.
// ---------------------------------------------------------------------------

const CAM_POS: Point = Point::new(50.0, 50.0);
const CAM_ZOOM: f32 = 2.0;

/// Maps a world point to its screen pixel under the (CAM_POS, CAM_ZOOM) camera.
fn world_to_screen(world: Point) -> Point {
    Point::new(
        (world.x + CAM_POS.x) * CAM_ZOOM,
        (world.y + CAM_POS.y) * CAM_ZOOM,
    )
}

/// The same two single-pin nodes as `pin_graph`, but viewed through a zoomed and
/// panned camera, so pin anchors land at non-trivial screen pixels.
fn zoomed_pin_graph(seed_edge: bool) -> Element<'static, Msg, Theme, Renderer> {
    let mut ng: Graph = NodeGraph::default()
        .width(Length::Fill)
        .height(Length::Fill)
        .view(CAM_POS, CAM_ZOOM)
        .on_connect(Msg::Connect)
        .on_disconnect(Msg::Disconnect);
    ng.push_node(node(
        0usize,
        OUT_POS,
        pin!(Right, 0usize, pin_body(), Output),
    ));
    ng.push_node(node(1usize, IN_POS, pin!(Left, 0usize, pin_body(), Input)));
    if seed_edge {
        ng.push_edge(edge!(PinRef::new(0, 0), PinRef::new(1, 0)));
    }
    ng.into()
}

#[test]
fn drag_connects_under_zoom_and_pan() {
    // The output and input anchors are world points; their screen pixels depend
    // on the camera. Correct screen->world hit detection means dragging between
    // the two screen pixels connects them just as it does at zoom 1.
    let mut ui = Simulator::new(zoomed_pin_graph(false));
    drag(
        &mut ui,
        world_to_screen(out_anchor()),
        world_to_screen(in_anchor()),
    );

    let msgs = messages(ui);
    assert!(
        msgs.contains(&Msg::Connect(PinRef::new(0, 0), PinRef::new(1, 0))),
        "dragging output -> input under zoom+pan must connect them: {msgs:?}",
    );
}

#[test]
fn ctrl_click_on_edge_disconnects_under_zoom_and_pan() {
    // Ctrl+click on the edge midpoint (in screen space) must hit the edge line
    // even though world != screen, exercising edge hit detection under zoom.
    let mut ui = Simulator::new(zoomed_pin_graph(true));
    // Both anchors share a world y, so the bezier midpoint sits on that y.
    let mid_world = Point::new((out_anchor().x + in_anchor().x) / 2.0, out_anchor().y);
    let mid = world_to_screen(mid_world);
    ui.point_at(mid);
    ui.simulate([
        iced::Event::Keyboard(keyboard::Event::ModifiersChanged(cmd())),
        moved(mid),
    ]);
    ui.simulate([press(), release()]);

    let msgs = messages(ui);
    assert!(
        msgs.contains(&Msg::Disconnect(PinRef::new(0, 0), PinRef::new(1, 0))),
        "ctrl+click on the edge under zoom+pan must disconnect it: {msgs:?}",
    );
}

// ---------------------------------------------------------------------------
// Shift-click toggles selection off.
// ---------------------------------------------------------------------------

#[test]
fn shift_click_deselects_already_selected_node() {
    let mut ui = Simulator::new(graph_with(&[(0, Point::new(100.0, 100.0))]));
    let c = center(Point::new(100.0, 100.0));
    click(&mut ui, c); // select node 0

    ui.point_at(c);
    ui.simulate([iced::Event::Keyboard(keyboard::Event::ModifiersChanged(
        keyboard::Modifiers::SHIFT,
    ))]);
    ui.simulate([moved(c), press(), release()]); // shift-click again -> toggle off

    assert_eq!(last_selection(&messages(ui)), Some(vec![]));
}

// ---------------------------------------------------------------------------
// Snapshot regression: a node dragged to the graph edge (partially clipped)
// and back to its origin (still held) must render identically to before the
// drag. If clip/culling is stale (computed before the move), the previously
// clipped side stays clipped.
//
// Backend NOTE: iced_test renders with WGPU when a GPU/adapter is available
// (golden files are suffixed `-wgpu`), else it falls back to tiny_skia where
// SDF `draw_primitive` is a no-op. So snapshots see the SDF node fill/border/
// pins only under WGPU; the iced child content (the colored body + text here)
// renders under both. This test asserts on the child content, which exercises
// the clip path regardless of backend.
// ---------------------------------------------------------------------------

fn snapshot_node_graph() -> Element<'static, Msg, Theme, Renderer> {
    let mut ng: Graph = NodeGraph::default()
        .width(Length::Fill)
        .height(Length::Fill)
        .on_select(Msg::Select)
        // on_move is required for node dragging (the widget gates the drag on it).
        .on_move(Msg::Move);
    // Left-aligned text inside the body, so it lands on the side that gets
    // clipped when the node is dragged off the left edge.
    let body = container(text("HELLO WORLD").size(24))
        .width(Length::Fixed(160.0))
        .height(Length::Fixed(80.0))
        .style(|_theme| iced::widget::container::Style {
            background: Some(iced::Background::Color(iced::Color::from_rgb(
                0.9, 0.2, 0.2,
            ))),
            text_color: Some(iced::Color::WHITE),
            ..Default::default()
        });
    // Centered: 1024x768 -> node spans (432,344)..(592,424).
    ng.push_node(node(0usize, Point::new(432.0, 344.0), body));
    ng.into()
}

/// Removes a golden and the backend-suffixed variants iced_test may have
/// written (`-wgpu`, `-tiny-skia`), so each run starts from a clean reference.
fn clear_golden(stem: &str) {
    let dir = std::env::temp_dir();
    for suffix in ["", "-wgpu", "-tiny-skia"] {
        let _ = std::fs::remove_file(dir.join(format!("{stem}{suffix}.png")));
    }
}

#[test]
fn node_dragged_to_edge_and_back_renders_identically() {
    // Regression: dragging a node so its child content is clipped at the graph
    // edge and back to the origin (still held) must restore the render exactly.
    // The clip is recomputed per frame from the live drag offset, which is
    // exactly 0.0 on return, so the round trip is pixel-identical.
    let mut ui = Simulator::new(snapshot_node_graph());
    let origin = Point::new(512.0, 384.0); // node body center

    click(&mut ui, origin); // select
    let at_origin = ui.snapshot(&Theme::Dark).expect("origin snapshot");

    ui.point_at(origin);
    ui.simulate([moved(origin), press()]);
    let edge = Point::new(30.0, 384.0); // offset ~ -482 -> node left edge ~ -50
    ui.point_at(edge);
    ui.simulate([moved(edge)]);
    let at_edge = ui.snapshot(&Theme::Dark).expect("edge snapshot");
    ui.point_at(origin);
    ui.simulate([moved(origin)]); // back to start, still dragging
    let back = ui.snapshot(&Theme::Dark).expect("round-trip snapshot");

    // Golden holds the origin frame; compare the other two against it.
    // (matches_image appends a `-<backend>` suffix and creates the file when
    // absent; temp dir keeps any leftover out of the repo.)
    let stem = "iced_ng_dragback_origin";
    clear_golden(stem);
    let golden = std::env::temp_dir().join(format!("{stem}.png"));
    let _ = at_origin.matches_image(&golden).expect("write golden");
    let edge_differs = !at_edge.matches_image(&golden).expect("edge vs origin");
    let back_matches = back.matches_image(&golden).expect("round-trip vs origin");
    clear_golden(stem);

    // Guard against a vacuous test: the edge frame must actually differ.
    assert!(
        edge_differs,
        "edge frame should differ from origin (drag/clip not exercised)",
    );
    assert!(
        back_matches,
        "node dragged to the edge and back must render identically to origin",
    );
}

#[test]
fn pin_press_without_on_connect_falls_through_to_selection() {
    // Gating: with no on_connect wired, pressing a pin must not start an edge drag;
    // the press falls through to selecting the pin's node instead.
    let mut ng: Graph = NodeGraph::default()
        .width(Length::Fill)
        .height(Length::Fill)
        .on_select(Msg::Select); // deliberately no on_connect
    ng.push_node(node(
        0usize,
        OUT_POS,
        pin!(Right, 0usize, pin_body(), Output),
    ));
    ng.push_node(node(1usize, IN_POS, pin!(Left, 0usize, pin_body(), Input)));
    let mut ui = Simulator::new(Element::from(ng));

    // Just inside node 0's right edge: within PIN_CLICK_THRESHOLD of the pin, yet
    // still over the body, so a blocked edge drag falls through to body selection.
    let near_pin = Point::new(OUT_POS.x + NODE_W - 2.0, OUT_POS.y + NODE_H / 2.0);
    drag(&mut ui, near_pin, in_anchor());

    let msgs = messages(ui);
    assert!(
        !msgs.iter().any(|m| matches!(m, Msg::Connect(_, _))),
        "without on_connect, pressing a pin must not start an edge: {msgs:?}",
    );
    assert!(
        msgs.iter()
            .any(|m| matches!(m, Msg::Select(ids) if ids.contains(&0))),
        "the pin press should fall through to selecting its node: {msgs:?}",
    );
}