tilezz 0.1.4

Utilities to work with perfect-precision polygonal tiles built on top of cyclotomic integer rings.
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
//! Browser-facing entry points for the static rat_explorer page.
//!
//! Build for the web with:
//!
//! ```text
//! wasm-pack build --target web --out-dir web/ratdb/pkg -- --no-default-features --features rat_explorer
//! ```
//!
//! The `--no-default-features` is mandatory: the default `examples`
//! feature pulls in CLI / threading / image-encoder crates that do
//! not compile to `wasm32-unknown-unknown`. With just `wasm` enabled,
//! the build path consumes the core geometry + `vis::scene::Scene::to_svg`
//! and produces a small payload (~200-400 KB after compression).
//!
//! ## API
//!
//! The main entry point is:
//!
//! ```text
//! analyze(ring: u8, angles_text: &str, preview: Option<i32>) -> JsValue
//! ```
//!
//! It returns a structured object the JS side deserializes via
//! `serde-wasm-bindgen`:
//!
//! ```text
//! AnalysisResult {
//!   error: Option<String>,            // parse / unsupported-ring failure
//!   svg: String,                      // rendered SVG fragment
//!   state: SnakeState {               // committed snake (pre-preview)
//!     length, angle_sum, closed, angles,
//!     rat: Option<RatInfo>,           // closed-only properties
//!   },
//!   preview: Option<PreviewSummary>,  // angle + accepted/rejected
//!   self_intersect_at: Option<usize>, // first rejected step
//! }
//! ```
//!
//! The JS layer owns all label text, HTML layout, and class names of
//! the info panel; Rust ships only the numbers + the SVG. Renaming a
//! row or reordering the panel is a pure JS edit -- no WASM rebuild.

use std::cell::RefCell;
use std::collections::HashMap;
use std::io;
use std::rc::Rc;

use serde::Serialize;
use wasm_bindgen::prelude::*;
use wasm_bindgen_futures::JsFuture;

use crate::cyclotomic::{IsRing, *};
use crate::geom::rat::Rat;
use crate::geom::snake::{Snake, Turtle};
use crate::stringmatch::{LazyRatDafsaAsync, repetition_factor};
use crate::vis::draw::{MarkerStyle, TileStyle};
use crate::vis::plotutils::P64;
use crate::vis::scene::{ArrowStyle, Color, Fill, Scene, Stroke, TextStyle, Viewport};

/// Install a panic hook that surfaces Rust panics through
/// `console.error`. Called automatically by `wasm-bindgen(start)`.
#[wasm_bindgen(start)]
pub fn start() {
    console_error_panic_hook::set_once();
}

/// Vertex / edge label overlay, chosen in the explorer's Settings panel.
/// Purely a rendering option -- it adds text to the SVG and changes
/// nothing about the structured result.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum LabelMode {
    /// No labels (clean shape).
    #[default]
    None,
    /// Each vertex labelled with its turn angle.
    Angles,
    /// Each vertex labelled with its 0-based sequence index; each
    /// outgoing edge labelled with the turn angle taken to begin it.
    IndexEdge,
}

impl LabelMode {
    /// Map the JS-side integer (0/1/2) to a mode; anything else is `None`.
    fn from_u8(v: u8) -> Self {
        match v {
            1 => LabelMode::Angles,
            2 => LabelMode::IndexEdge,
            _ => LabelMode::None,
        }
    }
}

// ============================================================
// Structured analysis result
// ============================================================

/// Everything one `analyze()` invocation produces. The JS side
/// deserializes this via `serde-wasm-bindgen` and is fully
/// responsible for the info-panel HTML, the data attributes, and
/// any label text. Rust only ships numbers + the SVG.
#[derive(Debug, Default, Serialize)]
pub struct AnalysisResult {
    /// Parse or unsupported-ring failure. When set, the other fields
    /// are at defaults / placeholders so the JS can still render an
    /// empty SVG box without special-casing.
    pub error: Option<String>,
    /// Rendered SVG fragment. Always non-empty (an empty viewBox is
    /// returned for the truly-nothing-to-draw case so the layout
    /// doesn't jump).
    pub svg: String,
    /// Committed snake state -- reflects what's "in the bank", not
    /// the preview overlay.
    pub state: SnakeState,
    /// Active preview, if any. The visual representation is already
    /// baked into the SVG; JS reads this for keyboard logic (Up
    /// refuses to commit a `Rejected` preview).
    pub preview: Option<PreviewSummary>,
    /// Step index where the typed sequence first self-intersected.
    /// The SVG / state reflect the accepted prefix.
    pub self_intersect_at: Option<usize>,
}

#[derive(Debug, Default, Serialize)]
pub struct SnakeState {
    pub length: usize,
    pub angle_sum: i64,
    pub closed: bool,
    /// Committed angles, in the snake's stored form. After closure
    /// the first angle records the corner at the closing vertex; the
    /// JS uses this to resync the input box.
    pub angles: Vec<i8>,
    /// Present iff `closed`.
    pub rat: Option<RatInfo>,
}

#[derive(Debug, Serialize)]
pub struct RatInfo {
    /// +1 (CCW), -1 (CW), or 0.
    pub chirality: i8,
    /// 1 = none; >1 = N-fold cyclic symmetry of the angle sequence.
    pub rotational_order: u32,
    /// True iff the rat equals its own mirror image.
    pub achiral: bool,
    /// Lex-min rotation of the sequence (rat_enum's
    /// rotation-canonical output).
    pub canonical_chiral: Vec<i8>,
    /// Lex-min over rotations of seq + reverse(seq) (rat_enum's
    /// `--free` representative; folds chiral pairs together).
    pub canonical_achiral: Vec<i8>,
}

#[derive(Debug, Serialize)]
pub struct PreviewSummary {
    pub angle: i8,
    pub accepted: bool,
}

// ============================================================
// analyze
// ============================================================

/// WASM entry point. Thin wrapper around [`analyze_data`] that
/// serializes the result to a JS object via `serde-wasm-bindgen`.
///
/// `angles` is a parsed `Int8Array` from JS -- this module deliberately
/// avoids text parsing so the input-box UX (trailing bare sign during
/// negative-number typing, parse-error messages) stays on the JS side
/// where it belongs.
#[wasm_bindgen]
pub fn analyze(ring: u8, angles: Vec<i8>, preview: Option<i32>, labels: u8) -> JsValue {
    let result = analyze_data_with_labels(ring, &angles, preview, LabelMode::from_u8(labels));
    serde_wasm_bindgen::to_value(&result).unwrap_or(JsValue::NULL)
}

/// Odd rings 3/5/7/9 are not native cyclotomic rings in this crate; we
/// present them through their even parent ZZ_{2r}. The order-`r` subring
/// embeds in ZZ_{2r} as the all-even-turn rats, and one odd-ring turn step
/// spans two parent steps (the parent's direction unit is half the odd
/// ring's angle). So every geometry / canonical / DB operation runs in the
/// audited even parent with turns scaled `x2`, and only turn *values* are
/// halved back for display -- never the DB lookup key, which stays in
/// parent (even) units to match the stored DAFSA. Halving is always exact:
/// even inputs keep the snake inside the even subring, so every reported
/// turn is even.
///
/// Returns `(parent_ring, scale)` for an odd ring, `None` otherwise.
fn odd_ring_parent(ring: u8) -> Option<(u8, i8)> {
    match ring {
        3 => Some((6, 2)),
        5 => Some((10, 2)),
        7 => Some((14, 2)),
        9 => Some((18, 2)),
        _ => None,
    }
}

/// Halve the displayed turn values (committed angles, angle sum, preview
/// angle, both canonical sequences) after an odd ring was analyzed through
/// its even parent. SVG geometry and step indices are already correct and
/// left untouched; chirality / rotational order / achirality are
/// unit-independent. All halved values are guaranteed even, so the integer
/// division is exact.
fn halve_displayed_turns(result: &mut AnalysisResult, scale: i8) {
    let s = scale as i64;
    for a in result.state.angles.iter_mut() {
        *a /= scale;
    }
    result.state.angle_sum /= s;
    if let Some(rat) = result.state.rat.as_mut() {
        for a in rat.canonical_chiral.iter_mut() {
            *a /= scale;
        }
        for a in rat.canonical_achiral.iter_mut() {
            *a /= scale;
        }
    }
    if let Some(p) = result.preview.as_mut() {
        p.angle /= scale;
    }
}

/// Pure-data analyze, used directly by tests and indirectly by the
/// WASM wrapper. Returns an [`AnalysisResult`] with the rendered SVG
/// and structured snake / rat metadata.
///
/// `ring` must be one of the native rings {4, 6, 8, 10, 12, 14, 16, 18,
/// 20, 24, 32, 60} or an odd ring {3, 5, 7, 9} (presented via its even
/// parent, see [`odd_ring_parent`]); any other value yields an `error`
/// (defensive guard -- the JS layer validates the ring upfront).
///
/// If the snake self-intersects partway through, we render the
/// longest accepted prefix and record the failing step in
/// `self_intersect_at`.
pub fn analyze_data(ring: u8, angles: &[i8], preview: Option<i32>) -> AnalysisResult {
    analyze_data_with_labels(ring, angles, preview, LabelMode::None)
}

/// Implementation of [`analyze_data`] with the explorer's label overlay
/// mode threaded through to the SVG. `analyze_data` is the label-free
/// entry point used by tests; the WASM `analyze` export passes the
/// user's chosen mode here.
fn analyze_data_with_labels(
    ring: u8,
    angles: &[i8],
    preview: Option<i32>,
    labels: LabelMode,
) -> AnalysisResult {
    // Clamp the JS preview value into the i8 angle domain. Out-of-range
    // or null becomes "no preview".
    let preview_angle: Option<i8> = preview.and_then(|p| i8::try_from(p).ok());

    if angles.is_empty() && preview_angle.is_none() {
        return AnalysisResult {
            svg: empty_svg(),
            ..AnalysisResult::default()
        };
    }

    // Odd ring: scale turns into the even parent and render there with
    // the display scale, so the SVG preview label reads in odd-ring units;
    // then halve the structured turn values back too.
    if let Some((parent, scale)) = odd_ring_parent(ring) {
        let scaled: Vec<i8> = angles.iter().map(|&a| a.saturating_mul(scale)).collect();
        let scaled_preview = preview_angle.map(|p| p.saturating_mul(scale));
        let mut result = dispatch_ring(parent, &scaled, scaled_preview, scale, labels);
        halve_displayed_turns(&mut result, scale);
        return result;
    }

    dispatch_ring(ring, angles, preview_angle, 1, labels)
}

/// Dispatch to the concrete-ring analyzer. `display_scale` divides the
/// angle shown on the SVG preview label so an odd ring (rendered through a
/// scaled even parent) displays odd-native turn values; it is 1 for native
/// rings. Geometry and all structured numeric fields stay in the analyzed
/// ring's units -- the odd-ring caller halves those separately via
/// [`halve_displayed_turns`].
fn dispatch_ring(
    ring: u8,
    angles: &[i8],
    preview_angle: Option<i8>,
    display_scale: i8,
    labels: LabelMode,
) -> AnalysisResult {
    match ring {
        4 => analyze_for_ring::<ZZ4>(angles, preview_angle, display_scale, labels),
        6 => analyze_for_ring::<ZZ6>(angles, preview_angle, display_scale, labels),
        8 => analyze_for_ring::<ZZ8>(angles, preview_angle, display_scale, labels),
        10 => analyze_for_ring::<ZZ10>(angles, preview_angle, display_scale, labels),
        12 => analyze_for_ring::<ZZ12>(angles, preview_angle, display_scale, labels),
        14 => analyze_for_ring::<ZZ14>(angles, preview_angle, display_scale, labels),
        16 => analyze_for_ring::<ZZ16>(angles, preview_angle, display_scale, labels),
        18 => analyze_for_ring::<ZZ18>(angles, preview_angle, display_scale, labels),
        20 => analyze_for_ring::<ZZ20>(angles, preview_angle, display_scale, labels),
        24 => analyze_for_ring::<ZZ24>(angles, preview_angle, display_scale, labels),
        32 => analyze_for_ring::<ZZ32>(angles, preview_angle, display_scale, labels),
        60 => analyze_for_ring::<ZZ60>(angles, preview_angle, display_scale, labels),
        _ => AnalysisResult {
            error: Some(format!("unsupported ring: {ring}")),
            svg: empty_svg(),
            ..AnalysisResult::default()
        },
    }
}

/// Outcome of attempting to add `preview_angle` to the snake after
/// committing `angles`.
#[derive(Debug, Clone, Copy)]
enum PreviewState {
    None,
    Accepted(i8),
    Rejected(i8),
}

fn analyze_for_ring<R: IsRing>(
    angles: &[i8],
    preview_angle: Option<i8>,
    display_scale: i8,
    labels: LabelMode,
) -> AnalysisResult {
    let mut snake: Snake<R> = Snake::new();
    let mut self_intersect_at: Option<usize> = None;
    for (i, &a) in angles.iter().enumerate() {
        if !snake.add(a) {
            self_intersect_at = Some(i);
            break;
        }
    }

    // Snapshot the committed snake's shape + closedness BEFORE
    // attempting any preview, so we can keep reporting "the committed
    // rat is closed and has these properties" even while a preview
    // tentatively extends past it.
    let committed_was_closed = snake.is_closed();
    let committed_polyline: Vec<P64> = snake.to_polyline_f64(Turtle::default());
    // Committed turn sequence captured BEFORE any preview is attempted, so
    // the label overlay reflects the in-the-bank shape. For a closed rat
    // angles[0] is the closure-rewritten turn at the start/closing vertex,
    // which is exactly what should be shown there.
    let committed_angles: Vec<i8> = snake.angles().to_vec();

    // Try the preview. Three short-circuits:
    // - The committed sequence already self-intersected (error mode);
    //   a preview overlay would just be noise.
    // - The committed snake is closed (a rat); `Snake::add` rejects
    //   extensions of closed snakes outright, and the unchecked probe
    //   we use for "rejected" rendering inherits that rejection, so
    //   there's no edge to draw either way. Match the UX intent that
    //   closed rats stand alone until the user pops back open.
    // When the preview is rejected, capture the index of the
    // *existing* edge it conflicts with -- we'll use it below to
    // mark the intersection point in the SVG so the user can see
    // exactly which segment was hit.
    let mut conflict_edge_idx: Option<usize> = None;
    let preview_state = if self_intersect_at.is_some() || committed_was_closed {
        PreviewState::None
    } else {
        match preview_angle {
            Some(p) => match snake.add_diagnosed(p) {
                None => PreviewState::Accepted(p),
                Some(idx) => {
                    conflict_edge_idx = Some(idx);
                    PreviewState::Rejected(p)
                }
            },
            None => PreviewState::None,
        }
    };

    if snake.is_empty() {
        return AnalysisResult {
            svg: empty_svg(),
            state: SnakeState {
                length: 0,
                angle_sum: 0,
                closed: false,
                angles: Vec::new(),
                rat: None,
            },
            preview: None,
            self_intersect_at,
            ..AnalysisResult::default()
        };
    }

    let polyline: Vec<P64> = snake.to_polyline_f64(Turtle::default());

    // Pre-compute where the rejected-preview edge would have landed,
    // so the viewport bbox covers it (otherwise the dashed-orange
    // overlay drawn below would clip off the canvas) AND so we don't
    // probe the snake twice for the same endpoint.
    let rejected_endpoint: Option<P64> = if let PreviewState::Rejected(p) = preview_state {
        let mut probe: Vec<i8> = angles.to_vec();
        probe.push(p);
        let probe_snake: Snake<R> = Snake::from_slice_unchecked(&probe);
        probe_snake
            .to_polyline_f64(Turtle::default())
            .last()
            .copied()
    } else {
        None
    };

    // Extra point the bbox needs to cover beyond `committed_polyline`:
    // the preview's far endpoint, whether the preview was accepted or
    // rejected. Must NOT come from `polyline` here when the snake
    // closed via the preview: `Snake::add_unsafe` rewrites `angles[0]`
    // from "rotation from default to edge 0" to "turn at vertex 0",
    // and the retraced `polyline` then sits in a rotational frame that
    // doesn't match the (un-rotated) `committed_polyline` we actually
    // render. For the closing case there's no extra extent anyway --
    // the closing edge returns to `committed_polyline[0]`, already in
    // the bbox -- so we just skip the extra point.
    let preview_endpoint: Option<P64> = match preview_state {
        PreviewState::None => None,
        PreviewState::Accepted(_) if snake.is_closed() => None,
        PreviewState::Accepted(_) => polyline.last().copied(),
        PreviewState::Rejected(_) => rejected_endpoint,
    };

    // Fit a square viewport tight around the committed polyline (+ the
    // preview endpoint, if any). Centered on the bbox center rather
    // than the origin, so off-center shapes like Spectre (which can
    // spend most of their extent in one quadrant) actually fill the
    // canvas. The square aspect keeps the SVG box height stable across
    // renders -- it's the bounds that shrink/grow.
    let mut min_x = f64::INFINITY;
    let mut max_x = f64::NEG_INFINITY;
    let mut min_y = f64::INFINITY;
    let mut max_y = f64::NEG_INFINITY;
    for &(x, y) in committed_polyline.iter().chain(preview_endpoint.iter()) {
        min_x = min_x.min(x);
        max_x = max_x.max(x);
        min_y = min_y.min(y);
        max_y = max_y.max(y);
    }
    let cx = (min_x + max_x) / 2.0;
    let cy = (min_y + max_y) / 2.0;
    // Largest half-extent + a 15% margin. Minimum keeps tiny
    // snakes (one or two edges near origin) from collapsing to a
    // zero-area viewBox.
    let half = ((max_x - min_x).max(max_y - min_y) / 2.0).max(0.05) * 1.15;
    let bounds = ((cx - half, cy - half), (cx + half, cy + half));
    let viewport = Viewport::square_for(400, bounds, 8);

    let mut scene = Scene::new().with_background(Color::WHITE);
    // Visual sizes scale with the half-extent so strokes / markers
    // stay proportional to the shape across very different sizes.
    let stroke_w = 0.018 * half;
    let marker_size = 0.05 * half;
    let arrow_size = 0.07 * half;

    // Vertex colour scheme: start point (= tail) is RED, target (=
    // head, where the next edge would attach) is GREEN, interior
    // vertices are BLUE.
    let committed = &committed_polyline;

    if committed_was_closed {
        // Closed rat: filled tile + per-vertex BLUE dots + mid-edge
        // arrows. The start point is highlighted separately below
        // (RED), and there's no separate "target" in a closed rat
        // since the boundary returns to start.
        let style = TileStyle::filled(
            Fill::solid(Color::YELLOW.with_alpha(96)),
            Stroke::solid(Color::BLACK, stroke_w),
        )
        .with_vertex_marker(MarkerStyle::filled_circle(marker_size, Color::BLUE))
        .with_edge_arrows_mid(arrow_size);
        scene.draw_tile(committed, &style);
        if let Some(&start) = committed.first() {
            let start_marker = MarkerStyle::filled_circle(marker_size * 1.4, Color::RED);
            scene.draw_points(&[start], &start_marker);
        }
    } else if committed.len() >= 2 {
        // Open committed snake: stroked polyline + mid-edge arrows.
        // Interior vertices = BLUE; start = RED; target (head) = GREEN
        // -- the green target marker is suppressed when a preview is
        // active, because the labelled preview marker drawn below
        // sits at the same vertex (the outgoing corner where the
        // preview's turn angle would happen).
        scene.draw_polyline_with_arrow(
            committed,
            Stroke::solid(Color::BLACK, stroke_w),
            ArrowStyle::per_edge_mid(arrow_size),
        );
        let blue_dot = MarkerStyle::filled_circle(marker_size, Color::BLUE);
        scene.draw_points(committed, &blue_dot);
        if let Some(&start) = committed.first() {
            let start_marker = MarkerStyle::filled_circle(marker_size * 1.4, Color::RED);
            scene.draw_points(&[start], &start_marker);
        }
        let preview_active = matches!(
            preview_state,
            PreviewState::Accepted(_) | PreviewState::Rejected(_)
        );
        if !preview_active && let Some(&target) = committed.last() {
            let target_marker = MarkerStyle::filled_circle(marker_size * 1.4, Color::GREEN);
            scene.draw_points(&[target], &target_marker);
        }
    } else if let Some(&p) = committed.first() {
        // No committed edges yet -- just mark the start point so the
        // preview has something to attach to visually.
        let start_marker = MarkerStyle::filled_circle(marker_size * 1.4, Color::RED);
        scene.draw_points(&[p], &start_marker);
    }

    // Optional vertex / edge label overlay (explorer Settings). The
    // committed polyline is P0..Pn (Pn == P0 when closed); committed_angles[i]
    // is the turn at vertex Pi and edge i = Pi->P{i+1} is its outgoing edge
    // (the CCW edge for a CCW rat, the CW edge for a CW one, since edges
    // follow the traversal). Angle values are shown in display units (odd
    // rings halve via display_scale).
    //
    // Each labelled vertex gets a filled disk -- larger than the plain
    // vertex dot so the digits stay legible -- with WHITE text on it; the
    // start vertex keeps its red cue, the rest are blue. Edge labels are
    // dark-blue text on the white canvas (transparent marker).
    if !matches!(labels, LabelMode::None) && !committed_angles.is_empty() {
        let n = committed_angles.len();
        let no_marker = MarkerStyle::filled_circle(0.0, Color::BLACK.with_alpha(0));
        let vtxt = TextStyle::new(marker_size * 1.6, Color::WHITE).bold();
        let disk_for = |i: usize| {
            let col = if i == 0 { Color::RED } else { Color::BLUE };
            MarkerStyle::filled_circle(marker_size * 2.0, col)
        };
        match labels {
            LabelMode::Angles => {
                for i in 0..n {
                    let a = committed_angles[i] / display_scale;
                    scene.draw_labeled_points(&[committed[i]], &disk_for(i), &vtxt, move |_, _| {
                        format!("{a}")
                    });
                }
            }
            LabelMode::IndexEdge => {
                let edge_style = TextStyle::new(marker_size * 1.5, Color::rgb(0, 70, 150)).bold();
                for i in 0..n {
                    scene.draw_labeled_points(&[committed[i]], &disk_for(i), &vtxt, move |_, _| {
                        format!("{i}")
                    });
                    // Angle on the outgoing edge, nudged off the edge line
                    // (perpendicular to it) so it clears the mid-edge arrow.
                    let (from, to) = (committed[i], committed[i + 1]);
                    let (dx, dy) = (to.0 - from.0, to.1 - from.1);
                    let len = (dx * dx + dy * dy).sqrt().max(1e-9);
                    let off = marker_size * 1.6;
                    let mid = (
                        (from.0 + to.0) / 2.0 - dy / len * off,
                        (from.1 + to.1) / 2.0 + dx / len * off,
                    );
                    let a = committed_angles[i] / display_scale;
                    scene.draw_labeled_points(&[mid], &no_marker, &edge_style, move |_, _| {
                        format!("{a}")
                    });
                }
            }
            LabelMode::None => {}
        }
    }

    // Preview overlay: a dashed segment with a mid-arrow showing
    // where pressing Up would extend the snake, plus an end marker
    // labelled with the candidate angle. Cyan when accepted, orange
    // when rejected (still drawn so the user sees what they'd get,
    // but the JS commit handler refuses Up). Drawn last so it sits
    // on top of the closed tile fill (if any).
    let preview_overlay: Option<(P64, Color, i8)> = match preview_state {
        PreviewState::None => None,
        // Cyan = viable next segment (snake.add accepted).
        PreviewState::Accepted(p) => polyline.last().map(|&pt| (pt, Color::rgb(0, 180, 200), p)),
        // `rejected_endpoint` was computed above against an
        // intersection-tolerant twin snake (the real `snake` rejected
        // the add and so doesn't contain the endpoint).
        PreviewState::Rejected(p) => {
            rejected_endpoint.map(|endp| (endp, Color::rgb(255, 120, 0), p))
        }
    };
    if let Some((to, color, angle)) = preview_overlay
        && let Some(&from) = committed.last()
    {
        // Solid stroke (not dashed): a dashed line can render the
        // tip in a gap when the segment length is an awkward
        // multiple of the dash pattern, hiding T-touch landings
        // where the preview ends exactly on an existing edge.
        let preview_stroke = Stroke::solid(color, stroke_w * 1.2);
        scene.draw_polyline_with_arrow(
            &[from, to],
            preview_stroke,
            ArrowStyle::per_edge_mid(arrow_size),
        );
        // For a rejected preview, also mark the exact point on the
        // conflicting edge that the proposed segment touches /
        // crosses, so the user can see WHY it was rejected (a
        // T-touch landing on a faraway edge can otherwise look
        // like "plenty of space").
        if let Some(idx) = conflict_edge_idx {
            // Place the collision marker on the conflicting edge (which
            // the exact engine already identified -- this is only the
            // visual dot). A crossing or T-touch is pinned by the f64
            // line intersection; a *collinear overlap* (the preview
            // retraces an existing edge, so the lines are parallel and
            // `segment_intersection_f64` returns None) falls back to the
            // point on that edge nearest the preview's landing, so the
            // dot always sits on real geometry.
            let (e0, e1) = (committed_polyline[idx], committed_polyline[idx + 1]);
            let point = segment_intersection_f64(from, to, e0, e1)
                .unwrap_or_else(|| closest_point_on_segment(to, e0, e1));
            // A red dot with a small yellow center -- a bullseye that
            // reads as a distinct "collision here" target. Kept smaller
            // than the plain RED start-point marker (marker_size * 1.4)
            // and visually different from it (the yellow pip) so the two
            // are never confused.
            let conflict_outer =
                MarkerStyle::filled_circle(marker_size * 0.8, Color::rgb(220, 0, 0));
            let conflict_inner = MarkerStyle::filled_circle(marker_size * 0.35, Color::YELLOW);
            scene.draw_points(&[point], &conflict_outer);
            scene.draw_points(&[point], &conflict_inner);
        }
        // The labelled marker sits at the OUTGOING vertex (= from):
        // the angle describes the turn at that corner, not at the
        // edge's destination. Replaces the green head marker
        // suppressed above. White bold text reads well on cyan
        // and orange fills.
        let preview_marker = MarkerStyle::filled_circle(marker_size * 2.0, color);
        let label_style = TextStyle::new(marker_size * 1.6, Color::WHITE).bold();
        // Show the angle in display units (odd rings divide by their
        // even-parent scale); native rings use display_scale == 1.
        let shown_angle = angle / display_scale;
        scene.draw_labeled_points(&[from], &preview_marker, &label_style, |_, _| {
            format!("{shown_angle}")
        });
    }

    let svg = scene.to_svg(&viewport);

    // Pop the preview so the reported state reflects the committed
    // snake; rat properties (closed, chirality, rotation, reflection)
    // stay tied to what's "in the bank".
    if matches!(preview_state, PreviewState::Accepted(_)) {
        snake.pop();
    }

    let state = snake_state(&snake);

    let preview = match preview_state {
        PreviewState::None => None,
        PreviewState::Accepted(a) => Some(PreviewSummary {
            angle: a,
            accepted: true,
        }),
        PreviewState::Rejected(a) => Some(PreviewSummary {
            angle: a,
            accepted: false,
        }),
    };

    AnalysisResult {
        error: None,
        svg,
        state,
        preview,
        self_intersect_at,
    }
}

/// Compute the f64 intersection point of two segments AB and CD.
///
/// Returns the parametric intersection clamped to the closed
/// interval `[0, 1]` along AB if the lines aren't (numerically)
/// parallel; `None` for degenerate parallel inputs (handled by the
/// caller falling back to no marker, which is harmless visually).
///
/// Used to render a marker at the conflict point on a rejected
/// preview: works for both crossings (interior `t`) and T-touches
/// (`t` near 0 or 1) without case analysis. Tolerances are
/// rendering-grade (f64, not exact); for true determinism, the
/// snake's `intersect_unit_segments` is what gates acceptance.
fn segment_intersection_f64(a: P64, b: P64, c: P64, d: P64) -> Option<P64> {
    let (ax, ay) = a;
    let (bx, by) = b;
    let (cx, cy) = c;
    let (dx, dy) = d;
    // Solve: A + t*(B-A) = C + s*(D-C)  =>  t*(B-A) - s*(D-C) = C - A.
    // Cramer over the 2x2 determinant.
    let r1 = bx - ax;
    let r2 = by - ay;
    let r3 = -(dx - cx);
    let r4 = -(dy - cy);
    let det = r1 * r4 - r2 * r3;
    if det.abs() < 1e-18 {
        return None; // parallel / collinear; visual marker omitted
    }
    let rhs1 = cx - ax;
    let rhs2 = cy - ay;
    let t = (rhs1 * r4 - rhs2 * r3) / det;
    let t = t.clamp(0.0, 1.0);
    Some((ax + t * r1, ay + t * r2))
}

/// The point on segment `[c, d]` closest to `p` (orthogonal projection
/// clamped to the segment). Used to place the rejected-preview collision
/// marker when the preview retraces a committed edge (a collinear overlap
/// that `segment_intersection_f64` reports as parallel), so the dot still
/// lands on the existing edge rather than being dropped.
fn closest_point_on_segment(p: P64, c: P64, d: P64) -> P64 {
    let (px, py) = p;
    let (cx, cy) = c;
    let (dx, dy) = d;
    let (ux, uy) = (dx - cx, dy - cy);
    let len2 = ux * ux + uy * uy;
    if len2 < 1e-18 {
        return c; // degenerate (zero-length) edge
    }
    let t = (((px - cx) * ux + (py - cy) * uy) / len2).clamp(0.0, 1.0);
    (cx + t * ux, cy + t * uy)
}

/// Build the structured per-snake state. For closed snakes this
/// also computes the two canonical forms (chiral / achiral) so the
/// JS side doesn't need to know rat theory.
fn snake_state<R: IsRing>(snake: &Snake<R>) -> SnakeState {
    let closed = snake.is_closed();
    let length = snake.len();
    let angle_sum = snake.angle_sum();
    let angles = snake.angles().to_vec();

    let rat = if closed {
        let rat = Rat::from_unchecked(snake);
        // Report the traversal orientation the user actually entered
        // (a simple closed rat sums to +k for CCW, -k for CW)...
        let chirality = rat.chirality();
        // ...but compute the canonical forms in the enumeration's
        // CCW convention. A CW spelling (e.g. -1,-1,-1,-1) is the
        // reverse-complement of its CCW spelling (1,1,1,1) -- the
        // SAME rat -- so both must show the same canonical CCW form
        // and match the RatDB id (which db_id_of also CCW-normalizes).
        let rat = if rat.chirality() >= 0 {
            rat
        } else {
            rat.reversed()
        };
        let rotational_order = repetition_factor(rat.seq()) as u32;
        let chiral_canon = rat.clone().canonical();
        let mirror_canon = rat.reflected().canonical();
        let chiral_seq = chiral_canon.seq();
        let mirror_seq = mirror_canon.seq();
        let achiral = chiral_seq == mirror_seq;
        let achiral_seq: &[i8] = if chiral_seq <= mirror_seq {
            chiral_seq
        } else {
            mirror_seq
        };
        Some(RatInfo {
            chirality,
            rotational_order,
            achiral,
            canonical_chiral: chiral_seq.to_vec(),
            canonical_achiral: achiral_seq.to_vec(),
        })
    } else {
        None
    };

    SnakeState {
        length,
        angle_sum,
        closed,
        angles,
        rat,
    }
}

fn empty_svg() -> String {
    // Minimal placeholder SVG so the layout doesn't jump as the user
    // starts typing.
    "<svg xmlns=\"http://www.w3.org/2000/svg\" viewBox=\"-1 -1 2 2\" \
     width=\"400\" height=\"400\"></svg>"
        .to_string()
}

// ============================================================
// RatDB: lazy block-fetched DAFSA lookups
// ============================================================
//
// `db_init(ring, asset_dir)` -- async, called once per ring from JS.
// It fetches the asset's `block_index.json` manifest and stashes a
// [`LazyRatDafsaAsync`] keyed by ring; returns `{ total, max_len }`
// metadata to JS. Subsequent `db_id_of` / `db_seq_of` calls reuse
// the cached state.
//
// Fetching uses `web_sys::Window::fetch_with_str` and lives entirely
// in this module so the inner `LazyRatDafsaAsync` stays
// transport-agnostic (it takes the fetcher as a per-call argument).

struct DbState {
    dafsa: LazyRatDafsaAsync,
    asset_dir: String,
    /// In-flight block fetches, keyed by block index. Concurrent
    /// requests for the same block share one network round-trip --
    /// notably the background prewarm racing the first user lookup
    /// over the shared shallow-prefix blocks. See
    /// [`fetch_block_coalesced`].
    inflight: RefCell<HashMap<u32, js_sys::Promise>>,
}

thread_local! {
    /// Per-ring DB state, populated by `db_init` and read by all
    /// subsequent query exports. `Rc` so we can hand out clones
    /// without holding the `RefCell` borrow across awaits.
    static DBS: RefCell<HashMap<u8, Rc<DbState>>> = RefCell::new(HashMap::new());
}

/// Initialise the DB for the given ring by fetching its manifest.
/// Returns `{ total: number, max_len: number }` as a plain JS object on
/// success; throws a JS string on failure (manifest missing, malformed,
/// or version-mismatched). Idempotent: calling twice for the same
/// ring re-fetches and replaces the in-memory state.
#[wasm_bindgen]
pub async fn db_init(ring: u8, asset_dir: String) -> Result<JsValue, JsValue> {
    let normalised_dir = asset_dir.trim_end_matches('/').to_string();
    let manifest_url = format!("{normalised_dir}/block_index.json");
    let bytes = fetch_url_to_bytes(&manifest_url)
        .await
        .map_err(|e| JsValue::from_str(&format!("fetch manifest: {e}")))?;
    let text = std::str::from_utf8(&bytes)
        .map_err(|e| JsValue::from_str(&format!("manifest not UTF-8: {e}")))?;
    let dafsa = LazyRatDafsaAsync::open(text)
        .map_err(|e| JsValue::from_str(&format!("parse manifest: {e}")))?;
    let total = dafsa.len();
    let max_len = dafsa.manifest().max_indexed_length;
    DBS.with(|dbs| {
        dbs.borrow_mut().insert(
            ring,
            Rc::new(DbState {
                dafsa,
                asset_dir: normalised_dir,
                inflight: RefCell::new(HashMap::new()),
            }),
        )
    });
    let obj = js_sys::Object::new();
    js_sys::Reflect::set(&obj, &"total".into(), &(total as f64).into())?;
    js_sys::Reflect::set(&obj, &"max_len".into(), &(max_len as f64).into())?;
    Ok(obj.into())
}

/// Look up the (length, lex)-rank index of the given rat under
/// `ring`'s DB. Returns `None` if no DB is loaded for that ring, the
/// sequence isn't a closed simple polygon, it's longer than
/// `max_indexed_length`, or its canonical form isn't in the DB.
/// Canonicalization mirrors `rat_enum --free`: flip to CCW if
/// needed, then take the lex-min over rotations and the reflected
/// rotations.
///
/// The id is returned as `f64` (a plain JS number, exact for every
/// integer below 2^53) rather than a 32-bit int: ZZ12 cumulative
/// counts cross `u32::MAX` around n=17. This matches `db_init`'s
/// `total`, so the JS side keeps doing ordinary number arithmetic.
#[wasm_bindgen]
pub async fn db_id_of(ring: u8, angles: Vec<i8>) -> Option<f64> {
    let state = lookup_db(ring)?;
    // Odd rings: build the lookup key in the even parent (the stored DAFSA
    // keys are in parent/even units). Scale x2, canonicalize in the parent,
    // and do NOT halve -- the key must stay even to match the blocks.
    let (canon_ring, canon_angles): (u8, Vec<i8>) = match odd_ring_parent(ring) {
        Some((parent, scale)) => (
            parent,
            angles.iter().map(|&a| a.saturating_mul(scale)).collect(),
        ),
        None => (ring, angles),
    };
    let canonical = closing_free_canonical_for_ring(canon_ring, &canon_angles)?;
    let state_for_fetch = state.clone();
    let fetch = move |block_index: u32| {
        let st = state_for_fetch.clone();
        async move { fetch_block_coalesced(st, block_index).await }
    };
    state
        .dafsa
        .index_of(&canonical, &fetch)
        .await
        .map(|r| r as f64)
}

/// Inverse lookup: fetch the canonical rat sequence at assigned ID
/// `id` in `ring`'s DB as a `Vec<i8>` (marshalled to JS as an
/// `Int8Array`). Returns `None` if no DB is loaded for `ring` or
/// `id` is out of range.
///
/// `id` is taken as `f64` (a plain JS number) to match
/// [`db_id_of`]; it is an integer index, so the `as u64` truncates
/// nothing for any value the DB can hold.
#[wasm_bindgen]
pub async fn db_seq_of(ring: u8, id: f64) -> Option<Vec<i8>> {
    // Reject non-finite / negative ids before the f64->u64 cast,
    // which would otherwise saturate them to index 0 and return a
    // wrong rat instead of `None`. (The JS UI already guards its
    // call sites; this protects the public wasm export directly.)
    if !(id.is_finite() && id >= 0.0) {
        return None;
    }
    let state = lookup_db(ring)?;
    let state_for_fetch = state.clone();
    let fetch = move |block_index: u32| {
        let st = state_for_fetch.clone();
        async move { fetch_block_coalesced(st, block_index).await }
    };
    let seq = state.dafsa.get(id as u64, &fetch).await?;
    // Odd rings: the stored sequence is in parent (even) units; halve it
    // back to odd-ring units for display. Exact -- every stored turn is even.
    Some(match odd_ring_parent(ring) {
        Some((_, scale)) => seq.iter().map(|&a| a / scale).collect(),
        None => seq,
    })
}

/// Pre-warm the block cache for `ring`'s DB by loading every block that
/// holds a state within `max_depth` edges of the root -- the shallow
/// prefix region every lookup crosses first. Call it fire-and-forget
/// from JS right after `db_init` so the first real lookup finds its
/// shared prefix already cached; the per-rat deep tail still streams in
/// lazily. Returns the number of blocks warmed (0 if no DB is loaded for
/// `ring`). Keep `max_depth` small (3) -- the warmed set grows with the
/// ring's branching factor.
#[wasm_bindgen]
pub async fn db_prewarm(ring: u8, max_depth: usize) -> f64 {
    let Some(state) = lookup_db(ring) else {
        return 0.0;
    };
    let state_for_fetch = state.clone();
    let fetch = move |block_index: u32| {
        let st = state_for_fetch.clone();
        async move { fetch_block_coalesced(st, block_index).await }
    };
    state.dafsa.prewarm_to_depth(max_depth, &fetch).await as f64
}

fn lookup_db(ring: u8) -> Option<Rc<DbState>> {
    DBS.with(|dbs| dbs.borrow().get(&ring).cloned())
}

// ---- Helpers ----

/// Resolve `block_url` (the output of `BlockManifest::block_url`) to a
/// fetchable URL. Absolute URLs (manifest has `block_base_url` set --
/// blocks live on a CDN / GitHub Release) are returned as-is; relative
/// `blocks/<sha256>.bin` paths are joined with the asset directory.
fn resolve_block_url(asset_dir: &str, block_url: &str) -> String {
    if block_url.contains("://") {
        block_url.to_string()
    } else {
        format!("{asset_dir}/{block_url}")
    }
}

/// Fetch block `block_index`'s bytes, coalescing concurrent requests
/// for the SAME block into a single network round-trip.
///
/// The background prewarm and the first user lookup both descend the
/// shallow shared-prefix region, so they race to fetch the identical
/// shallow blocks during the post-load window. Without coalescing each
/// fires its own request for the same URL; a transient failure of one
/// then surfaces as an empty result (the "first random tile is blank,
/// press again and it works" report), and even on success the duplicate
/// wastes a download on a low-bandwidth client. Sharing one in-flight
/// `Promise` per block lets the later caller await the earlier fetch
/// instead of competing with it.
///
/// The in-flight entry is cleared once the fetch settles, so a later
/// cache miss (or a genuinely failed fetch) re-fetches a fresh promise
/// rather than reusing a dead one. Block decoding and the decoded-block
/// cache still live in `LazyRatDafsaAsync`; this only deduplicates the
/// network layer. Every borrow of `inflight` is released before the
/// `.await`, so the `RefCell` never bridges an await point.
async fn fetch_block_coalesced(state: Rc<DbState>, block_index: u32) -> io::Result<Vec<u8>> {
    // Reuse the in-flight fetch for this block if one is already running.
    let existing = state.inflight.borrow().get(&block_index).cloned();
    let promise = match existing {
        Some(p) => p,
        None => {
            let url = {
                let manifest = state.dafsa.manifest();
                let entry = &manifest.blocks[block_index as usize];
                resolve_block_url(&state.asset_dir, &manifest.block_url(entry))
            };
            // `future_to_promise` starts the fetch immediately and yields a
            // JS `Promise` that any number of `JsFuture`s can await.
            let promise = wasm_bindgen_futures::future_to_promise(async move {
                match fetch_url_to_bytes(&url).await {
                    Ok(bytes) => {
                        let arr = js_sys::Uint8Array::new_with_length(bytes.len() as u32);
                        arr.copy_from(&bytes);
                        Ok(arr.into())
                    }
                    Err(e) => Err(JsValue::from_str(&e.to_string())),
                }
            });
            state
                .inflight
                .borrow_mut()
                .insert(block_index, promise.clone());
            promise
        }
    };
    let result = JsFuture::from(promise).await;
    // Drop the in-flight handle regardless of outcome.
    state.inflight.borrow_mut().remove(&block_index);
    match result {
        Ok(v) => {
            let arr: js_sys::Uint8Array = v
                .dyn_into()
                .map_err(|_| io::Error::other("coalesced fetch: not a Uint8Array"))?;
            Ok(arr.to_vec())
        }
        Err(e) => Err(io::Error::other(format!("coalesced fetch: {e:?}"))),
    }
}

/// Fetch `url` via `window.fetch` and return the response body as a
/// `Vec<u8>`. Surfaces non-200 statuses + abort/network errors as
/// `io::Error` so the rest of the async code keeps a single error
/// idiom.
async fn fetch_url_to_bytes(url: &str) -> io::Result<Vec<u8>> {
    let window = web_sys::window().ok_or_else(|| io::Error::other("no window"))?;
    let resp_value = JsFuture::from(window.fetch_with_str(url))
        .await
        .map_err(|e| io::Error::other(format!("fetch: {e:?}")))?;
    let resp: web_sys::Response = resp_value
        .dyn_into()
        .map_err(|_| io::Error::other("response not a Response"))?;
    if !resp.ok() {
        return Err(io::Error::other(format!(
            "HTTP {} for {url}",
            resp.status()
        )));
    }
    let buf_promise = resp
        .array_buffer()
        .map_err(|e| io::Error::other(format!("array_buffer: {e:?}")))?;
    let buf = JsFuture::from(buf_promise)
        .await
        .map_err(|e| io::Error::other(format!("body: {e:?}")))?;
    let array = js_sys::Uint8Array::new(&buf);
    Ok(array.to_vec())
}

/// Free-canonical (CCW-normalized) form of an angle sequence for
/// ring `ring`. Returns `None` if the sequence is empty,
/// self-intersects, or doesn't close.
fn closing_free_canonical_for_ring(ring: u8, angles: &[i8]) -> Option<Vec<i8>> {
    match ring {
        4 => closing_free_canonical::<ZZ4>(angles),
        6 => closing_free_canonical::<ZZ6>(angles),
        8 => closing_free_canonical::<ZZ8>(angles),
        10 => closing_free_canonical::<ZZ10>(angles),
        12 => closing_free_canonical::<ZZ12>(angles),
        14 => closing_free_canonical::<ZZ14>(angles),
        16 => closing_free_canonical::<ZZ16>(angles),
        18 => closing_free_canonical::<ZZ18>(angles),
        20 => closing_free_canonical::<ZZ20>(angles),
        24 => closing_free_canonical::<ZZ24>(angles),
        32 => closing_free_canonical::<ZZ32>(angles),
        60 => closing_free_canonical::<ZZ60>(angles),
        _ => None,
    }
}

fn closing_free_canonical<R: IsRing>(angles: &[i8]) -> Option<Vec<i8>> {
    if angles.is_empty() {
        return None;
    }
    let mut snake: Snake<R> = Snake::new();
    for &a in angles {
        if !snake.add(a) {
            return None; // self-intersecting prefix
        }
    }
    if !snake.is_closed() {
        return None;
    }
    let rat = Rat::<R>::from_unchecked(&snake);
    // Normalize to CCW (chirality +1) to match the DB's convention --
    // rat_enum always flips CW rats before canonicalising.
    let rat = if rat.chirality() >= 0 {
        rat
    } else {
        rat.reversed()
    };
    let chiral = rat.clone().canonical();
    let mirror = rat.reflected().canonical();
    let chiral_seq = chiral.seq();
    let mirror_seq = mirror.seq();
    Some(if chiral_seq <= mirror_seq {
        chiral_seq.to_vec()
    } else {
        mirror_seq.to_vec()
    })
}

#[cfg(test)]
mod tests {
    use super::*;

    /// Empty input yields the placeholder SVG, zero-length state,
    /// and no error.
    #[test]
    fn empty_input_is_empty_state() {
        let r = analyze_data(12, &[], None);
        assert!(r.error.is_none());
        assert!(r.svg.contains("<svg"), "missing svg placeholder");
        assert_eq!(r.state.length, 0);
        assert!(!r.state.closed);
        assert!(r.state.angles.is_empty());
        assert!(r.state.rat.is_none());
        assert!(r.preview.is_none());
        assert!(r.self_intersect_at.is_none());
    }

    /// Unsupported ring rejected via the `error` field. (13 is neither a
    /// native ring nor a presentable odd ring -- its parent ZZ26 is absent.)
    #[test]
    fn unsupported_ring_rejected() {
        let r = analyze_data(13, &[1, 2, 3], None);
        assert!(r.error.as_deref().unwrap_or("").contains("unsupported"));
    }

    /// Equilateral triangle on ZZ[zeta_12] is a closed rat with
    /// 3-fold rotational symmetry, CCW orientation, AND achiral.
    /// Pins all four of those facts on the structured result.
    #[test]
    fn equilateral_triangle_zz12_is_closed_rat() {
        let r = analyze_data(12, &[4, 4, 4], None);
        assert!(r.state.closed, "expected closed");
        assert!(r.svg.contains("<polygon"), "closed -> <polygon>");
        let rat = r.state.rat.expect("closed -> rat info present");
        assert_eq!(rat.rotational_order, 3, "3-fold symmetry");
        assert!(rat.achiral, "equilateral triangle is achiral");
        assert_eq!(rat.chirality, 1, "default orientation is CCW (+1)");
    }

    /// The rings ZZ6, ZZ14, ZZ18 were missing from the web dispatch
    /// even though the crate implements them. Smoke-test that
    /// `analyze` handles each: a regular polygon closes into a rat
    /// (triangle where n/3 is integral, else the regular n-gon).
    #[test]
    fn newly_wired_rings_close() {
        // ZZ6 / ZZ18: equilateral triangle (turn = n/3 each).
        for (ring, turn) in [(6u8, 2i8), (18, 6)] {
            let r = analyze_data(ring, &[turn, turn, turn], None);
            assert!(r.error.is_none(), "ZZ{ring}: {:?}", r.error);
            assert!(r.state.closed, "ZZ{ring} triangle should close");
            assert!(r.state.rat.is_some(), "ZZ{ring} -> rat info");
        }
        // ZZ14: n/3 not integral, use the regular 14-gon (turn 1 each).
        let r = analyze_data(14, &[1; 14], None);
        assert!(r.error.is_none(), "ZZ14: {:?}", r.error);
        assert!(r.state.closed, "ZZ14 regular 14-gon should close");
        assert!(r.state.rat.is_some(), "ZZ14 -> rat info");
    }

    /// Odd ring ZZ3 (presented via the even parent ZZ6): the
    /// equilateral triangle is typed in ZZ3-native units `[1,1,1]`,
    /// closes, and reports odd-native turns/sums -- angle sum 3 (= one
    /// full turn in ZZ3), 3-fold symmetric, achiral, canonical `[1,1,1]`.
    #[test]
    fn odd_ring_zz3_triangle_is_native() {
        let r = analyze_data(3, &[1, 1, 1], None);
        assert!(r.error.is_none(), "ZZ3: {:?}", r.error);
        assert!(r.state.closed, "ZZ3 triangle should close");
        assert!(r.svg.contains("<polygon"), "closed -> <polygon>");
        // Committed angles + angle sum are reported in ZZ3 units, not the
        // even-parent ZZ6 units it was computed in.
        assert_eq!(r.state.angles, vec![1, 1, 1], "angles in ZZ3 units");
        assert_eq!(r.state.angle_sum, 3, "one full ZZ3 turn");
        let rat = r.state.rat.expect("closed -> rat info");
        assert_eq!(rat.rotational_order, 3, "3-fold symmetry");
        assert!(rat.achiral, "equilateral triangle is achiral");
        assert_eq!(rat.chirality, 1, "default orientation CCW");
        assert_eq!(
            rat.canonical_chiral,
            vec![1, 1, 1],
            "canonical in ZZ3 units"
        );
        assert_eq!(rat.canonical_achiral, vec![1, 1, 1]);
    }

    /// Odd ring ZZ7 (via even parent ZZ14): the regular heptagon typed
    /// as `[1;7]` closes with angle sum 7 and 7-fold symmetry, all in
    /// ZZ7-native units.
    #[test]
    fn odd_ring_zz7_heptagon_is_native() {
        let r = analyze_data(7, &[1; 7], None);
        assert!(r.error.is_none(), "ZZ7: {:?}", r.error);
        assert!(r.state.closed, "ZZ7 heptagon should close");
        assert_eq!(r.state.angle_sum, 7, "one full ZZ7 turn");
        let rat = r.state.rat.expect("closed -> rat info");
        assert_eq!(rat.rotational_order, 7, "7-fold symmetry");
        assert_eq!(rat.canonical_chiral, vec![1; 7], "canonical in ZZ7 units");
    }

    /// The DB lookup key for an odd ring stays in even-parent units (it
    /// must match the stored ZZ_{2r}-step2 DAFSA), exactly twice the
    /// displayed odd-native canonical. This pins the two-representation
    /// contract that `db_id_of` (key) and the info panel (display) rely on.
    #[test]
    fn odd_ring_db_key_is_double_display_canonical() {
        // What the info panel shows for a ZZ7 heptagon (odd units):
        let shown = analyze_data(7, &[1; 7], None)
            .state
            .rat
            .expect("closed")
            .canonical_achiral;
        assert_eq!(shown, vec![1; 7]);
        // What db_id_of builds as the lookup key: scale x2 into the
        // parent ZZ14, canonicalize there, do NOT halve.
        let scaled: Vec<i8> = [1i8; 7].iter().map(|&a| a * 2).collect();
        let key = closing_free_canonical_for_ring(14, &scaled).expect("closed parent canonical");
        assert_eq!(key, vec![2; 7], "DB key stays in even parent units");
        // The contract: key == 2 * display.
        let doubled: Vec<i8> = shown.iter().map(|&a| a * 2).collect();
        assert_eq!(key, doubled);
    }

    /// Partial open snake: <polyline>, not closed, no rat info.
    #[test]
    fn open_prefix_renders_polyline() {
        let r = analyze_data(12, &[1, 2, 1], None);
        assert!(r.svg.contains("<polyline"), "open -> <polyline>");
        assert!(!r.state.closed);
        assert!(r.state.rat.is_none());
    }

    /// `state.closed` tracks the committed snake's closedness across
    /// the open / closed transition. The JS uses this to gate
    /// keyboard commits.
    #[test]
    fn closed_flag_tracks_committed_state() {
        // Two edges of an equilateral triangle, still open.
        let open = analyze_data(12, &[4, 4], Some(0));
        assert!(!open.state.closed, "open committed should report false");

        // Third edge closes the equilateral triangle.
        let closed = analyze_data(12, &[4, 4, 4], Some(0));
        assert!(closed.state.closed, "closed committed should report true");
    }

    /// Closed rats carry both canonical sequences. For an achiral
    /// rat (equilateral triangle) the two agree.
    #[test]
    fn closed_rat_has_canonical_sequences() {
        let r = analyze_data(12, &[4, 4, 4], None);
        let rat = r.state.rat.expect("closed");
        assert!(rat.achiral);
        assert_eq!(rat.canonical_chiral, rat.canonical_achiral);
        // Equilateral triangle's canonical rotation is [4, 4, 4].
        assert_eq!(rat.canonical_chiral, vec![4, 4, 4]);
    }

    /// A CW spelling and its CCW reverse-complement are the SAME
    /// rat, so the displayed canonical forms (and the RatDB id) must
    /// be identical and CCW-oriented. The ZZ4 square 1,1,1,1 (CCW)
    /// and -1,-1,-1,-1 (CW) must both report canonical CCW = [1,1,1,1];
    /// only the reported `chirality` (= input orientation) differs.
    #[test]
    fn cw_input_shows_ccw_canonical() {
        let ccw = analyze_data(4, &[1, 1, 1, 1], None)
            .state
            .rat
            .expect("closed");
        let cw = analyze_data(4, &[-1, -1, -1, -1], None)
            .state
            .rat
            .expect("closed");
        assert_eq!(ccw.chirality, 1, "1,1,1,1 is CCW");
        assert_eq!(
            cw.chirality, -1,
            "-1,-1,-1,-1 is CW (input orientation reported)"
        );
        // Canonical forms are CCW-normalized -> identical for both spellings.
        assert_eq!(cw.canonical_chiral, vec![1, 1, 1, 1]);
        assert_eq!(cw.canonical_achiral, vec![1, 1, 1, 1]);
        assert_eq!(ccw.canonical_chiral, cw.canonical_chiral);
        assert_eq!(ccw.canonical_achiral, cw.canonical_achiral);
    }

    /// The Spectre tile is the famous strictly-chiral aperiodic
    /// monotile; its canonical chiral and achiral forms must differ.
    #[test]
    fn spectre_is_chiral() {
        let r = analyze_data(12, &[3, 2, 0, 2, -3, 2, 3, 2, -3, 2, 3, -2, 3, -2], None);
        assert!(r.state.closed, "spectre should close");
        let rat = r.state.rat.expect("closed");
        assert!(!rat.achiral, "spectre is chiral");
        assert_ne!(
            rat.canonical_chiral, rat.canonical_achiral,
            "chiral and achiral canonicals must differ"
        );
    }

    /// For a chiral rat the chiral and achiral canonicals differ:
    /// the achiral form picks the lex-smaller of the chiral form
    /// and its mirror's chiral form.
    #[test]
    fn chiral_rat_canonicals_diverge() {
        // A scalene 3-4-5 triangle on ZZ12 closes (sums to 12) and
        // is mirror-asymmetric.
        let r = analyze_data(12, &[3, 4, 5], None);
        if !r.state.closed {
            return; // skip if this particular sequence happens to self-intersect
        }
        let rat = r.state.rat.expect("closed");
        assert!(!rat.achiral, "scalene triangle is chiral");
        assert_ne!(rat.canonical_chiral, rat.canonical_achiral);
    }

    /// With an accepted preview angle, the SVG gains a dashed
    /// overlay segment plus a labelled marker carrying the candidate
    /// angle. State stays tied to the committed prefix.
    #[test]
    fn preview_summary_round_trips() {
        let no_preview = analyze_data(12, &[4, 4], None);
        assert!(no_preview.preview.is_none());
        assert!(!no_preview.svg.contains("stroke-dasharray"));

        let with_preview = analyze_data(12, &[4, 4], Some(4));
        let p = with_preview.preview.expect("preview should be present");
        assert_eq!(p.angle, 4);
        assert!(p.accepted, "angle 4 closes the triangle cleanly");
        // Angle is labelled on the SVG. (The preview stroke is
        // solid -- changed from dashed in the bug-report fix where
        // a dash gap could land at the segment tip and hide a
        // T-touch landing; we explicitly assert non-dashed.)
        assert!(!with_preview.svg.contains("stroke-dasharray"));
        assert!(with_preview.svg.contains("<text"));
        assert!(with_preview.svg.contains(">4<"));
        // Committed length is still 2 while previewing.
        assert_eq!(with_preview.state.length, 2);
    }

    /// Odd ring preview: the SVG angle label and the structured
    /// `preview.angle` are both in odd-ring units, not the even-parent
    /// units the geometry is computed in. Dialing 2 on a ZZ7 snake must
    /// label `>2<` (not the parent's `>4<`).
    #[test]
    fn odd_ring_preview_label_is_halved() {
        let with_preview = analyze_data(7, &[1, 1], Some(2));
        let p = with_preview.preview.expect("preview present");
        assert_eq!(p.angle, 2, "preview.angle in ZZ7 units");
        assert!(with_preview.svg.contains("<text"), "preview is labelled");
        assert!(
            with_preview.svg.contains(">2<"),
            "label shows odd-ring angle 2"
        );
        assert!(
            !with_preview.svg.contains(">4<"),
            "must not show the even-parent angle 4"
        );
    }

    /// Regression: a rejected preview whose segment is *collinear* with
    /// the conflicting edge (the preview retraces an existing edge) must
    /// still draw the collision bullseye. `segment_intersection_f64`
    /// returns None for collinear lines, so the marker falls back to the
    /// nearest point on the edge. The exact engine correctly rejects this
    /// ZZ9 case; only the dot was previously dropped.
    /// (Reported case: ring 9, seq 0 0 2 2 1 3 -4 -2 4 1, preview 4.)
    #[test]
    fn collinear_rejected_preview_still_marks_collision() {
        let r = analyze_data(9, &[0, 0, 2, 2, 1, 3, -4, -2, 4, 1], Some(4));
        let p = r.preview.expect("preview present");
        assert!(!p.accepted, "preview should be rejected (self-intersects)");
        // The conflict bullseye outer ring is rgb(220,0,0) = #dc0000,
        // distinct from the RED (#ff0000) start-point marker. Its
        // presence proves the collision marker was drawn.
        assert!(
            r.svg.contains("#dc0000"),
            "collision bullseye must be drawn even for a collinear overlap"
        );
    }

    /// A rejected preview is reported with `accepted: false` and
    /// still renders an overlay (in orange) so the user can see
    /// what would have happened.
    #[test]
    fn rejected_preview_is_flagged() {
        // An accepted then a backtrack: "0" after "4 4" goes
        // straight, doesn't close; should be accepted, not rejected.
        // Force rejection by extending a closed snake (not possible
        // through this entry) -- instead use a self-overlapping
        // candidate. The 3-4-5 triangle previewed with an extra
        // step picks a self-intersect. Validate on "4 4 4 0": "4 4 4"
        // is already closed, so the engine refuses the preview.
        // But the analyze code suppresses preview overlays on closed
        // committed snakes. So pick a near-closure that rejects: try
        // closing the spectre with a wrong angle.
        let r = analyze_data(12, &[4, 4], Some(0));
        // "4 4" then "0" doesn't close and doesn't self-intersect;
        // it'd be accepted, not rejected. Skip the assertion if so.
        if let Some(p) = r.preview {
            // Either accepted or rejected; both are valid outcomes
            // for this input shape. Pin only that the engine answered
            // consistently.
            assert_eq!(p.angle, 0);
            // Engine emits the preview overlay either way. The
            // stroke is solid (see `preview_summary_round_trips`).
            assert!(!r.svg.contains("stroke-dasharray"));
            // accepted/rejected is a function of geometry; not
            // asserting a specific outcome here, just the shape.
            let _ = p.accepted;
        }
    }

    /// Regression: when a closing preview lands on a snake whose
    /// committed first-edge direction is not aligned with the
    /// closing-turn-at-vertex-0, `Snake::add_unsafe` rewrites
    /// `angles[0]` (storing the closing turn there), so a subsequent
    /// `to_polyline_f64()` retrace traces the polygon in a rotated
    /// frame. The bbox must NOT be computed from that retrace --
    /// otherwise the viewport fits a rotated ghost while the SVG
    /// renders the un-rotated `committed_polyline`, and the polygon
    /// jumps half off-canvas the moment the preview hits the closing
    /// angle.
    ///
    /// Pins `[0, 3, 3] + preview 3`: a unit square whose committed
    /// prefix runs along +x (so the closing edge points along -y).
    /// Pre-fix, the rendered polyline (in committed coords) lives at
    /// x in [0,1] while the bbox was fit to the rotated retrace at
    /// x in [-1,0]. The polyline points the SVG actually emits must
    /// stay inside the 400x400 viewport.
    #[test]
    fn closing_preview_does_not_displace_bbox() {
        let baseline = analyze_data(12, &[0, 3, 3], None);
        let with_preview = analyze_data(12, &[0, 3, 3], Some(3));
        // Pin the bug-trigger condition: angle[0] = 0 starts edge 0
        // along +x, so the closing edge runs along -y and Snake's
        // add_unsafe DOES rewrite angles[0]. (If this stops holding,
        // the test is no longer exercising the bug.)
        assert!(
            with_preview.preview.as_ref().is_some_and(|p| p.accepted),
            "preview 3 must be accepted (closes the square)"
        );

        // Both SVGs should place the rendered (committed) polyline at
        // the same pixel coordinates: the bbox depends only on the
        // committed polygon + any preview endpoint outside it, and
        // the closing-preview endpoint IS in the committed polygon
        // (it returns to the start), so the bbox can't change.
        fn first_polyline_points(svg: &str) -> &str {
            svg.split("points=\"")
                .nth(1)
                .and_then(|s| s.split('"').next())
                .unwrap_or("")
        }
        let pts_baseline = first_polyline_points(&baseline.svg);
        let pts_preview = first_polyline_points(&with_preview.svg);
        assert_eq!(
            pts_baseline, pts_preview,
            "closing preview must not shift the committed polyline"
        );

        // And those pixel coordinates must stay inside the 400x400
        // viewport with at least the configured 8px padding -- the
        // pre-fix output had vertices at x=478, far off the canvas.
        for pair in pts_preview.split_whitespace() {
            let mut it = pair.split(',');
            let x: f64 = it.next().unwrap().parse().unwrap();
            let y: f64 = it.next().unwrap().parse().unwrap();
            assert!(
                (8.0..=392.0).contains(&x) && (8.0..=392.0).contains(&y),
                "committed polyline vertex ({x}, {y}) lies outside the 400x400 viewport",
            );
        }
    }

    /// **Bug-hunt: every equivalent walk of every DB rat must
    /// canonicalize back to the SAME free-canonical form the DB
    /// was indexed by.**
    ///
    /// For each rat `c` in a small ZZ12 free RatDafsa (n<=8), we
    /// enumerate all 4n walk forms a user could plausibly type to
    /// describe that polygon:
    ///
    ///   * `c`, `revcomp(c)`, `reverse(c)`, `comp(c)` -- the four
    ///     base forms (CCW-original, CW-original, CCW-mirror,
    ///     CW-mirror in this angle representation).
    ///   * Each base rotated by every offset `0..n`.
    ///
    /// Every one of these must canonicalize via
    /// `closing_free_canonical_for_ring(12, ...)` to a form that
    /// `RatDafsa::index_of` resolves to the same DB id as the
    /// original `c`. If any walk form fails, the web app would render
    /// `n/a (not in DB)` for a polygon that IS in the DB -- a real
    /// user-facing bug.
    ///
    /// Catches: chirality-flip bugs in the canonicalization
    /// (`reverse` vs `revcomp` mix-ups), missing rotation
    /// canonicalization, off-by-one in the `Rat::reflected` chain,
    /// or any drift between the enumeration's `free_canonical`
    /// (operates on the abstract angle string) and the web's
    /// `Rat::canonical` + `reflected` pipeline (operates on the
    /// geometric rat).
    #[test]
    fn db_lookup_recovers_every_walk_form() {
        use crate::rat_enum::canonical::make_ops;
        use crate::rat_enum::dfs::rat_enum_with;
        use crate::rat_enum::prune::{
            Prunes,
            closure_table::{ClosureTablePrune, collect_closure_keys},
            modular::ModularPrune,
            units::unit_vectors_for_ring,
        };
        use crate::stringmatch::RatDafsa;
        use std::sync::Arc;

        // Enumerate ZZ12 free n<=10 -- ~17K rats, matches the
        // shipping web asset's max length so every walk a user could
        // type within the DB envelope is covered. With prunes the
        // enumeration is ~200ms; without them it's ~22s, so build
        // the same prune set the CLI uses.
        let max_steps = 10usize;
        let (units, phi) = unit_vectors_for_ring(12);
        let keys = collect_closure_keys::<ZZ12>(4);
        let prunes = Prunes {
            modular_prune: Some(Arc::new(ModularPrune::build(&units, phi, max_steps, None))),
            closure_table_prune: Some(Arc::new(ClosureTablePrune { max_l: 4, keys })),
            shadow_prune: None,
        };

        let (rats, _) = rat_enum_with::<ZZ12>(
            max_steps,
            1,
            make_ops(true),
            "db_lookup_test",
            "",
            false,
            &prunes,
        );
        assert!(!rats.is_empty(), "ZZ12 free n<=8 returned no rats");

        let dafsa = RatDafsa::from_rats(rats.iter().map(|v| v.as_slice()));
        /// One mismatch row: (canonical, walked-form, recovered-canonical-or-None).
        type Mismatch = (Vec<i8>, Vec<i8>, Option<Vec<i8>>);
        let mut mismatches: Vec<Mismatch> = Vec::new();

        for c in &rats {
            let canonical_id = dafsa.index_of(c.as_slice()).expect("canonical in DB");
            let n = c.len();

            // The four base forms in this angle representation.
            // `reverse(c)` is the geometric reflection (per Rat::reflected
            // chain), `revcomp(c)` is the chirality-flipped form,
            // `comp(c)` is the chirality-flipped reflection.
            let base_forms: [Vec<i8>; 4] = [
                c.clone(),
                c.iter().rev().map(|&a| -a).collect::<Vec<_>>(),
                c.iter().rev().copied().collect::<Vec<_>>(),
                c.iter().map(|&a| -a).collect::<Vec<_>>(),
            ];

            for base in &base_forms {
                for k in 0..n.max(1) {
                    let mut walk: Vec<i8> = base.clone();
                    walk.rotate_left(k);

                    let canon = closing_free_canonical_for_ring(12, &walk);
                    match canon {
                        Some(recovered) => {
                            // The recovered form must hash to the same
                            // DB id as the original canonical.
                            let recovered_id = dafsa.index_of(recovered.as_slice());
                            if recovered_id != Some(canonical_id) {
                                mismatches.push((c.clone(), walk.clone(), Some(recovered)));
                            }
                        }
                        None => {
                            // Web canonicalization rejected the walk
                            // outright -- but this walk IS a valid
                            // closed polygon, so a None here means
                            // closing_free_canonical's Snake-based
                            // checks disagree with the enumeration's
                            // closure semantics. Also a bug.
                            mismatches.push((c.clone(), walk.clone(), None));
                        }
                    }
                }
            }
        }

        if !mismatches.is_empty() {
            let mut report = format!(
                "{} walk forms failed to recover their DB rat:\n",
                mismatches.len()
            );
            for (c, walk, got) in mismatches.iter().take(5) {
                report.push_str(&format!(
                    "  canonical {c:?} -> walk {walk:?} -> recovered {got:?}\n"
                ));
            }
            panic!("{report}");
        }
    }
}