gobby-wiki 0.8.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
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
use std::collections::BTreeSet;
use std::path::{Path, PathBuf};
use std::sync::Arc;

use super::claims::{
    analyze_claims, claim_lines, has_codewiki_frontmatter_source_spans, has_inline_source_support,
    is_manifest_backed_source_digest,
};
use super::*;
use crate::lint::WikiPage;
use crate::provenance::ProvenanceGraph;
use crate::sources::{SourceDraft, SourceManifest};

/// Unsupported-claims view of [`analyze_claims`] for tests that only assert
/// on audit failures.
fn unsupported_claims(
    page: &WikiPage,
    provenance: &ProvenanceGraph,
    source_context: &Arc<Vec<AuditSourceContext>>,
    manifest_hashes: &BTreeSet<String>,
    options: &AuditOptions,
) -> Vec<UnsupportedClaim> {
    analyze_claims(page, provenance, source_context, manifest_hashes, options).unsupported
}

#[test]
fn catalog_surfaces_are_exempt_from_claim_scanning() {
    // The deterministic catalog files rebuilt by `catalog::regenerate` are
    // navigation artifacts; their placeholder prose must not flag, while the
    // identical text on any other page still does.
    let temp = tempfile::tempdir().expect("tempdir");
    let root = temp.path();
    let body = "# Code\n\n## Handbook\n\n(none yet)\n";
    for relative in ["_index.md", "knowledge/INDEX.md", "code/INDEX.md"] {
        let page = root.join(relative);
        std::fs::create_dir_all(page.parent().expect("page parent")).expect("create wiki dir");
        std::fs::write(&page, body).expect("write catalog page");
    }
    let control = root.join("knowledge/topics/placeholder.md");
    std::fs::create_dir_all(control.parent().expect("page parent")).expect("create wiki dir");
    std::fs::write(&control, body).expect("write control page");

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

    assert_eq!(
        report.unsupported_claims.len(),
        1,
        "only the non-catalog page flags: {:?}",
        report.unsupported_claims
    );
    assert_eq!(
        report.unsupported_claims[0].path,
        PathBuf::from("knowledge/topics/placeholder.md")
    );
}

#[test]
fn recap_pages_are_exempt_from_claim_scanning() {
    // Daily recaps are page-level supported (#17575): the deterministic
    // session listing is structural and the overview grounds on the day's
    // digests. The same prose on a non-recap page still flags.
    let temp = tempfile::tempdir().expect("tempdir");
    let root = temp.path();
    let body = "# Recap\n\n## Overview\n\nSynthesis was unavailable for this run.\n";
    let recap = root.join("recaps/2026-07-05.md");
    std::fs::create_dir_all(recap.parent().expect("recap parent")).expect("create recaps dir");
    std::fs::write(
        &recap,
        format!("---\ntitle: \"Recap: 2026-07-05\"\nrecap_date: 2026-07-05\n---\n{body}"),
    )
    .expect("write recap page");
    let control = root.join("knowledge/topics/not-a-recap.md");
    std::fs::create_dir_all(control.parent().expect("control parent")).expect("create topics dir");
    std::fs::write(&control, body).expect("write control page");

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

    assert_eq!(
        report.unsupported_claims.len(),
        1,
        "only the non-recap page flags: {:?}",
        report.unsupported_claims
    );
    assert_eq!(
        report.unsupported_claims[0].path,
        PathBuf::from("knowledge/topics/not-a-recap.md")
    );
}

#[test]
fn reports_unsupported_claims() {
    let temp = tempfile::tempdir().expect("tempdir");
    let root = temp.path();
    let source = SourceManifest::register(
        root,
        SourceDraft::url(
            "https://example.com/source",
            "2026-05-29T12:00:00Z",
            "source body",
        )
        .with_citation("Example Source"),
    )
    .expect("source registered");
    let page = root.join("knowledge/topics/claims.md");
    std::fs::create_dir_all(page.parent().expect("page parent")).expect("create wiki dir");
    std::fs::write(
        &page,
        "---\ntitle: Claims\nsource_kind: topic\n---\n# Claims\nUnsupported operational claim.\n",
    )
    .expect("write page");

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

    assert_eq!(report.unsupported_claims.len(), 1);
    let claim = &report.unsupported_claims[0];
    assert_eq!(claim.path, PathBuf::from("knowledge/topics/claims.md"));
    assert_eq!(claim.line, 6);
    assert_eq!(claim.heading.as_deref(), Some("Claims"));
    assert!(claim.claim.contains("Unsupported operational claim"));
    assert_eq!(claim.source_context[0].source_id, source.id);
    assert_eq!(
        claim.source_context[0].citation.as_deref(),
        Some("Example Source")
    );
}

#[test]
fn generated_codewiki_numeric_claims_do_not_inherit_raw_source_context() {
    let temp = tempfile::tempdir().expect("tempdir");
    let root = temp.path();
    let source = SourceManifest::register(
        root,
        SourceDraft::url(
            "https://example.com/raw-source",
            "2026-05-29T12:00:00Z",
            "raw source body",
        )
        .with_citation("Raw Source"),
    )
    .expect("source registered");
    let code_page = root.join("code/_changes.md");
    std::fs::create_dir_all(code_page.parent().expect("code parent")).expect("create code dir");
    std::fs::write(
        &code_page,
        "---\ntitle: Index Changes\nkind: code_changes\ngenerated_by: gcode-codewiki\ntrust: generated\nfreshness: indexed\n---\n# Index Changes\n\n## Current Snapshot\n\n- Files: 457\n- Symbols: 7901\n",
    )
    .expect("write code page");
    let knowledge_page = root.join("knowledge/topics/claims.md");
    std::fs::create_dir_all(knowledge_page.parent().expect("knowledge parent"))
        .expect("create knowledge dir");
    std::fs::write(
        &knowledge_page,
        "---\ntitle: Claims\nsource_kind: topic\n---\n# Claims\nUnsupported operational claim.\n",
    )
    .expect("write knowledge page");

    let report = run(root, ScopeIdentity::project("project-123")).expect("audit runs");

    let code_path = PathBuf::from("code/_changes.md");
    let generated_claims = report
        .unsupported_claims
        .iter()
        .filter(|claim| claim.path.as_path() == code_path.as_path())
        .collect::<Vec<_>>();
    // A codewiki-generated `code/**` projection page (index-count claims like
    // `Files: 457`/`Symbols: 7901`) is exempt from claim scanning: it must
    // contribute no unsupported claims and, in particular, must never inherit
    // raw source context from unrelated knowledge sources.
    assert!(
        generated_claims.is_empty(),
        "generated code projection page should be exempt, got {generated_claims:?}"
    );
    let rendered = render_text(&report);
    assert!(
        rendered
            .lines()
            .filter(|line| line.contains("code/_changes.md"))
            .all(|line| !line.contains("[sources:"))
    );
    let knowledge_claim = report
        .unsupported_claims
        .iter()
        .find(|claim| claim.path == Path::new("knowledge/topics/claims.md"))
        .expect("knowledge claim");
    assert_eq!(knowledge_claim.source_context[0].source_id, source.id);
}

#[test]
fn reports_include_paths_and_scope() {
    let temp = tempfile::tempdir().expect("tempdir");
    let root = temp.path();
    let page = root.join("knowledge/topics/path-scope.md");
    std::fs::create_dir_all(page.parent().expect("page parent")).expect("create wiki dir");
    std::fs::write(
        &page,
        "---\ntitle: Path Scope\nsource_kind: topic\n---\n# Path Scope\nClaim needing evidence.\n",
    )
    .expect("write page");

    let report = run(root, ScopeIdentity::project("project-123")).expect("audit runs");
    let json = serde_json::to_value(&report).expect("report serializes");

    assert_eq!(report.scope, ScopeIdentity::project("project-123"));
    assert_eq!(
        json.pointer("/scope/id")
            .and_then(serde_json::Value::as_str),
        Some("project-123")
    );
    assert_eq!(
        json.pointer("/unsupported_claims/0/path")
            .and_then(serde_json::Value::as_str),
        Some("knowledge/topics/path-scope.md")
    );
}

#[test]
fn topic_scope_audits_only_topic_pages() {
    let temp = tempfile::tempdir().expect("tempdir");
    let root = temp.path();
    let topic_page = root.join("knowledge/topics/topic-claim.md");
    let concept_page = root.join("knowledge/concepts/concept-claim.md");
    std::fs::create_dir_all(topic_page.parent().expect("topic parent")).expect("create topic dir");
    std::fs::create_dir_all(concept_page.parent().expect("concept parent"))
        .expect("create concept dir");
    std::fs::write(
        &topic_page,
        "---\ntitle: Topic\nsource_kind: topic\n---\n# Topic\nTopic claim.\n",
    )
    .expect("write topic page");
    std::fs::write(
        &concept_page,
        "---\ntitle: Concept\nsource_kind: concept\n---\n# Concept\nConcept claim.\n",
    )
    .expect("write concept page");

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

    assert_eq!(report.unsupported_claims.len(), 1);
    assert_eq!(
        report.unsupported_claims[0].path,
        PathBuf::from("knowledge/topics/topic-claim.md")
    );
}

#[test]
fn frontmatter_closes_only_on_matching_document_start_delimiter() {
    let page = WikiPage {
        path: PathBuf::from("knowledge/topics/frontmatter.md"),
        relative_path: PathBuf::from("knowledge/topics/frontmatter.md"),
        markdown: "+++\ntitle = \"Frontmatter\"\n---\nstill_frontmatter = true\n+++\n# Body\nClaim after TOML frontmatter.\n---\nClaim after thematic break.\n".to_string(),
        parsed: crate::markdown::parse_markdown(
            "knowledge/topics/frontmatter.md",
            "# Body\n",
            std::iter::empty::<&str>(),
        )
        .expect("parse markdown"),
        has_frontmatter: true,
    };

    let claims = claim_lines(&page, &AuditOptions::default());

    assert_eq!(claims.len(), 2);
    assert_eq!(claims[0].text, "Claim after TOML frontmatter.");
    assert_eq!(claims[1].text, "Claim after thematic break.");
}

#[test]
fn multiline_html_comments_do_not_emit_claims() {
    let page = WikiPage {
        path: PathBuf::from("knowledge/topics/comments.md"),
        relative_path: PathBuf::from("knowledge/topics/comments.md"),
        markdown: "# Comments\nVisible claim.\n<!--\nHidden claim.\n-->\nAfter claim.\n"
            .to_string(),
        parsed: crate::markdown::parse_markdown(
            "knowledge/topics/comments.md",
            "# Comments\n",
            std::iter::empty::<&str>(),
        )
        .expect("parse markdown"),
        has_frontmatter: false,
    };

    let claims = claim_lines(&page, &AuditOptions::default());

    assert_eq!(claims.len(), 2);
    assert_eq!(claims[0].text, "Visible claim.");
    assert_eq!(claims[1].text, "After claim.");
}

#[test]
fn inline_source_support_requires_link_like_target() {
    assert!(!has_inline_source_support("the upstream source: TBD"));
    assert!(has_inline_source_support(
        "Evidence source: https://example.com/report"
    ));
    assert!(has_inline_source_support(
        "Evidence citation: [[knowledge/sources/source-1]]"
    ));
}

#[test]
fn inline_source_support_accepts_codewiki_source_spans() {
    assert!(has_inline_source_support(
        "Purpose: documents the builder. [crates/gcode/src/commands/codewiki/build.rs:3-73]"
    ));
    assert!(has_inline_source_support(
        "Root metadata is loaded from [Cargo.toml:1]"
    ));
    assert!(!has_inline_source_support(
        "Not a source citation [placeholder:123]"
    ));
    assert!(!has_inline_source_support(
        "Invalid range [crates/gwiki/src/audit.rs:42-3]"
    ));
}

#[test]
fn codewiki_frontmatter_source_spans_support_structural_claims() {
    let markdown = r#"---
title: crates/example.rs
type: code_file
provenance:
- file: crates/example.rs
  ranges:
  - 1-12
---

# crates/example.rs

Module: [[code/modules/crates|crates]]
Signature: `fn example() -> bool {`
"#;
    let page = WikiPage {
        path: PathBuf::from("code/files/crates/example.rs.md"),
        relative_path: PathBuf::from("code/files/crates/example.rs.md"),
        markdown: markdown.to_string(),
        parsed: crate::markdown::parse_markdown(
            "code/files/crates/example.rs.md",
            markdown,
            std::iter::empty::<&str>(),
        )
        .expect("parse markdown"),
        has_frontmatter: true,
    };
    assert!(has_codewiki_frontmatter_source_spans(&page));

    let claims = unsupported_claims(
        &page,
        &ProvenanceGraph::default(),
        &Arc::new(Vec::new()),
        &BTreeSet::new(),
        &AuditOptions::default(),
    );

    assert!(claims.is_empty());
}

#[test]
fn codewiki_contract_golden_page_counts_as_code_source_spans() {
    let page = test_codewiki_page(
        "code/files/src/lib.rs.md",
        gobby_core::codewiki_contract::GOLDEN_PAGE,
    );

    assert!(has_codewiki_frontmatter_source_spans(&page));
}

#[test]
fn codewiki_frontmatter_source_spans_do_not_support_prose_claims() {
    let markdown = r#"---
title: crates/example.rs
type: code_file
provenance:
- file: crates/example.rs
  ranges:
  - 1-12
---

# crates/example.rs

Module: [[code/modules/crates|crates]]
This generated page makes an unsupported prose claim.
"#;
    let page = test_codewiki_page("code/files/crates/example.rs.md", markdown);

    let claims = unsupported_claims(
        &page,
        &ProvenanceGraph::default(),
        &Arc::new(Vec::new()),
        &BTreeSet::new(),
        &AuditOptions::default(),
    );

    assert_eq!(claims.len(), 1);
    assert_eq!(
        claims[0].claim,
        "This generated page makes an unsupported prose claim."
    );
}

#[test]
fn template_projected_code_docs_are_page_level_supported() {
    // Mirrors a real `ai_route: off` codewiki file page: file-level provenance,
    // no per-claim ranges, and purely mechanical scaffolding (overview count,
    // symbol/kind rows, module wikilink). Every line is index-derived and
    // attributed to the declared source file, so nothing is unsupported even
    // though none of the lines carry inline or frontmatter source spans.
    let markdown = r#"---
title: src/mathutil.py
type: code_file
provenance:
- file: src/mathutil.py
generated_by: gcode-codewiki
trust: generated
freshness: indexed
ai_route: off
ai_fallback: false
ai_generation_status: skipped
---

# src/mathutil.py

Module: [[code/modules/src|src]]

## Overview

`src/mathutil.py` exposes 1 indexed API symbol.

## Reference

- Symbol: `add`
  Kind: function
"#;
    let page = test_codewiki_page("code/files/src/mathutil.py.md", markdown);

    // Guard the precondition: this page has no per-claim source spans, so the
    // exemption is the only thing that can credit its claims.
    assert!(!has_codewiki_frontmatter_source_spans(&page));

    let claims = unsupported_claims(
        &page,
        &ProvenanceGraph::default(),
        &Arc::new(Vec::new()),
        &BTreeSet::new(),
        &AuditOptions::default(),
    );

    assert!(
        claims.is_empty(),
        "template-projected code doc should have no unsupported claims, got {claims:?}"
    );
}

#[test]
fn degraded_codewiki_docs_are_page_level_supported() {
    // A degraded page fell back to the template body (no AI narrative landed),
    // so it is credited wholesale just like a skipped page.
    let markdown = r#"---
title: src/mathutil.py
type: code_file
provenance:
- file: src/mathutil.py
generated_by: gcode-codewiki
trust: generated
freshness: indexed
ai_route: off
ai_fallback: true
ai_generation_status: degraded
degraded: true
degraded_sources:
- model_provider_unavailable
---

# src/mathutil.py

`src/mathutil.py` exposes 1 indexed API symbol.
"#;
    let page = test_codewiki_page("code/files/src/mathutil.py.md", markdown);

    let claims = unsupported_claims(
        &page,
        &ProvenanceGraph::default(),
        &Arc::new(Vec::new()),
        &BTreeSet::new(),
        &AuditOptions::default(),
    );

    assert!(
        claims.is_empty(),
        "degraded template code doc should have no unsupported claims, got {claims:?}"
    );
}

#[test]
fn generated_status_code_docs_are_grounded_by_file_provenance() {
    // A code page with landed AI narrative (ai_generation_status: generated) is
    // still grounded by its file provenance: the narrative is about that one
    // indexed file, and the per-line claim model cannot meaningfully audit
    // flowing prose. It is exempt, exactly like a template-projected page.
    let markdown = r#"---
title: src/mathutil.py
type: code_file
provenance:
- file: src/mathutil.py
generated_by: gcode-codewiki
trust: generated
freshness: indexed
ai_route: daemon
ai_fallback: false
ai_generation_status: generated
---

# src/mathutil.py

This narrative sentence describes the file and is grounded by its provenance.
"#;
    let page = test_codewiki_page("code/files/src/mathutil.py.md", markdown);

    let claims = unsupported_claims(
        &page,
        &ProvenanceGraph::default(),
        &Arc::new(Vec::new()),
        &BTreeSet::new(),
        &AuditOptions::default(),
    );

    assert!(
        claims.is_empty(),
        "generated-status code doc with file provenance should be grounded, got {claims:?}"
    );
}

#[test]
fn aggregate_code_pages_with_empty_provenance_are_exempt() {
    // The `code/repo.md` aggregate rolls up the whole tree and carries no
    // per-file frontmatter provenance (`provenance: []`), but it is still a
    // codewiki index projection (generated_by + trust: generated), so its
    // mechanical module counts and links are exempt.
    let markdown = r#"---
title: Repository Overview
type: code_repo
provenance: []
generated_by: gcode-codewiki
trust: generated
freshness: indexed
stub: true
ai_route: off
ai_fallback: false
ai_generation_status: skipped
---

# Repository Overview

Repository code documentation covers 2553 files across 275 modules.

Module: [[code/modules/crates|crates]]
Summary: `crates` contains 0 direct files and 4 child modules.
"#;
    let page = test_codewiki_page("code/repo.md", markdown);

    let claims = unsupported_claims(
        &page,
        &ProvenanceGraph::default(),
        &Arc::new(Vec::new()),
        &BTreeSet::new(),
        &AuditOptions::default(),
    );

    assert!(
        claims.is_empty(),
        "aggregate code projection page should be exempt, got {claims:?}"
    );
}

#[test]
fn frontmatter_migration_audits_legacy_and_shared_sources_equivalently() {
    let legacy = r#"---
title: crates/example.rs
source_files:
- file: crates/example.rs
  ranges:
  - 1-12
---

# crates/example.rs

Signature: `fn example() -> bool {`
"#;
    let canonical = r#"---
title: crates/example.rs
provenance:
- file: crates/example.rs
  ranges:
  - 1-12
generated_by: gcode-codewiki
trust: generated
freshness: indexed
---

# crates/example.rs

Signature: `fn example() -> bool {`
"#;

    let legacy_page = test_codewiki_page("code/files/crates/example.rs.md", legacy);
    let canonical_page = test_codewiki_page("code/files/crates/example.rs.md", canonical);

    assert!(!has_codewiki_frontmatter_source_spans(&legacy_page));
    assert!(has_codewiki_frontmatter_source_spans(&canonical_page));
    assert_eq!(
        unsupported_claims(
            &legacy_page,
            &ProvenanceGraph::default(),
            &Arc::new(Vec::new()),
            &BTreeSet::new(),
            &AuditOptions::default(),
        )
        .len(),
        1
    );
    assert!(
        unsupported_claims(
            &canonical_page,
            &ProvenanceGraph::default(),
            &Arc::new(Vec::new()),
            &BTreeSet::new(),
            &AuditOptions::default(),
        )
        .is_empty()
    );
}

#[test]
fn manifest_backed_source_digest_is_page_level_supported() {
    let temp = tempfile::tempdir().expect("tempdir");
    let root = temp.path();
    let record = SourceManifest::register(
        root,
        SourceDraft::url(
            "https://example.com/digest",
            "2026-05-29T12:00:00Z",
            "digest body",
        )
        .with_citation("Digest Source"),
    )
    .expect("source registered");

    let page = root.join(format!("knowledge/sources/{}.md", record.id));
    std::fs::create_dir_all(page.parent().expect("page parent")).expect("create sources dir");
    std::fs::write(
        &page,
        format!(
            "---\ntitle: Digest\nsource_kind: url\nsource_hash: {}\n---\n# Digest\nUncited prose claim from the digest.\nSignature: `fn example() -> bool {{`\n",
            record.content_hash
        ),
    )
    .expect("write digest page");

    let report = run(root, ScopeIdentity::project("project-123")).expect("audit runs");

    let digest_path = PathBuf::from(format!("knowledge/sources/{}.md", record.id));
    assert!(
        report
            .unsupported_claims
            .iter()
            .all(|claim| claim.path != digest_path),
        "manifest-backed digest claims should be page-level supported"
    );
}

#[test]
fn source_digest_without_manifest_match_is_still_audited() {
    let temp = tempfile::tempdir().expect("tempdir");
    let root = temp.path();
    let orphan = root.join("knowledge/sources/orphan.md");
    std::fs::create_dir_all(orphan.parent().expect("page parent")).expect("create sources dir");
    std::fs::write(
        &orphan,
        "---\ntitle: Orphan\nsource_kind: url\nsource_hash: not-a-registered-hash\n---\n# Orphan\nUncited prose claim with an orphan hash.\n",
    )
    .expect("write orphan digest");
    let hashless = root.join("knowledge/sources/hashless.md");
    std::fs::write(
        &hashless,
        "---\ntitle: Hashless\nsource_kind: url\n---\n# Hashless\nUncited prose claim without a source hash.\n",
    )
    .expect("write hashless digest");

    let report = run(root, ScopeIdentity::project("project-123")).expect("audit runs");

    assert!(
        report
            .unsupported_claims
            .iter()
            .any(|claim| claim.path == Path::new("knowledge/sources/orphan.md")),
        "orphan-hash digest should still be audited"
    );
    assert!(
        report
            .unsupported_claims
            .iter()
            .any(|claim| claim.path == Path::new("knowledge/sources/hashless.md")),
        "digest without a source hash should still be audited"
    );
}

#[test]
fn manifest_backed_digest_exemption_requires_sources_path_and_covers_all_kinds() {
    let markdown = "---\ntitle: Digest\nsource_kind: session\nsource_hash: hash-1\n---\n# Digest\nUncited prose claim from the digest.\nSignature: `fn example() -> bool {`\n";
    let digest = test_codewiki_page("knowledge/sources/src-1.md", markdown);
    let topic = test_codewiki_page("knowledge/topics/src-1.md", markdown);
    let hashes = BTreeSet::from(["hash-1".to_string()]);

    assert!(is_manifest_backed_source_digest(&digest, &hashes));
    assert!(!is_manifest_backed_source_digest(&topic, &hashes));
    assert!(!is_manifest_backed_source_digest(&digest, &BTreeSet::new()));

    let exempted = unsupported_claims(
        &digest,
        &ProvenanceGraph::default(),
        &Arc::new(Vec::new()),
        &hashes,
        &AuditOptions::default(),
    );
    assert!(
        exempted.is_empty(),
        "prose and structural claims are exempt"
    );

    let unmatched = unsupported_claims(
        &digest,
        &ProvenanceGraph::default(),
        &Arc::new(Vec::new()),
        &BTreeSet::new(),
        &AuditOptions::default(),
    );
    assert!(
        !unmatched.is_empty(),
        "no manifest match means no exemption"
    );
}

fn test_codewiki_page(path: &str, markdown: &str) -> WikiPage {
    WikiPage {
        path: PathBuf::from(path),
        relative_path: PathBuf::from(path),
        markdown: markdown.to_string(),
        parsed: crate::markdown::parse_markdown(path, markdown, std::iter::empty::<&str>())
            .expect("parse markdown"),
        has_frontmatter: true,
    }
}

#[test]
fn configured_ignored_sections_extend_defaults() {
    let page = WikiPage {
        path: PathBuf::from("knowledge/topics/release.md"),
        relative_path: PathBuf::from("knowledge/topics/release.md"),
        markdown: "# Release\nClaim needing support.\n## Notes\nIgnored internal note.\n## Sources\nIgnored source note.\n".to_string(),
        parsed: crate::markdown::parse_markdown(
            "knowledge/topics/release.md",
            "# Release\n",
            std::iter::empty::<&str>(),
        )
        .expect("parse markdown"),
        has_frontmatter: false,
    };
    let options = AuditOptions::default().with_additional_ignored_sections(["Notes"]);

    let claims = claim_lines(&page, &options);

    assert_eq!(claims.len(), 1);
    assert_eq!(claims[0].text, "Claim needing support.");
}

#[test]
fn source_excerpts_section_is_ignored() {
    // Daemon-synthesis concept pages quote verbatim source excerpts under a
    // `## Source excerpts` heading; each line names its session and is source
    // material, not a synthesized claim (#17704).
    let page = test_codewiki_page(
        "knowledge/concepts/example-concept.md",
        "---\ntitle: Example\nsource_kind: concept\n---\n# Example\n## Source excerpts\n- Session: 019d79c6 — 2026-04-10: # Session heading\n",
    );

    let claims = claim_lines(&page, &AuditOptions::default());

    assert!(
        claims.is_empty(),
        "source-excerpt lines should not be claims, got {claims:?}"
    );
}

#[test]
fn daemon_synthesis_concept_overview_is_credited_by_section_attribution() {
    // A daemon-synthesis concept page attributes its Overview synthesis to the
    // source with a trailing `_Source: [[knowledge/sources/...]]_` line. That
    // section-level attribution must credit the wrapped prose lines that carry
    // no inline citation of their own (#17704).
    let markdown = "---\ntitle: Claude\nsource_kind: concept\nsynthesis_mode: daemon\n---\n# Claude\n## Overview\nClaude is one of the providers investigated during runtime discovery.\n\nThe source also says Claude probing uses hardcoded shortnames.\n\n_Source: [[knowledge/sources/src-93b4c1e401d23dd7-session-019d79c6-9d23-7180-a50b-2622488b0c47|Session]]_\n";
    let page = test_codewiki_page("knowledge/concepts/claude-concept.md", markdown);

    let claims = unsupported_claims(
        &page,
        &ProvenanceGraph::default(),
        &Arc::new(Vec::new()),
        &BTreeSet::new(),
        &AuditOptions::default(),
    );

    assert!(
        claims.is_empty(),
        "daemon-synthesis Overview prose should be credited by its section attribution, got {claims:?}"
    );
}

#[test]
fn curated_concept_prose_without_daemon_synthesis_stays_audited() {
    // The section-attribution crediting is scoped to auto-generated
    // daemon-synthesis pages (`synthesis_mode: daemon`). An otherwise identical
    // page without that marker keeps per-claim auditing, so its uncited prose is
    // still flagged even though the section carries a trailing source line.
    let markdown = "---\ntitle: Claude\nsource_kind: concept\n---\n# Claude\n## Overview\nAn uncited curated assertion about Claude.\n\n_Source: [[knowledge/sources/src-93b4c1e401d23dd7-session-019d79c6-9d23-7180-a50b-2622488b0c47|Session]]_\n";
    let page = test_codewiki_page("knowledge/concepts/claude.md", markdown);

    let claims = unsupported_claims(
        &page,
        &ProvenanceGraph::default(),
        &Arc::new(Vec::new()),
        &BTreeSet::new(),
        &AuditOptions::default(),
    );

    assert_eq!(
        claims.len(),
        1,
        "curated (non-daemon) prose without an inline citation should stay flagged, got {claims:?}"
    );
    assert!(claims[0].claim.contains("uncited curated assertion"));
}

#[test]
fn classifies_extracted_inferred_and_ambiguous_claims() {
    let markdown = r#"---
title: crates/example.rs
type: code_file
provenance:
- file: crates/example.rs
  ranges:
  - 1-12
---

# crates/example.rs

Module: [[code/modules/crates|crates]]

Documents the builder pipeline. [crates/example.rs:3-9]

Uncited operational assertion about the builder.
"#;
    let page = test_codewiki_page("code/files/crates/example.rs.md", markdown);

    let analysis = analyze_claims(
        &page,
        &ProvenanceGraph::default(),
        &Arc::new(Vec::new()),
        &BTreeSet::new(),
        &AuditOptions::default(),
    );

    let classification_for = |needle: &str| {
        analysis
            .classified
            .iter()
            .find(|claim| claim.claim.contains(needle))
            .unwrap_or_else(|| panic!("claim containing {needle:?} in {:?}", analysis.classified))
            .classification
    };
    assert_eq!(
        classification_for("Module:"),
        ClaimClassification::Inferred,
        "structural claim on a source-span-grounded page is inferred"
    );
    assert_eq!(
        classification_for("Documents the builder"),
        ClaimClassification::Extracted,
        "inline code source span is a direct citation"
    );
    assert_eq!(
        classification_for("Uncited operational assertion"),
        ClaimClassification::Ambiguous,
        "prose without provenance is ambiguous"
    );
    assert_eq!(
        analysis.unsupported.len(),
        1,
        "only the ambiguous prose claim is an audit failure, got {:?}",
        analysis.unsupported
    );
    assert!(
        analysis.unsupported[0]
            .claim
            .contains("Uncited operational assertion")
    );
}

#[test]
fn conflict_and_gap_sections_classify_ambiguous_without_audit_failures() {
    let markdown = r#"---
title: Topic
source_kind: topic
---
# Topic

## Conflicting claims

- Source A says the cache is per-project; source B says it is global.

## Missing evidence

- No source covers the shutdown path.
"#;
    let page = test_codewiki_page("knowledge/topics/conflicts.md", markdown);

    let analysis = analyze_claims(
        &page,
        &ProvenanceGraph::default(),
        &Arc::new(Vec::new()),
        &BTreeSet::new(),
        &AuditOptions::default(),
    );

    let flagged: Vec<_> = analysis
        .classified
        .iter()
        .filter(|claim| {
            matches!(
                claim.heading.as_deref(),
                Some("Conflicting claims") | Some("Missing evidence")
            )
        })
        .collect();
    assert_eq!(
        flagged.len(),
        2,
        "conflict/gap claims enter the classified stream, got {:?}",
        analysis.classified
    );
    assert!(
        flagged
            .iter()
            .all(|claim| claim.classification == ClaimClassification::Ambiguous),
        "conflict/gap territory classifies ambiguous, got {flagged:?}"
    );
    assert!(
        analysis.unsupported.is_empty(),
        "explicitly flagged uncertainty is not an audit failure, got {:?}",
        analysis.unsupported
    );
}

#[test]
fn conflict_prefixed_claims_under_normal_headings_stay_unsupported() {
    let markdown = r#"---
title: Topic
source_kind: topic
---
# Topic

conflict: retention window is 30 days vs 90 days across sources.
"#;
    let page = test_codewiki_page("knowledge/topics/prefixed.md", markdown);

    let analysis = analyze_claims(
        &page,
        &ProvenanceGraph::default(),
        &Arc::new(Vec::new()),
        &BTreeSet::new(),
        &AuditOptions::default(),
    );

    assert_eq!(analysis.classified.len(), 1);
    assert_eq!(
        analysis.classified[0].classification,
        ClaimClassification::Ambiguous,
        "conflict-prefixed line is ambiguous territory"
    );
    assert_eq!(
        analysis.unsupported.len(),
        1,
        "a conflict-prefixed line under a normal heading still needs support"
    );
}

#[test]
fn audit_report_serializes_uppercase_claim_classifications() {
    let temp = tempfile::tempdir().expect("tempdir");
    let root = temp.path();
    let page = root.join("knowledge/topics/claims.md");
    std::fs::create_dir_all(page.parent().expect("page parent")).expect("create wiki dir");
    std::fs::write(
        &page,
        "---\ntitle: Claims\nsource_kind: topic\n---\n# Claims\nUnsupported operational claim.\n",
    )
    .expect("write page");

    let report = run(root, ScopeIdentity::topic("ops")).expect("audit runs");
    assert_eq!(report.claims.len(), 1);
    assert_eq!(
        report.claims[0].classification,
        ClaimClassification::Ambiguous
    );

    let json = serde_json::to_value(&report).expect("serialize report");
    assert_eq!(
        json.pointer("/claims/0/classification"),
        Some(&serde_json::Value::String("AMBIGUOUS".to_string()))
    );
}