office2pdf 0.5.0

Convert DOCX, XLSX, and PPTX files to PDF using pure Rust
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
use super::*;

// ── DataBar / IconSet codegen tests ──────────────────────────────

#[test]
fn test_data_bar_codegen() {
    use crate::ir::DataBarInfo;

    let cell = TableCell {
        content: vec![Block::Paragraph(Paragraph {
            style: ParagraphStyle::default(),
            runs: vec![Run {
                text: "50".to_string(),
                style: TextStyle::default(),
                href: None,
                footnote: None,
            }],
        })],
        data_bar: Some(DataBarInfo {
            color: Color::new(0x63, 0x8E, 0xC6),
            fill_pct: 50.0,
        }),
        ..TableCell::default()
    };
    let table = Table {
        rows: vec![TableRow {
            cells: vec![cell],
            height: None,
        }],
        column_widths: vec![100.0],
        ..Table::default()
    };
    let page = Page::Sheet(SheetPage {
        name: "Sheet1".to_string(),
        size: PageSize::default(),
        margins: Margins::default(),
        table,
        header: None,
        footer: None,
        charts: vec![],
    });
    let doc = make_doc(vec![page]);
    let output = generate_typst(&doc).unwrap();
    assert!(
        output.source.contains("fill: rgb(99, 142, 198)"),
        "DataBar should contain bar color fill. Got: {}",
        output.source,
    );
    assert!(
        output.source.contains("width: 50%"),
        "DataBar should contain 50% width. Got: {}",
        output.source,
    );
}

#[test]
fn test_icon_text_codegen() {
    let cell = TableCell {
        content: vec![Block::Paragraph(Paragraph {
            style: ParagraphStyle::default(),
            runs: vec![Run {
                text: "90".to_string(),
                style: TextStyle::default(),
                href: None,
                footnote: None,
            }],
        })],
        icon_text: Some("".to_string()),
        ..TableCell::default()
    };
    let table = Table {
        rows: vec![TableRow {
            cells: vec![cell],
            height: None,
        }],
        column_widths: vec![100.0],
        ..Table::default()
    };
    let page = Page::Sheet(SheetPage {
        name: "Sheet1".to_string(),
        size: PageSize::default(),
        margins: Margins::default(),
        table,
        header: None,
        footer: None,
        charts: vec![],
    });
    let doc = make_doc(vec![page]);
    let output = generate_typst(&doc).unwrap();
    assert!(
        output.source.contains(""),
        "Icon text should appear in output. Got: {}",
        output.source,
    );
}

#[test]
fn test_table_colspan_clamped_to_available_columns() {
    let wide_cell = TableCell {
        content: vec![Block::Paragraph(Paragraph {
            style: ParagraphStyle::default(),
            runs: vec![Run {
                text: "Wide".to_string(),
                style: TextStyle::default(),
                href: None,
                footnote: None,
            }],
        })],
        col_span: 3,
        ..TableCell::default()
    };
    let table = Table {
        rows: vec![
            TableRow {
                cells: vec![wide_cell],
                height: None,
            },
            TableRow {
                cells: vec![make_text_cell("A2"), make_text_cell("B2")],
                height: None,
            },
        ],
        column_widths: vec![100.0, 200.0],
        ..Table::default()
    };
    let doc = make_doc(vec![make_flow_page(vec![Block::Table(table)])]);
    let result = generate_typst(&doc).unwrap().source;
    assert!(
        result.contains("colspan: 2"),
        "Expected colspan clamped to 2, got: {result}"
    );
    assert!(
        !result.contains("colspan: 3"),
        "colspan: 3 should have been clamped, got: {result}"
    );
}

#[test]
fn test_table_colspan_clamped_mid_row() {
    let normal_cell = make_text_cell("A1");
    let wide_cell = TableCell {
        content: vec![Block::Paragraph(Paragraph {
            style: ParagraphStyle::default(),
            runs: vec![Run {
                text: "Wide".to_string(),
                style: TextStyle::default(),
                href: None,
                footnote: None,
            }],
        })],
        col_span: 3,
        ..TableCell::default()
    };
    let table = Table {
        rows: vec![TableRow {
            cells: vec![normal_cell, wide_cell],
            height: None,
        }],
        column_widths: vec![100.0, 100.0, 100.0],
        ..Table::default()
    };
    let doc = make_doc(vec![make_flow_page(vec![Block::Table(table)])]);
    let result = generate_typst(&doc).unwrap().source;
    assert!(
        result.contains("colspan: 2"),
        "Expected colspan clamped to 2, got: {result}"
    );
}

#[test]
fn test_table_colspan_no_column_widths_inferred() {
    let wide_cell = TableCell {
        content: vec![Block::Paragraph(Paragraph {
            style: ParagraphStyle::default(),
            runs: vec![Run {
                text: "Wide".to_string(),
                style: TextStyle::default(),
                href: None,
                footnote: None,
            }],
        })],
        col_span: 5,
        ..TableCell::default()
    };
    let table = Table {
        rows: vec![
            TableRow {
                cells: vec![wide_cell],
                height: None,
            },
            TableRow {
                cells: vec![
                    make_text_cell("A"),
                    make_text_cell("B"),
                    make_text_cell("C"),
                ],
                height: None,
            },
        ],
        column_widths: vec![],
        ..Table::default()
    };
    let doc = make_doc(vec![make_flow_page(vec![Block::Table(table)])]);
    let result = generate_typst(&doc).unwrap().source;
    assert!(
        result.contains("colspan: 3"),
        "Expected colspan clamped to 3 (inferred columns), got: {result}"
    );
    assert!(
        !result.contains("colspan: 5"),
        "colspan: 5 should have been clamped, got: {result}"
    );
}

// ── Metadata codegen tests ─────────────────────────────────────────

#[test]
fn test_generate_typst_with_metadata_title_and_author() {
    let doc = Document {
        metadata: Metadata {
            title: Some("Test Title".to_string()),
            author: Some("Test Author".to_string()),
            ..Default::default()
        },
        pages: vec![make_flow_page(vec![Block::Paragraph(Paragraph {
            runs: vec![Run {
                text: "Hello".to_string(),
                style: TextStyle::default(),
                footnote: None,
                href: None,
            }],
            style: ParagraphStyle::default(),
        })])],
        styles: StyleSheet::default(),
    };
    let result = generate_typst(&doc).unwrap().source;
    assert!(
        result.contains("#set document(title: \"Test Title\", author: \"Test Author\")"),
        "Expected document metadata in Typst output, got: {result}"
    );
}

#[test]
fn test_generate_typst_with_metadata_title_only() {
    let doc = Document {
        metadata: Metadata {
            title: Some("Only Title".to_string()),
            ..Default::default()
        },
        pages: vec![make_flow_page(vec![Block::Paragraph(Paragraph {
            runs: vec![Run {
                text: "Hello".to_string(),
                style: TextStyle::default(),
                footnote: None,
                href: None,
            }],
            style: ParagraphStyle::default(),
        })])],
        styles: StyleSheet::default(),
    };
    let result = generate_typst(&doc).unwrap().source;
    assert!(
        result.contains("#set document(title: \"Only Title\")"),
        "Expected title-only metadata in Typst output, got: {result}"
    );
}

#[test]
fn test_generate_typst_without_metadata() {
    let doc = make_doc(vec![make_flow_page(vec![Block::Paragraph(Paragraph {
        runs: vec![Run {
            text: "Hello".to_string(),
            style: TextStyle::default(),
            footnote: None,
            href: None,
        }],
        style: ParagraphStyle::default(),
    })])]);
    let result = generate_typst(&doc).unwrap().source;
    assert!(
        !result.contains("#set document("),
        "Should not emit #set document when no metadata, got: {result}"
    );
}

#[test]
fn test_generate_typst_with_metadata_created_date() {
    let doc = Document {
        metadata: Metadata {
            title: Some("Dated Doc".to_string()),
            created: Some("2024-06-15T10:30:00Z".to_string()),
            ..Default::default()
        },
        pages: vec![make_flow_page(vec![Block::Paragraph(Paragraph {
            runs: vec![Run {
                text: "Hello".to_string(),
                style: TextStyle::default(),
                footnote: None,
                href: None,
            }],
            style: ParagraphStyle::default(),
        })])],
        styles: StyleSheet::default(),
    };
    let result = generate_typst(&doc).unwrap().source;
    assert!(
        result.contains("date: datetime(year: 2024, month: 6, day: 15"),
        "Expected document date from metadata created field, got: {result}"
    );
}

#[test]
fn test_generate_typst_with_metadata_date_only() {
    let doc = Document {
        metadata: Metadata {
            created: Some("2023-12-25T08:00:00Z".to_string()),
            ..Default::default()
        },
        pages: vec![make_flow_page(vec![Block::Paragraph(Paragraph {
            runs: vec![Run {
                text: "Hello".to_string(),
                style: TextStyle::default(),
                footnote: None,
                href: None,
            }],
            style: ParagraphStyle::default(),
        })])],
        styles: StyleSheet::default(),
    };
    let result = generate_typst(&doc).unwrap().source;
    assert!(
        result.contains("date: datetime(year: 2023, month: 12, day: 25"),
        "Expected document date even without title/author, got: {result}"
    );
}

#[test]
fn test_generate_typst_with_invalid_created_date() {
    let doc = Document {
        metadata: Metadata {
            title: Some("Bad Date Doc".to_string()),
            created: Some("not-a-date".to_string()),
            ..Default::default()
        },
        pages: vec![make_flow_page(vec![Block::Paragraph(Paragraph {
            runs: vec![Run {
                text: "Hello".to_string(),
                style: TextStyle::default(),
                footnote: None,
                href: None,
            }],
            style: ParagraphStyle::default(),
        })])],
        styles: StyleSheet::default(),
    };
    let result = generate_typst(&doc).unwrap().source;
    assert!(
        !result.contains("date: datetime("),
        "Invalid date should not produce document date, got: {result}"
    );
}

#[test]
fn test_parse_iso8601_date_full() {
    let result = parse_iso8601_date("2024-06-15T10:30:45Z");
    assert_eq!(result, Some((2024, 6, 15, 10, 30, 45)));
}

#[test]
fn test_parse_iso8601_date_date_only() {
    let result = parse_iso8601_date("2023-12-25");
    assert_eq!(result, Some((2023, 12, 25, 0, 0, 0)));
}

#[test]
fn test_parse_iso8601_date_invalid() {
    assert_eq!(parse_iso8601_date("not-a-date"), None);
    assert_eq!(parse_iso8601_date(""), None);
    assert_eq!(parse_iso8601_date("2024"), None);
    assert_eq!(parse_iso8601_date("2024-13-01T00:00:00Z"), None);
    assert_eq!(parse_iso8601_date("2024-00-01T00:00:00Z"), None);
}

// ── Extended geometry codegen tests (US-085) ──────────────────────────

#[test]
fn test_triangle_polygon_codegen() {
    let doc = make_doc(vec![make_fixed_page(
        960.0,
        540.0,
        vec![make_shape_element(
            10.0,
            20.0,
            200.0,
            150.0,
            ShapeKind::Polygon {
                vertices: vec![(0.5, 0.0), (1.0, 1.0), (0.0, 1.0)],
            },
            Some(Color::new(255, 0, 0)),
            None,
        )],
    )]);
    let output = generate_typst(&doc).unwrap();
    assert!(
        output.source.contains("#polygon("),
        "Expected #polygon in: {}",
        output.source
    );
    assert!(
        output.source.contains("100pt"),
        "Expected 100pt vertex x in: {}",
        output.source
    );
    assert!(
        output.source.contains("fill: rgb(255, 0, 0)"),
        "Expected fill in: {}",
        output.source
    );
}

#[test]
fn test_rounded_rectangle_codegen() {
    let doc = make_doc(vec![make_fixed_page(
        960.0,
        540.0,
        vec![make_shape_element(
            10.0,
            20.0,
            200.0,
            100.0,
            ShapeKind::RoundedRectangle {
                radius_fraction: 0.1,
            },
            Some(Color::new(0, 0, 255)),
            None,
        )],
    )]);
    let output = generate_typst(&doc).unwrap();
    assert!(
        output.source.contains("#rect("),
        "Expected #rect in: {}",
        output.source
    );
    assert!(
        output.source.contains("radius:"),
        "Expected radius parameter in: {}",
        output.source
    );
    assert!(
        output.source.contains("radius: 10pt"),
        "Expected radius: 10pt in: {}",
        output.source
    );
}

#[test]
fn test_arrow_polygon_codegen() {
    let doc = make_doc(vec![make_fixed_page(
        960.0,
        540.0,
        vec![make_shape_element(
            0.0,
            0.0,
            300.0,
            150.0,
            ShapeKind::Polygon {
                vertices: vec![
                    (0.0, 0.25),
                    (0.6, 0.25),
                    (0.6, 0.0),
                    (1.0, 0.5),
                    (0.6, 1.0),
                    (0.6, 0.75),
                    (0.0, 0.75),
                ],
            },
            Some(Color::new(255, 136, 0)),
            None,
        )],
    )]);
    let output = generate_typst(&doc).unwrap();
    assert!(
        output.source.contains("#polygon("),
        "Expected #polygon for arrow in: {}",
        output.source
    );
    assert!(
        output.source.contains("300pt"),
        "Expected 300pt (arrow tip) in: {}",
        output.source
    );
}

#[test]
fn test_polygon_with_stroke_codegen() {
    let doc = make_doc(vec![make_fixed_page(
        960.0,
        540.0,
        vec![make_shape_element(
            0.0,
            0.0,
            100.0,
            100.0,
            ShapeKind::Polygon {
                vertices: vec![(0.5, 0.0), (1.0, 0.5), (0.5, 1.0), (0.0, 0.5)],
            },
            None,
            Some(BorderSide {
                width: 2.0,
                color: Color::new(0, 0, 0),
                style: BorderLineStyle::Solid,
            }),
        )],
    )]);
    let output = generate_typst(&doc).unwrap();
    assert!(
        output.source.contains("#polygon("),
        "Expected #polygon in: {}",
        output.source
    );
    assert!(
        output.source.contains("stroke: 2pt + rgb(0, 0, 0)"),
        "Expected stroke in: {}",
        output.source
    );
}

#[test]
fn test_font_substitution_calibri_produces_fallback_list() {
    let doc = make_doc(vec![make_flow_page(vec![Block::Paragraph(Paragraph {
        style: ParagraphStyle::default(),
        runs: vec![Run {
            text: "Calibri text".to_string(),
            style: TextStyle {
                font_family: Some("Calibri".to_string()),
                ..TextStyle::default()
            },
            href: None,
            footnote: None,
        }],
    })])]);
    let result = generate_typst(&doc).unwrap().source;
    assert!(
        result.contains(r#"font: ("Calibri", "Carlito", "Liberation Sans")"#),
        "Expected font fallback list for Calibri in: {result}"
    );
}

#[test]
fn test_font_substitution_arial_produces_fallback_list() {
    let doc = make_doc(vec![make_flow_page(vec![Block::Paragraph(Paragraph {
        style: ParagraphStyle::default(),
        runs: vec![Run {
            text: "Arial text".to_string(),
            style: TextStyle {
                font_family: Some("Arial".to_string()),
                ..TextStyle::default()
            },
            href: None,
            footnote: None,
        }],
    })])]);
    let result = generate_typst(&doc).unwrap().source;
    assert!(
        result.contains(r#"font: ("Arial", "Liberation Sans", "Arimo")"#),
        "Expected font fallback list for Arial in: {result}"
    );
}

#[test]
fn test_font_substitution_unknown_font_no_fallback() {
    let doc = make_doc(vec![make_flow_page(vec![Block::Paragraph(Paragraph {
        style: ParagraphStyle::default(),
        runs: vec![Run {
            text: "Custom text".to_string(),
            style: TextStyle {
                font_family: Some("Helvetica".to_string()),
                ..TextStyle::default()
            },
            href: None,
            footnote: None,
        }],
    })])]);
    let result = generate_typst(&doc).unwrap().source;
    assert!(
        result.contains(r#"font: "Helvetica""#),
        "Unknown font should use simple quoted string in: {result}"
    );
    assert!(
        !result.contains("font: (\"Helvetica\""),
        "Unknown font should not use array syntax in: {result}"
    );
}

#[test]
fn test_font_substitution_times_new_roman() {
    let doc = make_doc(vec![make_flow_page(vec![Block::Paragraph(Paragraph {
        style: ParagraphStyle::default(),
        runs: vec![Run {
            text: "TNR text".to_string(),
            style: TextStyle {
                font_family: Some("Times New Roman".to_string()),
                ..TextStyle::default()
            },
            href: None,
            footnote: None,
        }],
    })])]);
    let result = generate_typst(&doc).unwrap().source;
    assert!(
        result.contains(r#"font: ("Times New Roman", "Liberation Serif", "Tinos")"#),
        "Expected font fallback list for Times New Roman in: {result}"
    );
}

#[test]
fn test_font_family_infers_medium_weight_from_family_name() {
    let doc = make_doc(vec![make_flow_page(vec![Block::Paragraph(Paragraph {
        style: ParagraphStyle::default(),
        runs: vec![Run {
            text: "Title".to_string(),
            style: TextStyle {
                font_family: Some("Pretendard Medium".to_string()),
                ..TextStyle::default()
            },
            href: None,
            footnote: None,
        }],
    })])]);
    let result = generate_typst(&doc).unwrap().source;
    assert!(
        result.contains(r#"weight: "medium""#),
        "Expected medium weight inferred from family name in: {result}"
    );
}

#[test]
fn test_font_family_infers_extrabold_weight_from_family_name() {
    let doc = make_doc(vec![make_flow_page(vec![Block::Paragraph(Paragraph {
        style: ParagraphStyle::default(),
        runs: vec![Run {
            text: "Heading".to_string(),
            style: TextStyle {
                font_family: Some("Pretendard ExtraBold".to_string()),
                ..TextStyle::default()
            },
            href: None,
            footnote: None,
        }],
    })])]);
    let result = generate_typst(&doc).unwrap().source;
    assert!(
        result.contains(r#"weight: "extrabold""#),
        "Expected extrabold weight inferred from family name in: {result}"
    );
}

#[test]
fn test_generate_typst_prefers_office_font_order_when_context_present() {
    let doc = make_doc(vec![make_flow_page(vec![Block::Paragraph(Paragraph {
        style: ParagraphStyle::default(),
        runs: vec![Run {
            text: "Title".to_string(),
            style: TextStyle {
                font_family: Some("Pretendard".to_string()),
                ..TextStyle::default()
            },
            href: None,
            footnote: None,
        }],
    })])]);
    let context = FontSearchContext::for_test(
        Vec::new(),
        &["Apple SD Gothic Neo", "Malgun Gothic"],
        &["Malgun Gothic"],
        &[],
    );

    let output = generate_typst_with_options_and_font_context(
        &doc,
        &ConvertOptions::default(),
        Some(&context),
    )
    .unwrap();

    let apple_index = output
        .source
        .find("\"Apple SD Gothic Neo\"")
        .expect("Apple SD Gothic Neo should appear in Typst output");
    let malgun_index = output
        .source
        .find("\"Malgun Gothic\"")
        .expect("Malgun Gothic should appear in Typst output");
    assert!(
        malgun_index < apple_index,
        "Office-resolved font ordering should win in Typst output: {}",
        output.source
    );
}

// --- Heading level codegen tests (US-096) ---

#[test]
fn test_generate_heading_level_1() {
    let doc = make_doc(vec![make_flow_page(vec![Block::Paragraph(Paragraph {
        style: ParagraphStyle {
            heading_level: Some(1),
            ..ParagraphStyle::default()
        },
        runs: vec![Run {
            text: "Main Title".to_string(),
            style: TextStyle::default(),
            href: None,
            footnote: None,
        }],
    })])]);
    let result = generate_typst(&doc).unwrap().source;
    assert!(
        result.contains("#heading(level: 1)[Main Title]"),
        "H1 paragraph should emit #heading(level: 1): {result}"
    );
}

#[test]
fn test_generate_heading_level_2() {
    let doc = make_doc(vec![make_flow_page(vec![Block::Paragraph(Paragraph {
        style: ParagraphStyle {
            heading_level: Some(2),
            ..ParagraphStyle::default()
        },
        runs: vec![Run {
            text: "Sub Section".to_string(),
            style: TextStyle::default(),
            href: None,
            footnote: None,
        }],
    })])]);
    let result = generate_typst(&doc).unwrap().source;
    assert!(
        result.contains("#heading(level: 2)[Sub Section]"),
        "H2 paragraph should emit #heading(level: 2): {result}"
    );
}

#[test]
fn test_generate_heading_levels_3_to_6() {
    for level in 3..=6u8 {
        let text = format!("Heading {level}");
        let doc = make_doc(vec![make_flow_page(vec![Block::Paragraph(Paragraph {
            style: ParagraphStyle {
                heading_level: Some(level),
                ..ParagraphStyle::default()
            },
            runs: vec![Run {
                text: text.clone(),
                style: TextStyle::default(),
                href: None,
                footnote: None,
            }],
        })])]);
        let result = generate_typst(&doc).unwrap().source;
        let expected = format!("#heading(level: {level})[{text}]");
        assert!(
            result.contains(&expected),
            "H{level} should emit {expected}: {result}"
        );
    }
}

#[test]
fn test_generate_heading_with_styled_run() {
    let doc = make_doc(vec![make_flow_page(vec![Block::Paragraph(Paragraph {
        style: ParagraphStyle {
            heading_level: Some(1),
            ..ParagraphStyle::default()
        },
        runs: vec![Run {
            text: "Styled Heading".to_string(),
            style: TextStyle {
                bold: Some(true),
                font_size: Some(24.0),
                ..TextStyle::default()
            },
            href: None,
            footnote: None,
        }],
    })])]);
    let result = generate_typst(&doc).unwrap().source;
    assert!(
        result.contains("#heading(level: 1)"),
        "Heading with styling should still emit #heading: {result}"
    );
}

#[test]
fn test_generate_regular_paragraph_no_heading() {
    let doc = make_doc(vec![make_flow_page(vec![Block::Paragraph(Paragraph {
        style: ParagraphStyle::default(),
        runs: vec![Run {
            text: "Normal text".to_string(),
            style: TextStyle::default(),
            href: None,
            footnote: None,
        }],
    })])]);
    let result = generate_typst(&doc).unwrap().source;
    assert!(
        !result.contains("#heading"),
        "Regular paragraph should not emit #heading: {result}"
    );
}