container:
#  image: python:slim
#  image: ubuntu:jammy
#  image: python:3.10
  image: registry.gitlab.com/cryptoadvance/specter-desktop/cirrus-jammy:20230206

# We assume here that we're having a proper python3 system including virtualenv and pip
prep_stuff_template: &PREP_STUFF_TEMPLATE
  bitcoind_installation_cache:
    folder: ./tests/bitcoin
    fingerprint_script: 
      - cat pyproject.toml | grep "addopts = " | cut -d'=' -f2 |  sed 's/--/+/g' | tr '+' '\n' | grep bitcoin |  cut -d' ' -f2 
      - cat tests/bitcoin_gitrev_pinned 2> /dev/null || true
      - cat /etc/os-release | grep VERSION
      - cat ./tests/install_noded.sh
      - echo "binary" # if the next line is --bitcoin binary, otherwise use echo "compile" - this ensures different caching keys.
    populate_script: ./tests/install_noded.sh --debug --bitcoin binary
  elementsd_installation_cache:
    folder: ./tests/elements
    fingerprint_script: 
      - cat pyproject.toml | grep "addopts = " | cut -d'=' -f2 |  sed 's/--/+/g' | tr '+' '\n' | grep elements |  cut -d' ' -f2 
      - cat tests/elements_gitrev_pinned 2> /dev/null || true
      - cat /etc/os-release | grep VERSION
      - cat ./tests/install_noded.sh
      - echo "binary" # if the next line is --elements binary, otherwise use echo "compile" - this ensures different caching keys.
    populate_script: ./tests/install_noded.sh --debug --elements binary
  verify_script: 
    - echo "  --> Version of python, virtualenv and pip3"
    - python3 --version && virtualenv --version && pip3 --version
    - echo "  --> Executables in tests/elements/src"
    - find tests/elements/src -maxdepth 1 -type f -executable -exec ls -ld {} \; || true
    - echo "  --> Executables in tests/elements/bin"
    - find tests/elements/bin -maxdepth 1 -type f -executable -exec ls -ld {} \; || true
    - echo "  --> Executables in tests/bitcoin/src"
    - find tests/bitcoin/src -maxdepth 1 -type f -executable -exec ls -ld {} \; || true
    - echo "  --> Executables in tests/bitcoin/bin"
    - find tests/bitcoin/bin -maxdepth 1 -type f -executable -exec ls -ld {} \; || true
    - echo "  --> bitcoind version"
    - tests/bitcoin/src/bitcoind -version | head -1 || true
    - tests/bitcoin/bin/bitcoind -version | head -1 || true
    - echo "  --> elements version"
    - tests/elements/src/elementsd -version | head -1 || true
    - tests/elements/bin/elementsd -version | head -1 || true

  pip_script:
    #folder: /tmp/cirrus-ci-build/.env
    #fingerprint_script: echo muh && cat requirements.txt
    #populate_script: 
    - virtualenv --python=python .env 
    - source ./.env/bin/activate
    - pip3 install -r requirements.txt --require-hashes && pip3 install -e ".[test]"
  install_script: 
    - source ./.env/bin/activate
    - pip3 install -e .

test_task:
  << : *PREP_STUFF_TEMPLATE
  skip: "false"
  test_script: 
    - source ./.env/bin/activate
    - echo $PATH
    # needed so that setuptools_scm has a t least one tag to guess the version properly 
    # and the tests/test_util_version.py doesn't fail
    - git fetch origin refs/tags/v1.0.0 
    - pytest --cov=cryptoadvance --junitxml=./testresults.xml
  always:
    junit_artifacts:
      path: "./testresults.xml"
      format: junit
  

cypress_test_task:
  skip: "false"
  container:
    image: registry.gitlab.com/cryptoadvance/specter-desktop/cypress-python-jammy:20230206
    cpu: 6
    memory: 6G
  pre_prep_script: 
    # The stupid old debian-package is not installing a proper binary but just the python-package
    - echo -e '#!/bin/bash\npython3 -m virtualenv "$@"' > /usr/local/bin/virtualenv
    - chmod +x /usr/local/bin/virtualenv
    - virtualenv --version
  << : *PREP_STUFF_TEMPLATE
  npm_cache:
    folder: ./node_modules
    fingerprint_script: cat package-lock.json
    populate_script: npm ci
  cypress_script:
    - source ./.env/bin/activate
    #- pip3 install -e .
    - ./utils/test-cypress.sh --debug run
  junit_artifacts:
    path: "cypresstest-output.xml"
    type: text/xml
    format: junit
  always:
    cypress_screenshots_artifacts:
      path: "./cypress/screenshots/**"
    cypress_videos_artifacts:
      path: "./cypress/videos/**"
  
extension_smoketest_task:
  << : *PREP_STUFF_TEMPLATE
  test_script:
    - git config --global user.name "CI CD"
    - git config --global user.email "cicd@example.com"
    - source ./.env/bin/activate
    - echo $PATH
    - mkdir tmp && cd tmp
    - mkdir testextension && cd testextension
    - pwd
    - python3 -m cryptoadvance.specter ext gen --ext-id cicdtest --org cryptoadvance --no-isolated-client --devicename cicddevice
    - pip3 install -e .
    - python3 -m cryptoadvance.specter server --config DevelopmentConfig --debug  2> specter.log &
    - sleep 15
    - cat specter.log | grep "Found CicdtestService" # Discovery failed
    - cat specter.log | grep "Extension CicdtestService activated (alpha)"
    - apt-get update && apt-get -y install curl
    - curl http://127.0.0.1:25441/svc/cicdtest/ | grep "CicdtestService 4thewin."
    
