iced_nodegraph 0.5.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
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
//! High-level interaction tests driving NodeGraph through `iced_test::Simulator`.
//!
//! Unlike the recording-renderer tests in `tests/coordinates.rs` and
//! `tests/clipping.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, Size, Theme, Vector};
use iced::{keyboard, mouse};
use iced_nodegraph::{
    AnchorStatus, DragInfo, Ids, Minimap, NodeGraph, PinRef, anchor, default_anchor_style, edge,
    node, pin,
};
use iced_test::Simulator;

/// The vocabulary every scene here shares: `usize` ids throughout, so an
/// edge can be named by `on_edge_delete` and a message enum serves both the
/// plain and the routed scenes.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
struct SimIds;

impl Ids for SimIds {
    type NodeId = usize;
    type PinId = usize;
    type EdgeId = usize;
    type AnchorId = usize;
    type Payload = ();
}

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

/// Captures every interaction callback the graph can emit.
#[derive(Debug, Clone, PartialEq)]
enum Msg {
    Select(Vec<usize>),
    Move(Vector, Vec<usize>),
    Resize(usize, Size),
    Clone(Vec<usize>),
    Delete(Vec<usize>),
    Connect(Pin, Pin),
    Disconnect(Pin, Pin),
    Refused(Pin, Pin),
    Camera(Point, f32),
    DragStart(DragInfo<SimIds>),
    DragUpdate(Point),
    DragEnd,
    Button,
    Input(String),
    EdgeDelete(Vec<usize>),
    AnchorMove(usize, Point),
    AnchorCreated(usize, Point),
    RouteAttached(usize, usize),
    RouteDetached(usize, usize),
    AnchorDeleted(usize),
    InnerConnect(Pin, Pin),
    InnerCamera(Point, f32),
    InnerSelect(Vec<usize>),
}

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> {
    graph_with_selected(nodes, &[])
}

/// Like [`graph_with`], but the host marks `selected` ids - standing in for a
/// frame where it has already applied a reported selection.
fn graph_with_selected(
    nodes: &[(usize, Point)],
    selected: &[usize],
) -> Element<'static, Msg, Theme, Renderer> {
    graph_of(nodes, selected, false).into()
}

/// Like [`graph_with`], but every node carries a resize grip.
fn resizable_graph(nodes: &[(usize, Point)]) -> Element<'static, Msg, Theme, Renderer> {
    graph_of(nodes, &[], true).into()
}

/// Like [`graph_with_selected`], but node drags snap to a [`GRID`]-wide world
/// grid.
fn snap_graph(
    nodes: &[(usize, Point)],
    selected: &[usize],
) -> Element<'static, Msg, Theme, Renderer> {
    graph_of(nodes, selected, false).snap_grid(GRID).into()
}

fn graph_of(nodes: &[(usize, Point)], selected: &[usize], resizable: bool) -> Graph {
    let mut ng: Graph = NodeGraph::default()
        .width(Length::Fill)
        .height(Length::Fill)
        .on_select(Msg::Select)
        .on_move(Msg::Move)
        .on_resize(Msg::Resize)
        .on_clone(Msg::Clone)
        .on_delete(Msg::Delete)
        .on_drag_start(Msg::DragStart)
        .on_drag_update(Msg::DragUpdate)
        .on_drag_end(|| Msg::DragEnd);
    for &(id, pos) in nodes {
        let body = container(iced::widget::text("n"))
            .width(Length::Fixed(NODE_W))
            .height(Length::Fixed(NODE_H));
        ng = ng.push_node(
            node(id, pos, body)
                .selected(selected.contains(&id))
                .resizable(resizable),
        );
    }
    ng
}

/// 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() {
    // Node 0 already selected by the host; shift-click must extend, not replace.
    let mut ui = Simulator::new(graph_with_selected(
        &[(0, Point::new(100.0, 100.0)), (1, Point::new(400.0, 100.0))],
        &[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 is an empty selection box -> 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_selected(&[(0, Point::new(100.0, 100.0))], &[0]));
    ui.point_at(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 selection_box_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() {
    // Both selected by the host; dragging one reports the whole group.
    let mut ui = Simulator::new(graph_with_selected(
        &[(0, Point::new(100.0, 100.0)), (1, Point::new(400.0, 100.0))],
        &[0, 1],
    ));
    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:?}",
    );
}

// ---------------------------------------------------------------------------
// Grid snap
// ---------------------------------------------------------------------------

/// Grid spacing [`snap_graph`] uses. The scenes below place nodes off the grid
/// on purpose: a node already on it would pass every snap assertion by
/// accident.
const GRID: f32 = 40.0;

/// The delta and the ids of the first `Move` in `msgs`.
fn first_move(msgs: &[Msg]) -> (Vector, Vec<usize>) {
    msgs.iter()
        .find_map(|m| match m {
            Msg::Move(delta, ids) => Some((*delta, sorted(ids.clone()))),
            _ => None,
        })
        .unwrap_or_else(|| panic!("expected a Move, got {msgs:?}"))
}

#[test]
fn snapped_drag_reports_the_delta_that_lands_on_the_grid() {
    let start = Point::new(100.0, 100.0);
    let mut ui = Simulator::new(snap_graph(&[(0, start)], &[]));
    // (100,100) + (50,20) = (150,120): the nearest grid origin is (160,120).
    drag(
        &mut ui,
        center(start),
        center(start) + Vector::new(50.0, 20.0),
    );

    let (delta, ids) = first_move(&messages(ui));
    assert_eq!(ids, vec![0]);
    assert_eq!((delta.x, delta.y), (60.0, 20.0), "got {delta:?}");
}

#[test]
fn snap_override_modifier_reports_the_raw_delta() {
    let start = Point::new(100.0, 100.0);
    let mut ui = Simulator::new(snap_graph(&[(0, start)], &[]));
    ui.simulate([iced::Event::Keyboard(keyboard::Event::ModifiersChanged(
        keyboard::Modifiers::ALT,
    ))]);
    drag(
        &mut ui,
        center(start),
        center(start) + Vector::new(50.0, 20.0),
    );

    let (delta, ids) = first_move(&messages(ui));
    assert_eq!(ids, vec![0]);
    assert_eq!((delta.x, delta.y), (50.0, 20.0), "got {delta:?}");
}

#[test]
fn snapped_group_move_shares_the_grabbed_node_delta() {
    // Only the grabbed node lands on the grid; its partner keeps the same
    // relative offset, which is what one shared delta means.
    let (a, b) = (Point::new(100.0, 100.0), Point::new(413.0, 100.0));
    let mut ui = Simulator::new(snap_graph(&[(0, a), (1, b)], &[0, 1]));
    drag(&mut ui, center(a), center(a) + Vector::new(50.0, 20.0));

    let (delta, ids) = first_move(&messages(ui));
    assert_eq!(ids, vec![0, 1]);
    assert_eq!((delta.x, delta.y), (60.0, 20.0), "got {delta:?}");
}

/// The report is the anchor's snapped position, not the cursor's: the
/// anchor starts off the grid and the raw drop point (142, 117) is off it too.
#[test]
fn anchor_drag_lands_on_the_snap_grid() {
    let at = Point::new(105.0, 105.0);
    let mut ng: Graph = NodeGraph::default()
        .width(Length::Fill)
        .height(Length::Fill)
        .snap_grid(GRID)
        .on_anchor_move(Msg::AnchorMove);
    ng = ng.push_anchor(anchor(ANCHOR_A, at));
    let mut ui = Simulator::new(Element::from(ng));
    drag(&mut ui, at, at + Vector::new(37.0, 12.0));

    assert_eq!(
        messages(ui),
        vec![Msg::AnchorMove(ANCHOR_A, Point::new(160.0, 120.0))]
    );
}

/// The far corner lands on the grid, not the size: a node whose origin is off
/// the grid still gets on-grid right and bottom edges.
#[test]
fn resize_snaps_the_far_corner() {
    let start = Point::new(10.0, 10.0);
    let mut ui = Simulator::new(Element::from(
        graph_of(&[(0, start)], &[], true).snap_grid(GRID),
    ));
    // Raw size (167, 93) puts the far corner at (177, 103); the nearest grid
    // point is (160, 120).
    drag(&mut ui, grip(start), grip(start) + Vector::new(107.0, 63.0));

    let (id, size) = last_resize(&messages(ui)).expect("dragging the grip must emit Resize");
    assert_eq!(id, 0);
    assert_eq!(size, Size::new(150.0, 110.0));
}

// ---------------------------------------------------------------------------
// Frames
// ---------------------------------------------------------------------------

const FRAME_ID: usize = 3;
const INSIDE_ID: usize = 1;
const OUTSIDE_ID: usize = 2;
const FRAME_SIZE: Size = Size::new(400.0, 300.0);

/// A 400x300 frame at the origin with one node inside it and one outside,
/// the frame pushed LAST so it also carries the highest z: a press only reaches
/// it where no node covers the point.
fn frame_graph() -> Element<'static, Msg, Theme, Renderer> {
    let mut ng = graph_of(
        &[
            (INSIDE_ID, Point::new(50.0, 50.0)),
            (OUTSIDE_ID, Point::new(600.0, 600.0)),
        ],
        &[],
        false,
    );
    let body = container(text("frame"))
        .width(Length::Fixed(FRAME_SIZE.width))
        .height(Length::Fixed(FRAME_SIZE.height));
    ng = ng.push_node(node(FRAME_ID, Point::ORIGIN, body).frame());
    ng.into()
}

#[test]
fn dragging_a_frame_carries_the_nodes_inside_it() {
    let mut ui = Simulator::new(frame_graph());
    // (300,250) is inside the frame and clear of the node at (50,50).
    let empty_area = Point::new(300.0, 250.0);
    drag(&mut ui, empty_area, empty_area + Vector::new(30.0, 10.0));

    let (delta, ids) = first_move(&messages(ui));
    assert_eq!(
        ids,
        vec![INSIDE_ID, FRAME_ID],
        "the frame must carry the node inside it and leave the one outside",
    );
    assert_eq!((delta.x, delta.y), (30.0, 10.0), "got {delta:?}");
}

#[test]
fn pressing_a_node_inside_a_frame_drags_only_that_node() {
    let mut ui = Simulator::new(frame_graph());
    let from = center(Point::new(50.0, 50.0));
    drag(&mut ui, from, from + Vector::new(30.0, 10.0));

    let (delta, ids) = first_move(&messages(ui));
    assert_eq!(
        ids,
        vec![INSIDE_ID],
        "the node over the frame must take the press for itself",
    );
    assert_eq!((delta.x, delta.y), (30.0, 10.0), "got {delta:?}");
}

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

#[test]
fn delete_key_requests_delete_of_selection() {
    let mut ui = Simulator::new(graph_with_selected(&[(0, Point::new(100.0, 100.0))], &[0]));
    ui.point_at(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:?}",
    );
}

/// Selection travels on the node, so it cannot be lost to builder order the way
/// a graph-level setter resolved against a not-yet-filled node list could be.
/// Marking the node is enough - no call ordering, no id-to-index step.
#[test]
fn a_node_marked_selected_is_acted_on_without_any_prior_interaction() {
    let mut ui = Simulator::new(graph_with_selected(&[(7, Point::new(100.0, 100.0))], &[7]));
    ui.point_at(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![7])),
        "a host-marked selection must be live on the first frame: {msgs:?}",
    );
}

/// A burst of clicks composes even though the host has not applied any of them
/// yet: the widget holds what it reported until the host's own value moves on.
/// Without that, the second shift-click would start from the stale (empty) host
/// selection and replace the first instead of extending it.
#[test]
fn a_second_click_composes_with_one_the_host_has_not_applied_yet() {
    let (a, b) = (Point::new(100.0, 100.0), Point::new(400.0, 100.0));
    let mut ui = Simulator::new(graph_with(&[(0, a), (1, b)]));
    ui.simulate([iced::Event::Keyboard(keyboard::Event::ModifiersChanged(
        keyboard::Modifiers::SHIFT,
    ))]);
    ui.point_at(center(a));
    ui.simulate([moved(center(a)), press(), release()]);
    ui.point_at(center(b));
    ui.simulate([moved(center(b)), press(), release()]);

    let msgs = messages(ui);
    assert_eq!(
        last_selection(&msgs),
        Some(vec![0, 1]),
        "the second shift-click must extend the first, not replace it: {msgs:?}",
    );
}

/// The host's value is final: when it reports something else, the held value is
/// dropped rather than fighting it.
#[test]
fn a_host_selection_overrides_what_the_widget_reported() {
    let (a, b) = (Point::new(100.0, 100.0), Point::new(400.0, 100.0));
    // The host says node 1 is selected, whatever the widget last reported.
    let mut ui = Simulator::new(graph_with_selected(&[(0, a), (1, b)], &[1]));
    // Shift-click node 0: extends the HOST's selection, so both.
    ui.simulate([iced::Event::Keyboard(keyboard::Event::ModifiersChanged(
        keyboard::Modifiers::SHIFT,
    ))]);
    ui.point_at(center(a));
    ui.simulate([moved(center(a)), press(), release()]);

    let msgs = messages(ui);
    assert_eq!(
        last_selection(&msgs),
        Some(vec![0, 1]),
        "an interaction must build on the host's selection: {msgs:?}",
    );
}

#[test]
fn ctrl_d_requests_clone_of_selection() {
    let mut ui = Simulator::new(graph_with_selected(&[(0, Point::new(100.0, 100.0))], &[0]));
    ui.point_at(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<M: 'static>() -> iced::widget::Container<'static, M, Theme, Renderer> {
    container(text("p"))
        .width(Length::Fixed(NODE_W))
        .height(Length::Fixed(NODE_H))
}

/// A pin is type-erased into its node, so the graph finds it by the
/// `(PinId, Payload)` pair of its `Ids`; a pin built over a different id type
/// is unreachable and debug builds say so the first time the graph looks for
/// pins - here at a press on the node - instead of silently treating the node
/// as one without pins.
#[cfg(debug_assertions)]
#[test]
#[should_panic(expected = "a pin's id and payload types")]
fn a_pin_over_foreign_id_types_asserts_in_debug() {
    let mut ng: NodeGraph<'static, iced_nodegraph::Indexed, Msg, Theme, Renderer> =
        NodeGraph::default()
            .width(Length::Fill)
            .height(Length::Fill);
    ng = ng.push_node(node(
        0usize,
        OUT_POS,
        pin!(Right, "out", pin_body::<_>(), Output),
    ));
    let mut ui = Simulator::new(Element::from(ng));
    click(&mut ui, center(OUT_POS));
}

/// A node body is laid out against unbounded limits so it shrinks to its
/// content, which leaves a `Length::Fill` element with nothing to fill: its
/// size is infinite and the node has no rectangle to draw. Debug builds name
/// the node at layout instead of failing on NaN geometry in the renderer.
#[cfg(debug_assertions)]
#[test]
#[should_panic(expected = "resolved to an infinite size")]
fn a_fill_body_asserts_in_debug() {
    let mut ui = Simulator::new(graph_of(&[], &[], false).push_node(node(
        0usize,
        OUT_POS,
        container(text("wide")).width(Length::Fill),
    )));
    click(&mut ui, center(OUT_POS));
}

/// 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 = ng.push_node(node(
        0usize,
        OUT_POS,
        pin!(Right, 0usize, pin_body::<_>(), Output),
    ));
    ng = ng.push_node(node(
        1usize,
        IN_POS,
        pin!(Left, 0usize, pin_body::<_>(), Input),
    ));
    if seed_edge {
        ng = ng.push_edge(edge(0, 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:?}",
    );
}

// ---------------------------------------------------------------------------
// Refused drops
//
// A drop the validation turns down publishes nothing else: no snap happened,
// so no `on_connect`. `on_connect_refused` is what lets a host tell the user
// why - and it must stay quiet for the outcomes that are not refusals.
// ---------------------------------------------------------------------------

/// The two-pin scene of [`pin_graph`] with the refusal report wired.
/// `connect_ok` drives `can_connect`, so the same scene covers the accepted
/// and the refused drop.
fn refusal_graph(connect_ok: bool) -> Element<'static, Msg, Theme, Renderer> {
    let mut ng: Graph = NodeGraph::default()
        .width(Length::Fill)
        .height(Length::Fill)
        .on_connect(Msg::Connect)
        .on_connect_refused(Msg::Refused)
        .can_connect(move |_, _| connect_ok);
    ng = ng.push_node(node(
        0usize,
        OUT_POS,
        pin!(Right, 0usize, pin_body::<_>(), Output),
    ));
    ng = ng.push_node(node(
        1usize,
        IN_POS,
        pin!(Left, 0usize, pin_body::<_>(), Input),
    ));
    ng.into()
}

/// Every refused pair reported, in publish order.
fn refusals(msgs: &[Msg]) -> Vec<(Pin, Pin)> {
    msgs.iter()
        .filter_map(|m| match m {
            Msg::Refused(from, to) => Some((*from, *to)),
            _ => None,
        })
        .collect()
}

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

    let msgs = messages(ui);
    assert_eq!(
        refusals(&msgs),
        vec![(PinRef::new(0, 0), PinRef::new(1, 0))],
        "a drop onto a pin can_connect refuses must be reported exactly once: {msgs:?}",
    );
    assert!(
        !msgs.iter().any(|m| matches!(m, Msg::Connect(_, _))),
        "a refused drop must not also connect: {msgs:?}",
    );
}

#[test]
fn a_refused_drop_reports_the_drag_order() {
    // Dragged from the INPUT pin this time. `on_connect` would normalize the
    // pair output-first; a refusal names the pin the drag started on first,
    // because a refused pair need not contain an output at all.
    let mut ui = Simulator::new(refusal_graph(false));
    drag(&mut ui, in_anchor(), out_anchor());

    assert_eq!(
        refusals(&messages(ui)),
        vec![(PinRef::new(1, 0), PinRef::new(0, 0))],
    );
}

#[test]
fn a_drop_on_empty_canvas_reports_no_refusal() {
    // Nothing was refused there: the user simply let go of the cable.
    let mut ui = Simulator::new(refusal_graph(false));
    drag(&mut ui, out_anchor(), Point::new(600.0, 500.0));

    let msgs = messages(ui);
    assert_eq!(
        refusals(&msgs),
        vec![],
        "empty canvas is not a refusal: {msgs:?}"
    );
}

#[test]
fn a_drop_back_on_the_source_pin_reports_no_refusal() {
    // The pin the drag came from is not a target it was denied.
    let mut ui = Simulator::new(refusal_graph(false));
    drag(&mut ui, out_anchor(), out_anchor());

    let msgs = messages(ui);
    assert_eq!(
        refusals(&msgs),
        vec![],
        "releasing on the source pin is not a refusal: {msgs:?}",
    );
}

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

    let msgs = messages(ui);
    assert!(
        msgs.contains(&Msg::Connect(PinRef::new(0, 0), PinRef::new(1, 0))),
        "the drop must connect: {msgs:?}",
    );
    assert_eq!(
        refusals(&msgs),
        vec![],
        "a connection that was accepted must not also be reported refused: {msgs:?}",
    );
}

/// Ctrl+click on the edge line cuts it.
#[test]
fn ctrl_click_on_edge_disconnects() {
    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:?}",
    );
}

/// A cut edge is reported by the host's own edge id, so a host keyed by id needs
/// no endpoint search. `on_disconnect` cannot serve this: it also fires while a
/// drag leaves a snapped pin, where no host edge exists.
#[test]
fn cutting_an_edge_reports_its_host_id() {
    #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
    struct WireIds;

    impl Ids for WireIds {
        type NodeId = usize;
        type PinId = usize;
        type EdgeId = &'static str;
        type AnchorId = usize;
        type Payload = ();
    }

    #[derive(Debug, Clone, PartialEq)]
    enum M {
        Disconnect(PinRef<WireIds>, PinRef<WireIds>),
        Cut(Vec<&'static str>),
    }

    let mut ng: NodeGraph<'_, WireIds, M, Theme, Renderer> = NodeGraph::default()
        .width(Length::Fill)
        .height(Length::Fill)
        .on_disconnect(M::Disconnect)
        .on_edge_delete(M::Cut);
    ng = ng.push_node(node(
        0usize,
        OUT_POS,
        pin!(Right, 0usize, pin_body::<_>(), Output),
    ));
    ng = ng.push_node(node(
        1usize,
        IN_POS,
        pin!(Left, 0usize, pin_body::<_>(), Input),
    ));
    ng = ng.push_edge(edge("wire-7", PinRef::new(0, 0), PinRef::new(1, 0)));

    let mut ui = Simulator::new(Element::from(ng));
    let mid = Point::new((out_anchor().x + in_anchor().x) / 2.0, out_anchor().y);
    ui.point_at(mid);
    ui.simulate([
        iced::Event::Keyboard(keyboard::Event::ModifiersChanged(cmd())),
        moved(mid),
    ]);
    ui.simulate([press(), release()]);

    let msgs: Vec<M> = ui.into_messages().collect();
    assert!(
        msgs.contains(&M::Cut(vec!["wire-7"])),
        "a cut must name the edge id the host supplied: {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_camera(Msg::Camera);
    ng = 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 = ng.push_node(node(
        0usize,
        OUT_POS,
        pin!(Right, 0usize, pin_body::<_>(), Output),
    ));
    ng = ng.push_node(node(
        1usize,
        IN_POS,
        pin!(Left, 0usize, pin_body::<_>(), Input),
    ));
    ng = ng.push_node(node(
        2usize,
        Point::new(IN_POS.x, 300.0),
        pin!(Left, 0usize, pin_body::<_>(), Input),
    ));
    ng = ng.push_edge(edge(0, 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 = ng.push_node(node(
        0usize,
        OUT_POS,
        pin!(Right, 0usize, pin_body::<_>(), Output),
    ));
    ng = ng.push_node(node(
        1usize,
        IN_POS,
        pin!(Left, 0usize, pin_body::<_>(), Input),
    ));
    ng = ng.push_node(node(
        2usize,
        Point::new(OUT_POS.x, 300.0),
        pin!(Right, 0usize, pin_body::<_>(), Output),
    ));
    ng = ng.push_edge(edge(0, 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 = ng.push_node(node(
        0usize,
        OUT_POS,
        pin!(Right, 0usize, pin_body::<_>(), Output),
    ));
    ng = 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 = 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_camera(Msg::Camera);
    ng = 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:?}",
    );
}

// ---------------------------------------------------------------------------
// Nested graph: a NodeGraph as a node body. Each graph owns its own pins and
// input: the outer graph never adopts the inner one's pins, and a wheel tick
// or a keymap shortcut resolves to the innermost graph.
//
// The outer scene is `pin_graph`'s: node 0's Output pin at `out_anchor()`.
// Node 1 at IN_POS is a 200x150 body holding the inner graph, whose node 0 at
// the inner origin carries an Input pin, so that pin is drawn at `in_anchor()`
// - exactly where the outer graph would anchor a Left pin of node 1.
// ---------------------------------------------------------------------------

const INNER_SIZE: Size = Size::new(200.0, 150.0);

/// A point on the inner canvas, clear of the inner node.
const ON_INNER_CANVAS: Point = Point::new(400.0, 200.0);
/// A point on the outer canvas, clear of both nodes.
const ON_OUTER_CANVAS: Point = Point::new(700.0, 500.0);

/// The outer graph: every callback the tests observe wired to the plain `Msg`
/// variants, node 1 wrapping `inner`.
fn nested_graph(
    inner: impl Into<Element<'static, Msg, Theme, Renderer>>,
) -> Element<'static, Msg, Theme, Renderer> {
    let mut ng: Graph = NodeGraph::default()
        .width(Length::Fill)
        .height(Length::Fill)
        .on_connect(Msg::Connect)
        .on_camera(Msg::Camera)
        .on_select(Msg::Select);
    ng = ng.push_node(node(
        0usize,
        OUT_POS,
        pin!(Right, 0usize, pin_body::<_>(), Output),
    ));
    ng = ng.push_node(node(
        1usize,
        IN_POS,
        container(inner)
            .width(INNER_SIZE.width)
            .height(INNER_SIZE.height),
    ));
    ng.into()
}

/// The inner graph over the same `Ids`, reporting through the `Inner*`
/// variants.
fn inner_graph() -> Graph {
    let ng: Graph = NodeGraph::default()
        .on_connect(Msg::InnerConnect)
        .on_camera(Msg::InnerCamera)
        .on_select(Msg::InnerSelect);
    ng.push_node(node(
        0usize,
        Point::ORIGIN,
        pin!(Left, 0usize, pin_body::<_>(), Input),
    ))
}

fn wheel_at(ui: &mut Simulator<'_, Msg, Theme, Renderer>, at: Point) {
    ui.point_at(at);
    ui.simulate([
        moved(at),
        iced::Event::Mouse(mouse::Event::WheelScrolled {
            delta: mouse::ScrollDelta::Lines { x: 0.0, y: 3.0 },
        }),
    ]);
}

#[test]
fn nested_graph_pins_are_not_outer_pins() {
    let mut ui = Simulator::new(nested_graph(inner_graph()));
    drag(&mut ui, out_anchor(), in_anchor());

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

/// The inner graph's id vocabulary is its own business: its pins are not the
/// outer graph's to inspect, so they are no foreign-id assertion either.
#[test]
fn nested_graph_over_other_ids_does_not_assert() {
    #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
    struct InnerIds;

    impl Ids for InnerIds {
        type NodeId = usize;
        type PinId = &'static str;
        type EdgeId = usize;
        type AnchorId = usize;
        type Payload = ();
    }

    let inner: NodeGraph<'static, InnerIds, Msg, Theme, Renderer> = NodeGraph::default();
    let inner = inner.push_node(node(
        0usize,
        Point::ORIGIN,
        pin!(Left, "in", pin_body::<_>(), Input),
    ));
    let mut ui = Simulator::new(nested_graph(inner));
    drag(&mut ui, out_anchor(), in_anchor());

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

#[test]
fn wheel_over_nested_graph_zooms_inner_only() {
    let mut ui = Simulator::new(nested_graph(inner_graph()));
    wheel_at(&mut ui, ON_INNER_CANVAS);
    let msgs = messages(ui);
    assert!(
        msgs.iter().any(|m| matches!(m, Msg::InnerCamera(..))),
        "a wheel tick over the nested graph must zoom it: {msgs:?}",
    );
    assert!(
        !msgs.iter().any(|m| matches!(m, Msg::Camera(..))),
        "a wheel tick the nested graph took must not zoom the outer one: {msgs:?}",
    );

    let mut ui = Simulator::new(nested_graph(inner_graph()));
    wheel_at(&mut ui, ON_OUTER_CANVAS);
    let msgs = messages(ui);
    assert!(
        msgs.iter().any(|m| matches!(m, Msg::Camera(..))),
        "a wheel tick on the outer canvas must zoom the outer graph: {msgs:?}",
    );
    assert!(
        !msgs.iter().any(|m| matches!(m, Msg::InnerCamera(..))),
        "a wheel tick off the nested graph must not zoom it: {msgs:?}",
    );
}

#[test]
fn select_all_reaches_innermost_graph_only() {
    let mut ui = Simulator::new(nested_graph(inner_graph()));
    ui.point_at(ON_OUTER_CANVAS);
    ui.simulate([key_pressed(keyboard::Key::Character("a".into()), cmd())]);

    let msgs = messages(ui);
    assert!(
        msgs.iter().any(|m| matches!(m, Msg::InnerSelect(..))),
        "select-all must reach the nested graph: {msgs:?}",
    );
    assert!(
        !msgs.iter().any(|m| matches!(m, Msg::Select(..))),
        "a shortcut the nested graph took must not act on the outer one: {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 = 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 = 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:?}",
    );
}

// ---------------------------------------------------------------------------
// Shared node/anchor id space: debug-build guard. `node_index`/`anchor_index`
// resolve to the first match, so a duplicate renders one element twice and
// behaves undefined. Edges need no such guard: they carry no id and are
// addressed by index.
// ---------------------------------------------------------------------------

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

/// A node and an anchor may carry the same id: the two are separate id spaces,
/// so a host numbering anchors from zero never has to know what its nodes use.
#[test]
fn a_node_and_an_anchor_may_share_a_number() {
    let mut ng: Graph = NodeGraph::default();
    ng = ng.push_node(node(
        7usize,
        Point::new(0.0, 0.0),
        container(text("n"))
            .width(Length::Fixed(NODE_W))
            .height(Length::Fixed(NODE_H)),
    ));
    ng = ng.push_anchor(anchor(7usize, Point::new(120.0, 120.0)));

    let ui = Simulator::new(Element::from(ng));
    let _ = ui.into_messages();
}

// ---------------------------------------------------------------------------
// 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 = 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 = 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 = 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)
        .camera(CAM_POS, CAM_ZOOM)
        .on_connect(Msg::Connect)
        .on_disconnect(Msg::Disconnect);
    ng = ng.push_node(node(
        0usize,
        OUT_POS,
        pin!(Right, 0usize, pin_body::<_>(), Output),
    ));
    ng = ng.push_node(node(
        1usize,
        IN_POS,
        pin!(Left, 0usize, pin_body::<_>(), Input),
    ));
    if seed_edge {
        ng = ng.push_edge(edge(0, 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_selected(&[(0, Point::new(100.0, 100.0))], &[0]));
    let c = center(Point::new(100.0, 100.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![]));
}

// ---------------------------------------------------------------------------
// Live drag hooks
//
// `on_drag_start` / `on_drag_update` / `on_drag_end` report a drag while it
// happens, for hosts that mirror it elsewhere (a collaborative session, an
// inspector). They are observers: unlike `on_move`, nothing is gated on them,
// and they must bracket every drag exactly once.
// ---------------------------------------------------------------------------

fn drag_infos(msgs: &[Msg]) -> Vec<DragInfo<SimIds>> {
    msgs.iter()
        .filter_map(|m| match m {
            Msg::DragStart(info) => Some(info.clone()),
            _ => None,
        })
        .collect()
}

#[test]
fn node_drag_is_bracketed_by_start_and_end() {
    let pos = Point::new(100.0, 100.0);
    let mut ui = Simulator::new(graph_with(&[(0, pos)]));
    drag(&mut ui, center(pos), Point::new(300.0, 260.0));

    let msgs = messages(ui);
    assert_eq!(
        drag_infos(&msgs),
        vec![DragInfo::Node { node_id: 0 }],
        "one node drag must report exactly one start, naming the node's user id: {msgs:?}",
    );
    assert_eq!(
        msgs.iter().filter(|m| **m == Msg::DragEnd).count(),
        1,
        "the drag must end exactly once: {msgs:?}",
    );
}

#[test]
fn drag_update_reports_the_world_cursor() {
    let pos = Point::new(100.0, 100.0);
    let target = Point::new(400.0, 300.0);
    let mut ui = Simulator::new(graph_with(&[(0, pos)]));
    drag(&mut ui, center(pos), target);

    // Default camera: zoom 1, no pan, so world coordinates equal screen pixels.
    let updates: Vec<Point> = messages(ui)
        .into_iter()
        .filter_map(|m| match m {
            Msg::DragUpdate(p) => Some(p),
            _ => None,
        })
        .collect();
    assert_eq!(updates.last(), Some(&target));
}

#[test]
fn dragging_a_multi_selection_reports_the_whole_group() {
    let (a, b) = (Point::new(100.0, 100.0), Point::new(400.0, 100.0));
    // Both selected by the host, so grabbing one starts a group drag.
    let mut ui = Simulator::new(graph_with_selected(&[(0, a), (1, b)], &[0, 1]));
    drag(&mut ui, center(b), Point::new(600.0, 300.0));

    let msgs = messages(ui);
    let group = drag_infos(&msgs)
        .into_iter()
        .find_map(|info| match info {
            DragInfo::Group { node_ids } => Some(sorted(node_ids)),
            _ => None,
        })
        .unwrap_or_else(|| panic!("dragging a selected node must report a group: {msgs:?}"));
    assert_eq!(group, vec![0, 1]);
}

#[test]
fn selection_box_drag_reports_its_anchor() {
    let mut ui = Simulator::new(graph_with(&[(0, Point::new(400.0, 400.0))]));
    let anchor = Point::new(50.0, 60.0);
    drag(&mut ui, anchor, Point::new(500.0, 500.0));

    let msgs = messages(ui);
    assert_eq!(
        drag_infos(&msgs),
        vec![DragInfo::SelectionBox {
            start_x: anchor.x,
            start_y: anchor.y,
        }],
        "a drag on empty canvas must report the selection box anchor: {msgs:?}",
    );
}

/// The number of `DragEnd`s in `msgs`.
fn drag_ends(msgs: &[Msg]) -> usize {
    msgs.iter().filter(|m| **m == Msg::DragEnd).count()
}

#[test]
fn anchor_drag_reports_the_anchor() {
    let at = Point::new(200.0, 150.0);
    let mut ng: Graph = NodeGraph::default()
        .width(Length::Fill)
        .height(Length::Fill)
        .on_anchor_move(Msg::AnchorMove)
        .on_drag_start(Msg::DragStart)
        .on_drag_end(|| Msg::DragEnd);
    ng = ng.push_anchor(anchor(ANCHOR_A, at));
    let mut ui = Simulator::new(Element::from(ng));
    drag(
        &mut ui,
        at + CORE_NUDGE,
        at + CORE_NUDGE + Vector::new(60.0, 40.0),
    );

    let msgs = messages(ui);
    assert_eq!(
        drag_infos(&msgs),
        vec![DragInfo::Anchor {
            anchor_id: ANCHOR_A
        }],
        "an anchor drag must report exactly one start naming the anchor: {msgs:?}",
    );
    assert_eq!(
        drag_ends(&msgs),
        1,
        "the drag must end exactly once: {msgs:?}"
    );
}

#[test]
fn route_drag_reports_the_edge() {
    let ng = routing_graph()
        .on_drag_start(Msg::DragStart)
        .on_drag_end(|| Msg::DragEnd);
    let mut ui = Simulator::new(route_scene(ng, View::IDENTITY, &[], &[]));
    drag(&mut ui, bare_mid(), Point::new(330.0, 400.0));

    let msgs = messages(ui);
    assert_eq!(
        drag_infos(&msgs),
        vec![DragInfo::Route {
            edge_id: ROUTE_EDGE
        }],
        "a grab of the cable's run must report exactly one start naming the edge: {msgs:?}",
    );
    assert_eq!(
        drag_ends(&msgs),
        1,
        "the drag must end exactly once: {msgs:?}"
    );
}

#[test]
fn resize_drag_reports_the_node() {
    let start = Point::new(100.0, 100.0);
    let mut ui = Simulator::new(resizable_graph(&[(0, start)]));
    drag(&mut ui, grip(start), grip(start) + Vector::new(40.0, 10.0));

    let msgs = messages(ui);
    assert_eq!(
        drag_infos(&msgs),
        vec![DragInfo::Resize { node_id: 0 }],
        "a grip drag must report exactly one start naming the node: {msgs:?}",
    );
    assert_eq!(
        drag_ends(&msgs),
        1,
        "the drag must end exactly once: {msgs:?}"
    );
}

#[test]
fn edge_cut_drag_reports_the_cutting_tool() {
    let mut ui = Simulator::new(graph_with(&[(0, Point::new(400.0, 400.0))]));
    let from = Point::new(50.0, 60.0);
    ui.point_at(from);
    ui.simulate([
        iced::Event::Keyboard(keyboard::Event::ModifiersChanged(cmd())),
        moved(from),
    ]);
    drag(&mut ui, from, Point::new(300.0, 300.0));

    let msgs = messages(ui);
    assert_eq!(
        drag_infos(&msgs),
        vec![DragInfo::EdgeCut],
        "a cut across the canvas must report exactly one start: {msgs:?}",
    );
    assert_eq!(
        drag_ends(&msgs),
        1,
        "the drag must end exactly once: {msgs:?}"
    );
}

// ---------------------------------------------------------------------------
// 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 = 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 = ng.push_node(node(
        0usize,
        OUT_POS,
        pin!(Right, 0usize, pin_body::<_>(), Output),
    ));
    ng = 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:?}",
    );
}

/// A host that only reads `on_select` and never marks a node: selection has to
/// keep working on its own, so a click followed by Delete acts on the clicked
/// node. Marking nodes is an override, not a requirement.
#[test]
fn selection_works_without_the_host_marking_anything() {
    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])),
        "an unmarked host must still get a working selection: {msgs:?}",
    );
}

// ---------------------------------------------------------------------------
// Corner-grip resize
//
// The grip is the bottom-right RESIZE_GRIP_SIDE (12 world px at zoom 1) square
// of a resizable node's body, so for a NODE_W x NODE_H body at world `p` it
// spans `p + (48, 18) .. p + (60, 30)`. A resize is only ever REPORTED: the
// host owns the content, so the node keeps its size for the whole drag and
// every report is absolute (`size at press + cursor delta`).
// ---------------------------------------------------------------------------

/// A point inside the bottom-right grip of a node whose top-left is `p`.
fn grip(p: Point) -> Point {
    Point::new(p.x + NODE_W - 6.0, p.y + NODE_H - 6.0)
}

fn last_resize(msgs: &[Msg]) -> Option<(usize, Size)> {
    msgs.iter().rev().find_map(|m| match m {
        Msg::Resize(id, size) => Some((*id, *size)),
        _ => None,
    })
}

fn any_move(msgs: &[Msg]) -> bool {
    msgs.iter().any(|m| matches!(m, Msg::Move(..)))
}

#[test]
fn grip_drag_on_resizable_node_reports_the_new_size() {
    let start = Point::new(100.0, 100.0);
    let mut ui = Simulator::new(resizable_graph(&[(0, start)]));
    drag(&mut ui, grip(start), grip(start) + Vector::new(40.0, 10.0));

    let msgs = messages(ui);
    let (id, size) = last_resize(&msgs).expect("dragging the grip must emit Resize");
    assert_eq!(id, 0);
    assert!(
        (size.width - (NODE_W + 40.0)).abs() < 0.5 && (size.height - (NODE_H + 10.0)).abs() < 0.5,
        "grip drag should report (100, 40), got {size:?}",
    );
    assert!(
        !any_move(&msgs),
        "a resize must not move the node: {msgs:?}",
    );
}

#[test]
fn grip_drag_on_non_resizable_node_moves_it_instead() {
    let start = Point::new(100.0, 100.0);
    let mut ui = Simulator::new(graph_with(&[(0, start)]));
    drag(&mut ui, grip(start), grip(start) + Vector::new(40.0, 10.0));

    let msgs = messages(ui);
    assert_eq!(last_resize(&msgs), None, "unmarked node must not resize");
    let moved = msgs.iter().find_map(|m| match m {
        Msg::Move(delta, ids) => Some((*delta, sorted(ids.clone()))),
        _ => None,
    });
    let (delta, ids) = moved.expect("the corner of an unmarked node still drags it");
    assert_eq!(ids, vec![0]);
    assert!(
        (delta.x - 40.0).abs() < 0.5 && (delta.y - 10.0).abs() < 0.5,
        "node should move by (40, 10), got {delta:?}",
    );
}

/// Dragging the grip up and left past zero clamps at MIN_NODE_SIZE, so the node
/// keeps a body to grab and a grip to grow it back by.
#[test]
fn grip_drag_clamps_to_the_minimum_size() {
    let start = Point::new(100.0, 100.0);
    let mut ui = Simulator::new(resizable_graph(&[(0, start)]));
    // Delta (-44, -14) would ask for 16x16; both axes hit the floor.
    drag(&mut ui, grip(start), start + Vector::new(10.0, 10.0));

    let msgs = messages(ui);
    let (_, size) = last_resize(&msgs).expect("dragging the grip must emit Resize");
    assert_eq!(size, Size::new(32.0, 24.0));
}

#[test]
fn body_drag_on_a_resizable_node_still_moves_it() {
    let start = Point::new(100.0, 100.0);
    let mut ui = Simulator::new(resizable_graph(&[(0, start)]));
    drag(
        &mut ui,
        center(start),
        center(start) + Vector::new(50.0, 20.0),
    );

    let msgs = messages(ui);
    assert_eq!(
        last_resize(&msgs),
        None,
        "a body drag is a move, not a resize: {msgs:?}",
    );
    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 resizable node's body 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:?}",
    );
}

// ---------------------------------------------------------------------------
// Anchor dragging. An anchor is a grabbable object of the graph itself, and
// like every other gesture the widget only REPORTS the result - it never moves
// the anchor.
// ---------------------------------------------------------------------------

/// The camera a scene is viewed through together with the widget origin it is
/// drawn at: the two terms of the mapping a press has to aim through,
/// `screen = origin + (world + position) * zoom`.
#[derive(Debug, Clone, Copy)]
struct View {
    position: Point,
    zoom: f32,
    origin: Vector,
}

impl View {
    /// The default camera at the window origin, where a world point IS its
    /// screen pixel.
    const IDENTITY: Self = Self {
        position: Point::ORIGIN,
        zoom: 1.0,
        origin: Vector::ZERO,
    };

    /// The same camera read through a graph drawn `TOOLBAR` px down the window.
    const fn below_toolbar(self) -> Self {
        Self {
            position: self.position,
            zoom: self.zoom,
            origin: Vector::new(0.0, TOOLBAR),
        }
    }

    /// The screen pixel this view draws `world` at.
    fn screen(self, world: Point) -> Point {
        Point::new(
            self.origin.x + (world.x + self.position.x) * self.zoom,
            self.origin.y + (world.y + self.position.y) * self.zoom,
        )
    }
}

/// Half scale: a screen-pixel threshold divided by zoom reaches twice as far
/// into the world as it does at zoom 1, while a ring radius and a cable's arc
/// length stay where they are.
const HALF_SCALE: View = View {
    position: Point::ORIGIN,
    zoom: 0.5,
    origin: Vector::ZERO,
};

/// Double scale, panned, so neither term of the mapping is the identity.
const DOUBLE_SCALE: View = View {
    position: Point::new(-100.0, -100.0),
    zoom: 2.0,
    origin: Vector::ZERO,
};

/// The cameras every anchor and route gesture is replayed under. Zoom 1 is the
/// one point where a threshold divided by zoom and a world-fixed radius agree,
/// so a gesture pinned only there is pinned nowhere.
const OFF_UNIT_VIEWS: [View; 2] = [HALF_SCALE, DOUBLE_SCALE];

/// A press this far off an anchor's core, in world units: inside the core's
/// grab box at every zoom these tests use, and off centre enough that a report
/// carrying the anchor's own position can be told from one carrying the
/// cursor's.
const CORE_NUDGE: Vector = Vector::new(2.0, 1.0);

/// Radius of an anchor's innermost orbit, read from the style the widget
/// resolves it from. Every anchor in these scenes carries at most one edge, so
/// orbit 0 is the only ring in play.
fn orbit_0() -> f32 {
    default_anchor_style(&Theme::Dark, AnchorStatus::Idle).orbit_offset
}

/// The anchor a routed cable in these scenes wraps.
const ANCHOR_A: usize = 7;
/// A second anchor, off the cable: nothing reaches it until a route drag
/// carries the cursor there.
const ANCHOR_B: usize = 8;

/// A graph whose only content is one anchor, its move gesture wired.
fn graph_with_anchor(view: View, at: Point) -> Element<'static, Msg, Theme, Renderer> {
    let mut ng: Graph = NodeGraph::default()
        .width(Length::Fill)
        .height(Length::Fill)
        .camera(view.position, view.zoom)
        .on_anchor_move(Msg::AnchorMove);
    ng = ng.push_anchor(anchor(ANCHOR_A, at));
    ng.into()
}

/// The drag reports the world position the anchor ended up at, outright rather
/// than as a delta, so a host that lost a frame still lands where the cursor is.
///
/// The press sits OFF the core's centre: the report is the anchor's own
/// position plus the cursor's travel, and a press on the centre could not tell
/// that apart from a report of the drop point.
#[test]
fn dragging_an_anchor_reports_its_new_position() {
    let at = Point::new(200.0, 150.0);
    let travel = Vector::new(60.0, 40.0);
    let mut ui = Simulator::new(graph_with_anchor(View::IDENTITY, at));
    drag(&mut ui, at + CORE_NUDGE, at + CORE_NUDGE + travel);

    assert_eq!(messages(ui), vec![Msg::AnchorMove(ANCHOR_A, at + travel)]);
}

/// A press and release without motion is a click, not a move - the same rule
/// node dragging follows, so a plain click never dirties host state.
#[test]
fn clicking_an_anchor_reports_no_move() {
    let at = Point::new(200.0, 150.0);
    let mut ui = Simulator::new(graph_with_anchor(View::IDENTITY, at));
    click(&mut ui, at);

    let msgs = messages(ui);
    assert!(
        !msgs.iter().any(|m| matches!(m, Msg::AnchorMove(..))),
        "a motionless click reported a move: {msgs:?}",
    );
}

/// The grab is gated on `on_anchor_move` being wired: without it the press
/// falls through to the selection box rather than starting a drag the host
/// could never apply.
#[test]
fn an_anchor_is_not_grabbable_without_the_callback() {
    let at = Point::new(200.0, 150.0);
    let mut ng: Graph = NodeGraph::default()
        .width(Length::Fill)
        .height(Length::Fill)
        .on_select(Msg::Select);
    ng = ng.push_anchor(anchor(ANCHOR_A, at));
    let mut ui = Simulator::new(Element::from(ng));
    drag(&mut ui, at, Point::new(260.0, 190.0));

    let msgs = messages(ui);
    assert!(
        !msgs.iter().any(|m| matches!(m, Msg::AnchorMove(..))),
        "an anchor moved without a handler to report it to: {msgs:?}",
    );
}

// ---------------------------------------------------------------------------
// Route gestures. An edge connects two pins and carries the anchors it wraps;
// every gesture below edits that route, and every one of them only REPORTS.
//
// The scene: node 0's Right/Output pin at `out_anchor()` = (160, 115), node 1's
// Left/Input pin at `route_in_anchor()` = (500, 115). Both bezier control
// points of the unrouted cable lie on y = 115, so the bare cable IS the
// straight 340 px segment between the pins; `bare_mid()` sits 170 along it,
// clear of the 24 px end zone at either end.
//
// Routed through the anchor at `A_AT`, the cable leaves that line and runs
// tangent to orbit 0 instead, wrapping the side of the ring AWAY from the
// pin-to-pin run. `wrap_point()` - the lowest point of that circle - therefore
// lies on the arc the cable draws, `orbit_0()` px from the core it belongs to.
// ---------------------------------------------------------------------------

/// The graph shape the route scenes need: edge ids are `usize`, so an edge is
/// named by the index it was pushed at.
type RouteGraph = Graph;

/// The id of the one edge a route scene carries.
const ROUTE_EDGE: usize = 0;

/// The input node of the route scene, far enough from the output that the cable
/// has a mid-run between the two end zones.
const ROUTE_IN_POS: Point = Point::new(500.0, 100.0);

/// Anchor A, 85 px below the bare cable: far enough that the wrap and the
/// straight run are never the same press.
const A_AT: Point = Point::new(330.0, 200.0);
/// Anchor B, well clear of the cable.
const B_AT: Point = Point::new(330.0, 400.0);

fn route_in_anchor() -> Point {
    Point::new(ROUTE_IN_POS.x, ROUTE_IN_POS.y + NODE_H / 2.0)
}

/// The midpoint of the bare cable's straight run.
fn bare_mid() -> Point {
    Point::new((out_anchor().x + route_in_anchor().x) / 2.0, out_anchor().y)
}

/// Where a cable routed through the anchor at `at` wraps it.
fn wrap_point(at: Point) -> Point {
    Point::new(at.x, at.y + orbit_0())
}

/// Fills `ng` with the route scene, read through `view`: node 0's output pin,
/// node 1's input pin, one anchor per entry of `anchors`, and edge
/// [`ROUTE_EDGE`] between the two pins routed through `route`.
fn route_scene(
    ng: RouteGraph,
    view: View,
    anchors: &[(usize, Point)],
    route: &[usize],
) -> Element<'static, Msg, Theme, Renderer> {
    let mut ng = ng.camera(view.position, view.zoom);
    ng = ng.push_node(node(
        0usize,
        OUT_POS,
        pin!(Right, 0usize, pin_body::<_>(), Output),
    ));
    ng = ng.push_node(node(
        1usize,
        ROUTE_IN_POS,
        pin!(Left, 0usize, pin_body::<_>(), Input),
    ));
    for &(id, at) in anchors {
        ng = ng.push_anchor(anchor(id, at));
    }
    ng = ng.push_edge(
        edge(ROUTE_EDGE, PinRef::new(0, 0), PinRef::new(1, 0)).route(route.iter().copied()),
    );
    ng.into()
}

/// All three route-drag callbacks wired - the gating the mid-run and wrap zones
/// ask for before they are live at all.
fn routing_graph() -> RouteGraph {
    NodeGraph::default()
        .width(Length::Fill)
        .height(Length::Fill)
        .on_anchor_create(Msg::AnchorCreated)
        .on_route_attach(Msg::RouteAttached)
        .on_route_detach(Msg::RouteDetached)
}

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

/// A grab of the cable's run puts a new anchor where the drag ends: the widget
/// mints nothing itself, it names the edge and the world point and lets the
/// host decide what the anchor is called.
#[test]
fn grabbing_a_cable_mid_run_creates_an_anchor_on_release() {
    let mut ui = Simulator::new(route_scene(routing_graph(), View::IDENTITY, &[], &[]));
    let dropped_at = Point::new(330.0, 400.0);
    drag(&mut ui, bare_mid(), dropped_at);

    assert_eq!(
        messages(ui),
        vec![Msg::AnchorCreated(ROUTE_EDGE, dropped_at)],
    );
}

/// Attaching is plug behaviour: the moment the drag reaches an anchor's snap
/// distance the attachment is published, so the host's own route is what the
/// next frame draws. Release commits nothing further.
#[test]
fn a_route_drag_snaps_onto_an_anchor_immediately() {
    let snapped = || {
        let mut ui = Simulator::new(route_scene(
            routing_graph(),
            View::IDENTITY,
            &[(ANCHOR_B, B_AT)],
            &[],
        ));
        ui.point_at(bare_mid());
        ui.simulate([moved(bare_mid()), press()]);
        ui.point_at(B_AT);
        ui.simulate([moved(B_AT)]);
        ui
    };

    assert_eq!(
        messages(snapped()),
        vec![Msg::RouteAttached(ROUTE_EDGE, ANCHOR_B)],
        "the attachment must be published on snap, not held back for the release",
    );

    let mut released = snapped();
    released.simulate([release()]);
    assert_eq!(
        messages(released),
        vec![Msg::RouteAttached(ROUTE_EDGE, ANCHOR_B)],
        "releasing on the anchor must add nothing: it is already attached",
    );
}

/// Leaving the ring again reports the detachment while the drag is still in
/// flight, the mirror of the snap - a drag that only committed on release could
/// not be talked out of an anchor it brushed past.
#[test]
fn leaving_the_anchor_detaches_again() {
    let mut ui = Simulator::new(route_scene(
        routing_graph(),
        View::IDENTITY,
        &[(ANCHOR_B, B_AT)],
        &[],
    ));
    ui.point_at(bare_mid());
    ui.simulate([moved(bare_mid()), press()]);
    ui.point_at(B_AT);
    ui.simulate([moved(B_AT)]);
    let away = Point::new(B_AT.x, B_AT.y - 100.0);
    ui.point_at(away);
    ui.simulate([moved(away)]);

    assert_eq!(
        messages(ui),
        vec![
            Msg::RouteAttached(ROUTE_EDGE, ANCHOR_B),
            Msg::RouteDetached(ROUTE_EDGE, ANCHOR_B),
        ],
    );
}

/// A grab at the wrap holds the anchor rather than dropping it: the cable comes
/// off only once the cursor is past the unsnap distance, so a hand that shakes
/// while pressing leaves the route exactly as it was.
#[test]
fn a_wrap_grab_detaches_only_past_the_unsnap_threshold() {
    let wrap = wrap_point(A_AT);
    let scene = || {
        route_scene(
            routing_graph(),
            View::IDENTITY,
            &[(ANCHOR_A, A_AT)],
            &[ANCHOR_A],
        )
    };

    let mut wiggled = Simulator::new(scene());
    // 13 units outside orbit 0, and so 2 short of the UNSNAP_THRESHOLD of 15
    // the ring has to be left by: a shrunken threshold lets go right here.
    let nudge = Point::new(A_AT.x, A_AT.y + orbit_0() + 13.0);
    wiggled.point_at(wrap);
    wiggled.simulate([moved(wrap), press()]);
    wiggled.point_at(nudge);
    wiggled.simulate([moved(nudge), release()]);
    assert_eq!(
        messages(wiggled),
        Vec::<Msg>::new(),
        "a wrap grab that never left the ring must keep the anchor",
    );

    let mut pulled = Simulator::new(scene());
    let away = Point::new(A_AT.x, A_AT.y + 300.0);
    pulled.point_at(wrap);
    pulled.simulate([moved(wrap), press()]);
    pulled.point_at(away);
    pulled.simulate([moved(away), release()]);
    assert_eq!(
        messages(pulled),
        vec![
            Msg::RouteDetached(ROUTE_EDGE, ANCHOR_A),
            Msg::AnchorCreated(ROUTE_EDGE, away),
        ],
        "pulling the cable off must report the detachment, then the anchor the \
         release leaves behind",
    );
}

/// The pan button both pans and clicks, and travel is what tells them apart: a
/// click on a core deletes the anchor and pans nothing, a drag from the same
/// press pans from that very point and deletes nothing.
#[test]
fn a_click_of_the_pan_button_on_a_core_deletes() {
    let scene = || {
        route_scene(
            routing_graph()
                .on_anchor_delete(Msg::AnchorDeleted)
                .on_camera(Msg::Camera),
            View::IDENTITY,
            &[(ANCHOR_A, A_AT)],
            &[ANCHOR_A],
        )
    };

    let mut clicked = Simulator::new(scene());
    clicked.point_at(A_AT);
    clicked.simulate([moved(A_AT), pan_press(), pan_release()]);
    assert_eq!(
        messages(clicked),
        vec![Msg::AnchorDeleted(ANCHOR_A)],
        "a travel-free pan-button click on a core deletes it and pans nothing",
    );

    let mut dragged = Simulator::new(scene());
    let to = Point::new(A_AT.x + 60.0, A_AT.y);
    dragged.point_at(A_AT);
    dragged.simulate([moved(A_AT), pan_press()]);
    dragged.point_at(to);
    dragged.simulate([moved(to), pan_release()]);
    assert_eq!(
        messages(dragged),
        vec![Msg::Camera(Point::new(60.0, 0.0), 1.0)],
        "a pan-button press that travelled is a pan seeded at the press point, \
         and never a delete",
    );
}

/// The same click on a wrap takes that one cable off the anchor, leaving the
/// anchor and every other cable through it alone.
#[test]
fn a_click_of_the_pan_button_on_a_wrap_detaches() {
    let mut ui = Simulator::new(route_scene(
        routing_graph().on_camera(Msg::Camera),
        View::IDENTITY,
        &[(ANCHOR_A, A_AT)],
        &[ANCHOR_A],
    ));
    let wrap = wrap_point(A_AT);
    ui.point_at(wrap);
    ui.simulate([moved(wrap), pan_press(), pan_release()]);

    assert_eq!(
        messages(ui),
        vec![Msg::RouteDetached(ROUTE_EDGE, ANCHOR_A)],
        "a pan-button click on a wrap detaches that cable and pans nothing",
    );
}

/// An unwired zone is INERT, not merely silent: the press has to reach whatever
/// is behind the cable, which on empty canvas is the selection box.
#[test]
fn route_gestures_without_callbacks_fall_through() {
    let ng: RouteGraph = NodeGraph::default()
        .width(Length::Fill)
        .height(Length::Fill)
        .on_drag_start(Msg::DragStart);
    let mut ui = Simulator::new(route_scene(ng, View::IDENTITY, &[], &[]));
    let at = bare_mid();
    ui.point_at(at);
    ui.simulate([moved(at), press()]);

    assert_eq!(
        messages(ui),
        vec![Msg::DragStart(DragInfo::SelectionBox {
            start_x: at.x,
            start_y: at.y,
        })],
        "the mid-run zone must not swallow a press it has no callback for",
    );
}

/// One edge is one cable: two cables sharing an anchor are two connections, and
/// a cut through one reports exactly that one - by its host id, with the other
/// left wired.
#[test]
fn cutting_a_routed_cable_kills_one_edge() {
    let mut ng: RouteGraph = NodeGraph::default()
        .width(Length::Fill)
        .height(Length::Fill)
        .on_disconnect(Msg::Disconnect)
        .on_edge_delete(Msg::EdgeDelete);
    let shared = Point::new(330.0, 300.0);
    for (id, at) in [(0usize, OUT_POS), (2usize, Point::new(OUT_POS.x, 500.0))] {
        ng = ng.push_node(node(id, at, pin!(Right, 0usize, pin_body::<_>(), Output)));
    }
    for (id, at) in [
        (1usize, ROUTE_IN_POS),
        (3usize, Point::new(ROUTE_IN_POS.x, 500.0)),
    ] {
        ng = ng.push_node(node(id, at, pin!(Left, 0usize, pin_body::<_>(), Input)));
    }
    ng = ng.push_anchor(anchor(ANCHOR_A, shared));
    ng = ng.push_edge(edge(0usize, PinRef::new(0, 0), PinRef::new(1, 0)).route([ANCHOR_A]));
    ng = ng.push_edge(edge(1usize, PinRef::new(2, 0), PinRef::new(3, 0)).route([ANCHOR_A]));

    let mut ui = Simulator::new(Element::from(ng));
    // 100 px along the upper cable, where it has already left the straight run
    // toward the shared anchor but is still 147 px from the lower one.
    let on_upper_cable = Point::new(228.0, 160.0);
    ui.point_at(on_upper_cable);
    ui.simulate([
        iced::Event::Keyboard(keyboard::Event::ModifiersChanged(cmd())),
        moved(on_upper_cable),
    ]);
    ui.simulate([press(), release()]);

    assert_eq!(
        messages(ui),
        vec![
            Msg::Disconnect(PinRef::new(0, 0), PinRef::new(1, 0)),
            Msg::EdgeDelete(vec![0]),
        ],
        "a cut through one routed cable must take that edge and only that edge",
    );
}

/// A busy anchor takes a second cable on its next free orbit, and takes it
/// DURING the drag like every other snap.
///
/// The occupied case is its own test because the ring the drag is measured
/// against is not the anchor's innermost one: orbit 0 already carries a cable,
/// so the second cable is offered orbit 1 and snaps at that larger radius.
#[test]
fn a_route_drag_snaps_onto_an_occupied_anchor() {
    let scene = || {
        let mut ng = routing_graph();
        ng = ng.push_node(node(
            0usize,
            OUT_POS,
            pin!(Right, 0usize, pin_body::<_>(), Output),
        ));
        ng = ng.push_node(node(
            1usize,
            ROUTE_IN_POS,
            pin!(Left, 0usize, pin_body::<_>(), Input),
        ));
        ng = ng.push_node(node(
            2usize,
            Point::new(OUT_POS.x, 500.0),
            pin!(Right, 0usize, pin_body::<_>(), Output),
        ));
        ng = ng.push_node(node(
            3usize,
            Point::new(ROUTE_IN_POS.x, 500.0),
            pin!(Left, 0usize, pin_body::<_>(), Input),
        ));
        ng = ng.push_anchor(anchor(ANCHOR_A, A_AT));
        // Edge 0 already wraps A, so A is singly occupied.
        ng = ng.push_edge(edge(0usize, PinRef::new(0, 0), PinRef::new(1, 0)).route([ANCHOR_A]));
        // Edge 1 runs well below and is unrouted.
        ng = ng.push_edge(edge(1usize, PinRef::new(2, 0), PinRef::new(3, 0)));
        Element::from(ng)
    };
    // The lower cable's mid-run, and the core of the busy anchor.
    let lower_mid = Point::new(bare_mid().x, 515.0);

    let mut ui = Simulator::new(scene());
    ui.point_at(lower_mid);
    ui.simulate([moved(lower_mid), press()]);
    ui.point_at(A_AT);
    ui.simulate([moved(A_AT)]);

    assert_eq!(
        messages(ui),
        vec![Msg::RouteAttached(1, ANCHOR_A)],
        "a second cable must attach on snap, not wait for the release",
    );
}

/// A cable pulled off one anchor can be put straight onto another.
///
/// The detached anchor stays snap-eligible so the drag can also put it back,
/// and that must not stop a DIFFERENT anchor from taking it.
#[test]
fn a_detached_cable_can_be_attached_to_another_anchor() {
    let mut ui = Simulator::new(route_scene(
        routing_graph(),
        View::IDENTITY,
        &[(ANCHOR_A, A_AT), (ANCHOR_B, B_AT)],
        &[ANCHOR_A],
    ));
    let wrap = wrap_point(A_AT);
    ui.point_at(wrap);
    ui.simulate([moved(wrap), press()]);
    // Off A by more than the unsnap distance, clear of both anchors.
    let between = Point::new(A_AT.x, (A_AT.y + B_AT.y) / 2.0);
    ui.point_at(between);
    ui.simulate([moved(between)]);
    // Onto B.
    ui.point_at(B_AT);
    ui.simulate([moved(B_AT)]);

    assert_eq!(
        messages(ui),
        vec![
            Msg::RouteDetached(ROUTE_EDGE, ANCHOR_A),
            Msg::RouteAttached(ROUTE_EDGE, ANCHOR_B),
        ],
        "a cable pulled off one anchor must be able to land on another",
    );
}

// ---------------------------------------------------------------------------
// The same gestures at other zooms. Every threshold a press is resolved
// against is a screen-pixel distance divided by zoom, so it lands in world
// units and GROWS without bound as the camera pulls back, while the ring radius
// and the arc length it competes with stay where they are. Zoom 1 is the one
// point where the two agree, so a gesture pinned only there is pinned nowhere.
//
// The gesture itself is unchanged: the press aims at the SCREEN pixel the view
// draws the target at, and the messages are the ones zoom 1 publishes.
// ---------------------------------------------------------------------------

/// A world point clear of every node, cable and anchor of the route scene, and
/// on screen under each view these tests use.
const CLEAR_OF_EVERYTHING: Point = Point::new(330.0, 300.0);

/// Pulled back until the raw end-zone budget (`EDGE_END_GRAB_LENGTH`, 24
/// screen px) covers 192 world units at either end of the 340 unit cable, so
/// the two ends would meet in the middle and leave no run to grab.
const EIGHTH_SCALE: View = View {
    position: Point::ORIGIN,
    zoom: 0.125,
    origin: Vector::ZERO,
};

/// The wrap is grabbed by its RING, which is world-fixed: a press on it has to
/// reach the cable at every zoom rather than being swallowed by whatever zone
/// grew over it.
///
/// The anchor's own move gesture is wired, so the core is a live competitor for
/// the press - and a core that took it would drag the anchor away instead of
/// taking the cable off it.
#[test]
fn a_wrap_grab_detaches_at_any_zoom() {
    for view in OFF_UNIT_VIEWS {
        let zoom = view.zoom;
        let mut ui = Simulator::new(route_scene(
            routing_graph().on_anchor_move(Msg::AnchorMove),
            view,
            &[(ANCHOR_A, A_AT)],
            &[ANCHOR_A],
        ));
        drag(
            &mut ui,
            view.screen(wrap_point(A_AT)),
            view.screen(CLEAR_OF_EVERYTHING),
        );

        assert_eq!(
            messages(ui),
            vec![
                Msg::RouteDetached(ROUTE_EDGE, ANCHOR_A),
                Msg::AnchorCreated(ROUTE_EDGE, CLEAR_OF_EVERYTHING),
            ],
            "the wrap must take the press and report the detach at zoom {zoom}",
        );
    }
}

/// The pan-button click on a wrap, with the anchor's own delete wired: a press
/// the core swallowed does not merely go quiet, it deletes the anchor the click
/// was meant to take one cable off.
#[test]
fn a_pan_button_click_on_a_wrap_detaches_at_any_zoom() {
    for view in OFF_UNIT_VIEWS {
        let zoom = view.zoom;
        let mut ui = Simulator::new(route_scene(
            routing_graph()
                .on_anchor_delete(Msg::AnchorDeleted)
                .on_camera(Msg::Camera),
            view,
            &[(ANCHOR_A, A_AT)],
            &[ANCHOR_A],
        ));
        let wrap = view.screen(wrap_point(A_AT));
        ui.point_at(wrap);
        ui.simulate([moved(wrap), pan_press(), pan_release()]);

        assert_eq!(
            messages(ui),
            vec![Msg::RouteDetached(ROUTE_EDGE, ANCHOR_A)],
            "the click must detach that cable and leave the anchor at zoom {zoom}",
        );
    }
}

/// The core stays grabbable at every zoom, and the press is off centre, so the
/// report has to carry the anchor's own position plus the cursor's travel.
#[test]
fn an_anchor_core_is_grabbable_at_any_zoom() {
    let at = Point::new(200.0, 150.0);
    let travel = Vector::new(60.0, 40.0);
    for view in OFF_UNIT_VIEWS {
        let zoom = view.zoom;
        let mut ui = Simulator::new(graph_with_anchor(view, at));
        drag(
            &mut ui,
            view.screen(at + CORE_NUDGE),
            view.screen(at + CORE_NUDGE + travel),
        );

        assert_eq!(
            messages(ui),
            vec![Msg::AnchorMove(ANCHOR_A, at + travel)],
            "the core must grab and report the same move at zoom {zoom}",
        );
    }
}

/// The run between the two end zones is grabbable at every zoom, [`EIGHTH_SCALE`]
/// included: an end zone that outgrew the cable would take the press for an
/// unplug and create nothing.
#[test]
fn a_cable_mid_run_creates_an_anchor_at_any_zoom() {
    for view in [HALF_SCALE, DOUBLE_SCALE, EIGHTH_SCALE] {
        let zoom = view.zoom;
        let mut ui = Simulator::new(route_scene(routing_graph(), view, &[], &[]));
        drag(
            &mut ui,
            view.screen(bare_mid()),
            view.screen(CLEAR_OF_EVERYTHING),
        );

        assert_eq!(
            messages(ui),
            vec![Msg::AnchorCreated(ROUTE_EDGE, CLEAR_OF_EVERYTHING)],
            "the cable's run must take the press and name the anchor at zoom {zoom}",
        );
    }
}

// ---------------------------------------------------------------------------
// Anchors at a non-zero widget origin. An anchor is drawn by the graph itself
// rather than laid out as a child, so nothing carries the widget's origin for
// it: the hit test has to fold it in the same way `draw` does, or the dot the
// user aims at and the target that answers are `TOOLBAR` px apart. Nodes are
// immune - their layout bounds already carry the origin - which is why every
// other test here, sitting at the window origin, cannot see the difference.
//
// The scene puts a fixed-height element above the graph, so anchor 9 at world
// (230, 300) is DRAWN at (230, 400).
// ---------------------------------------------------------------------------

/// Height of the element above the graph.
const TOOLBAR: f32 = 100.0;
/// The edge and anchor of the off-origin scene.
const OFFSET_EDGE: usize = 7;
const OFFSET_ANCHOR: usize = 9;
/// World position of the off-origin scene's anchor.
const OFFSET_ANCHOR_AT: Point = Point::new(230.0, 300.0);

/// The view the off-origin scene is read through at zoom 1: the toolbar is part
/// of the mapping, so a world point lands `TOOLBAR` px down the window.
const OFFSET_VIEW: View = View::IDENTITY.below_toolbar();

/// Where a world point of the off-origin scene lands on screen at zoom 1.
fn drawn_at(world: Point) -> Point {
    OFFSET_VIEW.screen(world)
}

/// Output 0:0 -> input 1:0 as edge [`OFFSET_EDGE`] routed through `route`, plus
/// anchor [`OFFSET_ANCHOR`], read through `view`, with the whole graph pushed
/// down by a toolbar.
fn offset_scene(view: View, route: &[usize]) -> Element<'static, Msg, Theme, Renderer> {
    use iced::widget::column;

    let mut ng: RouteGraph = NodeGraph::default()
        .width(Length::Fill)
        .height(Length::Fill)
        .camera(view.position, view.zoom)
        .on_select(Msg::Select)
        .on_camera(Msg::Camera)
        .on_anchor_move(Msg::AnchorMove)
        .on_anchor_create(Msg::AnchorCreated)
        .on_anchor_delete(Msg::AnchorDeleted)
        .on_route_attach(Msg::RouteAttached)
        .on_route_detach(Msg::RouteDetached);
    ng = ng.push_node(node(
        0usize,
        OUT_POS,
        pin!(Right, 0usize, pin_body::<_>(), Output),
    ));
    ng = ng.push_node(node(
        1usize,
        IN_POS,
        pin!(Left, 0usize, pin_body::<_>(), Input),
    ));
    ng = ng.push_anchor(anchor(OFFSET_ANCHOR, OFFSET_ANCHOR_AT));
    ng =
        ng.push_edge(edge(OFFSET_EDGE, PinRef::new(0, 0), PinRef::new(1, 0)).route(route.to_vec()));
    column![
        container(text(""))
            .width(Length::Fill)
            .height(Length::Fixed(TOOLBAR)),
        ng
    ]
    .into()
}

/// The core is drawn at world plus widget origin, so that is where it has to be
/// grabbable - and what it reports is still a world position, because that is
/// what the host stores: the anchor's own coordinates plus the cursor's travel,
/// which a press on the core's centre could not tell from the drop point.
#[test]
fn an_anchor_core_is_grabbable_where_it_is_drawn() {
    let travel = Vector::new(50.0, 40.0);
    let press_at = OFFSET_ANCHOR_AT + CORE_NUDGE;
    let mut ui = Simulator::new(offset_scene(OFFSET_VIEW, &[]));
    drag(&mut ui, drawn_at(press_at), drawn_at(press_at + travel));

    let msgs = messages(ui);
    assert!(
        msgs.contains(&Msg::AnchorMove(OFFSET_ANCHOR, OFFSET_ANCHOR_AT + travel)),
        "the drawn core must grab and report a world position: {msgs:?}",
    );
}

/// And nowhere else: an anchor that answers a press `TOOLBAR` px above its dot
/// is a target the user cannot see.
#[test]
fn nothing_is_grabbable_at_the_unshifted_world_position() {
    let mut ui = Simulator::new(offset_scene(OFFSET_VIEW, &[]));
    drag(&mut ui, OFFSET_ANCHOR_AT, Point::new(280.0, 340.0));

    let msgs = messages(ui);
    assert!(
        !msgs.iter().any(|m| matches!(m, Msg::AnchorMove(..))),
        "the anchor must not answer where it is not drawn: {msgs:?}",
    );
}

/// The pan-button click runs through the same core hit test the drag does.
#[test]
fn a_pan_button_click_on_the_drawn_core_deletes_it() {
    let mut ui = Simulator::new(offset_scene(OFFSET_VIEW, &[]));
    let drawn = drawn_at(OFFSET_ANCHOR_AT);
    ui.point_at(drawn);
    ui.simulate([moved(drawn), pan_press(), pan_release()]);

    let msgs = messages(ui);
    assert!(
        msgs.contains(&Msg::AnchorDeleted(OFFSET_ANCHOR)),
        "the drawn core must take the delete click: {msgs:?}",
    );
}

/// `on_anchor_create` reports a WORLD position - the host stores it as an
/// anchor position - so the widget origin has to be folded back out of it.
#[test]
fn a_created_anchor_is_reported_in_world_space() {
    let mut ui = Simulator::new(offset_scene(OFFSET_VIEW, &[]));
    let mid_run = drawn_at(Point::new(230.0, out_anchor().y));
    drag(&mut ui, mid_run, drawn_at(Point::new(500.0, 400.0)));

    let msgs = messages(ui);
    assert!(
        msgs.contains(&Msg::AnchorCreated(OFFSET_EDGE, Point::new(500.0, 400.0))),
        "a created anchor is reported where the host would store it: {msgs:?}",
    );
}

/// The wrap arc is drawn around the anchor's drawn position, so that is where
/// the wrap is grabbed - and pulling it that far off has to report the detach.
#[test]
fn a_wrap_is_grabbable_where_it_is_drawn() {
    let mut ui = Simulator::new(offset_scene(OFFSET_VIEW, &[OFFSET_ANCHOR]));
    let ring_bottom = drawn_at(wrap_point(OFFSET_ANCHOR_AT));
    drag(&mut ui, ring_bottom, drawn_at(Point::new(600.0, 500.0)));

    let msgs = messages(ui);
    assert!(
        msgs.contains(&Msg::RouteDetached(OFFSET_EDGE, OFFSET_ANCHOR)),
        "the drawn wrap must take the press and report the anchor it came off: {msgs:?}",
    );
    assert!(
        !msgs.iter().any(|m| matches!(m, Msg::Select(_))),
        "the wrap took the press, so no selection box opened: {msgs:?}",
    );
    assert!(
        !msgs.iter().any(|m| matches!(m, Msg::AnchorMove(..))),
        "the wrap took the press, so the core never moved: {msgs:?}",
    );
}

/// The origin and the zoom compose: the wrap of an off-origin graph is grabbed
/// where the two together draw it, and the gesture reports the same detach it
/// does at zoom 1.
#[test]
fn a_wrap_is_grabbable_where_it_is_drawn_at_any_zoom() {
    for camera in OFF_UNIT_VIEWS {
        let view = camera.below_toolbar();
        let zoom = view.zoom;
        let dropped_at = Point::new(400.0, 400.0);
        let mut ui = Simulator::new(offset_scene(view, &[OFFSET_ANCHOR]));
        drag(
            &mut ui,
            view.screen(wrap_point(OFFSET_ANCHOR_AT)),
            view.screen(dropped_at),
        );

        let msgs = messages(ui);
        assert!(
            msgs.contains(&Msg::RouteDetached(OFFSET_EDGE, OFFSET_ANCHOR)),
            "the drawn wrap must report the detach at zoom {zoom}: {msgs:?}",
        );
        assert!(
            msgs.contains(&Msg::AnchorCreated(OFFSET_EDGE, dropped_at)),
            "the pulled-off cable must leave an anchor at zoom {zoom}: {msgs:?}",
        );
        assert!(
            !msgs.iter().any(|m| matches!(m, Msg::AnchorMove(..))),
            "the wrap took the press, so the core never moved at zoom {zoom}: {msgs:?}",
        );
    }
}

// ---------------------------------------------------------------------------
// Minimap
// ---------------------------------------------------------------------------

/// Where the default minimap's center lands in the 1024x768 root: 200x150 in
/// the bottom-right corner, 12 px off both edges.
const MAP_CENTER: Point = Point::new(1024.0 - 12.0 - 100.0, 768.0 - 12.0 - 75.0);

/// A graph with the default minimap enabled and the callbacks a map press must
/// not fire wired alongside the one it must.
fn minimap_graph(
    nodes: &[(usize, Point)],
    anchors: &[(usize, Point)],
) -> Element<'static, Msg, Theme, Renderer> {
    let mut ng: Graph = NodeGraph::default()
        .width(Length::Fill)
        .height(Length::Fill)
        .minimap(Minimap::default())
        .on_camera(Msg::Camera)
        .on_select(Msg::Select)
        .on_move(Msg::Move);
    for &(id, pos) in nodes {
        let body = container(text("n"))
            .width(Length::Fixed(NODE_W))
            .height(Length::Fixed(NODE_H));
        ng = ng.push_node(node(id, pos, body));
    }
    for &(id, at) in anchors {
        ng = ng.push_anchor(anchor(id, at));
    }
    ng.into()
}

/// Whether the world point `p` lands inside the 1024x768 viewport under the
/// camera `(position, zoom)`: screen = (world + position) * zoom.
fn in_viewport(p: Point, position: Point, zoom: f32) -> bool {
    let on_screen = Point::new((p.x + position.x) * zoom, (p.y + position.y) * zoom);
    (0.0..=1024.0).contains(&on_screen.x) && (0.0..=768.0).contains(&on_screen.y)
}

/// The map's center is the center of what it shows, so a press there has to
/// bring the graph into view - here from a node 1000 px past the viewport's
/// far corner, which the default camera does not show at all.
#[test]
fn a_press_at_the_minimap_center_brings_the_graph_into_view() {
    const NEAR: Point = Point::new(100.0, 100.0);
    const FAR: Point = Point::new(2000.0, 1500.0);
    let mut ui = Simulator::new(minimap_graph(&[(0, NEAR), (1, FAR)], &[]));
    click(&mut ui, MAP_CENTER);

    let msgs = messages(ui);
    let (position, zoom) = last_camera(&msgs).expect("a map press must publish a camera");
    // The center of the graph's world bounds, mapped through the published
    // camera.
    let center = Point::new(
        (NEAR.x + FAR.x + NODE_W) / 2.0,
        (NEAR.y + FAR.y + NODE_H) / 2.0,
    );
    assert!(
        in_viewport(center, position, zoom),
        "the graph's center must land in the viewport, got camera {position:?} at zoom {zoom}",
    );
}

/// The map bounds what a frame-all would fit, anchors included: an anchor past
/// every node is on the map, so the map's center is the center of node and
/// anchor together. Bounded by nodes alone, the press would center on the
/// lone node and leave that point off screen.
#[test]
fn the_minimap_bounds_anchors_beyond_every_node() {
    const NEAR: Point = Point::new(100.0, 100.0);
    const FAR_ANCHOR: Point = Point::new(2000.0, 1500.0);
    let mut ui = Simulator::new(minimap_graph(&[(0, NEAR)], &[(ANCHOR_A, FAR_ANCHOR)]));
    click(&mut ui, MAP_CENTER);

    let msgs = messages(ui);
    let (position, zoom) = last_camera(&msgs).expect("a map press must publish a camera");
    let center = Point::new((NEAR.x + FAR_ANCHOR.x) / 2.0, (NEAR.y + FAR_ANCHOR.y) / 2.0);
    assert!(
        in_viewport(center, position, zoom),
        "the node-and-anchor center must land in the viewport, got camera {position:?} \
         at zoom {zoom}",
    );
}

/// The map draws over the canvas, so it must also take the press over it: a
/// node the map happens to cover is chrome's business, not the node's.
#[test]
fn a_press_on_the_minimap_never_reaches_the_node_beneath_it() {
    let covered = Point::new(MAP_CENTER.x - NODE_W / 2.0, MAP_CENTER.y - NODE_H / 2.0);
    let mut ui = Simulator::new(minimap_graph(&[(0, covered)], &[]));
    drag(
        &mut ui,
        MAP_CENTER,
        Point::new(MAP_CENTER.x + 20.0, MAP_CENTER.y + 10.0),
    );

    let msgs = messages(ui);
    assert!(
        !msgs
            .iter()
            .any(|m| matches!(m, Msg::Move(..) | Msg::Select(_))),
        "the covered node must neither move nor be selected: {msgs:?}",
    );
    assert!(
        msgs.iter().any(|m| matches!(m, Msg::Camera(..))),
        "the press must still steer the camera: {msgs:?}",
    );
}