Tests/Touch/Touch.Force.Tests.ps1

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

AfterAll {
    Clear-TestEnvironment
}

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

    It 'Creates new file with -Force' {
        Touch 'new.txt' -Force -Path $script:testFolder
        Test-Path (Join-Path $script:testFolder 'new.txt') | Should -BeTrue
    }

    It 'Updates timestamp of existing file with -Force silently' {
        $file = Join-Path $script:testFolder 'existing.txt'
        New-Item -Path $file -ItemType File | Out-Null
        $oldTime = (Get-Item $file).LastWriteTime
        Start-Sleep -Milliseconds 100

        $warnings = Touch 'existing.txt' -Force -Path $script:testFolder 3>&1
        $warnings | Where-Object { $_ -match 'already exists' } | Should -BeNullOrEmpty
        (Get-Item $file).LastWriteTime | Should -BeGreaterThan $oldTime
    }

    It 'Warns on existing file without -Force but still updates timestamp' {
        $file = Join-Path $script:testFolder 'existing.txt'
        New-Item -Path $file -ItemType File | Out-Null
        $oldTime = (Get-Item $file).LastWriteTime
        Start-Sleep -Milliseconds 100

        $warnings = Touch 'existing.txt' -Path $script:testFolder 3>&1
        $warnings | Where-Object { $_ -match 'already exists' } | Should -Not -BeNullOrEmpty
        (Get-Item $file).LastWriteTime | Should -BeGreaterThan $oldTime
    }
}