|
| 1 | +using NSubstitute; |
| 2 | +using UniGetUI.Interface.Enums; |
| 3 | +using UniGetUI.PackageEngine.Interfaces; |
| 4 | +using UniGetUI.PackageEngine.PackageClasses; |
| 5 | + |
| 6 | +namespace UniGetUI.PackageEngine.Tests; |
| 7 | + |
| 8 | +/// <summary> |
| 9 | +/// Tests for duplicate detection in update operations (Issue #4131). |
| 10 | +/// These tests verify the logic used in AppOperationHelper.UpdateAll() and UpdateAllForManager() |
| 11 | +/// to prevent multiple update operations for the same package from being queued simultaneously. |
| 12 | +/// </summary> |
| 13 | +public class TestDuplicateUpdateDetection |
| 14 | +{ |
| 15 | + /// <summary> |
| 16 | + /// Test that verifies Package.GetHash() returns the same value for identical packages. |
| 17 | + /// This is the core mechanism used to detect duplicate operations in the queue. |
| 18 | + /// |
| 19 | + /// Background: Issue #4131 - When unattended updates trigger repeatedly while UAC prompts |
| 20 | + /// are pending, UpdateAll() was creating dozens of duplicate operations for the same package. |
| 21 | + /// The fix uses GetHash() to check if an operation for a package already exists in the queue. |
| 22 | + /// </summary> |
| 23 | + [Fact] |
| 24 | + public void GetHash_ShouldReturnSameValue_ForIdenticalPackages() |
| 25 | + { |
| 26 | + // Arrange - Create mock manager and source |
| 27 | + // Note: We use the same instances to ensure consistent hashing |
| 28 | + var mockManager = Substitute.For<IPackageManager>(); |
| 29 | + mockManager.Name.Returns("TestManager"); |
| 30 | + |
| 31 | + var mockSource = Substitute.For<IManagerSource>(); |
| 32 | + mockSource.Name.Returns("TestSource"); |
| 33 | + |
| 34 | + // Create two package instances with identical identity (same manager, source, and ID) |
| 35 | + var package1 = new Package( |
| 36 | + name: "TestPackage", |
| 37 | + id: "test.package.id", |
| 38 | + version: "1.0.0", |
| 39 | + source: mockSource, |
| 40 | + manager: mockManager |
| 41 | + ); |
| 42 | + |
| 43 | + var package2 = new Package( |
| 44 | + name: "TestPackage", |
| 45 | + id: "test.package.id", |
| 46 | + version: "1.0.0", |
| 47 | + source: mockSource, |
| 48 | + manager: mockManager |
| 49 | + ); |
| 50 | + |
| 51 | + // Act - Get hash for both packages |
| 52 | + long hash1 = package1.GetHash(); |
| 53 | + long hash2 = package2.GetHash(); |
| 54 | + |
| 55 | + // Assert - Hashes must be equal for the duplicate detection to work |
| 56 | + Assert.Equal(hash1, hash2); |
| 57 | + } |
| 58 | + |
| 59 | + /// <summary> |
| 60 | + /// Test that verifies Package.GetHash() returns different values for different packages. |
| 61 | + /// This ensures the duplicate detection doesn't falsely match unrelated packages. |
| 62 | + /// </summary> |
| 63 | + [Fact] |
| 64 | + public void GetHash_ShouldReturnDifferentValues_ForDifferentPackages() |
| 65 | + { |
| 66 | + // Arrange |
| 67 | + var mockManager = Substitute.For<IPackageManager>(); |
| 68 | + mockManager.Name.Returns("TestManager"); |
| 69 | + |
| 70 | + var mockSource = Substitute.For<IManagerSource>(); |
| 71 | + mockSource.Name.Returns("TestSource"); |
| 72 | + |
| 73 | + // Create packages with different IDs |
| 74 | + var package1 = new Package( |
| 75 | + name: "TestPackage1", |
| 76 | + id: "test.package.one", |
| 77 | + version: "1.0.0", |
| 78 | + source: mockSource, |
| 79 | + manager: mockManager |
| 80 | + ); |
| 81 | + |
| 82 | + var package2 = new Package( |
| 83 | + name: "TestPackage2", |
| 84 | + id: "test.package.two", |
| 85 | + version: "1.0.0", |
| 86 | + source: mockSource, |
| 87 | + manager: mockManager |
| 88 | + ); |
| 89 | + |
| 90 | + // Act |
| 91 | + long hash1 = package1.GetHash(); |
| 92 | + long hash2 = package2.GetHash(); |
| 93 | + |
| 94 | + // Assert - Hashes must be different so we don't skip legitimate different packages |
| 95 | + Assert.NotEqual(hash1, hash2); |
| 96 | + } |
| 97 | + |
| 98 | + /// <summary> |
| 99 | + /// Test that verifies packages from different managers have different hashes. |
| 100 | + /// The duplicate detection must consider the package manager as part of identity. |
| 101 | + /// </summary> |
| 102 | + [Fact] |
| 103 | + public void GetHash_ShouldDiffer_ForSamePackageFromDifferentManagers() |
| 104 | + { |
| 105 | + // Arrange - Create two different managers |
| 106 | + var mockManager1 = Substitute.For<IPackageManager>(); |
| 107 | + mockManager1.Name.Returns("WinGet"); |
| 108 | + |
| 109 | + var mockManager2 = Substitute.For<IPackageManager>(); |
| 110 | + mockManager2.Name.Returns("Chocolatey"); |
| 111 | + |
| 112 | + var mockSource = Substitute.For<IManagerSource>(); |
| 113 | + mockSource.Name.Returns("TestSource"); |
| 114 | + |
| 115 | + // Same package ID from different managers |
| 116 | + var packageFromWinGet = new Package( |
| 117 | + name: "TestPackage", |
| 118 | + id: "test.package", |
| 119 | + version: "1.0.0", |
| 120 | + source: mockSource, |
| 121 | + manager: mockManager1 |
| 122 | + ); |
| 123 | + |
| 124 | + var packageFromChoco = new Package( |
| 125 | + name: "TestPackage", |
| 126 | + id: "test.package", |
| 127 | + version: "1.0.0", |
| 128 | + source: mockSource, |
| 129 | + manager: mockManager2 |
| 130 | + ); |
| 131 | + |
| 132 | + // Act |
| 133 | + long hashWinGet = packageFromWinGet.GetHash(); |
| 134 | + long hashChoco = packageFromChoco.GetHash(); |
| 135 | + |
| 136 | + // Assert - Must be different because they're from different managers |
| 137 | + Assert.NotEqual(hashWinGet, hashChoco); |
| 138 | + } |
| 139 | + |
| 140 | + /// <summary> |
| 141 | + /// Test simulating the scenario where multiple UpdateAll() calls should detect duplicates. |
| 142 | + /// This represents what happens when auto-updates trigger while operations are queued. |
| 143 | + /// </summary> |
| 144 | + [Fact] |
| 145 | + public void SimulateDuplicateDetection_MultipleIdenticalPackages_ShouldHaveSameHash() |
| 146 | + { |
| 147 | + // Arrange - Simulate receiving same package multiple times from UpgradablePackagesLoader |
| 148 | + var mockManager = Substitute.For<IPackageManager>(); |
| 149 | + mockManager.Name.Returns("WinGet"); |
| 150 | + |
| 151 | + var mockSource = Substitute.For<IManagerSource>(); |
| 152 | + mockSource.Name.Returns("winget"); |
| 153 | + |
| 154 | + // Simulate UpdateAll() being called 3 times, each time getting "same" package |
| 155 | + var packagesFromFirstCall = new Package( |
| 156 | + name: "Git", |
| 157 | + id: "Git.Git", |
| 158 | + version: "2.40.0", |
| 159 | + source: mockSource, |
| 160 | + manager: mockManager |
| 161 | + ); |
| 162 | + |
| 163 | + var packagesFromSecondCall = new Package( |
| 164 | + name: "Git", |
| 165 | + id: "Git.Git", |
| 166 | + version: "2.40.0", |
| 167 | + source: mockSource, |
| 168 | + manager: mockManager |
| 169 | + ); |
| 170 | + |
| 171 | + var packagesFromThirdCall = new Package( |
| 172 | + name: "Git", |
| 173 | + id: "Git.Git", |
| 174 | + version: "2.40.0", |
| 175 | + source: mockSource, |
| 176 | + manager: mockManager |
| 177 | + ); |
| 178 | + |
| 179 | + // Act - Get hashes that would be used for duplicate detection |
| 180 | + var hash1 = packagesFromFirstCall.GetHash(); |
| 181 | + var hash2 = packagesFromSecondCall.GetHash(); |
| 182 | + var hash3 = packagesFromThirdCall.GetHash(); |
| 183 | + |
| 184 | + // Assert - All three should have the same hash, allowing duplicate detection |
| 185 | + Assert.Equal(hash1, hash2); |
| 186 | + Assert.Equal(hash2, hash3); |
| 187 | + Assert.Equal(hash1, hash3); |
| 188 | + |
| 189 | + // In the actual implementation, the second and third calls would find |
| 190 | + // an existing operation with matching hash and skip creating duplicates |
| 191 | + } |
| 192 | +} |
| 193 | + |
0 commit comments