Public/Test-AICodeReview.ps1

function Test-AICodeReview {
    <#
    .SYNOPSIS
    Test AI Code Review locally without running a pipeline
     
    .DESCRIPTION
    This function allows testing the AI Code Review functionality locally by:
    - Detecting changed files in your current branch
    - Reviewing them with configured AI provider
    - Displaying results in console
     
    Use this for development, testing, and debugging before committing changes.
     
    .PARAMETER BaseBranch
    The base branch to compare against (default: origin/master)
     
    .PARAMETER Provider
    AI provider to use. If not specified, uses default from config.
     
    .PARAMETER ConfigPath
    Path to model-config.json
     
    .PARAMETER RulesPath
    Path to rules directory
     
    .PARAMETER SeverityFailBuild
    Severity level that would fail the build (error, warning, none)
     
    .PARAMETER MaxTokens
    Maximum tokens for AI response
     
    .PARAMETER TestFile
    Optional: Test a specific file instead of detecting changed files
     
    .EXAMPLE
    Test-AICodeReview
    Test current branch against master using default provider
     
    .EXAMPLE
    Test-AICodeReview -Provider "anthropic"
    Test with Claude
     
    .EXAMPLE
    Test-AICodeReview -TestFile "MyTable.al"
    Test specific file
     
    .OUTPUTS
    System.Void
     
    .NOTES
    Author: waldo
    Version: 1.0.0
    #>

    [CmdletBinding()]
    param(
        [Parameter(Mandatory = $false)]
        [string]$BaseBranch = "origin/master",
        
        [Parameter(Mandatory = $false)]
        [string]$Provider,
        
        [Parameter(Mandatory = $false)]
        [string]$ConfigPath,
        
        [Parameter(Mandatory = $false)]
        [string]$RulesPath,
        
        [Parameter(Mandatory = $false)]
        [ValidateSet('error', 'warning', 'none')]
        [string]$SeverityFailBuild = 'error',
        
        [Parameter(Mandatory = $false)]
        [int]$MaxTokens = 4000,
        
        [Parameter(Mandatory = $false)]
        [string]$TestFile
    )
    
    begin {
        Write-Verbose "Starting $($MyInvocation.MyCommand.Name)"
    }
    
    process {
        try {
            Write-Host "=========================================" -ForegroundColor Cyan
            Write-Host "AI Code Review - Local Test Harness" -ForegroundColor Cyan
            Write-Host "=========================================" -ForegroundColor Cyan
            Write-Host ""
            
            # Check if we're in a git repository
            try {
                $gitRoot = git rev-parse --show-toplevel 2>&1
                if ($LASTEXITCODE -ne 0) {
                    throw "Not in a git repository"
                }
                Write-Host "✅ Git repository: $gitRoot" -ForegroundColor Green
            }
            catch {
                Write-Host "❌ Error: Not in a git repository." -ForegroundColor Red
                Write-Host " Please run this from within your AL project." -ForegroundColor Yellow
                return
            }
            
            # If testing specific file, validate it exists
            if ($TestFile) {
                if (-not (Test-Path $TestFile)) {
                    Write-Host "❌ Error: Test file not found: $TestFile" -ForegroundColor Red
                    return
                }
                Write-Host "Testing specific file: $TestFile" -ForegroundColor Cyan
            }
            
            # Build parameters for Invoke-AICodeReview
            $params = @{
                BaseBranch = $BaseBranch
                SeverityFailBuild = $SeverityFailBuild
                MaxTokens = $MaxTokens
                Verbose = $VerbosePreference -eq 'Continue'
            }
            
            if ($Provider) { $params['Provider'] = $Provider }
            if ($ConfigPath) { $params['ConfigPath'] = $ConfigPath }
            if ($RulesPath) { $params['RulesPath'] = $RulesPath }
            
            # Run review
            $result = Invoke-AICodeReview @params
            
            # Display results
            Write-Host ""
            if ($result.Violations.Count -gt 0) {
                ConvertTo-ADOLogFormat -Violations $result.Violations -SeverityFailBuild $SeverityFailBuild
            } else {
                Write-Host "✅ No violations found!" -ForegroundColor Green
            }
            
            # Summary
            Write-Host ""
            Write-Host "=========================================" -ForegroundColor Cyan
            Write-Host "Test Complete" -ForegroundColor Cyan
            Write-Host "=========================================" -ForegroundColor Cyan
            
            if ($result.Success) {
                Write-Host "Result: PASS ✅" -ForegroundColor Green
            } else {
                Write-Host "Result: FAIL ❌" -ForegroundColor Red
                Write-Host "Build would fail with severity threshold: $SeverityFailBuild" -ForegroundColor Yellow
            }
        }
        catch {
            Write-Error "Error in $($MyInvocation.MyCommand.Name): $_"
            throw
        }
    }
    
    end {
        Write-Verbose "Completed $($MyInvocation.MyCommand.Name)"
    }
}