#!/usr/bin/env bash
# misc_list_models — `nucleos parse --list-models` must match
# `dzn parse --list-models`: one `<name> <type>` line per model DECLARED in
# the file (imports are not resolved), in source order. `<type>` is one of
# interface / foreign / component / system; namespaced models use their
# dotted name. Models that appear only inside comments must be ignored.
set -euo pipefail

model="$MODEL_DIR/list_models.dzn"

expected="i interface
foreign_c foreign
leaf component
sys system
ns.inner interface"

out=$("$NUCLEOS" parse --list-models "$model" 2>/dev/null)
if [[ "$out" != "$expected" ]]; then
  echo "FAIL: parse --list-models output != dzn reference"
  echo "--- expected (dzn) ---"; printf '%s\n' "$expected"
  echo "--- got (nucleos) ---";  printf '%s\n' "$out"
  exit 1
fi

# The short flag `-l` must behave identically.
out_short=$("$NUCLEOS" parse -l "$model" 2>/dev/null)
if [[ "$out_short" != "$expected" ]]; then
  echo "FAIL: -l differs from --list-models"
  echo "--- got (-l) ---"; printf '%s\n' "$out_short"
  exit 1
fi

# Models that appear only inside comments must never be listed.
for decoy in fake_line fake_block fake_iface; do
  if grep -q "$decoy" <<<"$out"; then
    echo "FAIL: comment decoy '$decoy' leaked into list-models output"
    exit 1
  fi
done

echo "ok: parse --list-models matches dzn (interface/foreign/component/system + namespaced; comments ignored)"
