name: Test

on:
  push:
    branches:
      - main
  pull_request:

# No GITHUB_TOKEN permissions, as we don't use it.
permissions: {}

jobs:
  test:
    runs-on: ubuntu-latest

    services:
      postgres:
        image: postgres:16
        env:
          POSTGRES_USER: observatory
          POSTGRES_PASSWORD: observatory
          POSTGRES_DB: observatory
        ports:
          - 5432:5432
        options: --health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 5

    steps:
      - name: Checkout
        uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
        with:
          persist-credentials: false

      - name: Setup Node
        uses: actions/setup-node@820762786026740c76f36085b0efc47a31fe5020 # v7.0.0
        with:
          node-version-file: .nvmrc
          package-manager-cache: false

      - name: Install
        run: npm ci

      - name: Run tests
        run: npm run test:coverage
        env:
          FORCE_COLOR: 1
          PGDATABASE: observatory
          PGHOST: localhost
          PGUSER: observatory
          PGPASSWORD: observatory

  build:
    runs-on: ubuntu-latest

    steps:
      - name: Checkout
        uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
        with:
          persist-credentials: false

      - name: Setup Docker
        uses: docker/setup-buildx-action@37fe631027851001ddb9b187196cc803df7f5f0e # v4.3.0

      - name: Build Docker image
        uses: docker/build-push-action@53b7df96c91f9c12dcc8a07bcb9ccacbed38856a # v7.3.0
        with:
          context: .
          build-args: |
            GIT_SHA=${{ github.sha }}
          push: false
          cache-from: type=gha
          cache-to: type=gha,mode=max

  pack:
    runs-on: ubuntu-latest

    outputs:
      artifact-id: ${{ steps.upload.outputs.artifact-id }}

    steps:
      - name: Checkout
        uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
        with:
          persist-credentials: false

      - name: Setup Node
        uses: actions/setup-node@820762786026740c76f36085b0efc47a31fe5020 # v7.0.0
        with:
          node-version-file: .nvmrc
          package-manager-cache: false

      - name: Install
        run: npm ci

      - name: Build tarball
        id: build
        run: |
          npm pack
          TARBALL=$(ls mdn-mdn-http-observatory-*.tgz)
          echo "$TARBALL"
          ls -lh "$TARBALL"
          echo "tarball=$TARBALL" >> "$GITHUB_OUTPUT"

      - name: Upload tarball
        id: upload
        uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1
        with:
          path: ${{ steps.build.outputs.tarball }}
          archive: false

  test-cli:
    needs: pack
    strategy:
      matrix:
        os:
          - ubuntu-latest
          - macos-latest
          - windows-latest
        node-version:
          - 24
          - 26
    runs-on: ${{ matrix.os }}

    steps:
      - name: Setup Node
        uses: actions/setup-node@820762786026740c76f36085b0efc47a31fe5020 # v7.0.0
        with:
          node-version: ${{ matrix.node-version }}

      - name: Download tarball
        uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8.0.1
        with:
          artifact-ids: ${{ needs.pack.outputs.artifact-id }}

      - name: Install tarball
        shell: bash
        run: |
          TARBALL=$(ls mdn-mdn-http-observatory-*.tgz)
          npm install -g "$TARBALL"

      - name: Test --help output
        shell: bash
        run: |
          output=$(mdn-http-observatory-scan --help)
          echo "$output"
          echo "$output" | grep "Usage: mdn-http-observatory-scan"

      - name: Checkout scripts
        uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
        with:
          sparse-checkout: .github/scripts
          sparse-checkout-cone-mode: false
          persist-credentials: false

      - name: Scan a local HTTP server
        uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0
        env:
          SCRIPT_PATH: ${{ github.workspace }}/.github/scripts/scan-local-server.js
        with:
          script: |
            const { pathToFileURL } = await import("node:url");

            const scriptUrl = pathToFileURL(process.env.SCRIPT_PATH).href;
            const { scanLocalServer } = await import(scriptUrl);

            await scanLocalServer(core);

  lint:
    runs-on: ubuntu-latest

    steps:
      - name: Checkout
        uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
        with:
          persist-credentials: false

      - name: Setup Node
        uses: actions/setup-node@820762786026740c76f36085b0efc47a31fe5020 # v7.0.0
        with:
          node-version-file: ".nvmrc"
          package-manager-cache: false

      - name: Install
        run: npm ci

      - name: Run prettier
        run: npx prettier --check .

      - name: Run ESLint
        run: npm run lint

      - name: Run tsc
        run: npm run tsc
