tests/unit/Public/Get-KlippyPrinter.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-KlippyPrinter" -Tag Unit { BeforeAll { # Backup existing registry if present $registryPath = & (Get-Module KlippyCLI) { Get-KlippyRegistryPath } $registryFile = Join-Path $registryPath "printers.json" $backupFile = "$registryFile.bak" if (Test-Path $registryFile) { Copy-Item $registryFile $backupFile -Force } } AfterAll { # Restore backup if it existed $registryPath = & (Get-Module KlippyCLI) { Get-KlippyRegistryPath } $registryFile = Join-Path $registryPath "printers.json" $backupFile = "$registryFile.bak" if (Test-Path $backupFile) { Move-Item $backupFile $registryFile -Force } } BeforeEach { # Clear registry for each test $registryPath = & (Get-Module KlippyCLI) { Get-KlippyRegistryPath } $registryFile = Join-Path $registryPath "printers.json" if (Test-Path $registryFile) { Remove-Item $registryFile -Force } } Context "Empty Registry" { It "Should return empty when no printers registered" { $result = Get-KlippyPrinter $result | Should -BeNullOrEmpty } } Context "With Registered Printers" { BeforeEach { # Add test printers directly to registry $registryPath = & (Get-Module KlippyCLI) { Get-KlippyRegistryPath } if (-not (Test-Path $registryPath)) { New-Item -Path $registryPath -ItemType Directory -Force | Out-Null } $registryFile = Join-Path $registryPath "printers.json" $now = (Get-Date).ToString("o") $testPrinters = @( @{ Id = "11111111-1111-1111-1111-111111111111" PrinterName = "TestPrinter1" Uri = "http://192.168.1.100" ApiKey = $null IsDefault = $true CreatedAt = $now ModifiedAt = $now } @{ Id = "22222222-2222-2222-2222-222222222222" PrinterName = "TestPrinter2" Uri = "http://192.168.1.101" ApiKey = "testkey" IsDefault = $false CreatedAt = $now ModifiedAt = $now } ) $testPrinters | ConvertTo-Json -Depth 5 | Set-Content $registryFile } It "Should return all printers when no filter specified" { $result = Get-KlippyPrinter $result.Count | Should -Be 2 } It "Should filter by PrinterName" { $result = Get-KlippyPrinter -PrinterName "TestPrinter1" $result | Should -Not -BeNullOrEmpty $result.PrinterName | Should -Be "TestPrinter1" } It "Should filter by Id" { $result = Get-KlippyPrinter -Id "22222222-2222-2222-2222-222222222222" $result | Should -Not -BeNullOrEmpty $result.Id | Should -Be "22222222-2222-2222-2222-222222222222" } It "Should return default printer with -Default" { $result = Get-KlippyPrinter -Default $result | Should -Not -BeNullOrEmpty $result.IsDefault | Should -Be $true $result.PrinterName | Should -Be "TestPrinter1" } It "Should return null for non-existent printer name" { $result = Get-KlippyPrinter -PrinterName "NonExistent" $result | Should -BeNullOrEmpty } It "Should include expected properties" { $result = Get-KlippyPrinter -PrinterName "TestPrinter1" $result.PSObject.Properties.Name | Should -Contain "Id" $result.PSObject.Properties.Name | Should -Contain "PrinterName" $result.PSObject.Properties.Name | Should -Contain "Uri" $result.PSObject.Properties.Name | Should -Contain "IsDefault" } } Context "Parameter Validation" { It "Should accept PrinterName from pipeline" { # Setup $registryPath = & (Get-Module KlippyCLI) { Get-KlippyRegistryPath } if (-not (Test-Path $registryPath)) { New-Item -Path $registryPath -ItemType Directory -Force | Out-Null } $registryFile = Join-Path $registryPath "printers.json" $now = (Get-Date).ToString("o") @(@{ Id = "33333333-3333-3333-3333-333333333333"; PrinterName = "Pipe1"; Uri = "http://test"; IsDefault = $true; CreatedAt = $now; ModifiedAt = $now }) | ConvertTo-Json -Depth 5 | Set-Content $registryFile $result = "Pipe1" | ForEach-Object { Get-KlippyPrinter -PrinterName $_ } $result.PrinterName | Should -Be "Pipe1" } } } |