Skip to content

Commit d024d95

Browse files
Refactor ContextFilter tests for determinism and logging
Updated ContextFilter-related tests to use -WhatIf mode and PSFramework message inspection for deterministic validation, replacing API-dependent checks. Added verification of deduplication and warning messages, and improved test clarity by focusing on internal logic rather than external tool responses.
1 parent 40f83fa commit d024d95

File tree

1 file changed

+46
-39
lines changed

1 file changed

+46
-39
lines changed

Tests/aitools.Tests.ps1

Lines changed: 46 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,8 @@ function Get-TestData {
206206
Context 'ContextFilter Parameter' {
207207
BeforeAll {
208208
$script:recipesPath = Join-Path $PSScriptRoot 'recipes'
209+
# Set PSFramework message level to capture debug messages
210+
Set-PSFConfig -FullName 'PSFramework.Message.Info.Maximum' -Value 9
209211
}
210212

211213
It 'Should have test recipe files available' {
@@ -214,64 +216,69 @@ function Get-TestData {
214216
Test-Path (Join-Path $script:recipesPath 'alligator-eggs.fr-ca.md') | Should -Be $true
215217
}
216218

217-
It 'Should include English original as dynamic context and Claude should see it' {
219+
It 'Should resolve context file from ContextFilter (deterministic)' {
218220
$frFile = Join-Path $script:recipesPath 'alligator-eggs.fr.md'
219-
# Ask Claude to list the context files it can see - the English original should be included
220-
$result = Invoke-AITool -Path $frFile -Prompt "List ALL the filenames you can see in this prompt, including any context files. Just list the filenames, one per line." -Tool Claude -ContextFilter { $_ -replace '\.fr\.md$', '.md' }
221-
222-
$result | Should -Not -BeNullOrEmpty
223-
$result.Success | Should -Be $true
224-
$result.Tool | Should -Be 'Claude'
225-
# Claude should mention the English original file in its response
226-
$result.Result | Should -Match 'alligator-eggs\.md'
221+
# Clear previous messages and run with -WhatIf to avoid API call
222+
Get-PSFMessage | Out-Null
223+
$null = Invoke-AITool -Path $frFile -Prompt "test" -Tool Claude -ContextFilter { $_ -replace '\.fr\.md$', '.md' } -WhatIf
224+
225+
# Check debug messages to verify context file was found
226+
$messages = Get-PSFMessage -Last 50 | Where-Object Message -Match 'ContextFilter'
227+
$foundMessage = $messages | Where-Object Message -Match 'Found at:.*alligator-eggs\.md'
228+
$foundMessage | Should -Not -BeNullOrEmpty -Because "ContextFilter should find alligator-eggs.md"
227229
}
228230

229-
It 'Should use ContextFilterBase to find context files in different directory' {
231+
It 'Should resolve context file using ContextFilterBase (deterministic)' {
230232
$frCaFile = Join-Path $script:recipesPath 'alligator-eggs.fr-ca.md'
231-
# Ask Claude what files it sees - should include the English original from ContextFilterBase
232-
$result = Invoke-AITool -Path $frCaFile -Prompt "What filenames can you see? List them all." -Tool Claude -ContextFilter { [System.IO.Path]::GetFileName($_) -replace '\.fr-ca\.md$', '.md' } -ContextFilterBase $script:recipesPath
233-
234-
$result | Should -Not -BeNullOrEmpty
235-
$result.Success | Should -Be $true
236-
# Should see the English original
237-
$result.Result | Should -Match 'alligator-eggs\.md'
233+
Get-PSFMessage | Out-Null
234+
$null = Invoke-AITool -Path $frCaFile -Prompt "test" -Tool Claude `
235+
-ContextFilter { [System.IO.Path]::GetFileName($_) -replace '\.fr-ca\.md$', '.md' } `
236+
-ContextFilterBase $script:recipesPath -WhatIf
237+
238+
# Verify it searched in ContextFilterBase and found the file
239+
$messages = Get-PSFMessage -Last 50 | Where-Object Message -Match 'ContextFilter'
240+
$foundMessage = $messages | Where-Object Message -Match 'Found at:.*alligator-eggs\.md'
241+
$foundMessage | Should -Not -BeNullOrEmpty -Because "ContextFilter should find alligator-eggs.md via ContextFilterBase"
238242
}
239243

240-
It 'Should handle batch processing with ContextFilter and see context files' {
244+
It 'Should deduplicate context files in batch mode (deterministic)' {
245+
# Both .fr.md and .fr-ca.md derive the same .md file
241246
$frFiles = Get-ChildItem -Path $script:recipesPath -Filter '*.fr*.md'
242-
# Ask Claude what context files it can see in the batch
243-
$result = $frFiles | Invoke-AITool -Prompt "List ALL filenames you can see. Include both the files to process and any context files." -Tool Claude -ContextFilter { $_ -replace '\.fr(-ca)?\.md$', '.md' } -BatchSize 3
244-
245-
$result | Should -Not -BeNullOrEmpty
246-
$result.Success | Should -Be $true
247-
# Should see the English original as dynamic context
248-
$result.Result | Should -Match 'alligator-eggs\.md'
247+
Get-PSFMessage | Out-Null
248+
$null = $frFiles | Invoke-AITool -Prompt "test" -Tool Claude `
249+
-ContextFilter { $_ -replace '\.fr(-ca)?\.md$', '.md' } -BatchSize 3 -WhatIf
250+
251+
# Check that deduplication message appears (second file should be skipped)
252+
$messages = Get-PSFMessage -Last 50 | Where-Object Message -Match 'ContextFilter'
253+
$skipMessage = $messages | Where-Object Message -Match 'Skipping duplicate:.*alligator-eggs\.md'
254+
$skipMessage | Should -Not -BeNullOrEmpty -Because "Second derived file should be skipped as duplicate"
249255
}
250256

251-
It 'Should warn but not fail when ContextFilter derived file does not exist' {
257+
It 'Should warn when ContextFilter derived file does not exist' {
252258
$frFile = Join-Path $script:recipesPath 'alligator-eggs.fr.md'
253-
# This filter will derive a non-existent file
254-
$result = Invoke-AITool -Path $frFile -Prompt "Say hello" -Tool Claude -ContextFilter { $_ -replace '\.fr\.md$', '.nonexistent.md' } -WarningVariable warnings
259+
Get-PSFMessage | Out-Null
260+
$null = Invoke-AITool -Path $frFile -Prompt "test" -Tool Claude `
261+
-ContextFilter { $_ -replace '\.fr\.md$', '.nonexistent.md' } -WhatIf -WarningVariable warnings
255262

256-
$result | Should -Not -BeNullOrEmpty
257-
$result.Success | Should -Be $true
258263
# Should have warned about missing file
259-
$warnings | Should -Not -BeNullOrEmpty
264+
$messages = Get-PSFMessage -Last 50 | Where-Object Level -eq 'Warning'
265+
$warningMessage = $messages | Where-Object Message -Match 'ContextFilter derived file not found.*nonexistent'
266+
$warningMessage | Should -Not -BeNullOrEmpty -Because "Should warn about missing derived file"
260267
}
261268

262-
It 'Should deduplicate when multiple inputs derive same context file' {
263-
# Both .fr.md and .fr-ca.md would derive the same .md file
264-
$frFiles = Get-ChildItem -Path $script:recipesPath -Filter '*.fr*.md'
265-
# With deduplication, the English original should only appear once even though two files derive it
266-
$result = $frFiles | Invoke-AITool -Prompt "How many times does 'alligator-eggs.md' appear as a context file? Answer with just a number." -Tool Claude -ContextFilter { $_ -replace '\.fr(-ca)?\.md$', '.md' } -BatchSize 3
269+
It 'Should work end-to-end with ContextFilter (integration)' {
270+
$frFile = Join-Path $script:recipesPath 'alligator-eggs.fr.md'
271+
# One real API call to verify the whole flow works
272+
$result = Invoke-AITool -Path $frFile -Prompt "List ALL the filenames visible in this prompt. Output ONLY the filenames, one per line, nothing else." -Tool Claude -ContextFilter { $_ -replace '\.fr\.md$', '.md' }
267273

268274
$result | Should -Not -BeNullOrEmpty
269275
$result.Success | Should -Be $true
270-
# Should only see the context file once due to deduplication
271-
$result.Result | Should -Match '1'
276+
$result.Tool | Should -Be 'Claude'
277+
# Claude should see and mention both files
278+
$result.Result | Should -Match 'alligator-eggs'
272279
}
273280

274-
It 'Should work with null ContextFilter (no-op, existing behavior unchanged)' {
281+
It 'Should work with null ContextFilter (no-op)' {
275282
$frFile = Join-Path $script:recipesPath 'alligator-eggs.fr.md'
276283
$result = Invoke-AITool -Path $frFile -Prompt "Say hello" -Tool Claude
277284

0 commit comments

Comments
 (0)