| 1 | #!/bin/sh |
| 2 | # tests/run.sh -- offline test suite for plugin_slots |
| 3 | set -eu |
| 4 | |
| 5 | BIN="./plugin_slots" |
| 6 | PASS=0 |
| 7 | FAIL=0 |
| 8 | |
| 9 | ok() { PASS=$((PASS + 1)); printf ' PASS %s\n' "$1"; } |
| 10 | fail() { FAIL=$((FAIL + 1)); printf ' FAIL %s\n' "$1"; } |
| 11 | |
| 12 | assert_contains() { |
| 13 | case "$1" in |
| 14 | *"$2"*) ok "$3" ;; |
| 15 | *) fail "$3 (missing: $2)" ;; |
| 16 | esac |
| 17 | } |
| 18 | |
| 19 | TMP=$(mktemp -d) |
| 20 | trap 'rm -rf "$TMP"' EXIT |
| 21 | |
| 22 | NOW=$(date +%s) |
| 23 | mkdir -p "$TMP/sessions/demo-llama" |
| 24 | cat > "$TMP/sessions/demo-llama/meta.json" << 'EOF' |
| 25 | {"id":"demo-llama","cwd":"/tmp"} |
| 26 | EOF |
| 27 | cat > "$TMP/sessions/demo-llama/slots.json" << EOF |
| 28 | {"updated_epoch":${NOW},"dspark":true,"slots":[{"id":0,"is_processing":true,"n_ctx":131072,"tg":48.2,"pp":1420,"n_prompt_tokens":12000,"n_prompt_tokens_processed":2000,"n_decoded":350},{"id":1,"is_processing":false,"n_ctx":131072}]} |
| 29 | EOF |
| 30 | |
| 31 | echo "==> plugin_slots offline tests" |
| 32 | |
| 33 | # 1. --meta |
| 34 | OUT=$("$BIN" --meta) |
| 35 | assert_contains "$OUT" '"name":"slots"' "meta name" |
| 36 | assert_contains "$OUT" '"desc":"llama.cpp slots"' "meta desc" |
| 37 | assert_contains "$OUT" '"kind":"ui"' "meta kind" |
| 38 | |
| 39 | # 2. SX_PLUGIN_META=1 |
| 40 | OUT=$(SX_PLUGIN_META=1 "$BIN") |
| 41 | assert_contains "$OUT" '"name":"slots"' "SX_PLUGIN_META=1" |
| 42 | |
| 43 | # 3. Auth gate |
| 44 | OUT=$(BOT_PASSWORD="demo-auth-val" QUERY_STRING="action=sessions" "$BIN") |
| 45 | assert_contains "$OUT" 'Status: 401 Unauthorized' "auth rejects missing password" |
| 46 | |
| 47 | OUT=$(BOT_PASSWORD="demo-auth-val" HTTP_X_BOT_PASSWORD="demo-auth-val" SX_SESSIONS_DIR="$TMP/sessions" SCORPIOX_TMUX_BIN="/nonexistent" QUERY_STRING="action=sessions" "$BIN") |
| 48 | assert_contains "$OUT" '"ok":true' "auth accepts X-Bot-Password" |
| 49 | assert_contains "$OUT" '"demo-llama"' "sessions lists demo-llama" |
| 50 | assert_contains "$OUT" '"has_slots":true' "sessions flags has_slots=true" |
| 51 | |
| 52 | # 4. Slots from session slots.json |
| 53 | OUT=$(SX_SESSIONS_DIR="$TMP/sessions" SCORPIOX_TMUX_BIN="/nonexistent" QUERY_STRING="action=slots&id=demo-llama" "$BIN") |
| 54 | assert_contains "$OUT" '"dspark":true' "slots returns dspark" |
| 55 | assert_contains "$OUT" '"tg":48.2' "slots returns tg" |
| 56 | |
| 57 | # 5. Missing session returns 404 |
| 58 | OUT=$(SX_SESSIONS_DIR="$TMP/sessions" SCORPIOX_TMUX_BIN="/nonexistent" QUERY_STRING="action=slots&id=missing-sess" "$BIN") |
| 59 | assert_contains "$OUT" 'Status: 404 Not Found' "missing session returns 404" |
| 60 | |
| 61 | # 6. Bad URL rejected (400) |
| 62 | OUT=$(QUERY_STRING="action=slots&url=file%3A%2F%2F%2Fetc%2Fpasswd" "$BIN") |
| 63 | assert_contains "$OUT" 'Status: 400 Bad Request' "rejects non-http scheme" |
| 64 | |
| 65 | # 7. HTML UI & theme handoff |
| 66 | OUT=$(QUERY_STRING="embed=1&theme=dark" "$BIN") |
| 67 | assert_contains "$OUT" 'Content-Type: text/html' "html content-type" |
| 68 | assert_contains "$OUT" 'sx-theme-request' "theme handoff script present" |
| 69 | assert_contains "$OUT" 'Slots Monitor' "html title present" |
| 70 | |
| 71 | printf '\nTotal: %d passed, %d failed\n' "$PASS" "$FAIL" |
| 72 | [ "$FAIL" -eq 0 ] |