Tests/Unit/BasicFunctions.Tests.ps1

#Requires -Module Pester

BeforeAll {
    # Import module
    $modulePath = Join-Path $PSScriptRoot '..' '..' 'iFacto.AICodeReview.psd1'
    Import-Module $modulePath -Force
}

Describe "Get-ChangedALFiles" {
    Context "When run in a git repository" {
        It "Should not throw" {
            { Get-ChangedALFiles -BaseBranch "origin/master" } | Should -Not -Throw
        }
        
        It "Should return an array or null" {
            $result = Get-ChangedALFiles -BaseBranch "origin/master"
            ($result -is [array] -or $null -eq $result) | Should -Be $true
        }
    }
}

Describe "ConvertTo-ADOLogFormat" {
    Context "When given empty violations array" {
        It "Should return 0" {
            $result = ConvertTo-ADOLogFormat -Violations @()
            $result | Should -Be 0
        }
    }
    
    Context "When given violations" {
        It "Should output formatted messages" {
            $violations = @(
                @{
                    file = "test.al"
                    line = 10
                    severity = "warning"
                    message = "Test warning"
                }
            )
            
            { ConvertTo-ADOLogFormat -Violations $violations } | Should -Not -Throw
        }
    }
}

Describe "Parse-AIResponse" {
    BeforeAll {
        # Import private function for testing
        . (Join-Path $PSScriptRoot '..' '..' 'Private' 'Parse-AIResponse.ps1')
    }
    
    Context "When given valid JSON array" {
        It "Should parse direct JSON" {
            $json = '[{"file":"test.al","line":10,"severity":"warning","message":"Test"}]'
            $result = Parse-AIResponse -ResponseText $json
            $result | Should -HaveCount 1
            $result[0].file | Should -Be "test.al"
        }
    }
    
    Context "When given JSON in markdown" {
        It "Should extract from code block" {
            $markdown = @"
```json
[{"file":"test.al","line":10,"severity":"warning","message":"Test"}]
```
"@

            $result = Parse-AIResponse -ResponseText $markdown
            $result | Should -HaveCount 1
        }
    }
    
    Context "When given invalid JSON" {
        It "Should return empty array" {
            $result = Parse-AIResponse -ResponseText "Not JSON"
            $result | Should -HaveCount 0
        }
    }
}