dockgen 0.1.0

Blazing fast CLI for deterministic Dockerfile, .dockerignore, and docker-compose.yml generation
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
use std::collections::BTreeSet;
use std::fs;
use std::path::{Path, PathBuf};

use crate::models::{ServiceType, EXCLUDED_DIRS};

// ---------------------------------------------------------------------------
// Constants
// ---------------------------------------------------------------------------

/// Manifest file names that indicate a service root.
const MANIFEST_FILES: &[&str] = &[
    "package.json",
    "Cargo.toml",
    "go.mod",
    "requirements.txt",
    "pyproject.toml",
    "Pipfile",
    "pom.xml",
    "build.gradle",
    "build.gradle.kts",
    "composer.json",
    "Gemfile",
];

/// Well-known directory names that map to a specific [`ServiceType`].
const FRONTEND_DIRS: &[&str] = &["frontend", "web", "client", "ui", "app"];
const BACKEND_DIRS: &[&str] = &["backend", "server", "api", "gateway"];
const CONTAINER_DIRS: &[&str] = &["services", "apps", "packages"];

/// Source file extensions used to verify that a candidate directory contains
/// actual code, not just manifests.
const SOURCE_FILE_EXTENSIONS: &[&str] = &[
    "rs", "ts", "tsx", "js", "jsx", "mjs", "cjs", "py", "go", "java", "kt", "php", "cs", "fs", "rb",
];

/// Maximum depth (from `dir`) to scan for source files when verifying a
/// candidate.
const SOURCE_FILE_SCAN_DEPTH: usize = 3;

// ---------------------------------------------------------------------------
// Public types
// ---------------------------------------------------------------------------

/// A raw service candidate discovered during structural analysis.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct DiscoveredServiceCandidate {
    /// Human-readable name (derived from the directory name).
    pub name: String,
    /// Path relative to the project root.
    pub relative_path: PathBuf,
    /// Absolute path on disk.
    pub full_path: PathBuf,
    /// Assigned service type based on naming conventions.
    pub service_type: ServiceType,
    /// Manifest files found at the service root.
    pub manifest_files: Vec<PathBuf>,
}

/// Result of workspace and multi-service structural analysis.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct WorkspaceStructure {
    /// Absolute path to the project root.
    pub root_path: PathBuf,
    /// Whether the project is a monorepo / multi-service workspace.
    pub is_monorepo: bool,
    /// Detected workspace orchestration tool, if any.
    pub workspace_tool: Option<String>,
    /// Discovered service candidates, sorted by `relative_path`.
    pub candidates: Vec<DiscoveredServiceCandidate>,
}

// ---------------------------------------------------------------------------
// Public API
// ---------------------------------------------------------------------------

/// Analyses the directory structure at `root_path` and returns a
/// [`WorkspaceStructure`] describing discovered services.
///
/// # Errors
///
/// Returns an `io::Error` if the root path cannot be read.
pub fn analyze_structure(root_path: &Path) -> Result<WorkspaceStructure, std::io::Error> {
    let root_path = root_path.to_path_buf();
    let workspace_tool = detect_workspace_tool(&root_path);
    let is_monorepo = workspace_tool.is_some();

    let mut candidates = if is_monorepo {
        discover_monorepo_candidates(&root_path)
    } else {
        discover_flat_candidates(&root_path)
    };

    // Deterministic ordering.
    candidates.sort_by(|a, b| a.relative_path.cmp(&b.relative_path));

    Ok(WorkspaceStructure {
        root_path,
        is_monorepo,
        workspace_tool,
        candidates,
    })
}

// ---------------------------------------------------------------------------
// Level 1 — Workspace tool detection
// ---------------------------------------------------------------------------

/// Scans the root directory for known workspace orchestrator configuration
/// files and returns the tool name if found.
pub fn detect_workspace_tool(root: &Path) -> Option<String> {
    // Dedicated config files (checked first — unambiguous).
    if root.join("turbo.json").is_file() {
        return Some("turborepo".to_string());
    }
    if root.join("nx.json").is_file() {
        return Some("nx".to_string());
    }
    if root.join("pnpm-workspace.yaml").is_file() {
        return Some("pnpm".to_string());
    }
    if root.join("lerna.json").is_file() {
        return Some("lerna".to_string());
    }
    if root.join("go.work").is_file() {
        return Some("go-work".to_string());
    }

    // Cargo workspace — requires reading and parsing Cargo.toml.
    if let Some(tool) = detect_cargo_workspace(root) {
        return Some(tool);
    }

    // npm/yarn/bun workspaces — requires reading package.json.
    if let Some(tool) = detect_npm_workspace(root) {
        return Some(tool);
    }

    None
}

/// Checks whether `Cargo.toml` at `root` defines a `[workspace]`.
fn detect_cargo_workspace(root: &Path) -> Option<String> {
    let cargo_path = root.join("Cargo.toml");
    if !cargo_path.is_file() {
        return None;
    }
    let content = fs::read_to_string(&cargo_path).ok()?;
    let table: toml::Value = toml::from_str(&content).ok()?;
    if table.get("workspace").is_some() {
        Some("cargo".to_string())
    } else {
        None
    }
}

/// Checks whether `package.json` at `root` contains a `"workspaces"` field.
/// Disambiguates the workspace tool by checking for lockfiles.
fn detect_npm_workspace(root: &Path) -> Option<String> {
    let pkg_path = root.join("package.json");
    if !pkg_path.is_file() {
        return None;
    }
    let content = fs::read_to_string(&pkg_path).ok()?;
    let value: serde_json::Value = serde_json::from_str(&content).ok()?;
    if value.get("workspaces").is_some() {
        // Disambiguate by lockfile presence.
        if root.join("pnpm-lock.yaml").is_file() {
            Some("pnpm".to_string())
        } else if root.join("yarn.lock").is_file() {
            Some("yarn".to_string())
        } else if root.join("bun.lockb").is_file() || root.join("bun.lock").is_file() {
            Some("bun".to_string())
        } else {
            Some("npm".to_string())
        }
    } else {
        None
    }
}

// ---------------------------------------------------------------------------
// Level 2 — Structural discovery
// ---------------------------------------------------------------------------

/// Discovers service candidates in a monorepo by scanning well-known
/// container directories (`apps/`, `services/`, `packages/`) and direct
/// children.
fn discover_monorepo_candidates(root: &Path) -> Vec<DiscoveredServiceCandidate> {
    let mut candidates = Vec::new();
    let mut seen = BTreeSet::new();

    // 1. Scan well-known container directories.
    for container_dir in CONTAINER_DIRS {
        let container_path = root.join(container_dir);
        if !container_path.is_dir() {
            continue;
        }
        if let Ok(entries) = fs::read_dir(&container_path) {
            for entry in entries.flatten() {
                let path = entry.path();
                if !path.is_dir() {
                    continue;
                }
                let dir_name = file_name(&path);
                if is_excluded(&dir_name) {
                    continue;
                }
                if seen.contains(&path) {
                    continue;
                }

                let manifests = find_manifests(&path);
                if !manifests.is_empty() && has_verifiable_source_files(&path) {
                    let relative = relative_path(root, &path);
                    candidates.push(DiscoveredServiceCandidate {
                        name: dir_name.clone(),
                        relative_path: relative,
                        full_path: path.clone(),
                        service_type: ServiceType::MonorepoMember,
                        manifest_files: manifests,
                    });
                    seen.insert(path);
                }
            }
        }
    }

    // 2. Scan direct children for well-named service dirs.
    if let Ok(entries) = fs::read_dir(root) {
        for entry in entries.flatten() {
            let path = entry.path();
            if !path.is_dir() {
                continue;
            }
            let dir_name = file_name(&path);
            if is_excluded(&dir_name) {
                continue;
            }
            if seen.contains(&path) {
                continue;
            }

            let manifests = find_manifests(&path);
            if !manifests.is_empty() && has_verifiable_source_files(&path) {
                let st = classify_service_type(&dir_name);
                let relative = relative_path(root, &path);
                candidates.push(DiscoveredServiceCandidate {
                    name: dir_name,
                    relative_path: relative,
                    full_path: path.clone(),
                    service_type: st,
                    manifest_files: manifests,
                });
                seen.insert(path);
            }
        }
    }

    candidates
}

/// Discovers service candidates in a flat (non-monorepo) project.
///
/// - If depth-1 subdirectories contain well-known service names
///   (`frontend`, `backend`, `api`, etc.) **and** their own manifests, they are
///   returned as distinct candidates (e.g. a monorepo-like layout without a
///   workspace tool).
/// - Otherwise, if the root itself contains manifests, returns a single `Single`
///   candidate.
/// - Otherwise, scans depth-1 subdirectories for any manifests.
fn discover_flat_candidates(root: &Path) -> Vec<DiscoveredServiceCandidate> {
    // Check for well-known service sub-directories first — even when the root
    // has manifests (polyglot monorepo layout without workspace tool).
    let sub_services = discover_well_known_sub_services(root);
    if !sub_services.is_empty() {
        return sub_services;
    }

    let root_manifests = find_manifests(root);
    if !root_manifests.is_empty() {
        return vec![DiscoveredServiceCandidate {
            name: file_name(root),
            relative_path: PathBuf::from("."),
            full_path: root.to_path_buf(),
            service_type: ServiceType::Single,
            manifest_files: root_manifests,
        }];
    }

    let mut candidates = Vec::new();
    if let Ok(entries) = fs::read_dir(root) {
        for entry in entries.flatten() {
            let path = entry.path();
            if !path.is_dir() {
                continue;
            }
            let dir_name = file_name(&path);
            if is_excluded(&dir_name) {
                continue;
            }
            let manifests = find_manifests(&path);
            if !manifests.is_empty() && has_verifiable_source_files(&path) {
                let st = classify_service_type(&dir_name);
                let relative = relative_path(root, &path);
                candidates.push(DiscoveredServiceCandidate {
                    name: dir_name,
                    relative_path: relative,
                    full_path: path.clone(),
                    service_type: st,
                    manifest_files: manifests,
                });
            }
        }
    }

    candidates
}

/// Scans depth-1 subdirectories for well-known service names that contain
/// their own manifests.
fn discover_well_known_sub_services(root: &Path) -> Vec<DiscoveredServiceCandidate> {
    let mut candidates = Vec::new();
    let all_well_known: Vec<&str> = FRONTEND_DIRS
        .iter()
        .chain(BACKEND_DIRS.iter())
        .copied()
        .collect();

    if let Ok(entries) = fs::read_dir(root) {
        for entry in entries.flatten() {
            let path = entry.path();
            if !path.is_dir() {
                continue;
            }
            let dir_name = file_name(&path);
            if is_excluded(&dir_name) {
                continue;
            }
            let lower = dir_name.to_ascii_lowercase();
            if !all_well_known.contains(&lower.as_str()) {
                continue;
            }
            let manifests = find_manifests(&path);
            if !manifests.is_empty() && has_verifiable_source_files(&path) {
                let st = classify_service_type(&dir_name);
                let relative = relative_path(root, &path);
                candidates.push(DiscoveredServiceCandidate {
                    name: dir_name,
                    relative_path: relative,
                    full_path: path,
                    service_type: st,
                    manifest_files: manifests,
                });
            }
        }
    }

    candidates
}

// ---------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------

/// Returns `true` if `dir_name` is in the exclusion list.
pub fn is_excluded(dir_name: &str) -> bool {
    EXCLUDED_DIRS.contains(&dir_name)
}

/// Returns `true` if `dir` (non-recursively, up to `SOURCE_FILE_SCAN_DEPTH`
/// levels deep) contains at least one file whose extension is in
/// `SOURCE_FILE_EXTENSIONS`.
///
/// This prevents manifest-only directories (e.g. a `packages/shared`
/// containing only a `package.json` with no source files) from being
/// treated as distinct deployable services.
pub fn has_verifiable_source_files(dir: &Path) -> bool {
    has_source_files_recursive(dir, 0)
}

/// Recursive helper for `has_verifiable_source_files`.
fn has_source_files_recursive(dir: &Path, depth: usize) -> bool {
    if depth > SOURCE_FILE_SCAN_DEPTH {
        return false;
    }
    let Ok(entries) = fs::read_dir(dir) else {
        return false;
    };
    for entry in entries.flatten() {
        let path = entry.path();
        if path.is_file() {
            if let Some(ext) = path.extension().and_then(|e| e.to_str()) {
                if SOURCE_FILE_EXTENSIONS.contains(&ext) {
                    return true;
                }
            }
        } else if path.is_dir() {
            let dir_name = file_name(&path);
            if !is_excluded(&dir_name) && has_source_files_recursive(&path, depth + 1) {
                return true;
            }
        }
    }
    false
}

/// Scans `dir` (non-recursively) for known manifest file names and returns
/// their relative paths.
fn find_manifests(dir: &Path) -> Vec<PathBuf> {
    let mut found = Vec::new();
    for name in MANIFEST_FILES {
        let p = dir.join(name);
        if p.is_file() {
            found.push(PathBuf::from(name));
        }
    }
    found
}

/// Classifies a directory name into a [`ServiceType`].
fn classify_service_type(dir_name: &str) -> ServiceType {
    let lower = dir_name.to_ascii_lowercase();
    if FRONTEND_DIRS.contains(&lower.as_str()) {
        return ServiceType::Frontend;
    }
    if BACKEND_DIRS.contains(&lower.as_str()) {
        return ServiceType::Backend;
    }
    // Subdirectories inside well-known containers are MonorepoMember — but
    // this is handled at the call-site. Here we just handle top-level names.
    ServiceType::MonorepoMember
}

/// Extracts the file / directory name as a `String`.
fn file_name(path: &Path) -> String {
    path.file_name()
        .map(|n| n.to_string_lossy().into_owned())
        .unwrap_or_default()
}

/// Computes `path` relative to `base`.
fn relative_path(base: &Path, path: &Path) -> PathBuf {
    path.strip_prefix(base).unwrap_or(path).to_path_buf()
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

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

    // -- helpers -----------------------------------------------------------

    fn create_file(dir: &Path, name: &str) {
        fs::write(dir.join(name), "").expect("failed to create test file");
    }

    fn create_dir(dir: &Path, name: &str) -> PathBuf {
        let p = dir.join(name);
        fs::create_dir_all(&p).expect("failed to create test dir");
        p
    }

    // -- is_excluded -------------------------------------------------------

    #[test]
    fn excluded_dirs_recognized() {
        assert!(is_excluded("node_modules"));
        assert!(is_excluded(".git"));
        assert!(is_excluded("target"));
        assert!(is_excluded("vendor"));
        assert!(is_excluded("__pycache__"));
        assert!(is_excluded(".venv"));
    }

    #[test]
    fn non_excluded_dir_not_flagged() {
        assert!(!is_excluded("src"));
        assert!(!is_excluded("frontend"));
        assert!(!is_excluded("backend"));
    }

    // -- single service root -----------------------------------------------

    #[test]
    fn single_service_root_detection() {
        let tmp = TempDir::new().unwrap();
        let root = tmp.path();

        create_file(root, "package.json");

        let result = analyze_structure(root).unwrap();

        assert!(!result.is_monorepo);
        assert!(result.workspace_tool.is_none());
        assert_eq!(result.candidates.len(), 1);

        let svc = &result.candidates[0];
        assert_eq!(
            svc.name,
            root.file_name().unwrap().to_string_lossy().as_ref()
        );
        assert_eq!(svc.service_type, ServiceType::Single);
        assert_eq!(svc.relative_path, PathBuf::from("."));
        assert!(svc.manifest_files.contains(&PathBuf::from("package.json")));
    }

    #[test]
    fn single_service_cargo_root() {
        let tmp = TempDir::new().unwrap();
        let root = tmp.path();

        create_file(root, "Cargo.toml");

        let result = analyze_structure(root).unwrap();

        assert!(!result.is_monorepo);
        assert_eq!(result.candidates.len(), 1);
        assert_eq!(result.candidates[0].service_type, ServiceType::Single);
    }

    // -- monorepo detection (Level 1) --------------------------------------

    #[test]
    fn turbo_json_detected() {
        let tmp = TempDir::new().unwrap();
        let root = tmp.path();

        create_file(root, "turbo.json");
        let result = analyze_structure(root).unwrap();

        assert!(result.is_monorepo);
        assert_eq!(result.workspace_tool.as_deref(), Some("turborepo"));
    }

    #[test]
    fn nx_json_detected() {
        let tmp = TempDir::new().unwrap();
        let root = tmp.path();

        create_file(root, "nx.json");
        let result = analyze_structure(root).unwrap();

        assert!(result.is_monorepo);
        assert_eq!(result.workspace_tool.as_deref(), Some("nx"));
    }

    #[test]
    fn pnpm_workspace_detected() {
        let tmp = TempDir::new().unwrap();
        let root = tmp.path();

        create_file(root, "pnpm-workspace.yaml");
        let result = analyze_structure(root).unwrap();

        assert!(result.is_monorepo);
        assert_eq!(result.workspace_tool.as_deref(), Some("pnpm"));
    }

    #[test]
    fn lerna_detected() {
        let tmp = TempDir::new().unwrap();
        let root = tmp.path();

        create_file(root, "lerna.json");
        let result = analyze_structure(root).unwrap();

        assert!(result.is_monorepo);
        assert_eq!(result.workspace_tool.as_deref(), Some("lerna"));
    }

    #[test]
    fn go_work_detected() {
        let tmp = TempDir::new().unwrap();
        let root = tmp.path();

        create_file(root, "go.work");
        let result = analyze_structure(root).unwrap();

        assert!(result.is_monorepo);
        assert_eq!(result.workspace_tool.as_deref(), Some("go-work"));
    }

    #[test]
    fn cargo_workspace_detected() {
        let tmp = TempDir::new().unwrap();
        let root = tmp.path();

        create_file(root, "Cargo.toml");
        fs::write(
            root.join("Cargo.toml"),
            "[workspace]\nmembers = [\"crates/*\"]\n",
        )
        .unwrap();

        let result = analyze_structure(root).unwrap();

        assert!(result.is_monorepo);
        assert_eq!(result.workspace_tool.as_deref(), Some("cargo"));
    }

    #[test]
    fn npm_workspace_detected() {
        let tmp = TempDir::new().unwrap();
        let root = tmp.path();

        fs::write(
            root.join("package.json"),
            r#"{ "workspaces": ["apps/*", "packages/*"] }"#,
        )
        .unwrap();

        let result = analyze_structure(root).unwrap();

        assert!(result.is_monorepo);
        assert_eq!(result.workspace_tool.as_deref(), Some("npm"));
    }

    #[test]
    fn non_workspace_cargo_toml_not_monorepo() {
        let tmp = TempDir::new().unwrap();
        let root = tmp.path();

        // A Cargo.toml without [workspace] should not trigger monorepo.
        fs::write(
            root.join("Cargo.toml"),
            "[package]\nname = \"foo\"\nversion = \"0.1.0\"\nedition = \"2021\"\n",
        )
        .unwrap();

        let result = analyze_structure(root).unwrap();

        assert!(!result.is_monorepo);
        assert!(result.workspace_tool.is_none());
    }

    #[test]
    fn non_workspace_package_json_not_monorepo() {
        let tmp = TempDir::new().unwrap();
        let root = tmp.path();

        fs::write(
            root.join("package.json"),
            r#"{ "name": "foo", "version": "1.0.0" }"#,
        )
        .unwrap();

        let result = analyze_structure(root).unwrap();

        assert!(!result.is_monorepo);
        assert!(result.workspace_tool.is_none());
    }

    // -- empirical discovery (Level 2) -------------------------------------

    #[test]
    fn frontend_backend_folders_detected() {
        let tmp = TempDir::new().unwrap();
        let root = tmp.path();

        // Set up a monorepo so sub-dirs are scanned.
        create_file(root, "turbo.json");

        // frontend
        let fe = create_dir(root, "frontend");
        create_file(&fe, "package.json");
        create_file(&fe, "index.ts"); // source file

        // backend
        let be = create_dir(root, "backend");
        create_file(&be, "package.json");
        create_file(&be, "server.js"); // source file

        let result = analyze_structure(root).unwrap();

        assert!(result.is_monorepo);
        assert_eq!(result.candidates.len(), 2);

        let names: Vec<&str> = result.candidates.iter().map(|c| c.name.as_str()).collect();
        assert!(names.contains(&"backend"));
        assert!(names.contains(&"frontend"));

        let fe_candidate = result
            .candidates
            .iter()
            .find(|c| c.name == "frontend")
            .unwrap();
        assert_eq!(fe_candidate.service_type, ServiceType::Frontend);

        let be_candidate = result
            .candidates
            .iter()
            .find(|c| c.name == "backend")
            .unwrap();
        assert_eq!(be_candidate.service_type, ServiceType::Backend);
    }

    #[test]
    fn well_known_container_dirs() {
        let tmp = TempDir::new().unwrap();
        let root = tmp.path();

        create_file(root, "turbo.json");

        // apps/web
        let apps_web = create_dir(root, "apps");
        let apps_web = create_dir(&apps_web, "web");
        create_file(&apps_web, "package.json");
        create_file(&apps_web, "index.ts"); // source file

        // services/api
        let svc_api = create_dir(root, "services");
        let svc_api = create_dir(&svc_api, "api");
        create_file(&svc_api, "go.mod");
        create_file(&svc_api, "main.go"); // source file

        // packages/shared
        let pkg = create_dir(root, "packages");
        let pkg = create_dir(&pkg, "shared");
        create_file(&pkg, "package.json");
        create_file(&pkg, "index.ts"); // source file

        let result = analyze_structure(root).unwrap();

        assert!(result.is_monorepo);
        assert!(result.candidates.len() >= 3);

        let paths: Vec<PathBuf> = result
            .candidates
            .iter()
            .map(|c| c.relative_path.clone())
            .collect();
        assert!(paths.contains(&PathBuf::from("apps/web")));
        assert!(paths.contains(&PathBuf::from("services/api")));
        assert!(paths.contains(&PathBuf::from("packages/shared")));
    }

    #[test]
    fn node_modules_excluded() {
        let tmp = TempDir::new().unwrap();
        let root = tmp.path();

        create_file(root, "turbo.json");

        // node_modules should be skipped even if it contains package.json.
        let nm = create_dir(root, "node_modules");
        create_file(&nm, "package.json");

        // A real service.
        let svc = create_dir(root, "api");
        create_file(&svc, "package.json");
        create_file(&svc, "main.ts"); // source file

        let result = analyze_structure(root).unwrap();

        assert_eq!(result.candidates.len(), 1);
        assert_eq!(result.candidates[0].name, "api");
    }

    #[test]
    fn git_dir_excluded() {
        let tmp = TempDir::new().unwrap();
        let root = tmp.path();

        create_file(root, "turbo.json");

        let git_dir = create_dir(root, ".git");
        create_file(&git_dir, "config");

        let svc = create_dir(root, "web");
        create_file(&svc, "package.json");
        create_file(&svc, "index.ts"); // source file

        let result = analyze_structure(root).unwrap();

        assert_eq!(result.candidates.len(), 1);
        assert_eq!(result.candidates[0].name, "web");
    }

    #[test]
    fn dist_and_build_excluded() {
        let tmp = TempDir::new().unwrap();
        let root = tmp.path();

        create_file(root, "turbo.json");

        let dist = create_dir(root, "dist");
        create_file(&dist, "package.json");

        let build = create_dir(root, "build");
        create_file(&build, "Cargo.toml");

        let svc = create_dir(root, "app");
        create_file(&svc, "package.json");
        create_file(&svc, "index.ts"); // source file

        let result = analyze_structure(root).unwrap();

        assert_eq!(result.candidates.len(), 1);
        assert_eq!(result.candidates[0].name, "app");
    }

    #[test]
    fn no_manifests_no_candidates() {
        let tmp = TempDir::new().unwrap();
        let root = tmp.path();

        create_file(root, "turbo.json");

        // Empty sub-dirs with no manifests.
        create_dir(root, "frontend");
        create_dir(root, "backend");

        let result = analyze_structure(root).unwrap();

        assert!(result.is_monorepo);
        assert!(result.candidates.is_empty());
    }

    // -- flat project with sub-dirs ----------------------------------------

    #[test]
    fn flat_project_with_sub_services() {
        let tmp = TempDir::new().unwrap();
        let root = tmp.path();

        // No monorepo tool, root has no manifest.
        let fe = create_dir(root, "frontend");
        create_file(&fe, "package.json");
        create_file(&fe, "App.tsx"); // source file

        let be = create_dir(root, "backend");
        create_file(&be, "go.mod");
        create_file(&be, "main.go"); // source file

        let result = analyze_structure(root).unwrap();

        assert!(!result.is_monorepo);
        assert!(result.workspace_tool.is_none());
        assert_eq!(result.candidates.len(), 2);

        let fe_c = result
            .candidates
            .iter()
            .find(|c| c.name == "frontend")
            .unwrap();
        assert_eq!(fe_c.service_type, ServiceType::Frontend);

        let be_c = result
            .candidates
            .iter()
            .find(|c| c.name == "backend")
            .unwrap();
        assert_eq!(be_c.service_type, ServiceType::Backend);
    }

    #[test]
    fn root_manifest_with_well_known_sub_services() {
        let tmp = TempDir::new().unwrap();
        let root = tmp.path();

        // Root has a manifest (e.g. Laravel + frontend build).
        create_file(root, "composer.json");
        create_file(root, "package.json");

        // Well-known sub-services with their own manifests.
        let fe = create_dir(root, "frontend");
        create_file(&fe, "package.json");
        create_file(&fe, "App.tsx"); // source file

        let be = create_dir(root, "backend");
        create_file(&be, "composer.json");
        create_file(&be, "app.php"); // source file

        let result = analyze_structure(root).unwrap();

        assert!(!result.is_monorepo);
        // Should discover sub-services, not root as Single.
        assert_eq!(result.candidates.len(), 2);

        let names: Vec<&str> = result.candidates.iter().map(|c| c.name.as_str()).collect();
        assert!(names.contains(&"frontend"));
        assert!(names.contains(&"backend"));

        let fe_c = result
            .candidates
            .iter()
            .find(|c| c.name == "frontend")
            .unwrap();
        assert_eq!(fe_c.service_type, ServiceType::Frontend);

        let be_c = result
            .candidates
            .iter()
            .find(|c| c.name == "backend")
            .unwrap();
        assert_eq!(be_c.service_type, ServiceType::Backend);
    }

    #[test]
    fn root_manifest_no_well_known_sub_services() {
        let tmp = TempDir::new().unwrap();
        let root = tmp.path();

        // Root has manifest, sub-dirs are NOT well-known service names.
        create_file(root, "composer.json");

        let app = create_dir(root, "app");
        create_file(&app, "User.php");

        let config = create_dir(root, "config");
        create_file(&config, "app.php");

        let result = analyze_structure(root).unwrap();

        assert!(!result.is_monorepo);
        assert_eq!(result.candidates.len(), 1);
        assert_eq!(result.candidates[0].service_type, ServiceType::Single);
    }

    // -- deterministic sorting ----------------------------------------------

    #[test]
    fn candidates_sorted_by_relative_path() {
        let tmp = TempDir::new().unwrap();
        let root = tmp.path();

        create_file(root, "turbo.json");

        let c = create_dir(root, "services");
        let api = create_dir(&c, "api");
        create_file(&api, "package.json");
        create_file(&api, "main.ts"); // source file
        let web = create_dir(&c, "web");
        create_file(&web, "package.json");
        create_file(&web, "app.tsx"); // source file
        let auth = create_dir(&c, "auth");
        create_file(&auth, "go.mod");
        create_file(&auth, "main.go"); // source file

        let result = analyze_structure(root).unwrap();

        let paths: Vec<PathBuf> = result
            .candidates
            .iter()
            .map(|c| c.relative_path.clone())
            .collect();
        let mut sorted = paths.clone();
        sorted.sort();
        assert_eq!(paths, sorted);
    }

    // -- multiple manifests -------------------------------------------------

    #[test]
    fn multiple_manifests_collected() {
        let tmp = TempDir::new().unwrap();
        let root = tmp.path();

        create_file(root, "turbo.json");

        let svc = create_dir(root, "api");
        create_file(&svc, "Cargo.toml");
        create_file(&svc, "docker-compose.yml");
        create_file(&svc, "main.rs"); // source file

        let result = analyze_structure(root).unwrap();

        assert_eq!(result.candidates.len(), 1);
        assert!(result.candidates[0]
            .manifest_files
            .contains(&PathBuf::from("Cargo.toml")));
    }

    // -- relative paths ----------------------------------------------------

    #[test]
    fn relative_paths_are_correct() {
        let tmp = TempDir::new().unwrap();
        let root = tmp.path();

        create_file(root, "turbo.json");

        let apps = create_dir(root, "apps");
        let web = create_dir(&apps, "web");
        create_file(&web, "package.json");
        create_file(&web, "index.ts"); // source file

        let result = analyze_structure(root).unwrap();

        assert_eq!(result.candidates.len(), 1);
        assert_eq!(
            result.candidates[0].relative_path,
            PathBuf::from("apps/web")
        );
    }

    // -- source file verification -------------------------------------------

    #[test]
    fn monorepo_candidate_without_source_files_rejected() {
        let tmp = TempDir::new().unwrap();
        let root = tmp.path();

        create_file(root, "turbo.json");

        // apps/web has a package.json but no source files.
        let apps_web = create_dir(root, "apps");
        let apps_web = create_dir(&apps_web, "web");
        create_file(&apps_web, "package.json");

        // services/api has a go.mod AND source files — should be discovered.
        let svc_api = create_dir(root, "services");
        let svc_api = create_dir(&svc_api, "api");
        create_file(&svc_api, "go.mod");
        create_file(&svc_api, "main.go");

        let result = analyze_structure(root).unwrap();

        // Only the api service should be discovered (web has no source files).
        assert_eq!(result.candidates.len(), 1);
        assert_eq!(result.candidates[0].name, "api");
    }

    #[test]
    fn flat_subdir_without_source_files_rejected() {
        let tmp = TempDir::new().unwrap();
        let root = tmp.path();

        // No monorepo tool, no root manifest — triggers flat candidate scan.
        let fe = create_dir(root, "frontend");
        create_file(&fe, "package.json");
        // No source files in frontend/ — should NOT be discovered.

        let be = create_dir(root, "backend");
        create_file(&be, "go.mod");
        create_file(&be, "main.go"); // Has source files — should be discovered.

        let result = analyze_structure(root).unwrap();

        assert!(!result.is_monorepo);
        assert_eq!(result.candidates.len(), 1);
        assert_eq!(result.candidates[0].name, "backend");
    }

    #[test]
    fn has_verifiable_source_files_scans_depth_3() {
        let tmp = TempDir::new().unwrap();
        let root = tmp.path();

        // Create nested structure: src/components/Button.tsx
        let src = create_dir(root, "src");
        let components = create_dir(&src, "components");
        create_file(&components, "Button.tsx");

        assert!(has_verifiable_source_files(root));
    }

    #[test]
    fn has_verifiable_source_files_ignores_excluded_dirs() {
        let tmp = TempDir::new().unwrap();
        let root = tmp.path();

        // Source file inside node_modules should NOT count.
        let nm = create_dir(root, "node_modules");
        create_file(&nm, "index.js");

        assert!(!has_verifiable_source_files(root));
    }

    #[test]
    fn has_verifiable_source_files_returns_false_for_empty_dir() {
        let tmp = TempDir::new().unwrap();
        let root = tmp.path();
        create_dir(root, "empty");

        assert!(!has_verifiable_source_files(&root.join("empty")));
    }
}