agpm-cli 0.4.14

AGent Package Manager - A Git-based package manager for coding agents
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
//! Manage the global Git repository cache for AGPM.
//!
//! This module provides the `cache` command which allows users to manage the
//! global cache directory where AGPM stores cloned Git repositories. The cache
//! improves performance by avoiding repeated clones of the same repositories.
//!
//! # Features
//!
//! - **Cache Information**: View cache location, size, and contents
//! - **Selective Cleanup**: Remove unused cached repositories
//! - **Complete Cleanup**: Clear entire cache directory
//! - **Size Reporting**: Human-readable cache size formatting
//! - **Usage Analysis**: Identify active vs. unused cache entries
//!
//! # Cache Structure
//!
//! The cache directory (typically `~/.agpm/cache/`) contains:
//! - One subdirectory per source repository
//! - Each subdirectory is a bare Git clone
//! - Directory names match source names from manifests
//!
//! # Examples
//!
//! Show cache information:
//! ```bash
//! agpm cache info
//! agpm cache  # defaults to info
//! ```
//!
//! Clean unused cache entries:
//! ```bash
//! agpm cache clean
//! ```
//!
//! Clear entire cache:
//! ```bash
//! agpm cache clean --all
//! ```
//!
//! # Cache Management Strategy
//!
//! ## Automatic Cache Population
//! - Cache is populated during `install` and `update` commands
//! - Repositories are cloned as bare repositories for efficiency
//! - Multiple projects can share the same cached repository
//!
//! ## Cache Cleanup Logic
//! - **Unused Detection**: Compares cache contents with active project manifest
//! - **Safe Cleanup**: Only removes repositories not referenced in current project
//! - **Complete Cleanup**: `--all` flag removes entire cache regardless of usage
//!
//! ## Performance Benefits
//! - Avoids repeated Git clones for same repositories
//! - Reduces network traffic and installation time
//! - Enables offline operation for already-cached repositories
//!
//! # Security Considerations
//!
//! - Cache may contain authentication tokens in Git URLs
//! - Cleanup operations respect file permissions
//! - Cache location follows platform conventions for security
//!
//! # Error Conditions
//!
//! - Cache directory access permission issues
//! - File system errors during cleanup operations
//! - Manifest file parsing errors (for usage analysis)

use anyhow::Result;
use clap::{Args, Subcommand};
use colored::Colorize;

use crate::cache::Cache;
use crate::manifest::{Manifest, find_manifest_with_optional};
use std::path::PathBuf;

/// Command to manage the global Git repository cache.
///
/// This command provides operations for managing AGPM's global cache directory
/// where Git repositories are stored. The cache improves performance by avoiding
/// repeated clones of the same repositories across multiple projects.
///
/// # Default Behavior
///
/// If no subcommand is specified, defaults to showing cache information.
///
/// # Examples
///
/// ```rust,ignore
/// use agpm_cli::cli::cache::{CacheCommand, CacheSubcommands};
///
/// // Show cache info (default behavior)
/// let cmd = CacheCommand { command: None };
///
/// // Clean unused cache entries
/// let cmd = CacheCommand {
///     command: Some(CacheSubcommands::Clean { all: false })
/// };
///
/// // Clear entire cache
/// let cmd = CacheCommand {
///     command: Some(CacheSubcommands::Clean { all: true })
/// };
/// ```
#[derive(Args)]
pub struct CacheCommand {
    /// Cache management operation to perform
    #[command(subcommand)]
    command: Option<CacheSubcommands>,
}

/// Subcommands for cache management operations.
///
/// This enum defines the available operations for managing the Git repository cache.
/// Each operation serves a different cache management purpose.
#[derive(Subcommand)]
enum CacheSubcommands {
    /// Remove cached repositories that are no longer needed.
    ///
    /// By default, this command performs "smart" cleanup by analyzing the current
    /// project's manifest to determine which cache entries are still needed. Only
    /// repositories not referenced by the current project are removed.
    ///
    /// # Smart Cleanup Logic
    /// 1. Loads the current project manifest (`agpm.toml`)
    /// 2. Extracts all source repository names
    /// 3. Compares with cached repository directories
    /// 4. Removes cache entries not referenced in the manifest
    ///
    /// # Complete Cleanup
    /// With the `--all` flag, removes the entire cache directory regardless
    /// of current usage. This is useful for:
    /// - Freeing maximum disk space
    /// - Resolving cache corruption issues
    /// - Forcing fresh downloads of all repositories
    ///
    /// # Examples
    /// ```bash
    /// agpm cache clean           # Remove unused entries
    /// agpm cache clean --all     # Remove entire cache
    /// ```
    Clean {
        /// Remove all cache, not just unused entries
        ///
        /// When enabled, removes the entire cache directory instead of
        /// performing selective cleanup based on manifest analysis.
        #[arg(long)]
        all: bool,
    },

    /// Display information about the cache directory.
    ///
    /// Shows comprehensive information about the cache including:
    /// - Cache directory location
    /// - Total cache size in human-readable format
    /// - List of cached repositories
    /// - Usage tips and commands
    ///
    /// # Information Displayed
    /// - **Location**: Full path to cache directory
    /// - **Size**: Total disk space used by cache
    /// - **Repositories**: List of cached repository directories
    /// - **Tips**: Helpful commands for cache management
    ///
    /// This is the default command when no subcommand is specified.
    ///
    /// # Examples
    /// ```bash
    /// agpm cache info    # Explicit info command
    /// agpm cache         # Defaults to info
    /// ```
    Info,
}

impl CacheCommand {
    /// Execute the cache command with default cache configuration.
    ///
    /// This method creates a new cache instance using the default cache directory
    /// and dispatches to the appropriate subcommand handler.
    ///
    /// # Returns
    ///
    /// - `Ok(())` if the cache operation completed successfully
    /// - `Err(anyhow::Error)` if cache creation or operation fails
    ///
    /// # Examples
    ///
    /// ```rust,ignore
    /// use agpm_cli::cli::cache::CacheCommand;
    ///
    /// # tokio_test::block_on(async {
    /// let cmd = CacheCommand { command: None };
    /// // cmd.execute().await?;
    /// # Ok::<(), anyhow::Error>(())
    /// # });
    /// ```
    #[allow(dead_code)] // Used in integration tests to verify cache operations
    pub async fn execute(self) -> Result<()> {
        let cache = Cache::new()?;
        self.execute_with_cache_and_manifest(cache, None).await
    }

    /// Execute the cache command with a specific manifest path.
    ///
    /// This method creates a new cache instance and uses the provided manifest path
    /// for operations that need to read the project manifest.
    ///
    /// # Arguments
    ///
    /// * `manifest_path` - Optional path to the manifest file
    ///
    /// # Returns
    ///
    /// - `Ok(())` if the cache operation completed successfully
    /// - `Err(anyhow::Error)` if cache creation or operation fails
    pub async fn execute_with_manifest_path(self, manifest_path: Option<PathBuf>) -> Result<()> {
        let cache = Cache::new()?;
        self.execute_with_cache_and_manifest(cache, manifest_path).await
    }

    /// Execute the cache command with a specific cache instance.
    ///
    /// This method allows dependency injection of a cache instance, which is
    /// particularly useful for testing with temporary cache directories.
    ///
    /// # Arguments
    ///
    /// * `cache` - The cache instance to use for operations
    ///
    /// # Behavior
    ///
    /// Dispatches to the appropriate handler based on the subcommand:
    /// - `Clean { all: true }` → Complete cache cleanup
    /// - `Clean { all: false }` → Smart unused cache cleanup
    /// - `Info` or `None` → Display cache information
    ///
    /// # Returns
    ///
    /// - `Ok(())` if the operation completed successfully
    /// - `Err(anyhow::Error)` if the operation encounters an error
    ///
    /// # Examples
    ///
    /// ```rust,ignore
    /// use agpm_cli::cli::cache::{CacheCommand, CacheSubcommands};
    /// use agpm_cli::cache::Cache;
    /// use tempfile::TempDir;
    ///
    /// # tokio_test::block_on(async {
    /// let temp_dir = TempDir::new()?;
    /// let cache = Cache::with_dir(temp_dir.path().to_path_buf())?;
    /// let cmd = CacheCommand {
    ///     command: Some(CacheSubcommands::Info)
    /// };
    /// cmd.execute_with_cache(cache).await?;
    /// # Ok::<(), anyhow::Error>(())
    /// # });
    /// ```
    #[allow(dead_code)] // Used in tests for dependency injection with custom cache instances
    pub async fn execute_with_cache(self, cache: Cache) -> Result<()> {
        self.execute_with_cache_and_manifest(cache, None).await
    }

    /// Execute the cache command with a specific cache instance and manifest path.
    ///
    /// This method allows dependency injection of both cache and manifest path,
    /// which is particularly useful for testing.
    ///
    /// # Arguments
    ///
    /// * `cache` - The cache instance to use for operations
    /// * `manifest_path` - Optional path to the manifest file
    ///
    /// # Returns
    ///
    /// - `Ok(())` if the operation completed successfully
    /// - `Err(anyhow::Error)` if the operation encounters an error
    async fn execute_with_cache_and_manifest(
        self,
        cache: Cache,
        manifest_path: Option<PathBuf>,
    ) -> Result<()> {
        match self.command {
            Some(CacheSubcommands::Clean {
                all,
            }) => {
                if all {
                    self.clean_all(cache).await
                } else {
                    self.clean_unused(cache, manifest_path).await
                }
            }
            Some(CacheSubcommands::Info) | None => self.show_info(cache).await,
        }
    }

    /// Remove all cached repositories regardless of usage.
    ///
    /// This method performs complete cache cleanup by removing the entire cache
    /// directory and all its contents. This is more aggressive than selective
    /// cleanup and is useful for:
    ///
    /// - Freeing maximum disk space
    /// - Resolving cache corruption issues
    /// - Forcing fresh downloads of all repositories
    /// - Starting with a clean slate
    ///
    /// # Arguments
    ///
    /// * `cache` - The cache instance to operate on
    ///
    /// # Returns
    ///
    /// - `Ok(())` if cache cleanup completed successfully
    /// - `Err(anyhow::Error)` if file system operations fail
    ///
    /// # Side Effects
    ///
    /// - Removes the entire cache directory tree
    /// - All subsequent operations will need to re-clone repositories
    /// - Performance impact on next install/update operations
    async fn clean_all(&self, cache: Cache) -> Result<()> {
        println!("🗑️  Cleaning all cache...");

        // Also clean up stale lock files (older than 1 hour)
        let cache_dir = cache.cache_dir();
        if let Ok(removed) = crate::cache::lock::cleanup_stale_locks(cache_dir, 3600).await
            && removed > 0
        {
            println!("  Removed {removed} stale lock files");
        }

        cache.clear_all().await?;

        println!("{}", "✅ Cache cleared successfully".green().bold());
        Ok(())
    }

    /// Remove only cached repositories that are not referenced in the current manifest.
    ///
    /// This method performs intelligent cache cleanup by:
    ///
    /// 1. Loading the current project's manifest (`agpm.toml`)
    /// 2. Extracting the list of source repository names
    /// 3. Comparing cached repositories with active sources
    /// 4. Removing only cache entries not referenced in the manifest
    ///
    /// # Safety Features
    ///
    /// - Preserves cache entries for sources defined in the manifest
    /// - Gracefully handles missing manifest files
    /// - Provides clear feedback about cleanup results
    /// - Non-destructive when no manifest is found
    ///
    /// # Arguments
    ///
    /// * `cache` - The cache instance to operate on
    /// * `manifest_path` - Optional path to the manifest file
    ///
    /// # Behavior Without Manifest
    ///
    /// If no `agpm.toml` file is found:
    /// - Issues a warning message
    /// - Performs no cleanup operations
    /// - Suggests using `--all` flag for complete cleanup
    /// - Returns successfully without error
    ///
    /// # Returns
    ///
    /// - `Ok(())` if cleanup completed successfully or no manifest found
    /// - `Err(anyhow::Error)` if manifest loading or file operations fail
    ///
    /// # Examples
    ///
    /// Given a manifest with sources "official" and "community":
    /// - Cache entries "official" and "community" are preserved
    /// - Cache entry "old-unused" is removed
    /// - Cache entry "another-project" is removed
    async fn clean_unused(&self, cache: Cache, manifest_path: Option<PathBuf>) -> Result<()> {
        println!("🔍 Scanning for unused cache entries...");

        // Find manifest to get active sources
        let active_sources = if let Ok(manifest_path) = find_manifest_with_optional(manifest_path) {
            let manifest = Manifest::load(&manifest_path)?;
            manifest.sources.keys().cloned().collect::<Vec<_>>()
        } else {
            // No manifest found, can't determine what's in use
            println!("⚠️  No agpm.toml found. Use --all to clear entire cache.");
            return Ok(());
        };

        let removed = cache.clean_unused(&active_sources).await?;

        // Also clean up stale lock files (older than 1 hour)
        let cache_dir = cache.cache_dir();
        let lock_removed =
            crate::cache::lock::cleanup_stale_locks(cache_dir, 3600).await.unwrap_or(0);

        if removed > 0 || lock_removed > 0 {
            let mut messages = Vec::new();
            if removed > 0 {
                messages.push(format!("{removed} unused cache entries"));
            }
            if lock_removed > 0 {
                messages.push(format!("{lock_removed} stale lock files"));
            }
            println!("{}", format!("✅ Removed {}", messages.join(" and ")).green().bold());
        } else {
            println!("✨ Cache is already clean - no unused entries found");
        }

        Ok(())
    }

    /// Display comprehensive information about the cache directory and contents.
    ///
    /// This method provides a detailed overview of the cache including:
    ///
    /// - **Location**: Absolute path to the cache directory
    /// - **Size**: Total disk space used, formatted in human-readable units
    /// - **Contents**: List of cached repository directories
    /// - **Usage Tips**: Helpful commands for cache management
    ///
    /// # Information Displayed
    ///
    /// ## Cache Location
    /// Shows the full path to the cache directory, typically:
    /// - `~/.agpm/cache/` on Unix-like systems
    /// - `%APPDATA%/agpm/cache/` on Windows
    ///
    /// ## Cache Size
    /// Total disk space used by all cached repositories, automatically
    /// formatted using appropriate units (B, KB, MB, GB).
    ///
    /// ## Repository Listing
    /// Each cached repository is listed by name, corresponding to the
    /// source names defined in project manifests.
    ///
    /// # Arguments
    ///
    /// * `cache` - The cache instance to analyze
    ///
    /// # Returns
    ///
    /// - `Ok(())` if information display completed successfully
    /// - `Err(anyhow::Error)` if cache directory access or size calculation fails
    ///
    /// # Behavior
    ///
    /// - Handles non-existent cache directories gracefully
    /// - Shows empty cache state when no repositories are cached
    /// - Provides actionable tips for cache management
    /// - Uses async I/O for efficient directory scanning
    async fn show_info(&self, cache: Cache) -> Result<()> {
        let location = cache.get_cache_location();
        let size = cache.get_cache_size().await?;

        println!("{}", "Cache Information".bold());
        println!("  Location: {}", location.display());
        println!("  Size: {}", format_size(size));

        // List cached repositories
        if location.exists() {
            let mut entries = tokio::fs::read_dir(location).await?;
            let mut repos = Vec::new();

            while let Some(entry) = entries.next_entry().await? {
                if entry.path().is_dir()
                    && let Some(name) = entry.path().file_name()
                {
                    repos.push(name.to_string_lossy().to_string());
                }
            }

            if !repos.is_empty() {
                println!("\n{}", "Cached repositories:".bold());
                for repo in repos {
                    println!("{repo}");
                }
            }
        }

        println!("\n{}", "Tip:".yellow());
        println!("  Use 'agpm cache clean' to remove unused cache");
        println!("  Use 'agpm cache clean --all' to clear all cache");

        Ok(())
    }
}

/// Format byte size into human-readable string with appropriate units.
///
/// This function converts raw byte values into human-readable format using
/// standard binary prefixes (1024-based). It automatically selects the most
/// appropriate unit to avoid large numbers while maintaining reasonable precision.
///
/// # Arguments
///
/// * `bytes` - The number of bytes to format
///
/// # Units
///
/// Uses binary prefixes with the following progression:
/// - B (bytes): 0-1023 bytes
/// - KB (kilobytes): 1024+ bytes
/// - MB (megabytes): 1024+ KB
/// - GB (gigabytes): 1024+ MB
///
/// # Formatting Rules
///
/// - Bytes: Displayed as whole numbers (e.g., "512 B")
/// - Larger units: Displayed with 2 decimal places (e.g., "1.50 KB")
/// - Zero bytes: Special case returns "0 B"
///
/// # Returns
///
/// A formatted string with the size and appropriate unit.
///
/// # Examples
///
/// ```rust,ignore
/// # use agpm_cli::cli::cache::format_size;
/// assert_eq!(format_size(0), "0 B");
/// assert_eq!(format_size(512), "512 B");
/// assert_eq!(format_size(1024), "1.00 KB");
/// assert_eq!(format_size(1536), "1.50 KB");
/// assert_eq!(format_size(1048576), "1.00 MB");
/// assert_eq!(format_size(1073741824), "1.00 GB");
/// ```
fn format_size(bytes: u64) -> String {
    const UNITS: &[&str] = &["B", "KB", "MB", "GB"];

    if bytes == 0 {
        return "0 B".to_string();
    }

    #[allow(clippy::cast_precision_loss)]
    let mut size = bytes as f64;
    let mut unit_index = 0;

    while size >= 1024.0 && unit_index < UNITS.len() - 1 {
        size /= 1024.0;
        unit_index += 1;
    }

    if unit_index == 0 {
        format!("{} {}", bytes, UNITS[unit_index])
    } else {
        format!("{:.2} {}", size, UNITS[unit_index])
    }
}

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

    #[test]
    fn test_format_size() {
        assert_eq!(format_size(0), "0 B");
        assert_eq!(format_size(512), "512 B");
        assert_eq!(format_size(1024), "1.00 KB");
        assert_eq!(format_size(1536), "1.50 KB");
        assert_eq!(format_size(1048576), "1.00 MB");
        assert_eq!(format_size(1073741824), "1.00 GB");
    }

    #[test]
    fn test_format_size_edge_cases() {
        assert_eq!(format_size(1023), "1023 B");
        assert_eq!(format_size(1025), "1.00 KB");
        assert_eq!(format_size(1048575), "1024.00 KB");
        assert_eq!(format_size(1048577), "1.00 MB");
        assert_eq!(format_size(2097152), "2.00 MB");
        assert_eq!(format_size(5242880), "5.00 MB");
    }

    #[tokio::test]
    async fn test_cache_info_command() -> Result<()> {
        use tempfile::TempDir;

        let temp_dir = TempDir::new()?;
        let cache = Cache::with_dir(temp_dir.path().to_path_buf())?;

        let cmd = CacheCommand {
            command: Some(CacheSubcommands::Info),
        };

        // Create a cache directory with some test content
        let cache_dir = temp_dir.path().join("test-repo");
        std::fs::create_dir_all(&cache_dir)?;
        std::fs::write(cache_dir.join("test.txt"), "test content")?;

        // This won't fail even if the display output changes
        cmd.execute_with_cache(cache).await?;
        Ok(())
    }

    #[tokio::test]
    async fn test_cache_clean_all() -> Result<()> {
        use tempfile::TempDir;

        let temp_dir = TempDir::new()?;
        let cache = Cache::with_dir(temp_dir.path().to_path_buf())?;

        // Create some test cache directories
        let repo1 = temp_dir.path().join("repo1");
        let repo2 = temp_dir.path().join("repo2");
        std::fs::create_dir_all(&repo1)?;
        std::fs::create_dir_all(&repo2)?;
        std::fs::write(repo1.join("file.txt"), "content")?;
        std::fs::write(repo2.join("file.txt"), "content")?;

        assert!(repo1.exists());
        assert!(repo2.exists());

        let cmd = CacheCommand {
            command: Some(CacheSubcommands::Clean {
                all: true,
            }),
        };

        cmd.execute_with_cache(cache).await?;

        // Give a small delay to ensure async removal is completed
        tokio::time::sleep(tokio::time::Duration::from_millis(50)).await;

        // Check that cache directory itself was cleared
        // Note: clear_all removes the entire cache directory
        // Since clear_all removes the cache dir entirely, the entire temp_dir should be empty
        // But temp_dir itself (the parent) should still exist due to TempDir
        assert!(!repo1.exists());
        assert!(!repo2.exists());
        // The entire cache directory should be gone
        assert!(
            !temp_dir.path().exists()
                || temp_dir.path().read_dir().map(|mut d| d.next().is_none()).unwrap_or(false)
        );
        Ok(())
    }

    #[tokio::test]
    async fn test_cache_clean_unused_no_manifest() -> Result<()> {
        use tempfile::TempDir;

        let temp_dir = TempDir::new()?;
        let work_dir = TempDir::new()?;
        let cache = Cache::with_dir(temp_dir.path().to_path_buf())?;

        let cmd = CacheCommand {
            command: Some(CacheSubcommands::Clean {
                all: false,
            }),
        };

        // Pass a non-existent manifest path to ensure no manifest is found
        let non_existent_manifest = work_dir.path().join("agpm.toml");
        assert!(!non_existent_manifest.exists());

        // Without a manifest, should warn and not clean
        cmd.execute_with_cache_and_manifest(cache, Some(non_existent_manifest)).await?;
        Ok(())
    }

    #[tokio::test]
    async fn test_cache_clean_unused_with_manifest() -> Result<()> {
        use tempfile::TempDir;

        let temp_dir = TempDir::new()?;
        let work_dir = TempDir::new()?;
        let cache = Cache::with_dir(temp_dir.path().to_path_buf())?;

        // Create a manifest with one source
        let manifest = Manifest {
            sources: std::collections::HashMap::from([(
                "active".to_string(),
                "https://github.com/test/active.git".to_string(),
            )]),
            ..Default::default()
        };
        let manifest_path = work_dir.path().join("agpm.toml");
        manifest.save(&manifest_path)?;

        // Create cache directories - one active (matches manifest), one unused
        let active_cache = temp_dir.path().join("active");
        let unused_cache = temp_dir.path().join("unused-test-source");
        std::fs::create_dir_all(&active_cache)?;
        std::fs::create_dir_all(&unused_cache)?;

        // Verify both exist before cleaning
        assert!(active_cache.exists());
        assert!(unused_cache.exists());

        let cmd = CacheCommand {
            command: Some(CacheSubcommands::Clean {
                all: false,
            }),
        };

        cmd.execute_with_cache_and_manifest(cache, Some(manifest_path)).await?;

        // Give a small delay to ensure async removal is completed
        tokio::time::sleep(tokio::time::Duration::from_millis(50)).await;

        // Active cache should remain, unused should be removed
        assert!(active_cache.exists());
        assert!(!unused_cache.exists());
        Ok(())
    }

    #[tokio::test]
    async fn test_cache_default_command() -> Result<()> {
        use tempfile::TempDir;

        let temp_dir = TempDir::new()?;
        let cache = Cache::with_dir(temp_dir.path().to_path_buf())?;

        // Test that no subcommand defaults to Info
        let cmd = CacheCommand {
            command: None,
        };

        cmd.execute_with_cache(cache).await?;
        Ok(())
    }

    #[tokio::test]
    async fn test_cache_info_with_empty_cache() -> Result<()> {
        use tempfile::TempDir;

        let temp_dir = TempDir::new()?;
        let cache = Cache::with_dir(temp_dir.path().to_path_buf())?;

        // Ensure cache directory exists
        cache.ensure_cache_dir().await?;

        let cmd = CacheCommand {
            command: Some(CacheSubcommands::Info),
        };

        cmd.execute_with_cache(cache).await?;
        Ok(())
    }

    #[tokio::test]
    async fn test_cache_info_with_content() -> Result<()> {
        use tempfile::TempDir;

        let temp_dir = TempDir::new()?;
        let cache = Cache::with_dir(temp_dir.path().to_path_buf())?;

        // Ensure cache directory exists
        cache.ensure_cache_dir().await?;

        // Create some test content
        let source_dir = temp_dir.path().join("test-source");
        std::fs::create_dir_all(&source_dir)?;
        std::fs::write(source_dir.join("file1.txt"), "content1")?;
        std::fs::write(source_dir.join("file2.txt"), "content2 with more data")?;

        // Create nested directory with content
        let nested = source_dir.join("nested");
        std::fs::create_dir_all(&nested)?;
        std::fs::write(nested.join("file3.txt"), "nested content")?;

        // Get size before executing command (which consumes cache)
        let size = cache.get_cache_size().await?;
        assert!(size > 0);

        let cmd = CacheCommand {
            command: Some(CacheSubcommands::Info),
        };

        cmd.execute_with_cache(cache).await?;
        Ok(())
    }

    #[tokio::test]
    async fn test_cache_execute_without_dir() -> Result<()> {
        // Test CacheCommand::execute which creates its own Cache

        let cmd = CacheCommand {
            command: Some(CacheSubcommands::Info),
        };

        // This uses the default cache directory
        cmd.execute().await?;
        Ok(())
    }

    #[tokio::test]
    async fn test_cache_clean_all_empty_cache() -> Result<()> {
        use tempfile::TempDir;

        let temp_dir = TempDir::new()?;
        let cache = Cache::with_dir(temp_dir.path().to_path_buf())?;

        // Don't create any cache content
        let cmd = CacheCommand {
            command: Some(CacheSubcommands::Clean {
                all: true,
            }),
        };

        cmd.execute_with_cache(cache).await?;

        // Should handle empty cache gracefully
        assert!(!temp_dir.path().exists() || temp_dir.path().read_dir()?.next().is_none());
        Ok(())
    }

    #[tokio::test]
    async fn test_cache_clean_with_multiple_sources() -> Result<()> {
        use tempfile::TempDir;

        let temp_dir = TempDir::new()?;
        let work_dir = TempDir::new()?;
        let cache = Cache::with_dir(temp_dir.path().to_path_buf())?;

        // Create manifest with multiple sources
        let manifest = Manifest {
            sources: std::collections::HashMap::from([
                ("source1".to_string(), "https://github.com/test/repo1.git".to_string()),
                ("source2".to_string(), "https://github.com/test/repo2.git".to_string()),
            ]),
            ..Default::default()
        };
        let manifest_path = work_dir.path().join("agpm.toml");
        manifest.save(&manifest_path)?;

        // Create cache directories
        let source1_cache = temp_dir.path().join("source1");
        let source2_cache = temp_dir.path().join("source2");
        let unused_cache = temp_dir.path().join("unused");
        std::fs::create_dir_all(&source1_cache)?;
        std::fs::create_dir_all(&source2_cache)?;
        std::fs::create_dir_all(&unused_cache)?;

        let cmd = CacheCommand {
            command: Some(CacheSubcommands::Clean {
                all: false,
            }),
        };

        cmd.execute_with_cache_and_manifest(cache, Some(manifest_path)).await?;

        tokio::time::sleep(tokio::time::Duration::from_millis(50)).await;

        // Active sources should remain
        assert!(source1_cache.exists());
        assert!(source2_cache.exists());
        // Unused should be removed
        assert!(!unused_cache.exists());
        Ok(())
    }

    #[tokio::test]
    async fn test_cache_info_formatting() -> Result<()> {
        use tempfile::TempDir;

        let temp_dir = TempDir::new()?;
        let cache = Cache::with_dir(temp_dir.path().to_path_buf())?;

        // Create cache with known size
        cache.ensure_cache_dir().await?;
        let test_file = temp_dir.path().join("test.txt");
        // Write exactly 1024 bytes (1 KB)
        let content = vec![b'a'; 1024];
        std::fs::write(&test_file, content)?;

        let cmd = CacheCommand {
            command: Some(CacheSubcommands::Info),
        };

        cmd.execute_with_cache(cache).await?;

        // The output should format 1024 bytes as "1.0 KB" or similar
        // This tests the format_size function
        Ok(())
    }

    #[tokio::test]
    async fn test_cache_clean_no_manifest_warning() -> Result<()> {
        use tempfile::TempDir;

        let temp_dir = TempDir::new()?;
        let work_dir = TempDir::new()?;
        let cache = Cache::with_dir(temp_dir.path().to_path_buf())?;

        // No manifest file exists
        let non_existent_manifest = work_dir.path().join("agpm.toml");
        assert!(!non_existent_manifest.exists());

        // Create some cache directories
        let cache_dir1 = temp_dir.path().join("source1");
        std::fs::create_dir_all(&cache_dir1)?;

        let cmd = CacheCommand {
            command: Some(CacheSubcommands::Clean {
                all: false,
            }),
        };

        // Pass a non-existent manifest path to ensure no manifest is found
        cmd.execute_with_cache_and_manifest(cache, Some(non_existent_manifest)).await?;

        // Cache should remain untouched without manifest
        assert!(cache_dir1.exists());
        Ok(())
    }

    #[tokio::test]
    async fn test_cache_size_calculation() -> Result<()> {
        use tempfile::TempDir;

        let temp_dir = TempDir::new()?;
        let cache = Cache::with_dir(temp_dir.path().to_path_buf())?;

        cache.ensure_cache_dir().await?;

        // Create files with known sizes
        std::fs::write(temp_dir.path().join("file1.txt"), vec![b'a'; 100])?;
        std::fs::write(temp_dir.path().join("file2.txt"), vec![b'b'; 200])?;

        let sub_dir = temp_dir.path().join("subdir");
        std::fs::create_dir_all(&sub_dir)?;
        std::fs::write(sub_dir.join("file3.txt"), vec![b'c'; 300])?;

        // Total should be 100 + 200 + 300 = 600 bytes
        let size = cache.get_cache_size().await?;
        assert_eq!(size, 600);
        Ok(())
    }

    #[tokio::test]
    async fn test_cache_clean_preserves_lockfile_sources() -> Result<()> {
        use crate::lockfile::{LockFile, LockedSource};
        use tempfile::TempDir;

        let temp_dir = TempDir::new()?;
        let work_dir = TempDir::new()?;
        let cache = Cache::with_dir(temp_dir.path().to_path_buf())?;

        // Create manifest with one source
        let manifest = Manifest {
            sources: std::collections::HashMap::from([(
                "manifest-source".to_string(),
                "https://github.com/test/repo.git".to_string(),
            )]),
            ..Default::default()
        };
        let manifest_path = work_dir.path().join("agpm.toml");
        manifest.save(&manifest_path)?;

        // Create lockfile with additional sources
        let lockfile = LockFile {
            version: 1,
            sources: vec![
                LockedSource {
                    name: "manifest-source".to_string(),
                    url: "https://github.com/test/repo.git".to_string(),
                    fetched_at: chrono::Utc::now().to_string(),
                },
                LockedSource {
                    name: "lockfile-only".to_string(),
                    url: "https://github.com/test/other.git".to_string(),
                    fetched_at: chrono::Utc::now().to_string(),
                },
            ],
            agents: vec![],
            snippets: vec![],
            commands: vec![],
            mcp_servers: vec![],
            scripts: vec![],
            hooks: vec![],
            skills: vec![],
            manifest_hash: None,
            has_mutable_deps: None,
            resource_count: None,
        };
        lockfile.save(&work_dir.path().join("agpm.lock"))?;

        // Create cache directories
        let manifest_cache = temp_dir.path().join("manifest-source");
        let lockfile_cache = temp_dir.path().join("lockfile-only");
        let unused_cache = temp_dir.path().join("unused");
        std::fs::create_dir_all(&manifest_cache)?;
        std::fs::create_dir_all(&lockfile_cache)?;
        std::fs::create_dir_all(&unused_cache)?;

        let cmd = CacheCommand {
            command: Some(CacheSubcommands::Clean {
                all: false,
            }),
        };

        cmd.execute_with_cache_and_manifest(cache, Some(manifest_path)).await?;

        tokio::time::sleep(tokio::time::Duration::from_millis(50)).await;

        // Manifest source should be preserved
        assert!(manifest_cache.exists());
        // Note: Current implementation doesn't preserve lockfile-only sources
        // This would be a good enhancement but isn't implemented yet
        // assert!(lockfile_cache.exists());
        // Unused should be removed
        assert!(!unused_cache.exists());
        Ok(())
    }

    #[tokio::test]
    async fn test_format_size_function() {
        // Test::format_size helper function directly
        fn format_size(bytes: u64) -> String {
            const UNITS: &[&str] = &["B", "KB", "MB", "GB", "TB"];
            let mut size = bytes as f64;
            let mut unit_index = 0;

            while size >= 1024.0 && unit_index < UNITS.len() - 1 {
                size /= 1024.0;
                unit_index += 1;
            }

            if unit_index == 0 {
                format!("{} {}", size as u64, UNITS[unit_index])
            } else {
                format!("{:.1} {}", size, UNITS[unit_index])
            }
        }

        assert_eq!(format_size(0), "0 B");
        assert_eq!(format_size(100), "100 B");
        assert_eq!(format_size(1024), "1.0 KB");
        assert_eq!(format_size(1536), "1.5 KB");
        assert_eq!(format_size(1048576), "1.0 MB");
        assert_eq!(format_size(1073741824), "1.0 GB");
        assert_eq!(format_size(1099511627776), "1.0 TB");
    }

    #[tokio::test]
    async fn test_cache_path_display() -> Result<()> {
        use tempfile::TempDir;

        let temp_dir = TempDir::new()?;
        let cache = Cache::with_dir(temp_dir.path().to_path_buf())?;

        // Get cache location
        let location = cache.get_cache_location();
        assert_eq!(location, temp_dir.path());

        // Test that path displays correctly (for Info command output)
        let path_str = location.display().to_string();
        assert!(path_str.contains(temp_dir.path().file_name().unwrap().to_str().unwrap()));
        Ok(())
    }
}