name: unit testing
on:
    push:
    pull_request:
env:
    NODE_VERSION: 22
jobs:
    build-and-test:
        runs-on: ubuntu-latest
        steps:
            # Checks out code from Github.
            - name: Checkout repo
              uses: actions/checkout@v3
            - uses: actions/setup-node@v4
              with:
                  node-version: ${{ env.NODE_VERSION }}
            - name: Log versions
              run: echo "Using Node $(node --version), npm $(npm --version)"
            # Restore cache if available.
            - name: Restore cached dependencies
              id: dep-cache
              uses: actions/cache@v3
              env:
                  cache-name: jstoxml-cache
              with:
                  path: node_modules
                  key: ${{ env.NODE_VERSION }}${{ runner.os }}-build-${{ env.cache-name }}-${{ hashFiles('**/package-lock.json') }}
                  restore-keys: |
                      ${{ runner.os }}-build-${{ env.cache-name }}-
                      ${{ runner.os }}-build-
                      ${{ runner.os }}-
            # Fully install from scratch when no cache is available.
            - name: Install dependencies from scratch (cache miss only)
              if: steps.dep-cache.outputs.cache-hit != 'true'
              run: npm i
            - name: Unit tests
              run: npm test
              shell: bash
