tests/integration/Integration.Tests.ps1

#Requires -Modules Pester

<#
.SYNOPSIS
    Integration tests for KlippyCLI module.

.DESCRIPTION
    These tests require a real Klipper printer to be accessible.
    Set the following environment variables before running:

    Required:
        KLIPPY_TEST_PRINTER - PrinterName of registered printer to test against

    Optional:
        KLIPPY_TEST_URI - URI of printer (if not already registered)
        KLIPPY_TEST_APIKEY - API key if required

.EXAMPLE
    $env:KLIPPY_TEST_PRINTER = "voronv2"
    Invoke-Pester ./tests/integration -Tag Integration
#>


BeforeAll {
    $modulePath = Split-Path -Parent (Split-Path -Parent $PSScriptRoot)
    Import-Module "$modulePath/KlippyCLI.psd1" -Force

    # Check if integration tests should run
    $script:SkipIntegration = $null -eq $env:KLIPPY_TEST_PRINTER -or $env:KLIPPY_TEST_PRINTER -eq ''
    $script:TestPrinter = $env:KLIPPY_TEST_PRINTER

    if ($script:SkipIntegration) {
        Write-Warning "Integration tests skipped. Set KLIPPY_TEST_PRINTER environment variable to run."
    }
}

Describe "Printer Connection" -Tag Integration {
    BeforeAll {
        if ($script:SkipIntegration) {
            Set-ItResult -Skipped -Because "KLIPPY_TEST_PRINTER not set"
        }
    }

    It "Should connect to test printer" -Skip:$script:SkipIntegration {
        $result = Test-KlippyPrinterConnection -PrinterName $script:TestPrinter
        $result.Success | Should -Be $true
    }

    It "Should get printer status" -Skip:$script:SkipIntegration {
        $result = Get-KlippyStatus -PrinterName $script:TestPrinter
        $result | Should -Not -BeNullOrEmpty
        $result.State | Should -Not -BeNullOrEmpty
    }
}

Describe "Server Information" -Tag Integration {
    It "Should get server info" -Skip:$script:SkipIntegration {
        $result = Get-KlippyServerInfo -PrinterName $script:TestPrinter
        $result | Should -Not -BeNullOrEmpty
        $result.MoonrakerVersion | Should -Not -BeNullOrEmpty
    }

    It "Should get machine info" -Skip:$script:SkipIntegration {
        $result = Get-KlippyMachineInfo -PrinterName $script:TestPrinter
        $result | Should -Not -BeNullOrEmpty
        $result.Hostname | Should -Not -BeNullOrEmpty
    }

    It "Should get service status" -Skip:$script:SkipIntegration {
        $result = Get-KlippyServiceStatus -PrinterName $script:TestPrinter
        $result | Should -Not -BeNullOrEmpty
        $result.Count | Should -BeGreaterThan 0
    }
}

Describe "Printer Objects" -Tag Integration {
    It "Should get print_stats object" -Skip:$script:SkipIntegration {
        $result = Get-KlippyObject -PrinterName $script:TestPrinter -ObjectName "print_stats"
        $result | Should -Not -BeNullOrEmpty
    }

    It "Should get heater_bed object" -Skip:$script:SkipIntegration {
        $result = Get-KlippyObject -PrinterName $script:TestPrinter -ObjectName "heater_bed"
        $result | Should -Not -BeNullOrEmpty
        $result.PSObject.Properties.Name | Should -Contain "Temperature"
    }

    It "Should get multiple objects" -Skip:$script:SkipIntegration {
        $result = Get-KlippyObject -PrinterName $script:TestPrinter -ObjectName "print_stats", "heater_bed"
        $result | Should -Not -BeNullOrEmpty
        $result.PSObject.Properties.Name | Should -Contain "PrintStats"
        $result.PSObject.Properties.Name | Should -Contain "HeaterBed"
    }
}

Describe "File Operations" -Tag Integration {
    It "Should list gcode files" -Skip:$script:SkipIntegration {
        $result = Get-KlippyGcodeFile -PrinterName $script:TestPrinter
        # Result may be empty but should not throw
        $result | Should -Not -Throw
    }

    It "Should list gcode folders" -Skip:$script:SkipIntegration {
        $result = Get-KlippyGcodeFolder -PrinterName $script:TestPrinter
        # Result may be empty but should not throw
        $result | Should -Not -Throw
    }

    It "Should list config files" -Skip:$script:SkipIntegration {
        $result = Get-KlippyConfigFile -PrinterName $script:TestPrinter
        $result | Should -Not -BeNullOrEmpty
        # Should have at least printer.cfg
        $result.Name | Should -Contain "printer.cfg"
    }

    It "Should list log files" -Skip:$script:SkipIntegration {
        $result = Get-KlippyLogFile -PrinterName $script:TestPrinter
        $result | Should -Not -BeNullOrEmpty
    }
}

Describe "Print Job Information" -Tag Integration {
    It "Should get current print job info" -Skip:$script:SkipIntegration {
        $result = Get-KlippyPrintJob -PrinterName $script:TestPrinter
        $result | Should -Not -BeNullOrEmpty
        $result.State | Should -Not -BeNullOrEmpty
    }

    It "Should get print job history" -Skip:$script:SkipIntegration {
        $result = Get-KlippyPrintJob -PrinterName $script:TestPrinter -History -Limit 5
        # History may be empty on new printers
        # Just verify it doesn't throw
    }
}

Describe "Wait Commands" -Tag Integration, Slow {
    It "Should wait for ready state" -Skip:$script:SkipIntegration {
        # This should complete quickly if printer is already ready
        $result = Wait-KlippyReady -PrinterName $script:TestPrinter -Timeout 10
        $result | Should -Not -BeNullOrEmpty
    }
}