on:
  push:
    branches:
      - main
  pull_request:
name: Run Tests
jobs:
  test-e2e:
    strategy:
      matrix:
        os: [ubuntu-latest]
        go-version: ['^1.16.0']
        fleet-tier: [core, basic]
    runs-on: ${{ matrix.os }}
    steps:
    - name: Install Go
      uses: actions/setup-go@v2
      with:
        go-version: ${{ matrix.go-version }}
    - name: Checkout Code
      uses: actions/checkout@v2

    # Pre-starting dependencies here means they are ready to go when we need them.
    - name: Start Infra Dependencies
      # Use & to background this
      run: docker-compose up -d mysql_test redis mailhog saml_idp &

    - name: JS Dependency Cache
      id: js-cache
      uses: actions/cache@v2
      with:
        path: |
          **/node_modules
          ~/.cache/Cypress
        # Use a separate cache for this from other JS jobs since we run the
        # webpack steps and will have more to cache.
        key: ${{ runner.os }}-e2e-modules-${{ hashFiles('**/yarn.lock') }}
        restore-keys: |
          ${{ runner.os }}-e2e-modules-

    - name: Go Cache
      id: go-cache
      uses: actions/cache@v2
      with:
        # In order:
        # * Module download cache
        # * Build cache (Linux)
        # * Build cache (Mac)
        # * Build cache (Windows)
        path: |
          ~/go/pkg/mod
          ~/.cache/go-build
          ~/Library/Caches/go-build
          %LocalAppData%\go-build
        key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
        restore-keys: |
          ${{ runner.os }}-go-

    - name: Install JS Dependencies
      if: steps.js-cache.outputs.cache-hit != 'true'
      run: make deps-js

    - name: Install Go Dependencies
      if: steps.go-cache.outputs.cache-hit != 'true'
      run: make deps-go

    - name: Build Fleet
      run: |
        export PATH=$PATH:~/go/bin
        make generate-ci
        make

    - name: Run E2E Tests
      run: |
        .github/scripts/check-infra-dependencies.sh all
        make e2e-reset-db
        make e2e-serve-${{ matrix.fleet-tier }} &
        sleep 3
        make e2e-setup
        yarn cypress run --config-file cypress-${{ matrix.fleet-tier }}.json --config video=false


  test-js:
    strategy:
      matrix:
        os: [ubuntu-latest]
    runs-on: ${{ matrix.os }}

    steps:
    - name: Checkout Code
      uses: actions/checkout@v2

    - name: JS Dependency Cache
      id: js-cache
      uses: actions/cache@v2
      with:
        path: |
          **/node_modules
          ~/.cache/Cypress
        key: ${{ runner.os }}-modules-${{ hashFiles('**/yarn.lock') }}
        restore-keys: |
          ${{ runner.os }}-modules-

    - name: Install JS Dependencies
      if: steps.js-cache.outputs.cache-hit != 'true'
      run: make deps-js

    - name: Run JS Tests
      run: |
        make test-js


  lint-js:
    strategy:
      matrix:
        os: [ubuntu-latest]
    runs-on: ${{ matrix.os }}

    steps:
    - name: Checkout Code
      uses: actions/checkout@v2

    - name: JS Dependency Cache
      id: js-cache
      uses: actions/cache@v2
      with:
        path: |
          **/node_modules
          ~/.cache/Cypress
        key: ${{ runner.os }}-modules-${{ hashFiles('**/yarn.lock') }}
        restore-keys: |
          ${{ runner.os }}-modules-

    - name: Install JS Dependencies
      if: steps.js-cache.outputs.cache-hit != 'true'
      run: make deps-js

    - name: Run JS Linting
      run: |
        make lint-js

    - name: Run prettier formatting check
      run: |
        yarn prettier:check

  test-go:
    strategy:
      matrix:
        os: [ubuntu-latest]
        go-version: ['^1.16.0']
    runs-on: ${{ matrix.os }}

    steps:
    - name: Install Go
      uses: actions/setup-go@v2
      with:
        go-version: ${{ matrix.go-version }}
    - name: Checkout Code
      uses: actions/checkout@v2

    # Pre-starting dependencies here means they are ready to go when we need them.
    - name: Start Infra Dependencies
      # Use & to background this
      run: docker-compose up -d mysql_test redis &

    # It seems faster not to cache Go dependencies
    - name: Install Go Dependencies
      run: make deps-go

    - name: Generate static files
      run: |
        export PATH=$PATH:~/go/bin
        make generate-go

    - name: Run Go Tests
      run: |
        MYSQL_TEST=1 make test-go


  lint-go:
    strategy:
      matrix:
        os: [ubuntu-latest]
        go-version: ['^1.16.0']
    runs-on: ${{ matrix.os }}

    steps:
    - name: Install Go
      uses: actions/setup-go@v2
      with:
        go-version: ${{ matrix.go-version }}
    - name: Checkout Code
      uses: actions/checkout@v2

    - name: Run Go Linting
      run: |
        make lint-go
