gobby-wiki 0.2.0

Gobby wiki CLI shell
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
use std::collections::{BTreeMap, BTreeSet};
use std::fs;
use std::path::{Path, PathBuf};
use std::sync::{Mutex, OnceLock};

use chrono::{DateTime, NaiveDate, NaiveDateTime, Utc};
use linked_hash_map::LinkedHashMap;
use serde::Serialize;

use crate::lint::{collect_pages, title_for_page};
use crate::markdown::{MarkdownFence, markdown_fence_closes, markdown_fence_start};
use crate::provenance::ProvenanceGraph;
use crate::sources::{CompileStatus, SourceManifest, SourceRecord};
use crate::{ScopeIdentity, WikiError};

const AVERAGE_GREGORIAN_YEAR_SECONDS: u64 = 31_556_952;
const STALE_CITATION_YEARS_ENV: &str = "GWIKI_STALE_CITATION_YEARS";
const REGEX_CACHE_CAPACITY: usize = 1_000;

#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct HealthReport {
    pub command: &'static str,
    pub scope: ScopeIdentity,
    pub root: PathBuf,
    pub stale_pages: Vec<PathBuf>,
    pub stale_citations: Vec<HealthSourceIssue>,
    pub uncited_sources: Vec<HealthSourceIssue>,
    pub broken_links: Vec<crate::lint::LinkIssue>,
    pub duplicate_concepts: Vec<DuplicateConcept>,
    pub uncompiled_sources: Vec<HealthSourceIssue>,
    pub json_path: PathBuf,
    pub text_path: PathBuf,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct HealthSourceIssue {
    pub source_id: String,
    pub path: Option<PathBuf>,
    pub location: String,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct DuplicateConcept {
    pub title: String,
    pub paths: Vec<PathBuf>,
}

pub fn run(vault_root: &Path, scope: ScopeIdentity) -> Result<HealthReport, WikiError> {
    let lint_report = crate::lint::run(vault_root, scope.clone())?;
    let pages = collect_pages(vault_root)?;
    let manifest = SourceManifest::read(vault_root)?;
    let provenance = load_provenance(vault_root)?;
    let citation_index = build_citation_index(&manifest.entries, &pages, &provenance);
    let stale_pages = stale_pages(&pages);
    let stale_citations = manifest
        .entries
        .iter()
        .filter(|entry| source_citation_is_stale(entry))
        .map(source_issue)
        .collect();
    let uncited_sources = manifest
        .entries
        .iter()
        .filter(|entry| !citation_index.cites(&entry.id))
        .map(source_issue)
        .collect();
    let duplicate_concepts = duplicate_concepts(&pages);
    let uncompiled_sources = manifest
        .entries
        .iter()
        .filter(|entry| entry.compile_status == CompileStatus::Pending)
        .map(source_issue)
        .collect();
    let report = HealthReport {
        command: "health",
        scope,
        root: vault_root.to_path_buf(),
        stale_pages,
        stale_citations,
        uncited_sources,
        broken_links: lint_report.broken_links,
        duplicate_concepts,
        uncompiled_sources,
        json_path: PathBuf::from("meta/health/latest.json"),
        text_path: PathBuf::from("meta/health/latest.md"),
    };
    persist_report(vault_root, &report)?;
    Ok(report)
}

pub fn render_text(report: &HealthReport) -> String {
    let mut text = format!("Wiki health report\nScope: {}\n", report.scope);
    render_paths(&mut text, "Stale pages", &report.stale_pages);
    render_sources(&mut text, "Stale citations", &report.stale_citations);
    render_sources(&mut text, "Uncited sources", &report.uncited_sources);
    render_broken_links(&mut text, &report.broken_links);
    render_duplicate_concepts(&mut text, &report.duplicate_concepts);
    render_sources(&mut text, "Uncompiled sources", &report.uncompiled_sources);
    text
}

fn persist_report(vault_root: &Path, report: &HealthReport) -> Result<(), WikiError> {
    let health_dir = vault_root.join("meta").join("health");
    fs::create_dir_all(&health_dir).map_err(|error| WikiError::Io {
        action: "create health report directory",
        path: Some(health_dir.clone()),
        source: error,
    })?;
    let json_path = vault_root.join(&report.json_path);
    let text_path = vault_root.join(&report.text_path);
    let json = serde_json::to_string_pretty(report).map_err(|error| WikiError::Json {
        action: "serialize health report",
        path: Some(json_path.clone()),
        source: error,
    })?;
    fs::write(&json_path, json).map_err(|error| WikiError::Io {
        action: "write health JSON report",
        path: Some(json_path),
        source: error,
    })?;
    fs::write(&text_path, render_text(report)).map_err(|error| WikiError::Io {
        action: "write health text report",
        path: Some(text_path),
        source: error,
    })
}

fn stale_pages(pages: &[crate::lint::WikiPage]) -> Vec<PathBuf> {
    let mut paths: Vec<PathBuf> = pages
        .iter()
        .filter(|page| page_is_stale(page))
        .map(|page| page.relative_path.clone())
        .collect();
    paths.sort();
    paths
}

fn page_is_stale(page: &crate::lint::WikiPage) -> bool {
    let frontmatter = &page.parsed.frontmatter;
    if frontmatter
        .unknown
        .get("stale")
        .and_then(serde_json::Value::as_bool)
        == Some(true)
    {
        return true;
    }
    for key in ["status", "review_status"] {
        if frontmatter
            .unknown
            .get(key)
            .and_then(serde_json::Value::as_str)
            .is_some_and(|value| value.eq_ignore_ascii_case("stale"))
        {
            return true;
        }
    }
    frontmatter
        .unknown
        .get("stale_after")
        .and_then(serde_json::Value::as_str)
        .is_some_and(|value| stale_after_is_due(value, Utc::now()))
}

fn stale_after_is_due(value: &str, now: DateTime<Utc>) -> bool {
    let value = value.trim();
    if value.is_empty() {
        return false;
    }
    if let Ok(parsed) = DateTime::parse_from_rfc3339(value) {
        return parsed.with_timezone(&Utc) <= now;
    }
    if let Ok(parsed) = NaiveDate::parse_from_str(value, "%Y-%m-%d") {
        return parsed <= now.date_naive();
    }
    for format in ["%Y-%m-%d %H:%M:%S", "%Y-%m-%dT%H:%M:%S"] {
        if let Ok(parsed) = NaiveDateTime::parse_from_str(value, format) {
            return parsed.and_utc() <= now;
        }
    }
    false
}

fn source_citation_is_stale(source: &SourceRecord) -> bool {
    source_citation_is_stale_at(source, Utc::now())
}

fn source_citation_is_stale_at(source: &SourceRecord, now: DateTime<Utc>) -> bool {
    let stale_years = stale_citation_years();
    source.citation.is_some() && fetched_at_is_stale(&source.fetched_at, stale_years, now)
}

fn fetched_at_is_stale(value: &str, stale_years: u64, now: DateTime<Utc>) -> bool {
    if let Some(fetched_at) = parse_fetched_at(value) {
        let stale_seconds = stale_years.saturating_mul(AVERAGE_GREGORIAN_YEAR_SECONDS);
        let Ok(stale_seconds) = i64::try_from(stale_seconds) else {
            return false;
        };
        return fetched_at
            .checked_add_signed(chrono::Duration::seconds(stale_seconds))
            .is_some_and(|deadline| deadline <= now);
    }
    fetched_year(value)
        .is_some_and(|year| year.saturating_add(stale_years) < approximate_current_year_at(now))
}

fn parse_fetched_at(value: &str) -> Option<DateTime<Utc>> {
    if let Ok(parsed) = DateTime::parse_from_rfc3339(value) {
        return Some(parsed.with_timezone(&Utc));
    }
    if let Ok(parsed) = NaiveDate::parse_from_str(value, "%Y-%m-%d") {
        return parsed.and_hms_opt(0, 0, 0).map(|value| value.and_utc());
    }
    for format in ["%Y-%m-%d %H:%M:%S", "%Y-%m-%dT%H:%M:%S"] {
        if let Ok(parsed) = NaiveDateTime::parse_from_str(value, format) {
            return Some(parsed.and_utc());
        }
    }
    None
}

fn stale_citation_years() -> u64 {
    match std::env::var(STALE_CITATION_YEARS_ENV) {
        Ok(raw) => stale_citation_years_from_env(&raw).unwrap_or_else(|| {
            eprintln!("warning: ignoring invalid {STALE_CITATION_YEARS_ENV}={raw}");
            1
        }),
        Err(_) => 1,
    }
}

fn stale_citation_years_from_env(raw: &str) -> Option<u64> {
    raw.trim().parse::<u64>().ok().filter(|value| *value > 0)
}

fn fetched_year(value: &str) -> Option<u64> {
    let year = value.get(0..4)?;
    (year.chars().all(|ch| ch.is_ascii_digit()))
        .then(|| year.parse().ok())
        .flatten()
}

fn approximate_current_year_at(now: DateTime<Utc>) -> u64 {
    // Health checks only need a coarse stale-citation window; using the average
    // Gregorian year keeps this dependency-free and avoids timezone handling.
    1970 + u64::try_from(now.timestamp()).unwrap_or(0) / AVERAGE_GREGORIAN_YEAR_SECONDS
}

fn load_provenance(vault_root: &Path) -> Result<ProvenanceGraph, WikiError> {
    let path = vault_root.join("meta").join("provenance.json");
    if path.exists() {
        ProvenanceGraph::load_from_vault(vault_root)
    } else {
        Ok(ProvenanceGraph::default())
    }
}

#[derive(Default)]
struct SourceCitationIndex {
    cited_source_ids: BTreeSet<String>,
}

impl SourceCitationIndex {
    fn cites(&self, source_id: &str) -> bool {
        self.cited_source_ids.contains(source_id)
    }
}

fn build_citation_index(
    sources: &[SourceRecord],
    pages: &[crate::lint::WikiPage],
    provenance: &ProvenanceGraph,
) -> SourceCitationIndex {
    let mut cited_source_ids = sources
        .iter()
        .filter(|source| !provenance.links_for_source(&source.id).is_empty())
        .map(|source| source.id.clone())
        .collect::<BTreeSet<_>>();
    let mut patterns = Vec::new();
    let mut pattern_source_ids = Vec::new();
    for source in sources {
        for needle in source_reference_needles(source) {
            for pattern in source_reference_patterns(needle) {
                patterns.push(pattern);
                pattern_source_ids.push(source.id.as_str());
            }
        }
    }
    if patterns.is_empty() {
        return SourceCitationIndex { cited_source_ids };
    }
    let regex_set = match regex::RegexSet::new(&patterns) {
        Ok(regex_set) => regex_set,
        Err(error) => {
            log::warn!("failed to build health citation regex set: {error}");
            return SourceCitationIndex { cited_source_ids };
        }
    };

    for page in pages {
        let markdown = markdown_without_fenced_code(&page.markdown);
        for matched in regex_set.matches(&markdown) {
            cited_source_ids.insert(pattern_source_ids[matched].to_string());
        }
    }
    SourceCitationIndex { cited_source_ids }
}

fn source_reference_needles(source: &SourceRecord) -> Vec<&str> {
    let mut needles = vec![
        source.id.as_str(),
        source.location.as_str(),
        source.canonical_location.as_str(),
    ];
    if let Some(citation) = source.citation.as_deref() {
        needles.push(citation);
    }
    needles
}

fn source_reference_patterns(needle: &str) -> Vec<String> {
    let needle = needle.trim();
    if needle.is_empty() {
        return Vec::new();
    }
    vec![
        markdown_link_target_pattern(needle),
        bounded_text_pattern(needle),
    ]
}

#[cfg(test)]
fn source_reference_is_present(markdown: &str, needle: &str) -> bool {
    let needle = needle.trim();
    if needle.is_empty() {
        return false;
    }
    let markdown = markdown_without_fenced_code(markdown);
    markdown_link_target_matches(&markdown, needle) || bounded_text_matches(&markdown, needle)
}

fn markdown_without_fenced_code(markdown: &str) -> String {
    let mut output = String::new();
    let mut active_fence: Option<MarkdownFence> = None;
    for line in markdown.lines() {
        if let Some(fence) = active_fence {
            if markdown_fence_closes(line, fence) {
                active_fence = None;
                continue;
            }
        } else if let Some(fence) = markdown_fence_start(line) {
            active_fence = Some(fence);
            continue;
        }
        if active_fence.is_none() {
            output.push_str(line);
            output.push('\n');
        }
    }
    output
}

fn markdown_link_target_matches(markdown: &str, needle: &str) -> bool {
    cached_regex_is_match(markdown_link_target_pattern(needle), markdown)
}

fn bounded_text_matches(markdown: &str, needle: &str) -> bool {
    cached_regex_is_match(bounded_text_pattern(needle), markdown)
}

fn markdown_link_target_pattern(needle: &str) -> String {
    let escaped = regex::escape(needle);
    format!(
        r#"(?m)(\[[^\]]*\]\(\s*<?{escaped}>?(?:\s+["'][^"']*["'])?\s*\)|\[\[\s*{escaped}(?:\|[^\]]*)?\s*\]\])"#
    )
}

fn bounded_text_pattern(needle: &str) -> String {
    let escaped = regex::escape(needle);
    format!(r#"(^|[^\p{{L}}\p{{N}}_]){escaped}($|[^\p{{L}}\p{{N}}_])"#)
}

fn cached_regex_is_match(pattern: String, haystack: &str) -> bool {
    static CACHE: OnceLock<Mutex<RegexCache>> = OnceLock::new();
    let mut cache = match CACHE
        .get_or_init(|| Mutex::new(RegexCache::default()))
        .lock()
    {
        Ok(cache) => cache,
        Err(poisoned) => {
            // Regex compilation is deterministic; recovering the cache keeps a
            // prior panic from forcing every later check down the slow path.
            poisoned.into_inner()
        }
    };
    let regex = match cache.get(&pattern) {
        Some(regex) => regex,
        None => {
            let regex = match regex::Regex::new(&pattern) {
                Ok(regex) => regex,
                Err(error) => {
                    log::warn!("invalid health regex pattern `{pattern}`: {error}");
                    return false;
                }
            };
            cache.insert(pattern, regex.clone());
            regex
        }
    };
    drop(cache);
    regex.is_match(haystack)
}

#[derive(Default)]
struct RegexCache {
    entries: LinkedHashMap<String, regex::Regex>,
}

impl RegexCache {
    fn get(&mut self, pattern: &str) -> Option<regex::Regex> {
        let regex = self.entries.remove(pattern)?;
        let cloned = regex.clone();
        self.entries.insert(pattern.to_string(), regex);
        Some(cloned)
    }

    fn insert(&mut self, pattern: String, regex: regex::Regex) {
        self.entries.remove(&pattern);
        self.entries.insert(pattern, regex);
        while self.entries.len() > REGEX_CACHE_CAPACITY {
            self.entries.pop_front();
        }
    }
}

fn source_issue(source: &SourceRecord) -> HealthSourceIssue {
    HealthSourceIssue {
        source_id: source.id.clone(),
        path: Some(PathBuf::from("raw").join(format!("{}.md", source.id))),
        location: source.location.clone(),
    }
}

fn duplicate_concepts(pages: &[crate::lint::WikiPage]) -> Vec<DuplicateConcept> {
    let mut by_title: BTreeMap<String, (String, Vec<PathBuf>)> = BTreeMap::new();
    for page in pages {
        if !page.relative_path.starts_with("wiki/concepts") {
            continue;
        }
        let title = title_for_page(page);
        by_title
            .entry(title.to_ascii_lowercase())
            .or_insert_with(|| (title, Vec::new()))
            .1
            .push(page.relative_path.clone());
    }
    by_title
        .into_values()
        .filter_map(|(title, mut paths)| {
            paths.sort();
            (paths.len() > 1).then_some(DuplicateConcept { title, paths })
        })
        .collect()
}

fn render_paths(text: &mut String, heading: &str, paths: &[PathBuf]) {
    text.push('\n');
    text.push_str(heading);
    text.push_str(":\n");
    if paths.is_empty() {
        text.push_str("- none\n");
        return;
    }
    for path in paths {
        text.push_str("- ");
        text.push_str(&path.display().to_string());
        text.push('\n');
    }
}

fn render_sources(text: &mut String, heading: &str, sources: &[HealthSourceIssue]) {
    text.push('\n');
    text.push_str(heading);
    text.push_str(":\n");
    if sources.is_empty() {
        text.push_str("- none\n");
        return;
    }
    for source in sources {
        text.push_str("- ");
        text.push_str(&source.source_id);
        text.push_str(" (");
        text.push_str(&source.location);
        text.push_str(")\n");
    }
}

fn render_broken_links(text: &mut String, issues: &[crate::lint::LinkIssue]) {
    text.push_str("\nBroken links:\n");
    if issues.is_empty() {
        text.push_str("- none\n");
        return;
    }
    for issue in issues {
        text.push_str("- ");
        text.push_str(&issue.path.display().to_string());
        text.push(':');
        text.push_str(&issue.line.to_string());
        text.push_str(" -> ");
        text.push_str(&issue.target);
        text.push('\n');
    }
}

fn render_duplicate_concepts(text: &mut String, duplicates: &[DuplicateConcept]) {
    text.push_str("\nDuplicate concepts:\n");
    if duplicates.is_empty() {
        text.push_str("- none\n");
        return;
    }
    for duplicate in duplicates {
        text.push_str("- ");
        text.push_str(&duplicate.title);
        text.push_str(": ");
        text.push_str(
            &duplicate
                .paths
                .iter()
                .map(|path| path.display().to_string())
                .collect::<Vec<_>>()
                .join(", "),
        );
        text.push('\n');
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::sources::{IngestionMethod, SourceDraft, SourceKind, SourceManifest};

    #[test]
    fn health_checks_required_cases() {
        let temp = tempfile::tempdir().expect("tempdir");
        let root = temp.path();
        let source = SourceManifest::register(
            root,
            SourceDraft::url(
                "https://example.com/uncited",
                "2026-05-29T12:00:00Z",
                "uncited source",
            )
            .with_citation("Uncited Example"),
        )
        .expect("source registered");
        write_page(
            root,
            "wiki/topics/stale.md",
            "---\ntitle: Stale\nstale: true\n---\n# Stale\nSee [[Missing]].\n",
        );
        write_page(
            root,
            "wiki/concepts/cache-a.md",
            "---\ntitle: Cache\nsource_kind: concept\n---\n# Cache\nConcept A.\n",
        );
        write_page(
            root,
            "wiki/concepts/cache-b.md",
            "---\ntitle: Cache\nsource_kind: concept\n---\n# Cache\nConcept B.\n",
        );

        let report = run(root, ScopeIdentity::topic("ops")).expect("health runs");

        assert_eq!(
            report.stale_pages,
            vec![PathBuf::from("wiki/topics/stale.md")]
        );
        assert_eq!(report.uncited_sources[0].source_id, source.id);
        assert_eq!(report.broken_links[0].target, "Missing");
        assert_eq!(report.duplicate_concepts[0].title, "Cache");
        assert_eq!(report.uncompiled_sources[0].source_id, source.id);
        assert!(root.join("meta/health/latest.json").exists());
        assert!(root.join("meta/health/latest.md").exists());
    }

    #[test]
    fn source_reference_matching_skips_code_fences_and_partial_words() {
        assert!(!source_reference_is_present(
            "```md\nhttps://example.test/source\n```\n",
            "https://example.test/source"
        ));
        assert!(!source_reference_is_present(
            "prefixsource-idsuffix",
            "source-id"
        ));
        assert!(source_reference_is_present(
            "[Example](https://example.test/source)",
            "https://example.test/source"
        ));
    }

    #[test]
    fn citation_index_marks_cited_sources_once_per_page() {
        let temp = tempfile::tempdir().expect("tempdir");
        let root = temp.path();
        let cited = SourceManifest::register(
            root,
            SourceDraft::url(
                "https://example.com/cited",
                "2026-05-29T12:00:00Z",
                "cited source",
            )
            .with_citation("Cited Example"),
        )
        .expect("cited source registered");
        let uncited = SourceManifest::register(
            root,
            SourceDraft::url(
                "https://example.com/uncited",
                "2026-05-29T12:00:00Z",
                "uncited source",
            )
            .with_citation("Uncited Example"),
        )
        .expect("uncited source registered");
        write_page(
            root,
            "wiki/topics/cited.md",
            "# Cited\n\n[Cited Example](https://example.com/cited)\n",
        );

        let report = run(root, ScopeIdentity::topic("ops")).expect("health runs");
        let uncited_ids = report
            .uncited_sources
            .iter()
            .map(|issue| issue.source_id.as_str())
            .collect::<Vec<_>>();

        assert!(!uncited_ids.contains(&cited.id.as_str()));
        assert!(uncited_ids.contains(&uncited.id.as_str()));
    }

    #[test]
    fn cached_regex_returns_false_for_malformed_patterns() {
        assert!(!cached_regex_is_match("[".to_string(), "anything"));
    }

    #[test]
    fn stale_after_compares_dates_and_times_to_now() {
        let now = DateTime::parse_from_rfc3339("2026-06-02T12:00:00Z")
            .unwrap()
            .with_timezone(&Utc);

        assert!(stale_after_is_due("2026-06-02", now));
        assert!(stale_after_is_due("2026-06-02T11:59:59Z", now));
        assert!(!stale_after_is_due("2026-06-03", now));
        assert!(!stale_after_is_due("not-a-date", now));
    }

    #[test]
    fn regex_cache_touch_updates_lru_order() {
        let mut cache = RegexCache::default();
        cache.insert("one".to_string(), regex::Regex::new("one").unwrap());
        cache.insert("two".to_string(), regex::Regex::new("two").unwrap());

        assert!(cache.get("one").is_some());

        assert_eq!(
            cache.entries.keys().cloned().collect::<Vec<_>>(),
            vec!["two".to_string(), "one".to_string()]
        );
    }

    #[test]
    fn fenced_code_closes_only_on_matching_delimiter() {
        let markdown = "before\n~~~\nhttps://example.test/source\n```\nstill fenced\n~~~\nafter\n";

        let without_fences = markdown_without_fenced_code(markdown);

        assert_eq!(without_fences, "before\nafter\n");
    }

    #[test]
    fn fenced_code_requires_matching_marker_length() {
        let markdown =
            "before\n````\nhttps://example.test/source\n```\nstill fenced\n````\nafter\n";

        let without_fences = markdown_without_fenced_code(markdown);

        assert_eq!(without_fences, "before\nafter\n");
    }

    #[test]
    fn stale_citation_env_rejects_invalid_values() {
        assert_eq!(stale_citation_years_from_env("3"), Some(3));
        assert_eq!(stale_citation_years_from_env(" 2 "), Some(2));
        assert_eq!(stale_citation_years_from_env("0"), None);
        assert_eq!(stale_citation_years_from_env("nope"), None);
    }

    #[test]
    fn stale_citation_uses_full_fetched_timestamp() {
        let now = DateTime::parse_from_rfc3339("2026-06-02T12:00:00Z")
            .unwrap()
            .with_timezone(&Utc);

        assert!(source_citation_is_stale_at(
            &source_record("2025-06-02T05:00:00Z"),
            now
        ));
        assert!(!source_citation_is_stale_at(
            &source_record("2025-06-02T18:00:00Z"),
            now
        ));
    }

    fn write_page(root: &Path, relative: &str, markdown: &str) {
        let path = root.join(relative);
        std::fs::create_dir_all(path.parent().expect("page parent")).expect("create parent");
        std::fs::write(path, markdown).expect("write page");
    }

    fn source_record(fetched_at: &str) -> SourceRecord {
        SourceRecord {
            id: "source-id".to_string(),
            location: "https://example.test/source".to_string(),
            canonical_location: "https://example.test/source".to_string(),
            kind: SourceKind::Url,
            fetched_at: fetched_at.to_string(),
            content_hash: "hash".to_string(),
            title: None,
            citation: Some("Example".to_string()),
            license: None,
            ingestion_method: IngestionMethod::Manual,
            compile_status: CompileStatus::Compiled,
            replay: None,
        }
    }
}