#!/bin/bash
TGT=AdomLapper; fail=0
ab(){ adom-bridge-cli --target $TGT "$@" 2>&1; }
j(){ python3 -c "import sys,json;d=json.load(sys.stdin);print(d.get('$1'))" 2>/dev/null; }
sess(){ ab pup_list_windows '{}' --ai-thread rmn 2>/dev/null | python3 -c "
import sys,json
for s in json.load(sys.stdin).get('sessions') or []:
    if s.get('sessionId')=='$1': print(s.get('profile'),s.get('tabCount') or len(s.get('tabs') or []))"; }
ok(){ [ "$2" = "$3" ] && echo "  PASS $1 ($2)" || { echo "  FAIL $1 (got '$2' want '$3')"; fail=$((fail+1)); }; }
for r in 1 2; do
  echo "── round $r ──"
  ab pup_open_window '{"sessionId":"mn","url":"https://wiki.adom.inc/adom/pup-bridge","foreground":false}' --ai-thread "menu-ralph" >/dev/null 2>&1
  sleep 7
  ab pup_open_tab '{"sessionId":"mn","url":"https://wiki.adom.inc/adom/definitions"}' --ai-thread "menu-ralph" >/dev/null 2>&1
  sleep 4
  T2="https://wiki.adom.inc/adom/definitions"
  # 1. window info (popup path, resolved from tab 2)
  ok "info" "$(ab pup_window_info "{\"_tabUrl\":\"$T2\"}" --ai-thread "user-toolbar" | j success)" "True"
  # 2. annotate from tab 2 (popup path) -> overlay present -> esc
  ab pup_annotate "{\"_tabUrl\":\"$T2\"}" --ai-thread "user-toolbar" >/dev/null 2>&1; sleep 2
  ov=$(ab pup_eval '{"sessionId":"mn","tabId":"tab-2","expr":"(() => { const w = document.getElementById(\"__pup_annotate\"); if (w) { window.dispatchEvent(new KeyboardEvent(\"keydown\",{key:\"Escape\"})); return \"yes\"; } return \"no\"; })()"}' --ai-thread "menu-ralph" | j result)
  ok "annotate-overlay" "$ov" "yes"
  # 3. snip hotkey path (worker: _tabUrl + tool snip) -> overlay -> esc
  ab pup_annotate "{\"_tabUrl\":\"$T2\",\"tool\":\"snip\"}" --ai-thread "user-toolbar" >/dev/null 2>&1; sleep 2
  sv=$(ab pup_eval '{"sessionId":"mn","tabId":"tab-2","expr":"(() => { const w = document.getElementById(\"__pup_annotate\"); if (w) { window.dispatchEvent(new KeyboardEvent(\"keydown\",{key:\"Escape\"})); return \"yes\"; } return \"no\"; })()"}' --ai-thread "menu-ralph" | j result)
  ok "snip-overlay" "$sv" "yes"
  # 4. view toggle round trip from tab 2 (the wonky one)
  ab pup_wiki_set_view "{\"_tabUrl\":\"$T2\",\"view\":\"public\"}" --ai-thread "user-taskbar-menu" >/dev/null 2>&1; sleep 12
  ok "to-public" "$(sess mn)" "iso-mn-pub 2"
  ab pup_wiki_set_view '{"sessionId":"mn","view":"authed"}' --ai-thread "user-taskbar-menu" >/dev/null 2>&1; sleep 12
  ok "to-authed" "$(sess mn)" "adom-you 2"
  # 5. dashboard
  ab pup_dashboard '{}' --ai-thread "user-toolbar" >/dev/null 2>&1; sleep 6
  dc=$(ab pup_list_windows '{}' --ai-thread rmn | python3 -c "import sys,json;print(any(s.get('sessionId')=='pup-dashboard' for s in json.load(sys.stdin).get('sessions') or []))")
  ok "dashboard" "$dc" "True"
  ab pup_close_window '{"sessionId":"pup-dashboard","takeover":true}' --ai-thread "menu-ralph" >/dev/null 2>&1
  # 6. close via tab-2 resolution (user caller)
  ab pup_close_window "{\"_tabUrl\":\"$T2\"}" --ai-thread "user-taskbar-menu" >/dev/null 2>&1; sleep 4
  gone=$(ab pup_list_windows '{}' --ai-thread rmn | python3 -c "import sys,json;print(not any(s.get('sessionId')=='mn' for s in json.load(sys.stdin).get('sessions') or []))")
  ok "close" "$gone" "True"
done
echo "===================="; echo "MENU RALPH DONE | FAILURES: $fail"
