homeboy 0.69.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
//! Test scaffold — generate test stubs from source file conventions.
//!
//! Reads a source file, extracts its public API (methods, functions),
//! and generates a test file with one stub per public method. The output
//! follows project conventions for test file naming, base classes, and
//! assertion style.
//!
//! Supports two extraction modes:
//! - Grammar-based: uses extension-provided grammar.toml (preferred)
//! - Legacy regex: hardcoded patterns as fallback

use std::collections::HashSet;

use regex::Regex;
use serde::Serialize;
use std::path::{Path, PathBuf};

use crate::error::{Error, Result};
use crate::utils::{grammar, io};

// ============================================================================
// Models
// ============================================================================

/// A public method/function extracted from a source file.
#[derive(Debug, Clone, Serialize)]
pub struct ExtractedMethod {
    /// Method/function name.
    pub name: String,
    /// Visibility (public, protected, pub, pub(crate)).
    pub visibility: String,
    /// Whether it's static.
    pub is_static: bool,
    /// Line number in source file.
    pub line: usize,
    /// Parameters (raw string, not parsed).
    pub params: String,
}

/// Extracted class/struct info from a source file.
#[derive(Debug, Clone, Serialize)]
pub struct ExtractedClass {
    /// Class/struct/trait name.
    pub name: String,
    /// Namespace (PHP) or module path (Rust).
    pub namespace: String,
    /// Kind: class, trait, interface, struct, enum.
    pub kind: String,
    /// Public methods.
    pub methods: Vec<ExtractedMethod>,
}

/// Configuration for scaffold generation.
#[derive(Debug, Clone)]
pub struct ScaffoldConfig {
    /// Base test class to extend (e.g., "WP_UnitTestCase", "TestCase").
    pub base_class: String,
    /// Base test class import.
    pub base_class_import: String,
    /// Test method prefix (e.g., "test_").
    pub test_prefix: String,
    /// Body for incomplete tests.
    pub incomplete_body: String,
    /// Language: "php" or "rust".
    pub language: String,
}

impl ScaffoldConfig {
    /// WordPress/PHPUnit defaults.
    pub fn php() -> Self {
        Self {
            base_class: "WP_UnitTestCase".to_string(),
            base_class_import: "WP_UnitTestCase".to_string(),
            test_prefix: "test_".to_string(),
            incomplete_body: "$this->markTestIncomplete('TODO: implement');".to_string(),
            language: "php".to_string(),
        }
    }

    /// Rust defaults.
    pub fn rust() -> Self {
        Self {
            base_class: String::new(),
            base_class_import: String::new(),
            test_prefix: "test_".to_string(),
            incomplete_body: "todo!(\"implement test\");".to_string(),
            language: "rust".to_string(),
        }
    }
}

/// Result of scaffold generation.
#[derive(Debug, Clone, Serialize)]
pub struct ScaffoldResult {
    /// Source file analyzed.
    pub source_file: String,
    /// Generated test file path.
    pub test_file: String,
    /// Number of test stubs generated.
    pub stub_count: usize,
    /// The generated test file content.
    pub content: String,
    /// Whether the file was written to disk.
    pub written: bool,
    /// Whether a test file already existed (skipped).
    pub skipped: bool,
    /// Classes/structs found in source.
    pub classes: Vec<ExtractedClass>,
}

/// Result of scaffolding multiple files.
#[derive(Debug, Clone, Serialize)]
pub struct ScaffoldBatchResult {
    /// Individual results per source file.
    pub results: Vec<ScaffoldResult>,
    /// Total test stubs generated.
    pub total_stubs: usize,
    /// Total files written.
    pub total_written: usize,
    /// Total files skipped (test already exists).
    pub total_skipped: usize,
}

fn is_low_signal_test_name(name: &str) -> bool {
    matches!(name, "test_run" | "test_new" | "test_validate")
}

const MAX_AUTO_SCAFFOLD_STUBS: usize = 12;

fn generated_test_names(classes: &[ExtractedClass], config: &ScaffoldConfig) -> Vec<String> {
    let mut emitted = HashSet::new();

    classes
        .iter()
        .flat_map(|class| {
            class
                .methods
                .iter()
                .filter(|method| method.name != "__construct")
                .map(|method| {
                    if config.language == "rust" {
                        if class.name.is_empty() {
                            format!("test_{}", to_snake_case(&method.name))
                        } else {
                            format!(
                                "test_{}_{}",
                                to_snake_case(&class.name),
                                to_snake_case(&method.name)
                            )
                        }
                    } else {
                        format!("{}{}", config.test_prefix, to_snake_case(&method.name))
                    }
                })
        })
        .filter(|name| emitted.insert(name.clone()))
        .collect()
}

fn passes_scaffold_quality_gate(test_names: &[String]) -> bool {
    if test_names.is_empty() {
        return false;
    }

    if test_names.len() > MAX_AUTO_SCAFFOLD_STUBS {
        return false;
    }

    let low_signal = test_names
        .iter()
        .filter(|name| is_low_signal_test_name(name))
        .count();
    let meaningful = test_names.len().saturating_sub(low_signal);

    if meaningful == 0 {
        return false;
    }

    if test_names.len() >= 3 && low_signal > meaningful {
        return false;
    }

    true
}

// ============================================================================
// Source extraction
// ============================================================================

/// Extract classes and their public methods from a PHP source file.
pub(crate) fn extract_php(content: &str) -> Vec<ExtractedClass> {
    let mut classes = Vec::new();

    // Match namespace
    let ns_re = Regex::new(r"(?m)^namespace\s+([\w\\]+);").unwrap();
    let namespace = ns_re
        .captures(content)
        .map(|c| c[1].to_string())
        .unwrap_or_default();

    // Match class/trait/interface declarations
    let class_re =
        Regex::new(r"(?m)^(?:abstract\s+)?(?:final\s+)?(class|trait|interface)\s+(\w+)").unwrap();

    for cap in class_re.captures_iter(content) {
        let kind = cap[1].to_string();
        let name = cap[2].to_string();

        // Extract methods for this class
        let methods = extract_php_methods(content);

        classes.push(ExtractedClass {
            name,
            namespace: namespace.clone(),
            kind,
            methods,
        });
    }

    // If no class found but there are functions, treat as procedural
    if classes.is_empty() {
        let methods = extract_php_functions(content);
        if !methods.is_empty() {
            classes.push(ExtractedClass {
                name: String::new(),
                namespace: namespace.clone(),
                kind: "procedural".to_string(),
                methods,
            });
        }
    }

    classes
}

/// Extract public/protected methods from PHP content.
fn extract_php_methods(content: &str) -> Vec<ExtractedMethod> {
    let method_re = Regex::new(
        r"(?m)^\s*((?:(?:public|protected|private|static|abstract|final)\s+)*)function\s+(\w+)\s*\(([^)]*)\)"
    ).unwrap();

    let mut methods = Vec::new();

    for (i, line) in content.lines().enumerate() {
        if let Some(cap) = method_re.captures(line) {
            let modifiers = cap[1].to_string();
            let name = cap[2].to_string();
            let params = cap[3].trim().to_string();

            // Skip magic methods except __construct
            if name.starts_with("__") && name != "__construct" {
                continue;
            }

            let visibility = if modifiers.contains("private") {
                "private"
            } else if modifiers.contains("protected") {
                "protected"
            } else {
                "public"
            };

            // Only scaffold public and protected methods
            if visibility == "private" {
                continue;
            }

            let is_static = modifiers.contains("static");

            methods.push(ExtractedMethod {
                name,
                visibility: visibility.to_string(),
                is_static,
                line: i + 1,
                params,
            });
        }
    }

    methods
}

/// Extract top-level functions from PHP content (procedural files).
fn extract_php_functions(content: &str) -> Vec<ExtractedMethod> {
    let fn_re = Regex::new(r"(?m)^function\s+(\w+)\s*\(([^)]*)\)").unwrap();
    let mut methods = Vec::new();

    for (i, line) in content.lines().enumerate() {
        if let Some(cap) = fn_re.captures(line) {
            methods.push(ExtractedMethod {
                name: cap[1].to_string(),
                visibility: "public".to_string(),
                is_static: false,
                line: i + 1,
                params: cap[2].trim().to_string(),
            });
        }
    }

    methods
}

/// Extract public functions/methods from a Rust source file.
pub(crate) fn extract_rust(content: &str) -> Vec<ExtractedClass> {
    let mut classes = Vec::new();

    // Match struct/enum/trait with impl blocks
    let struct_re =
        Regex::new(r"(?m)^(?:pub(?:\(crate\))?\s+)?(?:struct|enum|trait)\s+(\w+)").unwrap();

    for cap in struct_re.captures_iter(content) {
        let name = cap[1].to_string();
        let methods = extract_rust_impl_methods(content, &name);

        if !methods.is_empty() {
            classes.push(ExtractedClass {
                name,
                namespace: String::new(),
                kind: "struct".to_string(),
                methods,
            });
        }
    }

    // Also get free functions
    let free_fns = extract_rust_free_functions(content);
    if !free_fns.is_empty() {
        classes.push(ExtractedClass {
            name: String::new(),
            namespace: String::new(),
            kind: "module".to_string(),
            methods: free_fns,
        });
    }

    classes
}

/// Extract pub methods from an impl block for a specific type.
fn extract_rust_impl_methods(content: &str, type_name: &str) -> Vec<ExtractedMethod> {
    let impl_re = Regex::new(&format!(
        r"impl(?:<[^>]*>)?\s+{}\b",
        regex::escape(type_name)
    ))
    .unwrap();
    let fn_re = Regex::new(r"(?m)^\s*(pub(?:\(crate\))?\s+)?(?:async\s+)?fn\s+(\w+)\s*\(([^)]*)\)")
        .unwrap();

    let mut methods = Vec::new();
    let mut in_impl = false;
    let mut brace_depth: i32 = 0;

    for (i, line) in content.lines().enumerate() {
        if !in_impl {
            if impl_re.is_match(line) {
                in_impl = true;
                brace_depth = 0;
            }
            if !in_impl {
                continue;
            }
        }

        // Track brace depth
        for ch in line.chars() {
            if ch == '{' {
                brace_depth += 1;
            } else if ch == '}' {
                brace_depth -= 1;
                if brace_depth <= 0 {
                    in_impl = false;
                }
            }
        }

        // Match function declarations inside the impl block
        if let Some(cap) = fn_re.captures(line) {
            let vis = cap.get(1).map_or("", |m| m.as_str().trim());
            let name = cap[2].to_string();
            let params = cap[3].trim().to_string();

            // Only pub functions
            if !vis.starts_with("pub") {
                continue;
            }

            // Skip test functions
            if name.starts_with("test_") {
                continue;
            }

            methods.push(ExtractedMethod {
                name,
                visibility: vis.to_string(),
                is_static: !params.contains("self"),
                line: i + 1,
                params,
            });
        }
    }

    methods
}

/// Extract top-level pub functions (not in impl blocks).
fn extract_rust_free_functions(content: &str) -> Vec<ExtractedMethod> {
    let fn_re = Regex::new(r"(?m)^\s*(pub(?:\(crate\))?\s+)?(?:async\s+)?fn\s+(\w+)\s*\(([^)]*)\)")
        .unwrap();

    let mut methods = Vec::new();
    let mut pending_test_attribute = false;

    for (i, line) in content.lines().enumerate() {
        let trimmed = line.trim();

        if trimmed.starts_with("#[") {
            if trimmed.contains("test") {
                pending_test_attribute = true;
            }
            continue;
        }

        if trimmed.is_empty() {
            continue;
        }

        if let Some(cap) = fn_re.captures(line) {
            let visibility = cap.get(1).map(|m| m.as_str()).unwrap_or("");
            let is_test_function = pending_test_attribute;
            pending_test_attribute = false;

            // Keep existing behavior for public functions, but also include
            // test-annotated private functions so scaffold can mirror existing
            // inline test names into dedicated test files when needed.
            if !visibility.starts_with("pub") && !is_test_function {
                continue;
            }

            let name = cap[2].to_string();
            let params = cap[3].trim().to_string();

            // Skip test functions and main
            if name.starts_with("test_") || name == "main" {
                continue;
            }

            methods.push(ExtractedMethod {
                name,
                visibility: if visibility.is_empty() {
                    "private".to_string()
                } else {
                    visibility.trim().to_string()
                },
                is_static: true,
                line: i + 1,
                params,
            });
        } else {
            pending_test_attribute = false;
        }
    }

    methods
}

// ============================================================================
// Test file generation
// ============================================================================

/// Determine the test file path for a given source file.
pub fn test_file_path(source_path: &Path, root: &Path) -> PathBuf {
    let relative = source_path.strip_prefix(root).unwrap_or(source_path);
    let rel_str = relative.to_string_lossy();

    // PHP: src/Abilities/Foo.php → tests/Unit/Abilities/FooTest.php
    // PHP: inc/Abilities/Foo.php → tests/Unit/Abilities/FooTest.php
    if rel_str.ends_with(".php") {
        let stripped = rel_str
            .strip_prefix("src/")
            .or_else(|| rel_str.strip_prefix("inc/"))
            .or_else(|| rel_str.strip_prefix("lib/"))
            .unwrap_or(&rel_str);

        let without_ext = stripped.strip_suffix(".php").unwrap_or(stripped);
        return root.join(format!("tests/Unit/{}Test.php", without_ext));
    }

    // Rust: src/core/foo.rs → tests/core/foo_test.rs
    if rel_str.ends_with(".rs") {
        let stripped = rel_str.strip_prefix("src/").unwrap_or(&rel_str);
        let without_ext = stripped.strip_suffix(".rs").unwrap_or(stripped);
        return root.join(format!("tests/{}_test.rs", without_ext));
    }

    // Fallback
    root.join("tests").join(relative)
}

/// Generate PHP test file content.
pub(crate) fn generate_php_test(classes: &[ExtractedClass], config: &ScaffoldConfig) -> String {
    let mut out = String::new();
    let mut emitted = HashSet::new();
    out.push_str("<?php\n");

    for class in classes {
        if class.kind == "procedural" {
            // Procedural test file
            if !class.namespace.is_empty() {
                out.push_str(&format!("namespace {}\\Tests;\n\n", class.namespace));
            }
            out.push_str(&format!("use {};\n\n", config.base_class_import));

            out.push_str(&format!(
                "class FunctionsTest extends {} {{\n\n",
                config.base_class
            ));

            let test_names: Vec<String> = class
                .methods
                .iter()
                .map(|method| format!("{}{}", config.test_prefix, to_snake_case(&method.name)))
                .filter(|name| emitted.insert(name.clone()))
                .collect();

            for test_name in test_names {
                out.push_str(&format!(
                    "    public function {}() {{\n        {}\n    }}\n\n",
                    test_name, config.incomplete_body
                ));
            }

            out.push_str("}\n");
            continue;
        }

        // Class-based test file
        let test_namespace = if !class.namespace.is_empty() {
            format!("{}\\Tests\\Unit", namespace_root(&class.namespace))
        } else {
            String::new()
        };

        if !test_namespace.is_empty() {
            out.push_str(&format!("namespace {};\n\n", test_namespace));
        }

        // Imports
        out.push_str(&format!("use {};\n", config.base_class_import));
        if !class.namespace.is_empty() {
            out.push_str(&format!("use {}\\{};\n", class.namespace, class.name));
        }
        out.push('\n');

        // Class doc
        if !class.namespace.is_empty() {
            out.push_str(&format!(
                "/**\n * @covers \\{}\\{}\n */\n",
                class.namespace, class.name
            ));
        }

        // Class declaration
        out.push_str(&format!(
            "class {}Test extends {} {{\n\n",
            class.name, config.base_class
        ));

        // Test methods
        let test_names: Vec<String> = class
            .methods
            .iter()
            .filter(|method| method.name != "__construct")
            .map(|method| format!("{}{}", config.test_prefix, to_snake_case(&method.name)))
            .filter(|name| emitted.insert(name.clone()))
            .collect();

        for test_name in test_names {
            out.push_str(&format!(
                "    public function {}() {{\n        {}\n    }}\n\n",
                test_name, config.incomplete_body
            ));
        }

        out.push_str("}\n");
    }

    out
}

/// Generate Rust test file content.
pub fn generate_rust_test(classes: &[ExtractedClass], _config: &ScaffoldConfig) -> String {
    let mut out = String::new();
    let mut emitted = HashSet::new();

    // Module-level test block
    out.push_str("#[cfg(test)]\nmod tests {\n    use super::*;\n\n");

    for class in classes {
        if !class.name.is_empty() {
            out.push_str(&format!("    // Tests for {}\n\n", class.name));
        }

        let test_names: Vec<String> = class
            .methods
            .iter()
            .map(|method| {
                if class.name.is_empty() {
                    format!("test_{}", to_snake_case(&method.name))
                } else {
                    format!(
                        "test_{}_{}",
                        to_snake_case(&class.name),
                        to_snake_case(&method.name)
                    )
                }
            })
            .filter(|name| emitted.insert(name.clone()))
            .collect();

        for test_name in test_names {
            out.push_str(&format!(
                "    #[test]\n    fn {}() {{\n        todo!(\"implement test\");\n    }}\n\n",
                test_name
            ));
        }
    }

    out.push_str("}\n");
    out
}

/// Scaffold tests for a single source file.
pub fn scaffold_file(
    source_path: &Path,
    root: &Path,
    config: &ScaffoldConfig,
    write: bool,
) -> Result<ScaffoldResult> {
    let relative = source_path
        .strip_prefix(root)
        .unwrap_or(source_path)
        .to_string_lossy()
        .to_string();

    let content = io::read_file(source_path, "read source file")?;

    let classes = if config.language == "rust" {
        extract_rust(&content)
    } else {
        extract_php(&content)
    };

    let test_path = test_file_path(source_path, root);
    let test_relative = test_path
        .strip_prefix(root)
        .unwrap_or(&test_path)
        .to_string_lossy()
        .to_string();

    // Check if test file already exists
    if test_path.exists() {
        return Ok(ScaffoldResult {
            source_file: relative,
            test_file: test_relative,
            stub_count: 0,
            content: String::new(),
            written: false,
            skipped: true,
            classes,
        });
    }

    let generated_names = generated_test_names(&classes, config);
    let stub_count = generated_names.len();

    if !passes_scaffold_quality_gate(&generated_names) {
        return Ok(ScaffoldResult {
            source_file: relative,
            test_file: test_relative,
            stub_count: 0,
            content: String::new(),
            written: false,
            skipped: false,
            classes,
        });
    }

    let generated = if config.language == "rust" {
        generate_rust_test(&classes, config)
    } else {
        generate_php_test(&classes, config)
    };

    if write && !generated.is_empty() {
        // Create directory if needed
        if let Some(parent) = test_path.parent() {
            std::fs::create_dir_all(parent).map_err(|e| {
                Error::internal_io(
                    format!("Failed to create test directory: {}", e),
                    Some("scaffold.write".to_string()),
                )
            })?;
        }
        io::write_file(&test_path, &generated, "write test scaffold")?;
    }

    Ok(ScaffoldResult {
        source_file: relative,
        test_file: test_relative,
        stub_count,
        content: generated,
        written: write,
        skipped: false,
        classes,
    })
}

/// Scaffold tests for all untested files under a root directory.
pub fn scaffold_untested(
    root: &Path,
    config: &ScaffoldConfig,
    write: bool,
) -> Result<ScaffoldBatchResult> {
    let source_dirs = if config.language == "rust" {
        vec!["src"]
    } else {
        vec!["src", "inc", "lib"]
    };

    let ext = if config.language == "rust" {
        "rs"
    } else {
        "php"
    };

    let mut source_files = Vec::new();
    for dir in &source_dirs {
        let dir_path = root.join(dir);
        if dir_path.exists() {
            collect_source_files(&dir_path, ext, &mut source_files);
        }
    }

    let mut results = Vec::new();
    let mut total_stubs = 0;
    let mut total_written = 0;
    let mut total_skipped = 0;

    for source_file in &source_files {
        let result = scaffold_file(source_file, root, config, write)?;
        if result.skipped {
            total_skipped += 1;
        } else {
            total_stubs += result.stub_count;
            if result.written {
                total_written += 1;
            }
        }
        results.push(result);
    }

    Ok(ScaffoldBatchResult {
        results,
        total_stubs,
        total_written,
        total_skipped,
    })
}

fn collect_source_files(dir: &Path, ext: &str, files: &mut Vec<PathBuf>) {
    let Ok(entries) = std::fs::read_dir(dir) else {
        return;
    };

    for entry in entries.flatten() {
        let path = entry.path();
        if path.is_dir() {
            let name = path.file_name().unwrap_or_default().to_string_lossy();
            if name == ".git" || name == "vendor" || name == "node_modules" || name == "target" {
                continue;
            }
            collect_source_files(&path, ext, files);
        } else if path.extension().and_then(|e| e.to_str()) == Some(ext) {
            files.push(path);
        }
    }
}

// ============================================================================
// Utilities
// ============================================================================

/// Convert CamelCase or camelCase to snake_case.
fn to_snake_case(s: &str) -> String {
    let mut result = String::new();
    for (i, ch) in s.chars().enumerate() {
        if ch.is_uppercase() && i > 0 {
            // Don't insert underscore between consecutive capitals
            let prev = s.chars().nth(i - 1).unwrap_or('_');
            if prev.is_lowercase() || prev.is_ascii_digit() {
                result.push('_');
            }
        }
        result.push(ch.to_lowercase().next().unwrap_or(ch));
    }
    result
}

/// Get the root namespace (first segment) from a PHP namespace.
fn namespace_root(ns: &str) -> &str {
    ns.split('\\').next().unwrap_or(ns)
}

// ============================================================================
// Grammar-based extraction (preferred path)
// ============================================================================

/// Extract classes and methods using a grammar file.
///
/// This is the preferred extraction path. It delegates to `utils/grammar.rs`
/// which applies the extension-provided grammar patterns with structural
/// awareness (brace depth, comment/string skipping).
pub fn extract_with_grammar(content: &str, grammar_def: &grammar::Grammar) -> Vec<ExtractedClass> {
    let symbols = grammar::extract(content, grammar_def);

    // Get namespace
    let ns = grammar::namespace(&symbols).unwrap_or_default();

    // Get classes/structs/traits
    let type_symbols: Vec<_> = symbols
        .iter()
        .filter(|s| {
            s.concept == "class"
                || s.concept == "struct"
                || s.concept == "trait"
                || s.concept == "interface"
                || s.concept == "type"
        })
        .collect();

    // Get methods/functions
    let method_symbols: Vec<_> = symbols
        .iter()
        .filter(|s| {
            s.concept == "method" || s.concept == "function" || s.concept == "free_function"
        })
        .collect();

    let mut classes = Vec::new();

    if !type_symbols.is_empty() {
        for ts in &type_symbols {
            let name = ts.name().unwrap_or("").to_string();
            let kind = ts.get("kind").unwrap_or(ts.concept.as_str()).to_string();

            // Collect methods that belong to this type (inside its block)
            // For now, associate all methods with each class (same as legacy behavior)
            let methods: Vec<ExtractedMethod> = method_symbols
                .iter()
                .filter(|m| {
                    let mname = m.name().unwrap_or("");
                    // Skip magic methods except __construct
                    if mname.starts_with("__") && mname != "__construct" {
                        return false;
                    }
                    // Skip private methods for PHP
                    if let Some(mods) = m.get("modifiers") {
                        if mods.contains("private") {
                            return false;
                        }
                    }
                    true
                })
                .map(|m| {
                    let mname = m.name().unwrap_or("").to_string();
                    let vis = if let Some(mods) = m.get("modifiers") {
                        if mods.contains("private") {
                            "private"
                        } else if mods.contains("protected") {
                            "protected"
                        } else {
                            "public"
                        }
                    } else if let Some(v) = m.visibility() {
                        if v.contains("pub") {
                            "pub"
                        } else {
                            "private"
                        }
                    } else {
                        "public"
                    };

                    ExtractedMethod {
                        name: mname,
                        visibility: vis.to_string(),
                        is_static: m
                            .get("modifiers")
                            .map_or(false, |mods| mods.contains("static"))
                            || m.get("params").map_or(false, |p| !p.contains("self")),
                        line: m.line,
                        params: m.get("params").unwrap_or("").to_string(),
                    }
                })
                .collect();

            classes.push(ExtractedClass {
                name,
                namespace: ns.clone(),
                kind,
                methods,
            });
        }
    } else if !method_symbols.is_empty() {
        // No classes — procedural/module level
        let kind = if grammar_def.language.id == "rust" {
            "module"
        } else {
            "procedural"
        };
        let methods: Vec<ExtractedMethod> = method_symbols
            .iter()
            .map(|m| {
                let mname = m.name().unwrap_or("").to_string();
                ExtractedMethod {
                    name: mname,
                    visibility: m.visibility().unwrap_or("public").to_string(),
                    is_static: true,
                    line: m.line,
                    params: m.get("params").unwrap_or("").to_string(),
                }
            })
            .collect();

        classes.push(ExtractedClass {
            name: String::new(),
            namespace: ns,
            kind: kind.to_string(),
            methods,
        });
    }

    classes
}

/// Try to load a grammar from the extension path.
/// Returns None if the grammar file doesn't exist.
pub fn load_extension_grammar(extension_path: &Path, language: &str) -> Option<grammar::Grammar> {
    // Try TOML first, then JSON
    let toml_path = extension_path.join("grammar.toml");
    if toml_path.exists() {
        return grammar::load_grammar(&toml_path).ok();
    }

    let json_path = extension_path.join("grammar.json");
    if json_path.exists() {
        return grammar::load_grammar_json(&json_path).ok();
    }

    // Try language-specific subdirectory
    let lang_toml = extension_path.join(language).join("grammar.toml");
    if lang_toml.exists() {
        return grammar::load_grammar(&lang_toml).ok();
    }

    None
}

// ============================================================================
// Tests
// ============================================================================

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

    #[test]
    fn extract_php_class_methods() {
        let content = r#"<?php
namespace DataMachine\Abilities;

class PipelineAbilities {
    public function register() {}
    public function executeCreate($config) {}
    protected function validate($input) {}
    private function internal() {}
    public static function getInstance() {}
}
"#;
        let classes = extract_php(content);
        assert_eq!(classes.len(), 1);
        assert_eq!(classes[0].name, "PipelineAbilities");
        assert_eq!(classes[0].namespace, "DataMachine\\Abilities");

        let names: Vec<&str> = classes[0].methods.iter().map(|m| m.name.as_str()).collect();
        assert!(names.contains(&"register"));
        assert!(names.contains(&"executeCreate"));
        assert!(names.contains(&"validate")); // protected included
        assert!(!names.contains(&"internal")); // private excluded
        assert!(names.contains(&"getInstance"));
    }

    #[test]
    fn extract_php_magic_methods_skipped() {
        let content = r#"<?php
class Foo {
    public function __construct() {}
    public function __toString() {}
    public function __get($name) {}
    public function realMethod() {}
}
"#;
        let classes = extract_php(content);
        let names: Vec<&str> = classes[0].methods.iter().map(|m| m.name.as_str()).collect();
        assert!(names.contains(&"__construct")); // constructor kept
        assert!(!names.contains(&"__toString")); // magic skipped
        assert!(!names.contains(&"__get")); // magic skipped
        assert!(names.contains(&"realMethod"));
    }

    #[test]
    fn extract_php_procedural() {
        let content = r#"<?php
function datamachine_init() {}
function datamachine_activate() {}
"#;
        let classes = extract_php(content);
        assert_eq!(classes.len(), 1);
        assert_eq!(classes[0].kind, "procedural");
        assert_eq!(classes[0].methods.len(), 2);
    }

    #[test]
    fn extract_rust_struct_methods() {
        let content = r#"
pub struct Config {
    data: HashMap<String, String>,
}

impl Config {
    pub fn new() -> Self { Self { data: HashMap::new() } }
    pub fn get(&self, key: &str) -> Option<&str> { None }
    fn private_method(&self) {}
    pub async fn load(path: &Path) -> Result<Self> { todo!() }
}
"#;
        let classes = extract_rust(content);
        let config = classes.iter().find(|c| c.name == "Config").unwrap();
        let names: Vec<&str> = config.methods.iter().map(|m| m.name.as_str()).collect();
        assert!(names.contains(&"new"));
        assert!(names.contains(&"get"));
        assert!(names.contains(&"load"));
        assert!(!names.contains(&"private_method"));
    }

    #[test]
    fn extract_rust_free_functions() {
        let content = r#"
pub fn parse_config(path: &Path) -> Config { todo!() }
pub fn validate(config: &Config) -> bool { true }
fn internal_helper() {}
"#;
        let classes = extract_rust(content);
        let module = classes.iter().find(|c| c.kind == "module").unwrap();
        let names: Vec<&str> = module.methods.iter().map(|m| m.name.as_str()).collect();
        assert!(names.contains(&"parse_config"));
        assert!(names.contains(&"validate"));
        assert!(!names.contains(&"internal_helper"));
    }

    #[test]
    fn extract_rust_includes_test_annotated_private_functions() {
        let content = r#"
#[test]
fn high_item_count_detected() {}

fn helper_not_a_test() {}
"#;

        let classes = extract_rust(content);
        let module = classes.iter().find(|c| c.kind == "module").unwrap();
        let names: Vec<&str> = module.methods.iter().map(|m| m.name.as_str()).collect();

        assert!(names.contains(&"high_item_count_detected"));
        assert!(!names.contains(&"helper_not_a_test"));
    }

    #[test]
    fn test_file_path_php() {
        let root = Path::new("/project");
        assert_eq!(
            test_file_path(Path::new("/project/src/Abilities/Foo.php"), root),
            PathBuf::from("/project/tests/Unit/Abilities/FooTest.php")
        );
        assert_eq!(
            test_file_path(Path::new("/project/inc/Core/Bar.php"), root),
            PathBuf::from("/project/tests/Unit/Core/BarTest.php")
        );
    }

    #[test]
    fn test_file_path_rust() {
        let root = Path::new("/project");
        assert_eq!(
            test_file_path(Path::new("/project/src/core/config.rs"), root),
            PathBuf::from("/project/tests/core/config_test.rs")
        );
    }

    #[test]
    fn generate_php_test_output() {
        let classes = vec![ExtractedClass {
            name: "FooAbilities".to_string(),
            namespace: "DataMachine\\Abilities".to_string(),
            kind: "class".to_string(),
            methods: vec![
                ExtractedMethod {
                    name: "register".to_string(),
                    visibility: "public".to_string(),
                    is_static: false,
                    line: 5,
                    params: String::new(),
                },
                ExtractedMethod {
                    name: "executeCreate".to_string(),
                    visibility: "public".to_string(),
                    is_static: false,
                    line: 10,
                    params: "$config".to_string(),
                },
            ],
        }];

        let config = ScaffoldConfig::php();
        let output = generate_php_test(&classes, &config);

        assert!(output.contains("class FooAbilitiesTest extends WP_UnitTestCase"));
        assert!(output.contains("@covers \\DataMachine\\Abilities\\FooAbilities"));
        assert!(output.contains("function test_register()"));
        assert!(output.contains("function test_execute_create()"));
        assert!(output.contains("markTestIncomplete"));
        assert!(output.contains("use DataMachine\\Abilities\\FooAbilities;"));
    }

    #[test]
    fn generate_rust_test_output() {
        let classes = vec![ExtractedClass {
            name: "Config".to_string(),
            namespace: String::new(),
            kind: "struct".to_string(),
            methods: vec![
                ExtractedMethod {
                    name: "new".to_string(),
                    visibility: "pub".to_string(),
                    is_static: true,
                    line: 5,
                    params: String::new(),
                },
                ExtractedMethod {
                    name: "load".to_string(),
                    visibility: "pub".to_string(),
                    is_static: true,
                    line: 10,
                    params: "path: &Path".to_string(),
                },
            ],
        }];

        let config = ScaffoldConfig::rust();
        let output = generate_rust_test(&classes, &config);

        assert!(output.contains("#[cfg(test)]"));
        assert!(output.contains("mod tests"));
        assert!(output.contains("fn test_config_new()"));
        assert!(output.contains("fn test_config_load()"));
        assert!(output.contains("todo!(\"implement test\")"));
    }

    #[test]
    fn generate_rust_test_dedupes_duplicate_names() {
        let classes = vec![
            ExtractedClass {
                name: "Config".to_string(),
                namespace: String::new(),
                kind: "struct".to_string(),
                methods: vec![ExtractedMethod {
                    name: "load".to_string(),
                    visibility: "pub".to_string(),
                    is_static: true,
                    line: 1,
                    params: String::new(),
                }],
            },
            ExtractedClass {
                name: "Config".to_string(),
                namespace: String::new(),
                kind: "struct".to_string(),
                methods: vec![ExtractedMethod {
                    name: "load".to_string(),
                    visibility: "pub".to_string(),
                    is_static: true,
                    line: 2,
                    params: String::new(),
                }],
            },
        ];

        let config = ScaffoldConfig::rust();
        let output = generate_rust_test(&classes, &config);

        assert_eq!(output.matches("fn test_config_load()").count(), 1);
    }

    #[test]
    fn scaffold_file_skips_low_signal_single_run_stub() {
        let dir = std::env::temp_dir().join("homeboy_test_scaffold_low_signal");
        let _ = std::fs::remove_dir_all(&dir);
        std::fs::create_dir_all(dir.join("src/commands")).unwrap();
        std::fs::write(dir.join("src/commands/api.rs"), "pub fn run() {}\n").unwrap();

        let result = scaffold_file(
            &dir.join("src/commands/api.rs"),
            &dir,
            &ScaffoldConfig::rust(),
            false,
        )
        .unwrap();

        assert_eq!(result.stub_count, 0);
        assert!(result.content.is_empty());

        let _ = std::fs::remove_dir_all(&dir);
    }

    #[test]
    fn passes_scaffold_quality_gate_rejects_low_signal_dominated_set() {
        let names = vec![
            "test_run".to_string(),
            "test_new".to_string(),
            "test_validate".to_string(),
        ];

        assert!(!passes_scaffold_quality_gate(&names));
    }

    #[test]
    fn passes_scaffold_quality_gate_accepts_meaningful_mix() {
        let names = vec![
            "test_run".to_string(),
            "test_component_args_load".to_string(),
            "test_component_args_resolve".to_string(),
        ];

        assert!(passes_scaffold_quality_gate(&names));
    }

    #[test]
    fn passes_scaffold_quality_gate_rejects_oversized_scaffold() {
        let names: Vec<String> = (0..=MAX_AUTO_SCAFFOLD_STUBS)
            .map(|i| format!("test_meaningful_case_{}", i))
            .collect();

        assert!(!passes_scaffold_quality_gate(&names));
    }

    #[test]
    fn to_snake_case_works() {
        assert_eq!(to_snake_case("executeCreate"), "execute_create");
        assert_eq!(to_snake_case("getInstance"), "get_instance");
        assert_eq!(to_snake_case("register"), "register");
        assert_eq!(to_snake_case("HTMLParser"), "htmlparser"); // consecutive caps
        assert_eq!(to_snake_case("loadConfig"), "load_config");
    }

    #[test]
    fn extract_with_grammar_php() {
        let grammar_path = std::path::Path::new(
            "/var/lib/datamachine/workspace/homeboy-modules/wordpress/grammar.toml",
        );
        if !grammar_path.exists() {
            return; // Skip if not in dev environment
        }
        let grammar_def = grammar::load_grammar(grammar_path).unwrap();

        let content = r#"<?php
namespace App\Abilities;

class FooAbilities {
    public function register() {}
    public function executeCreate($config) {}
    protected function validate($input) {}
    private function internal() {}
}
"#;

        let classes = extract_with_grammar(content, &grammar_def);
        assert!(!classes.is_empty(), "Should extract at least one class");

        let foo = &classes[0];
        assert_eq!(foo.name, "FooAbilities");
        assert_eq!(foo.namespace, "App\\Abilities");

        // Private methods should be filtered
        let names: Vec<&str> = foo.methods.iter().map(|m| m.name.as_str()).collect();
        assert!(names.contains(&"register"));
        assert!(names.contains(&"executeCreate"));
        assert!(names.contains(&"validate"));
        assert!(!names.contains(&"internal"));
    }

    #[test]
    fn extract_with_grammar_rust() {
        let grammar_path = std::path::Path::new(
            "/var/lib/datamachine/workspace/homeboy-modules/rust/grammar.toml",
        );
        if !grammar_path.exists() {
            return;
        }
        let grammar_def = grammar::load_grammar(grammar_path).unwrap();

        let content = r#"
pub struct Config {
    data: String,
}

impl Config {
    pub fn new() -> Self {
        Self { data: String::new() }
    }

    pub fn load(path: &Path) -> Result<Self> {
        todo!()
    }

    fn private_method(&self) {}
}
"#;

        let classes = extract_with_grammar(content, &grammar_def);
        assert!(!classes.is_empty());

        let config = classes.iter().find(|c| c.name == "Config").unwrap();
        let names: Vec<&str> = config.methods.iter().map(|m| m.name.as_str()).collect();
        assert!(names.contains(&"new"));
        assert!(names.contains(&"load"));
    }

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

        let src_dir = root.join("src/Abilities");
        fs::create_dir_all(&src_dir).unwrap();
        fs::write(
            src_dir.join("FooAbilities.php"),
            r#"<?php
namespace App\Abilities;

class FooAbilities {
    public function register() {}
    public function execute($id) {}
}
"#,
        )
        .unwrap();

        let config = ScaffoldConfig::php();
        let result =
            scaffold_file(&src_dir.join("FooAbilities.php"), root, &config, false).unwrap();

        assert!(!result.skipped);
        assert_eq!(result.stub_count, 2);
        assert!(result.content.contains("class FooAbilitiesTest"));
        assert!(result.content.contains("test_register"));
        assert!(result.content.contains("test_execute"));
        assert!(!result.written);
    }

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

        let src_dir = root.join("src");
        let test_dir = root.join("tests/Unit");
        fs::create_dir_all(&src_dir).unwrap();
        fs::create_dir_all(&test_dir).unwrap();

        fs::write(
            src_dir.join("Foo.php"),
            "<?php\nclass Foo {\n    public function bar() {}\n}\n",
        )
        .unwrap();
        // Pre-existing test file
        fs::write(test_dir.join("FooTest.php"), "<?php // existing").unwrap();

        let config = ScaffoldConfig::php();
        let result = scaffold_file(&src_dir.join("Foo.php"), root, &config, false).unwrap();
        assert!(result.skipped);
        assert_eq!(result.stub_count, 0);
    }

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

        let src_dir = root.join("src");
        fs::create_dir_all(&src_dir).unwrap();
        fs::write(
            src_dir.join("Bar.php"),
            "<?php\nclass Bar {\n    public function doThing() {}\n}\n",
        )
        .unwrap();

        let config = ScaffoldConfig::php();
        let result = scaffold_file(&src_dir.join("Bar.php"), root, &config, true).unwrap();

        assert!(result.written);
        assert!(root.join("tests/Unit/BarTest.php").exists());

        let written_content = fs::read_to_string(root.join("tests/Unit/BarTest.php")).unwrap();
        assert!(written_content.contains("class BarTest"));
    }

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

        let src_dir = root.join("src");
        let test_dir = root.join("tests/Unit");
        fs::create_dir_all(&src_dir).unwrap();
        fs::create_dir_all(&test_dir).unwrap();

        // One file with tests, one without
        fs::write(
            src_dir.join("HasTests.php"),
            "<?php\nclass HasTests {\n    public function foo() {}\n}\n",
        )
        .unwrap();
        fs::write(test_dir.join("HasTestsTest.php"), "<?php // existing").unwrap();

        fs::write(
            src_dir.join("NoTests.php"),
            "<?php\nclass NoTests {\n    public function bar() {}\n    public function baz() {}\n}\n",
        )
        .unwrap();

        let config = ScaffoldConfig::php();
        let result = scaffold_untested(root, &config, false).unwrap();

        assert_eq!(result.total_skipped, 1); // HasTests already has test
        assert_eq!(result.total_stubs, 2); // bar + baz from NoTests
    }
}