#!/usr/bin/env bash
# The scene parity test on a build box (the Mac runs no headless browser: standing rule, 7 October 2026). Carries the files the
# test needs to the box under this lane's prefix, runs tools/scene/parity.mjs there and prints its report; the box's own
# Playwright build and Chromium (root installed the libraries on 7 October 2026, 14:5x UK) do the rendering.
#
# tools/scene/parity-remote.sh [--box N] [--out
] default box 2; --out fetches the frames and the report back
#
# Where it runs: a box named by ~/.config/igneum/build-server[-N] (build@); on a machine with no box file but a local
# Playwright (a CI runner that is itself a box) it runs parity.mjs in place; with neither it prints a skip line and exits 0,
# so a plain GitHub runner stays green while every box-side and Mac-side gate renders for real.
set -uo pipefail
cd "$(git rev-parse --show-toplevel)" || exit 1
BOX=2; OUT=""
while [ $# -gt 0 ]; do case "$1" in --box) BOX="$2"; shift 2 ;; --out) OUT="$2"; shift 2 ;; *) echo "unknown argument $1" >&2; exit 2 ;; esac; done
HOSTFILE="$HOME/.config/igneum/build-server"; [ "$BOX" = 1 ] || HOSTFILE="$HOSTFILE-$BOX"
if [ ! -s "$HOSTFILE" ]; then
if [ -n "${PLAYWRIGHT_CORE:-}" ] || [ -f /srv/builds/_bin/overlap/node_modules/playwright-core/package.json ]; then exec node tools/scene/parity.mjs ${OUT:+--out "$OUT"}; fi
echo "scene-parity: skipped, no build box file at $HOSTFILE and no Playwright here"; exit 0
fi
HOST="$(cat "$HOSTFILE")"; KEY="$HOME/.ssh/igneum_ed25519"
SSH=(ssh -i "$KEY" -o BatchMode=yes -o ConnectTimeout=10 "$HOST")
WT="$(basename "$(pwd)")"; DEST="/srv/builds/scene-parity/$WT"
"${SSH[@]}" "mkdir -p $DEST/site $DEST/tools/ui-mock $DEST/tools/scene $DEST/scene $DEST/app/igneum-app/ui" || { echo "scene-parity: box $HOST unreachable" >&2; exit 1; }
RS=(rsync -a --delete -e "ssh -i $KEY -o BatchMode=yes")
"${RS[@]}" --exclude img --exclude verify --exclude '*.png' --exclude '*.ico' site/ "$HOST:$DEST/site/"
"${RS[@]}" scene/ "$HOST:$DEST/scene/"
"${RS[@]}" tools/scene/ "$HOST:$DEST/tools/scene/"
"${RS[@]}" tools/ui-mock/ "$HOST:$DEST/tools/ui-mock/"
"${RS[@]}" tools/site-serve.mjs "$HOST:$DEST/tools/site-serve.mjs"
if [ -f app/igneum-app/ui/live-dag.js ]; then "${RS[@]}" --exclude '*.woff2' app/igneum-app/ui/ "$HOST:$DEST/app/igneum-app/ui/"; else "${SSH[@]}" "rm -rf $DEST/app"; fi
[ -f brand/igneum-coin-1024.png ] && "${SSH[@]}" "mkdir -p $DEST/brand" && "${RS[@]}" brand/igneum-coin-1024.png "$HOST:$DEST/brand/igneum-coin-1024.png"
# the copied-sources rule: re-stamped on the far side before anything runs (touch), so no stale mtime decides anything there
"${SSH[@]}" "cd $DEST && find . -type f -exec touch {} + && git init -q . 2>/dev/null; [ -f /srv/builds/_bin/overlap/env.sh ] && . /srv/builds/_bin/overlap/env.sh; cd $DEST && timeout 240 nice -n 10 node tools/scene/parity.mjs --out $DEST/_out"
RC=$?
if [ -n "$OUT" ]; then mkdir -p "$OUT"; rsync -a -e "ssh -i $KEY -o BatchMode=yes" "$HOST:$DEST/_out/" "$OUT/" 2>/dev/null; fi
exit $RC