name: Tests
on:
  pull_request:
  push:
    branches:
      - main
      - release/**

jobs:
  cleanup-runs:
    runs-on: ubuntu-latest
    steps:
      - uses: rokroskar/workflow-run-cleanup-action@master
        env:
          GITHUB_TOKEN: "${{ secrets.GITHUB_TOKEN }}"
    if: "!startsWith(github.ref, 'refs/tags/') && github.ref != 'refs/heads/main'"

  install-tparse:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/setup-go@v2.1.4
        with:
          go-version: 1.16
      - name: Display Go Version
        run: go version
      - name: Install tparse
        run: |
          export GO111MODULE="on" && go get github.com/mfridman/tparse@v0.8.3
      - uses: actions/cache@v2.1.6
        with:
          path: ~/go/bin
          key: ${{ runner.os }}-go-tparse-binary

  test-unit-cover:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/setup-go@v2.1.4
        with:
          go-version: 1.16
      - uses: actions/checkout@v2.3.4
      - uses: technote-space/get-diff-action@v5
        with:
          PATTERNS: |
            **/**.go
            go.mod
            go.sum
      - name: Test and Create Coverage Report
        run: |
          make test-unit-cover
        if: env.GIT_DIFF
      - uses: codecov/codecov-action@v2.0.3
        with:
          file: ./coverage.txt
          fail_ci_if_error: true
        if: env.GIT_DIFF

  # TODO: refactor before enabling
  # test-importer:
  #   runs-on: ubuntu-latest
  #   timeout-minutes: 10
  #   steps:
  #     - uses: actions/checkout@v2.3.4
  #     - uses: actions/setup-go@v2.1.4
  #       with:
  #         go-version: 1.16
  #     - uses: technote-space/get-diff-action@v5
  #       id: git_diff
  #       with:
  #         SUFFIX_FILTER: |
  #           .go
  #           .mod
  #           .sum
  #     - name: test-importer
  #       run: |
  #         make test-import
  #       if: "env.GIT_DIFF != ''"

  test-solidity:
    runs-on: ubuntu-latest
    timeout-minutes: 10
    steps:
      - uses: actions/checkout@v2.3.4
      - uses: technote-space/get-diff-action@v5
        id: git_diff
        with:
          SUFFIX_FILTER: |
            .go
            .mod
            .sum
      - name: test-solidity
        run: |
          make test-solidity
        if: "env.GIT_DIFF != ''"

  liveness-test:
    runs-on: ubuntu-latest
    timeout-minutes: 10
    steps:
      - uses: actions/checkout@v2.3.4
      - uses: actions/setup-go@v2.1.4
        with:
          go-version: 1.16
      - uses: technote-space/get-diff-action@v5
        id: git_diff
        with:
          PATTERNS: |
            **/**.go
            go.mod
            go.sum
      - name: Install Starport
        run: |
          curl https://get.starport.network/starport! | bash
        if: env.GIT_DIFF
      - name: Start Local Network via Starport
        run: |
          starport chain serve --reset-once -v -c ./starport.yml > starport.out 2>&1 &
        if: env.GIT_DIFF
      - name: Test Local Network Liveness
        run: |
          sleep 2m
          ./contrib/scripts/test_localnet_liveness.sh 100 5 50 localhost
        if: env.GIT_DIFF
      