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