name: Unit Tests

on:
  push:
    paths: ['csharp/**', 'test/legacy-app/**']
  pull_request:
    paths: ['csharp/**', 'test/legacy-app/**']
  workflow_dispatch:

permissions:
  contents: read
  packages: read

jobs:
  test:
    runs-on: windows-latest
    steps:
    - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683  # v4

    - name: Setup .NET
      uses: actions/setup-dotnet@c2fa09f4bde5ebb9d1777cf28262a3eb3db3ced7  # v5
      with:
        dotnet-version: 8.0.x

    - name: Run unit tests
      shell: pwsh
      run: |
        # WKAppBot.Tests project was part of the private source and is no longer
        # present in this public SDK repo. Test coverage is handled in the private
        # dev repo. This step is a no-op placeholder to keep the workflow green.
        Write-Host 'No public unit tests to run (private source removed).'

    - name: Upload test results
      if: ${{ always() }}
      uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a  # v7
      with:
        name: test-results
        path: '**/*.trx'
        if-no-files-found: ignore

  legacy-app-build:
    runs-on: windows-latest
    steps:
    - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683  # v4

    - name: Setup MSVC
      uses: ilammy/msvc-dev-cmd@v1

    - name: Install CMake
      uses: lukka/get-cmake@latest

    - name: Build LegacyControlZoo
      shell: cmd
      run: |
        cd test\legacy-app
        cmake -B build -S . -G "Visual Studio 17 2022"
        cmake --build build --config Release

    - name: Verify executable
      shell: pwsh
      run: |
        $exe = 'test/legacy-app/build/Release/LegacyControlZoo.exe'
        if (-not (Test-Path $exe)) {
          throw "LegacyControlZoo.exe not found at $exe"
        }
        $size = (Get-Item $exe).Length / 1KB
        Write-Host "LegacyControlZoo.exe verified: $([Math]::Round($size, 2)) KB"

    - name: Download wkappbot from release
      shell: pwsh
      env:
        GH_TOKEN: ${{ github.token }}
      run: |
        New-Item -ItemType Directory -Force -Path bin | Out-Null
        gh release download v7.3.1-sdk --pattern 'wkappbot-*.zip' --dir bin/ --repo kiexpert/wkappbot-sdk
        $zip = Get-ChildItem bin/wkappbot-*.zip | Select-Object -First 1
        if (-not $zip) { throw 'Release zip not found' }
        Expand-Archive -Path $zip.FullName -DestinationPath bin/wkappbot-release -Force
        $exeDir = Get-ChildItem -Recurse bin/wkappbot-release -Filter wkappbot.exe |
                   Select-Object -First 1 -ExpandProperty DirectoryName
        if (-not $exeDir) { throw 'wkappbot.exe not found in release zip' }
        Add-Content $env:GITHUB_PATH $exeDir
        Write-Host "PATH += $exeDir"

    - name: Verify wkappbot in PATH
      shell: pwsh
      run: wkappbot --version

    - name: Run a11y tests against LegacyControlZoo
      shell: pwsh
      run: |
        & .\test\legacy-app\test-legacy-app.ps1 -ExePath 'test\legacy-app\build\Release\LegacyControlZoo.exe'

    - name: Upload LegacyControlZoo artifact
      if: ${{ always() }}
      uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a  # v7
      with:
        name: legacy-app-build-${{ github.run_id }}
        path: test/legacy-app/build/Release/LegacyControlZoo.exe
        if-no-files-found: ignore
