#!/bin/bash
# qa-diagrams.sh — every ```mermaid block in docs/ must parse in a real renderer.
# Usage: tests/qa-diagrams.sh   (needs docker + minlag/mermaid-cli + /usr/bin/chromium)
set -euo pipefail
QA=$(mktemp -d)
awk '/^```mermaid/{n++; f=QA"/d"n".mmd"; next} /^```/{f=""; next} f!=""{print > f}' QA="$QA" docs/*.md
echo '{"args":["--no-sandbox","--disable-setuid-sandbox","--disable-gpu"]}' > "$QA/pptr.json"
fail=0
docker run --rm -v "$QA":/qa --entrypoint sh minlag/mermaid-cli:latest -c '
for f in /qa/d*.mmd; do
  PUPPETEER_EXECUTABLE_PATH=/usr/bin/chromium mmdc -p /qa/pptr.json -i "$f" -o /tmp/out.svg >/dev/null 2>&1 || { echo "PARSE-FAIL: $f"; exit 1; }
  echo "PARSE-OK: $f"
done' || fail=1
rm -rf "$QA"
exit $fail
