name: "check-deploy"

on:
  push:
    # branches:
    #   - "master"

jobs:
  check-deployment:
    runs-on: ubuntu-latest
    defaults:
      run:
        shell: bash
    steps:
      - name: Checkout repository
        uses: actions/checkout@v2
      - uses: actions/cache@v2
        with:
          path: |
            ~/.cargo/registry
            ~/.cargo/git
            ~/.cargo/bin
            target
          key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}

      - run: docker run -d --rm -e RUST_LOG="info" --name fluence -p 5001:5001 -p 1210:1210 -p 4310:4310 fluencelabs/fluence -t 1210 -w 4310 -k gKdiCSUr1TFGFEgu2t8Ch1XEUsrN5A2UfBLjSZvfci9SPR3NvZpACfcpPGC3eY4zma1pk7UvYv5zb1VjvPHwCjj --local

      - name: Download marine
        run: sudo bash $GITHUB_WORKSPACE/.github/download_marine.sh

      - name: Install Rust
        run: |
          rustup toolchain install nightly-2021-04-24-x86_64-unknown-linux-gnu
          rustup default nightly-2021-04-24-x86_64-unknown-linux-gnu
          rustup target add wasm32-wasi --toolchain nightly-2021-04-24-x86_64-unknown-linux-gnu

      - uses: actions/cache@v2
        with:
          path: ~/.npm
          key: ${{ runner.os }}-v1-node-16-${{ hashFiles('**/package-lock.json') }}
          restore-keys: |
            ${{ runner.os }}-v1-node-16

      - name: Setup Node.js 16
        uses: actions/setup-node@v1
        with:
          node-version: 16

      - run: npm install -g @fluencelabs/aqua@0.7.4-320

      - name: "Wait for aqua-ipfs service to start"
        run: |
          set -o errexit -o nounset -o pipefail
          set -x
          while true; do
            STATUS=$(docker logs fluence |& grep -q 'Fluence has been successfully started.' && echo "OK" || echo "FAIL")
            if [ $STATUS == "OK" ]; then
              break;
            else
              sleep 0.5
              echo "Waiting for all builtin services to start"
            fi
          done

      - name: "Check call_parameters deployment"
        run: |
          set -o errexit -o nounset -o pipefail

          NODE="/ip4/127.0.0.1/tcp/4310/ws/p2p/12D3KooWKEprYXUXqoV5xSBeyqrWLpQLLH4PXfvVkDJtmcqmh5V3"
          SERVICE_ID=`./deploy.sh $NODE`
          INPUT=$GITHUB_WORKSPACE/.github/deploy.aqua

          RESULT=$(aqua run --addr $NODE --sk qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqo= -f 'call(service_id)' -d '{"service_id": "'$SERVICE_ID'"}' --input $INPUT | jq -r)

          EXPECTED_PEER_ID=$(echo -e "$RESULT" | sed -n '1p')
          RESULT_PEER_ID=$(echo -e "$RESULT" | sed -n '3p')
          if [ "$EXPECTED_PEER_ID" != "$RESULT_PEER_ID" ]; then
            echo "Expected $EXPECTED_PEER_ID, got $RESULT_PEER_ID"
            exit 1
          fi

          RESULT_SERVICE_ID=$(echo -e "$RESULT" | sed -n '2p')
          if [ "$SERVICE_ID" != "$RESULT_SERVICE_ID" ]; then
            echo "Expected $SERVICE_ID, got $RESULT_SERVICE_ID"
            exit 1
          fi
        working-directory: marine-examples/call_parameters
