#!/bin/sh
# Focused contract guard for native OMP hosted-session launch.
# Run: sh tests/native-launch-manifest.sh (exit 0 = pass).
set -u
ROOT=$(CDPATH= cd "$(dirname "$0")/.." && pwd)
MANIFEST="$ROOT/adapter/omp-spt.toml"
fail=0

field_of() { awk -v h="[$1]" -v f="$2" '$0 == h {s=1;next} /^\[/{s=0} s && $0 ~ f' "$MANIFEST"; }
expect() {
  label=$1 expected=$2 actual=$3
  if [ "$actual" = "$expected" ]; then
    echo "ok   $label"
  else
    echo "FAIL $label: expected [$expected], got [$actual]"
    fail=1
  fi
}

# [unit->REQ-OMP-NATIVE-TUI] [unit->REQ-OMP-EXECUTABLE-RESOLUTION] [unit->REQ-OMP-SESSION-TITLES]
expect "[session.self] uses launch-omp with endpoint and node naming keys" \
  'command = "{adapter_dir}/omp-spt launch-omp --id {id} --node {node} --extension {adapter_dir}/strings/omp-spt.mjs"' \
  "$(field_of session.self '^[[:space:]]*command[[:space:]]*=')"
expect "[session.self] declares endpoint and node fills" \
  'keys = ["id", "node"]' \
  "$(field_of session.self '^[[:space:]]*keys[[:space:]]*=')"
expect "[session.resume] forwards endpoint, node, and native resume ids" \
  'command = "{adapter_dir}/omp-spt launch-omp --id {id} --node {node} --resume {session_id} --extension {adapter_dir}/strings/omp-spt.mjs"' \
  "$(field_of session.resume '^[[:space:]]*command[[:space:]]*=')"
expect "[session.resume] declares endpoint, node, and session fills" \
  'keys = ["id", "node", "session_id"]' \
  "$(field_of session.resume '^[[:space:]]*keys[[:space:]]*=')"
# [unit->REQ-OMP-READY-LIVE]
expect "hostable_types is ReadyAgent + LiveAgent only" \
  'hostable_types = ["LiveAgent", "ReadyAgent"]' \
  "$(field_of adapter '^[[:space:]]*hostable_types[[:space:]]*=')"
# [unit->REQ-OMP-CONTINUITY-DROPS]
expect "commune drops under .spt" \
  'commune_dir = ".spt"' \
  "$(field_of session '^[[:space:]]*commune_dir[[:space:]]*=')"
expect "signoff drops under .spt" \
  'signoff_dir = ".spt"' \
  "$(field_of session '^[[:space:]]*signoff_dir[[:space:]]*=')"

spawn_commands="$(field_of session.self '^[[:space:]]*command[[:space:]]*=')
$(field_of session.resume '^[[:space:]]*command[[:space:]]*=')"
case "$spawn_commands" in
  *'--mode rpc'*) echo "FAIL a hosted session still uses --mode rpc"; fail=1 ;;
  *) echo "ok   no hosted session uses --mode rpc" ;;
esac

[ "$fail" -eq 0 ] && { echo "NATIVE-LAUNCH-MANIFEST OK"; exit 0; }
echo "NATIVE-LAUNCH-MANIFEST FAIL"
exit 1
