tdsl-render 1.18.0

SVG, HTML, and PDF rendering for Timeline DSL IR
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
//! Timeline DSL renderer: `TimelineIr` → standalone HTML with inline SVG.
//!
//! The public entry point is [`render_html`]. Internally it:
//! 1. Computes a [`layout::LayoutModel`] from the IR.
//! 2. Serializes it to an SVG string.
//! 3. Wraps the SVG in an HTML document with embedded CSS (hover tooltips only, no JS).

pub mod html;
pub mod layout;
#[cfg(feature = "pdf")]
pub mod pdf;
#[cfg(feature = "png")]
pub mod png;
pub mod svg;

pub use layout::{GridStyle, LayoutModel, Orientation, RenderOptions, Theme};
#[cfg(feature = "pdf")]
pub use pdf::{PdfDate, PdfError, PdfOptions, PdfPageSize, render_pdf, svg_to_pdf};
#[cfg(feature = "png")]
pub use png::{PngError, PngOptions, render_png, svg_to_png};

use tdsl_core::ir::TimelineIr;

/// Render the given IR as a standalone HTML document string.
pub fn render_html(ir: &TimelineIr, opts: RenderOptions) -> Result<String, std::fmt::Error> {
    let layout = LayoutModel::compute(ir, opts.clone());
    let svg = svg::render_svg(&layout)?;
    let table_html = if opts.show_table {
        Some(html::generate_table_html(ir, &ir.lanes))
    } else {
        None
    };
    if opts.interactive {
        Ok(html::wrap_html_interactive(
            &svg,
            &ir.meta.title,
            &opts,
            &ir.lanes,
            table_html.as_deref(),
        ))
    } else {
        Ok(html::wrap_html(
            &svg,
            &ir.meta.title,
            &opts,
            table_html.as_deref(),
        ))
    }
}

/// Render the given IR as a standalone SVG string.
pub fn render_svg_only(ir: &TimelineIr, opts: RenderOptions) -> Result<String, std::fmt::Error> {
    let layout = LayoutModel::compute(ir, opts);
    svg::render_svg(&layout)
}

#[cfg(test)]
mod tests {
    use super::*;
    use tdsl_core::ir::{Item, Lane, Meta, TimelineIr};

    fn sample_ir() -> TimelineIr {
        TimelineIr {
            meta: Meta {
                title: "サンプル年表".into(),
                unit: "year".into(),
                range: (-300, 300),
                calendar: "proleptic_gregorian".into(),
                color_map: std::collections::HashMap::new(),
                ..Default::default()
            },
            lanes: vec![Lane {
                id: "han".into(),
                label: "".into(),
                kind: "dynasty".into(),
                order: 10,
                group: None,
                source_span: None,
            }],
            items: vec![Item::Span {
                id: "span:han".into(),
                lane: "han".into(),
                start: -206,
                end: 220,
                label: "".into(),
                tags: vec!["dynasty".into()],
                source: Some("wd:Q7209".into()),
                origin: None,
                start_month: None,
                start_day: None,
                end_month: None,
                end_day: None,
                source_span: None,
            }],
            imports: vec![],
            sources: vec![],
        }
    }

    #[test]
    fn render_html_produces_complete_document() {
        let ir = sample_ir();
        let html = render_html(&ir, RenderOptions::default()).unwrap();
        assert!(html.starts_with("<!DOCTYPE html>"));
        assert!(html.contains("<svg"));
        assert!(html.contains("</svg>"));
        assert!(html.contains("サンプル年表"));
        assert!(html.ends_with("</html>\n") || html.ends_with("</html>"));
    }

    // ─── Render integration tests ──────────────────────────────────────────

    #[test]
    fn render_html_contains_lane_label() {
        let ir = sample_ir();
        let html = render_html(&ir, RenderOptions::default()).unwrap();
        assert!(html.contains(""));
    }

    #[test]
    fn render_html_contains_span_label() {
        let ir = sample_ir();
        let html = render_html(&ir, RenderOptions::default()).unwrap();
        assert!(html.contains(""));
    }

    #[test]
    fn render_html_multiple_lanes_ordered_by_order_field() {
        let ir = TimelineIr {
            meta: Meta {
                title: "Multi-lane".into(),
                unit: "year".into(),
                range: (0, 500),
                calendar: "proleptic_gregorian".into(),
                color_map: std::collections::HashMap::new(),
                ..Default::default()
            },
            lanes: vec![
                Lane {
                    id: "b".into(),
                    label: "B".into(),
                    kind: "dynasty".into(),
                    order: 20,
                    group: None,
                    source_span: None,
                },
                Lane {
                    id: "a".into(),
                    label: "A".into(),
                    kind: "dynasty".into(),
                    order: 10,
                    group: None,
                    source_span: None,
                },
            ],
            items: vec![],
            imports: vec![],
            sources: vec![],
        };
        let html = render_html(&ir, RenderOptions::default()).unwrap();
        // Both lanes should appear in output
        let pos_a = html.find(">A<").or_else(|| html.find("A</"));
        let pos_b = html.find(">B<").or_else(|| html.find("B</"));
        assert!(pos_a.is_some() || html.contains("A"));
        assert!(pos_b.is_some() || html.contains("B"));
    }

    #[test]
    fn render_html_empty_ir_does_not_panic() {
        let ir = TimelineIr {
            meta: Meta {
                title: "Empty".into(),
                unit: "year".into(),
                range: (0, 100),
                calendar: "proleptic_gregorian".into(),
                color_map: std::collections::HashMap::new(),
                ..Default::default()
            },
            lanes: vec![],
            items: vec![],
            imports: vec![],
            sources: vec![],
        };
        let html = render_html(&ir, RenderOptions::default()).unwrap();
        assert!(html.contains("Empty"));
    }

    #[test]
    fn render_html_event_item_appears_in_output() {
        let ir = TimelineIr {
            meta: Meta {
                title: "Events".into(),
                unit: "year".into(),
                range: (0, 500),
                calendar: "proleptic_gregorian".into(),
                color_map: std::collections::HashMap::new(),
                ..Default::default()
            },
            lanes: vec![Lane {
                id: "politics".into(),
                label: "政治".into(),
                kind: "custom".into(),
                order: 1,
                group: None,
                source_span: None,
            }],
            items: vec![Item::Event {
                id: "event:politics:100".into(),
                lane: "politics".into(),
                time: 100,
                label: "即位".into(),
                tags: vec![],
                source: None,
                origin: None,
                time_month: None,
                time_day: None,
                source_span: None,
            }],
            imports: vec![],
            sources: vec![],
        };
        let html = render_html(&ir, RenderOptions::default()).unwrap();
        assert!(html.contains("即位"));
    }

    #[test]
    fn render_html_event_range_item_appears_in_output() {
        let ir = TimelineIr {
            meta: Meta {
                title: "Ranges".into(),
                unit: "year".into(),
                range: (0, 500),
                calendar: "proleptic_gregorian".into(),
                color_map: std::collections::HashMap::new(),
                ..Default::default()
            },
            lanes: vec![Lane {
                id: "war".into(),
                label: "戦争".into(),
                kind: "custom".into(),
                order: 1,
                group: None,
                source_span: None,
            }],
            items: vec![Item::EventRange {
                id: "event_range:war:100".into(),
                lane: "war".into(),
                start: 100,
                end: 200,
                label: "大乱".into(),
                tags: vec![],
                source: None,
                origin: None,
                start_month: None,
                start_day: None,
                end_month: None,
                end_day: None,
                source_span: None,
            }],
            imports: vec![],
            sources: vec![],
        };
        let html = render_html(&ir, RenderOptions::default()).unwrap();
        assert!(html.contains("大乱"));
    }

    #[test]
    fn render_html_custom_scale_changes_width() {
        let ir = sample_ir();
        let opts_narrow = RenderOptions {
            scale: 1.0,
            ..RenderOptions::default()
        };
        let opts_wide = RenderOptions {
            scale: 5.0,
            ..RenderOptions::default()
        };
        let narrow = render_html(&ir, opts_narrow).unwrap();
        let wide = render_html(&ir, opts_wide).unwrap();
        // wider scale → larger viewBox width
        assert_ne!(narrow, wide);
    }

    #[test]
    fn render_html_interactive_contains_script_tag() {
        let ir = sample_ir();
        let opts = RenderOptions {
            interactive: true,
            ..RenderOptions::default()
        };
        let html = render_html(&ir, opts).unwrap();
        assert!(
            html.contains("<script>"),
            "interactive mode must include <script>"
        );
        assert!(
            html.contains("tdsl-search"),
            "interactive mode must include search input"
        );
        assert!(
            html.contains("tdsl-legend"),
            "interactive mode must include legend"
        );
        assert!(
            html.contains("tdsl-detail"),
            "interactive mode must include detail panel"
        );
        assert!(
            html.contains("data-label="),
            "interactive mode must include data-label attributes on SVG items"
        );
    }

    // ─── render_svg_only golden tests ────────────────────────────────────

    #[test]
    fn render_svg_only_year_precision_basic_structure() {
        // 年精度のみのシンプルな IR で SVG の基本構造を検証
        let ir = TimelineIr {
            meta: Meta {
                title: "年精度テスト".into(),
                unit: "year".into(),
                range: (-300, 300),
                calendar: "proleptic_gregorian".into(),
                color_map: std::collections::HashMap::new(),
                ..Default::default()
            },
            lanes: vec![Lane {
                id: "han".into(),
                label: "".into(),
                kind: "dynasty".into(),
                order: 10,
                group: None,
                source_span: None,
            }],
            items: vec![
                Item::Span {
                    id: "span:han".into(),
                    lane: "han".into(),
                    start: -206,
                    end: 220,
                    label: "".into(),
                    tags: vec![],
                    source: None,
                    origin: None,
                    start_month: None,
                    start_day: None,
                    end_month: None,
                    end_day: None,
                    source_span: None,
                },
                Item::Event {
                    id: "event:1".into(),
                    lane: "han".into(),
                    time: 0,
                    label: "紀元".into(),
                    tags: vec![],
                    source: None,
                    origin: None,
                    time_month: None,
                    time_day: None,
                    source_span: None,
                },
            ],
            imports: vec![],
            sources: vec![],
        };
        let svg = render_svg_only(&ir, RenderOptions::default()).unwrap();
        // SVG の基本構造
        assert!(svg.starts_with("<svg"), "SVG should start with <svg");
        assert!(svg.contains("</svg>"), "SVG should end with </svg>");
        // span と event が含まれる
        assert!(svg.contains("tdsl-span"), "should contain span element");
        assert!(
            svg.contains("tdsl-event-dot"),
            "should contain event element"
        );
        // レーンラベルが含まれる
        assert!(svg.contains(""), "should contain lane label");
    }

    #[test]
    fn render_svg_only_month_day_mix_precision() {
        // 月日精度ミックス: start_month / time_month を持つアイテムが正常に SVG に出力されること
        let ir = TimelineIr {
            meta: Meta {
                title: "月日精度テスト".into(),
                unit: "year".into(),
                range: (1939, 1946),
                calendar: "proleptic_gregorian".into(),
                color_map: std::collections::HashMap::new(),
                ..Default::default()
            },
            lanes: vec![Lane {
                id: "ww2".into(),
                label: "WW2".into(),
                kind: "conflict".into(),
                order: 10,
                group: None,
                source_span: None,
            }],
            items: vec![
                Item::Span {
                    id: "span:ww2".into(),
                    lane: "ww2".into(),
                    start: 1939,
                    end: 1945,
                    label: "第二次世界大戦".into(),
                    tags: vec![],
                    source: None,
                    origin: None,
                    start_month: Some(9),
                    start_day: Some(1),
                    end_month: Some(9),
                    end_day: Some(2),
                    source_span: None,
                },
                Item::Event {
                    id: "event:normandy".into(),
                    lane: "ww2".into(),
                    time: 1944,
                    label: "ノルマンディー上陸".into(),
                    tags: vec![],
                    source: None,
                    origin: None,
                    time_month: Some(6),
                    time_day: Some(6),
                    source_span: None,
                },
            ],
            imports: vec![],
            sources: vec![],
        };
        let svg = render_svg_only(&ir, RenderOptions::default()).unwrap();
        assert!(svg.starts_with("<svg"), "SVG should start with <svg");
        assert!(svg.contains("tdsl-span"), "should contain span element");
        assert!(
            svg.contains("tdsl-event-dot"),
            "should contain event element"
        );
        // 月日精度がツールチップに反映される
        assert!(
            svg.contains("1939 Sep"),
            "month-precision start should appear in tooltip, got SVG length={}",
            svg.len()
        );
    }

    #[test]
    fn render_svg_has_tdsl_root_class_on_root_element() {
        let ir = sample_ir();
        let svg = render_svg_only(&ir, RenderOptions::default()).unwrap();
        assert!(
            svg.contains(r#"class="tdsl-root""#),
            "SVG root must have class=tdsl-root for CSS scoping"
        );
        assert!(
            svg.contains(".tdsl-root text"),
            "SVG style must scope font via .tdsl-root text"
        );
    }

    #[test]
    fn render_svg_custom_font_family_appears_in_style() {
        let ir = sample_ir();
        let opts = RenderOptions {
            font_family: Some("Arial, sans-serif".to_string()),
            ..RenderOptions::default()
        };
        let svg = render_svg_only(&ir, opts).unwrap();
        assert!(
            svg.contains("Arial, sans-serif"),
            "custom font_family must appear in SVG style"
        );
    }

    // ─── 垂直レイアウト テスト ──────────────────────────────────────────

    #[test]
    fn vertical_layout_dimensions_are_swapped() {
        // 垂直レイアウトでは time_span が高さ方向に反映され、
        // lanes が幅方向に積まれる (水平と total_width/total_height が入れ替わる)。
        let ir = TimelineIr {
            meta: Meta {
                title: "vert test".into(),
                unit: "year".into(),
                range: (0, 1000),
                calendar: "proleptic_gregorian".into(),
                color_map: std::collections::HashMap::new(),
                ..Default::default()
            },
            lanes: vec![
                Lane {
                    id: "a".into(),
                    label: "A".into(),
                    kind: "k".into(),
                    order: 1,
                    group: None,
                    source_span: None,
                },
                Lane {
                    id: "b".into(),
                    label: "B".into(),
                    kind: "k".into(),
                    order: 2,
                    group: None,
                    source_span: None,
                },
            ],
            items: vec![],
            imports: vec![],
            sources: vec![],
        };
        let opts_h = RenderOptions::default(); // horizontal
        let opts_v = RenderOptions {
            orientation: Orientation::Vertical,
            ..RenderOptions::default()
        };
        let layout_h = LayoutModel::compute(&ir, opts_h.clone());
        let layout_v = LayoutModel::compute(&ir, opts_v.clone());

        // 水平: 幅 = left_gutter + 1000*scale + right_margin
        //       高さ = top_margin + 2*lane_height + bottom_margin
        let expected_h_w = opts_h.left_gutter + 1000.0 * opts_h.scale + opts_h.right_margin;
        let expected_h_h = opts_h.top_margin + 2.0 * opts_h.lane_height + opts_h.bottom_margin;
        assert!(
            (layout_h.total_width - expected_h_w).abs() < 0.01,
            "horizontal width mismatch: {} vs {}",
            layout_h.total_width,
            expected_h_w
        );
        assert!(
            (layout_h.total_height - expected_h_h).abs() < 0.01,
            "horizontal height mismatch: {} vs {}",
            layout_h.total_height,
            expected_h_h
        );

        // 垂直: 幅 = left_gutter + 2*lane_height + right_margin
        //       高さ = top_margin + 1000*scale + bottom_margin
        let expected_v_w = opts_v.left_gutter + 2.0 * opts_v.lane_height + opts_v.right_margin;
        let expected_v_h = opts_v.top_margin + 1000.0 * opts_v.scale + opts_v.bottom_margin;
        assert!(
            (layout_v.total_width - expected_v_w).abs() < 0.01,
            "vertical width mismatch: {} vs {}",
            layout_v.total_width,
            expected_v_w
        );
        assert!(
            (layout_v.total_height - expected_v_h).abs() < 0.01,
            "vertical height mismatch: {} vs {}",
            layout_v.total_height,
            expected_v_h
        );

        // 垂直では水平と幅・高さが入れ替わっている(大小関係)
        assert!(
            layout_v.total_height > layout_v.total_width,
            "vertical: height should exceed width for a long time span with few lanes"
        );
    }

    #[test]
    fn vertical_svg_contains_expected_orientation_markers() {
        // 垂直 SVG が時間軸ティックを Y 方向に配置することを確認する:
        // - 水平軸のベースライン (y1=y2 の水平線) の代わりに垂直ベースライン (x1=x2) が含まれる
        // - レーンラベルが上部に配置される (y 値が top_margin 付近)
        let ir = sample_ir();
        let opts = RenderOptions {
            orientation: Orientation::Vertical,
            ..RenderOptions::default()
        };
        let svg = render_svg_only(&ir, opts.clone()).unwrap();
        // SVG の基本構造
        assert!(svg.starts_with("<svg"), "SVG should start with <svg");
        assert!(svg.contains("</svg>"), "SVG should end with </svg>");
        // 垂直ベースライン: x1=x2 (左端の縦線) が存在するはず
        assert!(
            svg.contains(r#"class="tdsl-axis-baseline""#),
            "vertical SVG must contain axis baseline element"
        );
        // span アイテムが含まれる
        assert!(svg.contains("tdsl-span"), "should contain span element");
    }

    #[test]
    fn vertical_svg_span_item_dimensions_are_vertical() {
        // 垂直レイアウトでは Span の height (時間軸方向) が width より大きくなる (長い span の場合)
        let ir = TimelineIr {
            meta: Meta {
                title: "v-span".into(),
                unit: "year".into(),
                range: (0, 500),
                calendar: "proleptic_gregorian".into(),
                color_map: std::collections::HashMap::new(),
                ..Default::default()
            },
            lanes: vec![Lane {
                id: "x".into(),
                label: "X".into(),
                kind: "k".into(),
                order: 1,
                group: None,
                source_span: None,
            }],
            items: vec![Item::Span {
                id: "s1".into(),
                lane: "x".into(),
                start: 100,
                end: 400,
                label: "long span".into(),
                tags: vec![],
                source: None,
                origin: None,
                start_month: None,
                start_day: None,
                end_month: None,
                end_day: None,
                source_span: None,
            }],
            imports: vec![],
            sources: vec![],
        };
        let opts = RenderOptions {
            orientation: Orientation::Vertical,
            scale: 2.0,
            ..RenderOptions::default()
        };
        let layout = LayoutModel::compute(&ir, opts);
        let span = layout.items.iter().find_map(|i| match i {
            crate::layout::LaidItem::Span { width, height, .. } => Some((*width, *height)),
            _ => None,
        });
        let (w, h) = span.expect("span should be laid out");
        // 垂直レイアウトでは height (時間方向) >> width (レーン幅方向)
        assert!(
            h > w,
            "vertical span height ({h}) should exceed width ({w}) for a multi-century span"
        );
    }

    #[test]
    fn render_html_non_interactive_unchanged_behavior() {
        let ir = sample_ir();
        let opts_default = RenderOptions::default();
        let opts_explicit = RenderOptions {
            interactive: false,
            ..RenderOptions::default()
        };
        let html_default = render_html(&ir, opts_default).unwrap();
        let html_explicit = render_html(&ir, opts_explicit).unwrap();
        // interactive:false (default) should produce identical output to explicit false
        assert_eq!(html_default, html_explicit);
        // should NOT include interactive-only elements
        assert!(
            !html_default.contains("tdsl-search"),
            "non-interactive mode must not include search input"
        );
        assert!(
            !html_default.contains("tdsl-legend"),
            "non-interactive mode must not include legend"
        );
    }

    // ─── group ヘッダー描画テスト ────────────────────────────────

    fn grouped_ir() -> TimelineIr {
        TimelineIr {
            meta: Meta {
                title: "グループテスト".into(),
                unit: "year".into(),
                range: (0, 100),
                calendar: "proleptic_gregorian".into(),
                color_map: std::collections::HashMap::new(),
                ..Default::default()
            },
            lanes: vec![
                Lane {
                    id: "a".into(),
                    label: "A".into(),
                    kind: "custom".into(),
                    order: 1,
                    group: Some("グループ1".into()),
                    source_span: None,
                },
                Lane {
                    id: "b".into(),
                    label: "B".into(),
                    kind: "custom".into(),
                    order: 2,
                    group: Some("グループ1".into()),
                    source_span: None,
                },
                Lane {
                    id: "c".into(),
                    label: "C".into(),
                    kind: "custom".into(),
                    order: 10,
                    group: None,
                    source_span: None,
                },
            ],
            items: vec![],
            imports: vec![],
            sources: vec![],
        }
    }

    #[test]
    fn render_svg_grouped_lanes_contains_group_label() {
        let ir = grouped_ir();
        let svg = render_svg_only(&ir, RenderOptions::default()).unwrap();
        assert!(
            svg.contains("グループ1"),
            "SVG must contain the group label 'グループ1'"
        );
        assert!(
            svg.contains("tdsl-group-label"),
            "SVG must contain the tdsl-group-label class"
        );
    }

    #[test]
    fn render_svg_no_group_label_when_no_groups() {
        let ir = sample_ir();
        let svg = render_svg_only(&ir, RenderOptions::default()).unwrap();
        assert!(
            !svg.contains("tdsl-group-label"),
            "SVG must not contain group labels when no lanes have groups"
        );
    }

    #[test]
    fn render_svg_group_separator_present() {
        let ir = grouped_ir();
        let svg = render_svg_only(&ir, RenderOptions::default()).unwrap();
        assert!(
            svg.contains("tdsl-group-separator"),
            "SVG must contain the tdsl-group-separator element"
        );
    }

    #[test]
    fn render_html_grouped_lanes_contains_group_label() {
        let ir = grouped_ir();
        let html = render_html(&ir, RenderOptions::default()).unwrap();
        assert!(
            html.contains("グループ1"),
            "HTML must contain the group label 'グループ1'"
        );
    }

    // ─── show_table tests ─────────────────────────────────────────────────

    #[test]
    fn render_html_show_table_false_no_table() {
        let ir = sample_ir();
        let opts = RenderOptions {
            show_table: false,
            ..RenderOptions::default()
        };
        let html = render_html(&ir, opts).unwrap();
        assert!(
            !html.contains("<div class=\"tdsl-table-wrap\">"),
            "show_table=false must not include the table-wrap div element"
        );
        assert!(
            !html.contains("<table class=\"tdsl-table\""),
            "show_table=false must not include table element"
        );
    }

    #[test]
    fn render_html_show_table_true_includes_table() {
        let ir = sample_ir();
        let opts = RenderOptions {
            show_table: true,
            ..RenderOptions::default()
        };
        let html = render_html(&ir, opts).unwrap();
        assert!(
            html.contains("<div class=\"tdsl-table-wrap\">"),
            "show_table=true must include the table-wrap div element"
        );
        assert!(
            html.contains("<table class=\"tdsl-table\""),
            "show_table=true must include table element"
        );
        // item label "漢" must be in the table
        assert!(
            html.contains(""),
            "show_table=true must include item label in table"
        );
    }

    // ─── Golden SVG snapshot tests ─────────────────────────────────────────

    /// Read an example file relative to the workspace root.
    fn read_example(name: &str) -> String {
        let path = format!("../../examples/{name}");
        std::fs::read_to_string(&path).unwrap_or_else(|e| panic!("failed to read {path}: {e}"))
    }

    #[test]
    fn snapshot_china_dynasties_svg() {
        let src = read_example("china_dynasties.tdsl");
        let file = tdsl_parser::parse(&src).unwrap();
        let ir = tdsl_core::lower::lower_static(&file).unwrap();
        let svg = render_svg_only(&ir, RenderOptions::default()).unwrap();
        insta::assert_snapshot!(svg);
    }

    #[test]
    fn snapshot_world_wars_svg() {
        let src = read_example("world_wars.tdsl");
        let file = tdsl_parser::parse(&src).unwrap();
        let ir = tdsl_core::lower::lower_static(&file).unwrap();
        let svg = render_svg_only(&ir, RenderOptions::default()).unwrap();
        insta::assert_snapshot!(svg);
    }
}