#!/usr/bin/env nu
# checks that the harness skips a dead plc mirror instead of failing every test that resolves
use common.nu *
const DID = "did:plc:harnessfallback"
const PDS = "https://pds.fallback.example"
def main [] {
let dead = $"http://127.0.0.1:(resolve-test-port 3020)"
let mock_port = resolve-test-mock-port 3022
let live = $"http://127.0.0.1:($mock_port)"
let doc = {
id: $DID,
service: [{id: "#atproto_pds", type: "AtprotoPersonalDataServer", serviceEndpoint: $PDS}]
}
let dir = (mktemp -d -t hydrant_plc_mock.XXXXXX)
$"{|req|
if $req.path == '/_health' {
{} | to json
} else if $req.path == '/($DID)' {
($doc | to nuon) | to json
} else {
'not found' | metadata set { merge {http.response: {status: 404}} }
}
}" | save $"($dir)/plc.nu"
let log = $"($dir)/plc.log"
let mock_pid = (bash -c $"http-nu :($mock_port) ($dir)/plc.nu > ($log) 2>&1 & echo $!" | str trim | into int)
mut up = false
for _ in 1..50 {
if (try { http get $"($live)/_health"; true } catch { false }) {
$up = true
break
}
sleep 100ms
}
if not $up {
fail $"mock plc did not start: (open --raw $log)" $mock_pid
}
print " a dead mirror falls through to the next plc..."
with-env {HYDRANT_TEST_PLC_URL: $"($dead),($live)"} {
let urls = plc-urls
if $urls != [$live] {
fail $"expected only the live plc, got ($urls)" $mock_pid
}
let pds = resolve-pds $DID
if $pds != $PDS {
fail $"expected ($PDS), got ($pds)" $mock_pid
}
}
print " a live mirror keeps the later ones as fallbacks for hydrant..."
with-env {HYDRANT_TEST_PLC_URL: $"($live)/, ($dead)"} {
let urls = plc-urls
if $urls != [$live $dead] {
fail $"expected live then dead, got ($urls)" $mock_pid
}
}
print " when nothing answers, resolve-pds names every plc it tried..."
with-env {HYDRANT_TEST_PLC_URL: $dead} {
let err = (try { resolve-pds $DID; null } catch { |e| $e.msg })
if $err == null or not ($err | str contains $dead) {
fail $"expected an error naming ($dead), got ($err)" $mock_pid
}
}
kill $mock_pid
print "plc fallback passed!"
}