scorpiox-bot-plugin-seqthink
←run.shbash
529 lines · 33 KBHEADBlameDownload
1#!/bin/sh
2# tests/run.sh - offline checks for plugin_seqthink across all 4 modes.
3#
4# Runs against ./plugin_seqthink (native build) on the live machine. The --mcp
5# leg is driven with a scripted JSON-RPC session on stdin; the CGI leg is
6# driven through QUERY_STRING / REQUEST_METHOD / CONTENT_LENGTH; the chain
7# store is pointed at a scratch directory through SEQTHINK_STATE_DIR so the
8# real node directory is never touched. The last section audits the whole git
9# history for secrets and internal addresses.
10#
11# Exit code 0 = everything passed.
12
13set -u
14
15ROOT=$(cd "$(dirname "$0")/.." && pwd)
16cd "$ROOT" || exit 1
17
18BIN=./plugin_seqthink
19PASS=0
20FAIL=0
21TMP=$(mktemp -d 2>/dev/null || echo /tmp/seqthink_test_$$)
22mkdir -p "$TMP"
23trap 'rm -rf "$TMP"' EXIT
24
25if [ -t 1 ]; then
26 C_OK=$(printf '\033[32m'); C_BAD=$(printf '\033[31m'); C_B=$(printf '\033[1m'); C_0=$(printf '\033[0m')
27else
28 C_OK=; C_BAD=; C_B=; C_0=
29fi
30pass() { PASS=$((PASS + 1)); printf ' %sok%s %s\n' "$C_OK" "$C_0" "$1"; }
31fail() { FAIL=$((FAIL + 1)); printf ' %sFAIL%s %s\n' "$C_BAD" "$C_0" "$1"; }
32
33check() { # check <desc> <expected-substring> <actual>
34 if printf '%s' "$3" | grep -F -q -- "$2"; then pass "$1"; else
35 fail "$1 (want: $2)"; printf ' got: %s\n' "$(printf '%s' "$3" | head -c 300)"
36 fi
37}
38
39check_not() { # check_not <desc> <forbidden-substring> <actual>
40 if printf '%s' "$3" | grep -F -q -- "$2"; then
41 fail "$1 (must NOT contain: $2)"; else pass "$1"
42 fi
43}
44
45section() { printf '\n%s== %s ==%s\n' "$C_B" "$1" "$C_0"; }
46body() { printf '%s' "$1" | sed '1,/^\r$/d'; }
47
48jq_() { # jq_ <desc> <python-expression using d> <json>
49 printf '%s' "$3" > "$TMP/_j.json"
50 if python3 - "$TMP/_j.json" "$2" <<'PY' 2>/dev/null
51import json, sys
52d = json.load(open(sys.argv[1]))
53assert eval(sys.argv[2], {"d": d}), sys.argv[2]
54PY
55 then pass "$1"; else fail "$1"; fi
56}
57
58mcp() { # mcp <state-dir> [env assignments...] -- feeds $TMP/in.jsonl
59 local dir="$1"; shift
60 SEQTHINK_STATE_DIR="$dir" env "$@" "$BIN" --mcp < "$TMP/in.jsonl" 2>"$TMP/err.txt"
61}
62
63# ---------------------------------------------------------------- build
64section "build"
65if [ ! -x "$BIN" ]; then
66 echo " building native binary..."
67 cc -O2 -std=c99 -Wall -Wextra -I src -I vendor -o "$BIN" src/plugin_seqthink.c vendor/yyjson.c \
68 || { echo " build failed"; exit 1; }
69fi
70[ -x "$BIN" ] && pass "native binary exists and is executable" || fail "native binary missing"
71
72SRC=src/plugin_seqthink.c
73SRC_TXT=$(cat "$SRC")
74
75# ---------------------------------------------------------------- mode 1: --meta
76section "mode 1 --meta (must work without auth - discovery)"
77META=$("$BIN" --meta)
78check "meta emits name seqthink" '"name":"seqthink"' "$META"
79check "meta emits desc" '"desc":"Thought chains"' "$META"
80check "meta declares kind mcp" '"kind":"mcp"' "$META"
81check "meta declares mcp true" '"mcp":true' "$META"
82check "meta declares daemon false" '"daemon":false' "$META"
83check "meta declares menus false" '"menus":false' "$META"
84check "meta declares verified" '"verified":"live"' "$META"
85METAENV=$(SX_PLUGIN_META=1 "$BIN")
86check "SX_PLUGIN_META=1 yields same meta" '"name":"seqthink"' "$METAENV"
87[ "$META" = "$METAENV" ] && pass "argv and env meta identical" || fail "argv and env meta differ"
88if [ "$(printf '%s\n' "$META" | wc -l)" -eq 1 ]; then pass "meta is exactly one line"; else fail "meta is more than one line"; fi
89check "--version names the plugin" "plugin_seqthink" "$("$BIN" --version)"
90check "--schema is the input schema" '"required":["thought","nextThoughtNeeded","thoughtNumber","totalThoughts"]' \
91 "$("$BIN" --schema)"
92
93section "plugin.json agrees with --meta"
94PJ=$(cat plugin.json)
95jq_ "plugin.json slug/id" "d['slug']=='seqthink' and d['id']=='plugin_seqthink'" "$PJ"
96jq_ "plugin.json category" "d['category']=='devtools'" "$PJ"
97jq_ "plugin.json kind/mcp" "d['kind']=='mcp' and d['mcp'] is True" "$PJ"
98jq_ "plugin.json daemon/menus" "d['daemon'] is False and d['menus'] is False" "$PJ"
99jq_ "plugin.json verified" "d['verified']=='live'" "$PJ"
100jq_ "plugin.json has title/summary/trust" "d['title'] and len(d['summary'])>80 and d['trust_notice']" "$PJ"
101check_not "plugin.json has no LAN address" "192.168." "$PJ"
102
103# ---------------------------------------------------------------- mode 2: --mcp
104section "mode 2 --mcp handshake"
105cat > "$TMP/in.jsonl" <<'JSONL'
106{"jsonrpc":"2.0","id":1,"method":"initialize","params":{"protocolVersion":"2024-11-05","capabilities":{},"clientInfo":{"name":"probe","version":"1"}}}
107{"jsonrpc":"2.0","method":"notifications/initialized"}
108{"jsonrpc":"2.0","id":2,"method":"tools/list"}
109{"jsonrpc":"2.0","id":3,"method":"ping"}
110{"jsonrpc":"2.0","id":4,"method":"not/a/method"}
111JSONL
112OUT=$(mcp "$TMP/m_hand" env)
113R1=$(printf '%s' "$OUT" | sed -n 1p)
114R2=$(printf '%s' "$OUT" | sed -n 2p)
115R3=$(printf '%s' "$OUT" | sed -n 3p)
116R4=$(printf '%s' "$OUT" | sed -n 4p)
117check "initialize answers jsonrpc 2.0" '"jsonrpc":"2.0"' "$R1"
118check "initialize echoes id" '"id":1' "$R1"
119check "initialize carries protocol" '"protocolVersion":"2024-11-05"' "$R1"
120check "initialize advertises tools" '"capabilities":{"tools":{}}' "$R1"
121check "initialize names the server" '"name":"seqthink"' "$R1"
122check "notifications get no reply" '"id":2' "$R2"
123check "tools/list offers sequentialthinking" '"name":"sequentialthinking"' "$R2"
124check "tool carries a title" '"title":"Sequential Thinking"' "$R2"
125check "tool carries annotations" '"readOnlyHint":false' "$R2"
126check "annotations not idempotent" '"idempotentHint":false' "$R2"
127check "tool carries inputSchema" '"inputSchema"' "$R2"
128check "inputSchema requires 4 fields" '"required":["thought","nextThoughtNeeded","thoughtNumber","totalThoughts"]' "$R2"
129check "inputSchema minimum on thoughtNumber" '"thoughtNumber":{"type":"integer"' "$R2"
130check "tool carries outputSchema" '"thoughtHistoryLength":{"type":"number"}' "$R2"
131check "ping answered empty result" '"result":{}' "$R3"
132check "unknown method -> -32601" '"code":-32601' "$R4"
133
134section "mode 2 --mcp tool: happy path, revision, branch"
135cat > "$TMP/in.jsonl" <<'JSONL'
136{"jsonrpc":"2.0","id":11,"method":"tools/call","params":{"name":"sequentialthinking","arguments":{"thought":"break the problem down","nextThoughtNeeded":true,"thoughtNumber":1,"totalThoughts":3}}}
137{"jsonrpc":"2.0","id":12,"method":"tools/call","params":{"name":"sequentialthinking","arguments":{"thought":"question an earlier step","nextThoughtNeeded":"true","isRevision":true,"revisesThought":1,"thoughtNumber":2,"totalThoughts":3,"needsMoreThoughts":true}}}
138{"jsonrpc":"2.0","id":13,"method":"tools/call","params":{"name":"sequentialthinking","arguments":{"thought":"an alternative path","nextThoughtNeeded":"false","branchFromThought":1,"branchId":"alt-route","thoughtNumber":3,"totalThoughts":3}}}
139{"jsonrpc":"2.0","id":14,"method":"tools/call","params":{"name":"sequentialthinking","arguments":{"thought":"past the estimate","nextThoughtNeeded":false,"thoughtNumber":9,"totalThoughts":3}}}
140JSONL
141OUT=$(mcp "$TMP/m_ok" env)
142L11=$(printf '%s' "$OUT" | sed -n 1p)
143L12=$(printf '%s' "$OUT" | sed -n 2p)
144L13=$(printf '%s' "$OUT" | sed -n 3p)
145L14=$(printf '%s' "$OUT" | sed -n 4p)
146check "call 1 content is text" '"type":"text"' "$L11"
147check "call 1 reports thoughtNumber" 'thoughtNumber\":1' "$L11"
148check "call 1 history length 1" 'thoughtHistoryLength\":1}' "$L11"
149check "call 1 not an error" '"isError":false' "$L11"
150check "call 1 has structuredContent" '"structuredContent":{"thoughtNumber":1' "$L11"
151check "call 2 accepted a string bool" '"isError":false' "$L12"
152check "call 2 history length 2" 'thoughtHistoryLength\":2}' "$L12"
153check "call 3 records the branch" 'branches\":[\"alt-route\"' "$L13"
154check "call 4 raised totalThoughts" 'totalThoughts\":9' "$L14"
155check "call 4 kept the branch list" 'alt-route' "$L14"
156check "stderr logged thought 1" 'Thought 1/3' "$(cat "$TMP/err.txt")"
157check "stderr marked the revision" 'Revision 2/3 (revising thought 1)' "$(cat "$TMP/err.txt")"
158check "stderr marked the branch" 'Branch 3/3 (from thought 1, ID: alt-route)' "$(cat "$TMP/err.txt")"
159# (structuredContent parity is asserted in detail below)
160printf '%s' "$L11" > "$TMP/_c.json"
161if python3 - "$TMP/_c.json" <<'PY' 2>/dev/null
162import json, sys
163d = json.load(open(sys.argv[1]))
164r = d["result"]
165sc = r["structuredContent"]
166txt = json.loads(r["content"][0]["text"])
167assert sc == txt, (sc, txt)
168assert sorted(sc) == ["branches", "nextThoughtNeeded", "thoughtHistoryLength",
169 "thoughtNumber", "totalThoughts"], sorted(sc)
170assert sc["thoughtNumber"] == 1 and sc["totalThoughts"] == 3
171assert sc["nextThoughtNeeded"] is True
172assert sc["branches"] == [] and sc["thoughtHistoryLength"] == 1
173PY
174then pass "structuredContent == parsed content text, exactly 5 fields"
175else fail "structuredContent == parsed content text, exactly 5 fields"
176fi
177
178section "mode 2 --mcp tool: validation errors mirror upstream"
179cat > "$TMP/in.jsonl" <<'JSONL'
180{"jsonrpc":"2.0","id":21,"method":"tools/call","params":{"name":"sequentialthinking","arguments":{"thought":"","nextThoughtNeeded":true,"thoughtNumber":1,"totalThoughts":1}}}
181{"jsonrpc":"2.0","id":22,"method":"tools/call","params":{"name":"sequentialthinking","arguments":{"nextThoughtNeeded":true,"thoughtNumber":1,"totalThoughts":1}}}
182{"jsonrpc":"2.0","id":23,"method":"tools/call","params":{"name":"sequentialthinking","arguments":{"thought":"x","nextThoughtNeeded":true,"thoughtNumber":0,"totalThoughts":1}}}
183{"jsonrpc":"2.0","id":24,"method":"tools/call","params":{"name":"sequentialthinking","arguments":{"thought":"x","nextThoughtNeeded":true,"thoughtNumber":"abc","totalThoughts":1}}}
184{"jsonrpc":"2.0","id":25,"method":"tools/call","params":{"name":"sequentialthinking","arguments":{"thought":"x","nextThoughtNeeded":true,"thoughtNumber":1,"totalThoughts":0}}}
185{"jsonrpc":"2.0","id":26,"method":"tools/call","params":{"name":"sequentialthinking","arguments":{"thought":"x","nextThoughtNeeded":"yes","thoughtNumber":1,"totalThoughts":1}}}
186{"jsonrpc":"2.0","id":27,"method":"tools/call","params":{"name":"sequentialthinking","arguments":{"thought":"x","thoughtNumber":1,"totalThoughts":1}}}
187{"jsonrpc":"2.0","id":28,"method":"tools/call","params":{"name":"sequentialthinking","arguments":{"thought":"x","nextThoughtNeeded":true,"isRevision":"maybe","thoughtNumber":1,"totalThoughts":1}}}
188{"jsonrpc":"2.0","id":29,"method":"tools/call","params":{"name":"sequentialthinking","arguments":{"thought":"x","nextThoughtNeeded":true,"revisesThought":0,"thoughtNumber":1,"totalThoughts":1}}}
189{"jsonrpc":"2.0","id":30,"method":"tools/call","params":{"name":"sequentialthinking","arguments":{"thought":"x","nextThoughtNeeded":true,"branchFromThought":-2,"thoughtNumber":1,"totalThoughts":1}}}
190{"jsonrpc":"2.0","id":31,"method":"tools/call","params":{"name":"sequentialthinking","arguments":{"thought":"x","nextThoughtNeeded":true,"branchId":42,"thoughtNumber":1,"totalThoughts":1}}}
191{"jsonrpc":"2.0","id":32,"method":"tools/call","params":{"name":"sequentialthinking","arguments":{"thought":"x","nextThoughtNeeded":true,"needsMoreThoughts":"perhaps","thoughtNumber":1,"totalThoughts":1}}}
192{"jsonrpc":"2.0","id":33,"method":"tools/call","params":{"name":"nope","arguments":{}}}
193{"jsonrpc":"2.0","id":34,"method":"tools/call","params":{"name":"sequentialthinking"}}
194JSONL
195OUT=$(mcp "$TMP/m_err" env)
196E1=$(printf '%s' "$OUT" | sed -n 1p)
197E2=$(printf '%s' "$OUT" | sed -n 2p)
198E3=$(printf '%s' "$OUT" | sed -n 3p)
199E4=$(printf '%s' "$OUT" | sed -n 4p)
200E5=$(printf '%s' "$OUT" | sed -n 5p)
201E6=$(printf '%s' "$OUT" | sed -n 6p)
202E7=$(printf '%s' "$OUT" | sed -n 7p)
203E8=$(printf '%s' "$OUT" | sed -n 8p)
204E9=$(printf '%s' "$OUT" | sed -n 9p)
205E10=$(printf '%s' "$OUT" | sed -n 10p)
206E11=$(printf '%s' "$OUT" | sed -n 11p)
207E12=$(printf '%s' "$OUT" | sed -n 12p)
208E13=$(printf '%s' "$OUT" | sed -n 13p)
209E14=$(printf '%s' "$OUT" | sed -n 14p)
210check "empty thought refused" 'Invalid thought: must be a string' "$E1"
211check "missing thought refused" 'Invalid thought: must be a string' "$E2"
212check "thoughtNumber 0 refused" 'Invalid thoughtNumber: must be a number' "$E3"
213check "non-numeric thoughtNumber" 'Invalid thoughtNumber: must be a number' "$E4"
214check "totalThoughts 0 refused" 'Invalid totalThoughts: must be a number' "$E5"
215check "bad nextThoughtNeeded" 'Invalid nextThoughtNeeded: must be a boolean' "$E6"
216check "missing nextThoughtNeeded" 'Invalid nextThoughtNeeded: must be a boolean' "$E7"
217check "bad isRevision" 'Invalid isRevision: must be a boolean' "$E8"
218check "bad revisesThought" 'Invalid revisesThought: must be a number' "$E9"
219check "bad branchFromThought" 'Invalid branchFromThought: must be a number' "$E10"
220check "branchId must be a string" 'Invalid branchId: must be a string' "$E11"
221check "bad needsMoreThoughts" 'Invalid needsMoreThoughts: must be a boolean' "$E12"
222check "all refusals set isError" '"isError":true' "$E1"
223check "all refusals say failed" '"status\":\"failed\"' "$E1"
224check "unknown tool -> -32602" '"code":-32602' "$E13"
225check "missing arguments refused" 'Invalid thought: must be a string' "$E14"
226cat > "$TMP/in.jsonl" <<'JSONL'
227{"jsonrpc":"2.0","id":41,"method":"tools/call","params":{"name":"sequentialthinking","arguments":{"thought":"coerce me","nextThoughtNeeded":"TRUE","thoughtNumber":"2","totalThoughts":"4"}}}
228JSONL
229OUT=$(mcp "$TMP/m_coerce" env)
230check "string numbers coerce (upstream z.coerce)" 'totalThoughts\":4' "$OUT"
231check "string TRUE coerces to true" 'nextThoughtNeeded\":true' "$OUT"
232
233section "mode 2 --mcp: history cap and branch eviction"
234python3 - > "$TMP/in.jsonl" <<'PY'
235import json
236print(json.dumps({"jsonrpc":"2.0","id":1,"method":"tools/call","params":{"name":"sequentialthinking","arguments":{"thought":"base","nextThoughtNeeded":True,"thoughtNumber":1,"totalThoughts":9}}}))
237for i in range(2, 7):
238 print(json.dumps({"jsonrpc":"2.0","id":i,"method":"tools/call","params":{"name":"sequentialthinking","arguments":{"thought":"beta%d" % i,"nextThoughtNeeded":True,"branchFromThought":1,"branchId":"beta","thoughtNumber":i,"totalThoughts":9}}}))
239for i in range(7, 9):
240 print(json.dumps({"jsonrpc":"2.0","id":i,"method":"tools/call","params":{"name":"sequentialthinking","arguments":{"thought":"gamma%d" % i,"nextThoughtNeeded":True,"branchFromThought":1,"branchId":"gamma","thoughtNumber":i,"totalThoughts":9}}}))
241PY
242OUT=$(mcp "$TMP/m_cap1" MAX_THOUGHT_HISTORY=1)
243LAST=$(printf '%s' "$OUT" | tail -1)
244check "cap 1 keeps one thought" 'thoughtHistoryLength\":1}' "$LAST"
245check "cap 1 emptied beta dropped" 'branches\":[\"gamma\"]' "$LAST"
246check_not "cap 1 does not list beta" '\"beta\"' "$LAST"
247OUT=$(mcp "$TMP/m_cap2" MAX_THOUGHT_HISTORY=3)
248LAST=$(printf '%s' "$OUT" | tail -1)
249check "cap 3 keeps three thoughts" 'thoughtHistoryLength\":3}' "$LAST"
250check "cap 3 still lists beta" 'beta' "$LAST"
251OUT=$(mcp "$TMP/m_bad" MAX_THOUGHT_HISTORY=abc)
252check "bad cap warns on stderr" 'MAX_THOUGHT_HISTORY must be a positive integer' "$(cat "$TMP/err.txt")"
253check "bad cap falls back to default" "thoughtHistoryLength\\\":1}" "$OUT"
254OUT=$(mcp "$TMP/m_neg" MAX_THOUGHT_HISTORY=0)
255check "zero cap rejected too" 'must be a positive integer' "$(cat "$TMP/err.txt")"
256
257section "mode 2 --mcp: DISABLE_THOUGHT_LOGGING (upstream parity)"
258OUT=$(mcp "$TMP/m_dis" DISABLE_THOUGHT_LOGGING=true)
259check "call still succeeds when logging disabled" '"isError":false' "$OUT"
260if [ -s "$TMP/err.txt" ]; then fail "logging silenced on stderr"; else pass "logging silenced on stderr"; fi
261if [ -f "$TMP/m_dis/chains.json" ]; then fail "logging disabled also skips persistence"; else pass "logging disabled also skips persistence"; fi
262OUT=$(mcp "$TMP/m_dis2" DISABLE_THOUGHT_LOGGING=1)
263if [ -s "$TMP/err.txt" ]; then fail "DISABLE_THOUGHT_LOGGING=1 silences stderr"; else pass "DISABLE_THOUGHT_LOGGING=1 silences stderr"; fi
264
265section "mode 2 --mcp: chains.json snapshot for the viewer"
266CHAINJSON="$TMP/m_ok/chains.json"
267[ -f "$CHAINJSON" ] && pass "chains.json written" || fail "chains.json missing"
268CJ=$(cat "$CHAINJSON" 2>/dev/null || echo '{}')
269jq_ "chains.json has one chain" "len(d['chains'])==1" "$CJ"
270jq_ "chain keeps 4 thoughts" "d['chains'][0]['count']==4" "$CJ"
271jq_ "chain records the branch" "d['chains'][0]['branches']==['alt-route']" "$CJ"
272jq_ "thought carries fields" "set(['thought','thought_number','total_thoughts','next_needed','at']) <= set(d['chains'][0]['thoughts'][0])" "$CJ"
273jq_ "revision flagged" "d['chains'][0]['thoughts'][1].get('is_revision') is True" "$CJ"
274jq_ "revision target recorded" "d['chains'][0]['thoughts'][1]['revises']==1" "$CJ"
275jq_ "branch flagged" "d['chains'][0]['thoughts'][2]['branch_id']=='alt-route'" "$CJ"
276jq_ "needs_more flagged" "d['chains'][0]['thoughts'][1]['needs_more'] is True" "$CJ"
277# a second process must merge, not clobber
278cat > "$TMP/in.jsonl" <<'JSONL'
279{"jsonrpc":"2.0","id":1,"method":"tools/call","params":{"name":"sequentialthinking","arguments":{"thought":"second session","nextThoughtNeeded":false,"thoughtNumber":1,"totalThoughts":1}}}
280JSONL
281mcp "$TMP/m_ok" env >/dev/null
282CJ=$(cat "$CHAINJSON")
283jq_ "second session merged" "len(d['chains'])==2" "$CJ"
284jq_ "first chain untouched" "any(c['count']==4 for c in d['chains'])" "$CJ"
285# malformed store degrades to clean, never a crash or a poisoned file
286printf 'not json {{{\n' > "$TMP/m_ok/chains.json"
287mcp "$TMP/m_ok" env >/dev/null 2>&1
288check "corrupt store recovered" '"thought"' "$(cat "$TMP/m_ok/chains.json")"
289# the file is written .tmp then rename()d
290check "persistence uses a temp file" "rename(tmp, path)" "$SRC_TXT"
291check_not "persistence never writes the store in place" 'fopen(path, "wb")' "$SRC_TXT"
292
293# ---------------------------------------------------------------- mode 3: JSON API
294section "mode 3 JSON API: authentication"
295S="$TMP/api"; mkdir -p "$S"
296cat > "$TMP/in.jsonl" <<'JSONL'
297{"jsonrpc":"2.0","id":1,"method":"tools/call","params":{"name":"sequentialthinking","arguments":{"thought":"alpha one","nextThoughtNeeded":true,"thoughtNumber":1,"totalThoughts":2}}}
298{"jsonrpc":"2.0","id":2,"method":"tools/call","params":{"name":"sequentialthinking","arguments":{"thought":"alpha two","nextThoughtNeeded":false,"isRevision":true,"revisesThought":1,"thoughtNumber":2,"totalThoughts":2}}}
299JSONL
300mcp "$S" env >/dev/null
301R=$(SEQTHINK_STATE_DIR="$S" BOT_PASSWORD=secretpw QUERY_STRING="action=chains" "$BIN"; true)
302check "401 without a credential" "Status: 401 Unauthorized" "$R"
303check "401 body says invalid" "invalid node password" "$R"
304check_not "401 leaks no chains" '"chains":[' "$R"
305R=$(SEQTHINK_STATE_DIR="$S" BOT_PASSWORD=secretpw HTTP_X_BOT_PASSWORD=secretpw QUERY_STRING="action=chains" "$BIN"; true)
306check "header credential accepted" '"chains":[' "$R"
307R=$(SEQTHINK_STATE_DIR="$S" BOT_PASSWORD=secretpw HTTP_AUTHORIZATION="Bearer secretpw" QUERY_STRING="action=stats" "$BIN"; true)
308check "bearer credential accepted" '"ok":true' "$R"
309R=$(SEQTHINK_STATE_DIR="$S" BOT_PASSWORD=secretpw QUERY_STRING="action=chains&pwd=secretpw" "$BIN"; true)
310check "query pwd credential accepted" '"chains":[' "$R"
311R=$(SEQTHINK_STATE_DIR="$S" BOT_PASSWORD=secretpw QUERY_STRING="action=chains&pwd=nope" "$BIN"; true)
312check "wrong credential still 401" "Status: 401" "$R"
313R=$(SEQTHINK_STATE_DIR="$S" QUERY_STRING="action=chains" "$BIN"; true)
314check "open node (no BOT_PASSWORD) answers" '"chains":[' "$R"
315R=$(SEQTHINK_STATE_DIR="$S" BOT_PASSWORD=secretpw "$BIN" --meta; true)
316check "--meta stays unauthenticated" '"name":"seqthink"' "$R"
317printf '%s\n' '{"jsonrpc":"2.0","id":1,"method":"ping"}' > "$TMP/ping.jsonl"
318R=$(SEQTHINK_STATE_DIR="$S" BOT_PASSWORD=secretpw "$BIN" --mcp < "$TMP/ping.jsonl" 2>/dev/null; true)
319check "--mcp stays unauthenticated (stdio)" '"jsonrpc":"2.0"' "$R"
320check_not "--mcp never emits a CGI 401" "Status: 401" "$R"
321R=$(SEQTHINK_STATE_DIR="$S" BOT_PASSWORD=secretpw "$BIN" < /dev/null; true)
322check "bare HTML GET 401 without a credential" "Status: 401 Unauthorized" "$R"
323check_not "401 HTML leaks no page" "<!DOCTYPE html>" "$R"
324R=$(SEQTHINK_STATE_DIR="$S" BOT_PASSWORD=secretpw HTTP_X_BOT_PASSWORD=secretpw "$BIN" < /dev/null; true)
325check "HTML renders with a credential" "<!DOCTYPE html>" "$R"
326
327section "mode 3 JSON API: actions"
328R=$(SEQTHINK_STATE_DIR="$S" QUERY_STRING="action=chains" "$BIN"; true)
329CB=$(body "$R")
330jq_ "chains lists one chain" "d['count']==1" "$CB"
331jq_ "chains reports the plugin" "d['plugin']=='plugin_seqthink'" "$CB"
332jq_ "chain summary has thoughts" "d['chains'][0]['thoughts']==2" "$CB"
333jq_ "chain summary has last_head" "d['chains'][0]['last_head']=='alpha two'" "$CB"
334jq_ "chain summary lists branches" "isinstance(d['chains'][0]['branches'], list)" "$CB"
335jq_ "chains never leaks an absolute state path" "'/' not in d['state_dir_name']" "$CB"
336CID=$(printf '%s' "$CB" | python3 -c "import json,sys;print(json.load(sys.stdin)['chains'][0]['id'])")
337R=$(SEQTHINK_STATE_DIR="$S" QUERY_STRING="action=chain&id=$CID" "$BIN"; true)
338DB=$(body "$R")
339jq_ "chain returns thoughts" "len(d['thoughts'])==2" "$DB"
340jq_ "chain thought text intact" "d['thoughts'][0]['thought']=='alpha one'" "$DB"
341jq_ "chain marks the revision" "d['thoughts'][1]['is_revision'] is True" "$DB"
342R=$(SEQTHINK_STATE_DIR="$S" QUERY_STRING="action=chain&id=does-not-exist" "$BIN"; true)
343check "unknown chain id -> error" 'chain not found' "$(body "$R")"
344R=$(SEQTHINK_STATE_DIR="$S" QUERY_STRING="action=chain&id=..%2F..%2Fetc%2Fpasswd" "$BIN"; true)
345check "path traversal id refused" 'invalid chain id' "$(body "$R")"
346R=$(SEQTHINK_STATE_DIR="$S" QUERY_STRING="action=chain&id=a%3Brm" "$BIN"; true)
347check "shell metacharacter id refused" 'invalid chain id' "$(body "$R")"
348R=$(SEQTHINK_STATE_DIR="$S" QUERY_STRING="action=stats" "$BIN"; true)
349SB=$(body "$R")
350jq_ "stats counts thoughts" "d['thoughts']==2" "$SB"
351jq_ "stats counts chains" "d['chains']==1" "$SB"
352jq_ "stats counts revisions" "d['revisions']==1" "$SB"
353jq_ "stats reports the cap" "d['max_chains']==50" "$SB"
354R=$(SEQTHINK_STATE_DIR="$S" QUERY_STRING="action=info" "$BIN"; true)
355IB=$(body "$R")
356jq_ "info declares kind mcp" "d['kind']=='mcp' and d['mcp'] is True" "$IB"
357jq_ "info declares menus false" "d['menus'] is False and d['daemon'] is False" "$IB"
358jq_ "info lists the tool" "d['tools']==['sequentialthinking']" "$IB"
359jq_ "info carries the schema" "'thought' in d['tool_schema']['properties'] and 'branchId' in d['tool_schema']['properties']" "$IB"
360jq_ "info schema requires 4" "sorted(d['tool_schema']['required'])==sorted(['thought','nextThoughtNeeded','thoughtNumber','totalThoughts'])" "$IB"
361R=$(SEQTHINK_STATE_DIR="$S" QUERY_STRING="action=bogus" "$BIN"; true)
362check "unknown action refused" 'unknown action' "$(body "$R")"
363R=$(SEQTHINK_STATE_DIR="$S" QUERY_STRING="format=json" "$BIN"; true)
364check "format=json answers JSON" 'Content-Type: application/json' "$R"
365R=$(SEQTHINK_STATE_DIR="$S" QUERY_STRING="format=jsonx" "$BIN"; true)
366check "format=jsonx still serves HTML" 'Content-Type: text/html' "$R"
367R=$(SEQTHINK_STATE_DIR="$S" QUERY_STRING="action=clear" "$BIN"; true)
368check "clear needs confirmation" 'confirm=1 required' "$(body "$R")"
369[ -f "$S/chains.json" ] && pass "clear refused left the store" || fail "clear refused wiped the store"
370printf '{"action":"clear","confirm":"1"}' > "$S/_body.json"
371R=$(SEQTHINK_STATE_DIR="$S" REQUEST_METHOD=POST CONTENT_LENGTH=$(wc -c < "$S/_body.json") \
372 HTTP_X_BOT_PASSWORD=sh "$BIN" < "$S/_body.json"; true)
373check "POST JSON body clear works" '"cleared":true' "$(body "$R")"
374[ -f "$S/chains.json" ] && fail "clear removed the store" || pass "clear removed the store"
375
376section "mode 3 JSON API: bounded store"
377S2="$TMP/bound"; mkdir -p "$S2"
378i=1
379while [ $i -le 55 ]; do
380 printf '{"jsonrpc":"2.0","id":1,"method":"tools/call","params":{"name":"sequentialthinking","arguments":{"thought":"chain %s","nextThoughtNeeded":false,"thoughtNumber":1,"totalThoughts":1}}}\n' "$i" \
381 > "$TMP/one.jsonl"
382 SEQTHINK_STATE_DIR="$S2" env "$BIN" --mcp < "$TMP/one.jsonl" >/dev/null 2>&1
383 i=$((i + 1))
384done
385BJ=$(cat "$S2/chains.json")
386jq_ "store never exceeds 50 chains" "len(d['chains'])<=50" "$BJ"
387jq_ "newest chain survived" "any(c['thoughts'][0]['thought']=='chain 55' for c in d['chains'])" "$BJ"
388
389# ---------------------------------------------------------------- mode 4: HTML
390section "mode 4 HTML UI"
391Q=$(SEQTHINK_STATE_DIR="$TMP/api" QUERY_STRING="embed=1" "$BIN"; true)
392check "html content type" "Content-Type: text/html; charset=utf-8" "$Q"
393check "html declares charset" "<meta charset=" "$Q"
394check "html has a viewport" "width=device-width" "$Q"
395check "html is CSP-locked" "default-src 'none'" "$Q"
396check "html sets X-Frame-Options" "X-Frame-Options: SAMEORIGIN" "$Q"
397check_not "html never sends Content-Length" "Content-Length:" "$Q"
398check_not "html has no CDN script" "<script src=" "$Q"
399check_not "html has no cdnjs" "cdnjs" "$Q"
400check_not "html has no googleapis" "googleapis" "$Q"
401check_not "html has no private IPs" "192.168." "$Q"
402check_not "html has no localhost URL" "http://127.0.0.1" "$Q"
403check "html has the bot token block" "--bg-card" "$Q"
404check "html prepaints the theme" "data-theme" "$Q"
405check "html follows the bot" "sx-theme" "$Q"
406check "html asks for the theme" "sx-theme-request" "$Q"
407check "html flags embed mode" "embedded" "$Q"
408check "html names the plugin" "Thought Chains" "$Q"
409check "html branches the API route" "/plugin_seqthink?action=" "$Q"
410check "html supports the hub proxy route" "/plugins?plugin=" "$Q"
411check "html carries the node password" "X-Bot-Password" "$Q"
412check "html calls the chains API" "api('chains')" "$Q"
413check_not "html has no emoji" "💭" "$Q"
414check_not "html has no emoji (revision)" "🔄" "$Q"
415check_not "html has no emoji (branch)" "🌿" "$Q"
416if grep -qF "💭" src/ui.html; then fail "ui source has no emoji"; else pass "ui source has no emoji"; fi
417grep -c "<script>" src/ui.html >/dev/null 2>&1 && pass "ui has its own script tag" || fail "ui script tag missing"
418grep -q "<style>" src/ui.html && pass "ui has its own style tag" || fail "ui style tag missing"
419check_not "ui has no inline hex palette outside tokens" "#123456" "$Q"
420if python3 tools/gen_ui.py --check >/dev/null 2>&1; then
421 pass "src/ui_html.h is generated from src/ui.html"
422else
423 fail "src/ui_html.h is stale (run tools/gen_ui.py)"
424fi
425
426section "source invariants"
427check "_WIN32 forces binary stdout" "_setmode(_fileno(stdout), _O_BINARY)" "$SRC_TXT"
428check "_WIN32 gate precedes includes" "_WIN32_WINNT 0x0601" "$SRC_TXT"
429check "Apple needs _DARWIN_C_SOURCE" "_DARWIN_C_SOURCE" "$SRC_TXT"
430check "meta is emitted before auth" "return run_meta();" "$SRC_TXT"
431check "mcp mode is dispatched" "return run_mcp();" "$SRC_TXT"
432check "CGI surface gated before dispatch" "require_node_auth();" "$SRC_TXT"
433check_not "no shell command execution" "popen(" "$SRC_TXT"
434check_not "no system()" "system(" "$SRC_TXT"
435check_not "no sockets" "socket(" "$SRC_TXT"
436check_not "no TLS client" "mbedtls" "$SRC_TXT"
437check_not "no outbound http helper" "WinHttp" "$SRC_TXT"
438check "chain ids are validated" "valid_chain_id" "$SRC_TXT"
439check "history cap is bounded" "MAX_THOUGHT_HISTORY" "$SRC_TXT"
440check "thought text is length-capped" "THOUGHT_MAX" "$SRC_TXT"
441check "branch id is length-capped" "BRANCHID_MAX" "$SRC_TXT"
442check "state dir honours the test override" "SEQTHINK_STATE_DIR" "$SRC_TXT"
443check "state dir prefers /workspace" "/workspace/.scorpiox/connectors/" "$SRC_TXT"
444check "state dir uses LOCALAPPDATA on win" "LOCALAPPDATA" "$SRC_TXT"
445printf '%s' "$SRC_TXT" | grep -q getline && pass "mcp loop reads line-delimited stdio" || fail "mcp loop has no line reader"
446
447section "ASan + fortify (parses files it did not write)"
448if [ ! -x ./plugin_seqthink.asan ] || [ "$SRC_TXT" -nt ./plugin_seqthink.asan ]; then
449 cc -g -O0 -std=c99 -Wall -Wextra -fsanitize=address -I src -I vendor \
450 -o plugin_seqthink.asan src/plugin_seqthink.c vendor/yyjson.c >/dev/null 2>&1 \
451 || echo " note: ASan build unavailable here"
452fi
453if [ -x ./plugin_seqthink.asan ]; then
454 cat > "$TMP/in.jsonl" <<'JSONL'
455{"jsonrpc":"2.0","id":1,"method":"tools/call","params":{"name":"sequentialthinking","arguments":{"thought":"asan thought with \"quotes\" and \\ backslash and \ntab\t","nextThoughtNeeded":true,"isRevision":true,"revisesThought":1,"thoughtNumber":1,"totalThoughts":2}}}
456{"jsonrpc":"2.0","id":2,"method":"tools/call","params":{"name":"sequentialthinking","arguments":{"thought":"unicode ünïcødé and émojis ok","nextThoughtNeeded":false,"branchFromThought":1,"branchId":"ünï","thoughtNumber":2,"totalThoughts":2}}}
457JSONL
458 AOUT=$(SEQTHINK_STATE_DIR="$TMP/asan" ./plugin_seqthink.asan --mcp < "$TMP/in.jsonl" 2>"$TMP/aerr.txt")
459 if grep -q "AddressSanitizer" "$TMP/aerr.txt"; then
460 fail "ASan reported a problem"; sed -n 1,12p "$TMP/aerr.txt"
461 else
462 pass "ASan clean on quotes/backslash/newline/unicode thoughts"
463 fi
464 check "ASan run still answered" '"isError":false' "$AOUT"
465 R=$(SEQTHINK_STATE_DIR="$TMP/asan" QUERY_STRING="action=chains" ./plugin_seqthink.asan; true)
466 if grep -q "AddressSanitizer" "$TMP/aerr.txt"; then fail "ASan clean on the CGI read path"
467 else pass "ASan clean on the CGI read path"; fi
468 printf '{{{ not json' > "$TMP/asan/chains.json"
469 SEQTHINK_STATE_DIR="$TMP/asan" QUERY_STRING="action=chains" ./plugin_seqthink.asan >/dev/null 2>>"$TMP/aerr.txt"
470 SEQTHINK_STATE_DIR="$TMP/asan" QUERY_STRING="action=chain&id=th-x" ./plugin_seqthink.asan >/dev/null 2>>"$TMP/aerr.txt"
471 printf '{"chains":[{"id":42,"thoughts":"not an array"},{"thoughts":[]}]}' > "$TMP/asan/chains.json"
472 SEQTHINK_STATE_DIR="$TMP/asan" QUERY_STRING="action=stats" ./plugin_seqthink.asan >/dev/null 2>>"$TMP/aerr.txt"
473 if grep -q "AddressSanitizer" "$TMP/aerr.txt"; then fail "ASan clean on malformed stores"
474 else pass "ASan clean on malformed stores"; fi
475else
476 echo " skip ASan checks (build unavailable)"
477fi
478
479if [ ! -x ./plugin_seqthink.fortify ]; then
480 cc -O2 -std=c99 -Wall -Wextra -D_FORTIFY_SOURCE=2 -I src -I vendor \
481 -o plugin_seqthink.fortify src/plugin_seqthink.c vendor/yyjson.c >/dev/null 2>&1 \
482 || echo " note: fortify build unavailable here"
483fi
484if [ -x ./plugin_seqthink.fortify ]; then
485 SEQTHINK_STATE_DIR="$TMP/fort" ./plugin_seqthink.fortify --mcp < "$TMP/in.jsonl" >/dev/null 2>"$TMP/ferr.txt"
486 if grep -qi "buffer overflow" "$TMP/ferr.txt"; then fail "fortify build aborted"
487 else pass "fortify build survives the full session"; fi
488else
489 echo " skip fortify checks (build unavailable)"
490fi
491
492# ---------------------------------------------------------------- audit
493section "clean-room audit (working tree + full git history)"
494audit() { # audit <label> <regex>
495 local out
496 out=$(git grep -InE -e "$2" -- . 2>/dev/null | grep -v 'tests/run.sh' | head -5 || true)
497 if [ -n "$out" ]; then
498 fail "audit: $1"; printf '%s\n' "$out" | sed 's/^/ /'
499 else
500 pass "audit: $1"
501 fi
502}
503audit "no LAN 192.168 addresses" '192\.168\.[0-9]+\.[0-9]+'
504audit "no private 10.x addresses" '\b10\.[0-9]+\.[0-9]+\.[0-9]+'
505PAT="gh""p_[A-Za-z0-9]{36}"
506audit "no PAT prefix" "$PAT"
507TOK="3e""f2c00c"
508audit "no token prefix" "$TOK"
509SFP="ssh""pass -p [A-Za-z0-9]"
510audit "no ssh password flag" "$SFP"
511HOSTS="macm""ini|intel-""ultra""2|lazar""us"
512audit "no internal hostnames" "$HOSTS"
513if ! git rev-parse HEAD >/dev/null 2>&1; then
514 echo " skip git-history audit (repository has no commits yet)"
515elif command -v scorpiox-cli-botplugin >/dev/null 2>&1; then
516 if scorpiox-cli-botplugin audit "$ROOT" >/dev/null 2>&1; then
517 pass "scorpiox-cli-botplugin audit accepts the repo"
518 else
519 fail "scorpiox-cli-botplugin audit rejected the repo"
520 scorpiox-cli-botplugin audit "$ROOT" 2>&1 | tail -10 | sed 's/^/ /'
521 fi
522else
523 echo " skip scorpiox-cli-botplugin audit (not installed here)"
524fi
525
526printf '\n%s==============================%s\n' "$C_B" "$C_0"
527printf ' %s%d passed%s %s%d failed%s\n' "$C_OK" "$PASS" "$C_0" "$([ "$FAIL" -gt 0 ] && printf '%s' "$C_BAD" || printf '%s')" "$FAIL" "$C_0"
528[ "$FAIL" -eq 0 ] || exit 1
529exit 0