microsoft/hve-core
Publicmirrored fromhttps://github.com/microsoft/hve-coreAvailable
scripts/tests/linting/Invoke-LinkLanguageCheck.Tests.ps1
279lines · modecode
| 1 | #Requires -Modules Pester |
| 2 | <# |
| 3 | .SYNOPSIS |
| 4 | Pester tests for Invoke-LinkLanguageCheck.ps1 script |
| 5 | .DESCRIPTION |
| 6 | Tests for Link Language Check wrapper script: |
| 7 | - Link-Lang-Check.ps1 invocation |
| 8 | - JSON parsing |
| 9 | - GitHub Actions integration |
| 10 | - Exit code handling |
| 11 | #> |
| 12 | |
| 13 | BeforeAll { |
| 14 | $script:ScriptPath = Join-Path $PSScriptRoot '../../linting/Invoke-LinkLanguageCheck.ps1' |
| 15 | $script:ModulePath = Join-Path $PSScriptRoot '../../linting/Modules/LintingHelpers.psm1' |
| 16 | |
| 17 | # Import LintingHelpers for mocking |
| 18 | Import-Module $script:ModulePath -Force |
| 19 | } |
| 20 | |
| 21 | AfterAll { |
| 22 | Remove-Module LintingHelpers -Force -ErrorAction SilentlyContinue |
| 23 | } |
| 24 | |
| 25 | #region Link-Lang-Check Invocation Tests |
| 26 | |
| 27 | Describe 'Link-Lang-Check.ps1 Invocation' -Tag 'Unit' { |
| 28 | Context 'Script discovery' { |
| 29 | It 'Link-Lang-Check.ps1 exists' { |
| 30 | $linkLangCheckPath = Join-Path $PSScriptRoot '../../linting/Link-Lang-Check.ps1' |
| 31 | Test-Path $linkLangCheckPath | Should -BeTrue |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | Context 'Normal execution' { |
| 36 | It 'Invoke-LinkLanguageCheck.ps1 exists' { |
| 37 | $scriptExists = Test-Path $script:ScriptPath |
| 38 | $scriptExists | Should -BeTrue |
| 39 | } |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | #endregion |
| 44 | |
| 45 | #region JSON Parsing Tests |
| 46 | |
| 47 | Describe 'JSON Output Parsing' -Tag 'Unit' { |
| 48 | Context 'Valid JSON with issues' { |
| 49 | BeforeEach { |
| 50 | $script:JsonWithIssues = @' |
| 51 | [ |
| 52 | { |
| 53 | "file": "docs/guide.md", |
| 54 | "line_number": 15, |
| 55 | "original_url": "https://docs.microsoft.com/en-us/azure" |
| 56 | }, |
| 57 | { |
| 58 | "file": "README.md", |
| 59 | "line_number": 42, |
| 60 | "original_url": "https://learn.microsoft.com/en-us/dotnet" |
| 61 | } |
| 62 | ] |
| 63 | '@ |
| 64 | } |
| 65 | |
| 66 | It 'Parses JSON array correctly' { |
| 67 | $result = $script:JsonWithIssues | ConvertFrom-Json |
| 68 | $result | Should -HaveCount 2 |
| 69 | } |
| 70 | |
| 71 | It 'Extracts file property' { |
| 72 | $result = $script:JsonWithIssues | ConvertFrom-Json |
| 73 | $result[0].file | Should -Be 'docs/guide.md' |
| 74 | } |
| 75 | |
| 76 | It 'Extracts line_number property' { |
| 77 | $result = $script:JsonWithIssues | ConvertFrom-Json |
| 78 | $result[0].line_number | Should -Be 15 |
| 79 | } |
| 80 | |
| 81 | It 'Extracts original_url property' { |
| 82 | $result = $script:JsonWithIssues | ConvertFrom-Json |
| 83 | $result[0].original_url | Should -Be 'https://docs.microsoft.com/en-us/azure' |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | Context 'Empty JSON array' { |
| 88 | It 'Handles empty array' { |
| 89 | $result = '[]' | ConvertFrom-Json |
| 90 | $result | Should -BeNullOrEmpty |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | Context 'Invalid JSON' { |
| 95 | It 'Throws on malformed JSON' { |
| 96 | { 'not valid json' | ConvertFrom-Json } | Should -Throw |
| 97 | } |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | #endregion |
| 102 | |
| 103 | #region GitHub Actions Integration Tests |
| 104 | |
| 105 | Describe 'GitHub Actions Integration' -Tag 'Unit' { |
| 106 | Context 'Module exports verification' { |
| 107 | It 'Write-GitHubAnnotation is available in module' { |
| 108 | $module = Get-Module LintingHelpers |
| 109 | $module.ExportedFunctions.Keys | Should -Contain 'Write-GitHubAnnotation' |
| 110 | } |
| 111 | |
| 112 | It 'Set-GitHubOutput is available in module' { |
| 113 | $module = Get-Module LintingHelpers |
| 114 | $module.ExportedFunctions.Keys | Should -Contain 'Set-GitHubOutput' |
| 115 | } |
| 116 | |
| 117 | It 'Write-GitHubStepSummary is available in module' { |
| 118 | $module = Get-Module LintingHelpers |
| 119 | $module.ExportedFunctions.Keys | Should -Contain 'Write-GitHubStepSummary' |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | Context 'GitHub Actions detection' { |
| 124 | It 'Detects GitHub Actions via GITHUB_ACTIONS env var' { |
| 125 | $originalValue = $env:GITHUB_ACTIONS |
| 126 | try { |
| 127 | $env:GITHUB_ACTIONS = 'true' |
| 128 | $env:GITHUB_ACTIONS | Should -Be 'true' |
| 129 | |
| 130 | $env:GITHUB_ACTIONS = $null |
| 131 | $env:GITHUB_ACTIONS | Should -BeNullOrEmpty |
| 132 | } |
| 133 | finally { |
| 134 | $env:GITHUB_ACTIONS = $originalValue |
| 135 | } |
| 136 | } |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | #endregion |
| 141 | |
| 142 | #region Annotation Generation Tests |
| 143 | |
| 144 | Describe 'Annotation Generation' -Tag 'Unit' { |
| 145 | Context 'Annotation content' { |
| 146 | BeforeEach { |
| 147 | $script:Issue = [PSCustomObject]@{ |
| 148 | file = 'docs/test.md' |
| 149 | line_number = 25 |
| 150 | original_url = 'https://docs.microsoft.com/en-us/azure/overview' |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | It 'Issue object has required properties' { |
| 155 | $script:Issue.file | Should -Not -BeNullOrEmpty |
| 156 | $script:Issue.line_number | Should -BeGreaterThan 0 |
| 157 | $script:Issue.original_url | Should -Match 'en-us' |
| 158 | } |
| 159 | |
| 160 | It 'File path is workspace-relative' { |
| 161 | $script:Issue.file | Should -Not -Match '^[A-Z]:\\' |
| 162 | $script:Issue.file | Should -Not -Match '^/' |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | Context 'Annotation severity mapping' { |
| 167 | It 'Language path issues are warnings' { |
| 168 | # Link language issues are warnings, not errors |
| 169 | $severity = 'warning' |
| 170 | $severity | Should -Be 'warning' |
| 171 | } |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | #endregion |
| 176 | |
| 177 | #region Exit Code Tests |
| 178 | |
| 179 | Describe 'Exit Code Handling' -Tag 'Unit' { |
| 180 | Context 'No issues found' { |
| 181 | It 'Empty result indicates success' { |
| 182 | $issues = @() |
| 183 | $issues.Count | Should -Be 0 |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | Context 'Issues found' { |
| 188 | BeforeEach { |
| 189 | $script:Issues = @( |
| 190 | [PSCustomObject]@{ file = 'test.md'; line_number = 1; original_url = 'https://example.com/en-us/page' } |
| 191 | ) |
| 192 | } |
| 193 | |
| 194 | It 'Non-empty result indicates issues present' { |
| 195 | $script:Issues.Count | Should -BeGreaterThan 0 |
| 196 | } |
| 197 | |
| 198 | It 'Script should warn but not fail on issues' { |
| 199 | # Link language issues are warnings, script continues |
| 200 | $warningExpected = $true |
| 201 | $warningExpected | Should -BeTrue |
| 202 | } |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | #endregion |
| 207 | |
| 208 | #region Output Format Tests |
| 209 | |
| 210 | Describe 'Output Format' -Tag 'Unit' { |
| 211 | Context 'Console output' { |
| 212 | BeforeEach { |
| 213 | $script:SampleIssue = [PSCustomObject]@{ |
| 214 | file = 'README.md' |
| 215 | line_number = 10 |
| 216 | original_url = 'https://docs.microsoft.com/en-us/azure' |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | It 'Issue can be formatted as string' { |
| 221 | $formatted = "[$($script:SampleIssue.file):$($script:SampleIssue.line_number)] $($script:SampleIssue.original_url)" |
| 222 | $formatted | Should -Be '[README.md:10] https://docs.microsoft.com/en-us/azure' |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | Context 'Summary statistics' { |
| 227 | BeforeEach { |
| 228 | $script:Issues = @( |
| 229 | [PSCustomObject]@{ file = 'a.md'; line_number = 1; original_url = 'url1' }, |
| 230 | [PSCustomObject]@{ file = 'a.md'; line_number = 2; original_url = 'url2' }, |
| 231 | [PSCustomObject]@{ file = 'b.md'; line_number = 1; original_url = 'url3' } |
| 232 | ) |
| 233 | } |
| 234 | |
| 235 | It 'Can count total issues' { |
| 236 | $script:Issues.Count | Should -Be 3 |
| 237 | } |
| 238 | |
| 239 | It 'Can count affected files' { |
| 240 | $fileCount = ($script:Issues | Select-Object -ExpandProperty file -Unique).Count |
| 241 | $fileCount | Should -Be 2 |
| 242 | } |
| 243 | } |
| 244 | } |
| 245 | |
| 246 | #endregion |
| 247 | |
| 248 | #region Integration with Link-Lang-Check Tests |
| 249 | |
| 250 | Describe 'Link-Lang-Check Integration' -Tag 'Integration' { |
| 251 | Context 'Script dependencies' { |
| 252 | It 'LintingHelpers module can be imported' { |
| 253 | { Import-Module $script:ModulePath -Force } | Should -Not -Throw |
| 254 | } |
| 255 | |
| 256 | It 'Link-Lang-Check.ps1 exists at expected path' { |
| 257 | $linkLangCheckPath = Join-Path $PSScriptRoot '../../linting/Link-Lang-Check.ps1' |
| 258 | Test-Path $linkLangCheckPath | Should -BeTrue |
| 259 | } |
| 260 | } |
| 261 | |
| 262 | Context 'Output compatibility' { |
| 263 | It 'Link-Lang-Check output can be parsed as JSON' { |
| 264 | # Sample output format from Link-Lang-Check.ps1 |
| 265 | $sampleOutput = '[{"file":"test.md","line_number":1,"original_url":"https://example.com/en-us/page"}]' |
| 266 | { $sampleOutput | ConvertFrom-Json } | Should -Not -Throw |
| 267 | } |
| 268 | |
| 269 | It 'Parsed output has expected structure' { |
| 270 | $sampleOutput = '[{"file":"test.md","line_number":1,"original_url":"https://example.com/en-us/page"}]' |
| 271 | $parsed = $sampleOutput | ConvertFrom-Json |
| 272 | $parsed[0].PSObject.Properties.Name | Should -Contain 'file' |
| 273 | $parsed[0].PSObject.Properties.Name | Should -Contain 'line_number' |
| 274 | $parsed[0].PSObject.Properties.Name | Should -Contain 'original_url' |
| 275 | } |
| 276 | } |
| 277 | } |
| 278 | |
| 279 | #endregion |
| 280 | |