#!/usr/bin/env nu
use common.nu *
def main [] {
let env_vars = load-env-file
let did = ($env_vars | get --optional TEST_REPO)
let password = ($env_vars | get --optional TEST_PASSWORD)
if ($did | is-empty) or ($password | is-empty) {
print "SKIP: TEST_REPO and TEST_PASSWORD not set in .env"
exit 0
}
let port = resolve-test-port 3011
let url = $"http://localhost:($port)"
let db_path = (mktemp -d -t hydrant_signal_test.XXXXXX)
let random_str = ("a" + (random chars -l 5))
let collection = $"systems.hydrant.test.($random_str)"
print $"database path: ($db_path)"
let pds_url = resolve-pds $did
print $"resolved pds: ($pds_url)"
let session = authenticate $pds_url $did $password
let jwt = $session.accessJwt
print "authenticated"
let binary = build-hydrant
$env.HYDRANT_RELAY_HOST = "wss://bsky.network/"
let instance = start-hydrant $binary $db_path $port
mut test_passed = false
if (wait-for-api $url) {
# configure filter mode: index app.bsky.feed.post from anyone on the network
print "configuring filter mode..."
http patch -t application/json $"($url)/filter" {
mode: "filter",
signals: [$collection]
}
# verify filter state
let filter = (http get $"($url)/filter")
print $"filter state: ($filter | to json)"
if $filter.mode != "filter" {
print "FAILED: mode was not set to filter"
} else if not ($filter.signals | any { |s| $s == $collection }) {
print $"FAILED: ($collection) not in signals"
} else {
print "filter configured correctly"
# wait a moment for the firehose to connect and the filter to take effect
sleep 3sec
let timestamp = (date now | format date "%Y-%m-%dT%H:%M:%SZ")
let record_data = {
"$type": $collection,
text: $"hydrant signal filter test ($timestamp) - bsky.network relay",
createdAt: $timestamp
}
print "creating post..."
let create_res = (http post -t application/json -H ["Authorization" $"Bearer ($jwt)"] $"($pds_url)/xrpc/com.atproto.repo.createRecord" {
repo: $did,
collection: $collection,
validate: false,
record: $record_data
})
let rkey = ($create_res.uri | split row "/" | last)
print $"created: ($create_res.uri)"
print "waiting for record to be indexed..."
mut result = null
for i in 1..15 {
$result = (try {
http get $"($url)/xrpc/com.atproto.repo.getRecord?repo=($did)&collection=($collection)&rkey=($rkey)"
} catch {
null
})
if not ($result | is-empty) {
break
}
sleep 1sec
}
if ($result | is-empty) {
print "FAILED: record not found in hydrant index"
} else {
print $"indexed record cid: ($result.cid)"
print "test PASSED: signal filter correctly indexed the post"
$test_passed = true
}
# cleanup: delete the test post
print "cleaning up test post..."
try {
http post -t application/json -H ["Authorization" $"Bearer ($jwt)"] $"($pds_url)/xrpc/com.atproto.repo.deleteRecord" {
repo: $did,
collection: $collection,
rkey: $rkey
}
}
}
} else {
print "hydrant failed to start"
if ($instance.log | path exists) {
print "--- hydrant log ---"
print (open $instance.log)
print "-------------------"
}
}
print "stopping hydrant..."
try { kill -f $instance.pid }
if $test_passed {
exit 0
} else {
exit 1
}
}