microsoft/hve-core

Public

mirrored fromhttps://github.com/microsoft/hve-coreAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
feat/add-pester-code-coverage

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

scripts/tests/extension/Prepare-Extension.Tests.ps1

278lines · modecode

1#Requires -Modules Pester
2
3BeforeAll {
4 . $PSScriptRoot/../../extension/Prepare-Extension.ps1
5 # Check for ConvertFrom-Yaml availability (PowerShell-Yaml module)
6 $script:hasYamlSupport = $null -ne (Get-Command ConvertFrom-Yaml -ErrorAction SilentlyContinue)
7}
8
9Describe 'Get-AllowedMaturities' {
10 It 'Returns only stable for Stable channel' {
11 $result = Get-AllowedMaturities -Channel 'Stable'
12 $result | Should -Be @('stable')
13 }
14
15 It 'Returns all maturities for PreRelease channel' {
16 $result = Get-AllowedMaturities -Channel 'PreRelease'
17 $result | Should -Contain 'stable'
18 $result | Should -Contain 'preview'
19 $result | Should -Contain 'experimental'
20 }
21
22}
23
24Describe 'Get-FrontmatterData' {
25 BeforeAll {
26 $script:tempDir = Join-Path ([System.IO.Path]::GetTempPath()) ([System.Guid]::NewGuid().ToString())
27 New-Item -ItemType Directory -Path $script:tempDir -Force | Out-Null
28 }
29
30 AfterAll {
31 Remove-Item -Path $script:tempDir -Recurse -Force -ErrorAction SilentlyContinue
32 }
33
34 It 'Extracts description and maturity from frontmatter' {
35 if (-not $script:hasYamlSupport) {
36 Set-ItResult -Skipped -Because 'ConvertFrom-Yaml (PowerShell-Yaml module) not available'
37 return
38 }
39 $testFile = Join-Path $script:tempDir 'test.md'
40 @'
41---
42description: "Test description"
43maturity: preview
44---
45# Content
46'@ | Set-Content -Path $testFile
47
48 $result = Get-FrontmatterData -FilePath $testFile -FallbackDescription 'fallback'
49 $result.description | Should -Be 'Test description'
50 $result.maturity | Should -Be 'preview'
51 }
52
53 It 'Uses fallback description when not in frontmatter' {
54 $testFile = Join-Path $script:tempDir 'no-desc.md'
55 @'
56---
57maturity: stable
58---
59# Content
60'@ | Set-Content -Path $testFile
61
62 $result = Get-FrontmatterData -FilePath $testFile -FallbackDescription 'My Fallback'
63 $result.description | Should -Be 'My Fallback'
64 }
65
66 It 'Defaults maturity to stable when not specified' {
67 $testFile = Join-Path $script:tempDir 'no-maturity.md'
68 @'
69---
70description: "Desc"
71---
72# Content
73'@ | Set-Content -Path $testFile
74
75 $result = Get-FrontmatterData -FilePath $testFile -FallbackDescription 'fallback'
76 $result.maturity | Should -Be 'stable'
77 }
78}
79
80Describe 'Test-PathsExist' {
81 BeforeAll {
82 $script:tempDir = Join-Path ([System.IO.Path]::GetTempPath()) ([System.Guid]::NewGuid().ToString())
83 New-Item -ItemType Directory -Path $script:tempDir -Force | Out-Null
84 $script:extDir = Join-Path $script:tempDir 'extension'
85 $script:ghDir = Join-Path $script:tempDir '.github'
86 New-Item -ItemType Directory -Path $script:extDir -Force | Out-Null
87 New-Item -ItemType Directory -Path $script:ghDir -Force | Out-Null
88 $script:pkgJson = Join-Path $script:extDir 'package.json'
89 '{}' | Set-Content -Path $script:pkgJson
90 }
91
92 AfterAll {
93 Remove-Item -Path $script:tempDir -Recurse -Force -ErrorAction SilentlyContinue
94 }
95
96 It 'Returns valid when all paths exist' {
97 $result = Test-PathsExist -ExtensionDir $script:extDir -PackageJsonPath $script:pkgJson -GitHubDir $script:ghDir
98 $result.IsValid | Should -BeTrue
99 $result.MissingPaths | Should -BeNullOrEmpty
100 }
101
102 It 'Returns invalid when extension dir missing' {
103 $nonexistentPath = [System.IO.Path]::Combine([System.IO.Path]::GetTempPath(), 'nonexistent-ext-dir-12345')
104 $result = Test-PathsExist -ExtensionDir $nonexistentPath -PackageJsonPath $script:pkgJson -GitHubDir $script:ghDir
105 $result.IsValid | Should -BeFalse
106 $result.MissingPaths | Should -Contain $nonexistentPath
107 }
108
109 It 'Collects multiple missing paths' {
110 $missing1 = [System.IO.Path]::Combine([System.IO.Path]::GetTempPath(), 'missing-path-1')
111 $missing2 = [System.IO.Path]::Combine([System.IO.Path]::GetTempPath(), 'missing-path-2')
112 $missing3 = [System.IO.Path]::Combine([System.IO.Path]::GetTempPath(), 'missing-path-3')
113 $result = Test-PathsExist -ExtensionDir $missing1 -PackageJsonPath $missing2 -GitHubDir $missing3
114 $result.IsValid | Should -BeFalse
115 $result.MissingPaths.Count | Should -Be 3
116 }
117}
118
119Describe 'Get-DiscoveredAgents' {
120 BeforeAll {
121 $script:tempDir = Join-Path ([System.IO.Path]::GetTempPath()) ([System.Guid]::NewGuid().ToString())
122 $script:agentsDir = Join-Path $script:tempDir 'agents'
123 New-Item -ItemType Directory -Path $script:agentsDir -Force | Out-Null
124
125 # Create test agent files
126 @'
127---
128description: "Stable agent"
129maturity: stable
130---
131'@ | Set-Content -Path (Join-Path $script:agentsDir 'stable.agent.md')
132
133 @'
134---
135description: "Preview agent"
136maturity: preview
137---
138'@ | Set-Content -Path (Join-Path $script:agentsDir 'preview.agent.md')
139 }
140
141 AfterAll {
142 Remove-Item -Path $script:tempDir -Recurse -Force -ErrorAction SilentlyContinue
143 }
144
145 It 'Discovers agents matching allowed maturities' {
146 $result = Get-DiscoveredAgents -AgentsDir $script:agentsDir -AllowedMaturities @('stable', 'preview') -ExcludedAgents @()
147 $result.DirectoryExists | Should -BeTrue
148 $result.Agents.Count | Should -Be 2
149 }
150
151 It 'Filters agents by maturity' {
152 if (-not $script:hasYamlSupport) {
153 Set-ItResult -Skipped -Because 'ConvertFrom-Yaml (PowerShell-Yaml module) not available'
154 return
155 }
156 $result = Get-DiscoveredAgents -AgentsDir $script:agentsDir -AllowedMaturities @('stable') -ExcludedAgents @()
157 $result.Agents.Count | Should -Be 1
158 $result.Skipped.Count | Should -Be 1
159 }
160
161 It 'Excludes specified agents' {
162 $result = Get-DiscoveredAgents -AgentsDir $script:agentsDir -AllowedMaturities @('stable', 'preview') -ExcludedAgents @('stable')
163 $result.Agents.Count | Should -Be 1
164 }
165
166 It 'Returns empty when directory does not exist' {
167 $nonexistentPath = [System.IO.Path]::Combine([System.IO.Path]::GetTempPath(), 'nonexistent-agents-dir-12345')
168 $result = Get-DiscoveredAgents -AgentsDir $nonexistentPath -AllowedMaturities @('stable') -ExcludedAgents @()
169 $result.DirectoryExists | Should -BeFalse
170 $result.Agents | Should -BeNullOrEmpty
171 }
172}
173
174Describe 'Get-DiscoveredPrompts' {
175 BeforeAll {
176 $script:tempDir = Join-Path ([System.IO.Path]::GetTempPath()) ([System.Guid]::NewGuid().ToString())
177 $script:promptsDir = Join-Path $script:tempDir 'prompts'
178 $script:ghDir = Join-Path $script:tempDir '.github'
179 New-Item -ItemType Directory -Path $script:promptsDir -Force | Out-Null
180 New-Item -ItemType Directory -Path $script:ghDir -Force | Out-Null
181
182 @'
183---
184description: "Test prompt"
185maturity: stable
186---
187'@ | Set-Content -Path (Join-Path $script:promptsDir 'test.prompt.md')
188 }
189
190 AfterAll {
191 Remove-Item -Path $script:tempDir -Recurse -Force -ErrorAction SilentlyContinue
192 }
193
194 It 'Discovers prompts in directory' {
195 if (-not $script:hasYamlSupport) {
196 Set-ItResult -Skipped -Because 'ConvertFrom-Yaml (PowerShell-Yaml module) not available'
197 return
198 }
199 $result = Get-DiscoveredPrompts -PromptsDir $script:promptsDir -GitHubDir $script:ghDir -AllowedMaturities @('stable')
200 $result.DirectoryExists | Should -BeTrue
201 $result.Prompts.Count | Should -BeGreaterThan 0
202 }
203
204 It 'Returns empty when directory does not exist' {
205 $nonexistentPath = [System.IO.Path]::Combine([System.IO.Path]::GetTempPath(), 'nonexistent-prompts-dir-12345')
206 $result = Get-DiscoveredPrompts -PromptsDir $nonexistentPath -GitHubDir $script:ghDir -AllowedMaturities @('stable')
207 $result.DirectoryExists | Should -BeFalse
208 }
209}
210
211Describe 'Get-DiscoveredInstructions' {
212 BeforeAll {
213 $script:tempDir = Join-Path ([System.IO.Path]::GetTempPath()) ([System.Guid]::NewGuid().ToString())
214 $script:instrDir = Join-Path $script:tempDir 'instructions'
215 $script:ghDir = Join-Path $script:tempDir '.github'
216 New-Item -ItemType Directory -Path $script:instrDir -Force | Out-Null
217 New-Item -ItemType Directory -Path $script:ghDir -Force | Out-Null
218
219 @'
220---
221description: "Test instruction"
222applyTo: "**/*.ps1"
223maturity: stable
224---
225'@ | Set-Content -Path (Join-Path $script:instrDir 'test.instructions.md')
226 }
227
228 AfterAll {
229 Remove-Item -Path $script:tempDir -Recurse -Force -ErrorAction SilentlyContinue
230 }
231
232 It 'Discovers instructions in directory' {
233 if (-not $script:hasYamlSupport) {
234 Set-ItResult -Skipped -Because 'ConvertFrom-Yaml (PowerShell-Yaml module) not available'
235 return
236 }
237 $result = Get-DiscoveredInstructions -InstructionsDir $script:instrDir -GitHubDir $script:ghDir -AllowedMaturities @('stable')
238 $result.DirectoryExists | Should -BeTrue
239 $result.Instructions.Count | Should -BeGreaterThan 0
240 }
241
242 It 'Returns empty when directory does not exist' {
243 $nonexistentPath = [System.IO.Path]::Combine([System.IO.Path]::GetTempPath(), 'nonexistent-instr-dir-12345')
244 $result = Get-DiscoveredInstructions -InstructionsDir $nonexistentPath -GitHubDir $script:ghDir -AllowedMaturities @('stable')
245 $result.DirectoryExists | Should -BeFalse
246 }
247}
248
249Describe 'Update-PackageJsonContributes' {
250 It 'Updates contributes section with chat participants' {
251 $packageJson = [PSCustomObject]@{
252 name = 'test-extension'
253 contributes = [PSCustomObject]@{}
254 }
255 $agents = @(
256 @{ name = 'agent1'; description = 'Desc 1' }
257 )
258 $prompts = @(
259 @{ name = 'prompt1'; description = 'Prompt desc' }
260 )
261 $instructions = @(
262 @{ name = 'instr1'; description = 'Instr desc' }
263 )
264
265 $result = Update-PackageJsonContributes -PackageJson $packageJson -ChatAgents $agents -ChatPromptFiles $prompts -ChatInstructions $instructions
266 $result.contributes | Should -Not -BeNullOrEmpty
267 }
268
269 It 'Handles empty arrays' {
270 $packageJson = [PSCustomObject]@{
271 name = 'test-extension'
272 contributes = [PSCustomObject]@{}
273 }
274
275 $result = Update-PackageJsonContributes -PackageJson $packageJson -ChatAgents @() -ChatPromptFiles @() -ChatInstructions @()
276 $result | Should -Not -BeNullOrEmpty
277 }
278}
279