mdv 4.2.1

Terminal Markdown Viewer
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
use assert_cmd::Command;
use mdv::utils::strip_ansi;
use predicates::prelude::*;
use std::fs;
use tempfile::{NamedTempFile, TempDir};

fn mdv_cmd() -> Command {
    Command::new(assert_cmd::cargo::cargo_bin!("mdv"))
}

#[test]
fn test_help_command() {
    let mut cmd = mdv_cmd();
    cmd.arg("--help");
    cmd.assert()
        .success()
        .stdout(predicate::str::contains("terminal-based markdown viewer"));
}

#[test]
fn test_version_command() {
    let mut cmd = mdv_cmd();
    cmd.arg("--version");
    cmd.assert()
        .success()
        .stdout(predicate::str::contains("mdv"));
}

#[test]
fn test_basic_markdown_rendering() {
    let temp_file = NamedTempFile::new().unwrap();
    fs::write(&temp_file, "# Hello World\n\nThis is **bold** text.").unwrap();

    let mut cmd = mdv_cmd();
    cmd.arg(temp_file.path());
    cmd.assert()
        .success()
        .stdout(predicate::str::contains("Hello World"));
}

#[test]
fn test_stdin_input() {
    let mut cmd = mdv_cmd();
    cmd.arg("-");
    cmd.write_stdin("# Test\n\nFrom stdin");
    cmd.assert()
        .success()
        .stdout(predicate::str::contains("Test"));
}

#[test]
fn test_stdin_input_with_bom() {
    let mut cmd = mdv_cmd();
    cmd.arg("-");
    cmd.write_stdin("\u{feff}# Heading\n\nBody text");
    cmd.assert()
        .success()
        .stdout(predicate::str::contains("# Heading").not())
        .stdout(predicate::str::contains("Heading"))
        .stdout(predicate::str::contains("\u{feff}").not());
}

#[test]
fn test_html_output() {
    let temp_file = NamedTempFile::new().unwrap();
    fs::write(&temp_file, "# HTML Test\n\nThis is a test.").unwrap();

    let mut cmd = mdv_cmd();
    cmd.arg("-H").arg(temp_file.path());
    cmd.assert()
        .success()
        .stdout(predicate::str::contains("<h1>"))
        .stdout(predicate::str::contains("HTML Test"));
}

#[test]
fn test_no_colors_option() {
    let temp_file = NamedTempFile::new().unwrap();
    fs::write(&temp_file, "# Test\n\n**Bold text**").unwrap();

    let mut cmd = mdv_cmd();
    cmd.arg("-A").arg(temp_file.path());
    cmd.assert().success();
    // Note: We can't easily test for absence of ANSI codes in integration tests
}

#[test]
fn test_theme_option() {
    let temp_file = NamedTempFile::new().unwrap();
    fs::write(&temp_file, "# Theme Test\n\nTesting themes.").unwrap();

    let mut cmd = mdv_cmd();
    cmd.arg("-t").arg("monokai").arg(temp_file.path());
    cmd.assert()
        .success()
        .stdout(predicate::str::contains("Theme Test"));
}

#[test]
fn test_comments_rendered_by_default() {
    let temp_file = NamedTempFile::new().unwrap();
    fs::write(&temp_file, "<!-- note -->\n\nVisible text\n").unwrap();

    let mut cmd = mdv_cmd();
    cmd.arg("-A").arg(temp_file.path());
    cmd.assert()
        .success()
        .stdout(predicate::str::contains("<!-- note -->"))
        .stdout(predicate::str::contains("Visible text"));
}

#[test]
fn test_raw_html_rendered_as_literal_text() {
    let temp_file = NamedTempFile::new().unwrap();
    fs::write(
        &temp_file,
        "<div align=\"center\">Centered</div>\n\nText with <span class=\"raw\">inline</span> HTML.\n",
    )
    .unwrap();

    let mut cmd = mdv_cmd();
    cmd.arg("-A").arg(temp_file.path());
    cmd.assert()
        .success()
        .stdout(predicate::str::contains(
            "<div align=\"center\">Centered</div>",
        ))
        .stdout(predicate::str::contains(
            "<span class=\"raw\">inline</span>",
        ));
}

#[test]
fn test_render_html_option_formats_raw_html() {
    let temp_file = NamedTempFile::new().unwrap();
    fs::write(
        &temp_file,
        "<head><title>Hidden</title></head><div align=\"center\"><strong>Centered</strong></div>\n\n<p>Clip <img src=\"photo.png\" alt=\"photo\"><img src=\"animation.gif\" alt=\"demo gif\"><video src=\"movie.mp4\" title=\"demo video\"></video><video><source src=\"trailer.webm\" title=\"source video\"></video></p>\n",
    )
    .unwrap();

    let output = mdv_cmd()
        .arg("-A")
        .arg("-c")
        .arg("40")
        .arg("-E")
        .arg(temp_file.path())
        .output()
        .unwrap();
    assert!(output.status.success());

    let stdout = String::from_utf8(output.stdout).unwrap();
    assert!(
        stdout.contains("                Centered"),
        "stdout:\n{}",
        stdout
    );
    assert!(stdout.contains("Clip [IMAGE] photo"), "stdout:\n{}", stdout);
    assert!(stdout.contains("[GIF] demo gif"), "stdout:\n{}", stdout);
    assert!(stdout.contains("[VIDEO] demo video"), "stdout:\n{}", stdout);
    assert!(
        stdout.contains("[VIDEO] source video"),
        "stdout:\n{}",
        stdout
    );
    assert!(!stdout.contains("Hidden"), "stdout:\n{}", stdout);
    assert!(!stdout.contains("<div"), "stdout:\n{}", stdout);
    assert!(!stdout.contains("align=\"center\""), "stdout:\n{}", stdout);
}

#[test]
fn test_render_html_buffers_centered_semantic_blocks() {
    let temp_file = NamedTempFile::new().unwrap();
    fs::write(
        &temp_file,
        r#"<section align="center">
  <img src="logo.png" alt="Logo">
</section>

<figure style="text-align:center">
  <a href="https://example.com/one"><img src="one.svg" alt="ONE"></a>
  <a href="https://example.com/two"><img src="two.svg" alt="TWO"></a>
</figure>
"#,
    )
    .unwrap();

    let output = mdv_cmd()
        .arg("-A")
        .arg("-c")
        .arg("80")
        .arg("-E")
        .arg(temp_file.path())
        .output()
        .unwrap();
    assert!(output.status.success());

    let stdout = String::from_utf8(output.stdout).unwrap();
    let image_lines: Vec<_> = stdout
        .lines()
        .filter(|line| line.contains("[IMAGE]") || line.contains("[SVG]"))
        .collect();
    assert_eq!(image_lines.len(), 2, "stdout:\n{}", stdout);
    assert!(
        image_lines[0].starts_with("                              "),
        "stdout:\n{}",
        stdout
    );
    assert!(
        image_lines[1].starts_with("                    "),
        "stdout:\n{}",
        stdout
    );
}

#[test]
fn test_render_html_right_aligns_regular_blocks() {
    let temp_file = NamedTempFile::new().unwrap();
    fs::write(
        &temp_file,
        r#"<div align="right">Right edge</div>
<section style="text-align:right">
  <span>CSS right</span>
</section>
"#,
    )
    .unwrap();

    let output = mdv_cmd()
        .arg("-A")
        .arg("-c")
        .arg("40")
        .arg("-E")
        .arg(temp_file.path())
        .output()
        .unwrap();
    assert!(output.status.success());

    let stdout = String::from_utf8(output.stdout).unwrap();
    assert!(
        stdout
            .lines()
            .any(|line| line == "                              Right edge"),
        "stdout:\n{}",
        stdout
    );
    assert!(
        stdout
            .lines()
            .any(|line| line == "                               CSS right"),
        "stdout:\n{}",
        stdout
    );
}

#[test]
fn test_render_html_formats_inline_semantic_tags() {
    let temp_file = NamedTempFile::new().unwrap();
    fs::write(
        &temp_file,
        r#"<p><code>cargo test</code> <kbd>Ctrl+C</kbd> <samp>ok</samp> <mark>marked</mark> <small>tiny</small> H<sub>2</sub> x<sup>2</sup> <abbr title="HyperText Markup Language">HTML</abbr></p>
"#,
    )
    .unwrap();

    let output = mdv_cmd()
        .arg("-A")
        .arg("-c")
        .arg("120")
        .arg("-E")
        .arg(temp_file.path())
        .output()
        .unwrap();
    assert!(output.status.success());

    let stdout = String::from_utf8(output.stdout).unwrap();
    assert!(
        strip_ansi(&stdout).contains(
            "`cargo test` [Ctrl+C] `ok` marked tiny H_2 x^2 HTML (HyperText Markup Language)"
        ),
        "stdout:\n{}",
        stdout
    );
}

#[test]
fn test_render_html_details_summary_static_output() {
    let temp_file = NamedTempFile::new().unwrap();
    fs::write(
        &temp_file,
        r#"<details>
  <summary>Install</summary>
  <p>Run <code>cargo install mdv</code>.</p>
</details>
"#,
    )
    .unwrap();

    let output = mdv_cmd()
        .arg("-A")
        .arg("-E")
        .arg(temp_file.path())
        .output()
        .unwrap();
    assert!(output.status.success());

    let stdout = String::from_utf8(output.stdout).unwrap();
    let clean = strip_ansi(&stdout);
    let summary_pos = clean.find("Install").expect("summary missing");
    let body_pos = clean
        .find("Run `cargo install mdv`.")
        .expect("details body missing");
    assert!(summary_pos < body_pos, "stdout:\n{}", stdout);
}

#[test]
fn test_render_html_preserves_pre_and_textarea_whitespace() {
    let temp_file = NamedTempFile::new().unwrap();
    fs::write(
        &temp_file,
        r#"<pre>
pre keeps spaces:
    indented line
        deeper line
</pre>
<textarea>
textarea keeps spaces:
    typed content
</textarea>
"#,
    )
    .unwrap();

    let output = mdv_cmd()
        .arg("-A")
        .arg("-E")
        .arg("-c")
        .arg("80")
        .arg(temp_file.path())
        .output()
        .unwrap();
    assert!(output.status.success());

    let clean = strip_ansi(&String::from_utf8(output.stdout).unwrap());
    assert!(
        clean.contains("pre keeps spaces:\n    indented line\n        deeper line"),
        "stdout:\n{}",
        clean
    );
    assert!(
        clean.contains("textarea keeps spaces:\n    typed content"),
        "stdout:\n{}",
        clean
    );
}

#[test]
fn test_render_html_blockquote_uses_quote_prefix() {
    let temp_file = NamedTempFile::new().unwrap();
    fs::write(
        &temp_file,
        r#"<blockquote>
  <p>Quote <strong>body</strong></p>
  <p>Second line</p>
</blockquote>
"#,
    )
    .unwrap();

    let output = mdv_cmd()
        .arg("-A")
        .arg("-E")
        .arg(temp_file.path())
        .output()
        .unwrap();
    assert!(output.status.success());

    let clean = strip_ansi(&String::from_utf8(output.stdout).unwrap());
    assert!(clean.contains("│ Quote body"), "stdout:\n{}", clean);
    assert!(clean.contains("│ Second line"), "stdout:\n{}", clean);
}

#[test]
fn test_render_html_definition_lists() {
    let temp_file = NamedTempFile::new().unwrap();
    fs::write(
        &temp_file,
        r#"<dl>
  <dt>Config</dt>
  <dd>Path to the config file.</dd>
  <dt>Theme</dt>
  <dd><a href="https://example.com/theme">Theme docs</a></dd>
</dl>
"#,
    )
    .unwrap();

    let output = mdv_cmd()
        .arg("-A")
        .arg("-E")
        .arg(temp_file.path())
        .output()
        .unwrap();
    assert!(output.status.success());

    let clean = strip_ansi(&String::from_utf8(output.stdout).unwrap());
    assert!(
        clean.contains("Config\n  Path to the config file."),
        "stdout:\n{}",
        clean
    );
    assert!(clean.contains("Theme\n  Theme docs"), "stdout:\n{}", clean);
}

#[test]
fn test_render_html_figure_caption_is_rendered_after_content() {
    let temp_file = NamedTempFile::new().unwrap();
    fs::write(
        &temp_file,
        r#"<figure align="center">
  <img src="overview.png" alt="Overview">
  <figcaption>Overview caption</figcaption>
</figure>
"#,
    )
    .unwrap();

    let output = mdv_cmd()
        .arg("-A")
        .arg("-E")
        .arg("-c")
        .arg("80")
        .arg(temp_file.path())
        .output()
        .unwrap();
    assert!(output.status.success());

    let clean = strip_ansi(&String::from_utf8(output.stdout).unwrap());
    let image_pos = clean
        .find("[IMAGE] Overview")
        .expect("figure image missing");
    let caption_pos = clean.find("Overview caption").expect("figcaption missing");
    assert!(image_pos < caption_pos, "stdout:\n{}", clean);
    assert!(
        clean
            .lines()
            .any(|line| line.trim() == "Overview caption" && line.starts_with(" ")),
        "stdout:\n{}",
        clean
    );
}

#[test]
fn test_render_html_basic_inline_css_styles() {
    let temp_file = NamedTempFile::new().unwrap();
    fs::write(
        &temp_file,
        r#"<p><span style="font-weight:bold">bold css</span> <span style="font-style:italic">italic css</span> <span style="text-decoration:line-through">strike css</span> <span style="text-decoration:underline">underlined css</span></p>
"#,
    )
    .unwrap();

    let output = mdv_cmd()
        .arg("-E")
        .arg("-c")
        .arg("120")
        .arg(temp_file.path())
        .output()
        .unwrap();
    assert!(output.status.success());

    let stdout = String::from_utf8(output.stdout).unwrap();
    let clean = strip_ansi(&stdout);
    assert!(
        clean.contains("bold css italic css strike css underlined css"),
        "stdout:\n{}",
        stdout
    );
    assert!(stdout.contains("\u{1b}[1m"), "stdout:\n{}", stdout);
    assert!(stdout.contains("\u{1b}[3m"), "stdout:\n{}", stdout);
    assert!(stdout.contains("\u{1b}[9m"), "stdout:\n{}", stdout);
    assert!(stdout.contains("\u{1b}[4m"), "stdout:\n{}", stdout);
}

#[test]
fn test_render_html_inline_table_references_inside_html_containers() {
    let temp_file = NamedTempFile::new().unwrap();
    fs::write(
        &temp_file,
        r#"<details>
  <summary><a href="https://example.com/details">Details docs</a></summary>
  <p><img src="details.png" alt="Details image"></p>
</details>

<figure>
  <a href="https://example.com/figure"><img src="figure.png" alt="Figure image"></a>
  <figcaption><a href="https://example.com/caption">Caption docs</a></figcaption>
</figure>

<blockquote>
  <p><a href="https://example.com/quote">Quote link</a> <a href="https://example.com/quote-image"><img src="quote.png" alt="Quote image"></a></p>
</blockquote>
"#,
    )
    .unwrap();

    let output = mdv_cmd()
        .arg("-A")
        .arg("-E")
        .arg("-u")
        .arg("inlinetable")
        .arg(temp_file.path())
        .output()
        .unwrap();
    assert!(output.status.success());

    let clean = strip_ansi(&String::from_utf8(output.stdout).unwrap());
    for expected in [
        "Details docs[",
        "[IMAGE] Details image",
        "[IMAGE] Figure image[",
        "Caption docs[",
        "Quote link[",
        "[IMAGE] Quote image[",
        "https://example.com/details",
        "https://example.com/figure",
        "https://example.com/caption",
        "https://example.com/quote",
        "https://example.com/quote-image",
    ] {
        assert!(
            clean.contains(expected),
            "missing {expected}; stdout:\n{}",
            clean
        );
    }
}

#[test]
fn test_render_html_inline_table_references_reset_across_blocks() {
    let temp_file = NamedTempFile::new().unwrap();
    fs::write(
        &temp_file,
        r#"Markdown para with [a link](https://example.com/md-link).

<p align="center">
  © 2026-present <a href="https://example.com/user">User</a>
</p>

<div align="center">
  <a href="https://example.com/license"><img src="badge.svg" alt="LICENSE"></a>
</div>
"#,
    )
    .unwrap();

    let output = mdv_cmd()
        .arg("-A")
        .arg("-E")
        .arg("-u")
        .arg("inlinetable")
        .arg(temp_file.path())
        .output()
        .unwrap();
    assert!(output.status.success());

    let clean = strip_ansi(&String::from_utf8(output.stdout).unwrap());
    assert!(
        clean.contains("a link[1]"),
        "expected markdown paragraph link [1]; stdout:\n{}",
        clean
    );
    assert!(
        clean.contains("User[1]"),
        "expected HTML block link [1] after markdown paragraph; stdout:\n{}",
        clean
    );
    assert!(
        clean.contains("[SVG] LICENSE[1]"),
        "expected second HTML block link [1], not sequential [2]; stdout:\n{}",
        clean
    );
    assert!(
        !clean.contains("[2]"),
        "sequential numbering across blocks is a bug; stdout:\n{}",
        clean
    );
}

#[test]
fn test_render_html_ordered_list_attributes() {
    let temp_file = NamedTempFile::new().unwrap();
    fs::write(
        &temp_file,
        r#"<ol start="5">
  <li>Five</li>
  <li>Six</li>
</ol>
<ol start="3" reversed>
  <li>Three</li>
  <li>Two</li>
  <li>One</li>
</ol>
<ol type="a">
  <li>Alpha</li>
  <li>Beta</li>
</ol>
<ol type="A" start="27">
  <li>Upper alpha</li>
</ol>
<ol type="i" start="4">
  <li>Lower roman</li>
</ol>
<ol type="I" start="9">
  <li>Upper roman</li>
</ol>
<ol>
  <li value="4">Explicit value</li>
  <li>After explicit value</li>
</ol>
"#,
    )
    .unwrap();

    let output = mdv_cmd()
        .arg("-A")
        .arg("-E")
        .arg(temp_file.path())
        .output()
        .unwrap();
    assert!(output.status.success());

    let clean = strip_ansi(&String::from_utf8(output.stdout).unwrap());
    for expected in [
        "5. Five",
        "6. Six",
        "3. Three",
        "2. Two",
        "1. One",
        "a. Alpha",
        "b. Beta",
        "AA. Upper alpha",
        "iv. Lower roman",
        "IX. Upper roman",
        "4. Explicit value",
        "5. After explicit value",
    ] {
        assert!(
            clean.contains(expected),
            "missing {expected}; stdout:\n{}",
            clean
        );
    }
}

#[test]
fn test_render_html_unordered_list_type_markers() {
    let temp_file = NamedTempFile::new().unwrap();
    fs::write(
        &temp_file,
        r#"<ul type="circle"><li>Circle</li></ul>
<ul type="square"><li>Square</li></ul>
<ul type="disc"><li>Disc</li></ul>
<ul><li>Default</li></ul>
"#,
    )
    .unwrap();

    let output = mdv_cmd()
        .arg("-A")
        .arg("-E")
        .arg(temp_file.path())
        .output()
        .unwrap();
    assert!(output.status.success());

    let clean = strip_ansi(&String::from_utf8(output.stdout).unwrap());
    assert!(clean.contains("â—¦ Circle"), "stdout:\n{}", clean);
    assert!(clean.contains("â–ª Square"), "stdout:\n{}", clean);
    assert!(clean.contains("• Disc"), "stdout:\n{}", clean);
    assert!(clean.contains("- Default"), "stdout:\n{}", clean);
}

#[test]
fn test_render_html_option_formats_html_tables() {
    let temp_file = NamedTempFile::new().unwrap();
    fs::write(
        &temp_file,
        r#"<table>
  <thead>
    <tr>
      <th align="left">Project</th>
      <th style="text-align:center">Status</th>
      <th align="right">Asset</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td><strong>Alpha</strong></td>
      <td><a href="https://example.com/ready">Ready</a></td>
      <td><img src="logo.gif" alt="Logo"></td>
    </tr>
    <tr>
      <td>Beta</td>
      <td>Blocked</td>
      <td><video src="demo.mp4" title="Demo"></video></td>
    </tr>
  </tbody>
</table>
"#,
    )
    .unwrap();

    let output = mdv_cmd()
        .arg("-A")
        .arg("-c")
        .arg("80")
        .arg("--render-html")
        .arg(temp_file.path())
        .output()
        .unwrap();
    assert!(output.status.success());

    let stdout = String::from_utf8(output.stdout).unwrap();
    assert!(stdout.contains("Project"), "stdout:\n{}", stdout);
    assert!(stdout.contains("Status"), "stdout:\n{}", stdout);
    assert!(stdout.contains("Alpha"), "stdout:\n{}", stdout);
    assert!(stdout.contains("Ready"), "stdout:\n{}", stdout);
    assert!(stdout.contains("[GIF] Logo"), "stdout:\n{}", stdout);
    assert!(stdout.contains("[VIDEO] Demo"), "stdout:\n{}", stdout);
    assert!(stdout.contains("│ Project"), "stdout:\n{}", stdout);
    assert!(stdout.contains("│ Alpha"), "stdout:\n{}", stdout);
    assert!(!stdout.contains("<table"), "stdout:\n{}", stdout);
    assert!(!stdout.contains("<td"), "stdout:\n{}", stdout);
}

#[test]
fn test_comments_wrap_to_column_width() {
    for wrap_mode in ["char", "word"] {
        let temp_file = NamedTempFile::new().unwrap();
        fs::write(
            &temp_file,
            "# Title\n<!-- This file demonstrates a wide variety of Markdown capabilities, including formatting, tables, links, media, and references. -->\n",
        )
        .unwrap();

        let output = mdv_cmd()
            .arg("-A")
            .arg("-c")
            .arg("40")
            .arg("-W")
            .arg(wrap_mode)
            .arg(temp_file.path())
            .output()
            .unwrap();
        assert!(output.status.success());

        let stdout = String::from_utf8(output.stdout).unwrap();
        let clean = strip_ansi(&stdout);
        assert!(clean.contains("<!-- This file"), "stdout:\n{}", stdout);
        assert!(
            clean.lines().all(|line| line.chars().count() <= 40),
            "wrap_mode={wrap_mode}, stdout:\n{}",
            stdout
        );
    }
}

#[test]
fn test_hide_comments_option_hides_comments() {
    let temp_file = NamedTempFile::new().unwrap();
    fs::write(&temp_file, "<!-- secret -->\n\nVisible text\n").unwrap();

    let mut cmd = mdv_cmd();
    cmd.arg("--hide-comments").arg("-A").arg(temp_file.path());
    cmd.assert()
        .success()
        .stdout(predicate::str::contains("<!-- secret -->").not())
        .stdout(predicate::str::contains("Visible text"));
}

#[test]
fn test_column_width_option() {
    let temp_file = NamedTempFile::new().unwrap();
    fs::write(&temp_file, "# Width Test\n\nThis is a long line that should be wrapped according to the specified column width.").unwrap();

    let mut cmd = mdv_cmd();
    cmd.arg("-c").arg("40").arg(temp_file.path());
    cmd.assert()
        .success()
        .stdout(predicate::str::contains("Width Test"));
}

#[test]
fn test_word_wrap_list_inline_code_does_not_hang() {
    let temp_file = NamedTempFile::new().unwrap();
    fs::write(
        &temp_file,
        "- Original: `C:\\Users\\VeryLongFolderNameThatExceedsLimit\\Documents\\Projects\\MyProject`",
    )
    .unwrap();

    let mut cmd = mdv_cmd();
    cmd.arg("-A")
        .arg("--wrap")
        .arg("word")
        .arg("-c")
        .arg("75")
        .arg(temp_file.path());

    cmd.assert()
        .success()
        .stdout(predicate::str::contains("- Original:"))
        .stdout(predicate::str::contains(
            "VeryLongFolderNameThatExceedsLimit",
        ))
        .stdout(predicate::str::contains("\n  `"));
}

#[test]
fn test_reverse_option_preserves_block_layout() {
    let temp_file = NamedTempFile::new().unwrap();
    fs::write(
        &temp_file,
        "# First Heading\n\nParagraph one continues here.\n\n## Second Heading\n\nParagraph two comes last.",
    )
    .unwrap();

    let output = mdv_cmd()
        .arg("-A")
        .arg("-r")
        .arg(temp_file.path())
        .output()
        .unwrap();

    assert!(output.status.success());
    let stdout = String::from_utf8(output.stdout).unwrap();

    let second_pos = stdout
        .find("Second Heading")
        .expect("second heading missing in output");
    let first_pos = stdout
        .find("First Heading")
        .expect("first heading missing in output");

    assert!(
        second_pos < first_pos,
        "expected second heading to appear before first heading in reverse output"
    );
    assert!(
        stdout.contains("Paragraph two comes last."),
        "expected concluding paragraph to appear intact"
    );
}

#[test]
fn test_nonexistent_file() {
    let mut cmd = mdv_cmd();
    cmd.arg("nonexistent_file.md");
    cmd.assert()
        .failure()
        .stderr(predicate::str::contains("File not found"));
}

#[test]
fn test_from_text_option() {
    let temp_file = NamedTempFile::new().unwrap();
    fs::write(&temp_file, "# Start\n\nSome content.\n\n## Target Section\n\nThis is the target.\n\n## End\n\nMore content.").unwrap();

    let mut cmd = mdv_cmd();
    cmd.arg("-f").arg("Target Section").arg(temp_file.path());
    cmd.assert()
        .success()
        .stdout(predicate::str::contains("Target Section"));
}

#[test]
fn test_tab_length_option() {
    let temp_file = NamedTempFile::new().unwrap();
    fs::write(&temp_file, "# Tab Test\n\n\tIndented with tab").unwrap();

    let mut cmd = mdv_cmd();
    cmd.arg("-b").arg("8").arg(temp_file.path());
    cmd.assert()
        .success()
        .stdout(predicate::str::contains("Tab Test"));
}

#[test]
fn test_theme_info_without_file_lists_available_themes() {
    let mut cmd = mdv_cmd();
    cmd.arg("--theme").arg("terminal");
    cmd.arg("--theme-info");
    cmd.assert()
        .success()
        .stdout(predicate::str::contains("\nCurrent theme: terminal"))
        .stdout(predicate::str::contains("\nCurrent code theme: terminal"))
        .stdout(predicate::str::contains("Available themes:"));
}

#[test]
fn test_theme_info_with_file_outputs_file_contents() {
    let temp_file = NamedTempFile::new().unwrap();
    fs::write(&temp_file, "custom theme info").unwrap();

    let mut cmd = mdv_cmd();
    cmd.arg("--theme").arg("terminal");
    cmd.arg("--theme-info").arg(temp_file.path());
    cmd.assert()
        .success()
        .stdout(predicate::str::contains("\nCurrent theme: terminal"))
        .stdout(predicate::str::contains("\nCurrent code theme: terminal"))
        .stdout(predicate::str::contains("custom theme info"))
        .stdout(predicate::str::contains("Available themes").not());
}

#[test]
fn test_theme_info_from_config_prints_current_theme() {
    let config_dir = TempDir::new().unwrap();
    let config_path = config_dir.path().join("config.yaml");
    fs::write(&config_path, "theme_info: true\n").unwrap();

    let temp_file = NamedTempFile::new().unwrap();
    fs::write(&temp_file, "# Config Theme Info\n").unwrap();

    let mut cmd = mdv_cmd();
    cmd.arg("--config-file").arg(config_dir.path());
    cmd.arg(temp_file.path());
    cmd.assert()
        .success()
        .stdout(predicate::str::contains("\nCurrent theme: terminal"))
        .stdout(predicate::str::contains("\nCurrent code theme: terminal"))
        .stdout(predicate::str::contains("Available themes").not());
}

#[test]
fn test_text_highlight_background() {
    let temp_file = NamedTempFile::new().unwrap();
    fs::write(&temp_file, "Normal ==highlighted text== end.").unwrap();

    let output = mdv_cmd()
        .arg("-c")
        .arg("80")
        .arg(temp_file.path())
        .output()
        .unwrap();

    assert!(output.status.success());

    let stdout = String::from_utf8_lossy(&output.stdout);
    let clean = strip_ansi(&stdout);
    assert!(clean.contains("highlighted text"));
    assert!(!clean.contains("==highlighted text=="));
    assert!(stdout.contains("\u{1b}[48;"));
}

#[test]
fn test_init_config_creates_config_file_from_path() {
    let temp_dir = TempDir::new().unwrap();
    let config_path = temp_dir.path().join("config.yaml");

    let output = mdv_cmd()
        .arg("--init-config")
        .arg(temp_dir.path())
        .output()
        .unwrap();

    assert!(output.status.success());
    assert!(config_path.exists());

    let stdout = String::from_utf8(output.stdout).unwrap();
    assert!(stdout.contains(&config_path.display().to_string()));

    let config = fs::read_to_string(&config_path).unwrap();
    assert!(config.contains("theme: \"terminal\""));
    assert!(config.contains("link_style: \"clickable\""));
}

#[test]
fn test_init_config_creates_config_file_in_current_directory() {
    let temp_dir = TempDir::new().unwrap();
    let config_path = temp_dir.path().join("config.yaml");

    let output = mdv_cmd()
        .arg("--init-config")
        .arg(".")
        .current_dir(&temp_dir)
        .output()
        .unwrap();

    assert!(output.status.success());
    assert!(config_path.exists());
}

#[test]
fn test_init_config_creates_config_file_from_config_file_arg() {
    let temp_dir = TempDir::new().unwrap();
    let config_path = temp_dir.path().join("nested").join("config.yaml");

    let output = mdv_cmd()
        .arg("--init-config")
        .arg("--config-file")
        .arg(temp_dir.path().join("nested"))
        .output()
        .unwrap();

    assert!(output.status.success());
    assert!(config_path.exists());
}

#[test]
fn test_init_config_refuses_to_overwrite_existing_file() {
    let temp_dir = TempDir::new().unwrap();
    let config_path = temp_dir.path().join("config.yaml");
    fs::write(&config_path, "theme: \"monokai\"\n").unwrap();

    let output = mdv_cmd()
        .arg("--init-config")
        .arg(temp_dir.path())
        .output()
        .unwrap();

    assert!(!output.status.success());

    let stderr = String::from_utf8(output.stderr).unwrap();
    assert!(
        stderr.contains("Config file already exists"),
        "stderr:\n{}",
        stderr
    );
    assert_eq!(
        fs::read_to_string(&config_path).unwrap(),
        "theme: \"monokai\"\n"
    );
}

#[test]
fn test_init_config_uses_env_config_path() {
    let temp_dir = TempDir::new().unwrap();
    let config_path = temp_dir.path().join("config.yaml");

    let output = mdv_cmd()
        .arg("--init-config")
        .env("MDV_CONFIG_PATH", temp_dir.path())
        .output()
        .unwrap();

    assert!(output.status.success());
    assert!(config_path.exists());

    let stdout = String::from_utf8(output.stdout).unwrap();
    assert!(stdout.contains(&config_path.display().to_string()));
}

#[test]
fn test_init_config_positional_path_overrides_config_file_arg() {
    let temp_dir = TempDir::new().unwrap();
    let config_file_path = temp_dir.path().join("config-file");
    let positional_path = temp_dir.path().join("positional");
    let positional_config = positional_path.join("config.yaml");

    let output = mdv_cmd()
        .arg("--init-config")
        .arg(&positional_path)
        .arg("--config-file")
        .arg(&config_file_path)
        .output()
        .unwrap();

    assert!(output.status.success());
    assert!(positional_config.exists());
    assert!(!config_file_path.join("config.yaml").exists());
}

#[test]
fn test_pager_mode_prints_to_stdout_when_output_is_not_a_terminal() {
    let temp_file = NamedTempFile::new().unwrap();
    fs::write(&temp_file, "# Pager output\n").unwrap();

    let output = mdv_cmd()
        .arg("--pager")
        .arg(temp_file.path())
        .output()
        .unwrap();

    assert!(output.status.success());

    let stdout = String::from_utf8(output.stdout).unwrap();
    let clean = strip_ansi(&stdout);
    assert!(clean.contains("Pager output"), "stdout:\n{}", stdout);
}

#[test]
fn test_interactive_flag_is_rejected() {
    mdv_cmd()
        .arg("--interactive")
        .assert()
        .failure()
        .stderr(predicate::str::contains(
            "unexpected argument '--interactive'",
        ));
}