#!/usr/bin/env bash
# misc_import_notfound — an unresolved `import` must byte-match `dzn verify`.
#
# When an imported file cannot be found in any search dir, dzn reports two
# lines and exits 1 (the search dirs are `, `-joined with a trailing `;`;
# the cwd renders as `.`), then names the importing file:
#
#   No such file: does_not_exist.dzn found in: .;
#   imported from main.dzn
#
# With `-I libdir` the dir list grows: `found in: ., libdir;`.
#
# nucleos used to print its own `Import not found: does_not_exist.dzn
# (searched: )`. This pins the dzn-identical wording, including the -I case.
set -euo pipefail

work="$NUCTEST_TMPDIR/inf"
rm -rf "$work"; mkdir -p "$work/libdir"
cd "$work"

printf 'import does_not_exist.dzn;\n' > main.dzn

# --- Case 1: no -I, cwd renders as `.` ---
out=$("$NUCLEOS" verify main.dzn 2>&1) && rc=0 || rc=$?
expected="No such file: does_not_exist.dzn found in: .;
imported from main.dzn"
if [[ "$out" != "$expected" ]]; then
  echo "FAIL: import-not-found output != dzn reference"
  echo "--- expected (dzn) ---"; printf '%s\n' "$expected"
  echo "--- got (nucleos) ---";  printf '%s\n' "$out"
  exit 1
fi
if [[ "$rc" -ne 1 ]]; then
  echo "FAIL: expected exit 1 (unresolved import), got $rc"
  exit 1
fi

# --- Case 2: extra search dir via -I shows up in the list ---
out2=$("$NUCLEOS" verify -I libdir main.dzn 2>&1) && rc2=0 || rc2=$?
expected2="No such file: does_not_exist.dzn found in: ., libdir;
imported from main.dzn"
if [[ "$out2" != "$expected2" ]]; then
  echo "FAIL: import-not-found (-I) output != dzn reference"
  echo "--- expected (dzn) ---"; printf '%s\n' "$expected2"
  echo "--- got (nucleos) ---";  printf '%s\n' "$out2"
  exit 1
fi
if [[ "$rc2" -ne 1 ]]; then
  echo "FAIL: expected exit 1 (unresolved import, -I), got $rc2"
  exit 1
fi

echo "ok: unresolved import reported byte-identical to dzn (incl. -I, exit 1)"
