llmwiki-tooling 0.3.0

CLI for managing LLM-wikis with Obsidian-style wikilinks.
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
use std::collections::HashSet;
use std::path::Path;

use serde::Deserialize;

use crate::error::ConfigError;

/// Severity level for a check or rule.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum Severity {
    /// Causes non-zero exit code.
    #[default]
    Error,
    /// Prints finding but does not affect exit code.
    Warn,
    /// Suppressed entirely.
    Off,
}

impl std::fmt::Display for Severity {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Error => f.write_str("error"),
            Self::Warn => f.write_str("warn"),
            Self::Off => f.write_str("off"),
        }
    }
}

impl<'de> Deserialize<'de> for Severity {
    fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
        let s = String::deserialize(deserializer)?;
        match s.as_str() {
            "error" => Ok(Self::Error),
            "warn" => Ok(Self::Warn),
            "off" => Ok(Self::Off),
            other => Err(serde::de::Error::unknown_variant(
                other,
                &["error", "warn", "off"],
            )),
        }
    }
}

/// Complete wiki configuration, parsed from `wiki.toml` or auto-detected.
#[derive(Debug, Clone)]
pub struct WikiConfig {
    /// Path to the index file relative to wiki root. `None` means no index file.
    pub index: Option<String>,
    /// Content directories, sorted most-specific-first for resolution.
    pub directories: Vec<DirectoryConfig>,
    /// Markdown walk ignore settings.
    pub ignore: IgnoreConfig,
    /// Linking behavior settings.
    pub linking: LinkingConfig,
    /// Wiki-wide structural check severities.
    pub checks: ChecksConfig,
    /// Parameterized rules scoped to directories and/or frontmatter predicates.
    pub rules: Vec<RuleConfig>,
}

/// A directory containing wiki pages.
#[derive(Debug, Clone)]
pub struct DirectoryConfig {
    /// Path relative to wiki root.
    pub path: String,
    /// Whether pages in this directory feed bare mention detection.
    pub autolink: bool,
}

pub(crate) const EXAMPLE_CONFIG: &str = include_str!("example-config.toml");
const DEFAULT_IGNORE_PATTERNS: &[&str] = &[
    ".agents/",
    ".claude/",
    ".clinerules/",
    ".codex/",
    ".continue/",
    ".cursor/",
    ".gemini/",
    ".github/",
    ".gsd/",
    ".kiro/",
    ".kilocode/",
    ".opencode/",
    ".openhands/",
    ".pi/",
    ".qwen/",
    ".roo/",
    ".windsurf/",
];

/// Markdown walk ignore settings.
#[derive(Debug, Clone)]
pub struct IgnoreConfig {
    /// Whether built-in non-wiki tool directory patterns are included.
    pub default_patterns: bool,
    /// Extra gitignore-style patterns, additive with defaults.
    pub patterns: Vec<String>,
}

impl Default for IgnoreConfig {
    fn default() -> Self {
        Self {
            default_patterns: true,
            patterns: Vec::new(),
        }
    }
}

impl IgnoreConfig {
    pub(crate) fn effective_patterns(&self) -> impl Iterator<Item = &str> {
        DEFAULT_IGNORE_PATTERNS
            .iter()
            .copied()
            .filter(|_| self.default_patterns)
            .chain(self.patterns.iter().map(String::as_str))
    }
}

/// Global linking behavior.
#[derive(Debug, Clone)]
pub struct LinkingConfig {
    /// Page names to never auto-link.
    pub exclude: HashSet<String>,
    /// Frontmatter field for per-page auto-link opt-out.
    pub autolink_field: String,
}

/// Wiki-wide structural check severities.
#[derive(Debug, Clone)]
pub struct ChecksConfig {
    pub broken_links: Severity,
    pub unmanaged_broken_links: Severity,
    pub orphan_pages: Severity,
    pub index_coverage: Severity,
}

/// Frontmatter predicate that restricts a rule to matching pages.
#[derive(Debug, Clone)]
pub struct RulePredicate {
    pub field: String,
    pub value: String,
}

impl RulePredicate {
    fn parse(expr: &str) -> Result<Self, ConfigError> {
        let Some((field, value)) = expr.split_once("==") else {
            return Err(ConfigError::Validation(format!(
                "unsupported rule predicate '{expr}'; expected `field == value`"
            )));
        };
        let field = field.trim();
        let value = value.trim();
        if field.is_empty() || value.is_empty() {
            return Err(ConfigError::Validation(format!(
                "unsupported rule predicate '{expr}'; expected `field == value`"
            )));
        }
        Ok(Self {
            field: field.to_owned(),
            value: strip_quotes(value).to_owned(),
        })
    }
}

fn strip_quotes(value: &str) -> &str {
    value
        .strip_prefix('"')
        .and_then(|s| s.strip_suffix('"'))
        .or_else(|| value.strip_prefix('\'').and_then(|s| s.strip_suffix('\'')))
        .unwrap_or(value)
}

/// A parameterized rule scoped to specific directories and/or frontmatter.
#[derive(Debug, Clone)]
pub enum RuleConfig {
    RequiredSections {
        dirs: Vec<String>,
        when: Option<RulePredicate>,
        sections: Vec<String>,
        severity: Severity,
    },
    RequiredFrontmatter {
        dirs: Vec<String>,
        when: Option<RulePredicate>,
        fields: Vec<String>,
        severity: Severity,
    },
    MirrorParity {
        left: String,
        right: String,
        severity: Severity,
    },
    CitationPattern {
        name: String,
        dirs: Vec<String>,
        when: Option<RulePredicate>,
        pattern: String,
        match_in: String,
        match_mode: MatchMode,
        severity: Severity,
    },
}

impl RuleConfig {
    pub fn severity(&self) -> Severity {
        match self {
            Self::RequiredSections { severity, .. }
            | Self::RequiredFrontmatter { severity, .. }
            | Self::MirrorParity { severity, .. }
            | Self::CitationPattern { severity, .. } => *severity,
        }
    }
}

/// How a citation pattern match is verified against `match_in` pages.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum MatchMode {
    /// Search page file contents for the captured ID string.
    #[default]
    Content,
    /// Check if a page with the captured ID as its filename exists.
    Filename,
}

impl<'de> Deserialize<'de> for MatchMode {
    fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
        let s = String::deserialize(deserializer)?;
        match s.as_str() {
            "content" => Ok(Self::Content),
            "filename" => Ok(Self::Filename),
            other => Err(serde::de::Error::unknown_variant(
                other,
                &["content", "filename"],
            )),
        }
    }
}

// --- TOML deserialization types ---

#[derive(Deserialize)]
struct RawConfig {
    index: Option<String>,
    #[serde(default)]
    verbatim: Vec<String>,
    #[serde(default)]
    directories: Vec<RawDirectoryConfig>,
    #[serde(default)]
    ignore: RawIgnoreConfig,
    #[serde(default)]
    linking: RawLinkingConfig,
    #[serde(default)]
    checks: RawChecksConfig,
    #[serde(default)]
    rules: Vec<RawRuleConfig>,
}

#[derive(Deserialize)]
struct RawDirectoryConfig {
    path: String,
    #[serde(default = "default_true")]
    autolink: bool,
}

fn default_true() -> bool {
    true
}

#[derive(Deserialize)]
struct RawIgnoreConfig {
    #[serde(default = "default_true")]
    default_patterns: bool,
    #[serde(default)]
    patterns: Vec<String>,
}

impl Default for RawIgnoreConfig {
    fn default() -> Self {
        Self {
            default_patterns: true,
            patterns: Vec::new(),
        }
    }
}

#[derive(Deserialize, Default)]
struct RawLinkingConfig {
    #[serde(default)]
    exclude: Vec<String>,
    #[serde(default = "default_autolink_field")]
    autolink_field: String,
}

fn default_autolink_field() -> String {
    "autolink".to_owned()
}

#[derive(Deserialize, Default)]
struct RawChecksConfig {
    #[serde(default)]
    broken_links: Option<Severity>,
    #[serde(default)]
    unmanaged_broken_links: Option<Severity>,
    #[serde(default)]
    orphan_pages: Option<Severity>,
    #[serde(default)]
    index_coverage: Option<Severity>,
}

#[derive(Deserialize)]
#[serde(tag = "check")]
enum RawRuleConfig {
    #[serde(rename = "required-sections")]
    RequiredSections {
        #[serde(default)]
        dirs: Vec<String>,
        #[serde(default)]
        when: Option<String>,
        sections: Vec<String>,
        #[serde(default)]
        severity: Option<Severity>,
    },
    #[serde(rename = "required-frontmatter")]
    RequiredFrontmatter {
        #[serde(default)]
        dirs: Vec<String>,
        #[serde(default)]
        when: Option<String>,
        fields: Vec<String>,
        #[serde(default)]
        severity: Option<Severity>,
    },
    #[serde(rename = "mirror-parity")]
    MirrorParity {
        left: String,
        right: String,
        #[serde(default)]
        severity: Option<Severity>,
    },
    #[serde(rename = "citation-pattern")]
    CitationPattern {
        name: String,
        #[serde(default)]
        dirs: Vec<String>,
        #[serde(default)]
        when: Option<String>,
        #[serde(default)]
        pattern: Option<String>,
        #[serde(default)]
        preset: Option<String>,
        match_in: String,
        #[serde(default)]
        match_mode: Option<MatchMode>,
        #[serde(default)]
        severity: Option<Severity>,
    },
}

/// Built-in citation pattern presets.
fn resolve_preset(name: &str) -> Result<(String, MatchMode), ConfigError> {
    match name {
        "bold-method-year" => Ok((
            r"\*\*(?P<id>[A-Za-z][A-Za-z0-9-]+)\*\*\s*\([^)]*\d{4}[^)]*\)".to_owned(),
            MatchMode::Filename,
        )),
        other => Err(ConfigError::UnknownPreset(other.to_owned())),
    }
}

impl WikiConfig {
    /// Load config from `wiki.toml` in the given root directory.
    /// Returns `None` if `wiki.toml` doesn't exist.
    pub fn load(root: &Path) -> Result<Option<Self>, ConfigError> {
        let config_path = root.join("wiki.toml");
        let content = match std::fs::read_to_string(&config_path) {
            Ok(content) => content,
            Err(e) if e.kind() == std::io::ErrorKind::NotFound => return Ok(None),
            Err(e) => {
                return Err(ConfigError::Read {
                    path: config_path,
                    source: e,
                });
            }
        };
        let raw: RawConfig = toml::from_str(&content).map_err(|e| ConfigError::Parse {
            path: config_path,
            source: e,
        })?;
        Self::from_raw(raw).map(Some)
    }

    /// Auto-detect config when no `wiki.toml` exists.
    pub fn auto_detect(root: &Path) -> Self {
        let has_wiki_dir = root.join("wiki").is_dir();
        let dir_path = if has_wiki_dir { "wiki" } else { "." };

        Self {
            index: Some("index.md".to_owned()),
            directories: vec![DirectoryConfig {
                path: dir_path.to_owned(),
                autolink: true,
            }],
            ignore: IgnoreConfig::default(),
            linking: LinkingConfig {
                exclude: HashSet::new(),
                autolink_field: default_autolink_field(),
            },
            checks: ChecksConfig {
                broken_links: Severity::Error,
                unmanaged_broken_links: Severity::Warn,
                orphan_pages: Severity::Error,
                index_coverage: Severity::Error,
            },
            rules: Vec::new(),
        }
    }

    /// Load config from wiki.toml if present, otherwise auto-detect.
    pub fn load_or_detect(root: &Path) -> Result<Self, ConfigError> {
        match Self::load(root)? {
            Some(config) => Ok(config),
            None => Ok(Self::auto_detect(root)),
        }
    }

    fn from_raw(raw: RawConfig) -> Result<Self, ConfigError> {
        let mut directories: Vec<DirectoryConfig> = if raw.directories.is_empty() {
            // No directories declared — auto-detect
            vec![DirectoryConfig {
                path: "wiki".to_owned(),
                autolink: true,
            }]
        } else {
            raw.directories
                .into_iter()
                .map(|d| DirectoryConfig {
                    path: normalize_path(&d.path),
                    autolink: d.autolink,
                })
                .collect()
        };

        // Sort most-specific first (longest path) for resolution
        directories.sort_by_key(|dir| std::cmp::Reverse(dir.path.len()));

        let mut ignore_patterns = raw.ignore.patterns;
        ignore_patterns.extend(raw.verbatim);
        let ignore = IgnoreConfig {
            default_patterns: raw.ignore.default_patterns,
            patterns: ignore_patterns,
        };
        validate_ignore_patterns(&ignore)?;

        let linking = LinkingConfig {
            exclude: raw.linking.exclude.into_iter().collect(),
            autolink_field: raw.linking.autolink_field,
        };

        let checks = ChecksConfig {
            broken_links: raw.checks.broken_links.unwrap_or(Severity::Error),
            unmanaged_broken_links: raw.checks.unmanaged_broken_links.unwrap_or(Severity::Warn),
            orphan_pages: raw.checks.orphan_pages.unwrap_or(Severity::Error),
            index_coverage: raw.checks.index_coverage.unwrap_or(Severity::Error),
        };

        let mut rules = Vec::new();
        for raw_rule in raw.rules {
            rules.push(convert_rule(raw_rule)?);
        }

        // Validate citation patterns compile as regex
        for rule in &rules {
            if let RuleConfig::CitationPattern { pattern, name, .. } = rule {
                regex_lite::Regex::new(pattern).map_err(|e| ConfigError::InvalidPattern {
                    name: name.clone(),
                    source: e,
                })?;
            }
        }

        Ok(Self {
            index: match raw.index {
                Some(s) if s.is_empty() => None,
                Some(s) => Some(s),
                None => Some("index.md".to_owned()),
            },
            directories,
            ignore,
            linking,
            checks,
            rules,
        })
    }

    /// Get the directory config that applies to a given relative path.
    /// Returns the most-specific matching directory (longest prefix match).
    pub fn directory_for(&self, rel_path: &Path) -> Option<&DirectoryConfig> {
        // Directories are sorted most-specific first
        self.directories
            .iter()
            .find(|d| path_matches_prefix(rel_path, &d.path))
    }

    /// Check if a relative path matches a directory prefix from a rule's `dirs` list.
    pub fn matches_dirs(rel_path: &Path, dirs: &[String]) -> bool {
        dirs.iter().any(|d| path_matches_prefix(rel_path, d))
    }

    /// All mirror-parity rules' `right` paths (non-wiki directories used for parity checks).
    pub fn mirror_paths(&self) -> Vec<(&str, &str)> {
        self.rules
            .iter()
            .filter_map(|r| match r {
                RuleConfig::MirrorParity { left, right, .. } => {
                    Some((left.as_str(), right.as_str()))
                }
                _ => None,
            })
            .collect()
    }
}

fn validate_ignore_patterns(ignore: &IgnoreConfig) -> Result<(), ConfigError> {
    let mut builder = ignore::gitignore::GitignoreBuilder::new(Path::new(""));
    for pattern in ignore.effective_patterns() {
        if pattern.starts_with('!') {
            return Err(ConfigError::Validation(format!(
                "ignore pattern '{pattern}' cannot start with '!'"
            )));
        }
        builder
            .add_line(None, pattern)
            .map_err(|source| ConfigError::InvalidIgnorePattern {
                pattern: pattern.to_owned(),
                source,
            })?;
    }
    builder
        .build()
        .map_err(|source| ConfigError::CompileIgnorePatterns { source })?;
    Ok(())
}

fn convert_rule(raw: RawRuleConfig) -> Result<RuleConfig, ConfigError> {
    match raw {
        RawRuleConfig::RequiredSections {
            dirs,
            when,
            sections,
            severity,
        } => Ok(RuleConfig::RequiredSections {
            dirs: dirs.into_iter().map(|d| normalize_path(&d)).collect(),
            when: parse_when(when)?,
            sections,
            severity: severity.unwrap_or(Severity::Error),
        }),
        RawRuleConfig::RequiredFrontmatter {
            dirs,
            when,
            fields,
            severity,
        } => Ok(RuleConfig::RequiredFrontmatter {
            dirs: dirs.into_iter().map(|d| normalize_path(&d)).collect(),
            when: parse_when(when)?,
            fields,
            severity: severity.unwrap_or(Severity::Error),
        }),
        RawRuleConfig::MirrorParity {
            left,
            right,
            severity,
        } => Ok(RuleConfig::MirrorParity {
            left: normalize_path(&left),
            right: normalize_path(&right),
            severity: severity.unwrap_or(Severity::Error),
        }),
        RawRuleConfig::CitationPattern {
            name,
            dirs,
            when,
            pattern,
            preset,
            match_in,
            match_mode,
            severity,
        } => {
            let (resolved_pattern, resolved_mode) = match (pattern, preset) {
                (Some(p), None) => (p, match_mode.unwrap_or(MatchMode::Content)),
                (None, Some(preset_name)) => {
                    let (p, m) = resolve_preset(&preset_name)?;
                    (p, match_mode.unwrap_or(m))
                }
                (Some(_), Some(_)) => {
                    return Err(ConfigError::Validation(format!(
                        "citation-pattern '{name}': cannot specify both 'pattern' and 'preset'"
                    )));
                }
                (None, None) => {
                    return Err(ConfigError::Validation(format!(
                        "citation-pattern '{name}': must specify either 'pattern' or 'preset'"
                    )));
                }
            };
            Ok(RuleConfig::CitationPattern {
                name,
                dirs: dirs.into_iter().map(|d| normalize_path(&d)).collect(),
                when: parse_when(when)?,
                pattern: resolved_pattern,
                match_in: normalize_path(&match_in),
                match_mode: resolved_mode,
                severity: severity.unwrap_or(Severity::Warn),
            })
        }
    }
}

fn parse_when(when: Option<String>) -> Result<Option<RulePredicate>, ConfigError> {
    when.as_deref().map(RulePredicate::parse).transpose()
}

fn path_matches_prefix(rel_path: &Path, prefix: &str) -> bool {
    prefix == "." || rel_path.starts_with(Path::new(prefix))
}

/// Strip trailing slashes for consistent prefix matching.
fn normalize_path(path: &str) -> String {
    path.trim_end_matches('/').to_owned()
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn parses_minimal_config() {
        let toml = r#"
[[directories]]
path = "wiki"
"#;
        let raw: RawConfig = toml::from_str(toml).unwrap();
        let config = WikiConfig::from_raw(raw).unwrap();
        assert_eq!(config.directories.len(), 1);
        assert_eq!(config.directories[0].path, "wiki");
        assert!(config.directories[0].autolink);
        assert_eq!(config.checks.broken_links, Severity::Error);
        assert_eq!(config.checks.unmanaged_broken_links, Severity::Warn);
    }

    #[test]
    fn parses_full_config() {
        let toml = r#"
index = "contents.md"

[[directories]]
path = "wiki"

[[directories]]
path = "wiki/papers"
autolink = false

[linking]
exclude = ["the", "a"]
autolink_field = "auto"

[checks]
broken_links = "error"
unmanaged_broken_links = "warn"
orphan_pages = "warn"
index_coverage = "off"

[[rules]]
check = "required-sections"
dirs = ["wiki/concepts"]
sections = ["See also"]
severity = "error"

[[rules]]
check = "mirror-parity"
left = "wiki/papers"
right = "raw/papers"
severity = "warn"

[[rules]]
check = "citation-pattern"
name = "arxiv"
dirs = ["wiki"]
pattern = 'arxiv\.org/abs/(?P<id>\d{4}\.\d{4,5})'
match_in = "wiki/papers"
severity = "warn"

[[rules]]
check = "citation-pattern"
name = "bold-method"
preset = "bold-method-year"
dirs = ["wiki"]
match_in = "wiki/papers"
severity = "warn"
"#;
        let raw: RawConfig = toml::from_str(toml).unwrap();
        let config = WikiConfig::from_raw(raw).unwrap();

        assert_eq!(config.index.as_deref(), Some("contents.md"));
        assert!(config.linking.exclude.contains("the"));
        assert_eq!(config.linking.autolink_field, "auto");
        assert_eq!(config.checks.unmanaged_broken_links, Severity::Warn);
        assert_eq!(config.checks.orphan_pages, Severity::Warn);
        assert_eq!(config.checks.index_coverage, Severity::Off);
        assert_eq!(config.rules.len(), 4);

        // Most specific directory first
        assert_eq!(config.directories[0].path, "wiki/papers");
        assert!(!config.directories[0].autolink);
        assert_eq!(config.directories[1].path, "wiki");
        assert!(config.directories[1].autolink);
    }

    #[test]
    fn example_config_is_parseable() {
        let raw: RawConfig = toml::from_str(EXAMPLE_CONFIG).unwrap();
        WikiConfig::from_raw(raw).unwrap();
    }

    #[test]
    fn directory_resolution_most_specific_wins() {
        let config = WikiConfig {
            index: None,
            directories: vec![
                DirectoryConfig {
                    path: "wiki/papers".to_owned(),
                    autolink: false,
                },
                DirectoryConfig {
                    path: "wiki".to_owned(),
                    autolink: true,
                },
            ],
            ignore: IgnoreConfig::default(),
            linking: LinkingConfig {
                exclude: HashSet::new(),
                autolink_field: "autolink".to_owned(),
            },
            checks: ChecksConfig {
                broken_links: Severity::Error,
                unmanaged_broken_links: Severity::Warn,
                orphan_pages: Severity::Error,
                index_coverage: Severity::Error,
            },
            rules: Vec::new(),
        };

        assert!(
            config
                .directory_for(Path::new("wiki/concepts/GRPO.md"))
                .is_some_and(|dir| dir.autolink)
        );
        assert!(
            config
                .directory_for(Path::new("wiki/papers/deepseek.md"))
                .is_some_and(|dir| !dir.autolink)
        );
    }

    #[test]
    fn auto_detect_with_wiki_dir() {
        let dir = tempfile::tempdir().unwrap();
        std::fs::create_dir(dir.path().join("wiki")).unwrap();
        std::fs::write(dir.path().join("index.md"), "# Index").unwrap();

        let config = WikiConfig::auto_detect(dir.path());
        assert_eq!(config.directories[0].path, "wiki");
        assert_eq!(config.index.as_deref(), Some("index.md"));
    }

    #[test]
    fn auto_detect_flat_wiki() {
        let dir = tempfile::tempdir().unwrap();
        std::fs::write(dir.path().join("index.md"), "# Index").unwrap();

        let config = WikiConfig::auto_detect(dir.path());
        assert_eq!(config.directories[0].path, ".");
    }

    #[test]
    fn rejects_pattern_and_preset_together() {
        let toml = r#"
[[rules]]
check = "citation-pattern"
name = "test"
dirs = ["wiki"]
pattern = "foo"
preset = "bold-method-year"
match_in = "wiki"
"#;
        let raw: RawConfig = toml::from_str(toml).unwrap();
        let err = WikiConfig::from_raw(raw).unwrap_err();
        assert!(err.to_string().contains("cannot specify both"));
    }

    #[test]
    fn rejects_unknown_preset() {
        let toml = r#"
[[rules]]
check = "citation-pattern"
name = "test"
dirs = ["wiki"]
preset = "nonexistent"
match_in = "wiki"
"#;
        let raw: RawConfig = toml::from_str(toml).unwrap();
        let err = WikiConfig::from_raw(raw).unwrap_err();
        assert!(err.to_string().contains("nonexistent"));
    }

    #[test]
    fn parses_rule_when_predicate() {
        let toml = r#"
[[rules]]
check = "required-frontmatter"
when = "type == concept"
fields = ["owner"]
"#;
        let raw: RawConfig = toml::from_str(toml).unwrap();
        let config = WikiConfig::from_raw(raw).unwrap();

        let RuleConfig::RequiredFrontmatter { dirs, when, .. } = &config.rules[0] else {
            panic!("expected required-frontmatter rule");
        };
        assert!(dirs.is_empty());
        let when = when.as_ref().unwrap();
        assert_eq!(when.field, "type");
        assert_eq!(when.value, "concept");
    }

    #[test]
    fn parses_ignore_config() {
        let toml = r#"
[ignore]
default_patterns = false
patterns = ["generated/"]
"#;
        let raw: RawConfig = toml::from_str(toml).unwrap();
        let config = WikiConfig::from_raw(raw).unwrap();

        assert!(!config.ignore.default_patterns);
        assert_eq!(config.ignore.patterns, ["generated/"]);
        assert_eq!(
            config.ignore.effective_patterns().collect::<Vec<_>>(),
            ["generated/"]
        );
    }

    #[test]
    fn verbatim_patterns_are_additive_ignores() {
        let toml = r#"
verbatim = ["raw-inputs/", "scraped/"]

[ignore]
patterns = ["generated/"]
"#;
        let raw: RawConfig = toml::from_str(toml).unwrap();
        let config = WikiConfig::from_raw(raw).unwrap();

        assert!(config.ignore.patterns.contains(&"generated/".to_owned()));
        assert!(config.ignore.patterns.contains(&"raw-inputs/".to_owned()));
        assert!(config.ignore.patterns.contains(&"scraped/".to_owned()));
    }

    #[test]
    fn rejects_unignore_patterns() {
        let toml = r#"
[ignore]
patterns = ["!.claude/"]
"#;
        let raw: RawConfig = toml::from_str(toml).unwrap();
        let err = WikiConfig::from_raw(raw).unwrap_err();
        assert!(err.to_string().contains("cannot start with '!'"));
    }

    #[test]
    fn matches_dirs_prefix() {
        assert!(WikiConfig::matches_dirs(
            Path::new("wiki/concepts/GRPO.md"),
            &["wiki/concepts".to_owned()]
        ));
        assert!(WikiConfig::matches_dirs(
            Path::new("wiki/concepts/GRPO.md"),
            &["wiki".to_owned()]
        ));
        assert!(!WikiConfig::matches_dirs(
            Path::new("wiki/papers/foo.md"),
            &["wiki/concepts".to_owned()]
        ));
        assert!(!WikiConfig::matches_dirs(
            Path::new("wikiology/foo.md"),
            &["wiki".to_owned()]
        ));
    }
}