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
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
// 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::error::{KopiError, Result};
use crate::models::api::Package;
use crate::models::distribution::Distribution;
use crate::storage::disk_space::DiskSpaceChecker;
use crate::storage::installation::{InstallationContext, JdkInstaller};
use crate::storage::listing::{InstalledJdk, JdkLister};
use crate::version::{Version, VersionRequest};
use log::debug;
use std::fs;
use std::path::{Path, PathBuf};

pub struct JdkRepository<'a> {
    config: &'a KopiConfig,
}

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

    pub fn jdks_dir(&self) -> Result<PathBuf> {
        self.config.jdks_dir()
    }

    pub fn jdk_install_path(
        &self,
        distribution: &Distribution,
        distribution_version: &str,
        javafx_bundled: bool,
    ) -> Result<PathBuf> {
        let suffix = if javafx_bundled { "-fx" } else { "" };
        let dir_name = format!("{}-{distribution_version}{suffix}", distribution.id());
        Ok(self.config.jdks_dir()?.join(dir_name))
    }

    pub fn prepare_jdk_installation(
        &self,
        distribution: &Distribution,
        distribution_version: &str,
        javafx_bundled: bool,
    ) -> Result<InstallationContext> {
        let install_path =
            self.jdk_install_path(distribution, distribution_version, javafx_bundled)?;

        let disk_checker = DiskSpaceChecker::new(self.config.storage.min_disk_space_mb);
        disk_checker.check_disk_space(&install_path, self.config.kopi_home())?;

        let jdks_dir = self.config.jdks_dir()?;
        JdkInstaller::prepare_installation(&jdks_dir, &install_path)
    }

    pub fn finalize_installation(&self, context: InstallationContext) -> Result<PathBuf> {
        JdkInstaller::finalize_installation(context)
    }

    pub fn cleanup_failed_installation(&self, context: &InstallationContext) -> Result<()> {
        JdkInstaller::cleanup_failed_installation(context)
    }

    pub fn list_installed_jdks(&self) -> Result<Vec<InstalledJdk>> {
        let jdks_dir = self.config.jdks_dir()?;
        JdkLister::list_installed_jdks(&jdks_dir)
    }

    /// Check if a specific JDK version is installed
    pub fn check_installation(
        &self,
        distribution: &Distribution,
        version: &Version,
    ) -> Result<bool> {
        debug!(
            "Checking installation for {} version {}",
            distribution.id(),
            version
        );

        // Get list of installed JDKs
        if let Ok(installed_jdks) = self.list_installed_jdks() {
            debug!("Found {} installed JDKs", installed_jdks.len());

            // Look for an exact match
            for jdk in installed_jdks {
                debug!(
                    "Checking installed JDK: {} {} at {:?}",
                    jdk.distribution, jdk.version, jdk.path
                );

                if jdk.distribution == distribution.id() {
                    debug!(
                        "Distribution matches. Checking if search version {} matches installed \
                         version {}",
                        version, jdk.version
                    );

                    // Check if the installed version matches the search pattern
                    // For example: installed "17.0.15" matches search pattern "17"
                    if jdk.version.matches_pattern(&version.to_string()) {
                        debug!(
                            "Found matching JDK: {} {} (matched pattern {})",
                            distribution.name(),
                            jdk.version,
                            version
                        );
                        return Ok(true);
                    } else {
                        debug!(
                            "Version mismatch: installed version {} does not match search pattern \
                             {}",
                            jdk.version, version
                        );
                    }
                }
            }
        }

        debug!(
            "No matching JDK found for {} version {}",
            distribution.id(),
            version
        );
        Ok(false)
    }

    pub fn get_jdk_size(&self, path: &Path) -> Result<u64> {
        JdkLister::get_jdk_size(path)
    }

    pub fn remove_jdk(&self, path: &Path) -> Result<()> {
        let jdks_dir = self.config.jdks_dir()?;
        if !path.starts_with(&jdks_dir) {
            return Err(KopiError::SecurityError(format!(
                "Refusing to remove directory outside of JDKs directory: {path:?}"
            )));
        }

        fs::remove_dir_all(path)?;
        Ok(())
    }

    pub fn save_jdk_metadata(
        &self,
        distribution: &Distribution,
        distribution_version: &str,
        metadata: &Package,
    ) -> Result<()> {
        let jdks_dir = self.config.jdks_dir()?;
        super::save_jdk_metadata(&jdks_dir, distribution, distribution_version, metadata)
    }

    pub fn save_jdk_metadata_with_installation(
        &self,
        distribution: &Distribution,
        distribution_version: &str,
        metadata: &Package,
        installation_metadata: &super::InstallationMetadata,
        javafx_bundled: bool,
    ) -> Result<()> {
        let jdks_dir = self.config.jdks_dir()?;
        super::save_jdk_metadata_with_installation(
            &jdks_dir,
            distribution,
            distribution_version,
            metadata,
            installation_metadata,
            javafx_bundled,
        )
    }

    /// Find installed JDKs matching a version request and return them sorted by version (oldest first)
    ///
    /// # Arguments
    /// * `request` - Version request containing version pattern, optional distribution, and optional package type
    ///
    /// # Returns
    /// * Vec of InstalledJdk sorted by version (oldest first)
    ///
    /// # Examples
    /// * Version pattern "21" - Returns all 21.x.x.x versions, oldest first
    /// * Version pattern "21.2" - Returns all 21.2.x.x versions, oldest first  
    /// * Version pattern "21.2.13" - Returns all 21.2.13.x versions, oldest first
    /// * With distribution filter - Returns only JDKs from the specified distribution
    pub fn find_matching_jdks(&self, request: &VersionRequest) -> Result<Vec<InstalledJdk>> {
        // Get all installed JDKs
        let all_jdks = self.list_installed_jdks()?;

        // Filter JDKs based on distribution, version pattern, and JavaFX
        let mut matching_jdks: Vec<InstalledJdk> = all_jdks
            .into_iter()
            .filter(|jdk| {
                // Check distribution filter if specified
                if let Some(dist) = &request.distribution
                    && &jdk.distribution != dist
                {
                    return false;
                }

                // Check JavaFX filter if specified
                if let Some(javafx) = request.javafx_bundled
                    && jdk.javafx_bundled != javafx
                {
                    return false;
                }

                // Check version pattern
                jdk.version.matches_pattern(&request.version_pattern)
            })
            .collect();

        // Sort by version (oldest first)
        // When versions are equal, maintain stable sort order (which preserves distribution order from list_installed_jdks)
        matching_jdks.sort_by(|a, b| a.version.cmp(&b.version));

        Ok(matching_jdks)
    }
}

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

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

    impl TestStorage {
        fn new() -> Self {
            // Clear any leftover environment variables
            unsafe {
                std::env::remove_var("KOPI_AUTO_INSTALL");
                std::env::remove_var("KOPI_AUTO_INSTALL__ENABLED");
            }

            let temp_dir = TempDir::new().unwrap();
            let config = KopiConfig::new(temp_dir.path().to_path_buf()).unwrap();
            TestStorage {
                config,
                _temp_dir: temp_dir,
            }
        }

        fn manager(&self) -> JdkRepository<'_> {
            JdkRepository::new(&self.config)
        }
    }

    #[test]
    fn test_jdk_install_path() {
        let test_storage = TestStorage::new();
        let manager = test_storage.manager();
        let distribution = Distribution::Temurin;

        let path = manager
            .jdk_install_path(&distribution, "21.0.1+35.1", false)
            .unwrap();
        assert!(path.ends_with("jdks/temurin-21.0.1+35.1"));
    }

    #[test]
    fn test_remove_jdk_security() {
        let test_storage = TestStorage::new();
        let manager = test_storage.manager();

        let result = manager.remove_jdk(Path::new("/etc/passwd"));
        assert!(result.is_err());
        assert!(matches!(result.unwrap_err(), KopiError::SecurityError(_)));
    }

    #[test]
    fn test_min_disk_space_from_config() {
        let temp_dir = TempDir::new().unwrap();
        let config_path = temp_dir.path().join("config.toml");

        fs::write(
            &config_path,
            r#"
[storage]
min_disk_space_mb = 1024
"#,
        )
        .unwrap();

        let config = KopiConfig::new(temp_dir.path().to_path_buf()).unwrap();
        let manager = JdkRepository::new(&config);
        assert_eq!(manager.config.storage.min_disk_space_mb, 1024);
    }

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

        let config = KopiConfig::new(temp_dir.path().to_path_buf()).unwrap();
        let manager = JdkRepository::new(&config);
        assert_eq!(manager.config.storage.min_disk_space_mb, 500);
    }

    #[test]
    fn test_check_installation_empty_repository() {
        let test_storage = TestStorage::new();
        let manager = test_storage.manager();
        let distribution = Distribution::Temurin;
        let version = Version::new(21, 0, 0);

        let result = manager.check_installation(&distribution, &version).unwrap();
        assert!(!result);
    }

    #[test]
    fn test_check_installation_with_partial_version() {
        let test_storage = TestStorage::new();
        let manager = test_storage.manager();
        let jdks_dir = test_storage.config.jdks_dir().unwrap();

        // Create a JDK directory with a full version
        fs::create_dir_all(jdks_dir.join("temurin-17.0.15")).unwrap();

        // Search with just major version "17"
        let distribution = Distribution::Temurin;
        let search_version = Version::from_components(17, None, None);

        let result = manager
            .check_installation(&distribution, &search_version)
            .unwrap();
        assert!(
            result,
            "Should find temurin-17.0.15 when searching for version 17"
        );

        // Search with major.minor "17.0"
        let search_version = Version::from_components(17, Some(0), None);
        let result = manager
            .check_installation(&distribution, &search_version)
            .unwrap();
        assert!(
            result,
            "Should find temurin-17.0.15 when searching for version 17.0"
        );

        // Search with full version "17.0.15"
        let search_version = Version::new(17, 0, 15);
        let result = manager
            .check_installation(&distribution, &search_version)
            .unwrap();
        assert!(
            result,
            "Should find temurin-17.0.15 when searching for exact version"
        );

        // Search with different patch version should not match
        let search_version = Version::new(17, 0, 14);
        let result = manager
            .check_installation(&distribution, &search_version)
            .unwrap();
        assert!(
            !result,
            "Should not find temurin-17.0.15 when searching for 17.0.14"
        );

        // Search with different minor version should not match
        let search_version = Version::new(17, 1, 0);
        let result = manager
            .check_installation(&distribution, &search_version)
            .unwrap();
        assert!(
            !result,
            "Should not find temurin-17.0.15 when searching for 17.1.0"
        );
    }

    #[test]
    fn test_find_matching_jdks() {
        let test_storage = TestStorage::new();
        let manager = test_storage.manager();
        let jdks_dir = test_storage.config.jdks_dir().unwrap();

        // Create test JDK directories as mentioned in the user's example
        fs::create_dir_all(jdks_dir.join("temurin-21.2.13.4")).unwrap();
        fs::create_dir_all(jdks_dir.join("corretto-21.2.13.5")).unwrap();
        fs::create_dir_all(jdks_dir.join("temurin-21.2.15.6")).unwrap();
        fs::create_dir_all(jdks_dir.join("temurin-21.3.17.2")).unwrap();

        // Test case 1: Pattern "21" should return oldest 21.x version first
        let request = VersionRequest::new("21".to_string()).unwrap();
        let matches = manager.find_matching_jdks(&request).unwrap();
        assert_eq!(matches.len(), 4);
        // Check all elements in ascending order
        assert_eq!(matches[0].distribution, "temurin");
        assert_eq!(matches[0].version.to_string(), "21.2.13.4");
        assert_eq!(matches[1].distribution, "corretto");
        assert_eq!(matches[1].version.to_string(), "21.2.13.5");
        assert_eq!(matches[2].distribution, "temurin");
        assert_eq!(matches[2].version.to_string(), "21.2.15.6");
        assert_eq!(matches[3].distribution, "temurin");
        assert_eq!(matches[3].version.to_string(), "21.3.17.2");

        // Test case 2: Pattern "21.2" should return oldest 21.2.x version first
        let request = VersionRequest::new("21.2".to_string()).unwrap();
        let matches = manager.find_matching_jdks(&request).unwrap();
        assert_eq!(matches.len(), 3);
        // Check all elements in ascending order
        assert_eq!(matches[0].distribution, "temurin");
        assert_eq!(matches[0].version.to_string(), "21.2.13.4");
        assert_eq!(matches[1].distribution, "corretto");
        assert_eq!(matches[1].version.to_string(), "21.2.13.5");
        assert_eq!(matches[2].distribution, "temurin");
        assert_eq!(matches[2].version.to_string(), "21.2.15.6");

        // Test case 3: Pattern "21.2.13" should return oldest 21.2.13.x version first
        let request = VersionRequest::new("21.2.13".to_string()).unwrap();
        let matches = manager.find_matching_jdks(&request).unwrap();
        assert_eq!(matches.len(), 2);
        assert_eq!(matches[0].distribution, "temurin");
        assert_eq!(matches[0].version.to_string(), "21.2.13.4");
        assert_eq!(matches[1].distribution, "corretto");
        assert_eq!(matches[1].version.to_string(), "21.2.13.5");

        // Test case 4: Pattern "21.2.13.4" should return exact match
        let request = VersionRequest::new("21.2.13.4".to_string()).unwrap();
        let matches = manager.find_matching_jdks(&request).unwrap();
        assert_eq!(matches.len(), 1);
        assert_eq!(matches[0].distribution, "temurin");
        assert_eq!(matches[0].version.to_string(), "21.2.13.4");

        // Test case 5: Pattern "corretto@21" should return only corretto JDKs
        let request = VersionRequest::new("21".to_string())
            .unwrap()
            .with_distribution("corretto".to_string());
        let matches = manager.find_matching_jdks(&request).unwrap();
        assert_eq!(matches.len(), 1);
        assert_eq!(matches[0].distribution, "corretto");
        assert_eq!(matches[0].version.to_string(), "21.2.13.5");

        // Test case 6: Pattern for non-existent version
        let request = VersionRequest::new("22".to_string()).unwrap();
        let matches = manager.find_matching_jdks(&request).unwrap();
        assert_eq!(matches.len(), 0);

        // Test case 7: Pattern with distribution that doesn't exist
        let request = VersionRequest::new("21".to_string())
            .unwrap()
            .with_distribution("zulu".to_string());
        let matches = manager.find_matching_jdks(&request).unwrap();
        assert_eq!(matches.len(), 0);
    }

    #[test]
    fn test_find_matching_jdks_sorting() {
        let test_storage = TestStorage::new();
        let manager = test_storage.manager();
        let jdks_dir = test_storage.config.jdks_dir().unwrap();

        // Create JDKs in random order to test sorting
        fs::create_dir_all(jdks_dir.join("temurin-17.0.5")).unwrap();
        fs::create_dir_all(jdks_dir.join("temurin-21.0.0")).unwrap();
        fs::create_dir_all(jdks_dir.join("temurin-17.0.15")).unwrap();
        fs::create_dir_all(jdks_dir.join("temurin-21.0.7")).unwrap();
        fs::create_dir_all(jdks_dir.join("corretto-21.0.7")).unwrap();

        // Test that versions are sorted oldest first
        let request = VersionRequest::new("21".to_string()).unwrap();
        let matches = manager.find_matching_jdks(&request).unwrap();
        assert_eq!(matches.len(), 3);

        // Check all elements in ascending order
        assert_eq!(matches[0].version.to_string(), "21.0.0");
        assert_eq!(matches[1].version.to_string(), "21.0.7");
        assert_eq!(matches[2].version.to_string(), "21.0.7");

        // When versions are equal, order is stable (depends on distribution sorting from list_installed_jdks)
        let request = VersionRequest::new("21.0.7".to_string()).unwrap();
        let matches_21_0_7 = manager.find_matching_jdks(&request).unwrap();
        assert_eq!(matches_21_0_7.len(), 2);
        // corretto comes before temurin alphabetically
        assert_eq!(matches_21_0_7[0].distribution, "corretto");
        assert_eq!(matches_21_0_7[1].distribution, "temurin");
    }

    #[test]
    fn test_find_matching_jdks_invalid_pattern() {
        // Invalid @ format - testing at VersionRequest level
        let result = VersionRequest::from_str("dist@ver@extra");
        assert!(result.is_err());

        // Invalid version pattern
        let result = VersionRequest::new("invalid.version".to_string());
        assert!(result.is_err());
    }

    #[test]
    fn test_save_jdk_metadata_with_installation() {
        use crate::models::api::Links;
        use tempfile::TempDir;

        let temp_dir = TempDir::new().unwrap();
        let config = KopiConfig::new(temp_dir.path().to_path_buf()).unwrap();
        let repository = JdkRepository::new(&config);
        let jdks_dir = config.jdks_dir().unwrap();
        fs::create_dir_all(&jdks_dir).unwrap();

        let distribution = Distribution::Temurin;
        let distribution_version = "21.0.1+35.1";

        let package = Package {
            id: "test-package-id".to_string(),
            archive_type: "tar.gz".to_string(),
            distribution: "temurin".to_string(),
            major_version: 21,
            java_version: "21.0.1".to_string(),
            distribution_version: distribution_version.to_string(),
            jdk_version: 21,
            directly_downloadable: true,
            filename: "OpenJDK21U-jdk_aarch64_mac_hotspot_21.0.1_35.1.tar.gz".to_string(),
            links: Links {
                pkg_download_redirect: "https://example.com/download".to_string(),
                pkg_info_uri: Some("https://example.com/info".to_string()),
            },
            free_use_in_production: true,
            tck_tested: "yes".to_string(),
            size: 190000000,
            operating_system: "mac".to_string(),
            architecture: Some("aarch64".to_string()),
            lib_c_type: None,
            package_type: "jdk".to_string(),
            javafx_bundled: false,
            term_of_support: None,
            release_status: None,
            latest_build_available: None,
        };

        let installation_metadata = crate::storage::InstallationMetadata {
            java_home_suffix: "Contents/Home".to_string(),
            structure_type: "bundle".to_string(),
            platform: "macos_aarch64".to_string(),
            metadata_version: 1,
        };

        // Save metadata with installation info
        let result = repository.save_jdk_metadata_with_installation(
            &distribution,
            distribution_version,
            &package,
            &installation_metadata,
            false,
        );
        assert!(result.is_ok());

        // Verify the metadata file exists
        let metadata_path = jdks_dir.join(format!(
            "{}-{distribution_version}.meta.json",
            distribution.id()
        ));
        assert!(metadata_path.exists());

        // Read and verify the contents
        let content = fs::read_to_string(&metadata_path).unwrap();
        let parsed: serde_json::Value = serde_json::from_str(&content).unwrap();

        // Check API metadata fields
        assert_eq!(parsed["id"], "test-package-id");
        assert_eq!(parsed["distribution"], "temurin");
        assert_eq!(parsed["java_version"], "21.0.1");

        // Check installation metadata fields
        assert_eq!(
            parsed["installation_metadata"]["java_home_suffix"],
            "Contents/Home"
        );
        assert_eq!(parsed["installation_metadata"]["structure_type"], "bundle");
        assert_eq!(parsed["installation_metadata"]["platform"], "macos_aarch64");
        assert_eq!(parsed["installation_metadata"]["metadata_version"], 1);
    }

    #[test]
    fn test_javafx_directory_separation() {
        let test_storage = TestStorage::new();
        let manager = test_storage.manager();
        let jdks_dir = test_storage.config.jdks_dir().unwrap();

        // Create both JavaFX and non-JavaFX versions of the same JDK
        fs::create_dir_all(jdks_dir.join("liberica-21.0.5")).unwrap();
        fs::create_dir_all(jdks_dir.join("liberica-21.0.5-fx")).unwrap();

        // List installed JDKs
        let installed = manager.list_installed_jdks().unwrap();
        assert_eq!(installed.len(), 2);

        // Find the two JDKs and verify their properties
        let non_fx = installed.iter().find(|jdk| !jdk.javafx_bundled).unwrap();
        let with_fx = installed.iter().find(|jdk| jdk.javafx_bundled).unwrap();

        assert_eq!(non_fx.distribution, "liberica");
        assert_eq!(non_fx.version.to_string(), "21.0.5");
        assert!(!non_fx.javafx_bundled);
        assert!(non_fx.path.ends_with("liberica-21.0.5"));

        assert_eq!(with_fx.distribution, "liberica");
        assert_eq!(with_fx.version.to_string(), "21.0.5");
        assert!(with_fx.javafx_bundled);
        assert!(with_fx.path.ends_with("liberica-21.0.5-fx"));

        // Test version request filtering by JavaFX
        let request_no_fx = VersionRequest::new("21.0.5".to_string())
            .unwrap()
            .with_distribution("liberica".to_string())
            .with_javafx_bundled(false);
        let matches_no_fx = manager.find_matching_jdks(&request_no_fx).unwrap();
        assert_eq!(matches_no_fx.len(), 1);
        assert!(!matches_no_fx[0].javafx_bundled);

        let request_with_fx = VersionRequest::new("21.0.5".to_string())
            .unwrap()
            .with_distribution("liberica".to_string())
            .with_javafx_bundled(true);
        let matches_with_fx = manager.find_matching_jdks(&request_with_fx).unwrap();
        assert_eq!(matches_with_fx.len(), 1);
        assert!(matches_with_fx[0].javafx_bundled);

        // Test that without JavaFX filter, both are returned
        let request_all = VersionRequest::new("21.0.5".to_string())
            .unwrap()
            .with_distribution("liberica".to_string());
        let matches_all = manager.find_matching_jdks(&request_all).unwrap();
        assert_eq!(matches_all.len(), 2);
    }

    #[test]
    fn test_jdk_install_path_with_javafx() {
        let test_storage = TestStorage::new();
        let manager = test_storage.manager();
        let distribution = Distribution::Liberica;

        // Test non-JavaFX path
        let path_no_fx = manager
            .jdk_install_path(&distribution, "21.0.5", false)
            .unwrap();
        assert!(path_no_fx.ends_with("jdks/liberica-21.0.5"));

        // Test JavaFX path
        let path_with_fx = manager
            .jdk_install_path(&distribution, "21.0.5", true)
            .unwrap();
        assert!(path_with_fx.ends_with("jdks/liberica-21.0.5-fx"));
    }
}