#!/usr/bin/env bash
# misc_list_models_parse_error — LENIENT `parse --list-models` on a file that
# the strict parser rejects (a behavior body with UNBALANCED BRACES) and that
# also contains comment decoys.
#
# Like `dzn parse --list-models` (fall-back parser), nucleos must:
#   * still list every real model and exit 0,
#   * infer `broken`'s kind (component) despite its malformed body,
#   * IGNORE models that appear only inside `//` or `/* */` comments,
#   * not let stray braces inside comments corrupt brace-depth tracking.
set -euo pipefail

model="$MODEL_DIR/braces.dzn"

expected="i interface
good component
broken component"

out=$("$NUCLEOS" parse --list-models "$model" 2>/dev/null) && rc=0 || rc=$?

if [[ "$rc" -ne 0 ]]; then
  echo "FAIL: lenient list-models must exit 0 on a brace-mismatch file, got rc=$rc"
  echo "--- stdout ---"; printf '%s\n' "$out"
  exit 1
fi
if [[ "$out" != "$expected" ]]; then
  echo "FAIL: list-models output != dzn reference"
  echo "--- expected (dzn) ---"; printf '%s\n' "$expected"
  echo "--- got (nucleos) ---";  printf '%s\n' "$out"
  exit 1
fi
# Decoys that appear ONLY inside comments must never be listed.
for decoy in fake_line fake_trailing fake_block fake_iface; do
  if grep -q "$decoy" <<<"$out"; then
    echo "FAIL: comment decoy '$decoy' leaked into list-models output"
    printf '%s\n' "$out"
    exit 1
  fi
done

echo "ok: lenient list-models matches dzn on brace-mismatch + comments (decoys excluded, exit 0)"
