hwp2md 0.3.1

HWP/HWPX ↔ Markdown bidirectional converter
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
use super::*;

// ── Roundtrip / integration tests ────────────────────────────────────────

// ── helpers ─────────────────────────────────────────────────────────────

/// Build a simple document with one paragraph containing the given inlines.
fn roundtrip_doc(inlines: Vec<Inline>) -> Document {
    Document {
        metadata: Metadata::default(),
        sections: vec![Section {
            blocks: vec![Block::Paragraph { inlines }],
        }],
        assets: Vec::new(),
    }
}

/// Write a document to HWPX, read it back, and return the first paragraph's inlines.
fn roundtrip_inlines(inlines: Vec<Inline>) -> Vec<Inline> {
    let tmp = tempfile::NamedTempFile::new().expect("tmp file");
    let doc = roundtrip_doc(inlines);
    write_hwpx(&doc, tmp.path(), None).expect("write_hwpx");
    let read_back = read_hwpx(tmp.path()).expect("read_hwpx");
    read_back
        .sections
        .into_iter()
        .flat_map(|s| s.blocks)
        .filter_map(|b| match b {
            Block::Paragraph { inlines } => Some(inlines),
            _ => None,
        })
        .next()
        .unwrap_or_default()
}

// ── bold/italic/underline/strike roundtrip ──────────────────────────────

#[test]
fn roundtrip_bold_text_preserved() {
    let result = roundtrip_inlines(vec![bold_inline("bold text")]);
    assert_eq!(result.len(), 1, "expected 1 inline: {result:?}");
    assert_eq!(result[0].text, "bold text");
    assert!(result[0].bold, "bold flag must survive roundtrip: {result:?}");
}

#[test]
fn roundtrip_italic_text_preserved() {
    let result = roundtrip_inlines(vec![italic_inline("italic text")]);
    assert_eq!(result.len(), 1, "expected 1 inline: {result:?}");
    assert_eq!(result[0].text, "italic text");
    assert!(
        result[0].italic,
        "italic flag must survive roundtrip: {result:?}"
    );
}

#[test]
fn roundtrip_bold_italic_combined_preserved() {
    let input = Inline {
        text: "bold italic".into(),
        bold: true,
        italic: true,
        ..Inline::default()
    };
    let result = roundtrip_inlines(vec![input]);
    assert_eq!(result.len(), 1, "expected 1 inline: {result:?}");
    assert_eq!(result[0].text, "bold italic");
    assert!(result[0].bold, "bold must survive roundtrip: {result:?}");
    assert!(result[0].italic, "italic must survive roundtrip: {result:?}");
}

#[test]
fn roundtrip_underline_text_preserved() {
    let result = roundtrip_inlines(vec![underline_inline("underlined")]);
    assert_eq!(result.len(), 1, "expected 1 inline: {result:?}");
    assert_eq!(result[0].text, "underlined");
    assert!(
        result[0].underline,
        "underline flag must survive roundtrip: {result:?}"
    );
}

#[test]
fn roundtrip_strikethrough_text_preserved() {
    let input = Inline {
        text: "struck".into(),
        strikethrough: true,
        ..Inline::default()
    };
    let result = roundtrip_inlines(vec![input]);
    assert_eq!(result.len(), 1, "expected 1 inline: {result:?}");
    assert_eq!(result[0].text, "struck");
    assert!(
        result[0].strikethrough,
        "strikethrough flag must survive roundtrip: {result:?}"
    );
}

#[test]
fn roundtrip_superscript_text_preserved() {
    let input = Inline {
        text: "sup".into(),
        superscript: true,
        ..Inline::default()
    };
    let result = roundtrip_inlines(vec![input]);
    assert_eq!(result.len(), 1, "expected 1 inline: {result:?}");
    assert_eq!(result[0].text, "sup");
    assert!(
        result[0].superscript,
        "superscript flag must survive roundtrip: {result:?}"
    );
}

#[test]
fn roundtrip_subscript_text_preserved() {
    let input = Inline {
        text: "sub".into(),
        subscript: true,
        ..Inline::default()
    };
    let result = roundtrip_inlines(vec![input]);
    assert_eq!(result.len(), 1, "expected 1 inline: {result:?}");
    assert_eq!(result[0].text, "sub");
    assert!(
        result[0].subscript,
        "subscript flag must survive roundtrip: {result:?}"
    );
}

#[test]
fn roundtrip_color_text_preserved() {
    let input = Inline {
        text: "red".into(),
        color: Some("#FF0000".into()),
        ..Inline::default()
    };
    let result = roundtrip_inlines(vec![input]);
    assert_eq!(result.len(), 1, "expected 1 inline: {result:?}");
    assert_eq!(result[0].text, "red");
    assert_eq!(
        result[0].color.as_deref(),
        Some("#FF0000"),
        "color must survive roundtrip: {result:?}"
    );
}

#[test]
fn roundtrip_mixed_plain_and_bold_preserved() {
    let result = roundtrip_inlines(vec![inline("normal "), bold_inline("bold")]);
    assert_eq!(result.len(), 2, "expected 2 inlines: {result:?}");
    assert_eq!(result[0].text, "normal ");
    assert!(!result[0].bold, "first inline must not be bold: {result:?}");
    assert_eq!(result[1].text, "bold");
    assert!(result[1].bold, "second inline must be bold: {result:?}");
}

#[test]
fn roundtrip_bold_italic_underline_strike_color_combined() {
    let input = Inline {
        text: "all".into(),
        bold: true,
        italic: true,
        underline: true,
        strikethrough: true,
        color: Some("#00FF00".into()),
        ..Inline::default()
    };
    let result = roundtrip_inlines(vec![input]);
    assert_eq!(result.len(), 1, "expected 1 inline: {result:?}");
    let r = &result[0];
    assert!(r.bold, "bold: {result:?}");
    assert!(r.italic, "italic: {result:?}");
    assert!(r.underline, "underline: {result:?}");
    assert!(r.strikethrough, "strikethrough: {result:?}");
    assert_eq!(r.color.as_deref(), Some("#00FF00"), "color: {result:?}");
}

// ── section XML inline charPr emission ──────────────────────────────────

#[test]
fn section_xml_bold_inline_emits_inline_charpr() {
    let xml = section_xml(vec![Block::Paragraph {
        inlines: vec![bold_inline("hello")],
    }]);
    assert!(
        xml.contains(r#"bold="true""#),
        "section XML must contain inline charPr with bold=\"true\": {xml}"
    );
}

#[test]
fn section_xml_italic_inline_emits_inline_charpr() {
    let xml = section_xml(vec![Block::Paragraph {
        inlines: vec![italic_inline("hello")],
    }]);
    assert!(
        xml.contains(r#"italic="true""#),
        "section XML must contain inline charPr with italic=\"true\": {xml}"
    );
}

#[test]
fn section_xml_underline_inline_emits_inline_charpr() {
    let xml = section_xml(vec![Block::Paragraph {
        inlines: vec![underline_inline("hello")],
    }]);
    assert!(
        xml.contains(r#"underline="true""#),
        "section XML must contain inline charPr with underline=\"true\": {xml}"
    );
}

#[test]
fn section_xml_strikethrough_inline_emits_strikeout() {
    let input = Inline {
        text: "hello".into(),
        strikethrough: true,
        ..Inline::default()
    };
    let xml = section_xml(vec![Block::Paragraph {
        inlines: vec![input],
    }]);
    assert!(
        xml.contains(r#"strikeout="true""#),
        "section XML must contain inline charPr with strikeout=\"true\": {xml}"
    );
}

#[test]
fn section_xml_color_inline_emits_color_without_hash() {
    let input = Inline {
        text: "hello".into(),
        color: Some("#FF0000".into()),
        ..Inline::default()
    };
    let xml = section_xml(vec![Block::Paragraph {
        inlines: vec![input],
    }]);
    assert!(
        xml.contains(r#"color="FF0000""#),
        "section XML must contain inline charPr with color=\"FF0000\" (no hash): {xml}"
    );
    assert!(
        !xml.contains(r##"color="#FF0000""##),
        "color must not have leading # in OWPML: {xml}"
    );
}

#[test]
fn section_xml_plain_inline_no_charpr_element() {
    let xml = section_xml(vec![Block::Paragraph {
        inlines: vec![inline("plain")],
    }]);
    // Inside the <hp:run> there should be NO <hp:charPr> for plain text.
    // However the section XML itself may contain "hp:charPr" in other contexts
    // (e.g. attribute references), so we check specifically for the element pattern.
    assert!(
        !xml.contains("<hp:charPr "),
        "plain inline must NOT emit inline <hp:charPr> element: {xml}"
    );
}

// ── font_name roundtrip ────────────────────────────────────────────────

#[test]
fn roundtrip_font_name_preserved() {
    let input = Inline {
        text: "styled".into(),
        font_name: Some("Malgun Gothic".into()),
        ..Inline::default()
    };
    let result = roundtrip_inlines(vec![input]);
    assert_eq!(result.len(), 1, "expected 1 inline: {result:?}");
    assert_eq!(result[0].text, "styled");
    assert_eq!(
        result[0].font_name.as_deref(),
        Some("Malgun Gothic"),
        "font_name must survive roundtrip: {result:?}"
    );
}

#[test]
fn roundtrip_font_name_with_bold_preserved() {
    let input = Inline {
        text: "bold styled".into(),
        bold: true,
        font_name: Some("Malgun Gothic".into()),
        ..Inline::default()
    };
    let result = roundtrip_inlines(vec![input]);
    assert_eq!(result.len(), 1, "expected 1 inline: {result:?}");
    assert_eq!(result[0].text, "bold styled");
    assert!(result[0].bold, "bold must survive roundtrip: {result:?}");
    assert_eq!(
        result[0].font_name.as_deref(),
        Some("Malgun Gothic"),
        "font_name must survive roundtrip with bold: {result:?}"
    );
}

#[test]
fn section_xml_font_name_emits_face_name_id_ref() {
    let input = Inline {
        text: "hello".into(),
        font_name: Some("Malgun Gothic".into()),
        ..Inline::default()
    };
    let doc = doc_with_section(vec![Block::Paragraph {
        inlines: vec![input],
    }]);
    let tables = RefTables::build(&doc);
    let sec = &doc.sections[0];
    let xml = generate_section_xml(sec, 0, &tables).expect("generate_section_xml failed");

    assert!(
        xml.contains("faceNameIDRef="),
        "section XML must contain faceNameIDRef for font_name inline: {xml}"
    );

    // Verify the specific index value: "Malgun Gothic" is the second font
    // registered (index 1) because DEFAULT_FONT ("바탕") occupies index 0.
    let expected_idx = tables
        .font_names
        .iter()
        .position(|f| f == "Malgun Gothic")
        .expect("Malgun Gothic must be in font_names");
    assert_eq!(
        expected_idx, 1,
        "Malgun Gothic should be at index 1 (바탕 is 0): {:?}",
        tables.font_names
    );
    let expected_attr = format!("faceNameIDRef=\"{expected_idx}\"");
    assert!(
        xml.contains(&expected_attr),
        "section XML must contain {expected_attr}: {xml}"
    );
}

#[test]
fn header_xml_font_name_registered_in_fontface() {
    let input = Inline {
        text: "hello".into(),
        font_name: Some("Malgun Gothic".into()),
        ..Inline::default()
    };
    let doc = doc_with_section(vec![Block::Paragraph {
        inlines: vec![input],
    }]);
    let tables = RefTables::build(&doc);
    let header =
        super::header::generate_header_xml(&doc, &tables).expect("generate_header_xml failed");
    assert!(
        header.contains("Malgun Gothic"),
        "header XML must contain the registered font name: {header}"
    );
}

// ── default font / edge case roundtrip ──────────────────────────────────

/// An inline with NO font_name set (default "batang" at index 0) must
/// roundtrip with font_name remaining None.  The writer does not emit
/// faceNameIDRef for the default font, and the reader leaves font_name
/// as None when no faceNameIDRef attribute is present.
#[test]
fn roundtrip_default_font_no_font_name_preserved() {
    let input = Inline {
        text: "default font text".into(),
        bold: true,
        ..Inline::default()
    };
    assert!(
        input.font_name.is_none(),
        "precondition: input must have no font_name"
    );

    let result = roundtrip_inlines(vec![input]);
    assert_eq!(result.len(), 1, "expected 1 inline: {result:?}");
    assert_eq!(result[0].text, "default font text");
    assert!(result[0].bold, "bold must survive roundtrip: {result:?}");
    assert!(
        result[0].font_name.is_none(),
        "font_name must remain None for default font after roundtrip: {result:?}"
    );
}

/// When an inline carries a font_name that is NOT registered in the fontface
/// table, write_inline_charpr silently omits the faceNameIDRef attribute.
/// On read-back the font_name will therefore be None.
///
/// This scenario cannot happen through normal document construction (since
/// RefTables::build calls collect_from_inlines which registers every font),
/// but the test documents the expected graceful degradation if it somehow did.
#[test]
fn roundtrip_unknown_font_name_not_in_table() {
    // Build a document with one inline that has a known font.
    let doc = doc_with_section(vec![Block::Paragraph {
        inlines: vec![Inline {
            text: "known font".into(),
            font_name: Some("Malgun Gothic".into()),
            ..Inline::default()
        }],
    }]);
    let tables = RefTables::build(&doc);

    // Verify that the unknown font is indeed absent from the table.
    assert!(
        !tables.font_names.iter().any(|f| f == "Comic Sans MS"),
        "precondition: Comic Sans MS must not be in font_names: {:?}",
        tables.font_names
    );

    // Manually construct a section with an inline referencing a font NOT in the table.
    let rogue_section = Section {
        blocks: vec![Block::Paragraph {
            inlines: vec![Inline {
                text: "rogue".into(),
                font_name: Some("Comic Sans MS".into()),
                ..Inline::default()
            }],
        }],
    };
    let xml =
        generate_section_xml(&rogue_section, 0, &tables).expect("generate_section_xml failed");

    // The writer should NOT emit faceNameIDRef for a font not in the table.
    assert!(
        !xml.contains("faceNameIDRef="),
        "unknown font must not produce faceNameIDRef in section XML: {xml}"
    );

    // Still emits the charPr element (because font_name is Some, triggering
    // the has_formatting check), but without the faceNameIDRef attribute.
    assert!(
        xml.contains("<hp:charPr"),
        "charPr element should still be emitted for the font_name inline: {xml}"
    );
}

// ── image roundtrip ─────────────────────────────────────────────────────

#[test]
fn write_hwpx_image_roundtrip_preserves_asset() {
    let png_bytes = vec![0x89u8, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a];
    let tmp = tempfile::NamedTempFile::new().expect("tmp file");

    let original = Document {
        metadata: Metadata::default(),
        sections: vec![Section {
            blocks: vec![Block::Image {
                src: "photo.png".into(),
                alt: "a photo".into(),
            }],
        }],
        assets: vec![Asset {
            name: "photo.png".into(),
            data: png_bytes.clone(),
            mime_type: "image/png".into(),
        }],
    };
    write_hwpx(&original, tmp.path(), None).expect("write");

    let read_back = read_hwpx(tmp.path()).expect("read_hwpx");

    let has_image = read_back
        .sections
        .iter()
        .flat_map(|s| &s.blocks)
        .any(|b| matches!(b, Block::Image { src, .. } if src.contains("photo")));
    assert!(
        has_image,
        "image block must survive HWPX roundtrip; sections: {:?}",
        read_back.sections
    );

    assert_eq!(
        read_back.assets.len(),
        1,
        "one asset expected after roundtrip"
    );
    assert_eq!(
        read_back.assets[0].data, png_bytes,
        "asset binary content must be preserved through roundtrip"
    );
    assert_eq!(
        read_back.assets[0].mime_type, "image/png",
        "asset MIME type must be preserved"
    );
}

// ── golden file test: verify actual XML output ─────────────────────────

/// Golden file test that validates the writer's actual XML output byte-for-byte
/// rather than relying on roundtrip fidelity.  Builds a comprehensive IR document
/// (heading, bold paragraph, italic paragraph, table, list), writes it to HWPX,
/// then inspects specific XML files inside the ZIP for expected patterns.
#[test]
fn golden_comprehensive_document_structure() {
    use std::io::Read as _;

    // ── 1. Build a comprehensive IR document ──

    let doc = Document {
        metadata: Metadata {
            title: Some("Golden Test Doc".into()),
            author: Some("Test Author".into()),
            ..Metadata::default()
        },
        sections: vec![Section {
            blocks: vec![
                // H1 heading
                Block::Heading {
                    level: 1,
                    inlines: vec![Inline::plain("Main Title")],
                },
                // Paragraph with bold inline
                Block::Paragraph {
                    inlines: vec![
                        inline("Normal text "),
                        bold_inline("bold text"),
                    ],
                },
                // Paragraph with italic inline
                Block::Paragraph {
                    inlines: vec![italic_inline("italic text")],
                },
                // 2x2 table
                Block::Table {
                    rows: vec![
                        TableRow {
                            cells: vec![
                                TableCell {
                                    blocks: vec![Block::Paragraph {
                                        inlines: vec![inline("Cell A1")],
                                    }],
                                    colspan: 1,
                                    rowspan: 1,
                                },
                                TableCell {
                                    blocks: vec![Block::Paragraph {
                                        inlines: vec![inline("Cell B1")],
                                    }],
                                    colspan: 1,
                                    rowspan: 1,
                                },
                            ],
                            is_header: true,
                        },
                        TableRow {
                            cells: vec![
                                TableCell {
                                    blocks: vec![Block::Paragraph {
                                        inlines: vec![inline("Cell A2")],
                                    }],
                                    colspan: 1,
                                    rowspan: 1,
                                },
                                TableCell {
                                    blocks: vec![Block::Paragraph {
                                        inlines: vec![inline("Cell B2")],
                                    }],
                                    colspan: 1,
                                    rowspan: 1,
                                },
                            ],
                            is_header: false,
                        },
                    ],
                    col_count: 2,
                },
                // Code block
                Block::CodeBlock {
                    language: Some("rust".into()),
                    code: "fn main() {}".into(),
                },
                // Horizontal rule
                Block::HorizontalRule,
                // Block quote
                Block::BlockQuote {
                    blocks: vec![Block::Paragraph {
                        inlines: vec![inline("quoted text")],
                    }],
                },
                // Unordered list
                Block::List {
                    ordered: false,
                    start: 1,
                    items: vec![
                        ListItem {
                            blocks: vec![Block::Paragraph {
                                inlines: vec![inline("List item one")],
                            }],
                            children: Vec::new(),
                        },
                        ListItem {
                            blocks: vec![Block::Paragraph {
                                inlines: vec![inline("List item two")],
                            }],
                            children: Vec::new(),
                        },
                    ],
                },
            ],
        }],
        assets: Vec::new(),
    };

    // ── 2. Write to HWPX bytes ──

    let tmp = tempfile::NamedTempFile::new().expect("tmp file");
    write_hwpx(&doc, tmp.path(), None).expect("write_hwpx");

    // ── 3. Open the ZIP and read specific XML entries ──

    let file = std::fs::File::open(tmp.path()).expect("open zip");
    let mut archive = zip::ZipArchive::new(file).expect("parse zip");

    // -- section0.xml assertions --
    let section_xml = {
        let mut entry = archive
            .by_name("Contents/section0.xml")
            .expect("section0.xml must exist in HWPX");
        let mut buf = String::new();
        entry.read_to_string(&mut buf).expect("read section0.xml");
        buf
    };

    // Verify heading has styleIDRef
    assert!(
        section_xml.contains(r#"hp:styleIDRef="1""#),
        "H1 heading must have hp:styleIDRef=\"1\" in section XML:\n{section_xml}"
    );

    // Verify heading text
    assert!(
        section_xml.contains("<hp:t>Main Title</hp:t>"),
        "heading text 'Main Title' must appear in <hp:t>:\n{section_xml}"
    );

    // Verify bold inline charPr
    assert!(
        section_xml.contains(r#"bold="true""#),
        "bold inline must emit charPr with bold=\"true\":\n{section_xml}"
    );

    // Verify bold text content
    assert!(
        section_xml.contains("<hp:t>bold text</hp:t>"),
        "bold text content must appear in <hp:t>:\n{section_xml}"
    );

    // Verify italic inline charPr
    assert!(
        section_xml.contains(r#"italic="true""#),
        "italic inline must emit charPr with italic=\"true\":\n{section_xml}"
    );

    // Verify italic text content
    assert!(
        section_xml.contains("<hp:t>italic text</hp:t>"),
        "italic text content must appear in <hp:t>:\n{section_xml}"
    );

    // Verify normal (non-bold, non-italic) text
    assert!(
        section_xml.contains("<hp:t>Normal text </hp:t>"),
        "plain text must appear in <hp:t>:\n{section_xml}"
    );

    // Verify table structure
    assert!(
        section_xml.contains(r#"<hp:tbl"#),
        "table must emit <hp:tbl> element:\n{section_xml}"
    );
    assert!(
        section_xml.contains(r#"rowCnt="2""#),
        "table must have rowCnt=\"2\":\n{section_xml}"
    );
    assert!(
        section_xml.contains(r#"colCnt="2""#),
        "table must have colCnt=\"2\":\n{section_xml}"
    );
    assert!(
        section_xml.contains("<hp:tr>"),
        "table must contain <hp:tr> rows:\n{section_xml}"
    );
    assert!(
        section_xml.contains("<hp:tc>"),
        "table must contain <hp:tc> cells:\n{section_xml}"
    );
    assert!(
        section_xml.contains("<hp:t>Cell A1</hp:t>"),
        "table cell text 'Cell A1' must appear:\n{section_xml}"
    );
    assert!(
        section_xml.contains("<hp:t>Cell B2</hp:t>"),
        "table cell text 'Cell B2' must appear:\n{section_xml}"
    );

    // Verify list items are emitted as paragraphs
    assert!(
        section_xml.contains("<hp:t>List item one</hp:t>"),
        "list item text 'List item one' must appear:\n{section_xml}"
    );
    assert!(
        section_xml.contains("<hp:t>List item two</hp:t>"),
        "list item text 'List item two' must appear:\n{section_xml}"
    );

    // Verify section XML namespace declarations
    assert!(
        section_xml.contains("xmlns:hs="),
        "section XML must declare hs namespace:\n{section_xml}"
    );
    assert!(
        section_xml.contains("xmlns:hp="),
        "section XML must declare hp namespace:\n{section_xml}"
    );

    // Verify no inline <hp:charPr> for plain text runs (the plain text
    // runs should only have a charPrIDRef attribute, not an inline element).
    // We check that the number of <hp:charPr occurrences matches the number
    // of formatted inlines (bold + italic = 2).
    let charpr_count = section_xml.matches("<hp:charPr ").count();
    assert!(
        charpr_count >= 2,
        "at least 2 inline <hp:charPr> elements expected (bold + italic), found {charpr_count}:\n{section_xml}"
    );

    // -- content.hpf assertions --
    let content_hpf = {
        let mut entry = archive
            .by_name("Contents/content.hpf")
            .expect("content.hpf must exist in HWPX");
        let mut buf = String::new();
        entry.read_to_string(&mut buf).expect("read content.hpf");
        buf
    };

    assert!(
        content_hpf.contains("section0.xml"),
        "content.hpf must reference section0.xml:\n{content_hpf}"
    );
    assert!(
        content_hpf.contains("<hp:title>Golden Test Doc</hp:title>"),
        "content.hpf must contain document title:\n{content_hpf}"
    );
    assert!(
        content_hpf.contains("<hp:author>Test Author</hp:author>"),
        "content.hpf must contain document author:\n{content_hpf}"
    );

    // -- header.xml assertions --
    let header_xml = {
        let mut entry = archive
            .by_name("Contents/header.xml")
            .expect("header.xml must exist in HWPX");
        let mut buf = String::new();
        entry.read_to_string(&mut buf).expect("read header.xml");
        buf
    };

    // Header must contain fontface declarations
    assert!(
        header_xml.contains("hh:fontface"),
        "header.xml must contain fontface declarations:\n{header_xml}"
    );

    // Header must contain charProperties
    assert!(
        header_xml.contains("hh:charPr"),
        "header.xml must contain charPr entries:\n{header_xml}"
    );

    // Header must contain styles (heading styles)
    assert!(
        header_xml.contains("hh:style"),
        "header.xml must contain style entries:\n{header_xml}"
    );

    // -- Verify code block text --
    assert!(
        section_xml.contains("<hp:t>fn main() {}</hp:t>"),
        "code block text must appear in <hp:t>:\n{section_xml}"
    );

    // -- Verify horizontal rule produces box-drawing characters --
    assert!(
        section_xml.contains("\u{2500}"),
        "horizontal rule must emit box-drawing characters:\n{section_xml}"
    );

    // -- Verify block quote text (emitted as plain paragraph) --
    assert!(
        section_xml.contains("<hp:t>quoted text</hp:t>"),
        "block quote text must appear in <hp:t>:\n{section_xml}"
    );

    // -- mimetype assertion --
    let mimetype = {
        let mut entry = archive
            .by_name("mimetype")
            .expect("mimetype must exist in HWPX");
        let mut buf = String::new();
        entry.read_to_string(&mut buf).expect("read mimetype");
        buf
    };
    assert_eq!(
        mimetype, "application/hwp+zip",
        "mimetype must be exactly 'application/hwp+zip'"
    );

    // -- version.xml assertion --
    archive
        .by_name("version.xml")
        .expect("version.xml must exist in HWPX");

    // -- META-INF/container.xml assertion --
    let container_xml = {
        let mut entry = archive
            .by_name("META-INF/container.xml")
            .expect("META-INF/container.xml must exist in HWPX");
        let mut buf = String::new();
        entry.read_to_string(&mut buf).expect("read container.xml");
        buf
    };
    assert!(
        container_xml.contains("content.hpf"),
        "container.xml must reference content.hpf:\n{container_xml}"
    );
}