name: ci

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

concurrency:
  group: ci-${{ github.ref }}
  cancel-in-progress: true

jobs:
  lint-build-test:
    runs-on: ubuntu-latest
    strategy:
      fail-fast: false
      matrix:
        # Matches package.json engines (node >=22.13).
        node-version: ['22', '24']
    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: Setup Node ${{ matrix.node-version }}
        uses: actions/setup-node@v4
        with:
          node-version: ${{ matrix.node-version }}
{{CI_GITHUB_PM_SETUP}}

      - name: Install dependencies
        # Plain install rather than the frozen-lockfile variant because the
        # scaffold ships no lockfile — it is created on the user's first install
        # and should be committed before CI runs. Once the lockfile is
        # committed, switch this line to: {{CI_FROZEN_INSTALL_COMMAND}}
        run: {{CI_INSTALL_COMMAND}}

      - name: Generate type artifacts
        run: npm run generateArtifacts

      - name: Verify artifacts exist
        run: |
          test -s src/_sockets/apiTypes.generated.ts
          test -s src/docs/apiDocs.generated.json
          ls server/prod/generatedApis.*.ts

      - name: Lint
        run: npm run lint

      - name: Type check
        run: npm run typecheck

      - name: Build
        run: npm run build

      - name: Test sweep
        run: npm run test

      - name: E2E (if installed)
        run: |
          if [ -f "node_modules/@playwright/test/package.json" ]; then
            npx playwright install --with-deps chromium
            npm run test:e2e
          else
            echo "Skipping e2e — @playwright/test not installed."
          fi
        continue-on-error: true
