name: Next Runtime Tests

on:
  pull_request:
  push:
    branches: [main]
  schedule:
    - cron: '0 0 * * *'

jobs:
  build:
    name: Unit tests
    runs-on: ${{ matrix.os }}

    strategy:
      matrix:
        os: [ubuntu-latest, macOS-latest, windows-latest]
        node-version: [14, '*']
        exclude:
          - os: macOS-latest
            node-version: 14
          - os: windows-latest
            node-version: 14
      fail-fast: false

    steps:
      - uses: actions/checkout@v2
      - name: Installing with LTS Node.js
        uses: actions/setup-node@v2
        with:
          node-version: 'lts/*'
          check-latest: true
      - name: NPM Install
        run: npm install
      - name: Switching to Node.js ${{ matrix.node-version }} to run tests
        uses: actions/setup-node@v2
        if: "${{ matrix.node-version != 'lts/*' }}"
        with:
          node-version: ${{ matrix.node-version }}
          check-latest: true
      - name: Linting
        run: npm run format:ci
        if: "${{ matrix.node-version == 'lts/*' }}"
      - name: Run tests against next@latest
        run: npm test
  canary:
    name: Unit tests (Canary)
    runs-on: ${{ matrix.os }}

    strategy:
      matrix:
        os: [ubuntu-latest, macOS-latest, windows-latest]
        node-version: [14, '*']
        exclude:
          - os: macOS-latest
            node-version: 14
          - os: windows-latest
            node-version: 14
      fail-fast: false

    if: github.ref_name == 'main'
    steps:
      - uses: actions/checkout@v2
      - name: Installing with LTS Node.js
        uses: actions/setup-node@v2
        with:
          node-version: 'lts/*'
          check-latest: true
      - name: NPM Install
        run: npm install
      - name: Install Next.js Canary
        run: npm install -D next@canary --legacy-peer-deps
      - name: Switching to Node.js ${{ matrix.node-version }} to run tests
        uses: actions/setup-node@v2
        if: "${{ matrix.node-version != 'lts/*' }}"
        with:
          node-version: ${{ matrix.node-version }}
          check-latest: true
      - name: Run tests against next@canary
        run: npm test
