name: Test

on:
  push:
    branches: [main, develop]
  pull_request:
    branches: [main]

jobs:
  # ─────────────────────────────────────────────────────────────────────
  # Backend: unit tests + lint
  # ─────────────────────────────────────────────────────────────────────
  backend-test:
    runs-on: ubuntu-22.04
    timeout-minutes: 15
    steps:
      - uses: actions/checkout@v4

      - name: Setup Python
        uses: actions/setup-python@v5
        with:
          python-version: '3.11'
          cache: pip

      - name: Install dependencies
        run: |
          python -m pip install --upgrade pip
          pip install -e ".[dev]"

      - name: Ruff lint
        run: ruff check src/

      - name: Ruff format check
        run: ruff format --check src/

      - name: Unit tests
        run: pytest tests/unit/ -x -q --tb=short
        env:
          AGENTKIT_JWT_SECRET: ci-test-secret-do-not-use-in-prod

  # ─────────────────────────────────────────────────────────────────────
  # Frontend: unit tests + typecheck
  # ─────────────────────────────────────────────────────────────────────
  frontend-unit:
    runs-on: ubuntu-22.04
    timeout-minutes: 10
    defaults:
      run:
        working-directory: src/agentkit/server/frontend
    steps:
      - uses: actions/checkout@v4

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

      - name: Install dependencies
        run: npm ci

      - name: Typecheck
        run: npm run typecheck

      - name: Unit tests
        run: npm run test:unit

  # ─────────────────────────────────────────────────────────────────────
  # Backend API E2E (needs Redis + PostgreSQL)
  # ─────────────────────────────────────────────────────────────────────
  api-e2e:
    runs-on: ubuntu-22.04
    timeout-minutes: 20
    services:
      redis:
        image: redis:7-alpine
        ports:
          - 6381:6379
        options: >-
          --health-cmd "redis-cli ping"
          --health-interval 5s
          --health-timeout 3s
          --health-retries 5
      postgres:
        image: pgvector/pgvector:pg15
        env:
          POSTGRES_USER: agentkit_test
          POSTGRES_PASSWORD: agentkit_test_pw
          POSTGRES_DB: agentkit_test
        ports:
          - 5434:5432
        options: >-
          --health-cmd "pg_isready -U agentkit_test -d agentkit_test"
          --health-interval 5s
          --health-timeout 3s
          --health-retries 5
    env:
      AGENTKIT_JWT_SECRET: ci-test-secret-do-not-use-in-prod
      AGENTKIT_REDIS_URL: redis://localhost:6381/0
      AGENTKIT_DATABASE_URL: postgresql+asyncpg://agentkit_test:agentkit_test_pw@localhost:5434/agentkit_test
    steps:
      - uses: actions/checkout@v4

      - name: Setup Python
        uses: actions/setup-python@v5
        with:
          python-version: '3.11'
          cache: pip

      - name: Install dependencies
        run: |
          python -m pip install --upgrade pip
          pip install -e ".[dev]"

      - name: API E2E tests
        run: pytest tests/e2e/test_api_coverage.py -v --tb=short
        env:
          AGENTKIT_API_KEY: ci-test-api-key

  # ─────────────────────────────────────────────────────────────────────
  # Frontend E2E (Playwright — needs backend + frontend + Chrome)
  # ─────────────────────────────────────────────────────────────────────
  frontend-e2e:
    runs-on: ubuntu-22.04
    timeout-minutes: 30
    defaults:
      run:
        working-directory: src/agentkit/server/frontend
    env:
      CI: 'true'
      AGENTKIT_JWT_SECRET: ci-test-secret-do-not-use-in-prod
    steps:
      - uses: actions/checkout@v4

      - name: Setup Python
        uses: actions/setup-python@v5
        with:
          python-version: '3.11'
          cache: pip

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

      - name: Install backend dependencies
        run: |
          python -m pip install --upgrade pip
          pip install -e ".[dev]"
        working-directory: ${{ github.workspace }}

      - name: Install frontend dependencies
        run: npm ci

      - name: Install Playwright browsers
        run: npx playwright install --with-deps chromium

      - name: Run E2E tests
        run: npx playwright test --reporter=list

      - name: Upload test results
        if: failure()
        uses: actions/upload-artifact@v4
        with:
          name: playwright-report
          path: src/agentkit/server/frontend/playwright-report/
          retention-days: 7

      - name: Upload test artifacts
        if: failure()
        uses: actions/upload-artifact@v4
        with:
          name: test-results
          path: src/agentkit/server/frontend/test-results/
          retention-days: 7
