scorpiox-bot-plugin-tetris-3d
←run.shbash
317 lines · 14 KBHEADBlameDownload
1#!/bin/sh
2# tests/run.sh - offline assertion suite for plugin_tetris_3d.
3#
4# Everything here runs against a throwaway score store, so no real
5# ~/.scorpiox/plugins is touched and no network is involved. The gameplay
6# assertions drive the HTML UI's own script through node's "npm-free" check when
7# a browser is available (tests/e2e.py), and the C side is asserted directly.
8#
9# Usage: sh tests/run.sh [path-to-binary]
10# Exit: 0 when every assertion passes.
11
12set -u
13
14ROOT=$(cd "$(dirname "$0")/.." && pwd)
15BIN=${1:-"$ROOT/plugin_tetris_3d"}
16[ -x "$BIN" ] || { echo "binary not built: $BIN" >&2; exit 1; }
17
18TMP=$(mktemp -d "${TMPDIR:-/tmp}/sx_tetris3d_XXXXXX")
19trap 'rm -rf "$TMP"' EXIT
20
21export SX_TETRIS_SCORES="$TMP/scores.json"
22unset BOT_PASSWORD
23
24PASS=0
25FAIL=0
26ok() { PASS=$((PASS+1)); }
27bad() { FAIL=$((FAIL+1)); printf ' FAIL %s\n' "$1"; }
28check() { # check <label> <actual> <expected>
29 if [ "$2" = "$3" ]; then ok; else bad "$1 (got '$2', want '$3')"; fi
30}
31has() { # has <label> <file> <pattern>
32 if grep -qF -- "$3" "$2"; then ok; else bad "$1 (missing: $3)"; fi
33}
34hasE() { # hasE <label> <file> <regex>
35 if grep -qE -- "$3" "$2"; then ok; else bad "$1 (missing regex: $3)"; fi
36}
37not_has() {
38 if grep -qF -- "$3" "$2"; then bad "$1 (found: $3)"; else ok; fi
39}
40
41# run <query> [body] -> stdout only (headers stripped)
42run() {
43 _q="$1"; _b="${2-}"
44 if [ -n "$_b" ]; then
45 printf '%s' "$_b" | CONTENT_LENGTH=$(printf '%s' "$_b" | wc -c | tr -d ' ') \
46 REQUEST_METHOD=POST QUERY_STRING="$_q" "$BIN"
47 else
48 REQUEST_METHOD=GET QUERY_STRING="$_q" "$BIN"
49 fi
50}
51json() { # json <query> -> last line (JSON body)
52 run "$1" | tail -1
53}
54jget() { # jget <json-string> <python-expression-on-d>
55 python3 -c 'import json,sys; d=json.loads(sys.argv[1]); print(eval(sys.argv[2]))' "$1" "$2" 2>/dev/null
56}
57
58echo "== metadata"
59M=$(REQ=1 "$BIN" --meta)
60check "meta name" "$(jget "$M" 'd["name"]')" "tetris"
61check "meta desc" "$(jget "$M" 'd["desc"]')" "3D block game"
62check "meta desc length <= 13" "$(python3 -c 'print(len("3D block game"))')" "13"
63M2=$(SX_PLUGIN_META=1 "$BIN")
64check "SX_PLUGIN_META equals --meta" "$M2" "$M"
65check "meta has exactly two keys" "$(jget "$M" 'sorted(d.keys())')" "['desc', 'name']"
66check "--version" "$("$BIN" --version)" "plugin_tetris_3d 1.0.1"
67check "--meta needs no auth" "$(BOT_PASSWORD=secret "$BIN" --meta)" "$M"
68
69echo "== score API"
70rm -f "$SX_TETRIS_SCORES"
71J=$(json 'action=scores')
72check "empty store ok" "$(jget "$J" 'd["ok"]')" "True"
73check "empty store highscore" "$(jget "$J" 'd["highscore"]')" "0"
74check "empty store list" "$(jget "$J" 'd["scores"]')" "[]"
75check "plugin id" "$(jget "$J" 'd["plugin"]')" "plugin_tetris_3d"
76check "version" "$(jget "$J" 'd["version"]')" "1.0.1"
77
78J=$(json 'action=save_score&val=1200')
79check "save 1200 ok" "$(jget "$J" 'd["ok"]')" "True"
80check "save 1200 high" "$(jget "$J" 'd["highscore"]')" "1200"
81J=$(json 'action=save_score&val=300')
82check "lower score kept" "$(jget "$J" 'd["highscore"]')" "1200"
83check "lower score listed" "$(jget "$J" 'd["scores"]')" "[1200, 300]"
84J=$(json 'action=save_score&val=9900')
85check "higher score wins" "$(jget "$J" 'd["highscore"]')" "9900"
86check "descending order" "$(jget "$J" 'd["scores"]')" "[9900, 1200, 300]"
87J=$(json 'action=save_score&val=600')
88check "sorted insert" "$(jget "$J" 'd["scores"]')" "[9900, 1200, 600, 300]"
89J=$(json 'action=save_score&val=0')
90check "zero rejected" "$(jget "$J" 'd["ok"]')" "False"
91check "zero error" "$(jget "$J" 'd["error"]')" "invalid_score"
92J=$(json 'action=save_score&val=-5')
93check "negative rejected" "$(jget "$J" 'd["ok"]')" "False"
94J=$(json 'action=nonsense')
95check "unknown action ok" "$(jget "$J" 'd["ok"]')" "False"
96check "unknown action err" "$(jget "$J" 'd["error"]')" "unknown_action"
97J=$(json 'format=json')
98check "format=json works" "$(jget "$J" 'd["ok"]')" "True"
99J=$(json 'action=highscore')
100check "highscore alias" "$(jget "$J" 'd["highscore"]')" "9900"
101J=$(json 'action=leaderboard')
102check "leaderboard alias" "$(jget "$J" 'd["highscore"]')" "9900"
103J=$(json 'action=save_score&val=%31%32%33%34')
104check "percent-decoded val" "$(jget "$J" 'd["scores"][0]')" "9900"
105check "percent-decoded insert" "$(jget "$J" 'd["scores"]')" "[9900, 1234, 1200, 600, 300]"
106
107echo "== POST body"
108J=$(run 'action=save_score' 'val=17777' | tail -1)
109check "POST val param" "$(jget "$J" 'd["highscore"]')" "17777"
110J=$(run 'action=save_score' 'score=18888' | tail -1)
111check "POST score param" "$(jget "$J" 'd["highscore"]')" "18888"
112J=$(run 'action=save_score' '19999' | tail -1)
113check "POST bare numeric body" "$(jget "$J" 'd["highscore"]')" "19999"
114J=$(run '' 'val=24321' | tail -1)
115check "POST with no action saves" "$(jget "$J" 'd["highscore"]')" "24321"
116
117echo "== store file"
118[ -f "$SX_TETRIS_SCORES" ] && ok || bad "score file was created"
119has "file is json" "$SX_TETRIS_SCORES" '"scores"'
120check "file has 10 entries cap" "$(python3 -c '
121import json,sys
122print(len(json.load(open(sys.argv[1]))["scores"])<=10)' "$SX_TETRIS_SCORES")" "True"
123
124echo "== store cap"
125rm -f "$SX_TETRIS_SCORES"
126i=1
127while [ $i -le 14 ]; do
128 json "action=save_score&val=$((i * 100))" > /dev/null
129 i=$((i + 1))
130done
131J=$(json 'action=scores')
132check "cap keeps 10" "$(jget "$J" 'len(d["scores"])')" "10"
133check "cap keeps best" "$(jget "$J" 'd["scores"][0]')" "1400"
134check "cap drops worst" "$(jget "$J" '400 in d["scores"]')" "False"
135check "cap keeps eleventh" "$(jget "$J" '500 in d["scores"]')" "True"
136
137echo "== writability fallback"
138SX_TETRIS_SCORES=/proc/definitely/not/writable.json run 'action=save_score&val=123' | tail -1 > "$TMP/unw.json"
139check "unwritable store reports error" "$(jget "$(cat "$TMP/unw.json")" 'd["ok"]')" "False"
140check "unwritable store error name" "$(jget "$(cat "$TMP/unw.json")" 'd["error"]')" "score_store_unwritable"
141
142echo "== node auth"
143hdr() { head -1 | tr -d '\r'; }
144A=$(QUERY_STRING='action=scores' BOT_PASSWORD=testpw "$BIN" | hdr)
145check "401 without credentials" "$A" "Status: 401 Unauthorized"
146A=$(QUERY_STRING='action=scores&pwd=testpw' BOT_PASSWORD=testpw "$BIN" | tail -1)
147check "pwd query accepted" "$(jget "$A" 'd["ok"]')" "True"
148A=$(QUERY_STRING='action=scores' BOT_PASSWORD=testpw HTTP_X_BOT_PASSWORD=testpw "$BIN" | tail -1)
149check "X-Bot-Password accepted" "$(jget "$A" 'd["ok"]')" "True"
150A=$(QUERY_STRING='action=scores' BOT_PASSWORD=testpw HTTP_AUTHORIZATION='Bearer testpw' "$BIN" | tail -1)
151check "Bearer accepted" "$(jget "$A" 'd["ok"]')" "True"
152A=$(QUERY_STRING='action=scores' BOT_PASSWORD=testpw HTTP_AUTHORIZATION='Bearer wrong' "$BIN" | hdr)
153check "wrong Bearer rejected" "$A" "Status: 401 Unauthorized"
154A=$(QUERY_STRING='' BOT_PASSWORD=testpw "$BIN" | hdr)
155check "html is gated too" "$A" "Status: 401 Unauthorized"
156A=$(QUERY_STRING='' BOT_PASSWORD=testpw HTTP_X_BOT_PASSWORD=testpw "$BIN" | hdr)
157check "html served with password" "$A" "Content-Type: text/html; charset=utf-8"
158
159echo "== html"
160run 'embed=1' > "$TMP/html.txt"
161head -1 "$TMP/html.txt" | grep -q 'Content-Type: text/html' && ok || bad "html content type"
162has "html doctype" "$TMP/html.txt" '<!DOCTYPE html>'
163has "html lang" "$TMP/html.txt" '<html lang="en">'
164has "viewport meta" "$TMP/html.txt" 'width=device-width'
165has "canvas element" "$TMP/html.txt" '<canvas id="cv">'
166has "next preview canvas" "$TMP/html.txt" 'id="cvNext"'
167has "hold canvas" "$TMP/html.txt" 'id="cvHold"'
168has "score element" "$TMP/html.txt" 'id="vScore"'
169has "best element" "$TMP/html.txt" 'id="vBest"'
170has "lines element" "$TMP/html.txt" 'id="vLines"'
171has "overlay" "$TMP/html.txt" 'id="overlay"'
172has "theme toggle" "$TMP/html.txt" 'id="btnTheme"'
173has "dark media query" "$TMP/html.txt" 'prefers-color-scheme'
174has "explicit dark theme" "$TMP/html.txt" 'data-theme="dark"'
175has "explicit light theme" "$TMP/html.txt" 'data-theme="light"'
176has "theme reads ?theme=" "$TMP/html.txt" 'theme=(light|dark)'
177has "theme listens sx-theme" "$TMP/html.txt" "d.type==='sx-theme'"
178has "theme asks the host" "$TMP/html.txt" "sx-theme-request"
179has "bot token --bg-card" "$TMP/html.txt" '--bg-card'
180has "bot token --surface-2" "$TMP/html.txt" '--surface-2'
181had_toggle_embed=$(grep -c 'html.embedded #btnTheme' "$TMP/html.txt")
182check "toggle hidden when embedded ($had_toggle_embed)" "$had_toggle_embed" "1"
183not_has "no private palette 08080b" "$TMP/html.txt" '08080b'
184not_has "no private palette eceef3" "$TMP/html.txt" 'eceef3'
185not_has "no retired brand casing" "$TMP/html.txt" 'Scorpio'"X Bot"
186has "touch controls pad" "$TMP/html.txt" 'id="pad"'
187has "left button" "$TMP/html.txt" 'id="bLeft"'
188has "rotate button" "$TMP/html.txt" 'id="bRot"'
189has "hard drop button" "$TMP/html.txt" 'id="bDrop"'
190has "hold button" "$TMP/html.txt" 'id="bHold"'
191has "pause button" "$TMP/html.txt" 'id="bPause"'
192has "ghost piece calc" "$TMP/html.txt" 'function ghostY'
193has "hold queue logic" "$TMP/html.txt" 'function hold'
194has "next queue refill" "$TMP/html.txt" 'function refill'
195has "3d projector" "$TMP/html.txt" 'function project'
196has "cage rendering" "$TMP/html.txt" 'function drawCage'
197has "seven tetrominoes" "$TMP/html.txt" "k:'I'"
198has "Z piece" "$TMP/html.txt" "k:'Z'"
199has "keyboard handler" "$TMP/html.txt" "addEventListener('keydown'"
200has "pointer handler" "$TMP/html.txt" "addEventListener('pointerup'"
201has "requestAnimationFrame" "$TMP/html.txt" 'requestAnimationFrame(loop)'
202has "level speed curve" "$TMP/html.txt" 'dropInterval = Math.max(90'
203has "line clear scoring" "$TMP/html.txt" 'var table = [0, 100, 300, 500, 800]'
204has "mobile breakpoint" "$TMP/html.txt" '@media (max-width:420px)'
205has "short viewport rule" "$TMP/html.txt" '@media (max-height:560px)'
206has "hub proxy route" "$TMP/html.txt" "/plugins?plugin="
207has "direct plugin route" "$TMP/html.txt" "/plugin_tetris_3d?action="
208has "pwd bootstrap" "$TMP/html.txt" 'bot_active_pwd'
209has "pwd header" "$TMP/html.txt" 'X-Bot-Password'
210not_has "no Content-Length" "$TMP/html.txt" 'Content-Length'
211not_has "no external script" "$TMP/html.txt" '<script src='
212not_has "no CDN" "$TMP/html.txt" 'cdnjs'
213not_has "no three.js" "$TMP/html.txt" 'three.min.js'
214not_has "no emoji speaker" "$TMP/html.txt" '&#x1F50A;'
215check "html is substantial" "$(python3 -c 'import sys,os; print(os.path.getsize(sys.argv[1])>20000)' "$TMP/html.txt")" "True"
216
217echo "== emoji / control-char audit on source and UI"
218python3 - "$ROOT" <<'PY'
219import os, sys, unicodedata
220root = sys.argv[1]
221bad = []
222for base, dirs, files in os.walk(root):
223 dirs[:] = [d for d in dirs if d not in ('.git', '__pycache__', 'bin')]
224 for f in files:
225 if not f.endswith(('.c', '.h', '.sh', '.md', '.ps1', '.json', '.py')):
226 continue
227 p = os.path.join(base, f)
228 try:
229 t = open(p, encoding='utf-8').read()
230 except Exception:
231 continue
232 for ch in t:
233 o = ord(ch)
234 if o > 0x2100 and unicodedata.category(ch).startswith('S'):
235 bad.append((p, hex(o)))
236print('EMOJI_HITS=%d' % len(bad))
237for p, o in bad[:5]:
238 print(' %s %s' % (p, o))
239PY
240check "zero emoji in sources" "$(python3 - "$ROOT" <<'PY'
241import os, sys, unicodedata
242root = sys.argv[1]
243n = 0
244for base, dirs, files in os.walk(root):
245 dirs[:] = [d for d in dirs if d not in ('.git', '__pycache__', 'bin')]
246 for f in files:
247 if not f.endswith(('.c', '.h', '.sh', '.md', '.ps1', '.json', '.py')):
248 continue
249 try:
250 t = open(os.path.join(base, f), encoding='utf-8').read()
251 except Exception:
252 continue
253 n += sum(1 for ch in t if ord(ch) > 0x2100 and unicodedata.category(ch).startswith('S'))
254print(n)
255PY
256)" "0"
257
258echo "== secret & private-address audit (full git history)"
259SCAN="$TMP/history.txt"
260git -C "$ROOT" log -p --all 2>/dev/null \
261 | grep -v '^Author: ' \
262 | grep -vE "scan \"|audit \"|HITS=|grep -E|grep -qE|hits '" > "$SCAN" || : > "$SCAN"
263hits() { grep -Eo "$1" "$SCAN" 2>/dev/null | sort -u | wc -l | tr -d ' '; }
264check "no GitHub token" "$(hits 'ghp_[A-Za-z0-9]{20,}')" "0"
265check "no HuggingFace token" "$(hits 'hf_[A-Za-z0-9]{20,}')" "0"
266check "no scorpiox-git token" "$(hits '3ef2c00cb68e')" "0"
267check "no password literal" "$(hits 'Xboxone'09'0'9'')" "0"
268check "no runner pass" "$(hits 'RUNNER_'"'"'PASS=')" "0"
269check "no private 192.168 net" "$(hits '192\.168\.[0-9]{1,3}\.')" "0"
270check "no private 10.0 net" "$(hits '(^|[^0-9])10\.0\.[0-9]{1,3}\.')" "0"
271check "no private 172.16 net" "$(hits '172\.(1[6-9]|2[0-9]|3[01])\.[0-9]{1,3}\.')" "0"
272check "no internal hostnames" "$(hits 'INTERNAL_HOST_A|INTERNAL_HOST_B|INTERNAL_HOST_C')" "0"
273
274echo "== worktree hygiene"
275not_has "no Content-Length in C source" "$ROOT/src/plugin_tetris_3d.c" 'Content-Length'
276has "stdin guarded by CONTENT_LENGTH" "$ROOT/src/plugin_tetris_3d.c" 'CONTENT_LENGTH'
277has "require_node_auth present" "$ROOT/src/plugin_tetris_3d.c" 'require_node_auth'
278has "three credential forms: query" "$ROOT/src/plugin_tetris_3d.c" 'get_query_val(qs, "pwd"'
279has "three credential forms: header" "$ROOT/src/plugin_tetris_3d.c" 'HTTP_X_BOT_PASSWORD'
280has "three credential forms: bearer" "$ROOT/src/plugin_tetris_3d.c" 'Bearer '
281has "win32 branch present" "$ROOT/src/plugin_tetris_3d.c" 'defined(_WIN32)'
282has "darwin branch present" "$ROOT/src/plugin_tetris_3d.c" '__APPLE__'
283has "license file is MIT" "$ROOT/LICENSE" 'MIT License'
284
285echo "== cross-build artifacts"
286if [ -f "$ROOT/plugin_tetris_3d.exe" ]; then
287 check "windows artifact is PE" "$(head -c 2 "$ROOT/plugin_tetris_3d.exe")" "MZ"
288else
289 echo " skip windows PE check (not built here)"
290fi
291if [ -f "$ROOT/plugin_tetris_3d.musl" ]; then
292 head -c 4 "$ROOT/plugin_tetris_3d.musl" | od -An -c | grep -q '177 E L F' && ok || bad "musl artifact is ELF"
293 if grep -qi 'GNU/Linux' "$ROOT/plugin_tetris_3d.musl" 2>/dev/null; then
294 bad "musl artifact still references glibc"
295 else
296 ok
297 fi
298else
299 echo " skip musl ELF check (not built here)"
300fi
301if [ -f "$ROOT/plugin_tetris_3d.arm64" ]; then
302 file "$ROOT/plugin_tetris_3d.arm64" 2>/dev/null | grep -q 'ARM aarch64' && ok || bad "arm64 artifact is aarch64"
303else
304 echo " skip arm64 check (not built here)"
305fi
306
307echo "== browser viewport & console (optional)"
308if [ -x "$ROOT/plugin_tetris_3d" ] && python3 -c 'import playwright' >/dev/null 2>&1; then
309 python3 "$ROOT/tests/e2e.py" && ok || bad "playwright e2e"
310else
311 echo " skip playwright (not installed)"
312fi
313
314echo
315printf 'passed %d, failed %d\n' "$PASS" "$FAIL"
316[ "$FAIL" -eq 0 ] || exit 1
317exit 0