name: CI

on:
  push:
    branches:
      - main
  pull_request: {}

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

jobs:
  test:
    name: 'Test ${{ matrix.testenv.name }}'
    runs-on: ubuntu-latest
    timeout-minutes: 5
    strategy:
      matrix:
        testenv:
          - {name: 'Node', args: ''}
          - {name: 'Chrome', args: '--browser.name=chrome --browser.headless'}
          - {name: 'Firefox', args: '--browser.name=firefox --browser.headless'}

    steps:
      - uses: actions/checkout@v4
      - uses: wyvox/action-setup-pnpm@v3
      - run: pnpm install --no-lockfile
      - run: pnpm lint
      - run: pnpm build
      - run: pnpm vitest ${{ matrix.testenv.args }}

  benchmarks:
    runs-on: ubuntu-latest
    timeout-minutes: 10
    permissions:
      contents: write
      pull-requests: write
    steps:
      - uses: actions/checkout@v4
      - uses: wyvox/action-setup-pnpm@v3
      - run: pnpm install
      - run: pnpm benchmarks
      - uses: benchmark-action/github-action-benchmark@d48d326b4ca9ba73ca0cd0d59f108f9e02a381c7 #v1.20.4
        with:
          name: Benchmarks
          tool: 'customSmallerIsBetter'
          output-file-path: benchmarks.json
          auto-push: ${{ github.event_name == 'push' }}
          github-token: ${{ secrets.GITHUB_TOKEN }}
          comment-on-alert: true
          summary-always: true

## 
# NOTE: in ration is <1, the performance has improved.
      - uses: mshick/add-pr-comment@v2
        with:
          message: |
            ## Benchmarks[^note]
            
            Results for the latest run for this branch:

            https://github.com/proposal-signals/signal-polyfill/actions/runs/${{ github.run_id }}

            ---------------

            Compare with `main` over time:

            https://proposal-signals.github.io/signal-polyfill/dev/bench/

            [^note]: When the ratio is < 1, performance has improved in the PR.
