# Useful GitHub Actions docs:
#
# - https://help.github.com/en/actions
# - https://help.github.com/en/actions/reference/workflow-syntax-for-github-actions
# - https://help.github.com/en/actions/configuring-and-managing-workflows/configuring-a-workflow
# - https://help.github.com/en/actions/reference/context-and-expression-syntax-for-github-actions

name: Test

on:
  push:
    branches-ignore:
      # don't double-build dependabot PRs
      - dependabot/**
    tags: ["**"]
  pull_request:
  workflow_dispatch:

jobs:
  # Job to run linter / autoformat
  lint:
    runs-on: ubuntu-20.04
    steps:
      # Action Repo: https://github.com/actions/checkout
      - name: "Checkout repo"
        uses: actions/checkout@v2

      # Action Repo: https://github.com/actions/setup-node
      - name: "Setup Node"
        uses: actions/setup-node@v1
        with:
          node-version: "14"

      # Action Repo: https://github.com/actions/cache
      - name: "Cache node_modules"
        uses: actions/cache@v2
        with:
          path: node_modules
          key: ${{ runner.os }}-npm-${{ hashFiles('package-lock.json') }}
          restore-keys: |
            ${{ runner.os }}-npm-

      - name: "Install"
        run: |
          npm ci

      # Run the pre-commit action
      # Repo: https://github.com/pre-commit/action
      - uses: actions/setup-python@v2
      - uses: pre-commit/action@v2.0.0

      - name: npm audit
        run: |
          # If this fails, run `npm audit fix`
          npm audit --production --audit-level=moderate

  test:
    runs-on: ubuntu-20.04
    # - https://help.github.com/en/actions/reference/workflow-syntax-for-github-actions#jobsjob_idstrategy
    strategy:
      fail-fast: false # Do not cancel all jobs if one fails
      matrix:
        # IMPORTANT: Make sure to update package.json's enginges.node field to
        #            always require at least the oldest version, as well as our
        #            README.md file under the install section.
        node_version:
          - "12" # Remove 2022-04-30, its end-of-life.
          - "14"
          - "15" # Remove about when 17 is available?
          - "16"
          # - "17" # Add 2021-10-19, its initial release date.

    steps:
      - name: "Checkout repo"
        uses: actions/checkout@v2

      # Action Repo: https://github.com/actions/setup-node
      - name: "Setup Node"
        uses: actions/setup-node@v1
        with:
          node-version: ${{ matrix.node_version }}

      # Action Repo: https://github.com/actions/cache
      - name: "Cache node_modules"
        uses: actions/cache@v2
        with:
          path: node_modules
          key: ${{ runner.os }}-npm-${{ hashFiles('package-lock.json') }}
          restore-keys: |
            ${{ runner.os }}-npm-

      - name: "Install dependencies"
        run: |
          npm ci

      - name: "Run tests"
        run: |
          npm test

      # Action Repo: https://github.com/codecov/codecov-action
      - name: "Upload coverage to codecov"
        uses: codecov/codecov-action@v1
