# Specter: run your Specter tests against a packaged build on every push and pull request.
# Setup guide: https://spectertest.dev/docs/ci
name: Specter tests

on:
  push:
    branches: [main]
  pull_request:
  workflow_dispatch:

# One run at a time per branch: there's one game (and one port 8080) on the runner
concurrency:
  group: specter-${{ github.ref }}
  cancel-in-progress: true

# contents: write commits failure frames to the specter-ci-images branch; pull-requests: write posts the PR comment
permissions:
  contents: write
  pull-requests: write

jobs:
  test:
    # A Mac registered as a self-hosted runner with the "unreal" label
    runs-on: [self-hosted, macOS, unreal]
    timeout-minutes: 120
    env:
      UE_ROOT: /Users/Shared/Epic Games/UE_5.8
      # Your project file, relative to the repo root
      UPROJECT: MyGame.uproject

    steps:
      - name: Check out
        uses: actions/checkout@v7
        with:
          lfs: true
          # Keep Intermediate/ and DerivedDataCache/ between runs, so builds and cooks are incremental
          clean: false

      - name: Set up Node
        uses: actions/setup-node@v7
        with:
          node-version: 22
          cache: npm
          cache-dependency-path: specter/package-lock.json

      - name: Install Specter test dependencies
        working-directory: specter
        run: npm ci

      - name: Build, cook and package (Mac, Development)
        run: |
          "$UE_ROOT/Engine/Build/BatchFiles/RunUAT.sh" BuildCookRun \
            -project="$GITHUB_WORKSPACE/$UPROJECT" \
            -platform=Mac -clientconfig=Development \
            -build -cook -stage -package -pak -archive -archivedirectory="$GITHUB_WORKSPACE/ci-build" \
            -unattended -utf8output -nop4

      - name: Run Specter tests against the packaged game
        run: bash scripts/ci/run-specter-tests.sh "$GITHUB_WORKSPACE/ci-build"

      - name: Upload report, traces and logs
        id: results
        if: always()
        uses: actions/upload-artifact@v7
        with:
          name: specter-results-${{ github.run_number }}
          path: |
            specter/playwright-report/
            specter/test-results/
            specter/specter-traces/
            ci-logs/
          if-no-files-found: warn
          retention-days: 14

      # The run summary, and on pull requests one comment kept up to date: each failed test with the game frame
      # it failed on, its error, and links to the line and the trace
      - name: Report results
        if: always()
        continue-on-error: true
        env:
          GITHUB_TOKEN: ${{ github.token }}
          ARTIFACT_URL: ${{ steps.results.outputs.artifact-url }}
        run: node scripts/ci/report-failures.mjs specter/test-results/results.json
