Files
small-scripts/tests/test-agent-subscriptions.sh
Paul O'Reilly 52eb484ef7 agent-subscriptions: add window_seconds and elapsed_pct to JSON output
Elapsed is null when a window has no reset timestamp (MiniMax v1 fallback)
so pacing consumers fail closed; negative reset_in_seconds clamps to 0.

Claude-Session: https://claude.ai/code/session_01YQDoWNM7XPPii28khFWoMc
2026-08-02 21:18:02 +12:00

175 lines
7.7 KiB
Bash
Executable File

#!/usr/bin/env bash
set -uo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
SCRIPT="$SCRIPT_DIR/../scripts/agent-subscriptions"
GREEN='\033[32m'; RED='\033[31m'; RESET='\033[0m'
pass() { echo -e "${GREEN}PASS${RESET}: $1"; }
fail() { echo -e "${RED}FAIL${RESET}: $1"; FAILURES=$((FAILURES + 1)); }
FAILURES=0
# --dryrun: exits 0
output=$("$SCRIPT" --dryrun 2>&1)
code=$?
[[ $code -eq 0 ]] && pass "--dryrun exits 0" || fail "--dryrun exits 0 (got $code)"
# --dryrun: mentions Anthropic
echo "$output" | grep -qi "Anthropic" && pass "--dryrun mentions Anthropic" || fail "--dryrun mentions Anthropic"
# --dryrun: mentions MiniMax
echo "$output" | grep -qi "MiniMax" && pass "--dryrun mentions MiniMax" || fail "--dryrun mentions MiniMax"
# --dryrun: shows probe URL for Anthropic
echo "$output" | grep -q "api.anthropic.com" && pass "--dryrun shows Anthropic URL" || fail "--dryrun shows Anthropic URL"
# --dryrun: shows probe URL for MiniMax
echo "$output" | grep -q "minimax.io" && pass "--dryrun shows MiniMax URL" || fail "--dryrun shows MiniMax URL"
# --dryrun: mentions SOPS
echo "$output" | grep -qi "SOPS\|sops" && pass "--dryrun mentions SOPS" || fail "--dryrun mentions SOPS"
# -n (short flag): exits 0
"$SCRIPT" -n >/dev/null 2>&1
code=$?
[[ $code -eq 0 ]] && pass "-n exits 0" || fail "-n exits 0 (got $code)"
# --help: exits 0
"$SCRIPT" --help >/dev/null 2>&1
code=$?
[[ $code -eq 0 ]] && pass "--help exits 0" || fail "--help exits 0 (got $code)"
# --help: mentions dryrun
help_out=$("$SCRIPT" --help 2>&1)
echo "$help_out" | grep -q "\-\-dryrun\|-n" && pass "--help documents dryrun" || fail "--help documents dryrun"
# Unknown flag: exits non-zero
"$SCRIPT" --bogus-flag >/dev/null 2>&1
code=$?
[[ $code -ne 0 ]] && pass "unknown flag exits non-zero" || fail "unknown flag exits non-zero (got 0)"
# --help: documents --output
help_out=$("$SCRIPT" --help 2>&1)
echo "$help_out" | grep -q "\-\-output" && pass "--help documents --output" || fail "--help documents --output"
# --help: documents -j short flag
echo "$help_out" | grep -q "\-j" && pass "--help documents -j" || fail "--help documents -j"
# --output json: exits 0
json_out=$("$SCRIPT" --dryrun --output json 2>&1)
code=$?
[[ $code -eq 0 ]] && pass "--output json exits 0" || fail "--output json exits 0 (got $code)"
# --output json: contains top-level "providers" key
echo "$json_out" | grep -q '"providers"' && pass "--output json contains providers key" || fail "--output json contains providers key"
# --output json: contains probed_at key
echo "$json_out" | grep -q '"probed_at"' && pass "--output json contains probed_at" || fail "--output json contains probed_at"
# --output json: contains Anthropic provider name
echo "$json_out" | grep -q '"Anthropic"' && pass "--output json contains Anthropic" || fail "--output json contains Anthropic"
# --output json: contains MiniMax provider name
echo "$json_out" | grep -q '"MiniMax"' && pass "--output json contains MiniMax" || fail "--output json contains MiniMax"
# --output json: contains utilization_pct field
echo "$json_out" | grep -q '"utilization_pct"' && pass "--output json contains utilization_pct" || fail "--output json contains utilization_pct"
# --output json: contains reset_at field
echo "$json_out" | grep -q '"reset_at"' && pass "--output json contains reset_at" || fail "--output json contains reset_at"
# --output json: contains reset_in_seconds field
echo "$json_out" | grep -q '"reset_in_seconds"' && pass "--output json contains reset_in_seconds" || fail "--output json contains reset_in_seconds"
# --output json: contains seven_day window
echo "$json_out" | grep -q '"seven_day"' && pass "--output json contains seven_day window" || fail "--output json contains seven_day window"
# --output json: contains five_hour window
echo "$json_out" | grep -q '"five_hour"' && pass "--output json contains five_hour window" || fail "--output json contains five_hour window"
# --output json: well-formed JSON (python json.loads parses it)
echo "$json_out" | python3 -c "import sys, json; json.loads(sys.stdin.read())" 2>/dev/null
[[ $? -eq 0 ]] && pass "--output json is valid JSON" || fail "--output json is valid JSON"
# -j (short flag): identical to --output json
short_out=$("$SCRIPT" --dryrun -j 2>&1)
[[ "$json_out" == "$short_out" ]] && pass "-j matches --output json output" || fail "-j matches --output json output"
# -j (short flag): exits 0
"$SCRIPT" --dryrun -j >/dev/null 2>&1
code=$?
[[ $code -eq 0 ]] && pass "-j exits 0" || fail "-j exits 0 (got $code)"
# --output=json (equals form): exits 0
"$SCRIPT" --dryrun --output=json >/dev/null 2>&1
code=$?
[[ $code -eq 0 ]] && pass "--output=json exits 0" || fail "--output=json exits 0 (got $code)"
# --output json (default mode unchanged): dryrun emits probe preview, not JSON
table_out=$("$SCRIPT" --dryrun 2>&1)
echo "$table_out" | grep -q "\[dryrun\]" && pass "default dryrun emits [dryrun] header" || fail "default dryrun emits [dryrun] header"
echo "$table_out" | grep -q '"providers"' && fail "default mode should not emit JSON" || pass "default mode does not emit JSON"
# --dryrun --output json: no API calls attempted (output is JSON, not probe failure text)
echo "$json_out" | grep -qi "failed\|UNAVAILABLE\|HTTP " && fail "--dryrun --output json made API calls" || pass "--dryrun --output json made no API calls"
# --output non-json value: exits non-zero
"$SCRIPT" --dryrun --output yaml >/dev/null 2>&1
code=$?
[[ $code -ne 0 ]] && pass "--output yaml exits non-zero" || fail "--output yaml exits non-zero (got 0)"
# --output with no value: exits non-zero
"$SCRIPT" --output >/dev/null 2>&1
code=$?
[[ $code -ne 0 ]] && pass "--output with no value exits non-zero" || fail "--output with no value exits non-zero (got 0)"
# --output json: contains window_seconds field
echo "$json_out" | grep -q '"window_seconds"' && pass "--output json contains window_seconds" || fail "--output json contains window_seconds"
# --output json: contains elapsed_pct field
echo "$json_out" | grep -q '"elapsed_pct"' && pass "--output json contains elapsed_pct" || fail "--output json contains elapsed_pct"
# --output json: window_seconds and elapsed_pct present on every window, per provider,
# window_seconds equals the expected constant, elapsed_pct in [0,1] or null.
python3 - "$json_out" <<'PYEOF'
import json, sys
data = json.loads(sys.argv[1])
expected_window_seconds = {"five_hour": 18000, "seven_day": 604800}
ok = True
for provider in data.get("providers", []):
windows = provider.get("windows")
if windows is None:
continue # unavailable provider, no windows to check
for key, expected_seconds in expected_window_seconds.items():
win = windows.get(key)
if win is None:
print(f"FAIL: {provider.get('provider')} missing window {key}")
ok = False
continue
if "window_seconds" not in win or "elapsed_pct" not in win:
print(f"FAIL: {provider.get('provider')}.{key} missing window_seconds/elapsed_pct")
ok = False
continue
if win["window_seconds"] != expected_seconds:
print(f"FAIL: {provider.get('provider')}.{key} window_seconds={win['window_seconds']!r}, expected {expected_seconds}")
ok = False
pct = win["elapsed_pct"]
if pct is not None and not (0.0 <= pct <= 1.0):
print(f"FAIL: {provider.get('provider')}.{key} elapsed_pct={pct!r} out of [0,1]")
ok = False
sys.exit(0 if ok else 1)
PYEOF
[[ $? -eq 0 ]] && pass "--output json: window_seconds/elapsed_pct present, valid, and match constants" || fail "--output json: window_seconds/elapsed_pct present, valid, and match constants"
echo
if [[ $FAILURES -eq 0 ]]; then
echo -e "${GREEN}All tests passed.${RESET}"
exit 0
else
echo -e "${RED}$FAILURES test(s) failed.${RESET}"
exit 1
fi