#!/bin/bash
# KAR-824 V10 DOM-Regressionstests (Spec §7 A–F) — läuft gegen den gstack-Browser.
# Nutzung: bash tests/dom-tests.sh  (aus dem Workdir)
set -u
B="$HOME/.claude/skills/gstack/browse/dist/browse"
W="$(cd "$(dirname "$0")/.." && pwd)"
PASS=0; FAIL=0
ck(){ local name="$1" got="$2" want="$3"
  if [ "$got" = "$want" ]; then PASS=$((PASS+1)); else FAIL=$((FAIL+1)); echo "  ✗ $name: ist='$got' soll='$want'"; fi }

load_pair(){ $B goto "file://$W/SupplierPulse_QAF_Compare_V11.html" >/dev/null
  $B upload "#slotB input" "$1" >/dev/null; $B upload "#slotR input" "$2" >/dev/null
  $B wait --load >/dev/null; sleep "$3"; }

SECLIST_JS='[...document.querySelectorAll("#tab-meta section")].filter(s=>!s.classList.contains("hidden")&&s.id!=="sec-onetime").map(s=>s.id).join(",")'
CANON="sec-master,sec-form,sec1,sec2,sec3,sec4,sec5,sec6,sec7,sec8,sec-review,sec-plaus,sec-ai"

echo "=== A. DOM-Test G60 (v9_1-Referenz) ==="
load_pair "$W/input/100_G60_DP_QAF_Basis_24_10_BMW.xlsm" "$W/input/20260508_BMW_QAF_G60_DP_RePricing_HO.xlsm" 15
ck "Sektionsliste kanonisch" "$($B js "$SECLIST_JS")" "$CANON"
ck "Tabs vorhanden" "$($B js 'document.querySelectorAll(".tab").length')" "2"
ck "Stammdaten sichtbar" "$($B js '!document.querySelector("#sec-master").classList.contains("hidden")&&document.querySelectorAll("#master tbody tr").length>5')" "true"
ck "Hochrechnung sichtbar+interaktiv" "$($B js 'document.querySelectorAll("#vol-table input.pj-vol").length>0&&document.querySelectorAll("#vol-table input.pj-inc").length>0')" "true"
ck "Preistreiber nicht leer" "$($B js 'window.__app.getVM().movers.rows.length>0')" "true"
G60_SECS=$($B js "$SECLIST_JS")

echo "=== B. DOM-Test Summary (v9_2-Referenz) ==="
$B js 'document.querySelector("#reload").click();"r"' >/dev/null
load_pair "$W/input/FT_links_Vergabe2023_Brose_Anlage_16_QAF_B_Schloss_FT_links_12_Mio.xlsx" "$W/input/FT_links_aktuell_Brose_QAF_B-Schloss_FT_links_ZV_20260324.xlsx" 6
SUM_SECS=$($B js "$SECLIST_JS")
ck "gleiche Sektionsliste wie G60" "$SUM_SECS" "$G60_SECS"
ck "Tab 2 vorhanden+befüllt" "$($B js 'document.querySelector("[data-tab=proc]").click();document.querySelectorAll("#proc-cmp-body table").length>=2')" "true"
ck "Stammdaten sichtbar" "$($B js 'document.querySelector("[data-tab=meta]").click();document.querySelectorAll("#master tbody tr").length>5')" "true"
ck "Brücke: End-Beschriftung (€+Δ)" "$($B js 'JSON.stringify((window.__app.getVM().bridge.end.delta!=null)&&(window.__app.getVM().bridge.endComposition.length>2))')" "true"
ck "Vergleichstabelle sichtbar" "$($B js 'document.querySelectorAll("#bridge-table tbody tr").length>8')" "true"

echo "=== C. Text-Test: 'alle 4 QAFs' verboten ==="
ck "kein 'alle 4 QAFs' im UI" "$($B js 'document.body.textContent.includes("alle 4 QAFs")||document.body.textContent.includes("über alle 4 QAFs")')" "false"

echo "=== D. Tabellen-Test ==="
ck "alle sichtbaren Tabellen haben Header" "$($B js '[...document.querySelectorAll("#app table")].filter(t=>t.offsetParent!==null&&t.querySelectorAll("tbody tr").length>0).every(t=>{const h=t.querySelector("thead tr, tbody tr:first-child th");return h&&h.textContent.trim().length>0;})')" "true"
ck "Total-Zeilen fett (tot-Klasse)" "$($B js 'document.querySelectorAll("#formtable tr.tot").length')" "2"

echo "=== E. Farb-Test: Bucket-Farbe per Key ==="
ck "Farben key-gebunden" "$($B js 'const vm=window.__app.getVM();JSON.stringify(vm.buckets.colors.Material==="#3d6f80"&&vm.buckets.colors.Profit==="#3C6A3C")')" "true"

echo "=== F. Export-Test: JSON == HTML-Werte ==="
ck "VM-QP == KPI-Anzeige" "$($B js 'const vm=window.__app.getVM();const shown=document.querySelector("#kpis .kpi .sub").textContent;JSON.stringify(shown.includes(vm.kpis.qp.a.toFixed(2).replace(".",",")))')" "true"
ck "Prozess-Mapping im VM/Export" "$($B js 'JSON.stringify(window.__app.getVM().processes.available===true)')" "true"

echo "=== G. Mix-Fall: Banner statt Extra-Seite ==="
$B js 'document.querySelector("#reload").click();"r"' >/dev/null
load_pair "$W/input/100_G60_DP_QAF_Basis_24_10_BMW.xlsm" "$W/input/Vergabe_Kiekert_20230214_QAF_H2_eO_11Mio_FTL_ZV_fin.xlsx" 10
ck "Banner sichtbar" "$($B js '!document.querySelector("#mixbanner").classList.contains("hidden")')" "true"
ck "Ergebnis-Sektionen versteckt" "$($B js 'document.querySelector("#sec1").classList.contains("hidden")')" "true"
ck "Stammdaten trotzdem da" "$($B js 'document.querySelectorAll("#master tbody tr").length>5')" "true"

echo "=== H. Keine Console-Errors ==="
ERR=$($B console --errors 2>/dev/null | grep -c "ERROR\|error:" || true)
ck "0 JS-Errors" "$ERR" "0"

echo "============================================"
echo "$PASS PASS, $FAIL FAIL"
exit $([ $FAIL -eq 0 ] && echo 0 || echo 1)
