kopi 0.0.9

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
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
// 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::error::{KopiError, Result};
use config::{Config, ConfigError, Environment, File};
use dirs::home_dir;
use log::warn;
use serde::{Deserialize, Serialize};
use std::fs;
use std::path::{Path, PathBuf};

const CONFIG_FILE_NAME: &str = "config.toml";
const DEFAULT_MIN_DISK_SPACE_MB: u64 = 500;

// Directory names
const JDKS_DIR_NAME: &str = "jdks";
const CACHE_DIR_NAME: &str = "cache";
const BIN_DIR_NAME: &str = "bin";
const SHIMS_DIR_NAME: &str = "shims";

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct KopiConfig {
    #[serde(skip)]
    kopi_home: PathBuf,

    #[serde(default)]
    pub storage: StorageConfig,

    #[serde(default = "default_distribution")]
    pub default_distribution: String,

    #[serde(default)]
    pub additional_distributions: Vec<String>,

    #[serde(default)]
    pub auto_install: AutoInstallConfig,

    #[serde(default)]
    pub shims: ShimsConfig,

    #[serde(default)]
    pub cache: CacheConfig,

    #[serde(default)]
    pub metadata: MetadataConfig,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MetadataConfig {
    #[serde(default)]
    pub cache: MetadataCacheConfig,

    #[serde(default = "default_metadata_sources")]
    pub sources: Vec<SourceConfig>,
}

impl Default for MetadataConfig {
    fn default() -> Self {
        Self {
            cache: MetadataCacheConfig::default(),
            sources: default_metadata_sources(),
        }
    }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MetadataCacheConfig {
    #[serde(default = "default_metadata_cache_max_age_hours")]
    pub max_age_hours: u64,

    #[serde(default = "default_true")]
    pub auto_refresh: bool,
}

impl Default for MetadataCacheConfig {
    fn default() -> Self {
        Self {
            max_age_hours: default_metadata_cache_max_age_hours(),
            auto_refresh: true,
        }
    }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "type")]
pub enum SourceConfig {
    #[serde(rename = "http")]
    Http {
        name: String,
        #[serde(default = "default_true")]
        enabled: bool,
        base_url: String,
        #[serde(default = "default_true")]
        cache_locally: bool,
        #[serde(default = "default_timeout_secs")]
        timeout_secs: u64,
    },
    #[serde(rename = "local")]
    Local {
        name: String,
        #[serde(default = "default_true")]
        enabled: bool,
        directory: String,
        #[serde(default = "default_archive_pattern")]
        archive_pattern: String,
        #[serde(default = "default_true")]
        cache_extracted: bool,
    },
    #[serde(rename = "foojay")]
    Foojay {
        name: String,
        #[serde(default = "default_false")]
        enabled: bool,
        base_url: String,
        #[serde(default = "default_timeout_secs")]
        timeout_secs: u64,
    },
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct StorageConfig {
    #[serde(default = "default_min_disk_space_mb")]
    pub min_disk_space_mb: u64,
}

impl Default for StorageConfig {
    fn default() -> Self {
        Self {
            min_disk_space_mb: DEFAULT_MIN_DISK_SPACE_MB,
        }
    }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AutoInstallConfig {
    #[serde(default = "default_true")]
    pub enabled: bool,

    #[serde(default = "default_true")]
    pub prompt: bool,

    #[serde(default = "default_timeout_secs")]
    pub timeout_secs: u64,
}

impl Default for AutoInstallConfig {
    fn default() -> Self {
        Self {
            enabled: true,
            prompt: true,
            timeout_secs: 300,
        }
    }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ShimsConfig {
    #[serde(default = "default_true")]
    pub auto_create_shims: bool,
    #[serde(default)]
    pub additional_tools: Vec<String>,
    #[serde(default)]
    pub exclude_tools: Vec<String>,
    #[serde(default = "default_false")]
    pub auto_install: bool,
    #[serde(default = "default_true")]
    pub auto_install_prompt: bool,
    #[serde(default = "default_shim_install_timeout")]
    pub install_timeout: u64,
}

impl Default for ShimsConfig {
    fn default() -> Self {
        Self {
            auto_create_shims: true,
            additional_tools: Vec::new(),
            exclude_tools: Vec::new(),
            auto_install: false,
            auto_install_prompt: true,
            install_timeout: 600,
        }
    }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CacheConfig {
    #[serde(default = "default_cache_max_age_hours")]
    pub max_age_hours: u64,

    #[serde(default = "default_true")]
    pub auto_refresh: bool,

    #[serde(default = "default_true")]
    pub refresh_on_miss: bool,
}

impl Default for CacheConfig {
    fn default() -> Self {
        Self {
            max_age_hours: 720, // 30 days
            auto_refresh: true,
            refresh_on_miss: true,
        }
    }
}

// Default value functions
fn default_true() -> bool {
    true
}

fn default_false() -> bool {
    false
}

fn default_timeout_secs() -> u64 {
    300
}

fn default_shim_install_timeout() -> u64 {
    600 // 10 minutes for shim-specific installations
}

fn default_min_disk_space_mb() -> u64 {
    DEFAULT_MIN_DISK_SPACE_MB
}

fn default_distribution() -> String {
    "temurin".to_string()
}

fn default_cache_max_age_hours() -> u64 {
    720 // 30 days
}

fn default_metadata_sources() -> Vec<SourceConfig> {
    vec![
        SourceConfig::Http {
            name: "primary-http".to_string(),
            enabled: true,
            base_url: default_http_base_url(),
            cache_locally: true,
            timeout_secs: 30,
        },
        SourceConfig::Foojay {
            name: "foojay-api".to_string(),
            enabled: true,
            base_url: default_foojay_base_url(),
            timeout_secs: 30,
        },
    ]
}

fn default_metadata_cache_max_age_hours() -> u64 {
    720 // 30 days
}

fn default_http_base_url() -> String {
    "https://kopi-vm.github.io/metadata".to_string()
}

fn default_archive_pattern() -> String {
    "*.tar.gz".to_string()
}

fn default_foojay_base_url() -> String {
    "https://api.foojay.io/disco".to_string()
}

/// Create a new KopiConfig with automatic home directory resolution
pub fn new_kopi_config() -> Result<KopiConfig> {
    let kopi_home = resolve_kopi_home()?;
    KopiConfig::new(kopi_home)
}

/// Resolve the KOPI home directory from environment variable or default location
fn resolve_kopi_home() -> Result<PathBuf> {
    // Check KOPI_HOME environment variable first
    if let Ok(kopi_home) = std::env::var("KOPI_HOME") {
        let path = PathBuf::from(&kopi_home);
        if path.is_absolute() {
            return Ok(path);
        } else {
            let default_path = home_dir().map(|home| home.join(".kopi")).ok_or_else(|| {
                KopiError::ConfigError("Unable to determine home directory".to_string())
            })?;
            warn!(
                "KOPI_HOME environment variable '{}' is not an absolute path. Ignoring and using \
                 default path: {}",
                kopi_home,
                default_path.display()
            );
            return Ok(default_path);
        }
    }

    // Fall back to ~/.kopi
    home_dir()
        .map(|home| home.join(".kopi"))
        .ok_or_else(|| KopiError::ConfigError("Unable to determine home directory".to_string()))
}

impl KopiConfig {
    /// Create a new KopiConfig from the specified home directory
    pub fn new(kopi_home: PathBuf) -> Result<Self> {
        let config_path = kopi_home.join(CONFIG_FILE_NAME);

        // Build the configuration using the config crate
        let mut builder = Config::builder()
            // Set defaults
            .set_default("storage.min_disk_space_mb", DEFAULT_MIN_DISK_SPACE_MB)?
            .set_default("default_distribution", "temurin")?
            .set_default("additional_distributions", Vec::<String>::new())?
            .set_default("auto_install.enabled", true)?
            .set_default("auto_install.prompt", true)?
            .set_default("auto_install.timeout_secs", 300)?
            .set_default("shims.auto_create_shims", true)?
            .set_default("shims.additional_tools", Vec::<String>::new())?
            .set_default("shims.exclude_tools", Vec::<String>::new())?
            .set_default("shims.auto_install", false)?
            .set_default("shims.auto_install_prompt", true)?
            .set_default("shims.install_timeout", 600)?
            .set_default("cache.max_age_hours", 720)?
            .set_default("cache.auto_refresh", true)?
            .set_default("cache.refresh_on_miss", true)?;

        // Add the config file if it exists
        if config_path.exists() {
            log::debug!("Loading config from {config_path:?}");
            builder = builder.add_source(File::from(config_path.clone()).required(false));
        } else {
            log::debug!("Config file not found at {config_path:?}, using defaults");
        }

        // Add environment variables with KOPI_ prefix
        // The config crate will automatically map environment variables to config fields
        // Double underscore (__) is used for nested fields to avoid ambiguity
        // For example: KOPI_AUTO_INSTALL__ENABLED -> auto_install.enabled
        builder = builder.add_source(
            Environment::with_prefix("KOPI")
                .prefix_separator("_")
                .separator("__")
                .try_parsing(true),
        );

        // Build and deserialize the configuration
        let settings = builder
            .build()
            .map_err(|e| KopiError::ConfigError(format!("Failed to build config: {e}")))?;

        let mut config: KopiConfig = settings
            .try_deserialize()
            .map_err(|e| KopiError::ConfigError(format!("Failed to deserialize config: {e}")))?;

        // Set the kopi_home path
        config.kopi_home = kopi_home;

        Ok(config)
    }

    pub fn save(&self) -> Result<()> {
        let config_path = self.kopi_home.join(CONFIG_FILE_NAME);

        // Ensure parent directory exists
        if let Some(parent) = config_path.parent() {
            fs::create_dir_all(parent)?;
        }

        let contents = toml::to_string_pretty(self)
            .map_err(|e| KopiError::ConfigError(format!("Failed to serialize config: {e}")))?;

        fs::write(&config_path, contents)?;
        log::debug!("Saved config to {config_path:?}");
        Ok(())
    }

    /// Get the KOPI home directory
    pub fn kopi_home(&self) -> &Path {
        &self.kopi_home
    }

    /// Get the JDKs directory path and create it if it doesn't exist
    pub fn jdks_dir(&self) -> Result<PathBuf> {
        let dir = self.kopi_home.join(JDKS_DIR_NAME);
        fs::create_dir_all(&dir)
            .map_err(|e| KopiError::ConfigError(format!("Failed to create jdks directory: {e}")))?;
        Ok(dir)
    }

    /// Get the cache directory path and create it if it doesn't exist
    pub fn cache_dir(&self) -> Result<PathBuf> {
        let dir = self.kopi_home.join(CACHE_DIR_NAME);
        fs::create_dir_all(&dir).map_err(|e| {
            KopiError::ConfigError(format!("Failed to create cache directory: {e}"))
        })?;
        Ok(dir)
    }

    /// Get the bin directory path for kopi binary and create it if it doesn't exist
    pub fn bin_dir(&self) -> Result<PathBuf> {
        let dir = self.kopi_home.join(BIN_DIR_NAME);
        fs::create_dir_all(&dir)
            .map_err(|e| KopiError::ConfigError(format!("Failed to create bin directory: {e}")))?;
        Ok(dir)
    }

    /// Get the shims directory path and create it if it doesn't exist
    pub fn shims_dir(&self) -> Result<PathBuf> {
        let dir = self.kopi_home.join(SHIMS_DIR_NAME);
        fs::create_dir_all(&dir).map_err(|e| {
            KopiError::ConfigError(format!("Failed to create shims directory: {e}"))
        })?;
        Ok(dir)
    }

    /// Get the path to the metadata cache file (ensures cache directory exists)
    pub fn metadata_cache_path(&self) -> Result<PathBuf> {
        let cache_dir = self.cache_dir()?;
        Ok(cache_dir.join("metadata.json"))
    }

    /// Get the path to the config file
    pub fn config_path(&self) -> PathBuf {
        self.kopi_home.join(CONFIG_FILE_NAME)
    }
}

// Custom error conversion from config::ConfigError
impl From<ConfigError> for KopiError {
    fn from(err: ConfigError) -> Self {
        KopiError::ConfigError(err.to_string())
    }
}

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

    #[test]
    #[serial]
    fn test_default_config() {
        // Clear KOPI_HOME to ensure we get the default behavior
        unsafe {
            env::remove_var("KOPI_HOME");
        }

        let kopi_home = resolve_kopi_home().unwrap();
        let config = KopiConfig::new(kopi_home).unwrap();
        assert_eq!(config.storage.min_disk_space_mb, DEFAULT_MIN_DISK_SPACE_MB);
        assert_eq!(config.default_distribution, "temurin");
        // The path should contain .kopi - it could be absolute or relative
        let path_str = config.kopi_home.to_string_lossy();
        assert!(
            path_str.contains(".kopi"),
            "Expected path to contain '.kopi', but got: {path_str}"
        );
    }

    #[test]
    #[serial]
    fn test_load_missing_config() {
        // Clear any environment variables that might affect the test
        unsafe {
            env::remove_var("KOPI_STORAGE__MIN_DISK_SPACE_MB");
            env::remove_var("KOPI_DEFAULT_DISTRIBUTION");
            env::remove_var("KOPI_AUTO_INSTALL__ENABLED");
            env::remove_var("KOPI_AUTO_INSTALL__PROMPT");
            env::remove_var("KOPI_AUTO_INSTALL__TIMEOUT_SECS");
        }

        let temp_dir = TempDir::new().unwrap();
        let config = KopiConfig::new(temp_dir.path().to_path_buf()).unwrap();
        assert_eq!(config.storage.min_disk_space_mb, DEFAULT_MIN_DISK_SPACE_MB);
        assert_eq!(config.default_distribution, "temurin");
        assert_eq!(config.kopi_home, temp_dir.path());
    }

    #[test]
    #[serial]
    fn test_save_and_load_config() {
        // Clear any environment variables that might affect the test
        unsafe {
            env::remove_var("KOPI_STORAGE__MIN_DISK_SPACE_MB");
            env::remove_var("KOPI_DEFAULT_DISTRIBUTION");
            env::remove_var("KOPI_AUTO_INSTALL__ENABLED");
            env::remove_var("KOPI_AUTO_INSTALL__PROMPT");
            env::remove_var("KOPI_AUTO_INSTALL__TIMEOUT_SECS");
        }

        let temp_dir = TempDir::new().unwrap();

        let mut config = KopiConfig::new(temp_dir.path().to_path_buf()).unwrap();
        config.storage.min_disk_space_mb = 1024;
        config.default_distribution = "temurin".to_string();
        config.additional_distributions = vec!["mycustom".to_string(), "private-jdk".to_string()];
        config.auto_install.enabled = true;
        config.auto_install.prompt = false;
        config.auto_install.timeout_secs = 600;

        config.save().unwrap();

        let loaded = KopiConfig::new(temp_dir.path().to_path_buf()).unwrap();
        assert_eq!(loaded.storage.min_disk_space_mb, 1024);
        assert_eq!(loaded.default_distribution, "temurin");
        assert_eq!(
            loaded.additional_distributions,
            vec!["mycustom", "private-jdk"]
        );
        assert!(loaded.auto_install.enabled);
        assert!(!loaded.auto_install.prompt);
        assert_eq!(loaded.auto_install.timeout_secs, 600);
    }

    #[test]
    #[serial]
    fn test_partial_config() {
        // Clear any environment variables that might affect the test
        unsafe {
            env::remove_var("KOPI_STORAGE__MIN_DISK_SPACE_MB");
            env::remove_var("KOPI_DEFAULT_DISTRIBUTION");
        }

        let temp_dir = TempDir::new().unwrap();
        let config_path = temp_dir.path().join(CONFIG_FILE_NAME);

        // Write partial config with only default_distribution
        fs::write(&config_path, r#"default_distribution = "corretto""#).unwrap();

        let loaded = KopiConfig::new(temp_dir.path().to_path_buf()).unwrap();
        assert_eq!(loaded.storage.min_disk_space_mb, DEFAULT_MIN_DISK_SPACE_MB);
        assert_eq!(loaded.default_distribution, "corretto");
        assert!(loaded.additional_distributions.is_empty());
    }

    #[test]
    #[serial]
    fn test_config_with_storage_section() {
        // Clear any environment variables that might affect the test
        unsafe {
            env::remove_var("KOPI_STORAGE__MIN_DISK_SPACE_MB");
            env::remove_var("KOPI_DEFAULT_DISTRIBUTION");
        }

        let temp_dir = TempDir::new().unwrap();
        let config_path = temp_dir.path().join(CONFIG_FILE_NAME);

        fs::write(
            &config_path,
            r#"
default_distribution = "zulu"
additional_distributions = ["custom1", "custom2"]

[storage]
min_disk_space_mb = 2048
"#,
        )
        .unwrap();

        let loaded = KopiConfig::new(temp_dir.path().to_path_buf()).unwrap();
        assert_eq!(loaded.storage.min_disk_space_mb, 2048);
        assert_eq!(loaded.default_distribution, "zulu");
        assert_eq!(loaded.additional_distributions, vec!["custom1", "custom2"]);
    }

    #[test]
    #[serial]
    fn test_resolve_kopi_home_from_env() {
        // Clear any existing KOPI_HOME first
        unsafe {
            env::remove_var("KOPI_HOME");
        }

        let temp_dir = TempDir::new().unwrap();
        // Ensure we have a canonicalized absolute path on all platforms
        let abs_path = temp_dir.path().canonicalize().unwrap();

        unsafe {
            env::set_var("KOPI_HOME", &abs_path);
        }
        let result = resolve_kopi_home().unwrap();
        assert_eq!(result, abs_path);

        unsafe {
            env::remove_var("KOPI_HOME");
        }
    }

    #[test]
    #[serial]
    fn test_resolve_kopi_home_relative_path() {
        // Set a relative path
        unsafe {
            env::set_var("KOPI_HOME", "relative/path");
        }

        let result = resolve_kopi_home().unwrap();
        // Should fall back to default home
        assert!(result.ends_with(".kopi"));
        assert!(result.is_absolute());

        unsafe {
            env::remove_var("KOPI_HOME");
        }
    }

    #[test]
    #[serial]
    fn test_resolve_kopi_home_default() {
        unsafe {
            env::remove_var("KOPI_HOME");
        }

        let result = resolve_kopi_home().unwrap();
        assert!(result.ends_with(".kopi"));
        assert!(result.is_absolute());
    }

    #[test]
    #[serial]
    fn test_directory_paths() {
        // Clear any environment variables that might affect the test
        unsafe {
            env::remove_var("KOPI_AUTO_INSTALL__ENABLED");
            env::remove_var("KOPI_AUTO_INSTALL__PROMPT");
            env::remove_var("KOPI_AUTO_INSTALL__TIMEOUT_SECS");
            env::remove_var("KOPI_STORAGE__MIN_DISK_SPACE_MB");
            env::remove_var("KOPI_DEFAULT_DISTRIBUTION");
        }

        let temp_dir = TempDir::new().unwrap();
        let kopi_home = temp_dir.path();
        let config = KopiConfig::new(kopi_home.to_path_buf()).unwrap();

        // Test JDKs directory
        let jdks_dir = config.jdks_dir().unwrap();
        assert_eq!(jdks_dir, kopi_home.join("jdks"));
        assert!(jdks_dir.exists());

        // Test cache directory
        let cache_dir = config.cache_dir().unwrap();
        assert_eq!(cache_dir, kopi_home.join("cache"));
        assert!(cache_dir.exists());

        // Test bin directory
        let bin_dir = config.bin_dir().unwrap();
        assert_eq!(bin_dir, kopi_home.join("bin"));
        assert!(bin_dir.exists());

        // Test metadata cache path
        let cache_path = config.metadata_cache_path().unwrap();
        assert_eq!(cache_path, kopi_home.join("cache").join("metadata.json"));

        // Test config path
        let config_path = config.config_path();
        assert_eq!(config_path, kopi_home.join(CONFIG_FILE_NAME));
    }

    #[test]
    #[serial]
    fn test_directory_creation_on_access() {
        // Clear any environment variables that might affect the test
        unsafe {
            env::remove_var("KOPI_AUTO_INSTALL__ENABLED");
            env::remove_var("KOPI_AUTO_INSTALL__PROMPT");
            env::remove_var("KOPI_AUTO_INSTALL__TIMEOUT_SECS");
            env::remove_var("KOPI_STORAGE__MIN_DISK_SPACE_MB");
            env::remove_var("KOPI_DEFAULT_DISTRIBUTION");
        }

        let temp_dir = TempDir::new().unwrap();
        let kopi_home = temp_dir.path();
        let config = KopiConfig::new(kopi_home.to_path_buf()).unwrap();

        // Verify directories don't exist initially
        assert!(!kopi_home.join("jdks").exists());
        assert!(!kopi_home.join("cache").exists());
        assert!(!kopi_home.join("bin").exists());

        // Access directories - they should be created
        config.jdks_dir().unwrap();
        assert!(kopi_home.join("jdks").exists());

        config.cache_dir().unwrap();
        assert!(kopi_home.join("cache").exists());

        config.bin_dir().unwrap();
        assert!(kopi_home.join("bin").exists());
    }

    #[test]
    #[serial]
    fn test_auto_install_config_defaults() {
        // Clear any environment variables that might affect the test
        unsafe {
            env::remove_var("KOPI_AUTO_INSTALL__ENABLED");
            env::remove_var("KOPI_AUTO_INSTALL__PROMPT");
            env::remove_var("KOPI_AUTO_INSTALL__TIMEOUT_SECS");
        }

        let temp_dir = TempDir::new().unwrap();
        let config = KopiConfig::new(temp_dir.path().to_path_buf()).unwrap();

        // Test default auto-install settings
        assert!(config.auto_install.enabled);
        assert!(config.auto_install.prompt);
        assert_eq!(config.auto_install.timeout_secs, 300);
    }

    #[test]
    #[serial]
    fn test_shims_config_defaults() {
        // Clear any environment variables that might affect the test
        unsafe {
            env::remove_var("KOPI_SHIMS__AUTO_CREATE_SHIMS");
        }

        let temp_dir = TempDir::new().unwrap();
        let config = KopiConfig::new(temp_dir.path().to_path_buf()).unwrap();

        // Test default shims settings
        assert!(config.shims.auto_create_shims);
    }

    #[test]
    #[serial]
    fn test_cache_config_defaults() {
        // Clear any environment variables that might affect the test
        unsafe {
            env::remove_var("KOPI_CACHE__MAX_AGE_HOURS");
            env::remove_var("KOPI_CACHE__AUTO_REFRESH");
            env::remove_var("KOPI_CACHE__REFRESH_ON_MISS");
        }

        let temp_dir = TempDir::new().unwrap();
        let config = KopiConfig::new(temp_dir.path().to_path_buf()).unwrap();

        // Test default cache settings
        assert_eq!(config.cache.max_age_hours, 720); // 30 days
        assert!(config.cache.auto_refresh);
        assert!(config.cache.refresh_on_miss);
    }

    #[test]
    #[serial]
    fn test_partial_config_with_auto_install() {
        // Clear any environment variables that might affect the test
        unsafe {
            env::remove_var("KOPI_AUTO_INSTALL__ENABLED");
            env::remove_var("KOPI_AUTO_INSTALL__PROMPT");
            env::remove_var("KOPI_AUTO_INSTALL__TIMEOUT_SECS");
            env::remove_var("KOPI_DEFAULT_DISTRIBUTION");
        }

        let temp_dir = TempDir::new().unwrap();
        let config_path = temp_dir.path().join(CONFIG_FILE_NAME);

        // Write partial config with only auto_install section
        fs::write(
            &config_path,
            r#"
[auto_install]
enabled = true
timeout_secs = 120
"#,
        )
        .unwrap();

        let loaded = KopiConfig::new(temp_dir.path().to_path_buf()).unwrap();
        assert!(loaded.auto_install.enabled);
        assert!(loaded.auto_install.prompt); // Should use default
        assert_eq!(loaded.auto_install.timeout_secs, 120);
        assert_eq!(loaded.default_distribution, "temurin"); // Should use default
    }

    #[test]
    #[serial]
    fn test_env_var_overrides() {
        // Clear any existing environment variables first
        unsafe {
            env::remove_var("KOPI_AUTO_INSTALL__ENABLED");
            env::remove_var("KOPI_AUTO_INSTALL__PROMPT");
            env::remove_var("KOPI_AUTO_INSTALL__TIMEOUT_SECS");
            env::remove_var("KOPI_STORAGE__MIN_DISK_SPACE_MB");
            env::remove_var("KOPI_DEFAULT_DISTRIBUTION");
        }

        let temp_dir = TempDir::new().unwrap();
        let config_path = temp_dir.path().join(CONFIG_FILE_NAME);

        // Write config with specific values
        fs::write(
            &config_path,
            r#"
default_distribution = "temurin"

[auto_install]
enabled = true
prompt = true
timeout_secs = 300

[storage]
min_disk_space_mb = 500
"#,
        )
        .unwrap();

        // Set environment variables to override config
        // With separator "__", nested fields are separated by double underscores
        unsafe {
            env::set_var("KOPI_AUTO_INSTALL__ENABLED", "false");
            env::set_var("KOPI_AUTO_INSTALL__PROMPT", "false");
            env::set_var("KOPI_AUTO_INSTALL__TIMEOUT_SECS", "600");
            env::set_var("KOPI_STORAGE__MIN_DISK_SPACE_MB", "1024");
            env::set_var("KOPI_DEFAULT_DISTRIBUTION", "corretto");
        }

        let loaded = KopiConfig::new(temp_dir.path().to_path_buf()).unwrap();

        // Verify environment overrides
        assert!(!loaded.auto_install.enabled);
        assert!(!loaded.auto_install.prompt);
        assert_eq!(loaded.auto_install.timeout_secs, 600);
        assert_eq!(loaded.storage.min_disk_space_mb, 1024);
        assert_eq!(loaded.default_distribution, "corretto");

        // Cleanup
        unsafe {
            env::remove_var("KOPI_AUTO_INSTALL__ENABLED");
            env::remove_var("KOPI_AUTO_INSTALL__PROMPT");
            env::remove_var("KOPI_AUTO_INSTALL__TIMEOUT_SECS");
            env::remove_var("KOPI_STORAGE__MIN_DISK_SPACE_MB");
            env::remove_var("KOPI_DEFAULT_DISTRIBUTION");
        }
    }

    #[test]
    #[serial]
    fn test_env_var_invalid_values() {
        let temp_dir = TempDir::new().unwrap();

        // Test invalid timeout value
        unsafe {
            env::set_var("KOPI_AUTO_INSTALL__TIMEOUT_SECS", "not_a_number");
        }
        // The config crate with try_parsing(true) will fail on invalid values
        assert!(KopiConfig::new(temp_dir.path().to_path_buf()).is_err());
        unsafe {
            env::remove_var("KOPI_AUTO_INSTALL__TIMEOUT_SECS");
        }

        // Test invalid storage value
        unsafe {
            env::set_var("KOPI_STORAGE__MIN_DISK_SPACE_MB", "invalid");
        }
        // Invalid values cause parsing errors
        assert!(KopiConfig::new(temp_dir.path().to_path_buf()).is_err());
        unsafe {
            env::remove_var("KOPI_STORAGE__MIN_DISK_SPACE_MB");
        }

        // Test empty distribution value
        unsafe {
            env::set_var("KOPI_DEFAULT_DISTRIBUTION", ""); // Empty value
        }
        let loaded = KopiConfig::new(temp_dir.path().to_path_buf()).unwrap();
        // Empty string is accepted
        assert_eq!(loaded.default_distribution, "");
        unsafe {
            env::remove_var("KOPI_DEFAULT_DISTRIBUTION");
        }

        // Clear all potential leftover environment variables
        unsafe {
            env::remove_var("KOPI_AUTO_INSTALL__ENABLED");
            env::remove_var("KOPI_AUTO_INSTALL__PROMPT");
            env::remove_var("KOPI_AUTO_INSTALL__TIMEOUT_SECS");
            env::remove_var("KOPI_STORAGE__MIN_DISK_SPACE_MB");
            env::remove_var("KOPI_DEFAULT_DISTRIBUTION");
        }

        // Test invalid boolean value
        unsafe {
            env::set_var("KOPI_AUTO_INSTALL__ENABLED", "not_a_bool");
        }
        // Invalid boolean values cause parsing errors
        assert!(KopiConfig::new(temp_dir.path().to_path_buf()).is_err());
        unsafe {
            env::remove_var("KOPI_AUTO_INSTALL__ENABLED");
        }
    }
}