#!/usr/bin/env bash
set -e
cd "$(dirname "$0")"

# ---- Lua test files --------------------------------------------------------
fail=0
for t in test_*.lua; do
    echo "=== $t ==="
    if ! lua "$t"; then
        fail=$((fail+1))
    fi
done

# ---- Lattice drift sweep (skill Tier-1) ------------------------------------
# sweep.py is maintained in the redux-style-wow-addon skill (source of truth --
# not copied into the repo so it never goes stale). Gate the suite on CRITICAL
# hits only (silent-failure modes: selector-read drift, async API in a selector).
# IMPORTANT/INFO are reported but NON-BLOCKING: the addon carries a known
# IMPORTANT backlog -- run `python3 <sweep> ..` directly to triage it.
sweep_crit=0
SWEEP="$HOME/.claude/skills/redux-style-wow-addon/scripts/sweep.py"
if [ -f "$SWEEP" ]; then
    echo ""
    echo "=== sweep.py (Lattice drift) ==="
    set +e
    sweep_out="$(python3 "$SWEEP" .. 2>&1)"
    sweep_rc=$?
    set -e
    # Severity summary only -- the full hit list lives behind a direct run.
    echo "$sweep_out" | grep -E "=== (CRITICAL|IMPORTANT|INFO)" || true
    if [ "$sweep_rc" -eq 1 ]; then
        sweep_crit=1
        echo "!! sweep: CRITICAL hits present. Review: python3 \"$SWEEP\" .."
        echo "$sweep_out" | awk '/=== CRITICAL/{p=1} /=== IMPORTANT/{p=0} p'
    elif [ "$sweep_rc" -eq 2 ]; then
        echo "   sweep: IMPORTANT/INFO present (non-blocking backlog). Review: python3 \"$SWEEP\" .."
    fi
else
    echo ""
    echo "(sweep.py not found at $SWEEP -- skipping Lattice drift sweep)"
fi

# ---- Semantic drift tier (data-flow: aliased-state cascades) ---------------
# Materializes the guaranteed state shape by RUNNING the addon's factories, then
# parses every source file with luacheck's real Lua AST and walks it with exact
# lexical scoping, flagging `local X = state.<seeded-path>; X and X.field` dead
# guards -- the class regex CAN'T reach (needs the assignment + the factory
# shape). The AST covers multi-line cascades, dotted-prefix aliases
# (HDG.Store.state.X), exact scope, and string/comment safety that the old
# line-based POC missed. Repo-local (scripts/semantic_sweep.lua; runs from root;
# needs luacheck -- exit 3 = skip if absent). Semantic-sweep hits are CRITICAL
# (data-flow drift hides silent staleness) -- they gate the suite, never a soft
# backlog. Boosted to CRITICAL severity 2026-05-31.
sem_fail=0
echo ""
echo "=== semantic_sweep (data-flow tier -- CRITICAL) ==="
set +e
( cd .. && lua scripts/semantic_sweep.lua )
sem_rc=$?
set -e
if [ "$sem_rc" -eq 1 ]; then
    sem_fail=1; echo "!! semantic_sweep: CRITICAL drift present (above) -- strict-read them, do NOT defer."
elif [ "$sem_rc" -eq 3 ]; then
    echo "   semantic_sweep: SKIPPED (luacheck not installed -- 'luarocks install luacheck')."
fi

# ---- WoW-API type-safety tier (wowlua-ls) ----------------------------------
# TSM's Rust WoW-Lua LS (built at ../../wowlua-ls). Knows the real 12.0.5 API
# surface -> catches wrong-arity / removed-API / struct-field bugs the regex
# (sweep.py) + data-flow (semantic_sweep) tiers structurally cannot. The current
# WLL_BASELINE warnings are ALL triaged stub/inference false-positives as of
# 2026-06-06 (custom frame fields, variadic pcall, return-arity under-counts,
# flavor-mask quirks, mixin Event tables, boundary-guarded enum reads, control-
# flow-guaranteed non-nil). So we gate on a COUNT BASELINE: fail only when the
# count RISES (a new warning = a candidate real bug to triage against the wow-api
# MCP). Soft-skip if the binary isn't built.
WLL_BASELINE=44
wll_fail=0
echo ""
echo "=== wowlua-ls (WoW-API type tier -- count baseline $WLL_BASELINE) ==="
if ( cd .. && [ -x ../wowlua-ls/target/release/wowlua_ls ] ); then
    set +e
    wll_out="$( cd .. && ../wowlua-ls/target/release/wowlua_ls check . 2>&1 )"
    set -e
    wll_n="$(printf '%s\n' "$wll_out" | grep -cE 'warning\[')"
    echo "   wowlua-ls: $wll_n warnings (baseline $WLL_BASELINE)"
    if [ "$wll_n" -gt "$WLL_BASELINE" ]; then
        wll_fail=1
        echo "!! wowlua-ls: $wll_n > baseline $WLL_BASELINE -- NEW diagnostic(s); triage vs the wow-api MCP:"
        printf '%s\n' "$wll_out" | grep -E 'warning\[' | head -60
    elif [ "$wll_n" -lt "$WLL_BASELINE" ]; then
        echo "   wowlua-ls: below baseline -- lower WLL_BASELINE to $wll_n in run_all.sh."
    fi
else
    echo "   wowlua-ls: SKIPPED (binary not built at wowlua-ls/target/release/wowlua_ls)."
fi

# ---- Verdict ---------------------------------------------------------------
if [ "$fail" -gt 0 ] || [ "$sweep_crit" -gt 0 ] || [ "$sem_fail" -gt 0 ] || [ "$wll_fail" -gt 0 ]; then
    [ "$fail" -gt 0 ]       && echo "$fail test files failed"
    [ "$sweep_crit" -gt 0 ] && echo "sweep: CRITICAL hits present"
    [ "$sem_fail" -gt 0 ]   && echo "semantic_sweep: CRITICAL drift present"
    [ "$wll_fail" -gt 0 ]   && echo "wowlua-ls: warning count rose above baseline"
    exit 1
fi

echo "all test files passed + sweep clean + semantic tier clean + wowlua-ls at/below baseline"
