name: Run tests with workflow_call

on:
  workflow_call:
    inputs:
      ref:
        description: "git ref to checkout to"
        type: string
        default: "master"
      cargo-dependencies:
        description: "Cargo dependencies map"
        type: string
        default: "null"

jobs:
  tests:
    name: "cargo nextest"
    timeout-minutes: 60
    runs-on: builder

    permissions:
      checks: write # for coverall to create checks

    steps:
      - name: Checkout AquaVM
        uses: actions/checkout@v3
        with:
          repository: fluencelabs/aquavm
          ref: ${{ inputs.ref }}

      - name: Setup Rust toolchain
        uses: dsherret/rust-toolchain-file@v1

      - name: Download marine artifact
        id: marine
        uses: actions/download-artifact@v3
        continue-on-error: true
        with:
          name: marine
          path: ~/.local/bin

      - name: Make marine executable
        if: steps.marine.outcome == 'success'
        run: chmod +x ~/.local/bin/marine

      - name: Setup marine
        if: steps.marine.outcome == 'failure'
        uses: fluencelabs/setup-marine@v1

      - name: Set dependencies
        if: inputs.cargo-dependencies != 'null'
        uses: fluencelabs/github-actions/cargo-set-dependency@main
        with:
          dependencies: ${{ inputs.cargo-dependencies }}

      - name: Build Wasm binary for tests
        run: ./build_test_binaries.sh
        working-directory: air/tests/test_module

      - uses: taiki-e/install-action@cargo-llvm-cov
      - uses: taiki-e/install-action@nextest

      - run: cargo clean
      - run: cargo build

      - name: Run cargo nextest
        # It should have "--all-features", but it is temporarly disabled because of the signature-related feature flags
        run: cargo llvm-cov nextest --features test_with_native_code --release --profile ci --lcov --output-path lcov.info

      - name: Coveralls upload
        if: inputs.cargo-dependencies == 'null'
        uses: coverallsapp/github-action@v2
        with:
          github-token: ${{ secrets.GITHUB_TOKEN }}
          file: lcov.info

      - name: Archive code coverage results
        if: inputs.cargo-dependencies == 'null' && !github.event.pull_request.head.repo.fork
        uses: actions/upload-artifact@v3
        with:
          name: code-coverage-report.zip
          path: lcov.info

      - name: Upload test report
        uses: dorny/test-reporter@v1
        if: (success() || failure()) && !github.event.pull_request.head.repo.fork
        with:
          name: aquavm report
          path: target/nextest/ci/junit.xml
          reporter: java-junit

      - name: Run tests with test_with_native_code
        env:
          RUST_TEST_THREADS: 1
        run: |
          # The `memory` sanitizer on cargo test has false positive even on empty project.
          for san in address leak; do
            RUSTFLAGS="$RUSTFLAGS -Z sanitizer=$san" cargo test --features test_with_native_code --target x86_64-unknown-linux-gnu
          done

      - name: Check native aquavm-air-cli
        run: cargo check --package aquavm-air-cli --no-default-features