name: Tests

on:
  push:
    branches: [main]
  pull_request:
    branches: [main]

env:
  YARN_ENABLE_GLOBAL_CACHE: false

jobs:
  chore:
    name: "Testing chores"
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v4

      - uses: actions/setup-node@v4
        with:
          node-version: lts/*

      - name: Get the Yarn cache directory path
        id: yarn-cache-dir-path
        run: echo "dir=$(corepack yarn config get cacheFolder)" >> $GITHUB_OUTPUT
        shell: bash

      - uses: actions/cache@v4
        with:
          path: ${{steps.yarn-cache-dir-path.outputs.dir}}
          key: ${{runner.os}}-yarn-${{hashFiles('**/yarn.lock')}}
          restore-keys: |
            ${{runner.os}}-yarn-

      - run: corepack yarn install --immutable

      - name: "Check for type errors"
        run: corepack yarn typecheck

      - name: "Check for linting errors"
        run: corepack yarn lint

  build-and-test:
    strategy:
      fail-fast: false
      matrix:
        node:
          - 18
          - 20
          - 22
          - 23
        platform:
          - ubuntu-latest
          - macos-latest
          - windows-latest

      # Temporarily skipping Node.js 23 under Windows due to issue
      # https://github.com/nodejs/corepack/issues/597
      # ci vitest fails "handle integrity checks" on Windows Node.js 23
        exclude:
          - node: 23
            platform: windows-latest

    name: "${{matrix.platform}} w/ Node.js ${{matrix.node}}.x"
    runs-on: ${{matrix.platform}}

    steps:
      - uses: actions/checkout@v4

      - name: "Use Node.js ${{matrix.node}}.x"
        uses: actions/setup-node@v4
        with:
          node-version: ${{matrix.node}}.x

      - name: Get the Yarn cache directory path
        id: yarn-cache-dir-path
        run: echo "dir=$(corepack yarn config get cacheFolder)" >> $GITHUB_OUTPUT
        shell: bash

      - uses: actions/cache@v4
        with:
          path: ${{steps.yarn-cache-dir-path.outputs.dir}}
          key: ${{runner.os}}-yarn-${{hashFiles('**/yarn.lock')}}
          restore-keys: |
            ${{runner.os}}-yarn-

      - run: corepack yarn install --immutable
      - run: corepack yarn build # We need the stubs to run the tests

      - run: corepack yarn test
        env:
          NOCK_ENV: replay