name: Tests
on:
  push:
    branches:
      - '**'
      - '!mochajs.org'
    paths-ignore: ['*.md', 'docs/**']
    tags-ignore:
      - '**'
  pull_request:
    types: [opened, synchronize, reopened]
    paths-ignore: ['*.md', 'docs/**']

jobs:
  prevent-double-run:
    # skip 'push' event when an open PR exists
    name: Prevent double run
    runs-on: ubuntu-latest
    outputs:
      pr-id: ${{ steps.findPr.outputs.number }}
    steps:
      - name: Check event pull_request
        if: github.event_name == 'pull_request'
        run: 'echo pull_request: run workflow'
      - uses: actions/checkout@v2
        if: github.event_name == 'push'
      - name: Check event push
        id: findPr
        if: github.event_name == 'push'
        uses: jwalton/gh-find-current-pr@v1
        with:
          state: open

  smoke:
    name: 'Smoke [Node.js v${{ matrix.node }} / ${{ matrix.os }}]'
    needs: prevent-double-run
    if: needs.prevent-double-run.outputs.pr-id == ''
    runs-on: '${{ matrix.os }}'
    strategy:
      matrix:
        os:
          - ubuntu-latest
          - windows-2019
        node:
          - 14
          - 16
          - 18
    steps:
      - uses: actions/checkout@v2
      - uses: actions/setup-node@v2
        with:
          node-version: '${{ matrix.node }}'
      - run: npm install --production
      - run: npm run test:smoke

  lint:
    name: Linting code and markdown
    runs-on: ubuntu-latest
    needs: smoke
    steps:
      - uses: actions/setup-node@v2
        with:
          node-version: 16
      - uses: actions/checkout@v2
      - name: 'Cache node_modules'
        uses: actions/cache@v2
        with:
          path: '~/.npm'
          key: "ubuntu-latest-node-v16-${{ hashFiles('**/package-lock.json') }}"
          restore-keys: |
            ubuntu-latest-node-v16-
      - name: Install Dependencies
        run: npm ci --ignore-scripts
      - name: 'Check lint'
        run: npm start lint

  test-node:
    name: 'Node.js [v${{ matrix.node }} / ${{ matrix.os }}]'
    needs: smoke
    runs-on: '${{ matrix.os }}'
    env:
      NODE_OPTIONS: '--trace-warnings'
    strategy:
      matrix:
        os:
          - ubuntu-latest
          - windows-2019
        node:
          - 14
          - 16
          - 18
        include:
          - os: ubuntu-latest
            node: 16
            env:
              COVERAGE: 1
    steps:
      - uses: actions/setup-node@v2
        with:
          node-version: '${{ matrix.node }}'
      - uses: actions/checkout@v2
      - name: Get npm cache directory in Windows
        id: npm-cache
        if: ${{ matrix.os == 'windows-2019' }}
        run: |
          echo "::set-output name=dir::$(npm config get cache)"
      - name: 'Cache node_modules'
        uses: actions/cache@v2
        with:
          path: ${{ matrix.os == 'ubuntu-latest' && '~/.npm' || steps.npm-cache.outputs.dir }}
          key: "${{ matrix.os }}-node-v${{ matrix.node }}-${{ hashFiles('**/package-lock.json') }}"
          restore-keys: |
            ${{ matrix.os }}-node-v${{ matrix.node }}-
      - name: Install Dependencies
        run: npm ci --ignore-scripts
      - name: Install Annotation Support
        run: npm install mocha-github-actions-reporter
      - name: Run All Node.js Tests
        run: npm start test.node
        env:
          COVERAGE: '${{ matrix.env.COVERAGE }}'
          MOCHA_REPORTER: mocha-github-actions-reporter
          # this is so mocha-github-actions-reporter can find mocha
          NODE_PATH: lib
      - name: Generate Coverage Report (Linux + Node.js latest)
        if: ${{ matrix.env.COVERAGE }}
        run: npm start coverage-report-lcov
      - name: Upload Coverage to Coveralls (Linux + Node.js latest)
        if: ${{ matrix.env.COVERAGE }}
        uses: coverallsapp/github-action@master
        with:
          github-token: '${{ secrets.GITHUB_TOKEN }}'

  test-browser:
    name: 'Browser Tests'
    needs: smoke
    runs-on: ubuntu-latest
    timeout-minutes: 20
    # Don't run forked 'pull_request' without saucelabs token
    if: github.event_name == 'push' || !github.event.pull_request.head.repo.fork
    steps:
      - uses: actions/setup-node@v2
        with:
          node-version: 16
      - uses: actions/checkout@v2
      - name: 'Cache node_modules'
        uses: actions/cache@v2
        with:
          path: '~/.npm'
          # this key is different than above, since we are running scripts
          # (builds, postinstall lifecycle hooks, etc.)
          key: "ubuntu-latest-node-full-v16-${{ hashFiles('**/package-lock.json') }}"
          restore-keys: |
            ubuntu-latest-node-full-v16-
      - name: Install Dependencies
        run: npm ci
      - name: Run Browser Tests
        run: npm start test.browser
        env:
          SAUCE_USERNAME: ${{ secrets.SAUCE_USERNAME }}
          SAUCE_ACCESS_KEY: ${{ secrets.SAUCE_ACCESS_KEY }}
