src2md 0.1.8

Turn source code into a Markdown document with syntax highlighting, or extract it back.
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
#[cfg(feature = "restore")]
use src2md::extract_from_markdown;
use src2md::{Config, OUTPUT_MAGIC_HEADER, run_src2md};
use std::collections::HashSet;
use std::fs::File;
use std::io::Write;
use tempfile::tempdir;
use tokio::fs;

/// Creates a basic Config for testing.
fn test_config(output_path: std::path::PathBuf, project_root: std::path::PathBuf) -> Config {
    Config {
        output_path,
        ignore_file: None,
        specific_paths: HashSet::new(),
        project_root,
        #[cfg(feature = "restore")]
        restore_input: None,
        #[cfg(feature = "restore")]
        restore_path: None,
        verbosity: 0,
        fail_fast: true,
        extensions: HashSet::new(),
        #[cfg(feature = "git")]
        git_url: None,
        #[cfg(feature = "git")]
        git_branch: None,
        #[cfg(feature = "mdbook")]
        mdbook_output: None,
    }
}

/// Creates a Config with specific paths.
fn test_config_with_paths(
    output_path: std::path::PathBuf,
    project_root: std::path::PathBuf,
    specific_paths: HashSet<std::path::PathBuf>,
) -> Config {
    Config {
        output_path,
        ignore_file: None,
        specific_paths,
        project_root,
        #[cfg(feature = "restore")]
        restore_input: None,
        #[cfg(feature = "restore")]
        restore_path: None,
        verbosity: 0,
        fail_fast: true,
        extensions: HashSet::new(),
        #[cfg(feature = "git")]
        git_url: None,
        #[cfg(feature = "git")]
        git_branch: None,
        #[cfg(feature = "mdbook")]
        mdbook_output: None,
    }
}

/// Creates a Config with extension filtering.
fn test_config_with_extensions(
    output_path: std::path::PathBuf,
    project_root: std::path::PathBuf,
    extensions: HashSet<String>,
) -> Config {
    Config {
        output_path,
        ignore_file: None,
        specific_paths: HashSet::new(),
        project_root,
        #[cfg(feature = "restore")]
        restore_input: None,
        #[cfg(feature = "restore")]
        restore_path: None,
        verbosity: 0,
        fail_fast: true,
        extensions,
        #[cfg(feature = "git")]
        git_url: None,
        #[cfg(feature = "git")]
        git_branch: None,
        #[cfg(feature = "mdbook")]
        mdbook_output: None,
    }
}

#[tokio::test]
async fn it_generates_markdown_output() -> anyhow::Result<()> {
    let temp_dir = tempdir()?;
    let root_path = temp_dir.path().to_path_buf();
    let src_file_path = root_path.join("example.rs");

    let mut file = File::create(&src_file_path)?;
    writeln!(file, "fn main() {{ println!(\"Hello, world!\"); }}")?;

    let output_path = root_path.join("output.md");
    let config = test_config(output_path.clone(), root_path);

    run_src2md(config).await?;

    let contents = fs::read_to_string(&output_path).await?;

    // Should have magic header
    assert!(contents.starts_with(OUTPUT_MAGIC_HEADER));

    // Should have the code
    assert!(contents.contains("```rust"));
    assert!(contents.contains("fn main()"));

    Ok(())
}

#[tokio::test]
async fn it_handles_files_with_backticks() -> anyhow::Result<()> {
    let temp_dir = tempdir()?;
    let root_path = temp_dir.path().to_path_buf();
    let md_file_path = root_path.join("README.md");

    // Create a markdown file that contains triple backticks
    let mut file = File::create(&md_file_path)?;
    writeln!(file, "# Example\n\n```rust\nfn main() {{}}\n```")?;

    let output_path = root_path.join("output.md");
    let config = test_config(output_path.clone(), root_path);

    run_src2md(config).await?;

    let contents = fs::read_to_string(&output_path).await?;
    // Should use 4 backticks to wrap content with 3 backticks
    assert!(contents.contains("````"));

    Ok(())
}

#[cfg(feature = "restore")]
#[tokio::test]
async fn it_roundtrips_files() -> anyhow::Result<()> {
    let temp_dir = tempdir()?;
    let root_path = temp_dir.path().to_path_buf();

    // Create source files
    let src_dir = root_path.join("src");
    std::fs::create_dir_all(&src_dir)?;

    let main_content = "fn main() {\n    println!(\"Hello!\");\n}";
    std::fs::write(src_dir.join("main.rs"), main_content)?;

    let lib_content = "pub fn add(a: i32, b: i32) -> i32 {\n    a + b\n}";
    std::fs::write(src_dir.join("lib.rs"), lib_content)?;

    // Generate markdown
    let output_path = root_path.join("output.md");
    let config = test_config(output_path.clone(), root_path.clone());

    run_src2md(config).await?;

    // Extract to a new directory
    let extract_dir = root_path.join("extracted");
    extract_from_markdown(&output_path, Some(&extract_dir)).await?;

    // Verify roundtrip
    let extracted_main = fs::read_to_string(extract_dir.join("src/main.rs")).await?;
    assert_eq!(extracted_main, main_content);

    let extracted_lib = fs::read_to_string(extract_dir.join("src/lib.rs")).await?;
    assert_eq!(extracted_lib, lib_content);

    Ok(())
}

#[tokio::test]
async fn it_handles_specific_paths() -> anyhow::Result<()> {
    let temp_dir = tempdir()?;
    let root_path = temp_dir.path().to_path_buf();

    // Create multiple files
    std::fs::write(root_path.join("included.rs"), "// included")?;
    std::fs::write(root_path.join("excluded.rs"), "// excluded")?;

    let output_path = root_path.join("output.md");

    let mut specific_paths = HashSet::new();
    specific_paths.insert(root_path.join("included.rs"));

    let config = test_config_with_paths(output_path.clone(), root_path, specific_paths);

    run_src2md(config).await?;

    let contents = fs::read_to_string(&output_path).await?;
    assert!(contents.contains("included.rs"));
    assert!(!contents.contains("excluded.rs"));

    Ok(())
}

#[tokio::test]
async fn it_marks_binary_files() -> anyhow::Result<()> {
    let temp_dir = tempdir()?;
    let root_path = temp_dir.path().to_path_buf();

    // Create a binary file (just some non-UTF8 bytes)
    let binary_content: Vec<u8> = vec![0x00, 0x01, 0x02, 0xFF, 0xFE, 0x89, 0x50, 0x4E, 0x47];
    std::fs::write(root_path.join("binary.bin"), &binary_content)?;

    // Create a text file too
    std::fs::write(root_path.join("text.txt"), "Hello, world!")?;

    let output_path = root_path.join("output.md");
    let config = test_config(output_path.clone(), root_path);

    run_src2md(config).await?;

    let contents = fs::read_to_string(&output_path).await?;
    assert!(contents.contains("binary.bin"));
    assert!(contents.contains("(binary file omitted)"));
    assert!(contents.contains("Hello, world!"));

    Ok(())
}

#[tokio::test]
async fn it_excludes_previous_outputs() -> anyhow::Result<()> {
    let temp_dir = tempdir()?;
    let root_path = temp_dir.path().to_path_buf();

    // Create a source file
    std::fs::write(root_path.join("source.rs"), "// source code")?;

    // Create a previous src2md output file
    let previous_output = format!(
        "{}## old/file.rs\n\n```rust\n// old\n```\n",
        OUTPUT_MAGIC_HEADER
    );
    std::fs::write(root_path.join("previous_output.md"), previous_output)?;

    let output_path = root_path.join("new_output.md");
    let config = test_config(output_path.clone(), root_path);

    run_src2md(config).await?;

    let contents = fs::read_to_string(&output_path).await?;

    // Should contain source.rs
    assert!(contents.contains("source.rs"));
    assert!(contents.contains("// source code"));

    // Should NOT contain the previous output
    assert!(!contents.contains("previous_output.md"));
    assert!(!contents.contains("old/file.rs"));

    Ok(())
}

#[tokio::test]
async fn it_excludes_nested_outputs() -> anyhow::Result<()> {
    let temp_dir = tempdir()?;
    let root_path = temp_dir.path().to_path_buf();

    // Create a source file
    std::fs::write(root_path.join("source.rs"), "// source code")?;

    // Create a nested directory with a src2md output
    let docs_dir = root_path.join("docs");
    std::fs::create_dir_all(&docs_dir)?;
    std::fs::write(docs_dir.join("readme.md"), "# Normal readme")?;

    let nested_output = format!(
        "{}## nested/file.rs\n\n```rust\n// nested\n```\n",
        OUTPUT_MAGIC_HEADER
    );
    std::fs::write(docs_dir.join("generated.md"), nested_output)?;

    let output_path = root_path.join("output.md");
    let config = test_config(output_path.clone(), root_path);

    run_src2md(config).await?;

    let contents = fs::read_to_string(&output_path).await?;

    // Should contain source.rs and readme.md
    assert!(contents.contains("source.rs"));
    assert!(contents.contains("readme.md"));
    assert!(contents.contains("# Normal readme"));

    // Should NOT contain the nested output
    assert!(!contents.contains("generated.md"));
    assert!(!contents.contains("nested/file.rs"));

    Ok(())
}

#[tokio::test]
async fn it_can_rerun_in_same_directory() -> anyhow::Result<()> {
    let temp_dir = tempdir()?;
    let root_path = temp_dir.path().to_path_buf();

    // Create a source file
    std::fs::write(root_path.join("source.rs"), "// version 1")?;

    let output_path = root_path.join("output.md");

    // First run
    let config = test_config(output_path.clone(), root_path.clone());
    run_src2md(config).await?;

    let first_contents = fs::read_to_string(&output_path).await?;
    assert!(first_contents.contains("// version 1"));

    // Update source file
    std::fs::write(root_path.join("source.rs"), "// version 2")?;

    // Second run - should NOT include the previous output
    let config = test_config(output_path.clone(), root_path);
    run_src2md(config).await?;

    let second_contents = fs::read_to_string(&output_path).await?;

    // Should have new version
    assert!(second_contents.contains("// version 2"));

    // Should NOT have old version (would be there if output was included)
    assert!(!second_contents.contains("// version 1"));

    // Should NOT include itself
    assert!(!second_contents.contains("output.md"));

    Ok(())
}

#[tokio::test]
async fn it_excludes_output_being_written() -> anyhow::Result<()> {
    let temp_dir = tempdir()?;
    let root_path = temp_dir.path().to_path_buf();

    // Create a source file
    std::fs::write(root_path.join("source.rs"), "// source")?;

    // Create an existing output file at the target path (simulating overwrite)
    let output_path = root_path.join("output.md");
    std::fs::write(&output_path, "# Previous content")?;

    let config = test_config(output_path.clone(), root_path);
    run_src2md(config).await?;

    let contents = fs::read_to_string(&output_path).await?;

    // Should have new content
    assert!(contents.starts_with(OUTPUT_MAGIC_HEADER));
    assert!(contents.contains("source.rs"));

    // Should NOT include the output file itself
    assert!(!contents.contains("output.md"));
    assert!(!contents.contains("# Previous content"));

    Ok(())
}

#[tokio::test]
async fn it_excludes_lock_files() -> anyhow::Result<()> {
    let temp_dir = tempdir()?;
    let root_path = temp_dir.path().to_path_buf();

    // Create source files
    std::fs::write(root_path.join("main.rs"), "// main")?;
    std::fs::write(root_path.join("package.json"), r#"{"name": "test"}"#)?;

    // Create lock files that should be excluded
    std::fs::write(root_path.join("package-lock.json"), "{}")?;
    std::fs::write(root_path.join("yarn.lock"), "")?;
    std::fs::write(root_path.join("Cargo.lock"), "")?;
    std::fs::write(root_path.join("pnpm-lock.yaml"), "")?;
    std::fs::write(root_path.join("custom.lock"), "")?;

    let output_path = root_path.join("output.md");
    let config = test_config(output_path.clone(), root_path);

    run_src2md(config).await?;

    let contents = fs::read_to_string(&output_path).await?;

    // Should include source files
    assert!(contents.contains("main.rs"));
    assert!(contents.contains("package.json"));

    // Should NOT include lock files
    assert!(!contents.contains("package-lock.json"));
    assert!(!contents.contains("yarn.lock"));
    assert!(!contents.contains("Cargo.lock"));
    assert!(!contents.contains("pnpm-lock.yaml"));
    assert!(!contents.contains("custom.lock"));

    Ok(())
}

#[tokio::test]
async fn it_excludes_hidden_files_and_directories() -> anyhow::Result<()> {
    let temp_dir = tempdir()?;
    let root_path = temp_dir.path().to_path_buf();

    // Create visible files
    std::fs::write(root_path.join("visible.rs"), "// visible")?;

    // Create hidden files
    std::fs::write(root_path.join(".hidden"), "secret")?;
    std::fs::write(root_path.join(".env"), "SECRET=value")?;

    // Create hidden directory with files
    let hidden_dir = root_path.join(".git");
    std::fs::create_dir_all(&hidden_dir)?;
    std::fs::write(hidden_dir.join("config"), "[core]")?;

    // Create nested hidden directory
    let nested_hidden = root_path.join("src/.hidden");
    std::fs::create_dir_all(&nested_hidden)?;
    std::fs::write(nested_hidden.join("secret.rs"), "// secret")?;

    let output_path = root_path.join("output.md");
    let config = test_config(output_path.clone(), root_path);

    run_src2md(config).await?;

    let contents = fs::read_to_string(&output_path).await?;

    // Should include visible files
    assert!(contents.contains("visible.rs"));

    // Should NOT include hidden files or files in hidden directories
    assert!(!contents.contains(".hidden"));
    assert!(!contents.contains(".env"));
    assert!(!contents.contains(".git"));
    assert!(!contents.contains("config"));
    assert!(!contents.contains("secret"));

    Ok(())
}

#[tokio::test]
async fn it_filters_by_extension() -> anyhow::Result<()> {
    let temp_dir = tempdir()?;
    let root_path = temp_dir.path().to_path_buf();

    // Create files with different extensions
    std::fs::write(root_path.join("main.rs"), "// rust")?;
    std::fs::write(root_path.join("app.ts"), "// typescript")?;
    std::fs::write(root_path.join("index.js"), "// javascript")?;
    std::fs::write(root_path.join("style.css"), "/* css */")?;
    std::fs::write(root_path.join("readme.md"), "# readme")?;

    let output_path = root_path.join("output.md");

    let mut extensions = HashSet::new();
    extensions.insert("rs".to_string());
    extensions.insert("ts".to_string());

    let config = test_config_with_extensions(output_path.clone(), root_path, extensions);

    run_src2md(config).await?;

    let contents = fs::read_to_string(&output_path).await?;

    // Should include only .rs and .ts files
    assert!(contents.contains("main.rs"));
    assert!(contents.contains("// rust"));
    assert!(contents.contains("app.ts"));
    assert!(contents.contains("// typescript"));

    // Should NOT include other files
    assert!(!contents.contains("index.js"));
    assert!(!contents.contains("style.css"));
    assert!(!contents.contains("readme.md"));

    Ok(())
}

#[tokio::test]
async fn it_filters_extension_case_insensitive() -> anyhow::Result<()> {
    let temp_dir = tempdir()?;
    let root_path = temp_dir.path().to_path_buf();

    // Create files with mixed-case extensions
    std::fs::write(root_path.join("file1.RS"), "// uppercase")?;
    std::fs::write(root_path.join("file2.Rs"), "// mixed")?;
    std::fs::write(root_path.join("file3.rs"), "// lowercase")?;

    let output_path = root_path.join("output.md");

    let mut extensions = HashSet::new();
    extensions.insert("rs".to_string());

    let config = test_config_with_extensions(output_path.clone(), root_path, extensions);

    run_src2md(config).await?;

    let contents = fs::read_to_string(&output_path).await?;

    // Should include all .rs files regardless of case
    assert!(contents.contains("// uppercase"));
    assert!(contents.contains("// mixed"));
    assert!(contents.contains("// lowercase"));

    Ok(())
}

// Git feature tests (only compiled when git feature is enabled)
#[cfg(feature = "git")]
mod git_tests {
    use src2md::git::repo_name_from_url;

    #[test]
    fn test_repo_name_extraction() {
        assert_eq!(
            repo_name_from_url("https://github.com/user/repo.git"),
            Some("repo".to_string())
        );
        assert_eq!(
            repo_name_from_url("https://github.com/user/repo"),
            Some("repo".to_string())
        );
        assert_eq!(
            repo_name_from_url("git@github.com:user/repo.git"),
            Some("repo".to_string())
        );
    }
}

// mdbook feature tests (only compiled when mdbook feature is enabled)
#[cfg(feature = "mdbook")]
mod mdbook_tests {
    use src2md::filewalker::collect_files;
    use src2md::generate_mdbook;
    use std::collections::HashSet;
    use tempfile::tempdir;
    use tokio::fs;

    #[tokio::test]
    async fn it_generates_mdbook_structure() -> anyhow::Result<()> {
        let temp_dir = tempdir()?;
        let root_path = temp_dir.path().to_path_buf();

        // Create source files
        std::fs::write(root_path.join("README.md"), "# Project README")?;

        let src_dir = root_path.join("src");
        std::fs::create_dir_all(&src_dir)?;
        std::fs::write(src_dir.join("main.rs"), "fn main() {}")?;
        std::fs::write(src_dir.join("lib.rs"), "pub fn hello() {}")?;

        // Output directory
        let output_dir = root_path.join("book");

        // Collect files and generate mdbook
        let entries = collect_files(&root_path, None, &HashSet::new(), None, &HashSet::new())?;
        generate_mdbook(&entries, &root_path, &output_dir).await?;

        // Verify SUMMARY.md exists
        let summary_path = output_dir.join("SUMMARY.md");
        assert!(summary_path.exists(), "SUMMARY.md should exist");

        let summary = fs::read_to_string(&summary_path).await?;
        assert!(summary.contains("# Summary"), "Should have Summary header");
        assert!(
            summary.contains("[Introduction]"),
            "Should have Introduction link"
        );
        assert!(summary.contains("[src]"), "Should have src chapter");

        // Verify introduction.md (root files)
        let intro_path = output_dir.join("introduction.md");
        assert!(intro_path.exists(), "introduction.md should exist");
        let intro = fs::read_to_string(&intro_path).await?;
        assert!(
            intro.contains("# Introduction"),
            "Should have Introduction header"
        );
        assert!(
            intro.contains("README.md"),
            "Should contain README.md section"
        );

        // Verify src.md (src folder files)
        let src_md_path = output_dir.join("src.md");
        assert!(src_md_path.exists(), "src.md should exist");
        let src_md = fs::read_to_string(&src_md_path).await?;
        assert!(src_md.contains("## main.rs"), "Should have main.rs section");
        assert!(src_md.contains("## lib.rs"), "Should have lib.rs section");
        assert!(
            src_md.contains("fn main()"),
            "Should contain main.rs content"
        );

        Ok(())
    }

    #[tokio::test]
    async fn it_handles_nested_directories() -> anyhow::Result<()> {
        let temp_dir = tempdir()?;
        let root_path = temp_dir.path().to_path_buf();

        // Create nested structure
        let utils_dir = root_path.join("src").join("utils");
        std::fs::create_dir_all(&utils_dir)?;
        std::fs::write(root_path.join("src").join("main.rs"), "// main")?;
        std::fs::write(utils_dir.join("helpers.rs"), "// helpers")?;

        let output_dir = root_path.join("book");

        let entries = collect_files(&root_path, None, &HashSet::new(), None, &HashSet::new())?;
        generate_mdbook(&entries, &root_path, &output_dir).await?;

        // Verify nested chapter exists
        let summary = fs::read_to_string(output_dir.join("SUMMARY.md")).await?;
        assert!(summary.contains("[src]"), "Should have src chapter");
        assert!(summary.contains("[utils]"), "Should have utils sub-chapter");

        // Verify src/utils.md exists with proper nesting
        let utils_md = output_dir.join("src").join("utils.md");
        assert!(utils_md.exists(), "src/utils.md should exist");
        let utils_content = fs::read_to_string(&utils_md).await?;
        assert!(
            utils_content.contains("helpers.rs"),
            "Should have helpers.rs"
        );

        Ok(())
    }

    #[tokio::test]
    async fn it_writes_parent_chapter_files_when_only_nested_content_exists() -> anyhow::Result<()>
    {
        let temp_dir = tempdir()?;
        let root_path = temp_dir.path().to_path_buf();

        let nested_dir = root_path.join("src").join("utils");
        std::fs::create_dir_all(&nested_dir)?;
        std::fs::write(nested_dir.join("helpers.rs"), "// helpers")?;

        let output_dir = root_path.join("book");

        let entries = collect_files(&root_path, None, &HashSet::new(), None, &HashSet::new())?;
        generate_mdbook(&entries, &root_path, &output_dir).await?;

        let summary = fs::read_to_string(output_dir.join("SUMMARY.md")).await?;
        let normalized_summary = summary.replace('\\', "/");
        assert!(
            normalized_summary.contains("[src](./src.md)"),
            "Should link to src chapter file"
        );
        assert!(
            normalized_summary.contains("[utils](./src/utils.md)"),
            "Should link to nested utils chapter file"
        );

        assert!(output_dir.join("src.md").exists(), "src.md should exist");
        assert!(
            output_dir.join("src").join("utils.md").exists(),
            "src/utils.md should exist"
        );

        Ok(())
    }

    #[tokio::test]
    async fn it_handles_binary_files_in_mdbook() -> anyhow::Result<()> {
        let temp_dir = tempdir()?;
        let root_path = temp_dir.path().to_path_buf();

        // Create a binary file
        let binary_content: Vec<u8> = vec![0x00, 0x01, 0x02, 0xFF, 0xFE];
        std::fs::write(root_path.join("binary.bin"), &binary_content)?;

        let output_dir = root_path.join("book");

        let entries = collect_files(&root_path, None, &HashSet::new(), None, &HashSet::new())?;
        generate_mdbook(&entries, &root_path, &output_dir).await?;

        let intro = fs::read_to_string(output_dir.join("introduction.md")).await?;
        assert!(intro.contains("binary.bin"), "Should list binary file");
        assert!(
            intro.contains("(binary file omitted)"),
            "Should mark as binary"
        );

        Ok(())
    }
}