name: E2E Tests

on:
  push:
    branches: [v2-mission-control]
    paths:
      - 'v2/src/**'
      - 'v2/src-tauri/**'
      - 'v2/agor-core/**'
      - 'v2/tests/e2e/**'
      - '.github/workflows/e2e.yml'
  pull_request:
    branches: [master, v2-mission-control]
    paths:
      - 'v2/src/**'
      - 'v2/src-tauri/**'
      - 'v2/agor-core/**'
      - 'v2/tests/e2e/**'
  workflow_dispatch:

permissions:
  contents: read

env:
  CARGO_TERM_COLOR: always

jobs:
  unit-tests:
    runs-on: ubuntu-22.04
    steps:
      - uses: actions/checkout@v4

      - name: Setup Node.js
        uses: actions/setup-node@v4
        with:
          node-version: 20
          cache: npm
          cache-dependency-path: v2/package-lock.json

      - name: Install npm dependencies
        working-directory: v2
        run: npm ci --legacy-peer-deps

      - name: Run Vitest
        working-directory: v2
        run: npm run test

  cargo-tests:
    runs-on: ubuntu-22.04
    steps:
      - uses: actions/checkout@v4

      - name: Install system dependencies
        run: |
          sudo apt-get update
          sudo apt-get install -y \
            libwebkit2gtk-4.1-dev \
            libgtk-3-dev \
            libayatana-appindicator3-dev \
            librsvg2-dev \
            libssl-dev \
            build-essential \
            pkg-config

      - name: Setup Rust
        uses: dtolnay/rust-toolchain@stable

      - name: Cache Rust dependencies
        uses: actions/cache@v4
        with:
          path: |
            ~/.cargo/registry
            ~/.cargo/git
            v2/target
          key: ${{ runner.os }}-cargo-test-${{ hashFiles('v2/Cargo.lock') }}
          restore-keys: |
            ${{ runner.os }}-cargo-test-

      - name: Run cargo tests
        working-directory: v2/src-tauri
        run: cargo test

  e2e-tests:
    runs-on: ubuntu-22.04
    needs: [unit-tests, cargo-tests]
    steps:
      - uses: actions/checkout@v4

      - name: Install system dependencies
        run: |
          sudo apt-get update
          sudo apt-get install -y \
            libwebkit2gtk-4.1-dev \
            libgtk-3-dev \
            libayatana-appindicator3-dev \
            librsvg2-dev \
            libssl-dev \
            build-essential \
            pkg-config \
            xvfb

      - name: Setup Node.js
        uses: actions/setup-node@v4
        with:
          node-version: 20
          cache: npm
          cache-dependency-path: v2/package-lock.json

      - name: Setup Rust
        uses: dtolnay/rust-toolchain@stable

      - name: Cache Rust dependencies
        uses: actions/cache@v4
        with:
          path: |
            ~/.cargo/registry
            ~/.cargo/git
            v2/target
          key: ${{ runner.os }}-cargo-e2e-${{ hashFiles('v2/Cargo.lock') }}
          restore-keys: |
            ${{ runner.os }}-cargo-e2e-

      - name: Install tauri-driver
        run: cargo install tauri-driver

      - name: Install npm dependencies
        working-directory: v2
        run: npm ci --legacy-peer-deps

      - name: Build debug binary
        working-directory: v2
        run: npx tauri build --debug --no-bundle

      - name: Run E2E tests (Phase A — deterministic)
        working-directory: v2
        env:
          AGOR_TEST: '1'
          SKIP_BUILD: '1'
        run: |
          xvfb-run --auto-servernum --server-args="-screen 0 1920x1080x24" \
            npx wdio tests/e2e/wdio.conf.js \
            --spec tests/e2e/specs/agor.test.ts \
            --spec tests/e2e/specs/agent-scenarios.test.ts

      - name: Run E2E tests (Phase B — multi-project)
        if: success()
        working-directory: v2
        env:
          AGOR_TEST: '1'
          SKIP_BUILD: '1'
        run: |
          xvfb-run --auto-servernum --server-args="-screen 0 1920x1080x24" \
            npx wdio tests/e2e/wdio.conf.js \
            --spec tests/e2e/specs/phase-b.test.ts

      # LLM-judged tests only run when API key is available (manual/dispatch)
      - name: Run E2E tests (Phase B — LLM-judged)
        if: success() && env.ANTHROPIC_API_KEY != ''
        working-directory: v2
        env:
          AGOR_TEST: '1'
          SKIP_BUILD: '1'
          ANTHROPIC_API_KEY: ${{ secrets.ANTHROPIC_API_KEY }}
        run: |
          xvfb-run --auto-servernum --server-args="-screen 0 1920x1080x24" \
            npx wdio tests/e2e/wdio.conf.js \
            --spec tests/e2e/specs/phase-b.test.ts

      - name: Upload test results
        if: always()
        uses: actions/upload-artifact@v4
        with:
          name: e2e-results
          path: v2/test-results/
          if-no-files-found: ignore
