name: Spec tests

on:
  push:
    # We intentionally don't run push on feature branches. See PR for rational.
    branches: [unstable, stable]
  pull_request:
  workflow_dispatch:

jobs:
  tests-spec:
    name: Spec tests
    runs-on: ubuntu-latest
    steps:
      # As of October 2020, runner has +8GB of free space w/out this script (takes 1m30s to run)
      # - run: ./scripts/free-disk-space.sh

      # <common-build> - Uses YAML anchors in the future
      - uses: actions/checkout@v2
      - uses: actions/setup-node@v2
        with:
          node-version: 18
      - name: Node.js version
        id: node
        run: echo "v8CppApiVersion=$(node --print "process.versions.modules")" >> $GITHUB_OUTPUT
      - name: Restore dependencies
        uses: actions/cache@master
        id: cache-deps
        with:
          path: |
            node_modules
            packages/*/node_modules
          key: ${{ runner.os }}-${{ steps.node.outputs.v8CppApiVersion }}-${{ hashFiles('**/yarn.lock', '**/package.json') }}
      - name: Install & build
        if: steps.cache-deps.outputs.cache-hit != 'true'
        run: yarn install --frozen-lockfile --ignore-optional && yarn build
      - name: Build
        run: yarn build
        if: steps.cache-deps.outputs.cache-hit == 'true'
      # </common-build>

      # Download spec tests with cache
      - name: Restore spec tests cache
        uses: actions/cache@master
        with:
          path: packages/beacon-node/spec-tests
          key: spec-test-data-${{ hashFiles('packages/beacon-node/test/spec/specTestVersioning.ts') }}
      - name: Download spec tests
        run: yarn download-spec-tests
        working-directory: packages/beacon-node

      # Run them in different steps to quickly identifying which command failed
      # Otherwise just doing `yarn test:spec` you can't tell which specific suite failed
      # many of the suites have identical names for minimal and mainnet
      - name: Spec tests bls-general
        run: yarn test:spec-bls-general
        working-directory: packages/beacon-node
      - name: Spec tests minimal
        run: yarn test:spec-minimal
        working-directory: packages/beacon-node
      - name: Spec tests mainnet
        run:  NODE_OPTIONS='--max-old-space-size=4096' yarn test:spec-mainnet
        working-directory: packages/beacon-node
