name: Unity Tests

on:
  workflow_dispatch: {}
  push:
    branches: ["**"]
    paths:
      - TestProjects/UnityMCPTests/**
      - MCPForUnity/Editor/**
      - .github/workflows/unity-tests.yml

jobs:
  testAllModes:
    name: Test in ${{ matrix.testMode }}
    runs-on: ubuntu-latest
    strategy:
      fail-fast: false
      matrix:
        projectPath:
          - TestProjects/UnityMCPTests
        testMode:
          - editmode
        unityVersion:
          - 2021.3.45f2
    steps:
      - name: Checkout repository
        uses: actions/checkout@v4
        with:
          lfs: true

      - name: Detect Unity license secrets
        id: detect
        env:
          UNITY_LICENSE: ${{ secrets.UNITY_LICENSE }}
          UNITY_EMAIL: ${{ secrets.UNITY_EMAIL }}
          UNITY_PASSWORD: ${{ secrets.UNITY_PASSWORD }}
          UNITY_SERIAL: ${{ secrets.UNITY_SERIAL }}
        run: |
          set -e
          if [ -n "$UNITY_LICENSE" ] || { [ -n "$UNITY_EMAIL" ] && [ -n "$UNITY_PASSWORD" ] && [ -n "$UNITY_SERIAL" ]; }; then
            echo "unity_ok=true" >> "$GITHUB_OUTPUT"
          else
            echo "unity_ok=false" >> "$GITHUB_OUTPUT"
          fi

      - name: Skip Unity tests (missing license secrets)
        if: steps.detect.outputs.unity_ok != 'true'
        run: |
          echo "Unity license secrets missing; skipping Unity tests."

      - uses: actions/cache@v4
        with:
          path: ${{ matrix.projectPath }}/Library
          key: Library-${{ matrix.projectPath }}-${{ matrix.unityVersion }}
          restore-keys: |
            Library-${{ matrix.projectPath }}-
            Library-

      # Run domain reload tests first (they're [Explicit] so need explicit category)
      - name: Run domain reload tests
        if: steps.detect.outputs.unity_ok == 'true'
        uses: game-ci/unity-test-runner@v4
        id: domain-tests
        env:
          UNITY_EMAIL: ${{ secrets.UNITY_EMAIL }}
          UNITY_PASSWORD: ${{ secrets.UNITY_PASSWORD }}
          UNITY_LICENSE: ${{ secrets.UNITY_LICENSE }}
          UNITY_SERIAL: ${{ secrets.UNITY_SERIAL }}
        with:
          projectPath: ${{ matrix.projectPath }}
          unityVersion: ${{ matrix.unityVersion }}
          testMode: ${{ matrix.testMode }}
          customParameters: -testCategory domain_reload

      - name: Run tests
        if: steps.detect.outputs.unity_ok == 'true'
        uses: game-ci/unity-test-runner@v4
        id: tests
        env:
          UNITY_EMAIL: ${{ secrets.UNITY_EMAIL }}
          UNITY_PASSWORD: ${{ secrets.UNITY_PASSWORD }}
          UNITY_LICENSE: ${{ secrets.UNITY_LICENSE }}
          UNITY_SERIAL: ${{ secrets.UNITY_SERIAL }}
        with:
          projectPath: ${{ matrix.projectPath }}
          unityVersion: ${{ matrix.unityVersion }}
          testMode: ${{ matrix.testMode }}

      - uses: actions/upload-artifact@v4
        if: always() && steps.detect.outputs.unity_ok == 'true'
        with:
          name: Test results for ${{ matrix.testMode }}
          path: ${{ steps.tests.outputs.artifactsPath }}
