Tests/Touch/Touch.EdgeCases.Tests.ps1

BeforeAll {
    . "$PSScriptRoot\..\Shared\Setup.ps1"
    Initialize-TestEnvironment
}

AfterAll {
    Clear-TestEnvironment
}

Describe 'Touch - Edge cases' {
    BeforeEach {
        $script:testFolder = Join-Path $TestDrive "Touch_Edge_$(Get-Random)"
        New-Item -Path $script:testFolder -ItemType Directory -Force | Out-Null
    }

    It 'Creates file with spaces in name' {
        Touch 'my file.txt' -Path $script:testFolder
        Test-Path (Join-Path $script:testFolder 'my file.txt') | Should -BeTrue
    }

    It 'Creates file with cyrillic name' {
        Touch 'файл.txt' -Path $script:testFolder
        Test-Path (Join-Path $script:testFolder 'файл.txt') | Should -BeTrue
    }

    It 'Creates file with multiple dots in name' {
        Touch 'archive.tar.gz' -Path $script:testFolder
        Test-Path (Join-Path $script:testFolder 'archive.tar.gz') | Should -BeTrue
    }

    It 'Creates file without extension' {
        Touch 'README' -Path $script:testFolder
        Test-Path (Join-Path $script:testFolder 'README') | Should -BeTrue
    }

    It 'Combines positional args, -Count and -Force' {
        Touch 'a.txt' 'b.txt' -Count 2 -Force -Path $script:testFolder
        '(1)a.txt', '(2)a.txt', '(1)b.txt', '(2)b.txt' | ForEach-Object {
            Test-Path (Join-Path $script:testFolder $_) | Should -BeTrue
        }
    }

    It 'Preserves existing files when creating new ones alongside' {
        $existing = Join-Path $script:testFolder 'existing.txt'
        New-Item -Path $existing -ItemType File | Out-Null
        $existingTime = (Get-Item $existing).LastWriteTime
        Start-Sleep -Milliseconds 100

        Touch 'existing.txt' 'new.txt' -Path $script:testFolder
        Test-Path (Join-Path $script:testFolder 'new.txt') | Should -BeTrue
        (Get-Item $existing).LastWriteTime | Should -BeGreaterThan $existingTime
    }
}