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:
      # Checkout
      - name: Checkout repository
        uses: actions/checkout@v4
        with:
          lfs: true

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

      # Test
      - name: Run tests
        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 }}
        with:
          projectPath: ${{ matrix.projectPath }}
          unityVersion: ${{ matrix.unityVersion }}
          testMode: ${{ matrix.testMode }}

      # Upload test results
      - uses: actions/upload-artifact@v4
        if: always()
        with:
          name: Test results for ${{ matrix.testMode }}
          path: ${{ steps.tests.outputs.artifactsPath }}
