kopi 0.1.1

Kopi is a JDK version management tool
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
// Copyright 2025 dentsusoken
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use crate::config::KopiConfig;
use crate::doctor::{CheckCategory, CheckResult, CheckStatus, DiagnosticCheck};
use crate::platform::with_executable_extension;
use crate::storage::formatting::format_size;
use crate::storage::{InstalledJdk, JdkLister};
use std::time::Instant;

/// Check if any JDKs are installed
pub struct JdkInstallationCheck<'a> {
    config: &'a KopiConfig,
}

impl<'a> JdkInstallationCheck<'a> {
    pub fn new(config: &'a KopiConfig) -> Self {
        Self { config }
    }
}

impl<'a> DiagnosticCheck for JdkInstallationCheck<'a> {
    fn name(&self) -> &str {
        "JDK Installation Enumeration"
    }

    fn run(&self, start: Instant, category: CheckCategory) -> CheckResult {
        let jdks_dir = match self.config.jdks_dir() {
            Ok(dir) => dir,
            Err(e) => {
                return CheckResult::new(
                    self.name(),
                    category,
                    CheckStatus::Fail,
                    format!("Cannot determine JDKs directory: {e}"),
                    start.elapsed(),
                )
                .with_suggestion("Ensure KOPI_HOME is set correctly or use default ~/.kopi");
            }
        };

        match JdkLister::list_installed_jdks(&jdks_dir) {
            Ok(jdks) => {
                if jdks.is_empty() {
                    CheckResult::new(
                        self.name(),
                        category,
                        CheckStatus::Warning,
                        "No JDKs installed",
                        start.elapsed(),
                    )
                    .with_suggestion("Install a JDK with: kopi install <version>")
                } else {
                    let summary = format!(
                        "{} JDK{} installed",
                        jdks.len(),
                        if jdks.len() == 1 { "" } else { "s" }
                    );
                    let details = jdks
                        .iter()
                        .map(|jdk| format!("  - {}-{}", jdk.distribution, jdk.version))
                        .collect::<Vec<_>>()
                        .join("\n");

                    CheckResult::new(
                        self.name(),
                        category,
                        CheckStatus::Pass,
                        summary,
                        start.elapsed(),
                    )
                    .with_details(details)
                }
            }
            Err(e) => CheckResult::new(
                self.name(),
                category,
                CheckStatus::Fail,
                format!("Failed to list installed JDKs: {e}"),
                start.elapsed(),
            ),
        }
    }
}

/// Check JDK installation integrity
pub struct JdkIntegrityCheck<'a> {
    config: &'a KopiConfig,
}

impl<'a> JdkIntegrityCheck<'a> {
    pub fn new(config: &'a KopiConfig) -> Self {
        Self { config }
    }

    fn check_jdk_structure(jdk: &InstalledJdk) -> Result<(bool, Vec<String>), std::io::Error> {
        let mut issues = Vec::new();
        let bin_dir = jdk.path.join("bin");

        // Check if bin directory exists
        if !bin_dir.exists() {
            issues.push("Missing bin directory".to_string());
            return Ok((false, issues));
        }

        // List of required executables
        let required_executables = vec!["java", "javac"];
        let optional_executables = vec!["jar", "javadoc", "jlink", "jmod"];

        // Check required executables
        for exe in required_executables {
            let exe_name = with_executable_extension(exe);
            let exe_path = bin_dir.join(&exe_name);

            if !exe_path.exists() {
                issues.push(format!("Missing required executable: {exe_name}"));
                continue;
            }

            // Check if executable permissions are correct
            match crate::platform::file_ops::is_executable(&exe_path) {
                Ok(is_exec) => {
                    if !is_exec {
                        issues.push(format!("{exe_name} is not executable"));
                    }
                }
                Err(e) => {
                    issues.push(format!("Cannot check {exe_name} permissions: {e}"));
                }
            }
        }

        // Check optional executables (just note if missing, don't fail)
        let mut missing_optional = Vec::new();
        for exe in optional_executables {
            let exe_name = with_executable_extension(exe);
            let exe_path = bin_dir.join(&exe_name);
            if !exe_path.exists() {
                missing_optional.push(exe);
            }
        }

        if !missing_optional.is_empty() {
            log::debug!(
                "JDK {} missing optional executables: {}",
                jdk.path.display(),
                missing_optional.join(", ")
            );
        }

        Ok((issues.is_empty(), issues))
    }
}

impl<'a> DiagnosticCheck for JdkIntegrityCheck<'a> {
    fn name(&self) -> &str {
        "JDK Installation Integrity"
    }

    fn run(&self, start: Instant, category: CheckCategory) -> CheckResult {
        let jdks_dir = match self.config.jdks_dir() {
            Ok(dir) => dir,
            Err(_) => {
                return CheckResult::new(
                    self.name(),
                    category,
                    CheckStatus::Skip,
                    "Cannot check JDK integrity - JDKs directory not accessible",
                    start.elapsed(),
                );
            }
        };

        let jdks = match JdkLister::list_installed_jdks(&jdks_dir) {
            Ok(jdks) => jdks,
            Err(_) => {
                return CheckResult::new(
                    self.name(),
                    category,
                    CheckStatus::Skip,
                    "Cannot check JDK integrity - failed to list JDKs",
                    start.elapsed(),
                );
            }
        };

        if jdks.is_empty() {
            return CheckResult::new(
                self.name(),
                category,
                CheckStatus::Skip,
                "No JDKs installed to check",
                start.elapsed(),
            );
        }

        let mut all_issues = Vec::new();
        let mut corrupted_count = 0;

        for jdk in &jdks {
            match Self::check_jdk_structure(jdk) {
                Ok((is_valid, issues)) => {
                    if !is_valid {
                        corrupted_count += 1;
                        all_issues.push(format!(
                            "{}-{}:\n{}",
                            jdk.distribution,
                            jdk.version,
                            issues
                                .iter()
                                .map(|i| format!("    - {i}"))
                                .collect::<Vec<_>>()
                                .join("\n")
                        ));
                    }
                }
                Err(e) => {
                    corrupted_count += 1;
                    all_issues.push(format!(
                        "{}-{}: Failed to check structure: {e}",
                        jdk.distribution, jdk.version
                    ));
                }
            }
        }

        if corrupted_count == 0 {
            CheckResult::new(
                self.name(),
                category,
                CheckStatus::Pass,
                format!("All {} JDK installations are intact", jdks.len()),
                start.elapsed(),
            )
        } else {
            let message = format!(
                "{} of {} JDK installations have issues",
                corrupted_count,
                jdks.len()
            );
            CheckResult::new(
                self.name(),
                category,
                CheckStatus::Fail,
                message,
                start.elapsed(),
            )
            .with_details(all_issues.join("\n\n"))
            .with_suggestion("Reinstall corrupted JDKs with: kopi install <distribution>@<version>")
        }
    }
}

/// Check available disk space for JDK installations
pub struct JdkDiskSpaceCheck<'a> {
    config: &'a KopiConfig,
}

impl<'a> JdkDiskSpaceCheck<'a> {
    pub fn new(config: &'a KopiConfig) -> Self {
        Self { config }
    }
}

impl<'a> DiagnosticCheck for JdkDiskSpaceCheck<'a> {
    fn name(&self) -> &str {
        "JDK Disk Space Analysis"
    }

    fn run(&self, start: Instant, category: CheckCategory) -> CheckResult {
        let jdks_dir = match self.config.jdks_dir() {
            Ok(dir) => dir,
            Err(_) => {
                return CheckResult::new(
                    self.name(),
                    category,
                    CheckStatus::Skip,
                    "Cannot analyze disk space - JDKs directory not accessible",
                    start.elapsed(),
                );
            }
        };

        let jdks = match JdkLister::list_installed_jdks(&jdks_dir) {
            Ok(jdks) => jdks,
            Err(_) => {
                return CheckResult::new(
                    self.name(),
                    category,
                    CheckStatus::Skip,
                    "Cannot analyze disk space - failed to list JDKs",
                    start.elapsed(),
                );
            }
        };

        if jdks.is_empty() {
            return CheckResult::new(
                self.name(),
                category,
                CheckStatus::Skip,
                "No JDKs installed",
                start.elapsed(),
            );
        }

        // Calculate total size of all JDKs
        let mut total_size = 0u64;
        let mut jdk_sizes = Vec::new();

        for jdk in &jdks {
            match JdkLister::get_jdk_size(&jdk.path) {
                Ok(size) => {
                    total_size += size;
                    jdk_sizes.push((jdk, size));
                }
                Err(e) => {
                    log::warn!(
                        "Failed to calculate size for {}-{}: {}",
                        jdk.distribution,
                        jdk.version,
                        e
                    );
                }
            }
        }

        // Check available disk space
        let available_space = match fs2::available_space(&jdks_dir) {
            Ok(space) => space,
            Err(e) => {
                return CheckResult::new(
                    self.name(),
                    category,
                    CheckStatus::Warning,
                    format!("Cannot check available disk space: {e}"),
                    start.elapsed(),
                );
            }
        };

        // Sort JDKs by size (largest first) for details
        jdk_sizes.sort_by(|a, b| b.1.cmp(&a.1));

        let details = jdk_sizes
            .iter()
            .map(|(jdk, size)| {
                format!(
                    "  - {}-{}: {}",
                    jdk.distribution,
                    jdk.version,
                    format_size(*size)
                )
            })
            .collect::<Vec<_>>()
            .join("\n");

        let total_size_str = format_size(total_size);
        let available_str = format_size(available_space);

        // Warn if less than 1GB available
        let status = if available_space < 1024 * 1024 * 1024 {
            CheckStatus::Warning
        } else {
            CheckStatus::Pass
        };

        let message = format!("JDKs using {total_size_str}, {available_str} available");

        let mut result = CheckResult::new(self.name(), category, status, message, start.elapsed())
            .with_details(format!("JDK sizes:\n{details}"));

        if status == CheckStatus::Warning {
            result = result.with_suggestion(
                "Low disk space. Consider removing unused JDKs with: kopi uninstall <version>",
            );
        }

        result
    }
}

/// Check JDK version consistency between directory name and actual version
pub struct JdkVersionConsistencyCheck<'a> {
    config: &'a KopiConfig,
}

impl<'a> JdkVersionConsistencyCheck<'a> {
    pub fn new(config: &'a KopiConfig) -> Self {
        Self { config }
    }

    fn check_java_version(jdk: &InstalledJdk) -> Result<(bool, String), std::io::Error> {
        let java_path = jdk.path.join("bin").join(with_executable_extension("java"));

        if !java_path.exists() {
            return Ok((false, "Java executable not found".to_string()));
        }

        // Run java -version to get actual version
        let output = std::process::Command::new(&java_path)
            .arg("-version")
            .output()?;

        // Java outputs version info to stderr
        let version_output = String::from_utf8_lossy(&output.stderr);

        // Extract version from output (usually in format: openjdk version "21.0.1" or java version "17.0.9")
        let version_line = version_output
            .lines()
            .find(|line| line.contains("version"))
            .unwrap_or("");

        Ok((true, version_line.to_string()))
    }
}

impl<'a> DiagnosticCheck for JdkVersionConsistencyCheck<'a> {
    fn name(&self) -> &str {
        "JDK Version Consistency"
    }

    fn run(&self, start: Instant, category: CheckCategory) -> CheckResult {
        let jdks_dir = match self.config.jdks_dir() {
            Ok(dir) => dir,
            Err(_) => {
                return CheckResult::new(
                    self.name(),
                    category,
                    CheckStatus::Skip,
                    "Cannot check version consistency - JDKs directory not accessible",
                    start.elapsed(),
                );
            }
        };

        let jdks = match JdkLister::list_installed_jdks(&jdks_dir) {
            Ok(jdks) => jdks,
            Err(_) => {
                return CheckResult::new(
                    self.name(),
                    category,
                    CheckStatus::Skip,
                    "Cannot check version consistency - failed to list JDKs",
                    start.elapsed(),
                );
            }
        };

        if jdks.is_empty() {
            return CheckResult::new(
                self.name(),
                category,
                CheckStatus::Skip,
                "No JDKs installed to check",
                start.elapsed(),
            );
        }

        let mut inconsistencies = Vec::new();

        for jdk in &jdks {
            match Self::check_java_version(jdk) {
                Ok((found, version_output)) => {
                    if found {
                        // Simple check: see if the directory version appears in the output
                        let dir_version = jdk.version.to_string();
                        if !version_output.contains(&dir_version) {
                            inconsistencies.push(format!(
                                "{}-{}: {}",
                                jdk.distribution,
                                jdk.version,
                                version_output.trim()
                            ));
                        }
                    } else {
                        inconsistencies.push(format!(
                            "{}-{}: Java executable not found",
                            jdk.distribution, jdk.version
                        ));
                    }
                }
                Err(e) => {
                    log::debug!(
                        "Failed to check version for {}-{}: {}",
                        jdk.distribution,
                        jdk.version,
                        e
                    );
                }
            }
        }

        if inconsistencies.is_empty() {
            CheckResult::new(
                self.name(),
                category,
                CheckStatus::Pass,
                "All JDK versions are consistent",
                start.elapsed(),
            )
        } else {
            CheckResult::new(
                self.name(),
                category,
                CheckStatus::Warning,
                format!(
                    "{} JDK{} may have version inconsistencies",
                    inconsistencies.len(),
                    if inconsistencies.len() == 1 { "" } else { "s" }
                ),
                start.elapsed(),
            )
            .with_details(inconsistencies.join("\n"))
            .with_suggestion("Version mismatch may indicate corrupted installations")
        }
    }
}

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

    struct TestSetup {
        config: KopiConfig,
        _temp_dir: TempDir,
    }

    impl TestSetup {
        fn new() -> Self {
            let temp_dir = TempDir::new().unwrap();
            let config = KopiConfig::new(temp_dir.path().to_path_buf()).unwrap();

            // Create jdks directory
            fs::create_dir_all(temp_dir.path().join("jdks")).unwrap();

            TestSetup {
                config,
                _temp_dir: temp_dir,
            }
        }

        fn create_mock_jdk(&self, name: &str) {
            let jdk_path = self.config.jdks_dir().unwrap().join(name);
            let bin_dir = jdk_path.join("bin");
            fs::create_dir_all(&bin_dir).unwrap();

            // Create mock executables
            for exe in &["java", "javac"] {
                let exe_name = with_executable_extension(exe);
                let exe_path = bin_dir.join(exe_name);
                fs::write(&exe_path, "mock executable").unwrap();

                #[cfg(unix)]
                {
                    use std::os::unix::fs::PermissionsExt;
                    let mut perms = fs::metadata(&exe_path).unwrap().permissions();
                    perms.set_mode(0o755);
                    fs::set_permissions(&exe_path, perms).unwrap();
                }
            }
        }
    }

    #[test]
    fn test_jdk_installation_check_empty() {
        let setup = TestSetup::new();
        let check = JdkInstallationCheck::new(&setup.config);
        let result = check.run(Instant::now(), CheckCategory::Jdks);

        assert_eq!(result.status, CheckStatus::Warning);
        assert!(result.message.contains("No JDKs installed"));
        assert!(result.suggestion.is_some());
    }

    #[test]
    fn test_jdk_installation_check_with_jdks() {
        let setup = TestSetup::new();
        setup.create_mock_jdk("temurin-21.0.1");
        setup.create_mock_jdk("corretto-17.0.9");

        let check = JdkInstallationCheck::new(&setup.config);
        let result = check.run(Instant::now(), CheckCategory::Jdks);

        assert_eq!(result.status, CheckStatus::Pass);
        assert!(result.message.contains("2 JDKs installed"));
        assert!(result.details.is_some());
    }

    #[test]
    fn test_jdk_integrity_check() {
        let setup = TestSetup::new();

        // Create a valid JDK
        setup.create_mock_jdk("temurin-21.0.1");

        // Create a corrupted JDK (missing bin directory)
        let corrupted_path = setup.config.jdks_dir().unwrap().join("corretto-17.0.9");
        fs::create_dir_all(&corrupted_path).unwrap();

        let check = JdkIntegrityCheck::new(&setup.config);
        let result = check.run(Instant::now(), CheckCategory::Jdks);

        assert_eq!(result.status, CheckStatus::Fail);
        assert!(
            result
                .message
                .contains("1 of 2 JDK installations have issues")
        );
    }

    #[test]
    fn test_jdk_disk_space_check() {
        let setup = TestSetup::new();
        setup.create_mock_jdk("temurin-21.0.1");

        let check = JdkDiskSpaceCheck::new(&setup.config);
        let result = check.run(Instant::now(), CheckCategory::Jdks);

        // Should at least run without errors
        assert!(
            result.status == CheckStatus::Pass || result.status == CheckStatus::Warning,
            "Unexpected status: {:?}",
            result.status
        );
        assert!(result.message.contains("JDKs using"));
        assert!(result.message.contains("available"));
    }
}