tests/unit/Public/Get-KlippyFile.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-KlippyFile" -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
        }

        Mock -CommandName Invoke-KlippyHttpRequest -ModuleName KlippyCLI -MockWith {
            return @(
                [PSCustomObject]@{ Path = "root.gcode"; Size = 100; Modified = 1700000000 }
                [PSCustomObject]@{ Path = "folder/test.cfg"; Size = 200; Modified = 1700000001 }
                [PSCustomObject]@{ Path = "folder"; Dirname = "folder" }
            )
        }
    }

    It "Should list files for a root" {
        $result = Get-KlippyFile -PrinterName "TestPrinter" -Root "gcodes"

        $result | Should -Not -BeNullOrEmpty
        $result.Count | Should -Be 1
        $result[0].Path | Should -Be "root.gcode"
    }

    It "Should filter by wildcard path" {
        $result = Get-KlippyFile -PrinterName "TestPrinter" -Root "config" -Path "*.cfg" -Recurse

        $result.Count | Should -Be 1
        $result[0].Path | Should -Be "folder/test.cfg"
    }

    It "Should include subdirectory files when -Recurse is set" {
        $result = Get-KlippyFile -PrinterName "TestPrinter" -Root "config" -Recurse

        $result.Count | Should -Be 2
    }
}