name: Run tests with workflow_call

on:
  workflow_call:
    inputs:
      fluence-env:
        description: "Fluence enviroment to run tests agains"
        type: string
        default: "local"
      rust-peer-image:
        description: "rust-peer image tag"
        type: string
        default: "fluencelabs/rust-peer:minimal"
      aqua-version:
        description: "@fluencelabs/aqua version"
        type: string
        default: "null"
      fluence-cli-version:
        description: "@fluencelabs/cli version"
        type: string
        default: "null"
      cargo-dependencies:
        description: "Cargo dependencies map"
        type: string
      ref:
        description: "git ref to checkout to"
        type: string
        default: "main"

env:
  CI: true
  FORCE_COLOR: true
  RUST_PEER_IMAGE: "${{ inputs.rust-peer-image }}"
  FLUENCE_ENV: "${{ inputs.fluence-env }}"

jobs:
  aqua:
    name: "Run aqua tests"
    runs-on: builder
    timeout-minutes: 60

    permissions:
      contents: read
      id-token: write

    steps:
      - name: Import secrets
        uses: hashicorp/vault-action@v2.5.0
        with:
          url: https://vault.fluence.dev
          path: jwt/github
          role: ci
          method: jwt
          jwtGithubAudience: "https://github.com/fluencelabs"
          jwtTtl: 300
          secrets: |
            kv/docker-registry/basicauth/ci username | DOCKER_USERNAME ;
            kv/docker-registry/basicauth/ci password | DOCKER_PASSWORD ;
            kv/npm-registry/basicauth/ci token | NODE_AUTH_TOKEN;

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

      - name: Setup node with self-hosted registry
        uses: actions/setup-node@v3
        with:
          node-version: "18"
          registry-url: "https://npm.fluence.dev"
          cache: "npm"
          cache-dependency-path: "**/package-lock.json"

      - name: Run npm install in aqua
        run: npm install
        working-directory: aqua

      - name: Run npm install in aqua-tests
        run: npm install
        working-directory: aqua-tests

      - name: Set fluence-cli version
        if: inputs.fluence-cli-version != 'null'
        uses: fluencelabs/github-actions/npm-set-dependency@main
        with:
          package: "@fluencelabs/cli"
          version: ${{ inputs.fluence-cli-version }}
          working-directory: aqua-tests
          flags: "--save-dev"

      - name: Set aqua version
        if: inputs.aqua-version != 'null'
        uses: fluencelabs/github-actions/npm-set-dependency@main
        with:
          package: "@fluencelabs/aqua"
          version: ${{ inputs.aqua-version }}
          working-directory: aqua
          flags: "--save-dev"

      - name: Set versions in fluence.yaml
        uses: fluencelabs/github-actions/fluence-set-dependency@main
        env:
          NPM_CONFIG_REGISTRY: "https://npm.fluence.dev"
        with:
          path: aqua-tests
          versions: >
            {
              "npm": {
                "@fluencelabs/aqua": "${{ inputs.aqua-version }}"
              }
            }

      - name: Setup Rust toolchain
        uses: actions-rust-lang/setup-rust-toolchain@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 != ''
        uses: fluencelabs/github-actions/cargo-set-dependency@main
        with:
          dependencies: ${{ inputs.cargo-dependencies }}
          path: service/

      - name: Build service
        run: ./build.sh
        working-directory: service

      - name: Build distro
        run: ./build.sh

      - name: Install ipfs
        uses: nahsi/setup-ipfs@v1

      - name: Create builtin distribution package
        run: ./builtin-package/package.sh

      - name: Unpack registry package
        run: tar xvf registry.tar.gz

      - name: Login to DockerHub
        uses: docker/login-action@v2
        with:
          registry: docker.fluence.dev
          username: ${{ env.DOCKER_USERNAME }}
          password: ${{ env.DOCKER_PASSWORD }}

      - name: Pull rust-peer image
        run: docker pull $RUST_PEER_IMAGE

      - name: Run rust-peer
        uses: isbang/compose-action@v1.4.1
        with:
          compose-file: ".github/e2e/docker-compose.yml"
          down-flags: "--volumes"

      - name: Setup python
        uses: actions/setup-python@v4
        with:
          python-version: "3.9"
          cache: "pip"
          cache-dependency-path: aqua-tests/requirements.txt

      - name: Install python requirements
        run: pip install -r requirements.txt
        working-directory: aqua-tests

      # - name: Wait for registry to be deployed
      #   uses: nick-fields/retry@v2
      #   with:
      #     timeout_seconds: 30
      #     max_attempts: 5
      #     shell: bash
      #     command: ${{ github.workspace }}/.github/check-registry.sh
      #
      - name: Temporarely wait with sleep
        run: sleep 20

      - name: Install fluence-cli dependencies
        env:
          FLUENCE_USER_DIR: "${{ github.workspace }}/tmp/.fluence"
        run: npx fluence dep i --no-input
        working-directory: aqua-tests

      - name: Print Fluence CLI version
        run: pytest -s test_fluence_cli_version.py
        working-directory: aqua-tests

      - name: Run aqua tests
        env:
          FLUENCE_USER_DIR: "${{ github.workspace }}/tmp/.fluence"
        run: pytest test_aqua.py
        working-directory: aqua-tests

      - name: Print versions to check summary
        if: always()
        working-directory: aqua-tests
        run: |
          cat <<SNAPSHOT >> $GITHUB_STEP_SUMMARY
          ## Used versions
          \`\`\`
          $(npx fluence dep v)
          \`\`\`
          SNAPSHOT

      - name: Dump rust-peer logs on failure
        if: always()
        uses: jwalton/gh-docker-logs@v2

      - name: Remove registry directory
        if: always()
        run: sudo rm -rf registry

      - name: Remove tmp directory
        if: always()
        run: sudo rm -rf tmp
