name: run tests

on:
  push:
    branches:
      - master
    paths-ignore:
      - 'docs/**'
      - '**.md'
  pull_request:
    paths-ignore:
      - 'docs/**'
      - '**.md'

# This allows a subsequently queued workflow run to interrupt previous runs
concurrency:
  group: "${{ github.workflow }} @ ${{ github.event.pull_request.head.label || github.head_ref || github.ref }}"
  cancel-in-progress: true

jobs:
  setup-node_modules:
    runs-on:
      - ${{ matrix.os }}
    timeout-minutes: 15
    strategy:
      matrix:
        os: [ubuntu-latest, windows-latest, macos-latest]
    steps:
      - uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4
      - uses: pnpm/action-setup@v3.0.0
        with:
          version: 8
      - uses: actions/setup-node@v4
        with:
          node-version: 20
          cache: 'pnpm'
      - uses: "nick-fields/retry@v2.9.0"
        with:
          max_attempts: 10
          timeout_minutes: 15
          retry_on: error
          command: pnpm fetch --ignore-scripts
  
  ci-tests:
    needs: setup-node_modules
    runs-on: 
      - ${{ matrix.os }}
    timeout-minutes: 15
    strategy:
      matrix:
        node-version: [18, 20]
        os: [ubuntu-latest, windows-latest, macos-latest]
    steps:
    - uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4
    - uses: pnpm/action-setup@v3.0.0
      with:
        version: 8
    - uses: actions/setup-node@v4
      with:
        node-version: ${{ matrix.node-version }}
        cache: 'pnpm'
    - name: pnpm install
      uses: nick-fields/retry@v2.9.0
      with:
        max_attempts: 10
        timeout_minutes: 15
        retry_on: error
        command: pnpm install --frozen-lockfile
    - name: Run test suite
      run: pnpm test

  ci-build:
    needs: ci-tests
    runs-on: 
      - ${{ matrix.os }}
    timeout-minutes: 15
    strategy:
      matrix:
        os: [ubuntu-latest, windows-latest, macos-latest]
    steps:
    - uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4
    - uses: pnpm/action-setup@v3.0.0
      with:
        version: 8
    - uses: actions/setup-node@v4
      with:
        node-version: 20
        cache: 'pnpm'
    - name: pnpm install
      uses: nick-fields/retry@v2.9.0
      with:
        max_attempts: 10
        timeout_minutes: 15
        retry_on: error
        command: pnpm install --frozen-lockfile
    - name: prebuild
      run:  |
        pnpm prebuild --v8_enable_pointer_compression=false --v8_enable_31bit_smis_on_64bit_arch=false
    - name: Upload
      uses: actions/upload-artifact@v4
      with:
        name: binary-${{ runner.os }}
        path: prebuilds

  release:
    name: Release
    needs: ci-build
    runs-on: ubuntu-latest
    permissions:
      contents: write # to be able to publish a GitHub release
      issues: write # to be able to comment on released issues
      pull-requests: write # to be able to comment on released pull requests
      id-token: write # to enable use of OIDC for npm provenance
    steps:
      - name: Checkout
        uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4
        with:
          fetch-depth: 0
      - uses: pnpm/action-setup@v3.0.0
        with:
          version: 8
      - uses: actions/setup-node@v4
        with:
          node-version: 20
          cache: 'pnpm'
      - name: pnpm install
        uses: nick-fields/retry@v2.9.0
        with:
          max_attempts: 10
          timeout_minutes: 15
          retry_on: error
          command: pnpm install --frozen-lockfile
      - name: Download All Artifacts
        uses: actions/download-artifact@v4
        with:
          path: prebuilds
          pattern: binary-*
          merge-multiple: true
      - name: Release
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
        run: |
          zip -r prebuilds.zip prebuilds
          pnpm semantic-release
