name: Playwright Tests

on:
  push:
    branches: [main]
  pull_request:
    branches: [main]
    paths:
      - 'web/**'
      - '.github/workflows/playwright.yml'

permissions:
  pull-requests: write
  issues: write

jobs:
  test:
    name: Playwright E2E Tests
    runs-on: blacksmith-16vcpu-ubuntu-2404-arm
    timeout-minutes: 20
    
    steps:
      - name: Checkout code
        uses: actions/checkout@v4
        
      - name: Setup Node.js
        uses: actions/setup-node@v4
        with:
          node-version: '20'
          
      - name: Install pnpm
        uses: pnpm/action-setup@v4
        with:
          version: 10
          
      - name: Get pnpm store directory
        shell: bash
        run: |
          echo "STORE_PATH=$(pnpm store path --silent)" >> $GITHUB_ENV
          
      - name: Setup pnpm cache
        uses: actions/cache@v4
        with:
          path: ${{ env.STORE_PATH }}
          key: ${{ runner.os }}-pnpm-store-${{ hashFiles('**/pnpm-lock.yaml') }}
          restore-keys: |
            ${{ runner.os }}-pnpm-store-
            
      - name: Install system dependencies
        run: |
          sudo apt-get update
          sudo apt-get install -y libpam0g-dev xvfb
          
      - name: Install dependencies
        working-directory: ./web
        run: pnpm install
        
      - name: Build application
        working-directory: ./web
        run: pnpm run build
        env:
          VIBETUNNEL_SEA: "true"
          
      - name: Verify native executable
        working-directory: ./web
        run: |
          echo "Verifying native executable..."
          ls -la native/ || echo "Native directory not found"
          if [ -f native/vibetunnel ]; then
            echo "Native executable found"
            file native/vibetunnel
            ldd native/vibetunnel || echo "ldd failed"
            # Known issue: Node.js SEA executables segfault on ARM64 Linux
            # This affects both Node.js 20 and 24. The executable will be built
            # but we skip the version test and use TypeScript compilation for tests
            echo "⚠️  Skipping --version test on ARM64 Linux due to known Node.js SEA segfault"
            echo "The executable has been built but will not be used for tests"
          else
            echo "ERROR: Native executable not found!"
            exit 1
          fi
        
      - name: Install Playwright browsers
        working-directory: ./web
        run: pnpm exec playwright install --with-deps chromium
        
      - name: Kill any existing processes on port 4022
        run: |
          # Kill any process using port 4022
          if lsof -i :4022; then
            echo "Found process on port 4022, killing it..."
            lsof -ti :4022 | xargs kill -9 || true
          else
            echo "No process found on port 4022"
          fi
        
      - name: Run Playwright tests
        working-directory: ./web
        run: xvfb-run -a pnpm test:e2e:fast
        env:
          CI: true
          TERM: xterm
          SHELL: /bin/bash
          # Disable VIBETUNNEL_SEA on ARM64 Linux due to Node.js SEA segfault issues
          # The test-server.js will fall back to TypeScript compilation
          # VIBETUNNEL_SEA: "true"
          
      - name: Upload test results
        uses: actions/upload-artifact@v4
        if: always()
        with:
          name: playwright-report
          path: web/playwright-report/
          retention-days: 7
          
      - name: Upload test videos
        uses: actions/upload-artifact@v4
        if: always()
        with:
          name: playwright-videos
          path: web/test-results/
          retention-days: 7