tests/Set-DFToolXdgConfig.Tests.ps1

BeforeAll {
    . "$PSScriptRoot/TestSupport.ps1"
    foreach ($f in Get-DFTestModuleFile) { . $f }
}

Describe 'Set-DFToolXdgConfig' {
    BeforeEach {
        Set-DFTestXdg



        Remove-Item Env:\TESTXDG_CONFIG -ErrorAction Ignore
        Remove-Item Env:\TESTXDG_HIST -ErrorAction Ignore
    }
    AfterEach {

        Remove-Item Env:\TESTXDG_CONFIG -ErrorAction Ignore
        Remove-Item Env:\TESTXDG_HIST -ErrorAction Ignore
        Restore-DFTestXdg
    }

    It 'method env: sets env vars and creates directories' {
        $tool = @'
{
  "name": "envtool",
  "xdg": {
    "method": "env",
    "vars": { "TESTXDG_CONFIG": "${XDG_CONFIG_HOME}/envtool/config.conf" },
    "dirs": ["${XDG_CONFIG_HOME}/envtool"]
  }
}
'@
 | ConvertFrom-Json
        Set-DFToolXdgConfig -Tool $tool
        $Env:TESTXDG_CONFIG | Should -Be (Join-Path $Env:XDG_CONFIG_HOME 'envtool' 'config.conf')
        Test-Path (Join-Path $Env:XDG_CONFIG_HOME 'envtool') | Should -BeTrue
    }

    It 'method env: never overwrites a variable the user already set' {
        $Env:TESTXDG_CONFIG = 'C:\user\chose\this.conf'
        $tool = @'
{
  "name": "envtool",
  "xdg": {
    "method": "env",
    "vars": {
      "TESTXDG_CONFIG": "${XDG_CONFIG_HOME}/envtool/config.conf",
      "TESTXDG_HIST": "${XDG_STATE_HOME}/envtool/history"
    }
  }
}
'@
 | ConvertFrom-Json
        Set-DFToolXdgConfig -Tool $tool
        $Env:TESTXDG_CONFIG | Should -Be 'C:\user\chose\this.conf'
        $Env:TESTXDG_HIST | Should -Be (Join-Path $Env:XDG_STATE_HOME 'envtool' 'history') -Because 'unset variables still get the default'
    }

    It 'method manual: warns, including instructions when present' {
        $tool = @'
{
  "name": "manualtool",
  "xdg": { "method": "manual", "instructions": "See docs/manualtool.md" }
}
'@
 | ConvertFrom-Json
        Set-DFToolXdgConfig -Tool $tool -WarningVariable warns 3>$null
        $warns | Where-Object { $_ -match 'manualtool requires manual XDG configuration' -and $_ -match 'See docs/manualtool.md' } |
            Should -Not -BeNullOrEmpty
    }

    It 'method wrapper: no env/dir side effects (handled by companion)' {
        $tool = @'
{ "name": "wrappertool", "xdg": { "method": "wrapper" } }
'@
 | ConvertFrom-Json
        { Set-DFToolXdgConfig -Tool $tool -Verbose } | Should -Not -Throw
    }

    It 'method default: no-op' {
        $tool = @'
{ "name": "defaulttool", "xdg": { "method": "default" } }
'@
 | ConvertFrom-Json
        { Set-DFToolXdgConfig -Tool $tool } | Should -Not -Throw
    }

    It 'no xdg property at all: no-op, does not throw' {
        $tool = '{ "name": "noxdgtool" }' | ConvertFrom-Json
        { Set-DFToolXdgConfig -Tool $tool } | Should -Not -Throw
    }
}