name: Quality Checks

on:
  pull_request:
    branches:
      - main
  merge_group:
    types: [ checks_requested ]

jobs:
  release:
    # Using the same Ubuntu version as in the Dockerfile for consistency
    runs-on: ubuntu-22.04
    steps:
      - name: Checkout code
        uses: actions/checkout@v4
        with:
          fetch-depth: 0

      - name: Setup pnpm
        uses: pnpm/action-setup@v4

      - name: Setup Node.js
        uses: actions/setup-node@v4
        with:
          node-version: 24
          cache: 'pnpm'

      - name: Install dependencies
        run: pnpm install --frozen-lockfile

      - name: Type checks
        run: pnpm type-check

      - name: Linting
        run: pnpm lint

      # Formatting is only enforced on the files a change actually touches: the
      # codebase predates any enforced formatter run, so main still carries drift
      # that a repo-wide check would flag. Biome compares against `main`, which
      # needs to exist locally (checkout leaves us on a detached ref).
      - name: Restore main ref for changed-file detection
        run: git branch -f main origin/main

      - name: Code formatting (changed files)
        run: pnpm format:changed

      - name: Build
        run: pnpm build

      # Needs no secret, so it also gates pull requests from forks.
      - name: E2E — tool listing and app resources over HTTP
        run: pnpm test:e2e:protocol

      # The map-worker spec needs no API key and catches a MapLibre or
      # app-bundling change that leaves the map rendering blank.
      - name: Install Chromium for Playwright
        run: pnpm exec playwright install --with-deps chromium

      - name: E2E — MCP App map rendering
        run: pnpm exec playwright test e2e/mapWorker.spec.ts

      - name: Build the spec host
        run: pnpm ui:build

      # Each tool runs in the spec host and its app must draw, with real API calls,
      # also under the ChatGPT and Claude quirks the host profiles reproduce.
      - name: E2E — every tool renders its app in the spec host and the host profiles
        run: pnpm exec playwright test e2e/tools.spec.ts e2e/hostProfiles.spec.ts
        env:
          TOMTOM_API_KEY: ${{ secrets.TOMTOM_API_KEY }}

      - name: Upload the e2e screenshots and error context
        if: failure()
        uses: actions/upload-artifact@v4
        with:
          name: e2e-test-results
          path: test-results/
          retention-days: 7

      - name: Unit tests + integration tests
        run: pnpm test:all
        env:
          TOMTOM_API_KEY: ${{ secrets.TOMTOM_API_KEY }}

      - name: Soak — 1,000 requests, heap and sockets
        run: pnpm test:soak

