tests/unit/Public/Get-KlippySerialDevice.Tests.ps1

#Requires -Modules Pester

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

Describe "Get-KlippySerialDevice" -Tag Unit {
    BeforeEach {
        $script:testPrinter = [PSCustomObject]@{
            Id          = "11111111-1111-1111-1111-111111111111"
            PrinterName = "TestPrinter"
            Uri         = "http://127.0.0.1"
            ApiKey      = $null
        }

        Mock -CommandName Resolve-KlippyPrinterTarget -ModuleName KlippyCLI -MockWith {
            return $script:testPrinter
        }
    }

    It "Should return serial devices from JSON-RPC peripherals endpoint" {
        Mock -CommandName Invoke-KlippyJsonRpcMethod -ModuleName KlippyCLI -ParameterFilter { $Method -eq "machine.peripherals.serial" } -MockWith {
            return [PSCustomObject]@{
                SerialDevices = @([PSCustomObject]@{ DevicePath = "/dev/ttyUSB0" })
            }
        }

        $result = Get-KlippySerialDevice -PrinterName "TestPrinter"

        $result | Should -Not -BeNullOrEmpty
        $result.PSTypeName | Should -Be "KlippyCLI.SerialPeripheralInfo"
        $result.SerialDevices[0].DevicePath | Should -Be "/dev/ttyUSB0"
    }

    It "Should accept printer input from the pipeline" {
        Mock -CommandName Invoke-KlippyJsonRpcMethod -ModuleName KlippyCLI -MockWith {
            return @()
        }

        $result = $script:testPrinter | Get-KlippySerialDevice

        $result | Should -Not -BeNullOrEmpty
        $result.PrinterName | Should -Be "TestPrinter"
    }

    It "Should return raw response when -Raw is set" {
        Mock -CommandName Invoke-KlippyJsonRpcMethod -ModuleName KlippyCLI -MockWith {
            return [PSCustomObject]@{ serial_devices = @() }
        }

        $result = Get-KlippySerialDevice -PrinterName "TestPrinter" -Raw

        $result | Should -Not -BeNullOrEmpty
        $result.serial_devices | Should -Not -BeNullOrEmpty
    }
}