name: 🎭 E2E Tests

on:
  push:
    branches: [main]
  pull_request:
    branches: [main]
    paths:
      - "e2e/**"
      - ".github/workflows/e2e-tests.yml"

permissions:
  contents: read

jobs:
  e2e-tests:
    name: 🎭 Run E2E Tests
    runs-on: ubuntu-latest
    permissions:
      contents: read

    steps:
      - uses: actions/checkout@df4cb1c069e1874edd31b4311f1884172cec0e10 # v6
        with:
          persist-credentials: false

      - name: Setup Bun
        uses: oven-sh/setup-bun@0c5077e51419868618aeaa5fe8019c62421857d6 # v2

      - name: Install dependencies
        run: |
          cd e2e
          bun install

      - name: Install Playwright browsers
        run: |
          cd e2e
          bunx playwright install --with-deps

      - name: Install uv
        uses: astral-sh/setup-uv@fac544c07dec837d0ccb6301d7b5580bf5edae39 # v8.2.0
        with:
          enable-cache: true
          python-version: "3.12"

      - name: Install Python dependencies
        run: |
          uv sync
          uv pip install graphql-core==3.3.0rc0

      - name: Start FastAPI server
        run: |
          cd e2e
          # --no-sync prevents uv from reverting graphql-core to lockfile version
          uv run --no-sync fastapi dev app.py --port 8000 &
          echo $! > server.pid
          sleep 5  # Wait for server to start

      - name: Check if server is running
        run: |
          curl -f http://localhost:8000/graphql || (echo "Server is not running" && exit 1)
          echo "FastAPI GraphQL server is running successfully"

      - name: Run Playwright tests
        run: |
          cd e2e
          bunx playwright test

      - uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1
        if: always()
        with:
          name: playwright-report
          path: e2e/playwright-report/
          retention-days: 30
