name: CI — Node packages

on:
  push:
    paths:
      - 'packages/**'
  pull_request:
    paths:
      - 'packages/**'

jobs:
  test-packages:
    name: Test packages/*
    runs-on: ubuntu-latest

    steps:
      - name: Checkout
        uses: actions/checkout@v4

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

      - name: Run tests for each package
        shell: bash
        run: |
          set -euo pipefail

          # Find all package directories under packages/ that contain a package.json
          packages=(packages/*)
          found=0

          for p in "${packages[@]}"; do
            if [ -d "$p" ] && [ -f "$p/package.json" ]; then
              found=1
              echo "\n===== Package: $p ====="

              echo "-> Installing dependencies in $p"
              (cd "$p" && npm ci) || (cd "$p" && npm install)

              echo "-> Running tests in $p"
              (cd "$p" && npm test)

              echo "-> Running pack-inspect in $p"
              (cd "$p" && npm run pack-inspect)
            fi
          done

          if [ "$found" -eq 0 ]; then
            echo "No packages with package.json found under packages/"
          fi
