name: Run UI Tests

on:
  workflow_dispatch:
    inputs:
      edgar_ref:
        description: 'Arelle/EDGAR branch, tag or SHA to checkout (blank for default)'
        required: false
        type: string
      python_version:
        default: '3.13.8'
        description: 'Python version to use'
        required: false
        type: string
      xule_ref:
        description: 'xbrlus/xule branch, tag or SHA to checkout (blank for default)'
        required: false
        type: string

  pull_request:
    branches:
      - master

permissions: {}

jobs:
  build-windows-dispatch:
    if: github.event_name == 'workflow_dispatch'
    uses: ./.github/workflows/build-windows.yml
    with:
      edgar_ref: ${{ inputs.edgar_ref }}
      python_version: ${{ inputs.python_version }}
      xule_ref: ${{ inputs.xule_ref }}
  build-windows-pr:
    if: github.event_name == 'pull_request'
    uses: ./.github/workflows/build-windows.yml

  ui-tests:
    needs: [build-windows-dispatch, build-windows-pr]
    # https://github.com/actions/runner/issues/491
    if: always() && (needs.build-windows-dispatch.result == 'success' || needs.build-windows-pr.result == 'success') && !cancelled()
    runs-on: windows-2022
    strategy:
      fail-fast: false
      matrix:
        build-type:
          - installer
          - zip
          - source
    env:
      exe_artifact_versioned_name: ${{ github.event_name == 'pull_request' && needs.build-windows-pr.outputs.exe_artifact_versioned_name || needs.build-windows-dispatch.outputs.exe_artifact_versioned_name }}
      zip_artifact_versioned_name: ${{ github.event_name == 'pull_request' && needs.build-windows-pr.outputs.zip_artifact_versioned_name || needs.build-windows-dispatch.outputs.zip_artifact_versioned_name }}
      exe_uploaded_artifact_name: ${{ github.event_name == 'pull_request' && needs.build-windows-pr.outputs.exe_uploaded_artifact_name || needs.build-windows-dispatch.outputs.exe_uploaded_artifact_name }}
      zip_uploaded_artifact_name: ${{ github.event_name == 'pull_request' && needs.build-windows-pr.outputs.zip_uploaded_artifact_name || needs.build-windows-dispatch.outputs.zip_uploaded_artifact_name }}
    steps:
      - name: Checkout arelle
        uses: actions/checkout@v5.0.0
        with:
          fetch-depth: 0
      - name: Download installer artifact
        if: matrix.build-type == 'installer'
        uses: actions/download-artifact@v5.0.0
        with:
          name: ${{ env.exe_uploaded_artifact_name }}
      - name: Download ZIP artifact
        if: matrix.build-type == 'zip'
        uses: actions/download-artifact@v5.0.0
        with:
          name: ${{ env.zip_uploaded_artifact_name }}

      - name: Install Arelle
        if: matrix.build-type == 'installer'
        run: start-process ${{ env.exe_artifact_versioned_name }} -argumentlist /S -nonewwindow -wait

      - name: Extract artifact
        if: matrix.build-type == 'zip'
        run: expand-archive ${{ env.zip_artifact_versioned_name }} build

      - name: Install Python
        if: matrix.build-type == 'source'
        uses: actions/setup-python@v6.0.0
        with:
          cache: 'pip'
          check-latest: true
          python-version: ${{ github.event_name == 'workflow_dispatch' && inputs.python_version || '3.13.8' }}
      - name: Install Python dependencies
        if: matrix.build-type == 'source'
        run: |
          python -m pip install --upgrade pip setuptools wheel
          pip install -r requirements.txt

      - name: Install runner
        run: nuget install NUnit.ConsoleRunner
      - name: Build tests
        env:
          DOTNET_CLI_TELEMETRY_OPTOUT: 1
        run: dotnet build tests\integration_tests\ui_tests\ArelleGUITest\ArelleGUITest.sln
      - name: Set build path
        run: echo "BUILD_PATH=$((gi tests\integration_tests\ui_tests\ArelleGUITest\ArelleGUITest\bin\Debug\*).FullName)" >> $env:GITHUB_ENV

      - name: Run tests
        if: matrix.build-type == 'installer' || matrix.build-type == 'zip'
        env:
          ARELLE_USE_BUILD: 'true'
          ARELLE_PATH: ${{ matrix.build-type == 'zip' && format('{0}/build', github.workspace) || '' }}
          ARELLE_RESOURCES_PATH: ${{ github.workspace }}\tests\integration_tests\ui_tests\resources
          DOTNET_CLI_TELEMETRY_OPTOUT: 1
        run: dotnet test --logger junit ${{ env.BUILD_PATH }}\ArelleGUITest.dll --test-adapter-path:(gi .\NUnit.ConsoleRunner*\tools\nunit3-console.exe).FullName

      - name: Run tests
        if: matrix.build-type == 'source'
        env:
          ARELLE_PATH: ${{ github.workspace }}
          ARELLE_RESOURCES_PATH: ${{ github.workspace }}\tests\integration_tests\ui_tests\resources
          ARELLE_PYTHON_EXE: python
          DOTNET_CLI_TELEMETRY_OPTOUT: 1
        run: dotnet test --logger junit ${{ env.BUILD_PATH }}\ArelleGUITest.dll --test-adapter-path:(gi .\NUnit.ConsoleRunner*\tools\nunit3-console.exe).FullName

      - name: Upload logs
        if: always()
        uses: actions/upload-artifact@v4.6.2
        with:
          name: logs-${{ matrix.build-type }}
          if-no-files-found: error
          path: TestResults
      - name: Upload screenshots
        # screenshots are only taken on test failure
        if: failure()
        uses: actions/upload-artifact@v4.6.2
        with:
          name: screenshots-${{ matrix.build-type }}
          if-no-files-found: error
          path: 'd:\temp\testsmedia\*.png'
      - name: Upload videos
        # videos are only saved on test failure
        if: failure()
        uses: actions/upload-artifact@v4.6.2
        with:
          name: videos-${{ matrix.build-type }}
          if-no-files-found: error
          path: 'd:\temp\testsmedia\*.avi'
