# This workflow will do a clean install of node dependencies, build the source code and run tests across different versions of node
# For more information see: https://help.github.com/actions/language-and-framework-guides/using-nodejs-with-github-actions

name: Test

on:
  push:
    branches:
      - main
    paths-ignore:
      - 'pages/**'
  pull_request:
    branches: [ main ]

jobs:

  cypress-run:

    runs-on: ubuntu-latest
    if: "!contains(github.event.head_commit.message, 'ci skip')"
    strategy:
      fail-fast: false
      matrix:
        browser: [chrome, firefox]

    steps:
      - name: Checkout
        uses: actions/checkout@v3

      - name: Setup Node
        uses: actions/setup-node@v3
        with:
          node-version: '16.x'

      - name: Install dependencies
        run: npm ci

      - name: Cypress run
        uses: cypress-io/github-action@v4.2.0
        with:
          start: npm run start:ci
          wait-on: 'http://localhost:3000/'
          browser: ${{ matrix.browser }}

      - name: Create artifact folder
        if: failure()
        run: |
          mkdir -p cypress/screenshots
          mkdir -p cypress/videos

      - name: Upload failed test screenshots
        uses: actions/upload-artifact@v1
        if: failure()
        with:
          name: cypress-screenshots
          path: cypress/screenshots

      - name: Upload failed test videos
        uses: actions/upload-artifact@v1
        if: failure()
        with:
          name: cypress-videos
          path: cypress/videos

  build:

    name: Build
    runs-on: ubuntu-latest

    strategy:
      matrix:
        node: [16.x]
    steps:

      - name: Checkout
        uses: actions/checkout@v2

      - name: Use Node.js ${{ matrix.node }}
        uses: actions/setup-node@v1
        with:
          node-version: ${{ matrix.node }}
      - run: npm ci
      - run: npm run lint
      - run: npm run build --if-present
