Files
supply-intelligence/scripts/gateway_closure_smoke.sh
2026-05-12 18:49:52 +08:00

77 lines
2.7 KiB
Bash

#!/usr/bin/env bash
set -euo pipefail
BASE_URL="${BASE_URL:-http://127.0.0.1:8080}"
PLATFORM="${PLATFORM:-openai}"
MODEL="${MODEL:-gpt-4.1-mini}"
EVENT_ID="${EVENT_ID:-evt-smoke-$(date +%s)}"
OCCURRED_AT="${OCCURRED_AT:-$(date -u +%Y-%m-%dT%H:%M:%SZ)}"
CANDIDATE_STATUS_EXPECTED="${CANDIDATE_STATUS_EXPECTED:-published}"
need() {
command -v "$1" >/dev/null 2>&1 || {
echo "missing required command: $1" >&2
exit 1
}
}
need curl
need python3
json_get() {
local expr="$1"
python3 -c "import json,sys; data=json.load(sys.stdin); print($expr)"
}
echo "[1/4] publish package event"
publish_resp=$(curl -fsS -X POST "$BASE_URL/internal/supply-intelligence/publish/package-event" \
-H 'Content-Type: application/json' \
-d "{\"event_id\":\"$EVENT_ID\",\"platform\":\"$PLATFORM\",\"model\":\"$MODEL\",\"occurred_at\":\"$OCCURRED_AT\"}")
echo "$publish_resp"
publish_event_id=$(printf '%s' "$publish_resp" | json_get "data['event']['event_id']")
[ "$publish_event_id" = "$EVENT_ID" ] || {
echo "publish returned unexpected event id: $publish_event_id" >&2
exit 1
}
echo "[2/4] trigger consume-once"
consume_resp=$(curl -fsS -X POST "$BASE_URL/internal/supply-intelligence/gateway/consume-once" \
-H 'Content-Type: application/json' \
-d '{"consumer":"gateway"}')
echo "$consume_resp"
consume_items=$(printf '%s' "$consume_resp" | json_get "len(data['items'])")
[ "$consume_items" -ge 1 ] || {
echo "consume-once returned no items" >&2
exit 1
}
echo "[3/4] verify package change list includes event"
changes_resp=$(curl -fsS "$BASE_URL/internal/supply-intelligence/gateway/package-changes")
echo "$changes_resp"
found=$(printf '%s' "$changes_resp" | python3 -c "import json,sys; data=json.load(sys.stdin); print(any(item.get('event_id') == '$EVENT_ID' for item in data.get('items', [])))")
[ "$found" = "True" ] || {
echo "package change list missing event $EVENT_ID" >&2
exit 1
}
echo "[4/4] verify admission-state reflects publish/consume state"
admission_resp=$(curl -fsS "$BASE_URL/internal/supply-intelligence/models/$PLATFORM/$MODEL/admission-state")
echo "$admission_resp"
candidate_status=$(printf '%s' "$admission_resp" | json_get "data['candidate']['status'] if data.get('candidate') else ''")
gateway_status=$(printf '%s' "$admission_resp" | json_get "data.get('gateway_sync_status', '')")
[ "$candidate_status" = "$CANDIDATE_STATUS_EXPECTED" ] || {
echo "unexpected candidate status: $candidate_status" >&2
exit 1
}
case "$gateway_status" in
applied|pending|failed) ;;
*)
echo "unexpected gateway sync status: $gateway_status" >&2
exit 1
;;
esac
echo "gateway closure smoke passed: event=$EVENT_ID candidate_status=$candidate_status gateway_sync_status=$gateway_status"