name: Test

on:
  pull_request:
    branches: [develop]

jobs:
  build:
    name: Test
    runs-on: ubuntu-latest
    steps:
      - name: Setup dependencies
        run: sudo apt-get install libgflags-dev libsnappy-dev zlib1g-dev libbz2-dev liblz4-dev libzstd-dev

      - name: Set up Go 1.x
        uses: actions/setup-go@v2
        with:
          go-version: 1.18.5
        id: go

      - name: Check out code into the Go module directory
        uses: actions/checkout@v2

      - name: Get dependencies
        run: go get -v -t -d ./...

      - name: Build
        run: make build

      - name: Test
        run: make test-short

  contract-test:
    name: Wasm contract tests
    runs-on: ubuntu-latest
    strategy:
      matrix:
        TEST_LANG: [go, gowasm, tswasm, rswasm]
    steps:
      - name: checkout to the directory
        uses: actions/checkout@v3

      - name: install golang
        uses: actions/setup-go@v3
        with:
          go-version: "1.18.5"

      - name: install tinygo
        # only work on x86_64
        run: |
          wget https://github.com/tinygo-org/tinygo/releases/download/v0.23.0/tinygo_0.23.0_amd64.deb
          sudo dpkg -i tinygo_0.23.0_amd64.deb
          export PATH=$PATH:/usr/local/bin

      - name: install rust-toolchain
        uses: actions-rs/toolchain@v1.0.6
        with:
          toolchain: stable

      - name: install wasm-pack
        run: |
          curl https://rustwasm.github.io/wasm-pack/installer/init.sh -sSf | sh

      - name: install Node.js
        uses: actions/setup-node@v3
        with:
          node-version: 18

      - name: install schema
        run: |
          cd contracts/wasm/scripts
          bash schema_all.sh
          cd ..
          npm install

      - name: run builds
        env:
          TEST_LANG: ${{matrix.TEST_LANG}}
        run: |
          cd contracts/wasm/scripts
          bash core_build.sh
          if [ $TEST_LANG == "go" ]; then
            bash schema_all.sh
          elif [ $TEST_LANG == "gowasm" ]; then
            bash go_all.sh
          elif [ $TEST_LANG == "tswasm" ]; then
            bash ts_all.sh
          elif [ $TEST_LANG == "rswasm" ]; then
            bash rust_all.sh
          fi

      - name: run tests
        env:
          TEST_LANG: ${{matrix.TEST_LANG}}
        run: |
          cd contracts/wasm
          if [ $TEST_LANG == "go" ]; then
            go test ./...
          elif [ $TEST_LANG == "gowasm" ]; then
            go test ./... -gowasm
          elif [ $TEST_LANG == "tswasm" ]; then
            go test ./... -tswasm
          elif [ $TEST_LANG == "rswasm" ]; then
            go test ./... -rswasm
          fi

  golangci:
    name: Lint
    runs-on: ubuntu-latest
    steps:
      - uses: actions/setup-go@v2
        with:
          go-version: 1.19.1
        id: go

      - name: Check out code into the Go module directory
        uses: actions/checkout@v2

      # - name: Generate SC files
      #   run: |
      #     cd contracts/wasm/scripts
      #     bash schema_all.sh

      # - name: golangci-lint in SC
      #   uses: golangci/golangci-lint-action@v3
      #   with:
      #     working-directory: contracts/wasm
      #     args: --fix --timeout 5m0s --path-prefix=""
      #     skip-pkg-cache: true

      - name: Run global scope golangci-lint
        uses: golangci/golangci-lint-action@v3
        with:
          args: --timeout 5m0s
          skip-pkg-cache: true
