#!/bin/bash
WT=/opt/projects/.covalba-next-wp-worktrees/tom-check-du-site
L=/tmp/claude-1001/-opt-projects--covalba-next-wp-worktrees-tom-check-du-site/2e5bec39-75a9-4f87-8bff-902ecedde81a/scratchpad
cd "$WT" || exit 1
# 1) attendre la fin du build orphan
for i in $(seq 1 120); do ps -eo args | grep -q "[n]ext build" || break; sleep 3; done
echo "[1] orphan build terminé"
# 2) build final (avec l'edit BudgetQuoteModal)
npm run build > "$L/build-final.log" 2>&1
echo "[2] build final exit=$? — $(grep -c 'Compiled successfully' "$L/build-final.log") compiled, errors=$(grep -ciE '\berror\b' "$L/build-final.log")"
# 3) restart serveur 3101 (par PID)
PID=$(ss -ltnp 2>/dev/null | grep ":3101" | grep -oE "pid=[0-9]+" | head -1 | cut -d= -f2)
[ -n "$PID" ] && { kill "$PID" 2>/dev/null; sleep 2; }
PORT=3101 HOST=0.0.0.0 setsid nohup npm start > "$L/server-final.log" 2>&1 &
curl -s -o /dev/null --retry 30 --retry-delay 1 --retry-all-errors --retry-connrefused --max-time 90 http://localhost:3101/ 2>/dev/null
echo "[3] serveur home=$(curl -s -o /dev/null -w '%{http_code}' --max-time 8 http://localhost:3101/)"
# 4) re-scan Hn + sample-page
echo "[4] sample-page=$(curl -s -o /dev/null -w '%{http_code}' --max-time 8 http://localhost:3101/sample-page) test-blocks=$(curl -s -o /dev/null -w '%{http_code}' --max-time 8 http://localhost:3101/test-blocks)"
node -e '
const urls=["/","/logistique","/distribution","/covametal","/mentions-legales","/hub/aides-obligations"];
(async()=>{for(const u of urls){try{
 const html=await (await fetch("http://localhost:3101"+u)).text();
 const lv=[...html.matchAll(/<(h[1-6])\b/gi)].map(m=>+m[1][1]);
 let jump="";let p=0;for(const l of lv){if(p&&l>p+1){jump="SAUT H"+p+"->H"+l;break;}p=l;}
 console.log((jump?"[Hn] ❌ "+jump:"[Hn] ✅ OK")+"  "+u+"  ["+lv.join("")+"]");
}catch(e){console.log("[Hn] ERR "+u+" "+e.message);}}})();
'
echo "VERIFY_DONE"
