name: CI

on: [push, pull_request]

permissions:
  contents: read # to fetch code (actions/checkout)

jobs:
  build:
    # Skip pull_request events from PRs in the same repo. This prevents
    # duplicate build jobs from running when creating a PR in the original repo.
    if: ${{ github.event_name == 'push' || github.event.pull_request.head.repo.full_name != github.repository }}

    concurrency:
      group: ${{ github.workflow }}-${{ github.ref }}-${{ matrix.platform }}-${{ matrix.node[0] }}
      cancel-in-progress: true

    strategy:
      fail-fast: false
      matrix:
        node:
          - [20, 19, 4]
          - [22, 12, 0]
          - [24, 0, 0]
          - [25, 0, 0]
        platform:
          - ubuntu-latest
          - windows-latest

    name: ${{matrix.platform}} / Node.js ${{ matrix.node[0] }}
    runs-on: ${{matrix.platform}}

    steps:
    - name: Configure Git
      run: |
        git config --global core.autocrlf false
        git config --global user.name "xyz"
        git config --global user.email "x@y.z"
    - name: Checkout Commit
      uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0
    - name: Install pnpm
      uses: pnpm/action-setup@41ff72655975bd51cab0327fa583b6e92b6d3061 # v4.2.0
      with:
        standalone: true
    - name: Setup Node
      run: pnpm env use -g ${{ join(matrix.node, '.') }}
      timeout-minutes: 1
    - name: Install npm@8
      run: pnpm add --global npm@8
      timeout-minutes: 1
    - name: pnpm install
      run: pnpm install
      timeout-minutes: 1
    # - name: Cache TypeScript and Jest
      # uses: actions/cache@v3
      # with:
        # path: |
          # packages/*/lib
          # packages/*/tsconfig.tsbuildinfo
          # privatePackages/*/lib
          # privatePackages/*/tsconfig.tsbuildinfo
          # .jest-cache
          # .verdaccio-cache
        # key: ts-jest-${{ matrix.platform }}-${{ matrix.node }}-${{ github.run_id }}
        # restore-keys: ts-jest-${{ matrix.platform }}-${{ matrix.node }}-
    - name: run tests (main)
      timeout-minutes: 60
      if: github.ref_name == 'main'
      run: pnpm run test-main
      env:
        PNPM_WORKERS: 3
    - name: run tests (branch)
      timeout-minutes: 60
      if: github.ref_name != 'main'
      run: pnpm run test-branch
      env:
        PNPM_WORKERS: 3
