|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +using WinApp.Cli.Commands; |
| 5 | +using WinApp.Cli.Services; |
| 6 | + |
| 7 | +namespace WinApp.Cli.Tests; |
| 8 | + |
| 9 | +[TestClass] |
| 10 | +public class CacheCommandTests : BaseCommandTests |
| 11 | +{ |
| 12 | + private ICacheService _cacheService = null!; |
| 13 | + private IWinappDirectoryService _winappDirectoryService = null!; |
| 14 | + |
| 15 | + [TestInitialize] |
| 16 | + public void Setup() |
| 17 | + { |
| 18 | + _cacheService = GetRequiredService<ICacheService>(); |
| 19 | + _winappDirectoryService = GetRequiredService<IWinappDirectoryService>(); |
| 20 | + } |
| 21 | + |
| 22 | + [TestMethod] |
| 23 | + public void GetCacheDirectory_ReturnsDefaultLocation_WhenNoCustomLocationSet() |
| 24 | + { |
| 25 | + // Act |
| 26 | + var cacheDir = _cacheService.GetCacheDirectory(); |
| 27 | + var customLocation = _cacheService.GetCustomCacheLocation(); |
| 28 | + |
| 29 | + // Assert |
| 30 | + Assert.IsNotNull(cacheDir); |
| 31 | + Assert.IsNull(customLocation); |
| 32 | + Assert.IsTrue(cacheDir.FullName.Contains("packages")); |
| 33 | + } |
| 34 | + |
| 35 | + [TestMethod] |
| 36 | + public void GetCacheDirectory_ReturnsCustomLocation_WhenSet() |
| 37 | + { |
| 38 | + // Arrange |
| 39 | + var customPath = Path.Combine(_tempDirectory.FullName, "custom-cache"); |
| 40 | + |
| 41 | + // Act |
| 42 | + _cacheService.SetCustomCacheLocation(customPath); |
| 43 | + var cacheDir = _cacheService.GetCacheDirectory(); |
| 44 | + var customLocation = _cacheService.GetCustomCacheLocation(); |
| 45 | + |
| 46 | + // Assert |
| 47 | + Assert.IsNotNull(cacheDir); |
| 48 | + Assert.IsNotNull(customLocation); |
| 49 | + Assert.AreEqual(customPath, customLocation); |
| 50 | + Assert.AreEqual(customPath, cacheDir.FullName); |
| 51 | + } |
| 52 | + |
| 53 | + [TestMethod] |
| 54 | + public void SetCustomCacheLocation_CreatesConfigFile() |
| 55 | + { |
| 56 | + // Arrange |
| 57 | + var customPath = Path.Combine(_tempDirectory.FullName, "custom-cache"); |
| 58 | + |
| 59 | + // Act |
| 60 | + _cacheService.SetCustomCacheLocation(customPath); |
| 61 | + |
| 62 | + // Assert |
| 63 | + var configFile = Path.Combine(_testWinappDirectory.FullName, "cache-config.json"); |
| 64 | + Assert.IsTrue(File.Exists(configFile), "Config file should be created"); |
| 65 | + |
| 66 | + var configContent = File.ReadAllText(configFile); |
| 67 | + Assert.IsTrue(configContent.Contains(customPath), "Config should contain custom path"); |
| 68 | + } |
| 69 | + |
| 70 | + [TestMethod] |
| 71 | + public void RemoveCustomCacheLocation_DeletesConfigFile() |
| 72 | + { |
| 73 | + // Arrange |
| 74 | + var customPath = Path.Combine(_tempDirectory.FullName, "custom-cache"); |
| 75 | + _cacheService.SetCustomCacheLocation(customPath); |
| 76 | + var configFile = Path.Combine(_testWinappDirectory.FullName, "cache-config.json"); |
| 77 | + Assert.IsTrue(File.Exists(configFile), "Config file should exist"); |
| 78 | + |
| 79 | + // Act |
| 80 | + _cacheService.RemoveCustomCacheLocation(); |
| 81 | + |
| 82 | + // Assert |
| 83 | + Assert.IsFalse(File.Exists(configFile), "Config file should be deleted"); |
| 84 | + } |
| 85 | + |
| 86 | + [TestMethod] |
| 87 | + public async Task MoveCacheAsync_ThrowsException_WhenPathIsEmpty() |
| 88 | + { |
| 89 | + // Act & Assert |
| 90 | + await Assert.ThrowsExactlyAsync<ArgumentException>(async () => |
| 91 | + { |
| 92 | + await _cacheService.MoveCacheAsync(""); |
| 93 | + }); |
| 94 | + } |
| 95 | + |
| 96 | + [TestMethod] |
| 97 | + public async Task MoveCacheAsync_ThrowsException_WhenTargetDirectoryIsNotEmpty() |
| 98 | + { |
| 99 | + // Arrange |
| 100 | + var targetDir = _tempDirectory.CreateSubdirectory("target"); |
| 101 | + File.WriteAllText(Path.Combine(targetDir.FullName, "test.txt"), "test"); |
| 102 | + |
| 103 | + // Act & Assert |
| 104 | + await Assert.ThrowsExactlyAsync<InvalidOperationException>(async () => |
| 105 | + { |
| 106 | + await _cacheService.MoveCacheAsync(targetDir.FullName); |
| 107 | + }); |
| 108 | + } |
| 109 | + |
| 110 | + [TestMethod] |
| 111 | + public async Task MoveCacheAsync_MovesExistingCache_ToNewLocation() |
| 112 | + { |
| 113 | + // Arrange |
| 114 | + var defaultCacheDir = _winappDirectoryService.GetPackagesDirectory(); |
| 115 | + defaultCacheDir.Create(); |
| 116 | + |
| 117 | + // Create some test content in the default cache |
| 118 | + var testPackageDir = defaultCacheDir.CreateSubdirectory("TestPackage.1.0.0"); |
| 119 | + File.WriteAllText(Path.Combine(testPackageDir.FullName, "test.txt"), "test content"); |
| 120 | + |
| 121 | + var targetDir = _tempDirectory.CreateSubdirectory("new-cache-location"); |
| 122 | + targetDir.Delete(); // Delete so MoveCacheAsync will be prompted to create it |
| 123 | + |
| 124 | + // Note: This test won't prompt because Program.PromptYesNo is not mocked |
| 125 | + // For a real scenario, we'd need to mock the prompt |
| 126 | + |
| 127 | + // For now, let's just test with an existing empty directory |
| 128 | + targetDir.Create(); |
| 129 | + |
| 130 | + // Act |
| 131 | + await _cacheService.MoveCacheAsync(targetDir.FullName); |
| 132 | + |
| 133 | + // Assert |
| 134 | + var movedPackageDir = new DirectoryInfo(Path.Combine(targetDir.FullName, "TestPackage.1.0.0")); |
| 135 | + Assert.IsTrue(movedPackageDir.Exists, "Package directory should be moved"); |
| 136 | + Assert.IsTrue(File.Exists(Path.Combine(movedPackageDir.FullName, "test.txt")), "Package file should be moved"); |
| 137 | + Assert.IsFalse(testPackageDir.Exists, "Original package directory should be removed"); |
| 138 | + |
| 139 | + var customLocation = _cacheService.GetCustomCacheLocation(); |
| 140 | + Assert.AreEqual(targetDir.FullName, customLocation, "Custom location should be set"); |
| 141 | + } |
| 142 | + |
| 143 | + [TestMethod] |
| 144 | + public async Task MoveCacheAsync_SetsCustomLocation_WhenCacheDoesNotExist() |
| 145 | + { |
| 146 | + // Arrange |
| 147 | + var defaultCacheDir = _winappDirectoryService.GetPackagesDirectory(); |
| 148 | + Assert.IsFalse(defaultCacheDir.Exists, "Default cache should not exist"); |
| 149 | + |
| 150 | + var targetDir = _tempDirectory.CreateSubdirectory("new-cache-location"); |
| 151 | + |
| 152 | + // Act |
| 153 | + await _cacheService.MoveCacheAsync(targetDir.FullName); |
| 154 | + |
| 155 | + // Assert |
| 156 | + var customLocation = _cacheService.GetCustomCacheLocation(); |
| 157 | + Assert.AreEqual(targetDir.FullName, customLocation, "Custom location should be set"); |
| 158 | + } |
| 159 | + |
| 160 | + [TestMethod] |
| 161 | + public async Task ClearCacheAsync_RemovesAllContent_FromCacheDirectory() |
| 162 | + { |
| 163 | + // Arrange |
| 164 | + var cacheDir = _winappDirectoryService.GetPackagesDirectory(); |
| 165 | + cacheDir.Create(); |
| 166 | + |
| 167 | + var testPackage1 = cacheDir.CreateSubdirectory("Package1.1.0.0"); |
| 168 | + File.WriteAllText(Path.Combine(testPackage1.FullName, "test1.txt"), "test1"); |
| 169 | + |
| 170 | + var testPackage2 = cacheDir.CreateSubdirectory("Package2.2.0.0"); |
| 171 | + File.WriteAllText(Path.Combine(testPackage2.FullName, "test2.txt"), "test2"); |
| 172 | + |
| 173 | + Assert.AreEqual(2, cacheDir.GetDirectories().Length, "Should have 2 packages"); |
| 174 | + |
| 175 | + // Act |
| 176 | + await _cacheService.ClearCacheAsync(); |
| 177 | + |
| 178 | + // Assert |
| 179 | + cacheDir.Refresh(); |
| 180 | + Assert.AreEqual(0, cacheDir.GetFileSystemInfos().Length, "Cache should be empty"); |
| 181 | + } |
| 182 | + |
| 183 | + [TestMethod] |
| 184 | + public async Task ClearCacheAsync_DoesNotThrow_WhenCacheDoesNotExist() |
| 185 | + { |
| 186 | + // Arrange |
| 187 | + var cacheDir = _winappDirectoryService.GetPackagesDirectory(); |
| 188 | + Assert.IsFalse(cacheDir.Exists, "Cache should not exist"); |
| 189 | + |
| 190 | + // Act & Assert - should not throw |
| 191 | + await _cacheService.ClearCacheAsync(); |
| 192 | + } |
| 193 | + |
| 194 | + [TestMethod] |
| 195 | + public void GetPackagesDirectory_ReturnsCustomLocation_WhenConfigured() |
| 196 | + { |
| 197 | + // Arrange |
| 198 | + var customPath = Path.Combine(_tempDirectory.FullName, "custom-packages"); |
| 199 | + _cacheService.SetCustomCacheLocation(customPath); |
| 200 | + |
| 201 | + // Act |
| 202 | + var packagesDir = _winappDirectoryService.GetPackagesDirectory(); |
| 203 | + |
| 204 | + // Assert |
| 205 | + Assert.AreEqual(customPath, packagesDir.FullName); |
| 206 | + } |
| 207 | + |
| 208 | + [TestMethod] |
| 209 | + public void GetPackagesDirectory_ReturnsDefaultLocation_WhenNotConfigured() |
| 210 | + { |
| 211 | + // Act |
| 212 | + var packagesDir = _winappDirectoryService.GetPackagesDirectory(); |
| 213 | + |
| 214 | + // Assert |
| 215 | + Assert.IsTrue(packagesDir.FullName.EndsWith("packages")); |
| 216 | + Assert.IsTrue(packagesDir.FullName.Contains(_testWinappDirectory.FullName)); |
| 217 | + } |
| 218 | + |
| 219 | + [TestMethod] |
| 220 | + public void CacheCommand_Integration_GetPath() |
| 221 | + { |
| 222 | + // Arrange |
| 223 | + var command = GetRequiredService<CacheGetPathCommand>(); |
| 224 | + var handler = GetRequiredService<CacheGetPathCommand.Handler>(); |
| 225 | + |
| 226 | + // Act |
| 227 | + var result = handler.InvokeAsync(command.Parse(Array.Empty<string>())).Result; |
| 228 | + |
| 229 | + // Assert |
| 230 | + Assert.AreEqual(0, result); |
| 231 | + var output = ConsoleStdOut.ToString(); |
| 232 | + StringAssert.Contains(output, "Package cache location", "Output should contain cache location message"); |
| 233 | + } |
| 234 | + |
| 235 | + [TestMethod] |
| 236 | + public async Task CacheCommand_Integration_Clear() |
| 237 | + { |
| 238 | + // Arrange |
| 239 | + var cacheDir = _winappDirectoryService.GetPackagesDirectory(); |
| 240 | + cacheDir.Create(); |
| 241 | + var testFile = Path.Combine(cacheDir.FullName, "test.txt"); |
| 242 | + File.WriteAllText(testFile, "test"); |
| 243 | + |
| 244 | + var command = GetRequiredService<CacheClearCommand>(); |
| 245 | + var handler = GetRequiredService<CacheClearCommand.Handler>(); |
| 246 | + |
| 247 | + // Mock the prompt to return yes - this is a limitation of the current test |
| 248 | + // In a real scenario, we'd need to refactor to make prompting testable |
| 249 | + |
| 250 | + // For now, just verify the command is registered |
| 251 | + Assert.IsNotNull(command); |
| 252 | + Assert.IsNotNull(handler); |
| 253 | + } |
| 254 | +} |
0 commit comments