name: Run browser tests

on:
  # Runs for pushes, pull requests, and manual triggers.
  # (Dooes not run when only some config is changed.)
  push:
    branches:
      - main
    paths-ignore:
      - 'src/config/*'
  pull_request:
    branches:
      - main
    types:
      - opened
      - synchronize
      - reopened
      - ready_for_review
    paths-ignore:
      - 'src/config/**'
  workflow_dispatch:
    inputs:
      test_results_retention_days:
        description: 'Test results retention (in days)'
        required: true
        default: '7'
      node_version:
        description: 'NodeJS version'
        required: true
        default: '22.x'

jobs:
  test-browser:
    name: Run browser tests
    timeout-minutes: 10
    strategy:
      fail-fast: false
      matrix:
        include:

          # Extended support releases
          - channel: esr
            firefox: latest-esr

          # Stable releases
          - channel: stable
            chrome: stable
            firefox: latest
            coverage: true

          # Beta releases
          - channel: beta
            chrome: beta
            firefox: latest-beta

    runs-on: ubuntu-latest

    steps:
      - name: Check out code
        uses: actions/checkout@v4

      - name: Install Chrome
        if: ${{ matrix.chrome }}
        uses: browser-actions/setup-chrome@latest
        with:
          chrome-version: ${{ matrix.chrome }}

      - name: Configure Karma with path to Chrome executable
        if: ${{ matrix.chrome }}
        run: |
          chrome --version
          { echo CHROME_BIN="$(which chrome)"; echo CHROME_TEST=1; } | tee -a "$GITHUB_ENV"

      - name: Install Firefox
        if: ${{ matrix.firefox }}
        uses: browser-actions/setup-firefox@latest
        with:
          firefox-version: ${{ matrix.firefox }}
      - name: Configure Karma with path to Firefox executable
        if: ${{ matrix.firefox }}
        run: |
          firefox --version
          { echo FIREFOX_BIN="$(which firefox)"; echo FIREFOX_TEST=1; } | tee -a "$GITHUB_ENV"

      - name: Node setup
        uses: actions/setup-node@v4
        with:
          node-version: ${{ inputs.node_version || '22.x' }}
          cache: npm

      - name: Install NPM dependencies
        run: npm ci

      - name: Deps
        run: cat package-lock.json

      - name: Run injection tests
        run: npm run test:inject -- --ci --json --outputFile=test-results-inject.json

      - name: Run browser tests on Chromium MV2 build
        if: ${{ matrix.chrome }}
        uses: coactions/setup-xvfb@v1
        with:
          run: npm run test:chrome -- --json --outputFile=test-results-chrome.json

      - name: Run browser tests on Chromium MV3 build
        if: ${{ matrix.chrome }}
        uses: coactions/setup-xvfb@v1
        with:
          run: npm run test:chrome-mv3 -- --json --outputFile=test-results-chrome-mv3.json

      - name: Run browser tests on Firefox MV2 build
        if: ${{ matrix.firefox }}
        uses: coactions/setup-xvfb@v1
        with:
          run: npm run test:firefox -- --json --outputFile=test-results-firefox-mv2.json

      - name: Upload test results
        uses: actions/upload-artifact@v4
        with:
          name: test-results-chrome-${{ matrix.chrome }}-firefox-${{ matrix.firefox }}-coverage-${{ matrix.coverage }}
          path: test-results-*
          retention-days: ${{ inputs.test_results_retention_days }}
