|
| 1 | +Clear-Host |
| 2 | +if (-not (Get-Module -Name Pester)) { |
| 3 | + Import-Module -Name Pester -Force |
| 4 | +} |
| 5 | +Import-Module .\src\CRX.psm1 -Force |
| 6 | + |
| 7 | +Describe 'CRX.Tests' { |
| 8 | + Context 'Get-CRXUpdateInfo' { |
| 9 | + It 'Should return CRXUpdateInfo object' { |
| 10 | + $response = Get-CRXUpdateInfo "aoclhcccfdkjddgpaaajldgljhllhgmd" |
| 11 | + $response.Url | Should -Not -BeNullOrEmpty |
| 12 | + $response.Version | Should -Not -BeNullOrEmpty |
| 13 | + } |
| 14 | + |
| 15 | + It 'Should return null for invalid extension ID' { |
| 16 | + $response = Get-CRXUpdateInfo "invalid" |
| 17 | + $response | Should -BeNullOrEmpty |
| 18 | + } |
| 19 | + } |
| 20 | + |
| 21 | + Context 'Test-CRXUpdateAvailable' { |
| 22 | + It 'Should return true if update is available' { |
| 23 | + Mock -CommandName Get-CRXUpdateInfo -ModuleName CRX -MockWith { |
| 24 | + return [PSCustomObject]@{ |
| 25 | + Version = "2.0.0" |
| 26 | + Url = "http://example.com" |
| 27 | + SHA256 = "dummyhash" |
| 28 | + Status = "ok" |
| 29 | + Size = 12345 |
| 30 | + } |
| 31 | + } |
| 32 | + $result = Test-CRXUpdateAvailable -Id "aoclhcccfdkjddgpaaajldgljhllhgmd" -currentVersion "1.0.0" |
| 33 | + $result | Should -Be $true |
| 34 | + } |
| 35 | + |
| 36 | + It 'Should return false if no update is available' { |
| 37 | + Mock -CommandName Get-CRXUpdateInfo -ModuleName CRX -MockWith { |
| 38 | + return [PSCustomObject]@{ |
| 39 | + Version = "1.0.0" |
| 40 | + Url = "http://example.com" |
| 41 | + SHA256 = "dummyhash" |
| 42 | + Status = "ok" |
| 43 | + Size = 12345 |
| 44 | + } |
| 45 | + } |
| 46 | + $result = Test-CRXUpdateAvailable -Id "aoclhcccfdkjddgpaaajldgljhllhgmd" -currentVersion "1.0.0" |
| 47 | + $result | Should -Be $false |
| 48 | + } |
| 49 | + |
| 50 | + It 'Should return false if update info is null' { |
| 51 | + Mock -CommandName Get-CRXUpdateInfo -ModuleName CRX -MockWith { return $null } |
| 52 | + $result = Test-CRXUpdateAvailable -Id "aoclhcccfdkjddgpaaajldgljhllhgmd" -currentVersion "1.0.0" |
| 53 | + $result | Should -Be $false |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + Context 'Get-CRX' { |
| 58 | + It 'Should download CRX file to specified directory' { |
| 59 | + Mock -CommandName Get-CRXUpdateInfo -ModuleName CRX -MockWith { |
| 60 | + return [PSCustomObject]@{ |
| 61 | + Version = "2.0.0" |
| 62 | + Url = "http://example.com/extension.crx" |
| 63 | + SHA256 = "dummyhash" |
| 64 | + Status = "ok" |
| 65 | + Size = 12345 |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + Mock -CommandName Invoke-WebRequest -ModuleName CRX -MockWith { |
| 70 | + param ($Uri, $OutFile) |
| 71 | + New-Item -ItemType File -Path $OutFile -Force | Out-Null |
| 72 | + } |
| 73 | + |
| 74 | + $outputDir = ".\temp" |
| 75 | + $result = Get-CRX -Id "aoclhcccfdkjddgpaaajldgljhllhgmd" -OutputDirectory $outputDir |
| 76 | + |
| 77 | + $expectedPath = Join-Path -Path $outputDir -ChildPath "extension.crx" |
| 78 | + Test-Path $expectedPath | Should -Be $true |
| 79 | + $result | Should -BeOfType [System.IO.FileInfo] |
| 80 | + $result.FullName | Should -Be (Resolve-Path $expectedPath | Select-Object -ExpandProperty Path) |
| 81 | + |
| 82 | + Remove-Item -Path $outputDir -Recurse -Force |
| 83 | + } |
| 84 | + } |
| 85 | +} |
0 commit comments