name: VLY2 Push branch, PR master, all tests

on:
  push:
    branches-ignore:
      - master
      - releases/*
  # pull_request:
  #   branches:
  #     - master

jobs:
  setup:    
    name: Set up NPM, use cache, NPM CI
    runs-on: ubuntu-latest
    if: github.ref != 'refs/heads/master' #If we're not being pushed to master, do all the test steps
    steps:
    - uses: actions/checkout@v2

    - name: Cache node modules #Caches everything for the other tasks to run
      uses: actions/cache@v1
      env:
        cache-name: cache-node-modules
      with:
        path: ~/.npm # npm cache files are stored in `~/.npm` on Linux/macOS
        key: ${{ runner.os }}-build-${{ env.cache-name }}-${{ hashFiles('**/package-lock.json') }}
        restore-keys: |
          ${{ runner.os }}-build-${{ env.cache-name }}-
          ${{ runner.os }}-build-
          ${{ runner.os }}-

    - name: Use Node 16
      uses: actions/setup-node@v1
      with:
        node-version: 16
        mongoms_download_mirror: http://downloads.mongodb.org
        mongoms_version: 4.0.5
        mongoms_disable_postinstall: 1
        ci_node_index: 0
        ci_node_total: 1

    - name: Set up dependencies  #Warm cache
      run: npm ci

  lang:
    name: Lang #Do our language file weirdness and make sure we're all good going to build
    needs: setup
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v2
    - name: Cache node modules
      uses: actions/cache@v1
      env:
        cache-name: cache-node-modules
      with:
        path: ~/.npm # npm cache files are stored in `~/.npm` on Linux/macOS
        key: ${{ runner.os }}-build-${{ env.cache-name }}-${{ hashFiles('**/package-lock.json') }}
        restore-keys: |
          ${{ runner.os }}-build-${{ env.cache-name }}-
          ${{ runner.os }}-build-
          ${{ runner.os }}-
    - name: Lang
      run: |
        npm ci
        cp lang/en.json lang/en.orig.json
        npm run build:lang
        diff lang/en.json lang/en.orig.json

  lint:
    name: Lint
    needs: setup
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v2
    - name: Cache node modules
      uses: actions/cache@v1
      env:
        cache-name: cache-node-modules
      with:
        path: ~/.npm # npm cache files are stored in `~/.npm` on Linux/macOS
        key: ${{ runner.os }}-build-${{ env.cache-name }}-${{ hashFiles('**/package-lock.json') }}
        restore-keys: |
          ${{ runner.os }}-build-${{ env.cache-name }}-
          ${{ runner.os }}-build-
          ${{ runner.os }}-
    - name: lint
      run: |
        npm ci
        npx standard

  testroot:
    name: Test Root #Test only the high level tests 
    needs: setup
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v2
    - name: Cache node modules
      uses: actions/cache@v1
      env:
        cache-name: cache-node-modules
      with:
        path: ~/.npm # npm cache files are stored in `~/.npm` on Linux/macOS
        key: ${{ runner.os }}-build-${{ env.cache-name }}-${{ hashFiles('**/package-lock.json') }}
        restore-keys: |
          ${{ runner.os }}-build-${{ env.cache-name }}-
          ${{ runner.os }}-build-
          ${{ runner.os }}-
    - name: Test Root __tests__    
      run: |
        npm ci
        npm run test:root

  testserver:
    name: Test Server #Test server side, includes security
    needs: setup
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v2
    - name: Cache node modules
      uses: actions/cache@v1
      env:
        cache-name: cache-node-modules
      with:
        path: ~/.npm # npm cache files are stored in `~/.npm` on Linux/macOS
        key: ${{ runner.os }}-build-${{ env.cache-name }}-${{ hashFiles('**/package-lock.json') }}
        restore-keys: |
          ${{ runner.os }}-build-${{ env.cache-name }}-
          ${{ runner.os }}-build-
          ${{ runner.os }}-
    - name: Test Server (and security)
      run: |
        npm ci
        npm run test:server

  testcomponents:
    name: Test Components #Test components
    needs: setup
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v2
    - name: Cache node modules
      uses: actions/cache@v1
      env:
        cache-name: cache-node-modules
      with:
        path: ~/.npm # npm cache files are stored in `~/.npm` on Linux/macOS
        key: ${{ runner.os }}-build-${{ env.cache-name }}-${{ hashFiles('**/package-lock.json') }}
        restore-keys: |
          ${{ runner.os }}-build-${{ env.cache-name }}-
          ${{ runner.os }}-build-
          ${{ runner.os }}-
    - name: Test Server (and security)
      run: |
        npm ci
        npm run test:components

  testlibraries:
    name: Test Libraries #Test libs
    needs: setup
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v2
    - name: Cache node modules
      uses: actions/cache@v1
      env:
        cache-name: cache-node-modules
      with:
        path: ~/.npm # npm cache files are stored in `~/.npm` on Linux/macOS
        key: ${{ runner.os }}-build-${{ env.cache-name }}-${{ hashFiles('**/package-lock.json') }}
        restore-keys: |
          ${{ runner.os }}-build-${{ env.cache-name }}-
          ${{ runner.os }}-build-
          ${{ runner.os }}-
    - name: Test Server (and security)
      run: |
        npm ci
        npm run test:lib
