Skip to content

Commit

Permalink
test: add subscriber to top level bats
Browse files Browse the repository at this point in the history
  • Loading branch information
bodymindarts committed Nov 10, 2023
1 parent f4fdcbb commit 71ad2a4
Show file tree
Hide file tree
Showing 12 changed files with 355 additions and 19 deletions.
4 changes: 2 additions & 2 deletions bats/ci_setup_suite.bash
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#!/bin/bash

export REPO_ROOT=$(git rev-parse --show-toplevel)
source "${REPO_ROOT}/bats/helpers/setup-and-teardown.bash"
source "${REPO_ROOT}/bats/helpers/_common.bash"

TILT_PID_FILE=$REPO_ROOT/bats/.tilt_pid
TILT_PID_FILE="${BATS_ROOT_DIR}/.tilt_pid"

setup_suite() {
background buck2 run //dev:up -- --bats=True > "${REPO_ROOT}/bats/.e2e-tilt.log"
Expand Down
46 changes: 46 additions & 0 deletions bats/core/api/public.bats
Original file line number Diff line number Diff line change
@@ -1,9 +1,55 @@
#!/usr/bin/env bats

load "../../helpers/_common.bash"
load "../../helpers/subscriber.bash"

@test "public: can query globals" {
exec_graphql 'anon' 'globals'
network="$(graphql_output '.data.globals.network')"
[[ "${network}" = "regtest" ]] || exit 1
}

@test "public: can apply idempotency key to queries" {
fixed_idempotency_key=$(new_idempotency_key)
original_new_idempotency_key=$(declare -f new_idempotency_key)
new_idempotency_key() {
echo $fixed_idempotency_key
}

# Successful 1st attempt
exec_graphql 'anon' 'globals'
errors="$(graphql_output '.errors')"
[[ "$errors" == "null" ]] || exit 1

# Failed 2nd attempt with same idempotency key
exec_graphql 'anon' 'globals'
error_msg="$(graphql_output '.errors[0].message')"
[[ "$error_msg" == "HTTP fetch failed from 'public': 409: Conflict" ]] || exit 1

# Failed attempt with invalid idempotency key
new_idempotency_key() {
echo "invalid-key"
}
exec_graphql 'anon' 'globals'
error_msg="$(graphql_output '.errors[0].message')"
[[ "$error_msg" == "HTTP fetch failed from 'public': 400: Bad Request" ]] || exit 1

# Successful 3rd attempt with unique valid idempotency key
eval "$original_new_idempotency_key"
exec_graphql 'anon' 'globals'
[[ "$errors" == "null" ]] || exit 1
}

@test "public: can subscribe to price" {
subscribe_to 'anon' price-sub
retry 10 1 grep 'Data.*\bprice\b' .e2e-subscriber.log

num_errors=$(
grep 'Data.*\bprice\b' .e2e-subscriber.log \
| awk '{print $2}' \
| jq -r '.data.price.errors | length'
)
[[ "$num_errors" == "0" ]] || exit 1

stop_subscriber
}
14 changes: 14 additions & 0 deletions bats/gql/price-sub.gql
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
subscription {
price(
input: { amount: 100 amountCurrencyUnit: BTCSAT priceCurrencyUnit: USDCENT }
) {
errors {
message
}
price {
base
offset
currencyUnit
}
}
}
14 changes: 1 addition & 13 deletions bats/helpers/_common.bash
Original file line number Diff line number Diff line change
@@ -1,23 +1,11 @@
export REPO_ROOT=$(git rev-parse --show-toplevel)

export BATS_ROOT_DIR="${REPO_ROOT}/bats"
CACHE_DIR=${BATS_TMPDIR:-tmp/bats}/galoy-bats-cache
mkdir -p "$CACHE_DIR"

OATHKEEPER_PROXY=${OATHKEEPER_PROXY:-localhost:4455}

SERVICES_PID_FILE=$REPO_ROOT/bats/.services_pid

start_services() {
stop_services > /dev/null 2>&1 || true
background buck2 run //dev:up -- "$@" > "${REPO_ROOT}/bats/.e2e-services.log"
echo $! > "$SERVICES_PID_FILE"
}

stop_services() {
[[ -f "$SERVICES_PID_FILE" ]] && kill -9 "$(cat "$SERVICES_PID_FILE")" > /dev/null || true
buck2 run //dev:down
}

if ! type fail &>/dev/null; then
fail() {
echo "$1"
Expand Down
3 changes: 2 additions & 1 deletion bats/helpers/setup-and-teardown.bash
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
source 'bats/helpers/_common.bash'
CURRENT_FILE=${BASH_SOURCE:-bats/helpers/.}
source "$(dirname "$CURRENT_FILE")/_common.bash"

login_user() {
local token_name=$1
Expand Down
29 changes: 29 additions & 0 deletions bats/helpers/subscriber.bash
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
CURRENT_FILE=${BASH_SOURCE:-bats/helpers/.}
source "$(dirname "$CURRENT_FILE")/_common.bash"

export SUBSCRIBER_PID_FILE="${BATS_ROOT_DIR}/.gql_subscriber_pid"

subscribe_to() {
stop_subscriber > /dev/null 2>&1 || true

token_name=$1
if [[ -n "$token_name" && "$token_name" != 'anon' ]]; then
token="$(read_value "$token_name")"
fi
gql_filename=$2
variables=$3

background \
buck2 run //bats/helpers/subscriber:run -- \
"ws://${OATHKEEPER_PROXY}/graphqlws" \
"$(gql_file "$gql_filename")" \
"$token" \
"$variables" \
> "${BATS_ROOT_DIR}/.e2e-subscriber.log"
echo $! > "$SUBSCRIBER_PID_FILE"
}

stop_subscriber() {
[[ -f "$SUBSCRIBER_PID_FILE" ]] && kill $(cat $SUBSCRIBER_PID_FILE) > /dev/null || true
}

9 changes: 9 additions & 0 deletions bats/helpers/subscriber/BUCK
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
load(
"@toolchains//workspace-pnpm:macros.bzl",
"dev_pnpm_task_binary",
)

dev_pnpm_task_binary(
name = "run",
command = "subscriber",
)
13 changes: 13 additions & 0 deletions bats/helpers/subscriber/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"name": "subscriber",
"scripts": {
"subscriber": "ts-node src/gql-subscribe.ts"
},
"devDependencies": {
"@types/ws": "^8.5.8",
"@types/node": "^20.8.7",
"ws": "^8.14.2",
"graphql-ws": "^5.14.1",
"ts-node": "^10.9.1"
}
}
183 changes: 183 additions & 0 deletions bats/helpers/subscriber/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions bats/helpers/subscriber/pnpm-workspace.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
---
Loading

0 comments on commit 71ad2a4

Please sign in to comment.