lini 0.11.0

Pretty diagrams from plain text, with fine-grained control. Compiles to clean, themeable SVG.
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
//! The independent four-law checker (LINKING §The Four Laws) — judgeable on
//! the output alone: routed polylines, placed nodes, and the engine's report.
//!
//! No router knowledge: clearance and separation are segment-distance
//! arithmetic, contact is side arithmetic, and crossings are reconciled
//! against the report — every drawn crossing named, every named crossing
//! drawn. Anything found here is an engine bug (`Severity::Warning`); the
//! checker exists to catch it in CI, never to patch the routing.

use super::audit;
use super::bundle::link_clearance;
use super::rect::Rect;
use super::scene::SceneIndex;
use super::{Rule, Severity, Violation};
use crate::layout::ir::{PlacedNode, RoutedLink};
use crate::span::Span;
use std::collections::BTreeMap;

const EPS: f64 = 1e-6;

pub fn check(nodes: &[PlacedNode], links: &[RoutedLink], report: &[Violation]) -> Vec<Violation> {
    if links.is_empty() {
        return Vec::new();
    }
    let c = links
        .iter()
        .map(|w| link_clearance(&w.attrs))
        .fold(0.0_f64, f64::max);
    let index = SceneIndex::build(nodes);
    let mut out = Vec::new();
    contact(&index, links, c, &mut out);
    clearance(&index, links, c, &mut out);
    separation(&index, links, c, report, &mut out);
    self_crossing(links, &mut out);
    out
}

/// Law 3's blind spot made checkable: a link crossing **itself** is a
/// crossing the report cannot even name — always an engine bug, never
/// counted output.
fn self_crossing(links: &[RoutedLink], out: &mut Vec<Violation>) {
    for w in links {
        let segs: Vec<_> = w.path.windows(2).collect();
        for (i, sa) in segs.iter().enumerate() {
            for sb in segs.iter().skip(i + 1) {
                if let Some(at) = audit::cross(sa, sb) {
                    out.push(breach(
                        Rule::Crossing,
                        w,
                        format!("link crosses itself at {at:?}"),
                    ));
                }
            }
        }
    }
}

fn name(w: &RoutedLink) -> String {
    format!("{} -> {}", w.seg_from, w.seg_to)
}

fn breach(rule: Rule, w: &RoutedLink, detail: String) -> Violation {
    Violation {
        rule,
        severity: Severity::Warning,
        links: vec![name(w)],
        detail,
        span: w.decl_span,
    }
}

/// Distance between two axis-aligned boxes; segments degenerate to boxes.
fn box_dist(a: (f64, f64, f64, f64), b: (f64, f64, f64, f64)) -> f64 {
    let dx = (b.0 - a.2).max(a.0 - b.2).max(0.0);
    let dy = (b.1 - a.3).max(a.1 - b.3).max(0.0);
    (dx * dx + dy * dy).sqrt()
}

fn seg_box(s: &[(f64, f64)]) -> (f64, f64, f64, f64) {
    (
        s[0].0.min(s[1].0),
        s[0].1.min(s[1].1),
        s[0].0.max(s[1].0),
        s[0].1.max(s[1].1),
    )
}

fn rect_box(r: Rect) -> (f64, f64, f64, f64) {
    (r.x0, r.y0, r.x1, r.y1)
}

/// Law 2 — Contact: every end on a side, perpendicular, clear of corners;
/// ports sharing a side evenly spaced ≥ clearance apart, median on the side's
/// centre. A side past its capacity **compacts** (the compaction clause): a
/// sub-clearance pitch is excused only by genuine overflow — more ports
/// than the side holds at clearance — and must be uniform at exactly the
/// widest pitch the side allows. Orthogonality rides along: an oblique
/// segment voids every law.
fn contact(index: &SceneIndex, links: &[RoutedLink], c: f64, out: &mut Vec<Violation>) {
    let mut sides: BTreeMap<(String, u8), Vec<(f64, usize)>> = BTreeMap::new();
    for (wi, w) in links.iter().enumerate() {
        if w.path.len() < 2 {
            out.push(breach(Rule::Contact, w, "degenerate path".to_owned()));
            continue;
        }
        if let Some(s) = w
            .path
            .windows(2)
            .find(|s| s[0].0 != s[1].0 && s[0].1 != s[1].1)
        {
            out.push(breach(Rule::Contact, w, format!("diagonal segment {s:?}")));
        }
        let n = w.path.len();
        let ends = [
            (&w.seg_from, w.path[0], w.path[1]),
            (&w.seg_to, w.path[n - 1], w.path[n - 2]),
        ];
        for (path, port, inward) in ends {
            let Some(rect) = index.rect(path) else {
                out.push(breach(
                    Rule::Contact,
                    w,
                    format!("endpoint '{path}' has no placed body"),
                ));
                continue;
            };
            match landing(rect, port, inward, c) {
                Ok(side) => sides
                    .entry((path.clone(), side))
                    .or_default()
                    .push((ord_on(side, port), wi)),
                Err(why) => out.push(breach(
                    Rule::Contact,
                    w,
                    format!("{why} at {port:?} on '{path}'"),
                )),
            }
        }
    }

    for ((path, side), mut ports) in sides {
        ports.sort_by(|a, b| a.0.total_cmp(&b.0));
        let members: Vec<usize> = ports.iter().map(|p| p.1).collect();
        // Fan siblings share one port; identical ordinates are one slot.
        ports.dedup_by(|a, b| (a.0 - b.0).abs() <= EPS);
        let rect = index.rect(&path).expect("landed ends have a body");
        let centre = match side {
            0 | 2 => (rect.x0 + rect.x1) / 2.0,
            _ => (rect.y0 + rect.y1) / 2.0,
        };
        let mut flag = |wi: usize, detail: String| {
            out.push(breach(
                Rule::Contact,
                &links[wi],
                format!("{detail} on '{path}'"),
            ));
        };
        let gaps: Vec<f64> = ports.windows(2).map(|p| p[1].0 - p[0].0).collect();
        if ports.len() > side_capacity(rect, side, c) {
            let pitch = side_usable(rect, side, c) / (ports.len() as f64 - 1.0);
            if let Some(g) = gaps.iter().find(|g| (**g - pitch).abs() > EPS) {
                flag(
                    ports[0].1,
                    format!("compacted ports {g} apart, the side's even pitch is {pitch}"),
                );
            }
        } else if let Some(g) = gaps.iter().find(|g| **g < c - EPS) {
            flag(ports[0].1, format!("ports {g} apart, need ≥ {c}"));
        } else if gaps.windows(2).any(|g| (g[1] - g[0]).abs() > EPS) {
            flag(ports[0].1, "ports unevenly spaced".to_owned());
        }
        // Law 2's centred median binds sides holding two or more ports; a
        // lone port is free along its side (it aligns with its link).
        let median = (ports[0].0 + ports[ports.len() - 1].0) / 2.0;
        if ports.len() > 1
            && (median - centre).abs() > EPS
            && !slide_excused(links, &ports, &members, rect, side, centre - median, c)
        {
            flag(
                ports[0].1,
                format!("port median {median} off the side's centre {centre}"),
            );
        }
    }
}

/// Law 2's slide clause: a port group may sit off its side's centre only
/// when the centred rows are unavailable — some port, slid back by `shift`,
/// would come nearer than clearance to a link outside the group. Checkable
/// on the output alone.
fn slide_excused(
    links: &[RoutedLink],
    ports: &[(f64, usize)],
    members: &[usize],
    rect: Rect,
    side: u8,
    shift: f64,
    c: f64,
) -> bool {
    ports.iter().any(|&(o, _)| {
        let p = match side {
            0 => (o + shift, rect.y0),
            1 => (rect.x1, o + shift),
            2 => (o + shift, rect.y1),
            _ => (rect.x0, o + shift),
        };
        links.iter().enumerate().any(|(wj, w)| {
            !members.contains(&wj)
                && w.path
                    .windows(2)
                    .any(|s| box_dist((p.0, p.1, p.0, p.1), seg_box(s)) < c - EPS)
        })
    })
}

/// The port's ordinate along its side: x on horizontal sides, y on vertical.
fn ord_on(side: u8, port: (f64, f64)) -> f64 {
    match side {
        0 | 2 => port.0,
        _ => port.1,
    }
}

/// A side's extent along its own axis (0/2 horizontal → width).
fn side_extent(rect: Rect, side: u8) -> f64 {
    match side {
        0 | 2 => rect.w(),
        _ => rect.h(),
    }
}

fn side_usable(rect: Rect, side: u8, c: f64) -> f64 {
    (side_extent(rect, side) - 2.0 * c).max(0.0)
}

/// Law 2's side capacity: `floor((len − 2c)/c) + 1`, minimum 1.
fn side_capacity(rect: Rect, side: u8, c: f64) -> usize {
    let free = side_extent(rect, side) - 2.0 * c;
    if free < 0.0 {
        1
    } else {
        (free / c).floor() as usize + 1
    }
}

/// The compacted rows, established independently of the router from Law
/// 2's excuse: every `(node, side)` carrying more distinct ports than its
/// capacity — with the links that land there and the band its outermost
/// ports bound.
struct Row {
    vertical: bool,
    lo: f64,
    hi: f64,
    members: Vec<usize>,
}

fn compacted_rows(index: &SceneIndex, links: &[RoutedLink], c: f64) -> Vec<Row> {
    let mut rows: BTreeMap<(String, u8), Vec<(usize, f64)>> = BTreeMap::new();
    for (wi, w) in links.iter().enumerate() {
        if w.path.len() < 2 {
            continue;
        }
        let n = w.path.len();
        let ends = [
            (&w.seg_from, w.path[0], w.path[1]),
            (&w.seg_to, w.path[n - 1], w.path[n - 2]),
        ];
        for (path, port, inward) in ends {
            let Some(rect) = index.rect(path) else {
                continue;
            };
            let Ok(side) = landing(rect, port, inward, c) else {
                continue;
            };
            rows.entry((path.clone(), side))
                .or_default()
                .push((wi, ord_on(side, port)));
        }
    }
    let mut out = Vec::new();
    for ((path, side), ends) in rows {
        let rect = index.rect(&path).expect("landed ends have a body");
        let mut ords: Vec<f64> = ends.iter().map(|t| t.1).collect();
        ords.sort_by(f64::total_cmp);
        ords.dedup_by(|a, b| (*a - *b).abs() <= EPS);
        if ords.len() <= side_capacity(rect, side, c) {
            continue;
        }
        let mut members: Vec<usize> = ends.iter().map(|t| t.0).collect();
        members.sort_unstable();
        members.dedup();
        out.push(Row {
            vertical: side == 1 || side == 3,
            lo: ords[0],
            hi: *ords.last().unwrap(),
            members,
        });
    }
    out
}

/// Which side (0 top, 1 right, 2 bottom, 3 left) the port lands on — or why
/// the landing is illegal. Corner margin relaxes to half the side on sides
/// too short for full clearance (port capacity bottoms out at one).
fn landing(rect: Rect, port: (f64, f64), inward: (f64, f64), c: f64) -> Result<u8, String> {
    let (x, y) = port;
    let on_x = x > rect.x0 + EPS && x < rect.x1 - EPS;
    let on_y = y > rect.y0 + EPS && y < rect.y1 - EPS;
    let side = if (y - rect.y0).abs() <= EPS && on_x {
        0
    } else if (x - rect.x1).abs() <= EPS && on_y {
        1
    } else if (y - rect.y1).abs() <= EPS && on_x {
        2
    } else if (x - rect.x0).abs() <= EPS && on_y {
        3
    } else {
        return Err("end is not on a side".to_owned());
    };
    let (margin, len, perpendicular) = match side {
        0 | 2 => (
            (x - rect.x0).min(rect.x1 - x),
            rect.w(),
            (inward.0 - x).abs() <= EPS,
        ),
        _ => (
            (y - rect.y0).min(rect.y1 - y),
            rect.h(),
            (inward.1 - y).abs() <= EPS,
        ),
    };
    if margin < c.min(len / 2.0) - EPS {
        return Err(format!("end {margin} from a corner, needs ≥ {c}"));
    }
    if !perpendicular {
        return Err("oblique attachment".to_owned());
    }
    Ok(side)
}

/// Law 1 — Clearance from bodies: ≥ clearance from every solid rect, and
/// from the link's own endpoints on every segment but the adjoining stub.
/// A containment link runs inside its outer endpoint by design (LINKING
/// §Special shapes), so that body is skipped.
fn clearance(index: &SceneIndex, links: &[RoutedLink], c: f64, out: &mut Vec<Violation>) {
    for w in links {
        if w.path.len() < 2 {
            continue;
        }
        let segs = w.path.len() - 1;
        let solids = index.solid_rects_for([&w.seg_from, &w.seg_to]);
        'solids: for r in &solids {
            for s in w.path.windows(2) {
                let d = box_dist(seg_box(s), rect_box(*r));
                if d < c - EPS {
                    out.push(breach(
                        Rule::Clearance,
                        w,
                        format!("segment {s:?} is {d} from a body at {r:?}, needs ≥ {c}"),
                    ));
                    break 'solids;
                }
            }
        }
        let mut bodies = vec![w.seg_from.as_str()];
        if w.seg_to != w.seg_from {
            bodies.push(w.seg_to.as_str());
        }
        for body in bodies {
            let partner: &str = if body == w.seg_from {
                &w.seg_to
            } else {
                &w.seg_from
            };
            if SceneIndex::contains(body, partner) {
                continue;
            }
            let Some(rect) = index.rect(body) else {
                continue;
            };
            for (k, s) in w.path.windows(2).enumerate() {
                if (k == 0 && body == w.seg_from) || (k == segs - 1 && body == w.seg_to) {
                    continue;
                }
                let d = box_dist(seg_box(s), rect_box(rect));
                if d < c - EPS {
                    out.push(breach(
                        Rule::Clearance,
                        w,
                        format!("segment {s:?} is {d} from its own endpoint '{body}', needs ≥ {c}"),
                    ));
                    break;
                }
            }
        }
    }
}

/// Law 1 (link–link) and Law 3's audit promise: every segment pair of two
/// links keeps clearance, except the sanctioned contacts — transversal
/// crossings (reconciled against the report), fan-sibling trunks (drawn as
/// one line), and the port rows of compacted sides (Law 1's third
/// surrender): among one such side's links, the final approach legs and
/// each leg's feeding corner may under-clear one another.
fn separation(
    index: &SceneIndex,
    links: &[RoutedLink],
    c: f64,
    report: &[Violation],
    out: &mut Vec<Violation>,
) {
    let rows = compacted_rows(index, links, c);
    let mut drawn: BTreeMap<(String, String), Vec<(f64, f64)>> = BTreeMap::new();
    for i in 0..links.len() {
        for j in i + 1..links.len() {
            let (a, b) = (&links[i], &links[j]);
            let fan_pair = [a.fan_from, a.fan_to]
                .iter()
                .flatten()
                .any(|g| [b.fan_from, b.fan_to].contains(&Some(*g)));
            let bands: Vec<(bool, f64, f64)> = rows
                .iter()
                .filter(|r| r.members.contains(&i) && r.members.contains(&j))
                .map(|r| (r.vertical, r.lo, r.hi))
                .collect();
            let mut offence: Option<String> = None;
            for sa in a.path.windows(2) {
                for sb in b.path.windows(2) {
                    if let Some(at) = audit::cross(sa, sb) {
                        drawn.entry(pair_key(a, b)).or_default().push(at);
                        continue;
                    }
                    let d = box_dist(seg_box(sa), seg_box(sb));
                    if d >= c - EPS
                        || (fan_pair && trunk_contact(sa, sb, a, b, d))
                        || bands.iter().any(|&band| audit::band_contact(band, sa, sb))
                    {
                        continue;
                    }
                    offence.get_or_insert_with(|| {
                        format!("segments {sa:?} and {sb:?} are {d} apart, need ≥ {c}")
                    });
                }
            }
            if let Some(detail) = offence {
                out.push(Violation {
                    rule: Rule::Separation,
                    severity: Severity::Warning,
                    links: vec![name(a), name(b)],
                    detail,
                    span: b.decl_span,
                });
            }
        }
    }
    reconcile(links, &drawn, report, out);
}

/// Fan-sibling contact that is the shared trunk rather than a braid: an
/// outright overlap or touch, a segment that lies on the partner's polyline
/// (the trunk is one drawn line, so the partner extends it), or corner
/// adjacency where staggered branch points peel off the trunk — parallel
/// segments whose travel extents only touch. Siblings running alongside each
/// other past the split still breach.
fn trunk_contact(
    sa: &[(f64, f64)],
    sb: &[(f64, f64)],
    a: &RoutedLink,
    b: &RoutedLink,
    d: f64,
) -> bool {
    let on = |s: &[(f64, f64)], path: &[(f64, f64)]| path.windows(2).any(|t| lies_on(t, s));
    d <= EPS || on(sb, &a.path) || on(sa, &b.path) || break_out(sa, sb)
}

/// Whether `s` is collinear with and contained in `t`.
fn lies_on(t: &[(f64, f64)], s: &[(f64, f64)]) -> bool {
    let (tb, sb) = (seg_box(t), seg_box(s));
    let along_x = tb.1 == tb.3 && sb.1 == sb.3 && (tb.1 - sb.1).abs() <= EPS;
    let along_y = tb.0 == tb.2 && sb.0 == sb.2 && (tb.0 - sb.0).abs() <= EPS;
    (along_x || along_y)
        && sb.0 >= tb.0 - EPS
        && sb.1 >= tb.1 - EPS
        && sb.2 <= tb.2 + EPS
        && sb.3 <= tb.3 + EPS
}

/// Parallel segments whose extents along the travel axis at most touch —
/// branch runs leaving a shared trunk in opposite or staggered directions.
fn break_out(sa: &[(f64, f64)], sb: &[(f64, f64)]) -> bool {
    let (ab, bb) = (seg_box(sa), seg_box(sb));
    let (a_horizontal, b_horizontal) = (ab.1 == ab.3, bb.1 == bb.3);
    if a_horizontal != b_horizontal {
        return false;
    }
    if a_horizontal {
        ab.2.min(bb.2) - ab.0.max(bb.0) <= EPS
    } else {
        ab.3.min(bb.3) - ab.1.max(bb.1) <= EPS
    }
}

fn pair_key(a: &RoutedLink, b: &RoutedLink) -> (String, String) {
    let (x, y) = (name(a), name(b));
    if x <= y { (x, y) } else { (y, x) }
}

/// Law 3: "a crossing the report doesn't name is a bug" — and a named
/// crossing that is not drawn is the same bug mirrored.
fn reconcile(
    links: &[RoutedLink],
    drawn: &BTreeMap<(String, String), Vec<(f64, f64)>>,
    report: &[Violation],
    out: &mut Vec<Violation>,
) {
    let mut reported: BTreeMap<(String, String), (usize, Span)> = BTreeMap::new();
    for v in report.iter().filter(|v| v.rule == Rule::Crossing) {
        let [a, b] = v.links.as_slice() else {
            continue;
        };
        let key = if a <= b {
            (a.clone(), b.clone())
        } else {
            (b.clone(), a.clone())
        };
        reported.entry(key).or_insert((0, v.span)).0 += 1;
    }
    let span_of = |key: &(String, String)| {
        links
            .iter()
            .find(|w| name(w) == key.0 || name(w) == key.1)
            .map_or(Span::empty(), |w| w.decl_span)
    };
    for (key, points) in drawn {
        let named = reported.get(key).map_or(0, |(n, _)| *n);
        if points.len() > named {
            out.push(Violation {
                rule: Rule::Crossing,
                severity: Severity::Warning,
                links: vec![key.0.clone(), key.1.clone()],
                detail: format!(
                    "{} crossing(s) drawn but {named} named in the report (first at {:?})",
                    points.len(),
                    points[0],
                ),
                span: span_of(key),
            });
        }
    }
    for (key, (named, span)) in &reported {
        let count = drawn.get(key).map_or(0, Vec::len);
        if *named > count {
            out.push(Violation {
                rule: Rule::Crossing,
                severity: Severity::Warning,
                links: vec![key.0.clone(), key.1.clone()],
                detail: format!("{named} crossing(s) named in the report but {count} drawn"),
                span: *span,
            });
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::layout::ir::Bbox;
    use crate::resolve::{AttrMap, Markers, NodeKind, ResolvedValue};

    fn body(id: &str, cx: f64, cy: f64) -> PlacedNode {
        PlacedNode {
            id: Some(id.to_owned()),
            kind: NodeKind::Block,
            type_chain: Vec::new(),
            applied_styles: Vec::new(),
            label: None,
            attrs: AttrMap::default(),
            own_style: AttrMap::default(),
            markers: Markers::default(),
            cx,
            cy,
            bbox: Bbox::centered(40.0, 40.0),
            rotation: 0.0,
            children: Vec::new(),
            dividers: Vec::new(),
            span: Span::empty(),
        }
    }

    fn link(from: &str, to: &str, path: Vec<(f64, f64)>) -> RoutedLink {
        let mut attrs = AttrMap::default();
        attrs.insert("clearance", ResolvedValue::Number(8.0));
        RoutedLink {
            path,
            markers: Markers::default(),
            attrs,
            applied_styles: Vec::new(),
            texts: Vec::new(),
            data_from: from.to_owned(),
            data_to: to.to_owned(),
            seg_from: from.to_owned(),
            seg_to: to.to_owned(),
            decl_span: Span::empty(),
            fan_from: None,
            fan_to: None,
        }
    }

    fn rules(violations: &[Violation]) -> Vec<Rule> {
        violations.iter().map(|v| v.rule).collect()
    }

    /// a at origin, b to the right, both 40×40.
    fn pair() -> Vec<PlacedNode> {
        vec![body("a", 0.0, 0.0), body("b", 200.0, 0.0)]
    }

    #[test]
    fn a_clean_straight_link_is_silent() {
        let w = link("a", "b", vec![(20.0, 0.0), (180.0, 0.0)]);
        let out = check(&pair(), &[w], &[]);
        assert_eq!(out.len(), 0, "{out:?}");
    }

    #[test]
    fn clearance_fires_on_a_grazing_segment() {
        // The detour passes 4 over the blocking body — clearance is 8.
        let nodes = vec![
            body("a", 0.0, 0.0),
            body("b", 200.0, 0.0),
            body("wall", 100.0, 0.0),
        ];
        let w = link(
            "a",
            "b",
            vec![
                (20.0, 0.0),
                (50.0, 0.0),
                (50.0, -24.0),
                (150.0, -24.0),
                (150.0, 0.0),
                (180.0, 0.0),
            ],
        );
        let out = check(&nodes, &[w], &[]);
        assert!(rules(&out).contains(&Rule::Clearance), "{out:?}");
    }

    #[test]
    fn clearance_fires_inside_the_links_own_keepout() {
        // A middle segment sweeps back 4 over its own source body.
        let w = link(
            "a",
            "b",
            vec![
                (20.0, 0.0),
                (60.0, 0.0),
                (60.0, -24.0),
                (0.0, -24.0),
                (0.0, -60.0),
                (240.0, -60.0),
                (240.0, 0.0),
                (220.0, 0.0),
            ],
        );
        let out = check(&pair(), &[w], &[]);
        assert!(rules(&out).contains(&Rule::Clearance), "{out:?}");
    }

    #[test]
    fn contact_fires_on_corner_oblique_and_diagonal_landings() {
        let corner = link("a", "b", vec![(20.0, -20.0), (180.0, -20.0)]);
        let near_corner = link("a", "b", vec![(20.0, -15.0), (180.0, -15.0)]);
        let oblique = link(
            "a",
            "b",
            vec![(20.0, 0.0), (20.0, -40.0), (180.0, -40.0), (180.0, 0.0)],
        );
        let diagonal = link("a", "b", vec![(20.0, 0.0), (170.0, -10.0), (180.0, 0.0)]);
        for w in [corner, near_corner, oblique, diagonal] {
            let out = check(&pair(), &[w], &[]);
            assert!(rules(&out).contains(&Rule::Contact), "{out:?}");
        }
    }

    #[test]
    fn contact_fires_when_ports_cram_or_drift_off_centre() {
        let nodes = vec![
            body("a", 0.0, 0.0),
            body("b", 200.0, 0.0),
            body("c", 0.0, 100.0),
        ];
        // Two ports on b's left side 4 apart (need ≥ 8), median off centre.
        let w1 = link("a", "b", vec![(20.0, 0.0), (180.0, 0.0)]);
        let w2 = link(
            "c",
            "b",
            vec![(20.0, 100.0), (100.0, 100.0), (100.0, 4.0), (180.0, 4.0)],
        );
        let out = check(&nodes, &[w1, w2], &[]);
        assert!(
            out.iter()
                .any(|v| v.rule == Rule::Contact && v.detail.contains("ports")),
            "{out:?}"
        );
    }

    #[test]
    fn separation_fires_on_a_parallel_hug() {
        let nodes = vec![
            body("a", 0.0, 0.0),
            body("b", 200.0, 0.0),
            body("c", 0.0, 100.0),
            body("d", 200.0, 100.0),
        ];
        let w1 = link(
            "a",
            "b",
            vec![
                (20.0, 0.0),
                (60.0, 0.0),
                (60.0, 40.0),
                (160.0, 40.0),
                (160.0, 0.0),
                (180.0, 0.0),
            ],
        );
        let w2 = link(
            "c",
            "d",
            vec![
                (20.0, 100.0),
                (60.0, 100.0),
                (60.0, 44.0),
                (160.0, 44.0),
                (160.0, 100.0),
                (180.0, 100.0),
            ],
        );
        let out = check(&nodes, &[w1, w2], &[]);
        assert!(rules(&out).contains(&Rule::Separation), "{out:?}");
    }

    #[test]
    fn crossings_reconcile_against_the_report_both_ways() {
        let nodes = vec![
            body("a", 0.0, 0.0),
            body("b", 200.0, 0.0),
            body("c", 100.0, -100.0),
            body("d", 100.0, 100.0),
        ];
        let w1 = link("a", "b", vec![(20.0, 0.0), (180.0, 0.0)]);
        let w2 = link("c", "d", vec![(100.0, -80.0), (100.0, 80.0)]);
        let entry = |links: Vec<String>| Violation {
            rule: Rule::Crossing,
            severity: Severity::Info,
            links,
            detail: String::new(),
            span: Span::empty(),
        };

        // Drawn but unnamed: the checker flags the crossing.
        let out = check(&nodes, &[w1.clone(), w2.clone()], &[]);
        assert!(
            out.iter()
                .any(|v| v.rule == Rule::Crossing && v.severity == Severity::Warning),
            "{out:?}"
        );

        // Named exactly once: silent.
        let named = entry(vec!["a -> b".to_owned(), "c -> d".to_owned()]);
        let out = check(
            &nodes,
            &[w1.clone(), w2.clone()],
            std::slice::from_ref(&named),
        );
        assert_eq!(out.len(), 0, "{out:?}");

        // Named but not drawn: the phantom is flagged.
        let phantom = entry(vec!["a -> b".to_owned(), "x -> y".to_owned()]);
        let out = check(&nodes, &[w1, w2], &[named, phantom]);
        assert!(
            out.iter()
                .any(|v| v.detail.contains("named in the report but")),
            "{out:?}"
        );
    }

    #[test]
    fn a_link_crossing_itself_is_flagged() {
        // A hook past the port row whose final approach sweeps back through
        // the link's own run — the failed-inversion bubble shape.
        let w = link(
            "a",
            "b",
            vec![
                (20.0, 0.0),
                (60.0, 0.0),
                (60.0, 60.0),
                (230.0, 60.0),
                (230.0, -9.0),
                (239.0, -9.0),
                (239.0, 0.0),
                (180.0, 0.0),
            ],
        );
        let out = check(&pair(), &[w], &[]);
        assert!(
            out.iter().any(|v| v.detail.contains("crosses itself")),
            "{out:?}"
        );
    }

    #[test]
    fn fan_siblings_share_their_trunk_without_separation_noise() {
        let nodes = vec![
            body("a", 0.0, 0.0),
            body("b", 200.0, 0.0),
            body("c", 100.0, 160.0),
        ];
        let mut w1 = link("a", "b", vec![(20.0, 0.0), (180.0, 0.0)]);
        let mut w2 = link("a", "c", vec![(20.0, 0.0), (100.0, 0.0), (100.0, 140.0)]);
        // Untagged, the trunk overlap and split T-joint breach separation…
        let out = check(&nodes, &[w1.clone(), w2.clone()], &[]);
        assert!(rules(&out).contains(&Rule::Separation), "{out:?}");
        // …as fan siblings they are one drawn line.
        w1.fan_from = Some(0);
        w2.fan_from = Some(0);
        let out = check(&nodes, &[w1, w2], &[]);
        assert_eq!(out.len(), 0, "{out:?}");
    }
}