name: Coverage Flake Probe

# Manually-triggered probe for the birpc teardown race documented in ADR 012
# / #553. Runs the full coverage suite 20× in parallel against a single SHA
# and asserts zero `[birpc] rpc is closed` lines across every cell. Verifies
# the acceptance criterion that the race no longer surfaces under coverage.

on:
  workflow_dispatch:

jobs:
  coverage-flake-probe:
    name: Coverage flake probe (run ${{ matrix.run }})
    runs-on: ubuntu-latest
    container:
      image: mcr.microsoft.com/playwright:v1.58.2-noble
    strategy:
      fail-fast: false
      matrix:
        run: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
    steps:
      - uses: actions/checkout@v4

      - name: Cache node_modules
        id: node-modules-cache
        uses: actions/cache@v4
        with:
          path: frontend/node_modules
          key: node-modules-${{ hashFiles('frontend/package-lock.json') }}

      - name: Install dependencies
        if: steps.node-modules-cache.outputs.cache-hit != 'true'
        run: npm ci
        working-directory: frontend

      - name: Compile Paraglide i18n
        run: npx @inlang/paraglide-js compile --project ./project.inlang --outdir ./src/lib/paraglide
        working-directory: frontend

      - name: Run unit and component tests with coverage
        shell: bash
        run: |
          set -eo pipefail
          npm run test:coverage 2>&1 | tee /tmp/coverage-test-${{ github.run_id }}-${{ matrix.run }}.log
        working-directory: frontend
        env:
          TZ: Europe/Berlin

      - name: Assert no birpc teardown race
        shell: bash
        if: always()
        run: |
          if grep -qF "[birpc] rpc is closed" /tmp/coverage-test-${{ github.run_id }}-${{ matrix.run }}.log 2>/dev/null; then
            echo "FAIL: [birpc] rpc is closed teardown race detected in run ${{ matrix.run }}"
            grep -F "[birpc] rpc is closed" /tmp/coverage-test-${{ github.run_id }}-${{ matrix.run }}.log
            exit 1
          fi

      # Gitea Actions (act_runner) does not implement upload-artifact v4 protocol — pinned per ADR-014. Do NOT upgrade. See #557.
      - name: Upload coverage log on failure
        if: failure()
        uses: actions/upload-artifact@v3
        with:
          name: coverage-log-run-${{ matrix.run }}
          path: /tmp/coverage-test-${{ github.run_id }}-${{ matrix.run }}.log
