|
| 1 | +# Run tests: |
| 2 | +# Install-Module -Name Pester -Force -SkipPublisherCheck |
| 3 | +# Invoke-Pester -Passthru $PSScriptRoot/Verify-Links.Tests.ps1 |
| 4 | + |
| 5 | +BeforeAll { |
| 6 | + # Load only the functions we need by dot-sourcing the script with a dummy param block. |
| 7 | + # We source the script file and then override Invoke-RestMethod / Invoke-WebRequest |
| 8 | + # inside each test via Mock so no real network calls are made. |
| 9 | + |
| 10 | + # Stub out logging functions that the script depends on but are defined in logging.ps1 |
| 11 | + function LogWarning($msg) { Write-Warning $msg } |
| 12 | + function LogError($msg) { Write-Error $msg } |
| 13 | + function LogGroupStart($msg) {} |
| 14 | + function LogGroupEnd {} |
| 15 | + |
| 16 | + # Source only the function definitions (stop before the script-body runs) |
| 17 | + # by dot-sourcing within a script block that replaces the parameter-driven |
| 18 | + # body with a no-op after loading. |
| 19 | + $scriptPath = (Resolve-Path "$PSScriptRoot/../Verify-Links.ps1").Path |
| 20 | + |
| 21 | + # Extract and invoke only the function definitions by parsing the AST |
| 22 | + $ast = [System.Management.Automation.Language.Parser]::ParseFile($scriptPath, [ref]$null, [ref]$null) |
| 23 | + $functionDefs = $ast.FindAll({ $args[0] -is [System.Management.Automation.Language.FunctionDefinitionAst] }, $false) |
| 24 | + foreach ($fn in $functionDefs) { |
| 25 | + Invoke-Expression $fn.Extent.Text |
| 26 | + } |
| 27 | + |
| 28 | + # Script-scope variables referenced inside the functions |
| 29 | + $script:userAgent = "TestAgent/1.0" |
| 30 | + $script:requestTimeoutSec = 15 |
| 31 | +} |
| 32 | + |
| 33 | +Describe "ProcessNpmLink" { |
| 34 | + Context "Unparseable URL" { |
| 35 | + It "Returns true and skips verification for a URL that does not match npmjs.com package pattern" { |
| 36 | + $result = ProcessNpmLink ([System.Uri]"https://npmjs.com/browse/keyword/azure") |
| 37 | + $result | Should -Be $true |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + Context "Non-versioned URL - package found in ADO upstream" { |
| 42 | + BeforeEach { |
| 43 | + Mock Invoke-RestMethod { |
| 44 | + return @{ |
| 45 | + value = @( |
| 46 | + @{ version = "1.0.0" }, |
| 47 | + @{ version = "2.0.0" } |
| 48 | + ) |
| 49 | + } |
| 50 | + } -ParameterFilter { $Uri -like "*upstreamVersions*" } |
| 51 | + } |
| 52 | + |
| 53 | + It "Returns true when package has upstream versions" { |
| 54 | + $result = ProcessNpmLink ([System.Uri]"https://www.npmjs.com/package/@azure/ai-agents") |
| 55 | + $result | Should -Be $true |
| 56 | + } |
| 57 | + |
| 58 | + It "Encodes scoped package name correctly in the API URL" { |
| 59 | + ProcessNpmLink ([System.Uri]"https://www.npmjs.com/package/@azure/ai-agents") | Out-Null |
| 60 | + Should -Invoke Invoke-RestMethod -ParameterFilter { |
| 61 | + $Uri -like "*%40azure%2Fai-agents*" |
| 62 | + } -Times 1 -Exactly |
| 63 | + } |
| 64 | + |
| 65 | + It "Calls the ADO feed upstream API, not registry.npmjs.org or npmjs.com" { |
| 66 | + ProcessNpmLink ([System.Uri]"https://www.npmjs.com/package/@azure/core-http") | Out-Null |
| 67 | + Should -Invoke Invoke-RestMethod -ParameterFilter { |
| 68 | + $Uri -like "https://pkgs.dev.azure.com/azure-sdk/public/_apis/packaging/feeds/azure-sdk-for-js/npm/packages/*" |
| 69 | + } -Times 1 -Exactly |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + Context "Non-versioned URL - package not found (no upstream versions)" { |
| 74 | + BeforeEach { |
| 75 | + Mock Invoke-RestMethod { |
| 76 | + return @{ value = @() } |
| 77 | + } -ParameterFilter { $Uri -like "*upstreamVersions*" } |
| 78 | + } |
| 79 | + |
| 80 | + It "Returns false when the package has no upstream versions" { |
| 81 | + $result = ProcessNpmLink ([System.Uri]"https://www.npmjs.com/package/@azure/nonexistent-package") |
| 82 | + $result | Should -Be $false |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + Context "Non-versioned URL - package not found (ADO returns 404)" { |
| 87 | + BeforeEach { |
| 88 | + Mock Invoke-RestMethod { |
| 89 | + $response = [System.Net.HttpWebResponse]::new.Invoke(@()) |
| 90 | + throw [System.Net.WebException]::new( |
| 91 | + "The remote server returned an error: (404) Not Found.", |
| 92 | + $null, |
| 93 | + [System.Net.WebExceptionStatus]::ProtocolError, |
| 94 | + $null |
| 95 | + ) |
| 96 | + } -ParameterFilter { $Uri -like "*upstreamVersions*" } |
| 97 | + } |
| 98 | + |
| 99 | + It "Propagates exception (to be handled by CheckLink caller)" { |
| 100 | + { ProcessNpmLink ([System.Uri]"https://www.npmjs.com/package/@azure/nonexistent-package") } | Should -Throw |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + Context "Versioned URL - correct version present in ADO upstream" { |
| 105 | + BeforeEach { |
| 106 | + Mock Invoke-RestMethod { |
| 107 | + return @{ |
| 108 | + value = @( |
| 109 | + @{ version = "1.0.0" }, |
| 110 | + @{ version = "1.1.0" }, |
| 111 | + @{ version = "2.0.0" } |
| 112 | + ) |
| 113 | + } |
| 114 | + } -ParameterFilter { $Uri -like "*upstreamVersions*" } |
| 115 | + } |
| 116 | + |
| 117 | + It "Returns true when the specific version exists in upstream" { |
| 118 | + $result = ProcessNpmLink ([System.Uri]"https://www.npmjs.com/package/@azure/ai-agents/v/1.1.0") |
| 119 | + $result | Should -Be $true |
| 120 | + } |
| 121 | + |
| 122 | + It "Encodes scoped package name correctly for versioned URLs" { |
| 123 | + ProcessNpmLink ([System.Uri]"https://www.npmjs.com/package/@azure/ai-agents/v/1.1.0") | Out-Null |
| 124 | + Should -Invoke Invoke-RestMethod -ParameterFilter { |
| 125 | + $Uri -like "*%40azure%2Fai-agents*" |
| 126 | + } -Times 1 -Exactly |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + Context "Versioned URL - version not present in ADO upstream" { |
| 131 | + BeforeEach { |
| 132 | + Mock Invoke-RestMethod { |
| 133 | + return @{ |
| 134 | + value = @( |
| 135 | + @{ version = "1.0.0" }, |
| 136 | + @{ version = "2.0.0" } |
| 137 | + ) |
| 138 | + } |
| 139 | + } -ParameterFilter { $Uri -like "*upstreamVersions*" } |
| 140 | + } |
| 141 | + |
| 142 | + It "Returns false when the specific version does not exist in upstream" { |
| 143 | + $result = ProcessNpmLink ([System.Uri]"https://www.npmjs.com/package/@azure/ai-agents/v/9.9.9") |
| 144 | + $result | Should -Be $false |
| 145 | + } |
| 146 | + } |
| 147 | + |
| 148 | + Context "Versioned URL - unscoped package" { |
| 149 | + BeforeEach { |
| 150 | + Mock Invoke-RestMethod { |
| 151 | + return @{ |
| 152 | + value = @( |
| 153 | + @{ version = "3.2.1" } |
| 154 | + ) |
| 155 | + } |
| 156 | + } -ParameterFilter { $Uri -like "*upstreamVersions*" } |
| 157 | + } |
| 158 | + |
| 159 | + It "Handles unscoped package names in versioned URLs" { |
| 160 | + $result = ProcessNpmLink ([System.Uri]"https://www.npmjs.com/package/typescript/v/3.2.1") |
| 161 | + $result | Should -Be $true |
| 162 | + } |
| 163 | + |
| 164 | + It "Returns false when unscoped versioned package version is missing" { |
| 165 | + $result = ProcessNpmLink ([System.Uri]"https://www.npmjs.com/package/typescript/v/99.0.0") |
| 166 | + $result | Should -Be $false |
| 167 | + } |
| 168 | + } |
| 169 | + |
| 170 | + Context "Query parameters and fragments are excluded from package name" { |
| 171 | + BeforeEach { |
| 172 | + Mock Invoke-RestMethod { |
| 173 | + return @{ |
| 174 | + value = @( |
| 175 | + @{ version = "1.0.0" } |
| 176 | + ) |
| 177 | + } |
| 178 | + } -ParameterFilter { $Uri -like "*upstreamVersions*" } |
| 179 | + } |
| 180 | + |
| 181 | + It "Strips query string from non-versioned URL when extracting package name" { |
| 182 | + $result = ProcessNpmLink ([System.Uri]"https://www.npmjs.com/package/@azure/ai-agents?activeTab=readme") |
| 183 | + $result | Should -Be $true |
| 184 | + Should -Invoke Invoke-RestMethod -ParameterFilter { |
| 185 | + $Uri -like "*%40azure%2Fai-agents*" -and $Uri -notlike "*activeTab*" |
| 186 | + } -Times 1 -Exactly |
| 187 | + } |
| 188 | + } |
| 189 | +} |
0 commit comments