name: test
on:
  push:
    branches:
    - 'main'
  pull_request:
  workflow_dispatch:

defaults:
  run:
    shell: bash

permissions:
  contents: read

jobs:
  test:
    runs-on: ${{ matrix.os }}
    strategy:
      fail-fast: false
      # Test node 18/20/22/24 on ubuntu
      # Test node 24 on macos/windows
      # Enable annotations only for node 24 + ubuntu
      matrix:
        os: [ubuntu-latest]
        node: [20, 22, 24, 25, 26]
        include:
        - os: macos-latest
          node: 24
        - os: windows-latest
          node: 24
        - os: ubuntu-latest
          is_base_os_version: true
        - node: 24
          is_base_node_version: true
    steps:
      - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd
        with:
          fetch-depth: 1
      - uses: actions/setup-node@48b55a011bda9f5d6aeb4c2d9c7362e8dae4041e
        with:
          node-version: ${{ matrix.node }}

      - name: npm ci
        run: |
          npm ci
        if: ${{ (matrix.is_base_node_version && matrix.is_base_os_version) }}
      - name: npm ci
        run: |
          NPM_CONFIG_ENGINE_STRICT=false npm ci --ignore-scripts
        if: ${{ !(matrix.is_base_node_version && matrix.is_base_os_version) }}

      - name: determine modified workspaces
        run: |
          echo "MODIFIED_WORKSPACES=$(node './.github/bin/modified-workspaces/log-modified-workspaces.mjs')" >> $GITHUB_ENV
        env:
          GITHUB_ACTION_PULL_REQUEST_NUMBER: ${{ github.event.pull_request.number }}
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

        # Build and PostCSS Tape tests must all work and pass :
        # - with exact dependencies from package-lock.json
        # - without requiring postinstall scripts from dependencies to run
      - name: build
        run: |
          npm run build --if-present $MODIFIED_WORKSPACES
        if: matrix.is_base_node_version && matrix.is_base_os_version
        env:
          NODE_COMPILE_CACHE: '.cache/v8'

      - name: repo is clean after "build", "lint" and "docs" steps
        run: |
          git diff --name-only --exit-code

      # Starting with Ubuntu 23+, a security feature prevents running Puppeteer
      # by default. It needs to be disabled. Using the "easiest" option, see:
      # https://chromium.googlesource.com/chromium/src/+/main/docs/security/apparmor-userns-restrictions.md
      # https://github.com/puppeteer/puppeteer/pull/13196/files
      - name: Disable AppArmor
        run: echo 0 | sudo tee /proc/sys/kernel/apparmor_restrict_unprivileged_userns
        if: matrix.is_base_node_version && matrix.is_base_os_version

        # Basic tests
      - name: test
        run: npm run test --if-present $MODIFIED_WORKSPACES
        env:
          BROWSER_TESTS: ${{ (matrix.is_base_node_version && matrix.is_base_os_version) }}
          ENABLE_ANNOTATIONS_FOR_NODE: ${{ matrix.is_base_node_version }}
          ENABLE_ANNOTATIONS_FOR_OS: ${{ matrix.is_base_os_version }}

        # CLI Tests
        # running "npm install" places the cli in node_modules/bin
      - name: test:cli
        run: |
          NPM_CONFIG_ENGINE_STRICT=false npm ci --ignore-scripts
          npm run test:cli --if-present $MODIFIED_WORKSPACES

        # E2E Tests
      - name: test:e2e
        run: |
          cd e2e
          npm install
          npm run build
          npm run test
        if: matrix.is_base_node_version && matrix.is_base_os_version

        # E2E Package Managers Tests
      - name: test:e2e-package-managers
        run: |
          cd e2e-package-managers/yarn && bash test.sh
        if: matrix.is_base_node_version && matrix.is_base_os_version
