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@v4
        with:
          repository: fluencelabs/aquavm
          ref: ${{ inputs.ref }}

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

      - name: Setup marine
        uses: fluencelabs/setup-marine@v1
        with:
          artifact-name: marine

      - 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

      - name: Setup risczero
        uses: brndnmtthws/rust-action-cargo-binstall@v1
        with:
          packages: cargo-risczero

      - run: cargo clean
      - run: cargo risczero install
      - 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 --no-default-features --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.
          # It shouldn't have `--no-default-features`, but it has until legacy tests will work with signatures enabled.
          for san in address leak; do
            RUSTFLAGS="$RUSTFLAGS -Z sanitizer=$san" cargo test --no-default-features --features test_with_native_code --target x86_64-unknown-linux-gnu
          done
      - name: Run signature tests with test_with_native_code
        env:
          RUST_TEST_THREADS: 1
        run: |
          # Temporary solution until legacy tests work with signatures.
          cargo test --features test_with_native_code,gen_signatures,check_signatures --target x86_64-unknown-linux-gnu features::signatures

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