dx-driven 0.1.0

Professional AI-assisted development orchestrator with binary-first architecture
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
//! System Information Provider
//!
//! Provides comprehensive system information to agents, including OS details,
//! shell environment, installed languages, package managers, project structure,
//! git status, build tools, and test frameworks.
//!
//! Features:
//! - Caching with configurable TTL
//! - Automatic cache invalidation on system changes
//! - Project type detection from marker files

use serde::{Deserialize, Serialize};
use std::path::{Path, PathBuf};
use std::process::Command;
use std::time::Duration;

mod cache;
mod detectors;

#[cfg(test)]
mod property_tests;

pub use cache::{CacheEntry, SystemInfoCache};
pub use detectors::*;

/// Complete system information
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SystemInfo {
    /// Operating system details
    pub os: OsInfo,
    /// Shell environment
    pub shell: ShellInfo,
    /// Installed programming languages
    pub languages: Vec<LanguageInfo>,
    /// Available package managers
    pub package_managers: Vec<PackageManagerInfo>,
    /// Project information
    pub project: Option<ProjectInfo>,
    /// Git repository status
    pub git: Option<GitInfo>,
    /// Available build tools
    pub build_tools: Vec<BuildToolInfo>,
    /// Available test frameworks
    pub test_frameworks: Vec<TestFrameworkInfo>,
    /// Timestamp when info was collected
    pub collected_at: std::time::SystemTime,
}

/// Operating system information
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct OsInfo {
    /// OS name (e.g., "Windows", "Linux", "macOS")
    pub name: String,
    /// OS version
    pub version: String,
    /// Architecture (e.g., "x86_64", "aarch64")
    pub arch: String,
    /// OS family (e.g., "unix", "windows")
    pub family: String,
}

/// Shell environment information
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct ShellInfo {
    /// Shell name (e.g., "bash", "zsh", "powershell")
    pub name: String,
    /// Shell version
    pub version: Option<String>,
    /// Path to shell executable
    pub path: PathBuf,
}

/// Programming language information
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct LanguageInfo {
    /// Language name
    pub name: String,
    /// Version string
    pub version: String,
    /// Path to executable
    pub path: PathBuf,
}

/// Package manager information
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct PackageManagerInfo {
    /// Package manager name (e.g., "cargo", "npm", "pip")
    pub name: String,
    /// Version string
    pub version: Option<String>,
    /// Path to executable
    pub path: PathBuf,
}

/// Project information
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct ProjectInfo {
    /// Detected project type
    pub project_type: ProjectType,
    /// Project root directory
    pub root: PathBuf,
    /// Project name (from manifest if available)
    pub name: Option<String>,
    /// Detected frameworks
    pub frameworks: Vec<String>,
    /// Source directories
    pub source_dirs: Vec<PathBuf>,
}

/// Project type enumeration
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq, Hash)]
pub enum ProjectType {
    Rust,
    Node,
    Python,
    Go,
    Java,
    CSharp,
    Ruby,
    Php,
    Swift,
    Kotlin,
    Cpp,
    C,
    Mixed,
    Unknown,
}

impl std::fmt::Display for ProjectType {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            ProjectType::Rust => write!(f, "Rust"),
            ProjectType::Node => write!(f, "Node.js"),
            ProjectType::Python => write!(f, "Python"),
            ProjectType::Go => write!(f, "Go"),
            ProjectType::Java => write!(f, "Java"),
            ProjectType::CSharp => write!(f, "C#"),
            ProjectType::Ruby => write!(f, "Ruby"),
            ProjectType::Php => write!(f, "PHP"),
            ProjectType::Swift => write!(f, "Swift"),
            ProjectType::Kotlin => write!(f, "Kotlin"),
            ProjectType::Cpp => write!(f, "C++"),
            ProjectType::C => write!(f, "C"),
            ProjectType::Mixed => write!(f, "Mixed"),
            ProjectType::Unknown => write!(f, "Unknown"),
        }
    }
}

/// Git repository information
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct GitInfo {
    /// Current branch name
    pub branch: String,
    /// Remote URL (if configured)
    pub remote: Option<String>,
    /// Whether there are uncommitted changes
    pub is_dirty: bool,
    /// Number of commits ahead of remote
    pub ahead: u32,
    /// Number of commits behind remote
    pub behind: u32,
    /// Last commit hash (short)
    pub last_commit: Option<String>,
}

/// Build tool information
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct BuildToolInfo {
    /// Tool name (e.g., "cargo", "make", "gradle")
    pub name: String,
    /// Version string
    pub version: Option<String>,
    /// Path to executable
    pub path: PathBuf,
}

/// Test framework information
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct TestFrameworkInfo {
    /// Framework name (e.g., "cargo test", "jest", "pytest")
    pub name: String,
    /// Version string
    pub version: Option<String>,
    /// Associated language
    pub language: String,
}

/// System information provider with caching
pub struct SystemInfoProvider {
    cache: SystemInfoCache,
    ttl: Duration,
    project_root: Option<PathBuf>,
}

impl Default for SystemInfoProvider {
    fn default() -> Self {
        Self::new(Duration::from_secs(300)) // 5 minute default TTL
    }
}

impl SystemInfoProvider {
    /// Create a new provider with specified TTL
    pub fn new(ttl: Duration) -> Self {
        Self {
            cache: SystemInfoCache::new(),
            ttl,
            project_root: None,
        }
    }

    /// Set the project root for project-specific detection
    pub fn with_project_root(mut self, root: impl AsRef<Path>) -> Self {
        self.project_root = Some(root.as_ref().to_path_buf());
        self
    }

    /// Get system information (cached if within TTL)
    pub fn get(&mut self) -> crate::Result<SystemInfo> {
        if let Some(cached) = self.cache.get_if_valid(self.ttl) {
            return Ok(cached.clone());
        }
        self.refresh()
    }

    /// Force refresh of system information
    pub fn refresh(&mut self) -> crate::Result<SystemInfo> {
        let info = SystemInfo {
            os: self.detect_os()?,
            shell: self.detect_shell()?,
            languages: self.detect_languages(),
            package_managers: self.detect_package_managers(),
            project: self.detect_project(),
            git: self.detect_git(),
            build_tools: self.detect_build_tools(),
            test_frameworks: self.detect_test_frameworks(),
            collected_at: std::time::SystemTime::now(),
        };
        self.cache.set(info.clone());
        Ok(info)
    }

    /// Invalidate the cache
    pub fn invalidate(&mut self) {
        self.cache.invalidate();
    }

    /// Check if cache is valid
    pub fn is_cache_valid(&self) -> bool {
        self.cache.is_valid(self.ttl)
    }

    /// Detect operating system information
    pub fn detect_os(&self) -> crate::Result<OsInfo> {
        Ok(OsInfo {
            name: std::env::consts::OS.to_string(),
            version: get_os_version(),
            arch: std::env::consts::ARCH.to_string(),
            family: std::env::consts::FAMILY.to_string(),
        })
    }

    /// Detect shell environment
    pub fn detect_shell(&self) -> crate::Result<ShellInfo> {
        #[cfg(windows)]
        {
            // Check for PowerShell first, then cmd
            if let Ok(output) = Command::new("powershell")
                .args(["-Command", "$PSVersionTable.PSVersion.ToString()"])
                .output()
            {
                if output.status.success() {
                    let version = String::from_utf8_lossy(&output.stdout).trim().to_string();
                    return Ok(ShellInfo {
                        name: "powershell".to_string(),
                        version: Some(version),
                        path: PathBuf::from("powershell.exe"),
                    });
                }
            }
            Ok(ShellInfo {
                name: "cmd".to_string(),
                version: None,
                path: PathBuf::from("cmd.exe"),
            })
        }

        #[cfg(not(windows))]
        {
            let shell_path = std::env::var("SHELL").unwrap_or_else(|_| "/bin/sh".to_string());
            let shell_name = Path::new(&shell_path)
                .file_name()
                .and_then(|n| n.to_str())
                .unwrap_or("sh")
                .to_string();

            let version = get_shell_version(&shell_name);

            Ok(ShellInfo {
                name: shell_name,
                version,
                path: PathBuf::from(shell_path),
            })
        }
    }

    /// Detect installed programming languages
    pub fn detect_languages(&self) -> Vec<LanguageInfo> {
        let mut languages = Vec::new();

        // Rust
        if let Some(info) = detect_language("rustc", &["--version"], "rust") {
            languages.push(info);
        }

        // Python
        if let Some(info) = detect_language("python3", &["--version"], "python") {
            languages.push(info);
        } else if let Some(info) = detect_language("python", &["--version"], "python") {
            languages.push(info);
        }

        // Node.js
        if let Some(info) = detect_language("node", &["--version"], "node") {
            languages.push(info);
        }

        // Go
        if let Some(info) = detect_language("go", &["version"], "go") {
            languages.push(info);
        }

        // Java
        if let Some(info) = detect_language("java", &["-version"], "java") {
            languages.push(info);
        }

        // Ruby
        if let Some(info) = detect_language("ruby", &["--version"], "ruby") {
            languages.push(info);
        }

        // PHP
        if let Some(info) = detect_language("php", &["--version"], "php") {
            languages.push(info);
        }

        languages
    }

    /// Detect available package managers
    pub fn detect_package_managers(&self) -> Vec<PackageManagerInfo> {
        let mut managers = Vec::new();

        // Cargo (Rust)
        if let Some(info) = detect_package_manager("cargo", &["--version"]) {
            managers.push(info);
        }

        // npm (Node)
        if let Some(info) = detect_package_manager("npm", &["--version"]) {
            managers.push(info);
        }

        // yarn (Node)
        if let Some(info) = detect_package_manager("yarn", &["--version"]) {
            managers.push(info);
        }

        // pnpm (Node)
        if let Some(info) = detect_package_manager("pnpm", &["--version"]) {
            managers.push(info);
        }

        // pip (Python)
        if let Some(info) = detect_package_manager("pip", &["--version"]) {
            managers.push(info);
        }

        // go mod (Go)
        if let Some(info) = detect_package_manager("go", &["version"]) {
            let mut info = info;
            info.name = "go mod".to_string();
            managers.push(info);
        }

        managers
    }

    /// Detect project structure and type
    pub fn detect_project(&self) -> Option<ProjectInfo> {
        let root = self.project_root.as_ref()?;

        if !root.exists() {
            return None;
        }

        let project_type = detect_project_type(root);
        let name = detect_project_name(root, project_type);
        let frameworks = detect_frameworks(root, project_type);
        let source_dirs = detect_source_dirs(root, project_type);

        Some(ProjectInfo {
            project_type,
            root: root.clone(),
            name,
            frameworks,
            source_dirs,
        })
    }

    /// Detect git repository status
    pub fn detect_git(&self) -> Option<GitInfo> {
        let root = self.project_root.as_ref()?;

        // Check if .git exists
        if !root.join(".git").exists() {
            return None;
        }

        let branch = run_git_command(root, &["rev-parse", "--abbrev-ref", "HEAD"])?;
        let remote = run_git_command(root, &["config", "--get", "remote.origin.url"]);
        let is_dirty = run_git_command(root, &["status", "--porcelain"])
            .map(|s| !s.is_empty())
            .unwrap_or(false);

        let (ahead, behind) = get_git_ahead_behind(root);
        let last_commit = run_git_command(root, &["rev-parse", "--short", "HEAD"]);

        Some(GitInfo {
            branch,
            remote,
            is_dirty,
            ahead,
            behind,
            last_commit,
        })
    }

    /// Detect available build tools
    pub fn detect_build_tools(&self) -> Vec<BuildToolInfo> {
        let mut tools = Vec::new();

        // Cargo (Rust)
        if let Some(info) = detect_build_tool("cargo", &["--version"]) {
            tools.push(info);
        }

        // Make
        if let Some(info) = detect_build_tool("make", &["--version"]) {
            tools.push(info);
        }

        // CMake
        if let Some(info) = detect_build_tool("cmake", &["--version"]) {
            tools.push(info);
        }

        // Gradle
        if let Some(info) = detect_build_tool("gradle", &["--version"]) {
            tools.push(info);
        }

        // Maven
        if let Some(info) = detect_build_tool("mvn", &["--version"]) {
            let mut info = info;
            info.name = "maven".to_string();
            tools.push(info);
        }

        tools
    }

    /// Detect available test frameworks
    pub fn detect_test_frameworks(&self) -> Vec<TestFrameworkInfo> {
        let mut frameworks = Vec::new();

        // Check project-specific test frameworks
        if let Some(ref root) = self.project_root {
            // Rust: cargo test is always available if Cargo.toml exists
            if root.join("Cargo.toml").exists() {
                frameworks.push(TestFrameworkInfo {
                    name: "cargo test".to_string(),
                    version: None,
                    language: "rust".to_string(),
                });
            }

            // Node: check for jest, mocha, vitest
            if root.join("package.json").exists() {
                if let Some(fw) = detect_node_test_framework(root) {
                    frameworks.push(fw);
                }
            }

            // Python: check for pytest, unittest
            if root.join("pyproject.toml").exists() || root.join("setup.py").exists() {
                if let Some(fw) = detect_python_test_framework(root) {
                    frameworks.push(fw);
                }
            }
        }

        frameworks
    }
}

// Helper functions

fn get_os_version() -> String {
    #[cfg(windows)]
    {
        if let Ok(output) = Command::new("cmd").args(["/c", "ver"]).output() {
            let version = String::from_utf8_lossy(&output.stdout);
            // Extract version number from "Microsoft Windows [Version 10.0.xxxxx]"
            if let Some(start) = version.find('[') {
                if let Some(end) = version.find(']') {
                    return version[start + 1..end].replace("Version ", "");
                }
            }
        }
        "unknown".to_string()
    }

    #[cfg(target_os = "macos")]
    {
        if let Ok(output) = Command::new("sw_vers").args(["-productVersion"]).output() {
            return String::from_utf8_lossy(&output.stdout).trim().to_string();
        }
        "unknown".to_string()
    }

    #[cfg(target_os = "linux")]
    {
        if let Ok(output) = Command::new("uname").args(["-r"]).output() {
            return String::from_utf8_lossy(&output.stdout).trim().to_string();
        }
        "unknown".to_string()
    }

    #[cfg(not(any(windows, target_os = "macos", target_os = "linux")))]
    {
        "unknown".to_string()
    }
}

#[cfg(not(windows))]
fn get_shell_version(shell_name: &str) -> Option<String> {
    let args = match shell_name {
        "bash" => vec!["--version"],
        "zsh" => vec!["--version"],
        "fish" => vec!["--version"],
        _ => return None,
    };

    Command::new(shell_name)
        .args(&args)
        .output()
        .ok()
        .and_then(|output| {
            let version = String::from_utf8_lossy(&output.stdout);
            version.lines().next().map(|s| s.to_string())
        })
}

fn detect_language(cmd: &str, args: &[&str], name: &str) -> Option<LanguageInfo> {
    let output = Command::new(cmd).args(args).output().ok()?;

    if !output.status.success() {
        return None;
    }

    let version_output = String::from_utf8_lossy(&output.stdout);
    let version = extract_version(&version_output);

    let path = which::which(cmd).ok()?;

    Some(LanguageInfo {
        name: name.to_string(),
        version,
        path,
    })
}

fn detect_package_manager(cmd: &str, args: &[&str]) -> Option<PackageManagerInfo> {
    let output = Command::new(cmd).args(args).output().ok()?;

    if !output.status.success() {
        return None;
    }

    let version_output = String::from_utf8_lossy(&output.stdout);
    let version = Some(extract_version(&version_output));

    let path = which::which(cmd).ok()?;

    Some(PackageManagerInfo {
        name: cmd.to_string(),
        version,
        path,
    })
}

fn detect_build_tool(cmd: &str, args: &[&str]) -> Option<BuildToolInfo> {
    let output = Command::new(cmd).args(args).output().ok()?;

    if !output.status.success() {
        return None;
    }

    let version_output = String::from_utf8_lossy(&output.stdout);
    let version = Some(extract_version(&version_output));

    let path = which::which(cmd).ok()?;

    Some(BuildToolInfo {
        name: cmd.to_string(),
        version,
        path,
    })
}

fn extract_version(output: &str) -> String {
    // Try to extract version number from common patterns
    let re = regex::Regex::new(r"(\d+\.\d+(?:\.\d+)?(?:-[\w.]+)?)").ok();

    if let Some(re) = re {
        if let Some(caps) = re.captures(output) {
            return caps
                .get(1)
                .map(|m| m.as_str().to_string())
                .unwrap_or_default();
        }
    }

    output.lines().next().unwrap_or("").trim().to_string()
}

fn detect_project_type(root: &Path) -> ProjectType {
    // Check for project marker files
    let markers: Vec<(&str, ProjectType)> = vec![
        ("Cargo.toml", ProjectType::Rust),
        ("package.json", ProjectType::Node),
        ("pyproject.toml", ProjectType::Python),
        ("setup.py", ProjectType::Python),
        ("requirements.txt", ProjectType::Python),
        ("go.mod", ProjectType::Go),
        ("pom.xml", ProjectType::Java),
        ("build.gradle", ProjectType::Java),
        ("build.gradle.kts", ProjectType::Kotlin),
        ("*.csproj", ProjectType::CSharp),
        ("Gemfile", ProjectType::Ruby),
        ("composer.json", ProjectType::Php),
        ("Package.swift", ProjectType::Swift),
        ("CMakeLists.txt", ProjectType::Cpp),
        ("Makefile", ProjectType::C),
    ];

    let mut detected = Vec::new();

    for (marker, project_type) in markers {
        if marker.contains('*') {
            // Glob pattern
            let pattern = root.join(marker);
            if let Ok(entries) = glob::glob(pattern.to_str().unwrap_or("")) {
                if entries.count() > 0 {
                    detected.push(project_type);
                }
            }
        } else if root.join(marker).exists() {
            detected.push(project_type);
        }
    }

    match detected.len() {
        0 => ProjectType::Unknown,
        1 => detected[0],
        _ => ProjectType::Mixed,
    }
}

fn detect_project_name(root: &Path, project_type: ProjectType) -> Option<String> {
    match project_type {
        ProjectType::Rust => {
            let cargo_toml = root.join("Cargo.toml");
            if let Ok(content) = std::fs::read_to_string(&cargo_toml) {
                if let Ok(parsed) = content.parse::<toml::Table>() {
                    return parsed
                        .get("package")
                        .and_then(|p| p.get("name"))
                        .and_then(|n| n.as_str())
                        .map(|s| s.to_string());
                }
            }
        }
        ProjectType::Node => {
            let package_json = root.join("package.json");
            if let Ok(content) = std::fs::read_to_string(&package_json) {
                if let Ok(parsed) = serde_json::from_str::<serde_json::Value>(&content) {
                    return parsed
                        .get("name")
                        .and_then(|n| n.as_str())
                        .map(|s| s.to_string());
                }
            }
        }
        _ => {}
    }

    // Fall back to directory name
    root.file_name()
        .and_then(|n| n.to_str())
        .map(|s| s.to_string())
}

fn detect_frameworks(root: &Path, project_type: ProjectType) -> Vec<String> {
    let mut frameworks = Vec::new();

    match project_type {
        ProjectType::Rust => {
            if let Ok(content) = std::fs::read_to_string(root.join("Cargo.toml")) {
                // Check for common Rust frameworks
                if content.contains("actix-web") {
                    frameworks.push("Actix Web".to_string());
                }
                if content.contains("axum") {
                    frameworks.push("Axum".to_string());
                }
                if content.contains("rocket") {
                    frameworks.push("Rocket".to_string());
                }
                if content.contains("tokio") {
                    frameworks.push("Tokio".to_string());
                }
            }
        }
        ProjectType::Node => {
            if let Ok(content) = std::fs::read_to_string(root.join("package.json")) {
                if content.contains("\"react\"") {
                    frameworks.push("React".to_string());
                }
                if content.contains("\"vue\"") {
                    frameworks.push("Vue".to_string());
                }
                if content.contains("\"next\"") {
                    frameworks.push("Next.js".to_string());
                }
                if content.contains("\"express\"") {
                    frameworks.push("Express".to_string());
                }
                if content.contains("\"nestjs\"") || content.contains("\"@nestjs") {
                    frameworks.push("NestJS".to_string());
                }
            }
        }
        ProjectType::Python => {
            if let Ok(content) = std::fs::read_to_string(root.join("pyproject.toml")) {
                if content.contains("django") {
                    frameworks.push("Django".to_string());
                }
                if content.contains("flask") {
                    frameworks.push("Flask".to_string());
                }
                if content.contains("fastapi") {
                    frameworks.push("FastAPI".to_string());
                }
            }
        }
        _ => {}
    }

    frameworks
}

fn detect_source_dirs(root: &Path, project_type: ProjectType) -> Vec<PathBuf> {
    let mut dirs = Vec::new();

    let common_dirs = match project_type {
        ProjectType::Rust => vec!["src", "crates"],
        ProjectType::Node => vec!["src", "lib", "app"],
        ProjectType::Python => vec![
            "src",
            "lib",
            root.file_name().and_then(|n| n.to_str()).unwrap_or(""),
        ],
        ProjectType::Go => vec!["cmd", "pkg", "internal"],
        ProjectType::Java => vec!["src/main/java", "src"],
        _ => vec!["src", "lib"],
    };

    for dir in common_dirs {
        let path = root.join(dir);
        if path.exists() && path.is_dir() {
            dirs.push(path);
        }
    }

    dirs
}

fn run_git_command(root: &Path, args: &[&str]) -> Option<String> {
    Command::new("git")
        .current_dir(root)
        .args(args)
        .output()
        .ok()
        .filter(|o| o.status.success())
        .map(|o| String::from_utf8_lossy(&o.stdout).trim().to_string())
}

fn get_git_ahead_behind(root: &Path) -> (u32, u32) {
    let output = run_git_command(
        root,
        &["rev-list", "--left-right", "--count", "HEAD...@{upstream}"],
    );

    if let Some(output) = output {
        let parts: Vec<&str> = output.split_whitespace().collect();
        if parts.len() == 2 {
            let ahead = parts[0].parse().unwrap_or(0);
            let behind = parts[1].parse().unwrap_or(0);
            return (ahead, behind);
        }
    }

    (0, 0)
}

fn detect_node_test_framework(root: &Path) -> Option<TestFrameworkInfo> {
    let package_json = root.join("package.json");
    if let Ok(content) = std::fs::read_to_string(&package_json) {
        if content.contains("\"vitest\"") {
            return Some(TestFrameworkInfo {
                name: "vitest".to_string(),
                version: None,
                language: "javascript".to_string(),
            });
        }
        if content.contains("\"jest\"") {
            return Some(TestFrameworkInfo {
                name: "jest".to_string(),
                version: None,
                language: "javascript".to_string(),
            });
        }
        if content.contains("\"mocha\"") {
            return Some(TestFrameworkInfo {
                name: "mocha".to_string(),
                version: None,
                language: "javascript".to_string(),
            });
        }
    }
    None
}

fn detect_python_test_framework(root: &Path) -> Option<TestFrameworkInfo> {
    // Check pyproject.toml for pytest
    if let Ok(content) = std::fs::read_to_string(root.join("pyproject.toml")) {
        if content.contains("pytest") {
            return Some(TestFrameworkInfo {
                name: "pytest".to_string(),
                version: None,
                language: "python".to_string(),
            });
        }
    }

    // Check for pytest.ini or conftest.py
    if root.join("pytest.ini").exists() || root.join("conftest.py").exists() {
        return Some(TestFrameworkInfo {
            name: "pytest".to_string(),
            version: None,
            language: "python".to_string(),
        });
    }

    // Default to unittest
    Some(TestFrameworkInfo {
        name: "unittest".to_string(),
        version: None,
        language: "python".to_string(),
    })
}

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

    #[test]
    fn test_detect_os() {
        let provider = SystemInfoProvider::default();
        let os = provider.detect_os().unwrap();

        assert!(!os.name.is_empty());
        assert!(!os.arch.is_empty());
        assert!(!os.family.is_empty());
    }

    #[test]
    fn test_detect_shell() {
        let provider = SystemInfoProvider::default();
        let shell = provider.detect_shell().unwrap();

        assert!(!shell.name.is_empty());
        assert!(shell.path.exists() || cfg!(windows)); // On Windows, path might be relative
    }

    #[test]
    fn test_detect_languages() {
        let provider = SystemInfoProvider::default();
        let languages = provider.detect_languages();

        // At minimum, Rust should be detected since we're running Rust tests
        // But this might not always be true in CI, so just check it doesn't panic
        assert!(languages.len() >= 0);
    }

    #[test]
    fn test_detect_rust_project() {
        let temp = TempDir::new().unwrap();
        fs::write(
            temp.path().join("Cargo.toml"),
            r#"[package]
name = "test-project"
version = "0.1.0"
"#,
        )
        .unwrap();
        fs::create_dir(temp.path().join("src")).unwrap();

        let provider = SystemInfoProvider::default().with_project_root(temp.path());

        let project = provider.detect_project().unwrap();
        assert_eq!(project.project_type, ProjectType::Rust);
        assert_eq!(project.name, Some("test-project".to_string()));
    }

    #[test]
    fn test_detect_node_project() {
        let temp = TempDir::new().unwrap();
        fs::write(
            temp.path().join("package.json"),
            r#"{"name": "test-node-project", "version": "1.0.0"}"#,
        )
        .unwrap();

        let provider = SystemInfoProvider::default().with_project_root(temp.path());

        let project = provider.detect_project().unwrap();
        assert_eq!(project.project_type, ProjectType::Node);
        assert_eq!(project.name, Some("test-node-project".to_string()));
    }

    #[test]
    fn test_detect_git_repo() {
        let temp = TempDir::new().unwrap();

        // Initialize a git repo
        let init_result = std::process::Command::new("git")
            .current_dir(temp.path())
            .args(["init"])
            .output();

        // Skip test if git is not available
        if init_result.is_err() || !temp.path().join(".git").exists() {
            return;
        }

        // Configure git user for the commit (required in some environments)
        let _ = std::process::Command::new("git")
            .current_dir(temp.path())
            .args(["config", "user.email", "test@test.com"])
            .output();
        let _ = std::process::Command::new("git")
            .current_dir(temp.path())
            .args(["config", "user.name", "Test"])
            .output();

        // Create an initial commit so HEAD exists
        let test_file = temp.path().join("test.txt");
        std::fs::write(&test_file, "test").unwrap();
        let _ = std::process::Command::new("git")
            .current_dir(temp.path())
            .args(["add", "."])
            .output();
        let commit_result = std::process::Command::new("git")
            .current_dir(temp.path())
            .args(["commit", "-m", "initial"])
            .output();

        // Skip if commit failed (git might not be properly configured)
        if commit_result.is_err() || !commit_result.unwrap().status.success() {
            return;
        }

        let provider = SystemInfoProvider::default().with_project_root(temp.path());

        let git = provider.detect_git();
        assert!(
            git.is_some(),
            "Git should be detected when .git directory exists with commits"
        );
    }

    #[test]
    fn test_cache_behavior() {
        let mut provider = SystemInfoProvider::new(Duration::from_secs(60));

        // First call should populate cache
        let info1 = provider.get().unwrap();
        assert!(provider.is_cache_valid());

        // Second call should return cached value
        let info2 = provider.get().unwrap();
        assert_eq!(info1.os, info2.os);

        // Invalidate and check
        provider.invalidate();
        assert!(!provider.is_cache_valid());
    }

    #[test]
    fn test_project_type_display() {
        assert_eq!(ProjectType::Rust.to_string(), "Rust");
        assert_eq!(ProjectType::Node.to_string(), "Node.js");
        assert_eq!(ProjectType::Python.to_string(), "Python");
    }

    #[test]
    fn test_extract_version() {
        assert_eq!(extract_version("rustc 1.75.0"), "1.75.0");
        assert_eq!(extract_version("Python 3.11.4"), "3.11.4");
        assert_eq!(extract_version("v20.10.0"), "20.10.0");
    }

    #[test]
    fn test_detect_frameworks_rust() {
        let temp = TempDir::new().unwrap();
        fs::write(
            temp.path().join("Cargo.toml"),
            r#"[dependencies]
tokio = "1.0"
axum = "0.7"
"#,
        )
        .unwrap();

        let frameworks = detect_frameworks(temp.path(), ProjectType::Rust);
        assert!(frameworks.contains(&"Tokio".to_string()));
        assert!(frameworks.contains(&"Axum".to_string()));
    }

    #[test]
    fn test_detect_frameworks_node() {
        let temp = TempDir::new().unwrap();
        fs::write(
            temp.path().join("package.json"),
            r#"{"dependencies": {"react": "^18.0.0", "next": "^14.0.0"}}"#,
        )
        .unwrap();

        let frameworks = detect_frameworks(temp.path(), ProjectType::Node);
        assert!(frameworks.contains(&"React".to_string()));
        assert!(frameworks.contains(&"Next.js".to_string()));
    }
}