tests/mdv.Tests.ps1

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

    $script:MdvJson = Get-Content "$PSScriptRoot/../Tools/mdv.json" -Raw | ConvertFrom-Json
}

Describe 'Tools/mdv.json' {
    BeforeEach { Reset-DFTestSession }
    It 'declares the env XDG method' {
        $script:MdvJson.xdg.method | Should -Be 'env'
    }
    It 'points MDV_CONFIG_PATH at the XDG mdv dir' {
        $script:MdvJson.xdg.vars.MDV_CONFIG_PATH | Should -Be '${XDG_CONFIG_HOME}/mdv'
    }
    It 'names catppuccin-mocha as the default theme' {
        $script:MdvJson.settings.theme | Should -Be 'catppuccin-mocha'
    }
    It 'declares a themeMap translating the canonical family to catppuccin' {
        $script:MdvJson.themeMap.'catppuccin-mocha' | Should -Be 'catppuccin'
    }
    It 'declares a crates (cargo) package' {
        $script:MdvJson.packages.crates | Should -Be 'mdv'
    }
}

Describe 'mdv tool sidecar' -Skip:(-not (Get-Command mdv.exe -ErrorAction Ignore)) {
    BeforeEach { Reset-DFTestSession;
        $script:DFToolDb        = $null
        $script:DFToolAvailability = @{}
        Set-DFTestXdg
        $script:SavedConfigPath = $Env:MDV_CONFIG_PATH



        # Clean up mdv config/state from previous tests to ensure fresh state
        Remove-Item (Join-Path $Env:XDG_CONFIG_HOME 'mdv') -Recurse -Force -ErrorAction Ignore
        Remove-Item $Env:XDG_STATE_HOME -Recurse -Force -ErrorAction Ignore
        $Env:MDV_CONFIG_PATH    = $null
        Set-DFTestConfig $null
        $script:RealTools = Join-Path $PSScriptRoot '../Tools'
    }
    AfterEach {
        $Env:MDV_CONFIG_PATH = $script:SavedConfigPath

        $script:DFToolDb     = $null
        Set-DFTestConfig $null
        Restore-DFTestXdg
    }

    It 'sets MDV_CONFIG_PATH and creates the dir' {
        Register-DFTool -Name 'mdv' -ToolsPath $script:RealTools
        $Env:MDV_CONFIG_PATH | Should -Be (Join-Path $Env:XDG_CONFIG_HOME 'mdv')
        Test-Path $Env:MDV_CONFIG_PATH | Should -BeTrue
    }

    It 'seeds config.yaml with the catppuccin theme on first registration' {
        Register-DFTool -Name 'mdv' -ToolsPath $script:RealTools
        $cfg = Join-Path $Env:XDG_CONFIG_HOME 'mdv' 'config.yaml'
        Test-Path $cfg | Should -BeTrue
        (Get-Content $cfg -Raw) | Should -Match 'theme:\s*"catppuccin"'
    }

    It 'records setup state on first registration' {
        Register-DFTool -Name 'mdv' -ToolsPath $script:RealTools
        (Get-DFToolSetupState).PSObject.Properties['mdv'] | Should -Not -BeNullOrEmpty
    }

    It 'does not overwrite an existing config.yaml, but still records setup state' {
        $dir = Join-Path $Env:XDG_CONFIG_HOME 'mdv'
        New-Item -ItemType Directory -Force -Path $dir | Out-Null
        $cfg = Join-Path $dir 'config.yaml'
        'theme: "nord" # user edit' | Set-Content $cfg
        Register-DFTool -Name 'mdv' -ToolsPath $script:RealTools
        (Get-Content $cfg -Raw) | Should -Match 'user edit'
        (Get-DFToolSetupState).PSObject.Properties['mdv'] | Should -Not -BeNullOrEmpty
    }

    It 'does not reseed config.yaml after the user deletes it post-setup (closes the presence-check bug)' {
        Register-DFTool -Name 'mdv' -ToolsPath $script:RealTools
        $cfg = Join-Path $Env:XDG_CONFIG_HOME 'mdv' 'config.yaml'
        Test-Path $cfg | Should -BeTrue
        Remove-Item $cfg -Force

        Register-DFTool -Name 'mdv' -ToolsPath $script:RealTools

        Test-Path $cfg | Should -BeFalse
    }

    It 'skips config seeding entirely when SkipSetup names mdv' {
        Set-DFTestConfig @{ SkipSetup = @('mdv') }
        Register-DFTool -Name 'mdv' -ToolsPath $script:RealTools
        $cfg = Join-Path $Env:XDG_CONFIG_HOME 'mdv' 'config.yaml'
        Test-Path $cfg | Should -BeFalse
        (Get-DFToolSetupState).PSObject.Properties['mdv'] | Should -BeNullOrEmpty
    }

    It 'maps the catppuccin family down to mdv''s catppuccin theme' {
        Set-DFTestConfig @{ Theme = 'catppuccin-mocha' }
        Register-DFTool -Name 'mdv' -ToolsPath $script:RealTools
        $cfg = Join-Path $Env:XDG_CONFIG_HOME 'mdv' 'config.yaml'
        (Get-Content $cfg -Raw) | Should -Match 'theme:\s*"catppuccin"'
    }

    It 'tolerates $DFConfig being set to $null' {
        Set-DFTestConfig $null
        { Register-DFTool -Name 'mdv' -ToolsPath $script:RealTools } | Should -Not -Throw
    }
}

Describe 'Tools/carapace/specs/mdv.yaml' {
    BeforeEach { Reset-DFTestSession }
    BeforeAll {
        $script:SpecPath = "$PSScriptRoot/../Tools/carapace/specs/mdv.yaml"
        $script:Spec     = Get-Content $script:SpecPath -Raw
    }
    It 'exists and names the mdv command' {
        Test-Path $script:SpecPath | Should -BeTrue
        $script:Spec | Should -Match '(?m)^name:\s*mdv\b'
    }
    It 'declares the theme flag with catppuccin among its values' {
        $script:Spec | Should -Match '--theme'
        $script:Spec | Should -Match 'catppuccin'
    }
    It 'declares the config-file and pager flags' {
        $script:Spec | Should -Match '--config-file'
        $script:Spec | Should -Match '--pager'
    }
}