task:
  name: 'Functional tests'
  container:
    image: rust:1-bookworm
  timeout_in: 90m  # https://cirrus-ci.org/faq/#instance-timed-out

  env:
    EXECUTOR_WORKERS: 3
    VERBOSE: 0
    LOG_LEVEL: debug
    TIMEOUT: 300
  matrix:
    - name: 'Misc functional tests'
      env:
        TEST_GROUP: tests/test_misc.py
        matrix:
          - USE_MIN_BITCOIN_VERSION: 'TRUE'
          - USE_MIN_BITCOIN_VERSION: 'FALSE'
    - name: 'RPC functional tests'
      env:
        TEST_GROUP: tests/test_rpc.py
    - name: 'Chain functional tests'
      env:
        TEST_GROUP: tests/test_chain.py

  cargo_registry_cache:
    folders: $CARGO_HOME/registry
    fingerprint_script: cat Cargo.lock
  cargo_git_cache:
    folders: $CARGO_HOME/git  # It will fail if they aren't separated
    fingerprint_script: cat Cargo.lock
  target_cache:
    folder: target
    fingerprint_script:
      - rustc --version
      - cat Cargo.lock
  lianad_build_script: cargo build --release

  deps_script: apt update && apt install -y python3 python3-pip

  pip_cache:
    folder: ~/.cache/pip
  python_deps_script: pip install --break-system-packages -r tests/requirements.txt

  test_script: |
    set -xe

    if [ "$USE_MIN_BITCOIN_VERSION" = "TRUE" ]; then
      # Download the minimum required bitcoind binary
      curl -O https://bitcoincore.org/bin/bitcoin-core-24.0.1/bitcoin-24.0.1-x86_64-linux-gnu.tar.gz
      echo "49df6e444515d457ea0b885d66f521f2a26ca92ccf73d5296082e633544253bf bitcoin-24.0.1-x86_64-linux-gnu.tar.gz" | sha256sum -c
      tar -xzf bitcoin-24.0.1-x86_64-linux-gnu.tar.gz
      export BITCOIND_PATH=bitcoin-24.0.1/bin/bitcoind
      export IS_NOT_BITCOIND_24=0
    else
      # Download the bitcoind binary
      curl -O https://bitcoincore.org/bin/bitcoin-core-26.0/bitcoin-26.0-x86_64-linux-gnu.tar.gz
      echo "23e5ab226d9e01ffaadef5ffabe8868d0db23db952b90b0593652993680bb8ab bitcoin-26.0-x86_64-linux-gnu.tar.gz" | sha256sum -c
      tar -xzf bitcoin-26.0-x86_64-linux-gnu.tar.gz
      export BITCOIND_PATH=bitcoin-26.0/bin/bitcoind
    fi

    # The misc tests have a backward compat test that need the path to a previous version of Liana.
    # For now it requires using 0.3.
    if [ "$TEST_GROUP" = "tests/test_misc.py" ]; then
      curl -LO https://github.com/wizardsardine/liana/releases/download/0.3.1/liana-0.3.1-x86_64-linux-gnu.tar.gz
      echo "70c8595554b6f78ccc7b66ef5f5ebc5bac03a7b1ce28afe8a076f69adf59c583 liana-0.3.1-x86_64-linux-gnu.tar.gz" | sha256sum -c
      tar -xzf liana-0.3.1-x86_64-linux-gnu.tar.gz
      export OLD_LIANAD_PATH="$PWD/liana-0.3.1-x86_64-linux-gnu/lianad"
    fi

    # Run the functional tests
    LIANAD_PATH=$PWD/target/release/lianad pytest $TEST_GROUP -vvv -n 2

  before_cache_script: rm -rf $CARGO_HOME/registry/index
