#!/usr/bin/env bash
# ============================================================================
# Universal Lattice addon test runner — parse + sweep + lua tests.
#
# Works identically in the starter AND in scaffolded addons (scaffold.py
# copies this file verbatim; the layered logic auto-targets whichever
# addon-root it's run from).
#
# Layer 0 (Lua 5.1 parse):       luac-5.1 -p against every production .lua.
#                                Uses the actual Lua 5.1.4 parser WoW runs;
#                                catches goto/labels and any other 5.1-illegal
#                                syntax before sweep / tests / deploy. Skipped
#                                with a WARN if luac-5.1 isn't installed.
# Layer A (sweep clean):         run the redux-style-wow-addon sweep against
#                                the addon root. Fails on any CRITICAL hit.
# Layer B (runtime correctness): run every test_*.lua in the current dir.
#
# Layer 0 install (one-time):
#     curl -L https://www.lua.org/ftp/lua-5.1.5.tar.gz | tar xz -C /tmp
#     cd /tmp/lua-5.1.5 && make macosx
#     cp src/luac $HOME/.local/bin/luac-5.1
#     cp src/lua  $HOME/.local/bin/lua-5.1
#
# To validate the STARTER as a scaffold source (round-trip + residual literals),
# run `bash validate_starter.sh` from the starter's tests/ directory. That
# script only exists in the starter — it is not copied by scaffold.py.
#
# Run this AFTER any code change AND BEFORE any in-game deploy. Layer 0 is
# the gate that catches the goto/label class of bugs that surfaced in VDSR
# May 2026 ("'=' expected near 'continuePanel'" at in-game load).
# ============================================================================
set -e
cd "$(dirname "$0")"

ADDON_ROOT=$(cd .. && pwd)
SWEEP="$HOME/.claude/skills/redux-style-wow-addon/scripts/sweep.py"
LUAC51="$HOME/.local/bin/luac-5.1"

# ============================================================================
# Layer 0: parse every production .lua with the real Lua 5.1.4 parser
# ============================================================================
echo "=== Layer 0: Lua 5.1 parse-check ==="
if [ ! -x "$LUAC51" ]; then
    echo "WARNING: luac-5.1 not found at $LUAC51 — skipping Layer 0"
    echo "         (see install instructions in this script's header)"
else
    # Walk Core/, UI/, Modules/ — exclude tests/ (which use the test harness
    # mock + dofile path, not the production load path) and Libs/ (vendored
    # third-party code, not our responsibility).
    FILES=$(find "$ADDON_ROOT" -name "*.lua" \
        ! -path "*/tests/*" \
        ! -path "*/Libs/*" \
        ! -path "*/libs/*" 2>/dev/null)
    if [ -z "$FILES" ]; then
        echo "(no production .lua files found — skipping)"
    else
        FAIL=0
        for f in $FILES; do
            if ! "$LUAC51" -p "$f" 2>&1 ; then
                FAIL=$((FAIL + 1))
            fi
        done
        if [ $FAIL -gt 0 ]; then
            echo ""
            echo "FAIL [Layer 0]: $FAIL file(s) failed Lua 5.1 parse-check."
            echo "       These bugs WILL crash the addon at in-game load."
            exit 1
        fi
        echo "all production .lua files parse cleanly under Lua 5.1"
    fi
fi

# ============================================================================
# Layer A: sweep the addon source
# ============================================================================
echo ""
echo "=== Layer A: sweep $(basename "$ADDON_ROOT") ==="
if [ ! -f "$SWEEP" ]; then
    echo "WARNING: sweep.py not found at $SWEEP — skipping Layer A"
    echo "         install the redux-style-wow-addon skill to enable the gate"
elif ! python3 "$SWEEP" "$ADDON_ROOT" ; then
    echo ""
    echo "FAIL [Layer A]: $(basename "$ADDON_ROOT") has sweep violations."
    echo "       Fix the violations above before running tests or deploying."
    exit 1
fi

# ============================================================================
# Layer B: lua test loop
# ============================================================================
echo ""
echo "=== Layer B: lua test loop ==="
LUA=${LUA:-lua}
failed=0
for f in test_*.lua; do
    echo "--- $f ---"
    if ! $LUA "$f"; then
        failed=$((failed + 1))
    fi
done
if [ $failed -gt 0 ]; then
    echo ""
    echo "FAIL [Layer B]: $failed test files failed"
    exit 1
fi

echo ""
echo "all layers passed for $(basename "$ADDON_ROOT")"
