#!/bin/bash
# stageb_bundle_alias_table_ok_vm.sh — Stage‑B: alias table via env (multiple entries)

set -euo pipefail
if [ "${SMOKES_ENABLE_STAGEB:-0}" != "1" ]; then
  echo "[SKIP] stageb_bundle_alias_table_ok_vm (SMOKES_ENABLE_STAGEB=1 to enable)"
  exit 0
fi
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
if ROOT_GIT=$(git -C "$SCRIPT_DIR" rev-parse --show-toplevel 2>/dev/null); then
  ROOT="$ROOT_GIT"
else
  ROOT="$(cd "$SCRIPT_DIR/../../../../../../../../.." && pwd)"
fi
source "$ROOT/tools/smokes/v2/lib/test_runner.sh"
source "$ROOT/tools/smokes/v2/lib/stageb_helpers.sh"
require_env || exit 2

main='static box Main { method main(args) { return 0 } }'
table='U1:static box U1 { method id(a){ return a } }|||U2:static box U2 { method id(a){ return a } }'

export HAKO_BUNDLE_ALIAS_TABLE="$table"
json=$(stageb_compile_to_json_with_require "$main" "U1,U2") || { echo "[FAIL] stageb_bundle_alias_table_ok_vm (emit failed)" >&2; exit 1; }
if [ -s "$json" ] && head -n1 "$json" | grep -q '"version":0' && head -n1 "$json" | grep -q '"kind":"Program"'; then
  rm -f "$json"; echo "[PASS] stageb_bundle_alias_table_ok_vm"; exit 0
else
  echo "[FAIL] stageb_bundle_alias_table_ok_vm (missing header)" >&2
  test -f "$json" && head -n1 "$json" >&2 || true
  rm -f "$json"; exit 1
fi
