homeboy 0.63.0

CLI for multi-component deployment and development workflow automation
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
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
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
//! Documentation audit system for extracting and verifying claims from markdown files.
//!
//! This extension provides a doc-centric approach to documentation auditing:
//! 1. Extract claims from documentation (file paths, identifiers, code examples)
//! 2. Verify claims against the actual codebase
//! 3. Correlate docs with git changes to identify priority docs needing review
//! 4. Build an alignment report focused on actionable items

pub mod baseline;
mod claims;
mod tasks;
mod verify;

use std::collections::{HashMap, HashSet};
use std::fs;
use std::path::Path;

pub use claims::{Claim, ClaimConfidence, ClaimType};
pub use tasks::{AuditTask, AuditTaskStatus};
pub use verify::VerifyResult;

use regex::Regex;

use crate::{component, extension, git, utils::is_zero, Result};

/// A doc that needs content review due to referenced files changing.
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct PriorityDoc {
    pub doc: String,
    pub reason: String,
    pub changed_files_referenced: Vec<String>,
    pub code_examples: usize,
    pub action: String,
}

/// A feature found in source code with no mention in documentation.
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct UndocumentedFeature {
    pub name: String,
    pub source_file: String,
    pub line: usize,
    pub pattern: String,
}

/// A feature detected in source code (documented or not).
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct DetectedFeature {
    pub name: String,
    pub source_file: String,
    pub line: usize,
    pub pattern: String,
    pub documented: bool,
    /// Doc comment lines found directly above the feature (e.g. `///`, `/**`, `#`).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub description: Option<String>,
    /// Fields or items inside the feature's block (struct fields, enum variants, etc.).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub fields: Option<Vec<FeatureField>>,
}

/// A field or item inside a detected feature's block.
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct FeatureField {
    pub name: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub description: Option<String>,
}

/// A broken reference that needs fixing.
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct BrokenReference {
    pub doc: String,
    pub line: usize,
    pub claim: String,
    pub confidence: ClaimConfidence,
    /// Surrounding lines from the doc file for context (up to 3 lines around the reference).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub doc_context: Option<Vec<String>>,
    pub action: String,
}

/// Summary counts for the alignment report.
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct AlignmentSummary {
    pub docs_scanned: usize,
    pub priority_docs: usize,
    pub broken_references: usize,
    pub unchanged_docs: usize,
    /// Total features detected by extension-defined patterns (omitted when 0).
    #[serde(default, skip_serializing_if = "is_zero")]
    pub total_features: usize,
    /// Features with at least one mention in documentation (omitted when 0).
    #[serde(default, skip_serializing_if = "is_zero")]
    pub documented_features: usize,
    #[serde(default, skip_serializing_if = "is_zero")]
    pub undocumented_features: usize,
}

/// Result of auditing a component's documentation for content alignment.
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct AuditResult {
    pub component_id: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub baseline_ref: Option<String>,
    pub summary: AlignmentSummary,
    pub changed_files: Vec<String>,
    pub priority_docs: Vec<PriorityDoc>,
    pub broken_references: Vec<BrokenReference>,
    pub undocumented_features: Vec<UndocumentedFeature>,
    /// All detected features (only populated when `--features` flag is set).
    #[serde(skip_serializing_if = "Vec::is_empty")]
    pub detected_features: Vec<DetectedFeature>,
}

/// Audit documentation at a direct filesystem path without a registered component.
///
/// Uses the directory name as the label and defaults to "docs" for the docs
/// directory. Extension patterns and changelog exclusion are not available.
/// When `include_features` is true, the full detected features list is included.
pub fn audit_path(
    path: &str,
    docs_dir_override: Option<&str>,
    include_features: bool,
) -> Result<AuditResult> {
    let source_path = Path::new(path);
    if !source_path.is_dir() {
        return Err(crate::Error::validation_invalid_argument(
            "path",
            format!("'{}' is not a directory", path),
            Some(path.to_string()),
            None,
        ));
    }

    let label = source_path
        .file_name()
        .and_then(|n| n.to_str())
        .unwrap_or("unknown")
        .to_string();

    let docs_dir = docs_dir_override.unwrap_or("docs");
    let docs_dirs = vec![docs_dir.to_string()];
    let docs_path = source_path.join(docs_dir);

    let doc_files = find_doc_files(&docs_path, None);
    let docs_scanned = doc_files.len();

    let mut all_claims = Vec::new();
    let mut doc_contents: HashMap<String, String> = HashMap::new();
    for doc_file in &doc_files {
        let doc_path = docs_path.join(doc_file);
        if let Ok(content) = fs::read_to_string(&doc_path) {
            let claims = claims::extract_claims(&content, doc_file, &[]);
            all_claims.extend(claims);
            doc_contents.insert(doc_file.clone(), content);
        }
    }

    let mut tasks = Vec::new();
    for claim in all_claims {
        let result = verify::verify_claim(&claim, source_path, &docs_path, None);
        let task = tasks::build_task(claim, result);
        tasks.push(task);
    }

    // Get uncommitted changes directly from the path's git repo
    let changed_files = git::get_uncommitted_changes(path)
        .map(|u| {
            let mut files = Vec::new();
            files.extend(u.staged);
            files.extend(u.unstaged);
            files.sort();
            files.dedup();
            files
        })
        .unwrap_or_default();

    let priority_docs = build_priority_docs(&tasks, &changed_files);
    let broken_references = extract_broken_references(&tasks, &doc_contents);

    let feature_result = detect_features(&[], source_path, &docs_dirs, None, &HashMap::new());

    let docs_with_issues: HashSet<_> = priority_docs
        .iter()
        .map(|p| &p.doc)
        .chain(broken_references.iter().map(|b| &b.doc))
        .collect();
    let unchanged_docs = docs_scanned.saturating_sub(docs_with_issues.len());

    Ok(AuditResult {
        component_id: label,
        baseline_ref: None,
        summary: AlignmentSummary {
            docs_scanned,
            priority_docs: priority_docs.len(),
            broken_references: broken_references.len(),
            unchanged_docs,
            total_features: feature_result.total,
            documented_features: feature_result.documented,
            undocumented_features: feature_result.undocumented.len(),
        },
        changed_files,
        priority_docs,
        broken_references,
        undocumented_features: feature_result.undocumented,
        detected_features: if include_features {
            feature_result.all_features
        } else {
            Vec::new()
        },
    })
}

/// Audit a component's documentation and return an alignment report.
///
/// If `docs_dir_override` is provided, it's used instead of the component's
/// configured `docs_dir`/`docs_dirs` (which defaults to "docs").
/// When `include_features` is true, the full detected features list is included.
pub fn audit_component(
    component_id: &str,
    docs_dir_override: Option<&str>,
    include_features: bool,
) -> Result<AuditResult> {
    let comp = component::load(component_id)?;
    let source_path = Path::new(&comp.local_path);

    // Resolve docs directories: CLI override > docs_dirs > docs_dir > default "docs"
    let docs_dirs: Vec<String> = if let Some(override_dir) = docs_dir_override {
        vec![override_dir.to_string()]
    } else if !comp.docs_dirs.is_empty() {
        comp.docs_dirs.clone()
    } else {
        vec![comp.docs_dir.as_deref().unwrap_or("docs").to_string()]
    };

    // Primary docs path (first dir) for claim verification and priority docs
    let docs_path = source_path.join(&docs_dirs[0]);

    // Get changelog target to exclude from audit (historical references are expected).
    // Default to CHANGELOG.md since changelogs inherently contain historical paths.
    let changelog_exclude = comp.changelog_target.as_deref().or(Some("CHANGELOG.md"));

    // Collect ignore patterns from all linked extensions
    let ignore_patterns = collect_extension_ignore_patterns(&comp);

    // Collect feature patterns from all linked extensions
    let feature_patterns = collect_extension_feature_patterns(&comp);

    // Find all documentation files (excluding changelog)
    let doc_files = find_doc_files(&docs_path, changelog_exclude);
    let docs_scanned = doc_files.len();

    // Extract claims from all docs (keep content for context extraction)
    let mut all_claims = Vec::new();
    let mut doc_contents: HashMap<String, String> = HashMap::new();
    for doc_file in &doc_files {
        let doc_path = docs_path.join(doc_file);
        if let Ok(content) = fs::read_to_string(&doc_path) {
            let claims = claims::extract_claims(&content, doc_file, &ignore_patterns);
            all_claims.extend(claims);
            doc_contents.insert(doc_file.clone(), content);
        }
    }

    // Verify claims and build tasks (internal only)
    let mut tasks = Vec::new();
    for claim in all_claims {
        let result = verify::verify_claim(&claim, source_path, &docs_path, Some(component_id));
        let task = tasks::build_task(claim, result);
        tasks.push(task);
    }

    // Get changed files from git (both committed and uncommitted)
    let (changed_files, baseline_ref) = get_changed_files(component_id);

    // Build doc-centric outputs
    let priority_docs = build_priority_docs(&tasks, &changed_files);
    let broken_references = extract_broken_references(&tasks, &doc_contents);

    // Collect feature context extraction rules from extensions
    let context_rules = collect_extension_feature_context(&comp);

    // Detect features and documentation coverage across all source files
    let feature_result = detect_features(
        &feature_patterns,
        source_path,
        &docs_dirs,
        changelog_exclude,
        &context_rules,
    );

    // Calculate unchanged docs (docs with no priority items and no broken refs)
    let docs_with_issues: HashSet<_> = priority_docs
        .iter()
        .map(|p| &p.doc)
        .chain(broken_references.iter().map(|b| &b.doc))
        .collect();
    let unchanged_docs = docs_scanned.saturating_sub(docs_with_issues.len());

    Ok(AuditResult {
        component_id: component_id.to_string(),
        baseline_ref,
        summary: AlignmentSummary {
            docs_scanned,
            priority_docs: priority_docs.len(),
            broken_references: broken_references.len(),
            unchanged_docs,
            total_features: feature_result.total,
            documented_features: feature_result.documented,
            undocumented_features: feature_result.undocumented.len(),
        },
        changed_files,
        priority_docs,
        broken_references,
        undocumented_features: feature_result.undocumented,
        detected_features: if include_features {
            feature_result.all_features
        } else {
            Vec::new()
        },
    })
}

/// Find all markdown files in the docs directory.
///
/// Excludes the changelog file since changelogs contain historical references
/// to file paths that may no longer exist. Uses `changelog_target` from the
/// component config if set, otherwise defaults to excluding `CHANGELOG.md`.
fn find_doc_files(docs_path: &Path, exclude_changelog: Option<&str>) -> Vec<String> {
    let mut docs = Vec::new();

    if !docs_path.exists() {
        return docs;
    }

    // Extract changelog filename for comparison
    let changelog_filename = exclude_changelog
        .and_then(|p| Path::new(p).file_name())
        .and_then(|n| n.to_str())
        .map(|s| s.to_lowercase());

    fn scan_docs(
        dir: &Path,
        prefix: &str,
        docs: &mut Vec<String>,
        changelog_filename: &Option<String>,
    ) {
        if let Ok(entries) = fs::read_dir(dir) {
            for entry in entries.flatten() {
                let path = entry.path();
                let name = entry.file_name().to_string_lossy().to_string();

                if name.starts_with('.') {
                    continue;
                }

                if path.is_file() && name.ends_with(".md") {
                    // Skip changelog file if configured
                    if let Some(changelog) = changelog_filename {
                        if name.to_lowercase() == *changelog {
                            continue;
                        }
                    }

                    let relative = if prefix.is_empty() {
                        name
                    } else {
                        format!("{}/{}", prefix, name)
                    };
                    docs.push(relative);
                } else if path.is_dir() {
                    let new_prefix = if prefix.is_empty() {
                        name.clone()
                    } else {
                        format!("{}/{}", prefix, name)
                    };
                    scan_docs(&path, &new_prefix, docs, changelog_filename);
                }
            }
        }
    }

    scan_docs(docs_path, "", &mut docs, &changelog_filename);
    docs.sort();
    docs
}

/// Get changed files from git, including both uncommitted and committed changes.
/// Returns (changed_files, baseline_ref).
fn get_changed_files(component_id: &str) -> (Vec<String>, Option<String>) {
    // Request diff output to extract committed file changes
    let changes = match git::changes(Some(component_id), None, true) {
        Ok(c) => c,
        Err(_) => return (vec![], None),
    };

    let mut files: Vec<String> = Vec::new();

    // Uncommitted changes
    files.extend(changes.uncommitted.staged.iter().cloned());
    files.extend(changes.uncommitted.unstaged.iter().cloned());

    // Parse committed changes from diff output
    if let Some(ref diff) = changes.diff {
        files.extend(parse_diff_file_paths(diff));
    }

    files.sort();
    files.dedup();

    (files, changes.baseline_ref)
}

/// Parse git diff output to extract changed file paths.
fn parse_diff_file_paths(diff: &str) -> Vec<String> {
    diff.lines()
        .filter(|line| line.starts_with("diff --git "))
        .filter_map(|line| {
            // Format: "diff --git a/path/to/file b/path/to/file"
            line.split(" b/").nth(1).map(|s| s.to_string())
        })
        .collect()
}

/// Build priority docs by grouping tasks by doc and filtering for changed file references.
fn build_priority_docs(tasks: &[AuditTask], changed_files: &[String]) -> Vec<PriorityDoc> {
    // Group tasks by doc file
    let mut docs_map: HashMap<String, Vec<&AuditTask>> = HashMap::new();
    for task in tasks {
        docs_map.entry(task.doc.clone()).or_default().push(task);
    }

    let mut priority_docs: Vec<PriorityDoc> = Vec::new();

    for (doc, doc_tasks) in docs_map {
        // Find which changed files this doc references
        let referenced_changes: Vec<String> = changed_files
            .iter()
            .filter(|f| doc_tasks.iter().any(|t| references_file(&t.claim_value, f)))
            .cloned()
            .collect();

        if referenced_changes.is_empty() {
            continue; // Not a priority doc
        }

        // Count code examples in this doc
        let code_examples = doc_tasks
            .iter()
            .filter(|t| matches!(t.claim_type, ClaimType::CodeExample))
            .count();

        // Build action based on what needs review
        let action = build_doc_action(&referenced_changes, code_examples);

        priority_docs.push(PriorityDoc {
            doc,
            reason: format!(
                "{} referenced source file(s) changed since baseline",
                referenced_changes.len()
            ),
            changed_files_referenced: referenced_changes,
            code_examples,
            action,
        });
    }

    // Sort by impact (most changed files referenced first)
    priority_docs.sort_by(|a, b| {
        b.changed_files_referenced
            .len()
            .cmp(&a.changed_files_referenced.len())
    });

    priority_docs
}

/// Check if a claim value references a changed file.
fn references_file(claim_value: &str, changed_file: &str) -> bool {
    let claim_normalized = claim_value.trim_start_matches('/');
    let file_normalized = changed_file.trim_start_matches('/');

    // Exact path match
    if claim_normalized == file_normalized {
        return true;
    }

    // Directory contains changed file (claim is a directory path like "inc/Engine/")
    if claim_value.ends_with('/') && file_normalized.starts_with(claim_normalized) {
        return true;
    }

    // Basename match (for code examples that reference "ToolExecutor" without full path)
    if let Some(basename) = Path::new(changed_file).file_stem() {
        if let Some(name) = basename.to_str() {
            // Only match if the claim contains the basename as a significant reference
            // Avoid false positives by requiring the name to appear as a word boundary
            if claim_value.contains(name) && name.len() >= 4 {
                return true;
            }
        }
    }

    false
}

/// Build an action description for a priority doc.
fn build_doc_action(changed_files: &[String], code_examples: usize) -> String {
    let files_desc = if changed_files.len() == 1 {
        changed_files[0].clone()
    } else {
        format!("{} files", changed_files.len())
    };

    if code_examples > 0 {
        format!(
            "Documentation may be stale: {} code example(s) reference changed source ({}). Update docs to match current implementation.",
            code_examples, files_desc
        )
    } else {
        format!(
            "Source changed ({}). Review documentation for accuracy against current implementation.",
            files_desc
        )
    }
}

/// Extract broken references from tasks into a separate list.
///
/// Includes surrounding lines from the doc file for context when available.
fn extract_broken_references(
    tasks: &[AuditTask],
    doc_contents: &HashMap<String, String>,
) -> Vec<BrokenReference> {
    tasks
        .iter()
        .filter(|t| matches!(t.status, AuditTaskStatus::Broken))
        .map(|t| {
            let doc_context = extract_doc_context(doc_contents, &t.doc, t.line);
            BrokenReference {
                doc: t.doc.clone(),
                line: t.line,
                claim: t.claim.clone(),
                confidence: t.confidence.clone(),
                doc_context,
                action: t.action.clone().unwrap_or_else(|| {
                    "Stale reference. Update or remove from documentation.".to_string()
                }),
            }
        })
        .collect()
}

/// Extract surrounding lines from a doc file for context.
///
/// Returns up to 3 lines centered on the target line (1 before, target, 1 after).
/// Each line is prefixed with its line number for easy navigation.
fn extract_doc_context(
    doc_contents: &HashMap<String, String>,
    doc_file: &str,
    line: usize,
) -> Option<Vec<String>> {
    let content = doc_contents.get(doc_file)?;
    let lines: Vec<&str> = content.lines().collect();

    if line == 0 || line > lines.len() {
        return None;
    }

    let line_idx = line - 1; // 0-indexed
    let start = line_idx.saturating_sub(1);
    let end = (line_idx + 2).min(lines.len()); // exclusive, up to 1 line after

    let context: Vec<String> = (start..end)
        .map(|i| format!("{}: {}", i + 1, lines[i]))
        .collect();

    if context.is_empty() {
        None
    } else {
        Some(context)
    }
}

/// Collect feature detection patterns from all linked extensions.
fn collect_extension_feature_patterns(comp: &component::Component) -> Vec<String> {
    let mut patterns = Vec::new();
    if let Some(ref extensions) = comp.extensions {
        for extension_id in extensions.keys() {
            if let Ok(manifest) = extension::load_extension(extension_id) {
                patterns.extend(manifest.audit_feature_patterns().to_vec());
            }
        }
    }
    patterns
}

/// Collect feature context extraction rules from all linked extensions.
fn collect_extension_feature_context(
    comp: &component::Component,
) -> HashMap<String, extension::FeatureContextRule> {
    let mut rules = HashMap::new();
    if let Some(ref extensions) = comp.extensions {
        for extension_id in extensions.keys() {
            if let Ok(manifest) = extension::load_extension(extension_id) {
                for (key, rule) in manifest.audit_feature_context() {
                    rules.insert(key.clone(), rule.clone());
                }
            }
        }
    }
    rules
}

/// Result of feature detection including coverage counts.
struct FeatureDetectionResult {
    /// Total unique feature names found in source code.
    total: usize,
    /// Features that have at least one mention in documentation.
    documented: usize,
    /// Features with no documentation mention.
    undocumented: Vec<UndocumentedFeature>,
    /// All detected features (documented and undocumented).
    all_features: Vec<DetectedFeature>,
}

/// Extract doc comment lines above a byte position in source content.
///
/// Walks backwards from the match position, collecting lines that start with
/// doc comment markers: `///`, `//!`, `*`, `/**`, `#` (PHP/Python), `--` (SQL/Lua).
/// Strips the comment markers and returns the combined text.
fn extract_doc_comment(content: &str, byte_pos: usize) -> Option<String> {
    // Find the line containing byte_pos
    let before = &content[..byte_pos];
    let lines: Vec<&str> = before.lines().collect();

    if lines.is_empty() {
        return None;
    }

    let mut comment_lines: Vec<String> = Vec::new();

    // Walk backwards from the line before the match
    for line in lines.iter().rev() {
        let trimmed = line.trim();

        // Rust/JS/TS doc comments
        if let Some(text) = trimmed.strip_prefix("///") {
            comment_lines.push(text.trim().to_string());
        } else if let Some(text) = trimmed.strip_prefix("//!") {
            comment_lines.push(text.trim().to_string());
        }
        // Block comment continuation
        else if let Some(text) = trimmed.strip_prefix("* ") {
            // Skip opening /** and closing */
            if !trimmed.starts_with("/**") && !trimmed.starts_with("*/") {
                comment_lines.push(text.trim().to_string());
            }
        } else if trimmed == "*" {
            // Empty line in block comment
            comment_lines.push(String::new());
        }
        // PHP/Python doc comment openers
        else if trimmed.starts_with("/**") || trimmed.starts_with("\"\"\"") {
            // Opening line of a block — may have text after marker
            if let Some(text) = trimmed.strip_prefix("/**") {
                let text = text.trim();
                if !text.is_empty() && text != "*" {
                    comment_lines.push(text.to_string());
                }
            }
            break; // We've found the start of the block
        }
        // Python/Ruby comments
        else if let Some(text) = trimmed.strip_prefix("# ") {
            comment_lines.push(text.trim().to_string());
        } else if trimmed == "#" {
            comment_lines.push(String::new());
        }
        // Attribute lines (skip, keep going)
        else if trimmed.starts_with("#[") || trimmed.starts_with("#![") {
            continue;
        }
        // Empty lines between attributes and doc comments — keep going
        else if trimmed.is_empty() {
            // If we already have comment lines, an empty line means we've left the comment block
            if !comment_lines.is_empty() {
                break;
            }
            continue;
        }
        // Anything else means we've left the comment block
        else {
            break;
        }
    }

    if comment_lines.is_empty() {
        return None;
    }

    // Reverse since we walked backwards
    comment_lines.reverse();

    // Join and clean up
    let text = comment_lines.join(" ").trim().to_string();
    if text.is_empty() {
        None
    } else {
        Some(text)
    }
}

/// Extract field names and their doc comments from a block following a byte position.
///
/// Finds the opening `{` after the match, then extracts each field/variant until
/// the matching closing `}`. Handles Rust struct fields, enum variants, and
/// generic key-value patterns.
fn extract_block_fields(content: &str, byte_pos: usize) -> Option<Vec<FeatureField>> {
    let after = &content[byte_pos..];

    // Find the opening brace
    let brace_offset = after.find('{')?;
    let block_start = byte_pos + brace_offset + 1;

    // Find the matching closing brace (tracking nesting)
    let mut depth = 1;
    let mut pos = block_start;
    let bytes = content.as_bytes();
    while pos < content.len() && depth > 0 {
        match bytes[pos] {
            b'{' => depth += 1,
            b'}' => depth -= 1,
            _ => {}
        }
        if depth > 0 {
            pos += 1;
        }
    }

    if depth != 0 {
        return None; // Unbalanced braces
    }

    let block_content = &content[block_start..pos];
    let lines: Vec<&str> = block_content.lines().collect();

    let mut fields = Vec::new();
    let mut pending_doc: Vec<String> = Vec::new();

    for line in &lines {
        let trimmed = line.trim();

        // Collect doc comments for the next field
        if let Some(text) = trimmed.strip_prefix("///") {
            pending_doc.push(text.trim().to_string());
            continue;
        }

        // Skip attributes
        if trimmed.starts_with("#[") || trimmed.is_empty() {
            continue;
        }

        // Try to extract a field name
        // Strip visibility: pub, pub(crate), pub(super)
        let rest = trimmed
            .strip_prefix("pub(crate) ")
            .or_else(|| trimmed.strip_prefix("pub(super) "))
            .or_else(|| trimmed.strip_prefix("pub "))
            .unwrap_or(trimmed);

        // Extract identifier (up to :, (, =, <, ,, {, or whitespace)
        let name: String = rest
            .chars()
            .take_while(|c| c.is_alphanumeric() || *c == '_')
            .collect();

        if name.is_empty() {
            pending_doc.clear();
            continue;
        }

        // Must look like a field declaration or enum variant
        let after_name = rest[name.len()..].trim_start();
        let is_field = after_name.starts_with(':')
            || after_name.starts_with('(')
            || after_name.starts_with('{')
            || after_name.starts_with(',')
            || after_name.is_empty()
            || after_name == ","
            || after_name.starts_with("//");

        if is_field {
            let description = if pending_doc.is_empty() {
                None
            } else {
                Some(pending_doc.join(" "))
            };

            fields.push(FeatureField { name, description });
        }

        pending_doc.clear();
    }

    if fields.is_empty() {
        None
    } else {
        Some(fields)
    }
}

/// Detect features across all source files and report documentation coverage.
///
/// Scans the entire source tree (excluding vendor/node_modules/test dirs) for
/// feature registrations matching the configured patterns. Returns counts of
/// total, documented, and undocumented features.
///
/// Documentation is gathered from:
/// 1. All configured docs directories
/// 2. README.md and README.txt in the project root (auto-included)
fn detect_features(
    feature_patterns: &[String],
    source_path: &Path,
    docs_dirs: &[String],
    changelog_exclude: Option<&str>,
    context_rules: &HashMap<String, extension::FeatureContextRule>,
) -> FeatureDetectionResult {
    let empty = FeatureDetectionResult {
        total: 0,
        documented: 0,
        undocumented: Vec::new(),
        all_features: Vec::new(),
    };

    if feature_patterns.is_empty() {
        return empty;
    }

    // Compile regexes once
    let compiled: Vec<(Regex, String)> = feature_patterns
        .iter()
        .filter_map(|p| Regex::new(p).ok().map(|r| (r, p.clone())))
        .collect();

    if compiled.is_empty() {
        return empty;
    }

    // Collect all doc content from all configured directories
    let mut all_doc_parts: Vec<String> = Vec::new();

    for docs_dir in docs_dirs {
        let docs_path = source_path.join(docs_dir);
        let doc_files = find_doc_files(&docs_path, changelog_exclude);
        for f in &doc_files {
            if let Ok(content) = fs::read_to_string(docs_path.join(f)) {
                all_doc_parts.push(content);
            }
        }
    }

    // Auto-include README files from project root
    for readme in &["README.md", "readme.md", "README.txt", "readme.txt"] {
        let readme_path = source_path.join(readme);
        if readme_path.exists() {
            if let Ok(content) = fs::read_to_string(&readme_path) {
                all_doc_parts.push(content);
            }
        }
    }

    let all_doc_content = all_doc_parts.join("\n");

    // Find all source files in the project (excluding common non-source dirs)
    let source_files = find_source_files(source_path);

    let mut undocumented = Vec::new();
    let mut all_features = Vec::new();
    let mut seen_names: HashSet<String> = HashSet::new();
    let mut documented_count: usize = 0;

    for file in &source_files {
        let file_path = source_path.join(file);
        let content = match fs::read_to_string(&file_path) {
            Ok(c) => c,
            Err(_) => continue,
        };

        // Build line offset table for mapping byte positions to line numbers
        let line_offsets: Vec<usize> = std::iter::once(0)
            .chain(content.match_indices('\n').map(|(i, _)| i + 1))
            .collect();

        for (regex, pattern) in &compiled {
            // Search the full file content (not line-by-line) to handle
            // multi-line registrations like:
            //   register_rest_route(
            //       'namespace/v1',
            for caps in regex.captures_iter(&content) {
                if let Some(name_match) = caps.get(1) {
                    let name = name_match.as_str().to_string();
                    // Deduplicate: only count first occurrence of each feature name
                    if seen_names.contains(&name) {
                        continue;
                    }
                    seen_names.insert(name.clone());

                    let byte_pos = name_match.start();
                    let line_num = line_offsets.partition_point(|&offset| offset <= byte_pos);
                    let is_documented = all_doc_content.contains(&name);

                    // Extract context based on extension rules
                    let rule = context_rules
                        .iter()
                        .find(|(key, _)| pattern.contains(key.as_str()))
                        .map(|(_, r)| r);

                    let description = if rule.is_some_and(|r| r.doc_comment) {
                        extract_doc_comment(&content, byte_pos)
                    } else {
                        None
                    };

                    let fields = if rule.is_some_and(|r| r.block_fields) {
                        extract_block_fields(&content, byte_pos)
                    } else {
                        None
                    };

                    all_features.push(DetectedFeature {
                        name: name.clone(),
                        source_file: file.clone(),
                        line: line_num,
                        pattern: pattern.clone(),
                        documented: is_documented,
                        description,
                        fields,
                    });

                    if is_documented {
                        documented_count += 1;
                    } else {
                        undocumented.push(UndocumentedFeature {
                            name,
                            source_file: file.clone(),
                            line: line_num,
                            pattern: pattern.clone(),
                        });
                    }
                }
            }
        }
    }

    FeatureDetectionResult {
        total: seen_names.len(),
        documented: documented_count,
        undocumented,
        all_features,
    }
}

/// Directories to skip when scanning for source files.
const SKIP_DIRS: &[&str] = &[
    "vendor",
    "node_modules",
    ".git",
    "target",
    "build",
    "dist",
    "__pycache__",
    ".svn",
];

/// Recursively find all non-markdown source files, excluding common dependency dirs.
fn find_source_files(source_path: &Path) -> Vec<String> {
    let mut files = Vec::new();
    collect_source_files(source_path, source_path, &mut files);
    files.sort();
    files
}

fn collect_source_files(base: &Path, dir: &Path, files: &mut Vec<String>) {
    let entries = match fs::read_dir(dir) {
        Ok(e) => e,
        Err(_) => return,
    };

    for entry in entries.flatten() {
        let path = entry.path();
        let name = entry.file_name().to_string_lossy().to_string();

        if name.starts_with('.') {
            continue;
        }

        if path.is_dir() {
            if SKIP_DIRS.contains(&name.as_str()) {
                continue;
            }
            collect_source_files(base, &path, files);
        } else if path.is_file() && !name.ends_with(".md") {
            if let Ok(relative) = path.strip_prefix(base) {
                files.push(relative.to_string_lossy().to_string());
            }
        }
    }
}

/// Collect audit ignore patterns from all linked extensions.
fn collect_extension_ignore_patterns(comp: &component::Component) -> Vec<String> {
    let mut patterns = Vec::new();
    if let Some(ref extensions) = comp.extensions {
        for extension_id in extensions.keys() {
            if let Ok(manifest) = extension::load_extension(extension_id) {
                patterns.extend(manifest.audit_ignore_claim_patterns().to_vec());
            }
        }
    }
    patterns
}

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

    #[test]
    fn test_parse_diff_file_paths_basic() {
        let diff = r#"diff --git a/src/main.rs b/src/main.rs
index abc123..def456 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -1,3 +1,4 @@
+// New comment
 fn main() {}
diff --git a/src/lib.rs b/src/lib.rs
index 111222..333444 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -1 +1 @@
-old
+new
"#;
        let files = parse_diff_file_paths(diff);
        assert_eq!(files, vec!["src/main.rs", "src/lib.rs"]);
    }

    #[test]
    fn test_parse_diff_file_paths_empty() {
        let diff = "";
        let files = parse_diff_file_paths(diff);
        assert!(files.is_empty());
    }

    #[test]
    fn test_parse_diff_file_paths_no_diff_lines() {
        let diff = "Some random text\nwithout diff headers";
        let files = parse_diff_file_paths(diff);
        assert!(files.is_empty());
    }

    #[test]
    fn test_references_file_exact_match() {
        assert!(references_file("src/main.rs", "src/main.rs"));
        assert!(references_file("/src/main.rs", "src/main.rs"));
        assert!(references_file("src/main.rs", "/src/main.rs"));
    }

    #[test]
    fn test_references_file_directory_contains() {
        assert!(references_file("inc/Engine/", "inc/Engine/AI/Tools.php"));
        assert!(references_file("/inc/Engine/", "inc/Engine/AI/Tools.php"));
    }

    #[test]
    fn test_references_file_directory_no_match() {
        // Directory path should not match files outside it
        assert!(!references_file("inc/Engine/", "inc/Other/file.php"));
        // Non-directory paths should not use directory matching
        assert!(!references_file("inc/Engine", "inc/Engine/AI/Tools.php"));
    }

    #[test]
    fn test_references_file_basename_match() {
        // Code examples often reference class names without full paths
        assert!(references_file(
            "ToolExecutor::run()",
            "inc/Engine/ToolExecutor.php"
        ));
        assert!(references_file("new BaseTool()", "src/tools/BaseTool.rs"));
    }

    #[test]
    fn test_references_file_basename_short_name_no_match() {
        // Short names (< 4 chars) should not match to avoid false positives
        assert!(!references_file("use AI;", "src/AI.php"));
    }

    #[test]
    fn test_references_file_no_match() {
        assert!(!references_file("totally/different.rs", "src/main.rs"));
        assert!(!references_file("random text", "src/file.rs"));
    }

    #[test]
    fn test_build_doc_action_single_file() {
        let action = build_doc_action(&["src/main.rs".to_string()], 0);
        assert!(action.contains("src/main.rs"));
        assert!(action.contains("Source changed"));
    }

    #[test]
    fn test_build_doc_action_multiple_files() {
        let action = build_doc_action(&["src/main.rs".to_string(), "src/lib.rs".to_string()], 0);
        assert!(action.contains("2 files"));
    }

    #[test]
    fn test_build_doc_action_with_code_examples() {
        let action = build_doc_action(&["src/main.rs".to_string()], 3);
        assert!(action.contains("3 code example(s)"));
        assert!(action.contains("stale"));
    }

    #[test]
    fn test_extract_broken_references() {
        let tasks = vec![
            AuditTask {
                doc: "api/index.md".to_string(),
                line: 10,
                claim: "file path `src/old.rs`".to_string(),
                claim_type: ClaimType::FilePath,
                claim_value: "src/old.rs".to_string(),
                confidence: ClaimConfidence::Real,
                status: AuditTaskStatus::Broken,
                action: Some("File no longer exists".to_string()),
            },
            AuditTask {
                doc: "api/index.md".to_string(),
                line: 20,
                claim: "file path `src/main.rs`".to_string(),
                claim_type: ClaimType::FilePath,
                claim_value: "src/main.rs".to_string(),
                confidence: ClaimConfidence::Real,
                status: AuditTaskStatus::Verified,
                action: None,
            },
        ];

        let doc_contents = HashMap::new(); // No content for context extraction
        let broken = extract_broken_references(&tasks, &doc_contents);
        assert_eq!(broken.len(), 1);
        assert_eq!(broken[0].doc, "api/index.md");
        assert_eq!(broken[0].line, 10);
        assert_eq!(broken[0].action, "File no longer exists");
        assert!(broken[0].doc_context.is_none()); // No content provided
    }

    #[test]
    fn test_build_priority_docs_filters_by_changed_files() {
        let tasks = vec![
            AuditTask {
                doc: "api/tools.md".to_string(),
                line: 10,
                claim: "file path `inc/ToolExecutor.php`".to_string(),
                claim_type: ClaimType::FilePath,
                claim_value: "inc/ToolExecutor.php".to_string(),
                confidence: ClaimConfidence::Real,
                status: AuditTaskStatus::Verified,
                action: None,
            },
            AuditTask {
                doc: "api/other.md".to_string(),
                line: 5,
                claim: "file path `inc/Unrelated.php`".to_string(),
                claim_type: ClaimType::FilePath,
                claim_value: "inc/Unrelated.php".to_string(),
                confidence: ClaimConfidence::Real,
                status: AuditTaskStatus::Verified,
                action: None,
            },
        ];

        let changed_files = vec!["inc/ToolExecutor.php".to_string()];
        let priority = build_priority_docs(&tasks, &changed_files);

        // Only api/tools.md should be a priority doc
        assert_eq!(priority.len(), 1);
        assert_eq!(priority[0].doc, "api/tools.md");
        assert_eq!(
            priority[0].changed_files_referenced,
            vec!["inc/ToolExecutor.php"]
        );
    }

    #[test]
    fn test_build_priority_docs_sorts_by_impact() {
        let tasks = vec![
            AuditTask {
                doc: "doc_one.md".to_string(),
                line: 1,
                claim_type: ClaimType::FilePath,
                claim: "".to_string(),
                claim_value: "file1.rs".to_string(),
                confidence: ClaimConfidence::Real,
                status: AuditTaskStatus::Verified,
                action: None,
            },
            AuditTask {
                doc: "doc_two.md".to_string(),
                line: 1,
                claim_type: ClaimType::FilePath,
                claim: "".to_string(),
                claim_value: "file1.rs".to_string(),
                confidence: ClaimConfidence::Real,
                status: AuditTaskStatus::Verified,
                action: None,
            },
            AuditTask {
                doc: "doc_two.md".to_string(),
                line: 2,
                claim_type: ClaimType::FilePath,
                claim: "".to_string(),
                claim_value: "file2.rs".to_string(),
                confidence: ClaimConfidence::Real,
                status: AuditTaskStatus::Verified,
                action: None,
            },
        ];

        let changed_files = vec!["file1.rs".to_string(), "file2.rs".to_string()];
        let priority = build_priority_docs(&tasks, &changed_files);

        // doc_two.md references 2 files, doc_one.md references 1
        // So doc_two.md should come first
        assert_eq!(priority.len(), 2);
        assert_eq!(priority[0].doc, "doc_two.md");
        assert_eq!(priority[0].changed_files_referenced.len(), 2);
        assert_eq!(priority[1].doc, "doc_one.md");
        assert_eq!(priority[1].changed_files_referenced.len(), 1);
    }

    #[test]
    fn test_detect_features_finds_missing() {
        let dir = tempfile::tempdir().unwrap();
        let source_path = dir.path();
        let docs_path = source_path.join("docs");
        fs::create_dir_all(&docs_path).unwrap();

        // Create a source file with a feature registration
        fs::write(
            source_path.join("plugin.js"),
            "registerStepType('coolStep', handler);\nregisterStepType('docStep', handler);\n",
        )
        .unwrap();

        // Create a doc file that mentions docStep but not coolStep
        fs::write(
            docs_path.join("guide.md"),
            "Use the docStep to do things.\n",
        )
        .unwrap();

        let patterns = vec![r#"registerStepType\(\s*'(\w+)'"#.to_string()];

        let result = detect_features(
            &patterns,
            source_path,
            &["docs".to_string()],
            None,
            &HashMap::new(),
        );

        assert_eq!(result.total, 2);
        assert_eq!(result.documented, 1);
        assert_eq!(result.undocumented.len(), 1);
        assert_eq!(result.undocumented[0].name, "coolStep");
        assert_eq!(result.undocumented[0].source_file, "plugin.js");
        assert_eq!(result.undocumented[0].line, 1);
    }

    #[test]
    fn test_detect_features_empty_when_no_patterns() {
        let dir = tempfile::tempdir().unwrap();
        let result = detect_features(
            &[],
            dir.path(),
            &["docs".to_string()],
            None,
            &HashMap::new(),
        );
        assert_eq!(result.total, 0);
        assert!(result.undocumented.is_empty());
    }

    #[test]
    fn test_detect_features_skips_md_files() {
        let dir = tempfile::tempdir().unwrap();
        let source_path = dir.path();
        let docs_path = source_path.join("docs");
        fs::create_dir_all(&docs_path).unwrap();

        fs::write(
            source_path.join("notes.md"),
            "registerStepType('hidden', h);\n",
        )
        .unwrap();

        let patterns = vec![r#"registerStepType\(\s*'(\w+)'"#.to_string()];

        let result = detect_features(
            &patterns,
            source_path,
            &["docs".to_string()],
            None,
            &HashMap::new(),
        );
        assert_eq!(result.total, 0);
        assert!(result.undocumented.is_empty());
    }

    #[test]
    fn test_detect_features_all_documented() {
        let dir = tempfile::tempdir().unwrap();
        let source_path = dir.path();
        let docs_path = source_path.join("docs");
        fs::create_dir_all(&docs_path).unwrap();

        fs::write(
            source_path.join("plugin.js"),
            "registerStepType('myStep', handler);\n",
        )
        .unwrap();
        fs::write(
            docs_path.join("guide.md"),
            "The myStep feature does things.\n",
        )
        .unwrap();

        let patterns = vec![r#"registerStepType\(\s*'(\w+)'"#.to_string()];

        let result = detect_features(
            &patterns,
            source_path,
            &["docs".to_string()],
            None,
            &HashMap::new(),
        );
        assert_eq!(result.total, 1);
        assert_eq!(result.documented, 1);
        assert!(result.undocumented.is_empty());
    }

    #[test]
    fn test_detect_features_scans_subdirectories() {
        let dir = tempfile::tempdir().unwrap();
        let source_path = dir.path();
        let docs_path = source_path.join("docs");
        let inc_path = source_path.join("inc").join("Api");
        fs::create_dir_all(&docs_path).unwrap();
        fs::create_dir_all(&inc_path).unwrap();

        // Feature in a nested subdirectory (not in changed files)
        fs::write(
            inc_path.join("Routes.php"),
            "register_rest_route('myplugin/v1', '/items', []);\n",
        )
        .unwrap();

        let patterns = vec![r#"register_rest_route\(\s*['"](\w[\w-]*/v\d+)['"]"#.to_string()];

        let result = detect_features(
            &patterns,
            source_path,
            &["docs".to_string()],
            None,
            &HashMap::new(),
        );

        assert_eq!(result.undocumented.len(), 1);
        assert_eq!(result.undocumented[0].name, "myplugin/v1");
        assert!(result.undocumented[0].source_file.contains("Routes.php"));
    }

    #[test]
    fn test_detect_features_skips_vendor() {
        let dir = tempfile::tempdir().unwrap();
        let source_path = dir.path();
        let docs_path = source_path.join("docs");
        let vendor_path = source_path.join("vendor").join("lib");
        fs::create_dir_all(&docs_path).unwrap();
        fs::create_dir_all(&vendor_path).unwrap();

        // Feature in vendor should be ignored
        fs::write(
            vendor_path.join("plugin.php"),
            "register_rest_route('vendor/v1', '/stuff', []);\n",
        )
        .unwrap();

        let patterns = vec![r#"register_rest_route\(\s*['"](\w[\w-]*/v\d+)['"]"#.to_string()];

        let result = detect_features(
            &patterns,
            source_path,
            &["docs".to_string()],
            None,
            &HashMap::new(),
        );
        assert_eq!(result.total, 0);
        assert!(result.undocumented.is_empty());
    }

    #[test]
    fn test_detect_features_deduplicates() {
        let dir = tempfile::tempdir().unwrap();
        let source_path = dir.path();
        let docs_path = source_path.join("docs");
        fs::create_dir_all(&docs_path).unwrap();

        // Same feature name registered in two files
        fs::write(
            source_path.join("a.js"),
            "registerStepType('myStep', handler);\n",
        )
        .unwrap();
        fs::write(
            source_path.join("b.js"),
            "registerStepType('myStep', handler);\n",
        )
        .unwrap();

        let patterns = vec![r#"registerStepType\(\s*'(\w+)'"#.to_string()];

        let result = detect_features(
            &patterns,
            source_path,
            &["docs".to_string()],
            None,
            &HashMap::new(),
        );
        assert_eq!(result.total, 1); // Deduplicated
        assert_eq!(result.undocumented.len(), 1);
    }

    #[test]
    fn test_detect_features_reads_readme() {
        let dir = tempfile::tempdir().unwrap();
        let source_path = dir.path();
        let docs_path = source_path.join("docs");
        fs::create_dir_all(&docs_path).unwrap();

        // Feature in source
        fs::write(
            source_path.join("plugin.js"),
            "registerStepType('readmeStep', handler);\nregisterStepType('hiddenStep', handler);\n",
        )
        .unwrap();

        // README.md documents one of them (no docs/ file does)
        fs::write(
            source_path.join("README.md"),
            "This plugin provides readmeStep for automation.\n",
        )
        .unwrap();

        let patterns = vec![r#"registerStepType\(\s*'(\w+)'"#.to_string()];
        let result = detect_features(
            &patterns,
            source_path,
            &["docs".to_string()],
            None,
            &HashMap::new(),
        );

        // readmeStep is documented via README, hiddenStep is not
        assert_eq!(result.total, 2);
        assert_eq!(result.documented, 1);
        assert_eq!(result.undocumented.len(), 1);
        assert_eq!(result.undocumented[0].name, "hiddenStep");
    }

    #[test]
    fn test_detect_features_multiple_dirs() {
        let dir = tempfile::tempdir().unwrap();
        let source_path = dir.path();
        let docs_path = source_path.join("docs");
        let wiki_path = source_path.join("wiki");
        fs::create_dir_all(&docs_path).unwrap();
        fs::create_dir_all(&wiki_path).unwrap();

        // Feature in source
        fs::write(
            source_path.join("plugin.js"),
            "registerStepType('wikiStep', handler);\nregisterStepType('orphanStep', handler);\n",
        )
        .unwrap();

        // Documented in wiki, not in docs
        fs::write(
            wiki_path.join("features.md"),
            "The wikiStep handles wiki operations.\n",
        )
        .unwrap();

        let patterns = vec![r#"registerStepType\(\s*'(\w+)'"#.to_string()];

        // Only scanning docs/ — both undocumented
        let result = detect_features(
            &patterns,
            source_path,
            &["docs".to_string()],
            None,
            &HashMap::new(),
        );
        assert_eq!(result.total, 2);
        assert_eq!(result.undocumented.len(), 2);

        // Scanning both dirs — wikiStep found in wiki/
        let result = detect_features(
            &patterns,
            source_path,
            &["docs".to_string(), "wiki".to_string()],
            None,
            &HashMap::new(),
        );
        assert_eq!(result.total, 2);
        assert_eq!(result.documented, 1);
        assert_eq!(result.undocumented.len(), 1);
        assert_eq!(result.undocumented[0].name, "orphanStep");
    }

    #[test]
    fn test_extract_doc_context_with_content() {
        let mut doc_contents = HashMap::new();
        doc_contents.insert(
            "api/tools.md".to_string(),
            "# Tools\n\nSee `src/old.rs` for details.\n\nMore content here.\n".to_string(),
        );

        // Line 3 is "See `src/old.rs` for details."
        let context = extract_doc_context(&doc_contents, "api/tools.md", 3);
        assert!(context.is_some());
        let lines = context.unwrap();
        assert_eq!(lines.len(), 3); // line 2, 3, 4
        assert!(lines[0].starts_with("2:"));
        assert!(lines[1].contains("src/old.rs"));
        assert!(lines[2].starts_with("4:"));
    }

    #[test]
    fn test_extract_doc_context_first_line() {
        let mut doc_contents = HashMap::new();
        doc_contents.insert("test.md".to_string(), "# Title\nSecond line\n".to_string());

        let context = extract_doc_context(&doc_contents, "test.md", 1);
        assert!(context.is_some());
        let lines = context.unwrap();
        assert_eq!(lines.len(), 2); // line 1, 2 (no line before)
        assert!(lines[0].starts_with("1:"));
    }

    #[test]
    fn test_extract_doc_context_missing_doc() {
        let doc_contents = HashMap::new();
        let context = extract_doc_context(&doc_contents, "nonexistent.md", 1);
        assert!(context.is_none());
    }

    #[test]
    fn test_broken_reference_includes_context() {
        let tasks = vec![AuditTask {
            doc: "api/tools.md".to_string(),
            line: 2,
            claim: "file path `src/old.rs`".to_string(),
            claim_type: ClaimType::FilePath,
            claim_value: "src/old.rs".to_string(),
            confidence: ClaimConfidence::Real,
            status: AuditTaskStatus::Broken,
            action: Some("File no longer exists".to_string()),
        }];

        let mut doc_contents = HashMap::new();
        doc_contents.insert(
            "api/tools.md".to_string(),
            "# API\nSee `src/old.rs` for the tool.\nMore info below.\n".to_string(),
        );

        let broken = extract_broken_references(&tasks, &doc_contents);
        assert_eq!(broken.len(), 1);
        assert!(broken[0].doc_context.is_some());
        let ctx = broken[0].doc_context.as_ref().unwrap();
        assert!(ctx.iter().any(|l| l.contains("src/old.rs")));
    }

    #[test]
    fn test_find_doc_files_excludes_changelog_by_default() {
        let dir = tempfile::tempdir().unwrap();
        let docs_path = dir.path();

        fs::write(docs_path.join("guide.md"), "# Guide\n").unwrap();
        fs::write(
            docs_path.join("CHANGELOG.md"),
            "# Changelog\n## v1.0\n- Removed old/path.rs\n",
        )
        .unwrap();
        fs::write(docs_path.join("api.md"), "# API\n").unwrap();

        // With default CHANGELOG.md exclusion
        let files = find_doc_files(docs_path, Some("CHANGELOG.md"));
        assert_eq!(files.len(), 2);
        assert!(files.contains(&"api.md".to_string()));
        assert!(files.contains(&"guide.md".to_string()));
        assert!(!files.iter().any(|f| f.to_lowercase().contains("changelog")));
    }

    #[test]
    fn test_find_doc_files_changelog_exclusion_case_insensitive() {
        let dir = tempfile::tempdir().unwrap();
        let docs_path = dir.path();

        fs::write(docs_path.join("guide.md"), "# Guide\n").unwrap();
        fs::write(docs_path.join("changelog.md"), "# Changes\n").unwrap();

        // CHANGELOG.md target should match lowercase changelog.md
        let files = find_doc_files(docs_path, Some("CHANGELOG.md"));
        assert_eq!(files.len(), 1);
        assert_eq!(files[0], "guide.md");
    }

    #[test]
    fn test_find_doc_files_no_exclusion_when_none() {
        let dir = tempfile::tempdir().unwrap();
        let docs_path = dir.path();

        fs::write(docs_path.join("guide.md"), "# Guide\n").unwrap();
        fs::write(docs_path.join("CHANGELOG.md"), "# Changelog\n").unwrap();

        // Without exclusion, changelog should be included
        let files = find_doc_files(docs_path, None);
        assert_eq!(files.len(), 2);
        assert!(files.iter().any(|f| f == "CHANGELOG.md"));
    }

    #[test]
    fn test_find_doc_files_custom_changelog_target() {
        let dir = tempfile::tempdir().unwrap();
        let docs_path = dir.path();

        fs::write(docs_path.join("guide.md"), "# Guide\n").unwrap();
        fs::write(docs_path.join("CHANGELOG.md"), "# Changelog\n").unwrap();
        fs::write(docs_path.join("CHANGES.md"), "# Changes\n").unwrap();

        // Custom target should exclude CHANGES.md, not CHANGELOG.md
        let files = find_doc_files(docs_path, Some("CHANGES.md"));
        assert_eq!(files.len(), 2);
        assert!(files.contains(&"CHANGELOG.md".to_string()));
        assert!(files.contains(&"guide.md".to_string()));
        assert!(!files.iter().any(|f| f == "CHANGES.md"));
    }

    #[test]
    fn test_build_priority_docs_empty_when_no_changes() {
        let tasks = vec![AuditTask {
            doc: "api/tools.md".to_string(),
            line: 10,
            claim: "file path `inc/Tool.php`".to_string(),
            claim_type: ClaimType::FilePath,
            claim_value: "inc/Tool.php".to_string(),
            confidence: ClaimConfidence::Real,
            status: AuditTaskStatus::Verified,
            action: None,
        }];

        let changed_files: Vec<String> = vec![];
        let priority = build_priority_docs(&tasks, &changed_files);

        assert!(priority.is_empty());
    }
}