repolens 2.0.0

A CLI tool to audit and prepare repositories for open source or enterprise standards
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
//! Documentation rules
//!
//! This module provides rules for checking repository documentation, including:
//! - README files and their quality
//! - LICENSE files
//! - CONTRIBUTING guidelines
//! - CODE_OF_CONDUCT files
//! - SECURITY policy files
//! - CHANGELOG presence and format

use crate::error::RepoLensError;

use crate::config::Config;
use crate::rules::engine::RuleCategory;
use crate::rules::results::{Finding, Severity};
use crate::scanner::Scanner;

/// Rules for checking repository documentation
pub struct DocsRules;

#[async_trait::async_trait]
impl RuleCategory for DocsRules {
    /// Get the category name
    fn name(&self) -> &'static str {
        "docs"
    }

    /// Run all documentation-related rules
    ///
    /// # Arguments
    ///
    /// * `scanner` - The scanner to access repository files
    /// * `config` - The configuration with enabled rules
    ///
    /// # Returns
    ///
    /// A vector of findings for documentation issues
    async fn run(&self, scanner: &Scanner, config: &Config) -> Result<Vec<Finding>, RepoLensError> {
        let mut findings = Vec::new();

        // Check README
        if config.is_rule_enabled("docs/readme") {
            findings.extend(check_readme(scanner).await?);
        }

        // Check LICENSE
        if config.is_rule_enabled("docs/license") {
            findings.extend(check_license(scanner, config).await?);
        }

        // Check CONTRIBUTING
        if config.is_rule_enabled("docs/contributing") {
            findings.extend(check_contributing(scanner).await?);
        }

        // Check CODE_OF_CONDUCT
        if config.is_rule_enabled("docs/code-of-conduct") {
            findings.extend(check_code_of_conduct(scanner).await?);
        }

        // Check SECURITY
        if config.is_rule_enabled("docs/security") {
            findings.extend(check_security(scanner).await?);
        }

        // Check CHANGELOG
        if config.is_rule_enabled("docs/changelog") {
            findings.extend(check_changelog(scanner).await?);
        }

        // Check CHANGELOG format
        if config.is_rule_enabled("docs/changelog-format") {
            findings.extend(check_changelog_format(scanner).await?);
        }

        // Check CHANGELOG unreleased section
        if config.is_rule_enabled("docs/changelog-unreleased") {
            findings.extend(check_changelog_unreleased(scanner).await?);
        }

        Ok(findings)
    }
}

/// Check for README file and assess its quality
///
/// Verifies README existence and checks for recommended sections like
/// installation, usage, and license information.
///
/// # Arguments
///
/// * `scanner` - The scanner to access repository files
///
/// # Returns
///
/// A vector of findings for README issues
async fn check_readme(scanner: &Scanner) -> Result<Vec<Finding>, RepoLensError> {
    let mut findings = Vec::new();

    let readme_files = ["README.md", "README", "README.txt", "README.rst"];
    let has_readme = readme_files.iter().any(|f| scanner.file_exists(f));

    if !has_readme {
        findings.push(
            Finding::new(
                "DOC001",
                "docs",
                Severity::Warning,
                "README file is missing",
            )
            .with_description(
                "A README file is essential for explaining what the project does and how to use it.",
            )
            .with_remediation(
                "Create a README.md file with project description, installation instructions, and usage examples.",
            ),
        );
        return Ok(findings);
    }

    // Check README quality
    if let Ok(content) = scanner.read_file("README.md") {
        let line_count = content.lines().count();

        if line_count < 10 {
            findings.push(
                Finding::new(
                    "DOC002",
                    "docs",
                    Severity::Warning,
                    format!("README is too short ({} lines)", line_count),
                )
                .with_description(
                    "A comprehensive README should include sections for description, installation, usage, and contribution guidelines.",
                ),
            );
        }

        // Check for recommended sections
        let sections = [
            ("installation", "Installation instructions"),
            ("usage", "Usage examples"),
            ("license", "License information"),
        ];

        for (keyword, description) in sections {
            if !content.to_lowercase().contains(keyword) {
                findings.push(Finding::new(
                    "DOC003",
                    "docs",
                    Severity::Info,
                    format!("README missing section: {}", description),
                ));
            }
        }
    }

    Ok(findings)
}

/// Check for LICENSE file
///
/// Verifies that a LICENSE file exists. For enterprise preset, LICENSE is optional.
///
/// # Arguments
///
/// * `scanner` - The scanner to access repository files
/// * `config` - The configuration (used to check preset)
///
/// # Returns
///
/// A vector of findings for LICENSE issues
async fn check_license(scanner: &Scanner, config: &Config) -> Result<Vec<Finding>, RepoLensError> {
    let mut findings = Vec::new();

    let license_files = [
        "LICENSE",
        "LICENSE.md",
        "LICENSE.txt",
        "COPYING",
        "LICENSE-MIT",
        "LICENSE-APACHE",
        "LICENCE",
    ];
    let has_license = license_files.iter().any(|f| scanner.file_exists(f));

    // For enterprise preset, LICENSE is optional
    if config.preset == "enterprise" && !has_license {
        return Ok(findings);
    }

    if !has_license {
        findings.push(
            Finding::new(
                "DOC004",
                "docs",
                Severity::Critical,
                "LICENSE file is missing",
            )
            .with_description(
                "A LICENSE file is required for open source projects to define how others can use your code.",
            )
            .with_remediation(
                "Add a LICENSE file with an appropriate open source license (MIT, Apache-2.0, GPL-3.0, etc.).",
            ),
        );
    }

    Ok(findings)
}

/// Check for CONTRIBUTING file
///
/// Verifies that a CONTRIBUTING file exists to guide contributors.
///
/// # Arguments
///
/// * `scanner` - The scanner to access repository files
///
/// # Returns
///
/// A vector of findings for CONTRIBUTING issues
async fn check_contributing(scanner: &Scanner) -> Result<Vec<Finding>, RepoLensError> {
    let mut findings = Vec::new();

    let contributing_files = ["CONTRIBUTING.md", "CONTRIBUTING", ".github/CONTRIBUTING.md"];
    let has_contributing = contributing_files.iter().any(|f| scanner.file_exists(f));

    if !has_contributing {
        findings.push(
            Finding::new(
                "DOC005",
                "docs",
                Severity::Warning,
                "CONTRIBUTING file is missing",
            )
            .with_description(
                "A CONTRIBUTING file helps potential contributors understand how to participate in your project.",
            )
            .with_remediation(
                "Create a CONTRIBUTING.md file with contribution guidelines, code style, and pull request process.",
            ),
        );
    }

    Ok(findings)
}

/// Check for CODE_OF_CONDUCT file
///
/// Verifies that a CODE_OF_CONDUCT file exists to establish community standards.
///
/// # Arguments
///
/// * `scanner` - The scanner to access repository files
///
/// # Returns
///
/// A vector of findings for CODE_OF_CONDUCT issues
async fn check_code_of_conduct(scanner: &Scanner) -> Result<Vec<Finding>, RepoLensError> {
    let mut findings = Vec::new();

    let coc_files = [
        "CODE_OF_CONDUCT.md",
        "CODE_OF_CONDUCT",
        ".github/CODE_OF_CONDUCT.md",
    ];
    let has_coc = coc_files.iter().any(|f| scanner.file_exists(f));

    if !has_coc {
        findings.push(
            Finding::new(
                "DOC006",
                "docs",
                Severity::Warning,
                "CODE_OF_CONDUCT file is missing",
            )
            .with_description(
                "A Code of Conduct establishes expectations for behavior and helps create a welcoming community.",
            )
            .with_remediation(
                "Add a CODE_OF_CONDUCT.md file. Consider using the Contributor Covenant as a starting point.",
            ),
        );
    }

    Ok(findings)
}

/// Check for SECURITY policy file
///
/// Verifies that a SECURITY.md file exists for reporting vulnerabilities.
///
/// # Arguments
///
/// * `scanner` - The scanner to access repository files
///
/// # Returns
///
/// A vector of findings for SECURITY policy issues
async fn check_security(scanner: &Scanner) -> Result<Vec<Finding>, RepoLensError> {
    let mut findings = Vec::new();

    let security_files = ["SECURITY.md", ".github/SECURITY.md"];
    let has_security = security_files.iter().any(|f| scanner.file_exists(f));

    if !has_security {
        findings.push(
            Finding::new(
                "DOC007",
                "docs",
                Severity::Warning,
                "SECURITY policy file is missing",
            )
            .with_description(
                "A SECURITY.md file tells users how to report security vulnerabilities responsibly.",
            )
            .with_remediation(
                "Create a SECURITY.md file with instructions for reporting security issues.",
            ),
        );
    }

    Ok(findings)
}

/// Find the changelog file path if it exists
///
/// Checks for common changelog file names.
///
/// # Arguments
///
/// * `scanner` - The scanner to access repository files
///
/// # Returns
///
/// The path to the changelog file, or None if not found
fn find_changelog(scanner: &Scanner) -> Option<&'static str> {
    let changelog_files = [
        "CHANGELOG.md",
        "CHANGELOG",
        "CHANGELOG.txt",
        "HISTORY.md",
        "CHANGES.md",
    ];
    changelog_files.into_iter().find(|f| scanner.file_exists(f))
}

/// Check for CHANGELOG file
///
/// Verifies that a CHANGELOG file exists.
///
/// # Arguments
///
/// * `scanner` - The scanner to access repository files
///
/// # Returns
///
/// A vector of findings for missing CHANGELOG
async fn check_changelog(scanner: &Scanner) -> Result<Vec<Finding>, RepoLensError> {
    let mut findings = Vec::new();

    if find_changelog(scanner).is_none() {
        findings.push(
            Finding::new(
                "DOC008",
                "docs",
                Severity::Warning,
                "CHANGELOG file is missing",
            )
            .with_description(
                "A CHANGELOG file helps users and contributors track notable changes between releases.",
            )
            .with_remediation(
                "Create a CHANGELOG.md file. Consider following the Keep a Changelog format (https://keepachangelog.com).",
            ),
        );
    }

    Ok(findings)
}

/// Check if CHANGELOG follows Keep a Changelog format
///
/// Looks for semver version patterns like `## [x.y.z]` in the changelog.
///
/// # Arguments
///
/// * `scanner` - The scanner to access repository files
///
/// # Returns
///
/// A vector of findings for non-standard changelog format
async fn check_changelog_format(scanner: &Scanner) -> Result<Vec<Finding>, RepoLensError> {
    let mut findings = Vec::new();

    if let Some(changelog_path) = find_changelog(scanner) {
        if let Ok(content) = scanner.read_file(changelog_path) {
            // Look for ## [x.y.z] pattern (Keep a Changelog format)
            let has_semver_headers = content.lines().any(|line| {
                let trimmed = line.trim();
                trimmed.starts_with("## [")
                    && trimmed.contains('.')
                    && (trimmed.contains(']') || trimmed.ends_with(']'))
            });

            if !has_semver_headers {
                findings.push(
                    Finding::new(
                        "DOC009",
                        "docs",
                        Severity::Info,
                        "CHANGELOG does not follow Keep a Changelog format",
                    )
                    .with_location(changelog_path)
                    .with_description(
                        "The Keep a Changelog format uses '## [x.y.z]' headers for each version, making it easier to parse and read.",
                    )
                    .with_remediation(
                        "Consider reformatting your CHANGELOG to follow Keep a Changelog (https://keepachangelog.com).",
                    ),
                );
            }
        }
    }

    Ok(findings)
}

/// Check if CHANGELOG has content in the Unreleased section
///
/// If an `## [Unreleased]` section exists, checks whether it has content.
///
/// # Arguments
///
/// * `scanner` - The scanner to access repository files
///
/// # Returns
///
/// A vector of findings for empty Unreleased section
async fn check_changelog_unreleased(scanner: &Scanner) -> Result<Vec<Finding>, RepoLensError> {
    let mut findings = Vec::new();

    if let Some(changelog_path) = find_changelog(scanner) {
        if let Ok(content) = scanner.read_file(changelog_path) {
            let lines: Vec<&str> = content.lines().collect();

            // Find ## [Unreleased] section
            let unreleased_idx = lines.iter().position(|line| {
                let trimmed = line.trim().to_lowercase();
                trimmed.starts_with("## [unreleased]") || trimmed == "## [unreleased]"
            });

            if let Some(idx) = unreleased_idx {
                // Find the next ## [ section
                let next_section_idx = lines
                    .iter()
                    .skip(idx + 1)
                    .position(|line| line.trim().starts_with("## ["))
                    .map(|pos| pos + idx + 1);

                let end = next_section_idx.unwrap_or(lines.len());

                // Check if there's any non-empty content between Unreleased header and next section
                let has_content = lines[idx + 1..end]
                    .iter()
                    .any(|line| !line.trim().is_empty());

                if !has_content {
                    findings.push(
                        Finding::new(
                            "DOC010",
                            "docs",
                            Severity::Info,
                            "CHANGELOG has empty Unreleased section",
                        )
                        .with_location(changelog_path)
                        .with_description(
                            "The [Unreleased] section in the CHANGELOG is empty. Consider adding pending changes or removing the section until there are changes to document.",
                        )
                        .with_remediation(
                            "Add pending changes to the [Unreleased] section or remove it until there are changes to document.",
                        ),
                    );
                }
            }
        }
    }

    Ok(findings)
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::config::Config;
    use crate::scanner::Scanner;
    use std::fs;
    use tempfile::TempDir;

    #[tokio::test]
    async fn test_check_readme_missing() {
        let temp_dir = TempDir::new().unwrap();
        let root = temp_dir.path();

        let scanner = Scanner::new(root.to_path_buf());
        let findings = check_readme(&scanner).await.unwrap();

        assert!(!findings.is_empty());
        assert!(findings.iter().any(|f| f.rule_id == "DOC001"));
    }

    #[tokio::test]
    async fn test_check_readme_too_short() {
        let temp_dir = TempDir::new().unwrap();
        let root = temp_dir.path();
        let readme = root.join("README.md");

        fs::write(&readme, "# Test\n\nShort.").unwrap();

        let scanner = Scanner::new(root.to_path_buf());
        let findings = check_readme(&scanner).await.unwrap();

        assert!(findings.iter().any(|f| f.rule_id == "DOC002"));
    }

    #[tokio::test]
    async fn test_check_readme_missing_sections() {
        let temp_dir = TempDir::new().unwrap();
        let root = temp_dir.path();
        let readme = root.join("README.md");

        fs::write(&readme, "# Project\n\nDescription here.\n\nMore content.\n\nEven more.\n\nAnd more.\n\nAnd more.\n\nAnd more.\n\nAnd more.\n\nAnd more.\n\nAnd more.\n\nAnd more.").unwrap();

        let scanner = Scanner::new(root.to_path_buf());
        let findings = check_readme(&scanner).await.unwrap();

        assert!(findings.iter().any(|f| f.rule_id == "DOC003"));
    }

    #[tokio::test]
    async fn test_check_license_missing() {
        let temp_dir = TempDir::new().unwrap();
        let root = temp_dir.path();

        let scanner = Scanner::new(root.to_path_buf());
        let config = Config::default();
        let findings = check_license(&scanner, &config).await.unwrap();

        assert!(!findings.is_empty());
        assert!(findings.iter().any(|f| f.rule_id == "DOC004"));
    }

    #[tokio::test]
    async fn test_check_license_enterprise_optional() {
        let temp_dir = TempDir::new().unwrap();
        let root = temp_dir.path();

        let scanner = Scanner::new(root.to_path_buf());
        let config = Config {
            preset: "enterprise".to_string(),
            ..Default::default()
        };
        let findings = check_license(&scanner, &config).await.unwrap();

        assert!(findings.is_empty());
    }

    #[tokio::test]
    async fn test_check_contributing_missing() {
        let temp_dir = TempDir::new().unwrap();
        let root = temp_dir.path();

        let scanner = Scanner::new(root.to_path_buf());
        let findings = check_contributing(&scanner).await.unwrap();

        assert!(!findings.is_empty());
        assert!(findings.iter().any(|f| f.rule_id == "DOC005"));
    }

    #[tokio::test]
    async fn test_check_code_of_conduct_missing() {
        let temp_dir = TempDir::new().unwrap();
        let root = temp_dir.path();

        let scanner = Scanner::new(root.to_path_buf());
        let findings = check_code_of_conduct(&scanner).await.unwrap();

        assert!(!findings.is_empty());
        assert!(findings.iter().any(|f| f.rule_id == "DOC006"));
    }

    #[tokio::test]
    async fn test_check_security_missing() {
        let temp_dir = TempDir::new().unwrap();
        let root = temp_dir.path();

        let scanner = Scanner::new(root.to_path_buf());
        let findings = check_security(&scanner).await.unwrap();

        assert!(!findings.is_empty());
        assert!(findings.iter().any(|f| f.rule_id == "DOC007"));
    }

    // ===== DOC008: CHANGELOG Tests =====

    #[tokio::test]
    async fn test_check_changelog_missing() {
        let temp_dir = TempDir::new().unwrap();
        let root = temp_dir.path();

        let scanner = Scanner::new(root.to_path_buf());
        let findings = check_changelog(&scanner).await.unwrap();

        assert!(!findings.is_empty());
        assert!(findings.iter().any(|f| f.rule_id == "DOC008"));
    }

    #[tokio::test]
    async fn test_check_changelog_present() {
        let temp_dir = TempDir::new().unwrap();
        let root = temp_dir.path();
        fs::write(
            root.join("CHANGELOG.md"),
            "# Changelog\n\n## [1.0.0]\n\n- Initial release",
        )
        .unwrap();

        let scanner = Scanner::new(root.to_path_buf());
        let findings = check_changelog(&scanner).await.unwrap();

        assert!(findings.iter().all(|f| f.rule_id != "DOC008"));
    }

    #[tokio::test]
    async fn test_check_changelog_present_as_history() {
        let temp_dir = TempDir::new().unwrap();
        let root = temp_dir.path();
        fs::write(
            root.join("HISTORY.md"),
            "# History\n\n## 1.0.0\n\n- First release",
        )
        .unwrap();

        let scanner = Scanner::new(root.to_path_buf());
        let findings = check_changelog(&scanner).await.unwrap();

        assert!(findings.iter().all(|f| f.rule_id != "DOC008"));
    }

    #[tokio::test]
    async fn test_check_changelog_present_as_changes() {
        let temp_dir = TempDir::new().unwrap();
        let root = temp_dir.path();
        fs::write(root.join("CHANGES.md"), "# Changes").unwrap();

        let scanner = Scanner::new(root.to_path_buf());
        let findings = check_changelog(&scanner).await.unwrap();

        assert!(findings.iter().all(|f| f.rule_id != "DOC008"));
    }

    // ===== DOC009: CHANGELOG Format Tests =====

    #[tokio::test]
    async fn test_check_changelog_format_valid() {
        let temp_dir = TempDir::new().unwrap();
        let root = temp_dir.path();
        fs::write(
            root.join("CHANGELOG.md"),
            "# Changelog\n\n## [Unreleased]\n\n## [1.0.0] - 2024-01-01\n\n### Added\n- Initial release\n",
        )
        .unwrap();

        let scanner = Scanner::new(root.to_path_buf());
        let findings = check_changelog_format(&scanner).await.unwrap();

        assert!(findings.iter().all(|f| f.rule_id != "DOC009"));
    }

    #[tokio::test]
    async fn test_check_changelog_format_invalid() {
        let temp_dir = TempDir::new().unwrap();
        let root = temp_dir.path();
        fs::write(
            root.join("CHANGELOG.md"),
            "# Changelog\n\nJust some text about what changed.\nNo structured headers here.\n",
        )
        .unwrap();

        let scanner = Scanner::new(root.to_path_buf());
        let findings = check_changelog_format(&scanner).await.unwrap();

        assert!(findings.iter().any(|f| f.rule_id == "DOC009"));
    }

    #[tokio::test]
    async fn test_check_changelog_format_no_changelog() {
        let temp_dir = TempDir::new().unwrap();
        let root = temp_dir.path();

        let scanner = Scanner::new(root.to_path_buf());
        let findings = check_changelog_format(&scanner).await.unwrap();

        // No changelog file means no DOC009 finding
        assert!(findings.iter().all(|f| f.rule_id != "DOC009"));
    }

    // ===== DOC010: CHANGELOG Unreleased Tests =====

    #[tokio::test]
    async fn test_check_changelog_unreleased_with_content() {
        let temp_dir = TempDir::new().unwrap();
        let root = temp_dir.path();
        fs::write(
            root.join("CHANGELOG.md"),
            "# Changelog\n\n## [Unreleased]\n\n### Added\n- New feature\n\n## [1.0.0] - 2024-01-01\n\n- Initial release\n",
        )
        .unwrap();

        let scanner = Scanner::new(root.to_path_buf());
        let findings = check_changelog_unreleased(&scanner).await.unwrap();

        assert!(findings.iter().all(|f| f.rule_id != "DOC010"));
    }

    #[tokio::test]
    async fn test_check_changelog_unreleased_empty() {
        let temp_dir = TempDir::new().unwrap();
        let root = temp_dir.path();
        fs::write(
            root.join("CHANGELOG.md"),
            "# Changelog\n\n## [Unreleased]\n\n## [1.0.0] - 2024-01-01\n\n- Initial release\n",
        )
        .unwrap();

        let scanner = Scanner::new(root.to_path_buf());
        let findings = check_changelog_unreleased(&scanner).await.unwrap();

        assert!(findings.iter().any(|f| f.rule_id == "DOC010"));
    }

    #[tokio::test]
    async fn test_check_changelog_unreleased_no_unreleased_section() {
        let temp_dir = TempDir::new().unwrap();
        let root = temp_dir.path();
        fs::write(
            root.join("CHANGELOG.md"),
            "# Changelog\n\n## [1.0.0] - 2024-01-01\n\n- Initial release\n",
        )
        .unwrap();

        let scanner = Scanner::new(root.to_path_buf());
        let findings = check_changelog_unreleased(&scanner).await.unwrap();

        // No Unreleased section, so no DOC010 finding
        assert!(findings.iter().all(|f| f.rule_id != "DOC010"));
    }

    #[tokio::test]
    async fn test_check_changelog_unreleased_no_changelog() {
        let temp_dir = TempDir::new().unwrap();
        let root = temp_dir.path();

        let scanner = Scanner::new(root.to_path_buf());
        let findings = check_changelog_unreleased(&scanner).await.unwrap();

        // No changelog file means no DOC010 finding
        assert!(findings.iter().all(|f| f.rule_id != "DOC010"));
    }

    #[tokio::test]
    async fn test_check_changelog_unreleased_at_end_of_file_empty() {
        let temp_dir = TempDir::new().unwrap();
        let root = temp_dir.path();
        fs::write(
            root.join("CHANGELOG.md"),
            "# Changelog\n\n## [1.0.0] - 2024-01-01\n\n- Initial release\n\n## [Unreleased]\n",
        )
        .unwrap();

        let scanner = Scanner::new(root.to_path_buf());
        let findings = check_changelog_unreleased(&scanner).await.unwrap();

        assert!(findings.iter().any(|f| f.rule_id == "DOC010"));
    }
}