jarvy 0.0.5

Jarvy is a fast, cross-platform CLI that installs and manages developer tools across macOS and Linux.
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
//! Deep Tool Diagnosis Command (PRD-027)
//!
//! Provides comprehensive diagnosis for specific tools including:
//! - Installation status and location
//! - Binary analysis (type, permissions, symlinks)
//! - Dependency verification
//! - Configuration examination
//! - Network connectivity tests
//! - Health checks
//! - Automated fix suggestions
//!
//! ## Usage
//!
//! ```bash
//! jarvy diagnose docker          # Diagnose docker installation
//! jarvy diagnose node --fix      # Diagnose and attempt fixes
//! jarvy diagnose git --export    # Export diagnostic bundle
//! ```

use crate::observability::Sanitizer;
use crate::tools::registry::get_tool;
use crate::tools::spec::{ToolSpec, get_tool_spec};
use serde::Serialize;
use std::path::PathBuf;
use std::process::Command;

/// Diagnostic report for a tool
#[derive(Debug, Serialize)]
pub struct DiagnosticReport {
    /// Tool name
    pub tool: String,
    /// Installation status
    pub installation: InstallationStatus,
    /// Binary analysis
    pub binary: Option<BinaryAnalysis>,
    /// Dependencies
    pub dependencies: Vec<DependencyStatus>,
    /// Configuration files found
    pub config_files: Vec<ConfigFile>,
    /// Health check results
    pub health_checks: Vec<HealthCheck>,
    /// Issues found
    pub issues: Vec<Issue>,
    /// Suggested fixes
    pub fixes: Vec<Fix>,
}

/// Installation status
#[derive(Debug, Serialize)]
pub struct InstallationStatus {
    /// Whether the tool is installed
    pub installed: bool,
    /// Version if installed
    pub version: Option<String>,
    /// Install location
    pub location: Option<String>,
    /// Install method (homebrew, apt, manual, etc.)
    pub method: Option<String>,
}

/// Binary analysis
#[derive(Debug, Serialize)]
pub struct BinaryAnalysis {
    /// File type (e.g., "Mach-O 64-bit executable arm64")
    pub file_type: String,
    /// Permissions (e.g., "-rwxr-xr-x")
    pub permissions: String,
    /// Owner
    pub owner: String,
    /// Symlink target if applicable
    pub symlink_target: Option<String>,
    /// Size in bytes
    pub size: u64,
}

/// Dependency status
#[derive(Debug, Serialize)]
pub struct DependencyStatus {
    /// Dependency name
    pub name: String,
    /// Whether it's available
    pub available: bool,
    /// Details
    pub details: Option<String>,
}

/// Configuration file
#[derive(Debug, Serialize)]
pub struct ConfigFile {
    /// File path
    pub path: String,
    /// Whether it exists
    pub exists: bool,
    /// File size if exists
    pub size: Option<u64>,
}

/// Health check result
#[derive(Debug, Serialize)]
pub struct HealthCheck {
    /// Check name
    pub name: String,
    /// Whether it passed
    pub passed: bool,
    /// Details or error message
    pub details: Option<String>,
}

/// Issue found during diagnosis
#[derive(Debug, Serialize)]
pub struct Issue {
    /// Issue severity
    pub severity: IssueSeverity,
    /// Issue description
    pub description: String,
    /// Suggested fix ID
    pub fix_id: Option<String>,
}

/// Issue severity levels
#[derive(Debug, Clone, Copy, Serialize, PartialEq, Eq)]
#[serde(rename_all = "lowercase")]
pub enum IssueSeverity {
    Error,
    Warning,
    #[allow(dead_code)] // Reserved for informational diagnostics
    Info,
}

/// Suggested fix
#[derive(Debug, Serialize)]
pub struct Fix {
    /// Fix identifier
    pub id: String,
    /// Description of the fix
    pub description: String,
    /// Command to run
    pub command: Option<String>,
    /// Whether it can be auto-applied
    pub auto_applicable: bool,
}

/// Run the diagnose command
pub fn run_diagnose(tool: &str, fix: bool, export: bool, _scope: &str, output_format: &str) -> i32 {
    // Check if tool exists in registry - spec is required for diagnosis
    let tool_spec = match get_tool_spec(tool) {
        Some(spec) => spec,
        None => {
            // Tool not found in spec registry
            if get_tool(tool).is_some() {
                eprintln!("Tool '{}' is registered but has no diagnostic spec.", tool);
                eprintln!("Only tools with full spec definitions can be diagnosed.");
            } else {
                eprintln!(
                    "Unknown tool: '{}'. Run 'jarvy tools' to see available tools.",
                    tool
                );
            }
            return 1;
        }
    };

    println!("Diagnosing: {}", tool);
    println!("{}", "=".repeat(50));
    println!();

    // Generate diagnostic report
    let report = diagnose_tool(tool, tool_spec);

    // Output report
    if output_format == "json" {
        match serde_json::to_string_pretty(&report) {
            Ok(json) => println!("{}", json),
            Err(e) => eprintln!("Failed to serialize report: {}", e),
        }
    } else {
        print_diagnostic_report(&report);
    }

    // Handle export
    if export {
        let filename = format!("jarvy-diagnose-{}-{}.json", tool, chrono_timestamp());
        let sanitizer = Sanitizer::new();
        let json = serde_json::to_string_pretty(&report).unwrap_or_default();
        let sanitized = sanitizer.sanitize(&json);

        match std::fs::write(&filename, sanitized) {
            Ok(_) => println!("\nDiagnostic export saved to: {}", filename),
            Err(e) => eprintln!("\nFailed to export: {}", e),
        }
    }

    // Handle fix
    if fix && !report.fixes.is_empty() {
        println!("\nApplying fixes...");
        for fix_item in &report.fixes {
            if fix_item.auto_applicable {
                if let Some(ref cmd) = fix_item.command {
                    println!("  Running: {}", cmd);
                    // Would run the fix here in a real implementation
                    println!("    (Fix application not yet implemented)");
                }
            } else {
                println!("  Manual fix required: {}", fix_item.description);
            }
        }
    }

    0
}

/// Generate diagnostic report for a tool
fn diagnose_tool(tool_name: &str, spec: &ToolSpec) -> DiagnosticReport {
    let mut issues = Vec::new();
    let mut fixes = Vec::new();

    // Check installation status
    let installation = check_installation(tool_name, spec);

    // Binary analysis (if installed)
    let binary = if installation.installed {
        installation
            .location
            .as_ref()
            .and_then(|loc| analyze_binary(loc).ok())
    } else {
        issues.push(Issue {
            severity: IssueSeverity::Error,
            description: format!("{} is not installed", tool_name),
            fix_id: Some("install".to_string()),
        });
        fixes.push(Fix {
            id: "install".to_string(),
            description: format!("Install {} using Jarvy", tool_name),
            command: Some(format!("jarvy setup --only {}", tool_name)),
            auto_applicable: false,
        });
        None
    };

    // Check dependencies
    let dependencies = check_dependencies(tool_name, spec);

    // Find configuration files
    let config_files = find_config_files(tool_name);

    // Run health checks
    let health_checks = run_health_checks(tool_name, spec, &installation);

    // Check for PATH issues
    if installation.installed {
        if let Some(ref loc) = installation.location {
            let path_issues = check_path_issues(loc);
            issues.extend(path_issues);
        }
    }

    // Add fixes for health check failures
    for check in &health_checks {
        if !check.passed {
            issues.push(Issue {
                severity: IssueSeverity::Warning,
                description: format!(
                    "Health check '{}' failed: {}",
                    check.name,
                    check.details.as_deref().unwrap_or("unknown")
                ),
                fix_id: None,
            });
        }
    }

    DiagnosticReport {
        tool: tool_name.to_string(),
        installation,
        binary,
        dependencies,
        config_files,
        health_checks,
        issues,
        fixes,
    }
}

/// Check installation status
fn check_installation(_tool_name: &str, spec: &ToolSpec) -> InstallationStatus {
    let command = spec.command;

    // Try to find the binary
    let which_output = Command::new("which").arg(command).output();

    let location = which_output.ok().and_then(|o| {
        if o.status.success() {
            String::from_utf8(o.stdout)
                .ok()
                .map(|s| s.trim().to_string())
        } else {
            None
        }
    });

    let installed = location.is_some();

    // Get version (use standard --version flag)
    let version = if installed {
        get_tool_version(command, "--version")
    } else {
        None
    };

    // Detect install method
    let method = if installed {
        detect_install_method(location.as_deref())
    } else {
        None
    };

    InstallationStatus {
        installed,
        version,
        location,
        method,
    }
}

/// Get tool version
fn get_tool_version(command: &str, version_arg: &str) -> Option<String> {
    let output = Command::new(command).arg(version_arg).output().ok()?;

    if output.status.success() {
        let stdout = String::from_utf8_lossy(&output.stdout);
        let stderr = String::from_utf8_lossy(&output.stderr);
        let combined = format!("{}{}", stdout, stderr);

        // Extract version number using regex
        let re = regex::Regex::new(r"(\d+\.\d+(?:\.\d+)?(?:-[\w.]+)?)").ok()?;
        re.captures(&combined)
            .and_then(|c| c.get(1))
            .map(|m| m.as_str().to_string())
    } else {
        None
    }
}

/// Detect how a tool was installed
fn detect_install_method(location: Option<&str>) -> Option<String> {
    let loc = location?;

    if loc.contains("/homebrew/")
        || loc.contains("/opt/homebrew/")
        || loc.contains("/usr/local/Cellar/")
    {
        Some("homebrew".to_string())
    } else if loc.contains("/.cargo/") {
        Some("cargo".to_string())
    } else if loc.contains("/.nvm/") {
        Some("nvm".to_string())
    } else if loc.contains("/.pyenv/") {
        Some("pyenv".to_string())
    } else if loc.contains("/.rustup/") {
        Some("rustup".to_string())
    } else if loc.contains("/snap/") {
        Some("snap".to_string())
    } else if loc.starts_with("/usr/bin/") || loc.starts_with("/bin/") {
        Some("system".to_string())
    } else {
        Some("manual".to_string())
    }
}

/// Analyze a binary file (Unix). Reads POSIX mode/uid/gid from filesystem
/// metadata, which is unavailable on Windows.
#[cfg(unix)]
fn analyze_binary(path: &str) -> Result<BinaryAnalysis, std::io::Error> {
    use std::os::unix::fs::MetadataExt;

    let metadata = std::fs::metadata(path)?;
    let symlink_meta = std::fs::symlink_metadata(path)?;

    // Get file type using `file` command
    let file_type = Command::new("file")
        .arg("-b")
        .arg(path)
        .output()
        .ok()
        .and_then(|o| String::from_utf8(o.stdout).ok())
        .map(|s| s.trim().to_string())
        .unwrap_or_else(|| "unknown".to_string());

    // Get permissions
    let mode = metadata.mode();
    let permissions = format_permissions(mode);

    // Get owner
    let owner = format!("{}:{}", metadata.uid(), metadata.gid());

    // Check if symlink
    let symlink_target = if symlink_meta.file_type().is_symlink() {
        std::fs::read_link(path)
            .ok()
            .map(|p| p.to_string_lossy().to_string())
    } else {
        None
    };

    Ok(BinaryAnalysis {
        file_type,
        permissions,
        owner,
        symlink_target,
        size: metadata.len(),
    })
}

// Windows stub: POSIX mode/uid/gid don't exist on Windows. The single
// caller in this file uses `.and_then(|loc| analyze_binary(loc).ok())`,
// so an Unsupported error degrades gracefully to `None` on Windows.
#[cfg(not(unix))]
fn analyze_binary(_path: &str) -> Result<BinaryAnalysis, std::io::Error> {
    Err(std::io::Error::new(
        std::io::ErrorKind::Unsupported,
        "binary analysis is unix-only",
    ))
}

/// Format Unix permissions
fn format_permissions(mode: u32) -> String {
    let user = (mode >> 6) & 0o7;
    let group = (mode >> 3) & 0o7;
    let other = mode & 0o7;

    let format_triplet = |bits: u32| -> String {
        format!(
            "{}{}{}",
            if bits & 4 != 0 { 'r' } else { '-' },
            if bits & 2 != 0 { 'w' } else { '-' },
            if bits & 1 != 0 { 'x' } else { '-' }
        )
    };

    format!(
        "-{}{}{}",
        format_triplet(user),
        format_triplet(group),
        format_triplet(other)
    )
}

/// Check dependencies for a tool
fn check_dependencies(tool_name: &str, _spec: &ToolSpec) -> Vec<DependencyStatus> {
    let mut deps = Vec::new();

    // Tool-specific dependency checks
    match tool_name {
        "docker" => {
            // Check Docker daemon
            let daemon_running = Command::new("docker")
                .arg("info")
                .output()
                .map(|o| o.status.success())
                .unwrap_or(false);

            deps.push(DependencyStatus {
                name: "Docker daemon".to_string(),
                available: daemon_running,
                details: if daemon_running {
                    Some("Running".to_string())
                } else {
                    Some("Not running or not accessible".to_string())
                },
            });

            // Check Docker socket
            let socket_exists = std::path::Path::new("/var/run/docker.sock").exists();
            deps.push(DependencyStatus {
                name: "Docker socket".to_string(),
                available: socket_exists,
                details: Some("/var/run/docker.sock".to_string()),
            });
        }
        "node" | "npm" => {
            // Check npm
            let npm_available = Command::new("npm")
                .arg("--version")
                .output()
                .map(|o| o.status.success())
                .unwrap_or(false);

            deps.push(DependencyStatus {
                name: "npm".to_string(),
                available: npm_available,
                details: None,
            });
        }
        "rust" | "cargo" => {
            // Check rustup
            let rustup_available = Command::new("rustup")
                .arg("--version")
                .output()
                .map(|o| o.status.success())
                .unwrap_or(false);

            deps.push(DependencyStatus {
                name: "rustup".to_string(),
                available: rustup_available,
                details: None,
            });
        }
        _ => {}
    }

    deps
}

/// Find configuration files for a tool
fn find_config_files(tool_name: &str) -> Vec<ConfigFile> {
    let home = dirs::home_dir().unwrap_or_else(|| PathBuf::from("/"));
    let mut configs = Vec::new();

    // Tool-specific config files
    let paths: Vec<PathBuf> = match tool_name {
        "docker" => vec![
            home.join(".docker/config.json"),
            home.join(".docker/daemon.json"),
        ],
        "git" => vec![home.join(".gitconfig"), home.join(".gitignore_global")],
        "node" | "npm" => vec![home.join(".npmrc"), home.join(".nvmrc")],
        "rust" | "cargo" => vec![
            home.join(".cargo/config.toml"),
            home.join(".cargo/config"),
            home.join(".rustup/settings.toml"),
        ],
        "kubectl" | "kubernetes" => vec![home.join(".kube/config")],
        _ => vec![],
    };

    for path in paths {
        let exists = path.exists();
        let size = if exists {
            std::fs::metadata(&path).ok().map(|m| m.len())
        } else {
            None
        };

        configs.push(ConfigFile {
            path: path.to_string_lossy().to_string(),
            exists,
            size,
        });
    }

    configs
}

/// Run health checks for a tool
fn run_health_checks(
    tool_name: &str,
    spec: &ToolSpec,
    installation: &InstallationStatus,
) -> Vec<HealthCheck> {
    let mut checks = Vec::new();

    if !installation.installed {
        return checks;
    }

    // Basic version check
    checks.push(HealthCheck {
        name: format!("{} --version", spec.command),
        passed: installation.version.is_some(),
        details: installation.version.clone(),
    });

    // Tool-specific health checks
    match tool_name {
        "docker" => {
            // Check docker ps
            let ps_ok = Command::new("docker")
                .args(["ps", "--format", "{{.ID}}"])
                .output()
                .map(|o| o.status.success())
                .unwrap_or(false);

            checks.push(HealthCheck {
                name: "docker ps".to_string(),
                passed: ps_ok,
                details: if ps_ok {
                    None
                } else {
                    Some("Cannot list containers".to_string())
                },
            });
        }
        "git" => {
            // Check git config
            let config_ok = Command::new("git")
                .args(["config", "--get", "user.name"])
                .output()
                .map(|o| o.status.success())
                .unwrap_or(false);

            checks.push(HealthCheck {
                name: "git config user.name".to_string(),
                passed: config_ok,
                details: if config_ok {
                    None
                } else {
                    Some("User name not configured".to_string())
                },
            });
        }
        "node" => {
            // Check node can execute
            let exec_ok = Command::new("node")
                .args(["-e", "console.log('ok')"])
                .output()
                .map(|o| o.status.success())
                .unwrap_or(false);

            checks.push(HealthCheck {
                name: "node execution".to_string(),
                passed: exec_ok,
                details: if exec_ok {
                    None
                } else {
                    Some("Cannot execute Node.js".to_string())
                },
            });
        }
        _ => {}
    }

    checks
}

/// Check for PATH issues
fn check_path_issues(binary_location: &str) -> Vec<Issue> {
    let mut issues = Vec::new();

    // Get directory of binary
    let binary_dir = std::path::Path::new(binary_location)
        .parent()
        .map(|p| p.to_string_lossy().to_string());

    if let Some(dir) = binary_dir {
        // Check if directory is in PATH
        if let Ok(path) = std::env::var("PATH") {
            if !path.split(':').any(|p| p == dir) {
                issues.push(Issue {
                    severity: IssueSeverity::Warning,
                    description: format!("Binary directory '{}' may not be in PATH", dir),
                    fix_id: Some("add-to-path".to_string()),
                });
            }
        }
    }

    issues
}

/// Print diagnostic report in pretty format
fn print_diagnostic_report(report: &DiagnosticReport) {
    // Installation Status
    println!("Installation Status");
    println!("{}", "-".repeat(40));
    println!(
        "Installed: {}",
        if report.installation.installed {
            "Yes"
        } else {
            "No"
        }
    );
    if let Some(ref version) = report.installation.version {
        println!("Version:   {}", version);
    }
    if let Some(ref location) = report.installation.location {
        println!("Location:  {}", location);
    }
    if let Some(ref method) = report.installation.method {
        println!("Method:    {}", method);
    }
    println!();

    // Binary Analysis
    if let Some(ref binary) = report.binary {
        println!("Binary Analysis");
        println!("{}", "-".repeat(40));
        println!("File type:   {}", binary.file_type);
        println!("Permissions: {}", binary.permissions);
        println!("Owner:       {}", binary.owner);
        if let Some(ref target) = binary.symlink_target {
            println!("Symlink:     -> {}", target);
        }
        println!("Size:        {} bytes", binary.size);
        println!();
    }

    // Dependencies
    if !report.dependencies.is_empty() {
        println!("Dependencies");
        println!("{}", "-".repeat(40));
        for dep in &report.dependencies {
            let status = if dep.available {
                "\x1b[32m[OK]\x1b[0m"
            } else {
                "\x1b[31m[MISSING]\x1b[0m"
            };
            print!("{} {}", status, dep.name);
            if let Some(ref details) = dep.details {
                print!(" ({})", details);
            }
            println!();
        }
        println!();
    }

    // Configuration Files
    if !report.config_files.is_empty() {
        println!("Configuration");
        println!("{}", "-".repeat(40));
        for config in &report.config_files {
            let status = if config.exists {
                "\x1b[32m[EXISTS]\x1b[0m"
            } else {
                "\x1b[33m[MISSING]\x1b[0m"
            };
            print!("{} {}", status, config.path);
            if let Some(size) = config.size {
                print!(" ({} bytes)", size);
            }
            println!();
        }
        println!();
    }

    // Health Checks
    if !report.health_checks.is_empty() {
        println!("Health Checks");
        println!("{}", "-".repeat(40));
        for check in &report.health_checks {
            let status = if check.passed {
                "\x1b[32m[PASS]\x1b[0m"
            } else {
                "\x1b[31m[FAIL]\x1b[0m"
            };
            print!("{} {}", status, check.name);
            if let Some(ref details) = check.details {
                print!(" - {}", details);
            }
            println!();
        }
        println!();
    }

    // Issues
    if !report.issues.is_empty() {
        println!("Issues Found");
        println!("{}", "-".repeat(40));
        for issue in &report.issues {
            let icon = match issue.severity {
                IssueSeverity::Error => "\x1b[31m[ERROR]\x1b[0m",
                IssueSeverity::Warning => "\x1b[33m[WARN]\x1b[0m",
                IssueSeverity::Info => "\x1b[34m[INFO]\x1b[0m",
            };
            println!("{} {}", icon, issue.description);
        }
        println!();
    }

    // Fixes
    if !report.fixes.is_empty() {
        println!("Suggested Fixes");
        println!("{}", "-".repeat(40));
        for (i, fix) in report.fixes.iter().enumerate() {
            println!("{}. {}", i + 1, fix.description);
            if let Some(ref cmd) = fix.command {
                println!("   Command: {}", cmd);
            }
        }
        println!();
    }

    // Summary
    let error_count = report
        .issues
        .iter()
        .filter(|i| i.severity == IssueSeverity::Error)
        .count();
    let warning_count = report
        .issues
        .iter()
        .filter(|i| i.severity == IssueSeverity::Warning)
        .count();

    if error_count == 0 && warning_count == 0 {
        println!(
            "\x1b[32mNo issues detected. {} is healthy.\x1b[0m",
            report.tool
        );
    } else {
        println!(
            "Summary: {} error(s), {} warning(s)",
            error_count, warning_count
        );
    }
}

/// Generate a timestamp for filenames
fn chrono_timestamp() -> String {
    use std::time::{SystemTime, UNIX_EPOCH};

    let duration = SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .unwrap_or_default();

    let secs = duration.as_secs();
    format!("{}", secs)
}

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

    #[test]
    fn test_format_permissions() {
        assert_eq!(format_permissions(0o755), "-rwxr-xr-x");
        assert_eq!(format_permissions(0o644), "-rw-r--r--");
        assert_eq!(format_permissions(0o700), "-rwx------");
    }

    #[test]
    fn test_detect_install_method() {
        assert_eq!(
            detect_install_method(Some("/opt/homebrew/bin/git")),
            Some("homebrew".to_string())
        );
        assert_eq!(
            detect_install_method(Some("/Users/test/.cargo/bin/rustc")),
            Some("cargo".to_string())
        );
        assert_eq!(
            detect_install_method(Some("/usr/bin/ls")),
            Some("system".to_string())
        );
    }

    #[test]
    fn test_issue_severity_serialization() {
        let issue = Issue {
            severity: IssueSeverity::Error,
            description: "Test".to_string(),
            fix_id: None,
        };
        let json = serde_json::to_string(&issue).unwrap();
        assert!(json.contains("\"severity\":\"error\""));
    }
}