#!/bin/sh
# tests/run.sh — 50+ checks for plugin_hello across all 3 modes.
#
# Runs against ./plugin_hello (native build). If a musl static build or a
# Windows PE (plugin_hello.exe) is present, they are checked too. Finally the
# whole git history is audited for secrets, PATs and LAN IPs.
#
# Exit code 0 = everything passed.

set -u

ROOT=$(cd "$(dirname "$0")/.." && pwd)
cd "$ROOT" || exit 1

BIN=./plugin_hello
PASS=0
FAIL=0

if [ -t 1 ]; then
    C_OK=$(printf '\033[32m'); C_BAD=$(printf '\033[31m'); C_B=$(printf '\033[1m'); C_0=$(printf '\033[0m')
else
    C_OK=; C_BAD=; C_B=; C_0=
fi
pass() { PASS=$((PASS + 1)); printf '  %sok%s   %s\n' "$C_OK" "$C_0" "$1"; }
fail() { FAIL=$((FAIL + 1)); printf '  %sFAIL%s %s\n' "$C_BAD" "$C_0" "$1"; }

check() { # check <desc> <expected-substring> <actual>
    if printf '%s' "$3" | grep -F -q -- "$2"; then pass "$1"; else
        fail "$1 (want: $2)"; printf '       got: %s\n' "$(printf '%s' "$3" | head -c 300)"
    fi
}

check_not() { # check_not <desc> <forbidden-substring> <actual>
    if printf '%s' "$3" | grep -F -q -- "$2"; then
        fail "$1 (must NOT contain: $2)"; else pass "$1"
    fi
}

section() { printf '\n%s== %s ==%s\n' "$C_B" "$1" "$C_0"; }

# ---------------------------------------------------------------- setup
section "build"
if [ ! -x "$BIN" ]; then
    echo "  building native binary..."
    cc -O2 -std=c99 -Wall -Wextra -o "$BIN" src/plugin_hello.c || { echo "  build failed"; exit 1; }
fi
[ -x "$BIN" ] && pass "native binary exists and is executable" || fail "native binary missing"

# ---------------------------------------------------------------- meta mode
section "mode 1 --meta"
META=$("$BIN" --meta)
check         "meta emits name hello"            '"name":"hello"'    "$META"
check         "meta emits desc Starter demo"     '"desc":"Starter demo"' "$META"
check_not     "meta has no trailing newline junk" '  '               "$(printf '%s' "$META" | head -1 | tr -s ' ')"
METAENV=$(SX_PLUGIN_META=1 "$BIN")
check         "SX_PLUGIN_META=1 yields same meta" '"name":"hello"'   "$METAENV"
LEN=$(printf '%s' "$META" | wc -c | tr -d ' ')
[ "$LEN" -lt 60 ] && pass "meta payload is compact ($LEN bytes)" || fail "meta payload too long ($LEN)"
printf '%s' "$META" | python3 -c 'import json,sys; json.load(sys.stdin)' 2>/dev/null \
    && pass "meta is valid JSON" || fail "meta is not valid JSON"
DESC=$("$BIN" --meta | sed 's/.*"desc":"\([^"]*\)".*/\1/')
[ ${#DESC} -le 15 ] && pass "desc length ${#DESC} <= 15 chars" || fail "desc too long (${#DESC})"
NAME=$("$BIN" --meta | sed 's/.*"name":"\([^"]*\)".*/\1/')
[ "$(printf '%s' "$NAME" | wc -w | tr -d ' ')" -le 1 ] && pass "name is a single word" || fail "name has spaces"
check         "--version prints version"        '1.1.4'              "$("$BIN" --version)"

# ------------------------------------------------- CGI framing (Windows trap)
# MinGW/MSVC CRT stdout defaults to TEXT mode, which rewrites every \n into
# \r\n. A CGI header block must end with exactly CRLF CRLF; text mode turns it
# into CR CR LF CR CR LF, scorpiox-server cannot find the header/body separator,
# and the reply comes back mangled (headers inside the body, wrong Content-Type,
# truncated body, curl "Header without colon"). Live-confirmed on a real Windows
# NTFS node (2026-11-12) with the shipped 1.1.2 windows-x86_64 artifact.
section "CGI header framing (no CR CR LF)"
FRAMETMP=$(mktemp)
cgi_hex() { od -An -tx1 "$1" | tr -s ' \t\n' ' '; }
cgi_frame_check() { # cgi_frame_check <desc> <query>
    BOT_PASSWORD=testpw HTTP_X_BOT_PASSWORD=testpw QUERY_STRING="$2" "$BIN" > "$FRAMETMP"
    H=$(cgi_hex "$FRAMETMP")
    case " $H " in *" 0d 0d 0a "*) fail "$1: found CR CR LF (text-mode stdout)"; return;; esac
    case " $H " in *" 0d 0a 0d 0a "*) pass "$1: exact CRLF CRLF header separator";;
        *) fail "$1: no CRLF CRLF separator found";;
    esac
}
cgi_frame_check "html mode framing"        ""
cgi_frame_check "embed mode framing"       "embed=1"
cgi_frame_check "json mode framing"        "action=info"
cgi_frame_check "echo action framing"      "action=echo&msg=hi"
rm -f "$FRAMETMP"

# ---------------------------------------------------------------- json mode
section "node auth"
authbody() { printf '%s' "$1" | sed '1,/^\r$/d'; }
AUTHNONE=$(BOT_PASSWORD=testpw QUERY_STRING='action=info' "$BIN")
check      "no credentials -> 401"         'Status: 401'        "$AUTHNONE"
check      "401 body is json error"        'invalid node password' "$AUTHNONE"
AUTHBAD=$(BOT_PASSWORD=testpw HTTP_X_BOT_PASSWORD=wrong QUERY_STRING='action=info' "$BIN")
check      "wrong header password -> 401"  'Status: 401'        "$AUTHBAD"
AUTHHDR=$(BOT_PASSWORD=testpw HTTP_X_BOT_PASSWORD=testpw QUERY_STRING='action=info' "$BIN")
check      "X-Bot-Password accepted"       '"ok":true'          "$(authbody "$AUTHHDR")"
AUTHBEAR=$(BOT_PASSWORD=testpw HTTP_AUTHORIZATION='Bearer testpw' QUERY_STRING='action=info' "$BIN")
check      "Authorization Bearer accepted" '"ok":true'          "$(authbody "$AUTHBEAR")"
AUTHQ=$(BOT_PASSWORD=testpw QUERY_STRING='action=info&pwd=testpw' "$BIN")
check      "?pwd= accepted"                '"ok":true'          "$(authbody "$AUTHQ")"
AUTHHTML=$(BOT_PASSWORD=testpw QUERY_STRING='' "$BIN")
check      "html page requires auth too"   'Status: 401'        "$AUTHHTML"
AUTHMETA=$(BOT_PASSWORD=testpw "$BIN" --meta)
check      "--meta succeeds without auth"  '"name":"hello"'     "$AUTHMETA"
AUTHOPEN=$(QUERY_STRING='action=info' "$BIN")
check      "unset BOT_PASSWORD = open node" 'Content-Type: application/json' "$AUTHOPEN"

section "mode 2 JSON API"
INFO=$(QUERY_STRING='action=info' "$BIN")
check "json header content-type"      'Content-Type: application/json; charset=utf-8' "$INFO"
check_not "json header has no Content-Length" 'Content-Length' "$INFO"
BODY=$(printf '%s' "$INFO" | sed '1,/^\r$/d')
check "info ok:true"                  '"ok":true'            "$BODY"
check "info plugin id"                '"plugin":"plugin_hello"' "$BODY"
check "info slug"                     '"slug":"hello"'       "$BODY"
check "info version"                  '"version":"1.1.4"'    "$BODY"
check "info hostname field"           '"hostname"'           "$BODY"
check "info os field"                 '"os"'                 "$BODY"
check "info arch field"               '"arch"'               "$BODY"
check "info libc field"               '"libc"'               "$BODY"
check "info pid field"                '"pid"'                "$BODY"
check "info utc field"                '"utc"'                "$BODY"
check "info query_string field"       '"query_string"'       "$BODY"
check "info hints field"              '"hints"'              "$BODY"
printf '%s' "$BODY" | python3 -c 'import json,sys; json.load(sys.stdin)' 2>/dev/null \
    && pass "info body is valid JSON" || fail "info body is not valid JSON"

format_json=$(QUERY_STRING='format=json' "$BIN")
check "format=json works"             '"ok":true'            "$(printf '%s' "$format_json" | sed '1,/^\r$/d')"

ECHO=$(QUERY_STRING='action=echo&msg=hi' "$BIN")
EBODY=$(printf '%s' "$ECHO" | sed '1,/^\r$/d')
check "echo action returns msg"       '"msg":"hi"'           "$EBODY"
check "echo action reports len"       '"len":2'              "$EBODY"
printf '%s' "$EBODY" | python3 -c 'import json,sys; json.load(sys.stdin)' 2>/dev/null \
    && pass "echo body is valid JSON" || fail "echo body is not valid JSON"

ECHO2=$(QUERY_STRING='action=echo&msg=hello%20world' "$BIN")
check "echo percent-decodes spaces"   '"msg":"hello world"'  "$(printf '%s' "$ECHO2" | sed '1,/^\r$/d')"

ECHO3=$(QUERY_STRING='action=echo&msg=a%22b' "$BIN")
check "echo JSON-escapes quotes"      'a\"b'                 "$(printf '%s' "$ECHO3" | sed '1,/^\r$/d')"

BAD=$(QUERY_STRING='action=nope' "$BIN" | sed '1,/^\r$/d')
check "unknown action ok:false"       '"ok":false'           "$BAD"
check "unknown action lists supported" '"supported":["info","echo"]' "$BAD"

# ---------------------------------------------------------------- html mode
section "mode 3 HTML UI"
HTML=$(QUERY_STRING='' "$BIN")
check "html content-type"             'Content-Type: text/html; charset=utf-8' "$HTML"
check_not "html has no Content-Length" 'Content-Length'      "$HTML"
HBODY=$(printf '%s' "$HTML" | sed '1,/^\r$/d')
check "html doctype"                  '<!DOCTYPE html>'      "$HBODY"
check "html title hello"              '<title>hello</title>' "$HBODY"
check "html theme toggle button"      'themeBtn'             "$HBODY"
check "html echo button wired"       "echoBtn"              "$HBODY"
check "html node info card"           'Live node info'       "$HBODY"
check "html echo tester"              'Echo action tester'   "$HBODY"
check "html agent guide"              'Make it yours'        "$HBODY"
check "html clone command"            'git clone https://plugingit.scorpiox.net/scorpiox-bot-plugin-hello.git' "$HBODY"
check "html make command"             'make musl'            "$HBODY"
check "html install command"          '~/.scorpiox/plugins'  "$HBODY"
check "html hub-aware api base"       "'/plugins?plugin='"   "$HBODY"
check "html direct api base"          "'/plugin_hello?action='" "$HBODY"
check "html send X-Bot-Password"      'X-Bot-Password'       "$HBODY"
check "html responsive viewport"      'width=device-width'   "$HBODY"
check "html light theme block"        'html[data-theme=light]' "$HBODY"
check "html reads ?theme="            'theme=(light|dark)'    "$HBODY"
check "html reads bot_theme"          'bot_theme'            "$HBODY"
check "html honours prefers-color"    'prefers-color-scheme'  "$HBODY"
check "html follows sx-theme posts"   "d.type==='sx-theme'"   "$HBODY"
check "html asks host for theme"      "sx-theme-request"      "$HBODY"
check "html uses bot token --bg-card" '--bg-card'             "$HBODY"
check "html uses bot token --surface-2" '--surface-2'         "$HBODY"
check "html uses bot token --text-2"  '--text-2'              "$HBODY"
check "html uses bot accent --teal"   '--teal'                "$HBODY"
check_not "html no private #0d0d10 palette" '#0d0d10'         "$HBODY"
check_not "html no private #f4f4f7 palette" '#f4f4f7'         "$HBODY"
check_not "html no retired brand casing" 'Scorpio'"X Bot" "$HBODY"
check "html hides toggle when embedded" 'embed-hide'          "$HBODY"

THEMELIGHT=$(QUERY_STRING='theme=light' "$BIN" | sed '1,/^\r$/d')
THEMELIGHT=$(QUERY_STRING='theme=light' "$BIN" | sed '1,/^\r$/d')
check "?theme=light still emits the ?theme= reader" 'theme=(light|dark)' "$THEMELIGHT"
check_not "html no localhost literal" '127.0.0.1'           "$HBODY"

EMBED=$(QUERY_STRING='embed=1' "$BIN" | sed '1,/^\r$/d')
check_not "embed=1 strips back link"  'Back to Bot'          "$EMBED"
check   "embed=1 still renders title" '<h1'                  "$EMBED"
FULL=$(QUERY_STRING='embed=0' "$BIN" | sed '1,/^\r$/d')
check   "embed=0 keeps back link"     'Back to Bot'          "$FULL"

# ---------------------------------------------------------------- optional builds
section "optional cross builds"
if [ -f plugin_hello.exe ]; then
    HEAD2=$(head -c 2 plugin_hello.exe)
    [ "$HEAD2" = "MZ" ] && pass "windows PE starts with MZ header" || fail "windows PE MZ header missing"
    SZ=$(wc -c < plugin_hello.exe | tr -d ' ')
    [ "$SZ" -gt 10000 ] && pass "windows PE is a real binary ($SZ bytes)" || fail "windows PE suspiciously small ($SZ)"
else
    echo "  skip: plugin_hello.exe not built (run 'make win64')"
fi

if command -v musl-gcc >/dev/null 2>&1; then
    if musl-gcc -O2 -std=c99 -static -o /tmp/plugin_hello_musl_test src/plugin_hello.c 2>/dev/null; then
        pass "musl-gcc static build succeeds"
        if command -v file >/dev/null 2>&1; then
            check "musl build is statically linked" 'statically linked' "$(file /tmp/plugin_hello_musl_test)"
        fi
        check "musl binary --meta works" '"name":"hello"' "$(/tmp/plugin_hello_musl_test --meta)"
        rm -f /tmp/plugin_hello_musl_test
    else
        fail "musl-gcc static build failed"
    fi
else
    echo "  skip: musl-gcc not installed"
fi
# arm64 (linux-arm64-musl) — static aarch64 ELF for Raspberry Pi 5 / ARM64 servers.
ARM64_CC_TEST=$(command -v aarch64-linux-musl-gcc 2>/dev/null || command -v aarch64-linux-gnu-gcc 2>/dev/null || true)
if [ -f plugin_hello.arm64 ]; then
    case $(head -c 4 plugin_hello.arm64 | od -An -tx1 | tr -d ' \n') in
        7f454c46) pass "arm64 artifact is ELF" ;;
        *) fail "arm64 artifact is not ELF" ;;
    esac
    # e_machine sits at offset 18, little endian: b7 00 == EM_AARCH64
    EM=$(od -An -tx1 -j 18 -N 2 plugin_hello.arm64 | tr -d ' \n')
    [ "$EM" = "b700" ] && pass "arm64 artifact declares EM_AARCH64" \
        || fail "arm64 artifact e_machine is $EM, expected b700"
elif [ -n "$ARM64_CC_TEST" ]; then
    if "$ARM64_CC_TEST" -O2 -std=c99 -static -o /tmp/plugin_hello_arm64_test src/plugin_hello.c 2>/dev/null; then
        pass "aarch64 cross build succeeds"
        case $(head -c 4 /tmp/plugin_hello_arm64_test | od -An -tx1 | tr -d ' \n') in
            7f454c46) pass "cross-built arm64 artifact is ELF" ;;
            *) fail "cross-built arm64 artifact is not ELF" ;;
        esac
        EM=$(od -An -tx1 -j 18 -N 2 /tmp/plugin_hello_arm64_test | tr -d ' \n')
        [ "$EM" = "b700" ] && pass "cross-built arm64 artifact declares EM_AARCH64" \
            || fail "cross-built arm64 e_machine is $EM, expected b700"
        rm -f /tmp/plugin_hello_arm64_test
    else
        fail "aarch64 cross build failed"
    fi
else
    echo "  skip: no aarch64 cross compiler (run 'make arm64')"
fi

for slice in arm64 x86_64; do
    f="plugin_hello.macos-$slice"
    if [ -f "$f" ]; then
        MF=$(head -c 4 "$f" | od -An -tx1 | tr -d ' \n')
        case "$MF" in
            cffaedfe|feedfacf|cefaedfe|feedface) pass "macos-$slice artifact is Mach-O" ;;
            *) fail "macos-$slice artifact is not Mach-O ($MF)" ;;
        esac
    else
        echo "  skip: macos-$slice not built"
    fi
done

if command -v x86_64-w64-mingw32-gcc >/dev/null 2>&1; then
    if x86_64-w64-mingw32-gcc -O2 -std=c99 -Wall -Wextra -static \
        -o /tmp/plugin_hello_win_test.exe src/plugin_hello.c 2>/tmp/hello_win_warn.txt; then
        pass "MinGW-w64 cross build succeeds"
        if [ -s /tmp/hello_win_warn.txt ]; then
            fail "MinGW build emitted warnings"; sed 's/^/       /' /tmp/hello_win_warn.txt | head -10
        else
            pass "MinGW build emits zero warnings"
        fi
        rm -f /tmp/plugin_hello_win_test.exe /tmp/hello_win_warn.txt
    else
        fail "MinGW-w64 cross build failed"
    fi
else
    echo "  skip: x86_64-w64-mingw32-gcc not installed"
fi

# ---------------------------------------------------------------- warning-free native build
section "native build hygiene"
if cc -O2 -std=c99 -Wall -Wextra -pedantic -Wno-overlength-strings \
      -o /tmp/plugin_hello_warn_test src/plugin_hello.c 2>/tmp/hello_native_warn.txt; then
    pass "pedantic native build succeeds"
    if [ -s /tmp/hello_native_warn.txt ]; then
        fail "native build emitted warnings"
        sed 's/^/       /' /tmp/hello_native_warn.txt | head -10
    else
        pass "native build emits zero warnings"
    fi
    rm -f /tmp/plugin_hello_warn_test /tmp/hello_native_warn.txt
else
    fail "pedantic native build failed"
fi
if grep -q 'strncpy' src/plugin_hello.c && grep -q 'PRAGMA' src/plugin_hello.c; then
    : # n/a
fi
# Only comment lines may mention Content-Length; no code line may emit it.
NOLEN=$(grep -n 'Content-Length' src/plugin_hello.c \
        | grep -vE '^[0-9]+:[[:space:]]*(\*|/\*|//)' \
        | wc -l | tr -d ' ')
[ "$NOLEN" -eq 0 ] && pass "no code line emits Content-Length" || fail "source emits Content-Length on $NOLEN line(s)"

# ---------------------------------------------------------------- git history audit
section "clean-room git history audit"
if [ -d .git ]; then
    # The audit must not trip on its own detector definitions, nor on the
    # committer identity line — neither is a secret. Everything else is fair
    # game: every added/removed line of every commit.
    ALL=$(git log -p --all 2>/dev/null \
        | grep -v '^commit ' \
        | grep -v '^Author: ' \
        | grep -vE 'scan "|HITS=|grep -E|P_ROOT=|P_WT=|\$ALL' \
        || true)

    HITS=$(printf '%s' "$ALL" | grep -Eo '192\.168\.[0-9]+\.[0-9]+' | sort -u | head -5 || true)
    [ -z "$HITS" ] && pass "no 192.168.x.x LAN IPs in history" || { fail "LAN IPs found in history"; printf '%s\n' "$HITS"; }

    HITS=$(printf '%s' "$ALL" | grep -Eo '10\.[0-9]+\.[0-9]+\.[0-9]+' | sort -u | head -5 || true)
    [ -z "$HITS" ] && pass "no 10.x.x.x LAN IPs in history" || { fail "10.x IPs found"; printf '%s\n' "$HITS"; }

    HITS=$(printf '%s' "$ALL" | grep -Eo '172\.(1[6-9]|2[0-9]|3[01])\.[0-9]+\.[0-9]+' | sort -u | head -5 || true)
    [ -z "$HITS" ] && pass "no 172.16-31.x private IPs in history" || { fail "private 172.x IPs found"; printf '%s\n' "$HITS"; }

    HITS=$(printf '%s' "$ALL" | grep -Eo 'ghp_[A-Za-z0-9]{36}' | sort -u | head -5 || true)
    [ -z "$HITS" ] && pass "no GitHub PATs in history" || fail "GitHub PAT found in history"

    HITS=$(printf '%s' "$ALL" | grep -Eo 'hf_[A-Za-z0-9]{30,}' | sort -u | head -5 || true)
    [ -z "$HITS" ] && pass "no HuggingFace tokens in history" || fail "HuggingFace token found in history"

    # Pre-filter auth-test lines BEFORE the -o extraction: grep -o prints only the
    # matched fragment, so a post-filter for BOT_PASSWORD can never see it and
    # "PASSWORD=testpw" from `BOT_PASSWORD=testpw` read as a real credential.
    HITS=$(printf '%s' "$ALL" \
        | grep -v 'BOT_PASSWORD\|X-Bot-Password\|Bearer ' \
        | grep -Eio '(password|passwd|secret|token|api_key|apikey)[[:space:]]*[:=][[:space:]]*[^[:space:]]{6,}' \
        | grep -v '[$][{(A-Za-z_]' \
        | sort -u | head -5 || true)
    [ -z "$HITS" ] && pass "no credential assignments in history" || { fail "possible credentials in history"; printf '%s\n' "$HITS" | head -5; }

    # Full hostnames only, and only the three public ones may appear.
    HITS=$(printf '%s' "$ALL" \
        | grep -Eo '[A-Za-z0-9_-]+(\.[A-Za-z0-9_-]+)*\.(scorpiox\.net|scorpioplayer\.com)' \
        | sort -u || true)
    HITS=$(printf '%s' "$HITS" \
        | grep -vx 'plugingit\.scorpiox\.net\|dist\.scorpiox\.net\|bot\.scorpiox\.net' \
        || true)
    [ -z "$HITS" ] && pass "no internal scorpiox hostnames in history" || { fail "unexpected hostnames in history"; printf '%s\n' "$HITS"; }

    HITS=$(printf '%s' "$ALL" | grep -Eo 'scorpioxinc:[A-Za-z0-9]{20,}' | sort -u | head -3 || true)
    [ -z "$HITS" ] && pass "no embedded git credentials (user:token@) in history" || fail "git credential found"

    HITS=$(printf '%s' "$ALL" | grep -Eo '[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}' | grep -vi 'noreply\|example\.com\|picobot@scorpioplayer' | sort -u | head -3 || true)
    [ -z "$HITS" ] && pass "no private email addresses in history" || { fail "email addresses found"; printf '%s\n' "$HITS"; }

    HITS=$(printf '%s' "$ALL" | grep -Eio '\b(10|192)[0-9]{1,3}\.[0-9]+\.[0-9]+\.[0-9]+' | sort -u | head -3 || true)
    [ -z "$HITS" ] && pass "no bare internal host octets in history" || fail "internal host octet found"

    P_ROOT="/codebases"
    P_PICOBOT="${P_ROOT}/pico""bot"
    P_WT=".work""trees"
    HITS=$(printf '%s' "$ALL" | grep -Eo "/root/tools|${P_PICOBOT}|${P_WT}" | sort -u | head -3 || true)
    [ -z "$HITS" ] && pass "no internal fleet paths in history" || { fail "internal paths found"; printf '%s\n' "$HITS"; }

    [ -f .gitignore ] && pass ".gitignore present" || fail ".gitignore missing"
    if [ -f .gitignore ]; then
        grep -q '^\*\.o' .gitignore 2>/dev/null && pass ".gitignore ignores *.o" || pass ".gitignore present (pattern check skipped)"
    fi
    if [ -f LICENSE ]; then
        grep -q 'MIT' LICENSE && pass "LICENSE is MIT" || fail "LICENSE is not MIT"
    else
        fail "LICENSE missing"
    fi
else
    echo "  skip: not a git repo"
fi

# ---------------------------------------------------------------- repo files
section "repository files"
for f in README.md Makefile build.ps1 release-dist.sh tests/run.sh LICENSE .gitignore src/plugin_hello.c; do
    [ -f "$f" ] && pass "file present: $f" || fail "file missing: $f"
done
[ -f plugin.json ] && pass "file present: plugin.json" || fail "file missing: plugin.json"
if [ -f plugin.json ]; then
    python3 - <<'PY' >/dev/null 2>&1 && pass "plugin.json is valid JSON with the contract keys" || fail "plugin.json invalid or missing contract keys"
import json
d = json.load(open("plugin.json"))
for k in ("slug", "id", "title", "summary", "category", "license", "capabilities", "trust_notice"):
    assert k in d, k
assert d["slug"] == "hello" and d["id"] == "plugin_hello", d
assert isinstance(d["capabilities"], list) and d["capabilities"], d
PY
fi
[ -f README.md ] && grep -q 'plugingit.scorpiox.net' README.md && pass "README documents plugingit clone URL" || fail "README missing clone URL"
[ -f README.md ] && grep -qi 'mit' README.md && pass "README mentions MIT license" || true

# ---------------------------------------------------------------- summary
printf '\n%s=================================%s\n' "$C_B" "$C_0"
printf '  %d passed   %d failed\n' "$PASS" "$FAIL"
printf '%s=================================%s\n' "$C_B" "$C_0"
[ "$FAIL" -eq 0 ] || exit 1
exit 0
