name: Test

# Many build steps have their own caches, so each job has its own cache to improve subsequent build times.
# Build tools are configured to cache cache to node_modules/.cache, so this is cached independently of node_modules.
# See https://jongleberry.medium.com/speed-up-your-ci-and-dx-with-node-modules-cache-ac8df82b7bb0.

on:
  push:
    branches:
      - main
  pull_request:
  # manual trigger
  workflow_dispatch:

jobs:
  lint:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - uses: ./.github/actions/setup
      - uses: actions/cache@v3
        id: eslint-cache
        with:
          path: node_modules/.cache
          key: ${{ runner.os }}-eslint-${{ hashFiles('**/yarn.lock') }}-${{ github.run_id }}
          restore-keys: ${{ runner.os }}-eslint-${{ hashFiles('**/yarn.lock') }}-
      - run: yarn lint
  
  typecheck:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - uses: ./.github/actions/setup
      - uses: actions/cache@v3
        id: tsc-cache
        with:
          path: node_modules/.cache
          key: ${{ runner.os }}-tsc-${{ hashFiles('**/yarn.lock') }}-${{ github.run_id }}
          restore-keys: ${{ runner.os }}-tsc-${{ hashFiles('**/yarn.lock') }}-
      - run: yarn prepare
      - run: yarn typecheck

  deps-tests:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - uses: ./.github/actions/setup
      - run: yarn yarn-deduplicate --strategy=highest --list --fail

  unit-tests:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - uses: ./.github/actions/setup
      - uses: actions/cache@v3
        id: jest-cache
        with:
          path: node_modules/.cache
          key: ${{ runner.os }}-jest-${{ hashFiles('**/yarn.lock') }}-${{ github.run_id }}
          restore-keys: ${{ runner.os }}-jest-${{ hashFiles('**/yarn.lock') }}-
      - run: yarn prepare
      - run: yarn test --coverage --maxWorkers=100%
      - uses: codecov/codecov-action@v3
        with:
          token: ${{ secrets.CODECOV_TOKEN }}
          fail_ci_if_error: false
          verbose: true
          flags: unit-tests

  build:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - uses: ./.github/actions/setup
      - uses: actions/cache@v3
        id: build-cache
        with:
          path: node_modules/.cache
          key: ${{ runner.os }}-build-${{ hashFiles('**/yarn.lock') }}-${{ github.run_id }}
          restore-keys: ${{ runner.os }}-build-${{ hashFiles('**/yarn.lock') }}-
      - run: yarn prepare
      - run: yarn build
      - uses: actions/upload-artifact@v3
        with:
          name: build
          path: build
          if-no-files-found: error

  size-tests:
    needs: [build]
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - uses: ./.github/actions/setup
      - uses: actions/download-artifact@v3
        with:
          name: build
          path: build
      - run: yarn test:size

  build-e2e:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - uses: ./.github/actions/setup
      - uses: actions/cache@v3
        id: build-e2e-cache
        with:
          path: node_modules/.cache
          key: ${{ runner.os }}-build-e2e-${{ hashFiles('**/yarn.lock') }}-${{ github.run_id }}
          restore-keys: ${{ runner.os }}-build-e2e-${{ hashFiles('**/yarn.lock') }}-
      - run: yarn prepare
      - run: yarn build:e2e
        env:
          NODE_OPTIONS: "--max_old_space_size=4096"
      - uses: actions/upload-artifact@v2
        with:
          name: build-e2e
          path: build
          if-no-files-found: error

  # Allows for parallel re-runs of cypress tests without re-building.
  cypress-rerun:
    runs-on: ubuntu-latest
    steps:
      - run: exit 0

  cypress-test-matrix:
    needs: [build-e2e, cypress-rerun]
    runs-on: ubuntu-latest
    container: cypress/browsers:node-18.14.1-chrome-111.0.5563.64-1-ff-111.0-edge-111.0.1661.43-1
    strategy:
      fail-fast: false
      matrix:
        containers: [1, 2, 3, 4]
    steps:
      - uses: actions/checkout@v3
      - uses: ./.github/actions/setup
      - uses: actions/cache@v3
        id: cypress-cache
        with:
          path: /root/.cache/Cypress
          key: ${{ runner.os }}-cypress
      - run: |
          yarn cypress install
          yarn cypress info

      - uses: actions/download-artifact@v3
        with:
          name: build-e2e
          path: build

      - uses: cypress-io/github-action@v4
        with:
          install: false
          start: yarn serve
          wait-on: 'http://localhost:3000'
          browser: chrome
          record: true
          parallel: true
        env:
          CYPRESS_RECORD_KEY: ${{ secrets.CYPRESS_RECORD_KEY }}
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

      - uses: codecov/codecov-action@v3
        with:
          token: ${{ secrets.CODECOV_TOKEN }}
          fail_ci_if_error: false
          verbose: true
          flags: e2e-tests

  # Included as a single job to check for cypress-test-matrix success, as a matrix cannot be checked.
  cypress-tests:
    if: ${{ always() }}
    needs: [cypress-test-matrix]
    runs-on: ubuntu-latest
    steps:
      - if: needs.cypress-test-matrix.result != 'success'
        run: exit 1
