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
//! Full-widget GPU pixel oracle.
//!
//! The SDF crate's `pixel_tests` render SDF primitives in isolation; the unit
//! `coordinate_tests` drive the full `NodeGraph::draw` path but through a MOCK
//! recording renderer (draw-call rects, no rasterization). Neither rasterizes the
//! whole widget - SDF layers AND hosted iced content (text) - to real pixels.
//!
//! This harness does: it drives `NodeGraph::draw` through the REAL
//! `iced_wgpu::Renderer` headlessly (via the shared `common` harness) and reads
//! back the framebuffer via `Renderer::screenshot`. That is the oracle the plan
//! calls for ("the golden harness must drive the FULL widget path ... with a real
//! text+caret node"), and the prerequisite for pixel-gating widget-level Phase C
//! work (layer collapse, static-background cache) without a human in the loop.
//!
//! Tests that need a GPU adapter skip gracefully when none is present (CI without
//! a GPU), exactly like a developer running headless.
#![cfg(not(target_arch = "wasm32"))]

mod common;

use iced::advanced::widget::{Tree, Widget};
use iced::advanced::{Layout, layout, mouse, renderer};
use iced::widget::text;
use iced::{Color, Element, Length, Point, Rectangle, Size, Theme};
use iced_wgpu::core::clipboard;

use common::shared;
use iced_nodegraph::{ColorQuad, NodeGraph, NodeStyle, Pattern, default_node_style, node};
use iced_wgpu::Renderer;
use iced_wgpu::graphics::Viewport;

const W: u32 = 320;
const H: u32 = 240;
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
struct RouteIds;

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

/// Render a one-node graph (node carries hosted text content) to RGBA pixels.
/// Returns `None` if no GPU is available.
fn render_one_node() -> Option<Vec<[u8; 4]>> {
    let mut guard = shared()?;
    let renderer = &mut *guard;

    // Camera centred so the node (world origin) lands mid-viewport at zoom 1.
    let mut graph: NodeGraph<'static, iced_nodegraph::Indexed, (), Theme, Renderer> =
        NodeGraph::default()
            .width(Length::Fixed(W as f32))
            .height(Length::Fixed(H as f32))
            .camera(
                Point::new(W as f32 * 0.5 - 40.0, H as f32 * 0.5 - 20.0),
                1.0,
            );
    graph = graph.push_node(node(
        0_usize,
        Point::new(0.0, 0.0),
        Element::from(text("Hi")),
    ));

    let mut tree = Tree::new(&graph as &dyn Widget<(), Theme, Renderer>);
    let layout_node = graph.layout(
        &mut tree,
        &*renderer,
        &layout::Limits::new(Size::ZERO, Size::new(W as f32, H as f32)),
    );
    let layout = Layout::new(&layout_node);
    let viewport_rect = Rectangle::new(Point::ORIGIN, Size::new(W as f32, H as f32));

    // One update syncs the controlled `camera()` into the widget camera.
    let mut msgs: Vec<()> = Vec::new();
    let mut shell = iced_wgpu::core::Shell::new(&mut msgs);
    let mut clipboard = clipboard::Null;
    graph.update(
        &mut tree,
        &iced::Event::Mouse(mouse::Event::CursorMoved {
            position: Point::new(-1.0, -1.0),
        }),
        layout,
        mouse::Cursor::Unavailable,
        &*renderer,
        &mut clipboard,
        &mut shell,
        &viewport_rect,
    );

    graph.draw(
        &tree,
        renderer,
        &Theme::Dark,
        &renderer::Style {
            text_color: Color::WHITE,
        },
        layout,
        mouse::Cursor::Unavailable,
        &viewport_rect,
    );

    let bytes = renderer.screenshot(
        &Viewport::with_physical_size(Size::new(W, H), 1.0),
        Color::TRANSPARENT,
    );
    Some(bytes.as_chunks::<4>().0.to_vec())
}

/// The full widget rasterizes end-to-end: a full-bounds background PLUS a
/// localized node (fill/border/hosted text) on top. The node is detected against
/// the background - the most-frequent colour is the background; it must dominate
/// (so the frame is not full-screen garbage) yet not cover EVERYTHING (so the
/// node actually drew). Several distinct colours (bg + fill + border + text)
/// rule out a blank or single-colour frame.
#[test]
fn full_widget_renders_localized_node() {
    use std::collections::HashMap;

    let Some(px) = render_one_node() else {
        eprintln!("no GPU adapter - skipping full_widget_renders_localized_node");
        return;
    };
    assert_eq!(px.len(), (W * H) as usize);

    let mut counts: HashMap<[u8; 4], usize> = HashMap::new();
    for p in &px {
        *counts.entry(*p).or_default() += 1;
    }
    let distinct = counts.len();
    let (&bg, &bg_count) = counts.iter().max_by_key(|(_, c)| **c).unwrap();
    let bg_frac = bg_count as f32 / px.len() as f32;

    assert!(
        distinct > 3,
        "near-uniform frame ({distinct} distinct colours): background or node \
         failed to render",
    );
    assert!(
        bg_frac < 0.97,
        "node did not render: background colour {bg:?} covers {:.1}% of the frame",
        bg_frac * 100.0,
    );
    assert!(
        bg_frac > 0.30,
        "full-screen garbage: no dominant background (top colour only {:.1}%)",
        bg_frac * 100.0,
    );
}

/// Render a zoom-out node GRID through the real widget and count nodes whose red
/// body fill is missing or mis-sized at its expected screen centre. `frames` runs
/// the SAME scene through the SHARED (persistent) pipeline that many times before
/// scoring the LAST frame: a static camera repeated across frames drives the
/// background cache Direct -> Populate -> Blit, the cross-frame state the live app
/// hits but a single render never does.
fn zoomout_grid_missing_nodes(scale: f32, frames: u32) -> Option<usize> {
    zoomout_grid_missing_nodes_at(scale, frames, Point::new(-327.7, -132.0), 0.24131)
}

fn zoomout_grid_missing_nodes_at(scale: f32, frames: u32, cam: Point, zoom: f32) -> Option<usize> {
    use iced::widget::container;
    use iced::widget::text;

    // Logical viewport; physical = logical * scale (DPI).
    const GW: u32 = 640;
    const GH: u32 = 480;
    let pw = (GW as f32 * scale) as u32;
    let ph = (GH as f32 * scale) as u32;

    let mut guard = shared()?;
    let renderer = &mut *guard;

    // Camera-relative node grid: a FIXED screen lattice (top-left screen px),
    // with each node's WORLD position derived from the current camera so the grid
    // always fills the viewport at any pan offset. This models panning across
    // content - the node's world coordinate AND cam both vary, the way they do
    // when the user pans right looking for the flicker position.
    let nw = 60.0_f32;
    let nh = 40.0_f32;
    let lattice: Vec<(f32, f32)> = {
        let mut v = Vec::new();
        let mut tly = 30.0;
        while tly < GH as f32 - 30.0 {
            let mut tlx = 30.0;
            while tlx < GW as f32 - 30.0 {
                v.push((tlx, tly));
                tlx += 42.0;
            }
            tly += 38.0;
        }
        v
    };
    // world top-left = screen_topleft/zoom - cam; screen centre = topleft + body/2.
    let world_of = |tlx: f32, tly: f32| (tlx / zoom - cam.x, tly / zoom - cam.y);
    let centers: Vec<(f32, f32)> = lattice
        .iter()
        .map(|&(tlx, tly)| (tlx + nw * zoom * 0.5, tly + nh * zoom * 0.5))
        .collect();

    let viewport_rect = Rectangle::new(Point::ORIGIN, Size::new(GW as f32, GH as f32));
    let mut px: Vec<[u8; 4]> = Vec::new();
    for _ in 0..frames.max(1) {
        // Rebuild the camera each frame, exactly as a live app does.
        let mut graph: NodeGraph<'static, iced_nodegraph::Indexed, (), Theme, Renderer> =
            NodeGraph::default()
                .width(Length::Fixed(GW as f32))
                .height(Length::Fixed(GH as f32))
                .camera(cam, zoom);
        for (id, &(tlx, tly)) in lattice.iter().enumerate() {
            let (wx, wy) = world_of(tlx, tly);
            graph = graph.push_node(
                node(
                    id,
                    Point::new(wx, wy),
                    Element::from(
                        container(text(""))
                            .width(Length::Fixed(nw))
                            .height(Length::Fixed(nh)),
                    ),
                )
                .style(|theme, status| NodeStyle {
                    fill_color: ColorQuad::solid(Color::from_rgb(1.0, 0.0, 0.0)),
                    ..default_node_style(theme, status)
                }),
            );
        }

        let mut tree = Tree::new(&graph as &dyn Widget<(), Theme, Renderer>);
        let layout_node = graph.layout(
            &mut tree,
            &*renderer,
            &layout::Limits::new(Size::ZERO, Size::new(GW as f32, GH as f32)),
        );
        let layout = Layout::new(&layout_node);

        let mut msgs: Vec<()> = Vec::new();
        let mut shell = iced_wgpu::core::Shell::new(&mut msgs);
        let mut clipboard = clipboard::Null;
        graph.update(
            &mut tree,
            &iced::Event::Mouse(mouse::Event::CursorMoved {
                position: Point::new(-1.0, -1.0),
            }),
            layout,
            mouse::Cursor::Unavailable,
            &*renderer,
            &mut clipboard,
            &mut shell,
            &viewport_rect,
        );

        graph.draw(
            &tree,
            renderer,
            &Theme::Dark,
            &renderer::Style {
                text_color: Color::WHITE,
            },
            layout,
            mouse::Cursor::Unavailable,
            &viewport_rect,
        );

        let bytes = renderer.screenshot(
            &Viewport::with_physical_size(Size::new(pw, ph), scale),
            Color::TRANSPARENT,
        );
        px = bytes.as_chunks::<4>().0.to_vec();
    }

    let is_red = |p: &[u8; 4]| p[0] > 120 && p[1] < 90 && p[2] < 90;
    // Expected node body size in PHYSICAL pixels at this camera.
    let exp_w = nw * zoom * scale;
    let exp_h = nh * zoom * scale;
    let mut missing = 0usize;
    for (scx, scy) in &centers {
        // Only score nodes whose centre lands comfortably inside the viewport.
        if *scx < 16.0 || *scy < 16.0 || *scx > (GW as f32 - 16.0) || *scy > (GH as f32 - 16.0) {
            continue;
        }
        // Sample a generous window (PHYSICAL px) around the expected centre and
        // measure the red bounding box - both presence AND size. A "collapsed"
        // node is empty, shrunk, or blown up well past its true footprint.
        let cx = (*scx * scale) as i32;
        let cy = (*scy * scale) as i32;
        let win = (24.0 * scale) as i32;
        let (mut rminx, mut rminy, mut rmaxx, mut rmaxy) = (i32::MAX, i32::MAX, i32::MIN, i32::MIN);
        let mut red = 0;
        for dy in -win..=win {
            for dx in -win..=win {
                let x = cx + dx;
                let y = cy + dy;
                if x < 0 || y < 0 || x >= pw as i32 || y >= ph as i32 {
                    continue;
                }
                if is_red(&px[(y as u32 * pw + x as u32) as usize]) {
                    red += 1;
                    rminx = rminx.min(x);
                    rminy = rminy.min(y);
                    rmaxx = rmaxx.max(x);
                    rmaxy = rmaxy.max(y);
                }
            }
        }
        if red < 4 {
            missing += 1;
            continue;
        }
        let bw = (rmaxx - rminx + 1) as f32;
        let bh = (rmaxy - rminy + 1) as f32;
        // Flag a node whose red footprint is far from its true body size in either
        // axis (shrunk to a speck or ballooned across neighbours).
        let wrong_size = bw < exp_w * 0.5
            || bh < exp_h * 0.5
            || bw > exp_w * 2.2 + 6.0
            || bh > exp_h * 2.2 + 6.0;
        if wrong_size {
            missing += 1;
        }
    }
    Some(missing)
}

#[test]
fn zoomout_grid_all_nodes_render() {
    for scale in [1.0_f32, 1.5, 2.0] {
        let Some(missing) = zoomout_grid_missing_nodes(scale, 1) else {
            eprintln!("no GPU adapter - skipping zoomout_grid_all_nodes_render");
            return;
        };
        assert_eq!(
            missing, 0,
            "{missing} node fills did not render at zoom 0.24, scale {scale} \
             (zoom-out float collapse)",
        );
    }
}

/// Root-cause repro for the pan-dependent washed nodes (fill over text, no
/// border/pins): iced PREPARES every custom-primitive instance but SKIPS drawing
/// the ones whose bounds snap empty / fall off the viewport. The SDF pipeline pairs
/// prepare-order to draw-order with a draw counter, so ONE skipped node desyncs the
/// `DrawData` index of every later node - they then read the wrong camera/tiles and
/// misrender. Here node 0 sits off the right of the framebuffer (graph wider than
/// the screenshot viewport) so iced skips drawing it; the fully-visible nodes that
/// follow must still render their fill AND text.
#[test]
fn offscreen_node_does_not_desync_later_nodes() {
    use iced::widget::{container, text};

    // Graph wider than the framebuffer so a node can sit off-viewport-right.
    const GW: u32 = 800;
    const GH: u32 = 320;
    const VW: u32 = 640; // screenshot viewport (framebuffer) width
    let zoom = 1.0_f32;
    let cam = Point::new(0.0, 0.0);
    let nw = 80.0_f32;
    let nh = 44.0_f32;

    let Some(mut guard) = shared() else {
        eprintln!("no GPU adapter - skipping offscreen_node_does_not_desync_later_nodes");
        return;
    };
    let renderer = &mut *guard;

    // Node 0 is off the right of the framebuffer (x = 700, framebuffer is 640 wide)
    // but inside the 800-wide graph, so it is PREPARED yet skipped in render.
    // Nodes 1..=3 are fully visible and submitted after it.
    let visible = [(60.0_f32, 120.0_f32), (240.0, 120.0), (420.0, 120.0)];
    let mut worlds = vec![(700.0_f32, 120.0_f32)];
    worlds.extend_from_slice(&visible);

    let mut graph: NodeGraph<'static, iced_nodegraph::Indexed, (), Theme, Renderer> =
        NodeGraph::default()
            .width(Length::Fixed(GW as f32))
            .height(Length::Fixed(GH as f32))
            .camera(cam, zoom);
    for (id, &(wx, wy)) in worlds.iter().enumerate() {
        graph = graph.push_node(
            node(
                id,
                Point::new(wx, wy),
                Element::from(
                    container(text("Xy"))
                        .width(Length::Fixed(nw))
                        .height(Length::Fixed(nh)),
                ),
            )
            .style(|theme, status| NodeStyle {
                fill_color: ColorQuad::solid(Color::from_rgb(0.10, 0.15, 0.55)),
                shadow_distance: 0.0,
                shadow_color: Color::TRANSPARENT,
                ..default_node_style(theme, status)
            }),
        );
    }

    let mut tree = Tree::new(&graph as &dyn Widget<(), Theme, Renderer>);
    let layout_node = graph.layout(
        &mut tree,
        &*renderer,
        &layout::Limits::new(Size::ZERO, Size::new(GW as f32, GH as f32)),
    );
    let layout = Layout::new(&layout_node);
    let viewport_rect = Rectangle::new(Point::ORIGIN, Size::new(GW as f32, GH as f32));

    let mut msgs: Vec<()> = Vec::new();
    let mut shell = iced_wgpu::core::Shell::new(&mut msgs);
    let mut clipboard = clipboard::Null;
    graph.update(
        &mut tree,
        &iced::Event::Mouse(mouse::Event::CursorMoved {
            position: Point::new(-1.0, -1.0),
        }),
        layout,
        mouse::Cursor::Unavailable,
        &*renderer,
        &mut clipboard,
        &mut shell,
        &viewport_rect,
    );
    graph.draw(
        &tree,
        renderer,
        &Theme::Dark,
        &renderer::Style {
            text_color: Color::WHITE,
        },
        layout,
        mouse::Cursor::Unavailable,
        &viewport_rect,
    );

    let bytes = renderer.screenshot(
        &Viewport::with_physical_size(Size::new(VW, GH), 1.0),
        Color::from_rgb(0.0, 0.0, 0.0),
    );
    let px: Vec<[u8; 4]> = bytes.as_chunks::<4>().0.to_vec();

    let is_fill = |p: &[u8; 4]| p[2] > 90 && p[2] as i32 > p[0] as i32 + 30;
    let is_text = |p: &[u8; 4]| p[0] > 170 && p[1] > 170 && p[2] > 170;
    let mut broken = Vec::new();
    for (i, &(wx, wy)) in visible.iter().enumerate() {
        let cx = ((wx + nw * 0.5 + cam.x) * zoom) as i32;
        let cy = ((wy + nh * 0.5 + cam.y) * zoom) as i32;
        let (mut fill_px, mut text_px) = (0, 0);
        for dy in -20..=20i32 {
            for dx in -36..=36i32 {
                let x = cx + dx;
                let y = cy + dy;
                if x < 0 || y < 0 || x >= VW as i32 || y >= GH as i32 {
                    continue;
                }
                let p = &px[(y as u32 * VW + x as u32) as usize];
                if is_fill(p) {
                    fill_px += 1;
                }
                if is_text(p) {
                    text_px += 1;
                }
            }
        }
        if fill_px < 100 || text_px < 3 {
            broken.push((i + 1, fill_px, text_px));
        }
    }
    assert!(
        broken.is_empty(),
        "off-viewport node 0 desynced later nodes' DrawData: broken (node, fill_px, text_px) = {broken:?}",
    );
}

/// The full-widget render is deterministic: a static graph produces byte-
/// identical pixels across renders. This is the property layer-collapse and the
/// background-texture cache must preserve, and what makes a golden image stable.
#[test]
fn full_widget_render_is_deterministic() {
    let Some(a) = render_one_node() else {
        eprintln!("no GPU adapter - skipping full_widget_render_is_deterministic");
        return;
    };
    let b = render_one_node().expect("GPU was available a moment ago");
    let differ = a.iter().zip(b.iter()).filter(|(x, y)| x != y).count();
    assert_eq!(differ, 0, "full-widget render flickered on {differ} pixels");
}

/// Writes idle and selected renders of the same node side by side, for eyeballing
/// the theme-derived selection default.
#[test]
#[ignore = "visual probe: writes selected_node_idle.png / selected_node_sel.png"]
fn probe_selected_node_appearance() {
    for (selected, name) in [(false, "idle"), (true, "sel")] {
        let Some(px) = render_node_selection(selected) else {
            eprintln!("no GPU adapter; skipping");
            return;
        };
        let path = format!("selected_node_{name}.png");
        let file = std::fs::File::create(&path).unwrap();
        let mut enc = png::Encoder::new(std::io::BufWriter::new(file), W, H);
        enc.set_color(png::ColorType::Rgba);
        enc.set_depth(png::BitDepth::Eight);
        let mut w = enc.write_header().unwrap();
        let flat: Vec<u8> = px.iter().flat_map(|p| p.iter().copied()).collect();
        w.write_image_data(&flat).unwrap();
        eprintln!("wrote {path}");
    }
}

/// One node, optionally selected via the controlled `selection()` channel.
fn render_node_selection(selected: bool) -> Option<Vec<[u8; 4]>> {
    let mut guard = shared()?;
    let renderer = &mut *guard;

    let mut graph: NodeGraph<'static, iced_nodegraph::Indexed, (), Theme, Renderer> =
        NodeGraph::default()
            .width(Length::Fixed(W as f32))
            .height(Length::Fixed(H as f32))
            .camera(
                Point::new(W as f32 * 0.5 - 40.0, H as f32 * 0.5 - 20.0),
                1.0,
            );
    // A realistically sized body: the halo is judged relative to the node, and a
    // bare text label is an order of magnitude smaller than a real node.
    graph = graph.push_node(
        node(
            0_usize,
            Point::new(0.0, 0.0),
            Element::from(
                iced::widget::container(text("Node"))
                    .width(Length::Fixed(160.0))
                    .height(Length::Fixed(90.0)),
            ),
        )
        .selected(selected),
    );

    let mut tree = Tree::new(&graph as &dyn Widget<(), Theme, Renderer>);
    let layout_node = graph.layout(
        &mut tree,
        &*renderer,
        &layout::Limits::new(Size::ZERO, Size::new(W as f32, H as f32)),
    );
    let layout = Layout::new(&layout_node);
    let viewport_rect = Rectangle::new(Point::ORIGIN, Size::new(W as f32, H as f32));

    let mut msgs: Vec<()> = Vec::new();
    let mut shell = iced_wgpu::core::Shell::new(&mut msgs);
    let mut clipboard = clipboard::Null;
    graph.update(
        &mut tree,
        &iced::Event::Mouse(mouse::Event::CursorMoved {
            position: Point::new(-1.0, -1.0),
        }),
        layout,
        mouse::Cursor::Unavailable,
        &*renderer,
        &mut clipboard,
        &mut shell,
        &viewport_rect,
    );
    graph.draw(
        &tree,
        renderer,
        &Theme::Dark,
        &renderer::Style {
            text_color: Color::WHITE,
        },
        layout,
        mouse::Cursor::Unavailable,
        &viewport_rect,
    );
    let bytes = renderer.screenshot(
        &Viewport::with_physical_size(Size::new(W, H), 1.0),
        Color::TRANSPARENT,
    );
    Some(bytes.as_chunks::<4>().0.to_vec())
}

/// A graph inside a node, viewed through a zoomed outer camera: the outer
/// camera at zoom 2 over one node at the world origin whose body is a 150x100
/// inner graph at zoom 1, holding one red 40x20 node at inner world (10, 10).
/// That node's screen rect is (20, 20)-(100, 60).
fn render_nested_graph() -> Option<Vec<[u8; 4]>> {
    let mut guard = shared()?;
    let renderer = &mut *guard;

    let inner: NodeGraph<'static, iced_nodegraph::Indexed, (), Theme, Renderer> =
        NodeGraph::default()
            .width(Length::Fixed(150.0))
            .height(Length::Fixed(100.0))
            .camera(Point::ORIGIN, 1.0)
            .push_node(
                node(
                    0_usize,
                    Point::new(10.0, 10.0),
                    Element::from(
                        iced::widget::container(iced::widget::Space::new())
                            .width(Length::Fixed(40.0))
                            .height(Length::Fixed(20.0)),
                    ),
                )
                .style(|theme, status| NodeStyle {
                    fill_color: ColorQuad::solid(Color::from_rgb(1.0, 0.0, 0.0)),
                    corner_radius: 0.0,
                    border_pattern: Pattern::solid(0.0),
                    border_outline_width: 0.0,
                    shadow_distance: 0.0,
                    shadow_color: Color::TRANSPARENT,
                    ..default_node_style(theme, status)
                }),
            );
    let mut graph: NodeGraph<'static, iced_nodegraph::Indexed, (), Theme, Renderer> =
        NodeGraph::default()
            .width(Length::Fixed(W as f32))
            .height(Length::Fixed(H as f32))
            .camera(Point::ORIGIN, 2.0);
    graph = graph.push_node(node(0_usize, Point::ORIGIN, Element::from(inner)));

    let mut tree = Tree::new(&graph as &dyn Widget<(), Theme, Renderer>);
    let layout_node = graph.layout(
        &mut tree,
        &*renderer,
        &layout::Limits::new(Size::ZERO, Size::new(W as f32, H as f32)),
    );
    let layout = Layout::new(&layout_node);
    let viewport_rect = Rectangle::new(Point::ORIGIN, Size::new(W as f32, H as f32));

    // One update syncs both cameras: the outer directly, the inner through
    // child dispatch.
    let mut msgs: Vec<()> = Vec::new();
    let mut shell = iced_wgpu::core::Shell::new(&mut msgs);
    let mut clipboard = clipboard::Null;
    graph.update(
        &mut tree,
        &iced::Event::Mouse(mouse::Event::CursorMoved {
            position: Point::new(-1.0, -1.0),
        }),
        layout,
        mouse::Cursor::Unavailable,
        &*renderer,
        &mut clipboard,
        &mut shell,
        &viewport_rect,
    );
    graph.draw(
        &tree,
        renderer,
        &Theme::Dark,
        &renderer::Style {
            text_color: Color::WHITE,
        },
        layout,
        mouse::Cursor::Unavailable,
        &viewport_rect,
    );
    let bytes = renderer.screenshot(
        &Viewport::with_physical_size(Size::new(W, H), 1.0),
        Color::TRANSPARENT,
    );
    Some(bytes.as_chunks::<4>().0.to_vec())
}

/// The SDF layers of a nested graph scale with the enclosing camera like its
/// iced content does: the inner node's fill covers its full doubled rect, not
/// only the unscaled half of it.
#[test]
fn nested_graph_sdf_scales_with_outer_zoom() {
    let Some(px) = render_nested_graph() else {
        eprintln!("no GPU adapter; skipping");
        return;
    };
    let at = |x: u32, y: u32| px[(y * W + x) as usize];
    let is_red = |p: [u8; 4]| p[0] > 120 && p[1] < 90 && p[2] < 90;
    assert!(
        is_red(at(30, 30)),
        "inner node fill missing near its top-left corner: {:?}",
        at(30, 30),
    );
    assert!(
        is_red(at(90, 50)),
        "inner node fill must reach the far side of its doubled rect: {:?}",
        at(90, 50),
    );
    assert!(
        !is_red(at(110, 50)),
        "inner node fill must stop at its doubled rect: {:?}",
        at(110, 50),
    );
}

/// Writes the nested-graph scene to PNG for eyeballing: the inner grid, node
/// and text must all sit at double size, aligned with each other.
#[test]
#[ignore = "visual probe: writes nested_graph.png"]
fn probe_nested_graph_appearance() {
    let Some(px) = render_nested_graph() else {
        eprintln!("no GPU adapter; skipping");
        return;
    };
    let path = "nested_graph.png";
    let file = std::fs::File::create(path).unwrap();
    let mut enc = png::Encoder::new(std::io::BufWriter::new(file), W, H);
    enc.set_color(png::ColorType::Rgba);
    enc.set_depth(png::BitDepth::Eight);
    let mut w = enc.write_header().unwrap();
    let flat: Vec<u8> = px.iter().flat_map(|p| p.iter().copied()).collect();
    w.write_image_data(&flat).unwrap();
    eprintln!("wrote {path}");
}

/// One node whose single Output pin is drawn as `shape` in an unmistakable
/// magenta, big enough that its footprint spans tens of pixels.
fn render_pin_shape(shape: iced_nodegraph::PinShape) -> Option<Vec<[u8; 4]>> {
    use iced_nodegraph::{PinDirection, PinSide, PinStyle, default_pin_style, node_pin};

    let mut guard = shared()?;
    let renderer = &mut *guard;

    let mut graph: NodeGraph<'static, iced_nodegraph::Indexed, (), Theme, Renderer> =
        NodeGraph::default()
            .width(Length::Fixed(W as f32))
            .height(Length::Fixed(H as f32))
            .camera(Point::ORIGIN, 1.0);
    graph = graph.push_node(
        node(
            0_usize,
            Point::new(90.0, 70.0),
            Element::from(
                iced::widget::container(
                    node_pin(PinSide::Left, 0_usize, text("")).direction(PinDirection::Output),
                )
                .width(Length::Fixed(120.0))
                .height(Length::Fixed(80.0)),
            ),
        )
        .pin_style(move |theme, _pin, _peer, status| PinStyle {
            color: Color::from_rgb(1.0, 0.0, 1.0).into(),
            radius: 20.0,
            shape,
            // A cutout or a border would add geometry outside the indicator and
            // blur the footprint this test measures.
            cutout_radius: 0.0,
            border_width: 0.0,
            ..default_pin_style(theme, status)
        }),
    );

    let mut tree = Tree::new(&graph as &dyn Widget<(), Theme, Renderer>);
    let layout_node = graph.layout(
        &mut tree,
        &*renderer,
        &layout::Limits::new(Size::ZERO, Size::new(W as f32, H as f32)),
    );
    let layout = Layout::new(&layout_node);
    let viewport_rect = Rectangle::new(Point::ORIGIN, Size::new(W as f32, H as f32));

    let mut msgs: Vec<()> = Vec::new();
    let mut shell = iced_wgpu::core::Shell::new(&mut msgs);
    let mut clipboard = clipboard::Null;
    graph.update(
        &mut tree,
        &iced::Event::Mouse(mouse::Event::CursorMoved {
            position: Point::new(-1.0, -1.0),
        }),
        layout,
        mouse::Cursor::Unavailable,
        &*renderer,
        &mut clipboard,
        &mut shell,
        &viewport_rect,
    );
    graph.draw(
        &tree,
        renderer,
        &Theme::Dark,
        &renderer::Style {
            text_color: Color::WHITE,
        },
        layout,
        mouse::Cursor::Unavailable,
        &viewport_rect,
    );
    let bytes = renderer.screenshot(
        &Viewport::with_physical_size(Size::new(W, H), 1.0),
        Color::TRANSPARENT,
    );
    Some(bytes.as_chunks::<4>().0.to_vec())
}

/// Fraction of the magenta footprint's bounding box that the footprint covers.
/// A disc fills PI/4 of its box; an axis-aligned square fills all of it.
fn magenta_box_fill(px: &[[u8; 4]]) -> f32 {
    let hit = |p: &[u8; 4]| p[0] > 200 && p[2] > 200 && p[1] < 60;
    let (mut x0, mut y0, mut x1, mut y1) = (W, H, 0u32, 0u32);
    let mut count = 0u32;
    for (i, p) in px.iter().enumerate() {
        if !hit(p) {
            continue;
        }
        let (x, y) = (i as u32 % W, i as u32 / W);
        x0 = x0.min(x);
        y0 = y0.min(y);
        x1 = x1.max(x);
        y1 = y1.max(y);
        count += 1;
    }
    assert!(count > 300, "pin indicator did not render ({count} px)");
    count as f32 / ((x1 - x0 + 1) * (y1 - y0 + 1)) as f32
}

/// `PinShape` is a rendering contract, not a label: every variant must reach
/// the GPU as its own silhouette. Circle and Square are drawn at equal area, so
/// only the footprint's shape tells them apart - a disc leaves its bounding
/// box's corners empty, a square fills them.
#[test]
fn every_pin_shape_draws_its_own_silhouette() {
    use iced_nodegraph::PinShape;

    let Some(circle) = render_pin_shape(PinShape::Circle) else {
        eprintln!("no GPU adapter - skipping every_pin_shape_draws_its_own_silhouette");
        return;
    };
    let square = render_pin_shape(PinShape::Square).expect("GPU was available a moment ago");

    let circle_fill = magenta_box_fill(&circle);
    let square_fill = magenta_box_fill(&square);
    assert!(
        circle_fill < 0.85,
        "Circle filled {circle_fill:.3} of its bounding box; a disc covers about 0.785",
    );
    assert!(
        square_fill > 0.93,
        "Square filled {square_fill:.3} of its bounding box; a square covers about 1.0 - \
         it is being drawn as a disc",
    );
}

/// Which routing scene to render: no anchor at all, an anchor no cable wraps, a
/// cable routed through that anchor, that cable with the anchor held mid-drag,
/// or with the cursor resting on the cable's grabbable output end.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum RouteScene {
    Bare,
    Anchored,
    Wrapped,
    Dragging,
    HoveringEnd,
}

/// Render two pin-carrying nodes joined by one edge, with an anchor placed well
/// below the direct pin-to-pin line and, in the routed scenes, named in that
/// edge's route.
///
/// Each node's content is one 60x50 pin body, so its pin anchor sits at the
/// middle of the side it is on: node 0's Right/output pin at world (60, 25) and
/// node 1's Left/input pin at (200, 25). `camera()` offsets world by (20, 20) at
/// zoom 1, so those are screen (80, 45) and (220, 45).
fn render_routed_edge(scene: RouteScene) -> Option<Vec<[u8; 4]>> {
    use iced::widget::container;
    use iced_nodegraph::{PinDirection, PinRef, PinSide, anchor, edge, node_pin};

    let mut guard = shared()?;
    let renderer = &mut *guard;

    const ANCHOR: usize = 9;

    let mut graph: NodeGraph<'static, iced_nodegraph::Indexed, (), Theme, Renderer> =
        NodeGraph::default()
            .width(Length::Fixed(W as f32))
            .height(Length::Fixed(H as f32))
            .camera(Point::new(20.0, 20.0), 1.0)
            .on_connect(|_, _| ())
            .on_anchor_move(|_id, _position| ());

    let pin_body = || {
        container(text("p"))
            .width(Length::Fixed(60.0))
            .height(Length::Fixed(50.0))
    };
    graph = graph.push_node(node(
        0usize,
        Point::new(0.0, 0.0),
        Element::from(node_pin(PinSide::Right, 0usize, pin_body()).direction(PinDirection::Output)),
    ));
    graph = graph.push_node(node(
        1usize,
        Point::new(200.0, 0.0),
        Element::from(node_pin(PinSide::Left, 0usize, pin_body()).direction(PinDirection::Input)),
    ));

    let routed = matches!(
        scene,
        RouteScene::Wrapped | RouteScene::Dragging | RouteScene::HoveringEnd
    );
    let route: Vec<usize> = if routed { vec![ANCHOR] } else { Vec::new() };
    graph = graph
        .push_edge(edge((), PinRef::new(0usize, 0usize), PinRef::new(1usize, 0usize)).route(route));
    if scene != RouteScene::Bare {
        graph = graph.push_anchor(anchor(ANCHOR, Point::new(115.0, 160.0)));
    }

    let mut tree = Tree::new(&graph as &dyn Widget<(), Theme, Renderer>);
    let layout_node = graph.layout(
        &mut tree,
        &*renderer,
        &layout::Limits::new(Size::ZERO, Size::new(W as f32, H as f32)),
    );
    let layout = Layout::new(&layout_node);
    let viewport_rect = Rectangle::new(Point::ORIGIN, Size::new(W as f32, H as f32));

    let mut msgs: Vec<()> = Vec::new();
    let mut shell = iced_wgpu::core::Shell::new(&mut msgs);
    let mut clipboard = clipboard::Null;
    let mut feed =
        |graph: &mut NodeGraph<'static, iced_nodegraph::Indexed, (), Theme, Renderer>,
         tree: &mut Tree,
         event: iced::Event,
         cursor: mouse::Cursor| {
            graph.update(
                tree,
                &event,
                layout,
                cursor,
                &*renderer,
                &mut clipboard,
                &mut shell,
                &viewport_rect,
            );
        };
    feed(
        &mut graph,
        &mut tree,
        iced::Event::Mouse(mouse::Event::CursorMoved {
            position: Point::new(-1.0, -1.0),
        }),
        mouse::Cursor::Unavailable,
    );

    let mut cursor = mouse::Cursor::Unavailable;
    if scene == RouteScene::HoveringEnd {
        // 20 px along the cable from the output pin: inside the 24 px end zone
        // a press would unplug, and 16 px clear of the pin itself.
        let on_end = GLOW_HOVER;
        feed(
            &mut graph,
            &mut tree,
            iced::Event::Mouse(mouse::Event::CursorMoved { position: on_end }),
            mouse::Cursor::Available(on_end),
        );
        cursor = mouse::Cursor::Available(on_end);
    }
    // Grab the anchor and hold the cursor elsewhere WITHOUT releasing, so the
    // frame shows the in-flight preview rather than a committed move.
    if scene == RouteScene::Dragging {
        let grab = Point::new(135.0, 180.0);
        let held = Point::new(95.0, 130.0);
        for (event, at) in [
            (
                iced::Event::Mouse(mouse::Event::CursorMoved { position: grab }),
                grab,
            ),
            (
                iced::Event::Mouse(mouse::Event::ButtonPressed(mouse::Button::Left)),
                grab,
            ),
            (
                iced::Event::Mouse(mouse::Event::CursorMoved { position: held }),
                held,
            ),
        ] {
            feed(&mut graph, &mut tree, event, mouse::Cursor::Available(at));
        }
        cursor = mouse::Cursor::Available(held);
    }

    graph.draw(
        &tree,
        renderer,
        &Theme::Dark,
        &renderer::Style {
            text_color: Color::WHITE,
        },
        layout,
        cursor,
        &viewport_rect,
    );

    let bytes = renderer.screenshot(
        &Viewport::with_physical_size(Size::new(W, H), 1.0),
        Color::TRANSPARENT,
    );
    Some(bytes.as_chunks::<4>().0.to_vec())
}

/// Renders the scene with `route` applied to the edge, optionally after driving
/// a route drag from the cable's mid-run onto the anchor.
///
/// `route` is what the HOST has caught up to, so an empty route with a drag in
/// flight is the frame between the widget publishing the attachment and the
/// host applying it - the frame the preview has to carry alone.
fn render_route_drag(route: &[usize], drag: bool) -> Option<Vec<[u8; 4]>> {
    use iced::widget::container;
    use iced_nodegraph::{PinDirection, PinRef, PinSide, anchor, edge, node_pin};

    let mut guard = shared()?;
    let renderer = &mut *guard;

    const ANCHOR: usize = 9;

    let mut graph: NodeGraph<'static, RouteIds, (), Theme, Renderer> = NodeGraph::default()
        .width(Length::Fixed(W as f32))
        .height(Length::Fixed(H as f32))
        .camera(Point::new(20.0, 20.0), 1.0)
        .on_anchor_create(|_, _| ())
        .on_route_attach(|_, _| ())
        .on_route_detach(|_, _| ());

    let pin_body = || {
        container(text("p"))
            .width(Length::Fixed(60.0))
            .height(Length::Fixed(50.0))
    };
    graph = graph.push_node(node(
        0usize,
        Point::new(0.0, 0.0),
        Element::from(node_pin(PinSide::Right, 0usize, pin_body()).direction(PinDirection::Output)),
    ));
    graph = graph.push_node(node(
        1usize,
        Point::new(200.0, 0.0),
        Element::from(node_pin(PinSide::Left, 0usize, pin_body()).direction(PinDirection::Input)),
    ));
    graph = graph.push_anchor(anchor(ANCHOR, Point::new(115.0, 160.0)));
    graph = graph.push_edge(
        edge(
            0usize,
            PinRef::new(0usize, 0usize),
            PinRef::new(1usize, 0usize),
        )
        .route(route.to_vec()),
    );

    let mut tree = Tree::new(&graph as &dyn Widget<(), Theme, Renderer>);
    let layout_node = graph.layout(
        &mut tree,
        &*renderer,
        &layout::Limits::new(Size::ZERO, Size::new(W as f32, H as f32)),
    );
    let layout = Layout::new(&layout_node);
    let viewport_rect = Rectangle::new(Point::ORIGIN, Size::new(W as f32, H as f32));

    let mut msgs: Vec<()> = Vec::new();
    let mut shell = iced_wgpu::core::Shell::new(&mut msgs);
    let mut clipboard = clipboard::Null;
    let mut cursor = mouse::Cursor::Unavailable;
    // One event before anything else, so `update` syncs `camera()` into the
    // camera and picks up the viewport origin. Without it a frame that feeds no
    // events is drawn from an unsynced camera and lands offset from one that
    // does - which would read as a geometry difference.
    graph.update(
        &mut tree,
        &iced::Event::Mouse(mouse::Event::CursorMoved {
            position: Point::new(-1.0, -1.0),
        }),
        layout,
        mouse::Cursor::Unavailable,
        &*renderer,
        &mut clipboard,
        &mut shell,
        &viewport_rect,
    );
    if drag {
        // Grab the bare cable's mid-run, then hold the cursor on the anchor's
        // core without releasing.
        let mid = Point::new(150.0, 45.0);
        let core = Point::new(135.0, 180.0);
        for (event, at) in [
            (
                iced::Event::Mouse(mouse::Event::CursorMoved { position: mid }),
                mid,
            ),
            (
                iced::Event::Mouse(mouse::Event::ButtonPressed(mouse::Button::Left)),
                mid,
            ),
            (
                iced::Event::Mouse(mouse::Event::CursorMoved { position: core }),
                core,
            ),
        ] {
            graph.update(
                &mut tree,
                &event,
                layout,
                mouse::Cursor::Available(at),
                &*renderer,
                &mut clipboard,
                &mut shell,
                &viewport_rect,
            );
        }
        cursor = mouse::Cursor::Available(core);
    }

    graph.draw(
        &tree,
        renderer,
        &Theme::Dark,
        &renderer::Style {
            text_color: Color::WHITE,
        },
        layout,
        cursor,
        &viewport_rect,
    );

    let bytes = renderer.screenshot(
        &Viewport::with_physical_size(Size::new(W, H), 1.0),
        Color::TRANSPARENT,
    );
    Some(bytes.as_chunks::<4>().0.to_vec())
}

/// Drives the wrap-grab gesture across frames: grab the cable where it wraps
/// the anchor, pull it off, then put it back on the core.
///
/// The graph is rebuilt from a fresh route at each step over one surviving
/// `Tree`, so the host can be seen catching up with the detach - or not.
/// `after_detach` is its route once the detach has been reported,
/// `at_reattach` its route on the frame the drag snaps back on. The returned
/// pixels are that last frame.
fn render_regrab(after_detach: &[usize], at_reattach: &[usize]) -> Option<Vec<[u8; 4]>> {
    use iced::widget::container;
    use iced_nodegraph::{PinDirection, PinRef, PinSide, anchor, edge, node_pin};

    let mut guard = shared()?;
    let renderer = &mut *guard;

    const ANCHOR: usize = 9;

    let scene = |route: &[usize]| {
        let mut graph: NodeGraph<'static, RouteIds, (), Theme, Renderer> = NodeGraph::default()
            .width(Length::Fixed(W as f32))
            .height(Length::Fixed(H as f32))
            .camera(Point::new(20.0, 20.0), 1.0)
            .on_anchor_create(|_, _| ())
            .on_route_attach(|_, _| ())
            .on_route_detach(|_, _| ());
        let pin_body = || {
            container(text("p"))
                .width(Length::Fixed(60.0))
                .height(Length::Fixed(50.0))
        };
        graph = graph.push_node(node(
            0usize,
            Point::new(0.0, 0.0),
            Element::from(
                node_pin(PinSide::Right, 0usize, pin_body()).direction(PinDirection::Output),
            ),
        ));
        graph = graph.push_node(node(
            1usize,
            Point::new(200.0, 0.0),
            Element::from(
                node_pin(PinSide::Left, 0usize, pin_body()).direction(PinDirection::Input),
            ),
        ));
        graph = graph.push_anchor(anchor(ANCHOR, Point::new(115.0, 160.0)));
        graph = graph.push_edge(
            edge(
                0usize,
                PinRef::new(0usize, 0usize),
                PinRef::new(1usize, 0usize),
            )
            .route(route.to_vec()),
        );
        graph
    };

    let wrap = Point::new(135.0, 191.0);
    let away = Point::new(135.0, 240.0);
    let core = Point::new(135.0, 180.0);
    // Route in force for each step, and the event driving it.
    let steps: [(&[usize], iced::Event, Point); 4] = [
        (
            &[ANCHOR],
            iced::Event::Mouse(mouse::Event::CursorMoved { position: wrap }),
            wrap,
        ),
        (
            &[ANCHOR],
            iced::Event::Mouse(mouse::Event::ButtonPressed(mouse::Button::Left)),
            wrap,
        ),
        (
            &[ANCHOR],
            iced::Event::Mouse(mouse::Event::CursorMoved { position: away }),
            away,
        ),
        (
            after_detach,
            iced::Event::Mouse(mouse::Event::CursorMoved { position: core }),
            core,
        ),
    ];

    let mut graph = scene(&[ANCHOR]);
    let mut tree = Tree::new(&graph as &dyn Widget<(), Theme, Renderer>);
    let layout_node = graph.layout(
        &mut tree,
        &*renderer,
        &layout::Limits::new(Size::ZERO, Size::new(W as f32, H as f32)),
    );
    let layout = Layout::new(&layout_node);
    let viewport_rect = Rectangle::new(Point::ORIGIN, Size::new(W as f32, H as f32));
    let mut msgs: Vec<()> = Vec::new();
    let mut shell = iced_wgpu::core::Shell::new(&mut msgs);
    let mut clipboard = clipboard::Null;

    for (route, event, at) in steps {
        let mut g = scene(route);
        tree.diff(&g as &dyn Widget<(), Theme, Renderer>);
        g.update(
            &mut tree,
            &event,
            layout,
            mouse::Cursor::Available(at),
            &*renderer,
            &mut clipboard,
            &mut shell,
            &viewport_rect,
        );
    }

    // The frame the user is looking at: snapped back on, host at `at_reattach`.
    let final_graph = scene(at_reattach);
    tree.diff(&final_graph as &dyn Widget<(), Theme, Renderer>);
    final_graph.draw(
        &tree,
        renderer,
        &Theme::Dark,
        &renderer::Style {
            text_color: Color::WHITE,
        },
        layout,
        mouse::Cursor::Available(core),
        &viewport_rect,
    );
    let bytes = renderer.screenshot(
        &Viewport::with_physical_size(Size::new(W, H), 1.0),
        Color::TRANSPARENT,
    );
    let _ = &mut graph;
    Some(bytes.as_chunks::<4>().0.to_vec())
}

/// A snapped route drag previews the cable it is about to commit.
///
/// The widget publishes the attachment on snap, so for at least one frame it
/// renders a route the host has not applied yet. That frame must already draw
/// the cable where the committed frame draws it, and must NOT still be drawing
/// the straight bare cable. The anchor's own rings are excluded from the
/// comparison: an offered orbit legitimately becomes an occupied one.
#[test]
fn a_snapped_route_preview_draws_the_cable_it_will_commit() {
    let Some(previewed) = render_route_drag(&[], true) else {
        eprintln!("no GPU adapter; skipping");
        return;
    };
    // The end state, drawn without any drag in flight: what the preview is
    // promising the user they will get.
    let committed = render_route_drag(&[9], false).expect("adapter present");
    let bare = render_route_drag(&[], false).expect("adapter present");
    // Pull the cable off the anchor and put it straight back on. Empty
    // `at_reattach` is the frame before the host applies the re-attachment;
    // naming the anchor there is every frame after it has, which is the whole
    // rest of the drag - and the case where the drag's own record of what it
    // detached must not outrank being snapped to it.
    let regrabbed_pending = render_regrab(&[], &[]).expect("adapter present");
    let regrabbed_applied = render_regrab(&[], &[9]).expect("adapter present");

    // The anchor's core sits at screen (135, 180); its outermost drawn ring is
    // well inside 40 px of it.
    const CX: u32 = 135;
    const CY: u32 = 180;
    const R: u32 = 40;
    let outside_the_anchor = |a: &[[u8; 4]], b: &[[u8; 4]]| {
        let mut count = 0;
        for y in 0..H {
            for x in 0..W {
                let near = x.abs_diff(CX) <= R && y.abs_diff(CY) <= R;
                if !near && a[(y * W + x) as usize] != b[(y * W + x) as usize] {
                    count += 1;
                }
            }
        }
        count
    };

    let pc = outside_the_anchor(&previewed, &committed);
    let pb = outside_the_anchor(&previewed, &bare);
    let cb = outside_the_anchor(&committed, &bare);
    eprintln!("previewed vs committed: {pc}, previewed vs bare: {pb}, committed vs bare: {cb}");

    assert_eq!(
        pc, 0,
        "the previewed cable runs somewhere the committed one does not",
    );
    assert!(
        pb > 200,
        "the preview still draws the straight bare cable: the snapped wrap is missing",
    );

    // Putting the cable back on the anchor it was just pulled off must preview
    // exactly like any other snap, whether or not the host has applied the
    // re-attachment yet. Once it has, the drag's record of what it detached
    // names the very anchor it is snapped to: read as an exclusion, that
    // suppresses the real wrap while the offered one is skipped as "already
    // routed", and the cable draws as if it had never been grabbed.
    for (label, frame) in [
        ("host pending", &regrabbed_pending),
        ("host applied", &regrabbed_applied),
    ] {
        let vs_committed = outside_the_anchor(frame, &committed);
        let vs_bare = outside_the_anchor(frame, &bare);
        eprintln!("re-grabbed ({label}): vs committed {vs_committed}, vs bare {vs_bare}");
        assert_eq!(
            vs_committed, 0,
            "re-attaching to the anchor it was pulled off ({label}) draws a \
             different cable than committing it does",
        );
        assert!(
            vs_bare > 200,
            "re-attaching to the anchor it was pulled off ({label}) draws the \
             straight bare cable: the wrap is missing",
        );
    }
}

/// The screen point the glow test hovers: 20 px along the cable from node 0's
/// output pin, which leaves the pin at (80, 45) heading right.
const GLOW_HOVER: Point = Point::new(95.7, 48.1);

/// Pixels differing between two frames inside the screen rect
/// `x0..=x1` by `y0..=y1`.
fn diff_in(a: &[[u8; 4]], b: &[[u8; 4]], x0: u32, y0: u32, x1: u32, y1: u32) -> usize {
    let mut count = 0;
    for y in y0..=y1 {
        for x in x0..=x1 {
            let i = (y * W + x) as usize;
            if a[i] != b[i] {
                count += 1;
            }
        }
    }
    count
}

/// Routing is a rendering contract, not bookkeeping. Four frames pin it:
/// pushing an anchor has to change the framebuffer (its core reaches the GPU
/// even with no cable on it), and naming that anchor in an edge's route has to
/// change it again - the cable leaves the direct curve to wrap the ring the
/// anchor now carries.
///
/// Comparing `Anchored` against `Wrapped` isolates the routing: the anchor is in
/// both, so every differing pixel belongs to the cable and its new ring. The
/// fourth frame holds a grab in flight - no release, nothing committed - and
/// must still move both the core and the cable hanging off it, because a drag
/// you cannot see is a drag that feels broken.
#[test]
fn full_widget_renders_routed_edge_and_anchor() {
    let Some(bare) = render_routed_edge(RouteScene::Bare) else {
        eprintln!("no GPU adapter - skipping full_widget_renders_routed_edge_and_anchor");
        return;
    };
    let anchored = render_routed_edge(RouteScene::Anchored).expect("GPU was available");
    let routed = render_routed_edge(RouteScene::Wrapped).expect("GPU was available");

    for (label, px) in [
        ("bare", &bare),
        ("anchored", &anchored),
        ("routed", &routed),
    ] {
        let distinct: std::collections::HashSet<[u8; 4]> = px.iter().copied().collect();
        assert!(
            distinct.len() > 3,
            "{label} frame is near-uniform ({} distinct colours): nothing rendered",
            distinct.len(),
        );
    }

    let diff = |a: &[[u8; 4]], b: &[[u8; 4]]| a.iter().zip(b).filter(|(x, y)| x != y).count();

    let anchor_px = diff(&bare, &anchored);
    assert!(
        anchor_px > 20,
        "pushing an anchor changed only {anchor_px} pixels: the core did not draw",
    );

    let cable_px = diff(&anchored, &routed);
    assert!(
        cable_px > 400,
        "routing the edge changed only {cable_px} pixels: the anchor did not bend the cable",
    );

    let dragging = render_routed_edge(RouteScene::Dragging).expect("GPU was available");
    let preview_px = diff(&routed, &dragging);
    assert!(
        preview_px > 400,
        "holding the anchor mid-drag changed only {preview_px} pixels: the preview is missing",
    );
}

/// Hovering a cable's grabbable end has to reach the framebuffer - the feedback
/// IS the pixels - and it has to mark THAT end only.
///
/// The glow is the stretch a press would take hold of, so a whole-cable
/// highlight is a different promise: the far end, whose own end zone a press
/// there would grab instead, must come out of the hover untouched.
#[test]
fn a_hovered_cable_end_glows() {
    let Some(idle) = render_routed_edge(RouteScene::Wrapped) else {
        eprintln!("no GPU adapter - skipping a_hovered_cable_end_glows");
        return;
    };
    let hovered = render_routed_edge(RouteScene::HoveringEnd).expect("GPU was available");

    // The hovered end zone runs from the output pin at (80, 45) to (98.3, 49.4);
    // this box holds it clear of node 0's body, which ends at x = 80.
    let near = diff_in(&idle, &hovered, 82, 38, 130, 58);
    // The end zone at the other end runs from (202.2, 49.1) to the input pin at
    // (220, 45); node 1's body starts at x = 220.
    let far = diff_in(&idle, &hovered, 195, 38, 218, 58);

    assert!(
        near > 40,
        "hovering the cable's output end changed only {near} pixels there: \
         the glow is missing",
    );
    assert_eq!(
        far, 0,
        "the far end changed too: the glow marks the whole cable instead of the \
         stretch the press would take",
    );
}

/// Renders the bare two-pin cable of `render_routed_edge` (a straight 140 px
/// run from screen (80, 45) to (220, 45)) carrying one magenta particle per
/// `(age_secs, speed)` pair. A negative age is a particle born in the future.
///
/// Also returns whether the frame after the draw asked for another one: the
/// widget drives its own redraws while a particle is moving or pending.
fn render_particles(particles: &[(f32, f32)]) -> Option<(Vec<[u8; 4]>, bool)> {
    use iced::time::{Duration, Instant};
    use iced::widget::container;
    use iced_nodegraph::{ParticleStyle, PinDirection, PinRef, PinSide, edge, node_pin, particle};

    let mut guard = shared()?;
    let renderer = &mut *guard;

    let mut graph: NodeGraph<'static, iced_nodegraph::Indexed, (), Theme, Renderer> =
        NodeGraph::default()
            .width(Length::Fixed(W as f32))
            .height(Length::Fixed(H as f32))
            .camera(Point::new(20.0, 20.0), 1.0)
            .on_connect(|_, _| ());

    let pin_body = || {
        container(text("p"))
            .width(Length::Fixed(60.0))
            .height(Length::Fixed(50.0))
    };
    graph = graph.push_node(node(
        0usize,
        Point::new(0.0, 0.0),
        Element::from(node_pin(PinSide::Right, 0usize, pin_body()).direction(PinDirection::Output)),
    ));
    graph = graph.push_node(node(
        1usize,
        Point::new(200.0, 0.0),
        Element::from(node_pin(PinSide::Left, 0usize, pin_body()).direction(PinDirection::Input)),
    ));

    let now = Instant::now();
    let born = |age: f32| {
        if age >= 0.0 {
            now - Duration::from_secs_f32(age)
        } else {
            now + Duration::from_secs_f32(-age)
        }
    };
    graph = graph.push_edge(
        edge((), PinRef::new(0usize, 0usize), PinRef::new(1usize, 0usize)).particles(
            particles.iter().map(|&(age, speed)| {
                particle(born(age), speed).style(|_| ParticleStyle {
                    color: Color::from_rgb(1.0, 0.0, 1.0),
                    radius: 5.0,
                })
            }),
        ),
    );

    let mut tree = Tree::new(&graph as &dyn Widget<(), Theme, Renderer>);
    let layout_node = graph.layout(
        &mut tree,
        &*renderer,
        &layout::Limits::new(Size::ZERO, Size::new(W as f32, H as f32)),
    );
    let layout = Layout::new(&layout_node);
    let viewport_rect = Rectangle::new(Point::ORIGIN, Size::new(W as f32, H as f32));

    let mut msgs: Vec<()> = Vec::new();
    let mut shell = iced_wgpu::core::Shell::new(&mut msgs);
    let mut clipboard = clipboard::Null;
    graph.update(
        &mut tree,
        &iced::Event::Mouse(mouse::Event::CursorMoved {
            position: Point::new(-1.0, -1.0),
        }),
        layout,
        mouse::Cursor::Unavailable,
        &*renderer,
        &mut clipboard,
        &mut shell,
        &viewport_rect,
    );

    graph.draw(
        &tree,
        renderer,
        &Theme::Dark,
        &renderer::Style {
            text_color: Color::WHITE,
        },
        layout,
        mouse::Cursor::Unavailable,
        &viewport_rect,
    );

    let bytes = renderer.screenshot(
        &Viewport::with_physical_size(Size::new(W, H), 1.0),
        Color::TRANSPARENT,
    );

    let mut msgs: Vec<()> = Vec::new();
    let mut shell = iced_wgpu::core::Shell::new(&mut msgs);
    graph.update(
        &mut tree,
        &iced::Event::Window(iced::window::Event::RedrawRequested(Instant::now())),
        layout,
        mouse::Cursor::Unavailable,
        &*renderer,
        &mut clipboard,
        &mut shell,
        &viewport_rect,
    );
    let redraws = shell.redraw_request() == iced::window::RedrawRequest::NextFrame;

    Some((bytes.as_chunks::<4>().0.to_vec(), redraws))
}

/// One second at 70 world units per second puts the particle 70 px along the
/// 140 px cable: at screen (150, 45), and nowhere else on the run. A moving
/// particle keeps the frame loop running without the host driving a clock.
#[test]
fn a_particle_rides_the_cable_by_speed_and_age() {
    let Some((px, redraws)) = render_particles(&[(1.0, 70.0)]) else {
        eprintln!("no GPU adapter; skipping");
        return;
    };
    assert!(redraws, "a moving particle did not request the next frame");
    let magenta = |p: [u8; 4]| p[0] > 150 && p[1] < 100 && p[2] > 150;
    let at = |x: u32, y: u32| px[(y * W + x) as usize];

    assert!(
        magenta(at(150, 45)),
        "no particle at the cable's midpoint: {:?}",
        at(150, 45)
    );
    assert!(
        !magenta(at(120, 45)),
        "particle drawn behind its position: {:?}",
        at(120, 45)
    );
    assert!(
        !magenta(at(180, 45)),
        "particle drawn ahead of its position: {:?}",
        at(180, 45)
    );
}

/// Three seconds at 70 per second is 210 px, past the 140 px cable: the
/// particle has arrived, is not drawn anywhere, and lets the frame loop stop.
#[test]
fn an_arrived_particle_is_not_drawn() {
    let Some((px, redraws)) = render_particles(&[(3.0, 70.0)]) else {
        eprintln!("no GPU adapter; skipping");
        return;
    };
    assert!(!redraws, "an arrived particle kept the frame loop running");
    let magenta = |p: [u8; 4]| p[0] > 150 && p[1] < 100 && p[2] > 150;
    assert!(
        !px.iter().copied().any(magenta),
        "an arrived particle is still drawn somewhere in the frame"
    );
}

/// A particle born one second from now has not started: nothing is drawn, but
/// the frame loop keeps running so it appears on time.
#[test]
fn an_unborn_particle_is_not_drawn() {
    let Some((px, redraws)) = render_particles(&[(-1.0, 70.0)]) else {
        eprintln!("no GPU adapter; skipping");
        return;
    };
    assert!(redraws, "a pending particle did not request the next frame");
    let magenta = |p: [u8; 4]| p[0] > 150 && p[1] < 100 && p[2] > 150;
    assert!(
        !px.iter().copied().any(magenta),
        "an unborn particle is drawn somewhere in the frame"
    );
}