#!/usr/bin/env bash
# HousingDecorGuide/tests/run_lua_tests.sh
set -e
cd "$(dirname "$0")"

LUA="${LUA:-lua5.1}"
if ! command -v "$LUA" > /dev/null 2>&1; then
    LUA="luajit"
fi
if ! command -v "$LUA" > /dev/null 2>&1; then
    LUA="lua5.4"
fi
if ! command -v "$LUA" > /dev/null 2>&1; then
    LUA="lua"
fi
if ! command -v "$LUA" > /dev/null 2>&1; then
    echo "ERROR: Need lua5.1, luajit, lua5.4, or lua on PATH"
    exit 2
fi

# Discover and run all test_*.lua files in this directory
failures=0
for f in test_*.lua; do
    if [[ "$f" == "test_helpers.lua" ]]; then continue; fi
    echo ""
    echo "===== $f ====="
    if ! "$LUA" "$f"; then failures=$((failures+1)); fi
done

if (( failures > 0 )); then
    echo ""
    echo "$failures test file(s) failed"
    exit 1
fi
echo ""
echo "All tests passed"
