#!/usr/bin/env bash
# AI-assisted; verified with the tests and tools listed above.
# Usage: seed-and-run.sh CHECKOUT SCRATCH SIDE SERVER_PORT WEB_PORT
# CHECKOUT must already be the requested base or head, with `vp i` completed.
set -euo pipefail
checkout=$1
scratch=$2
side=$3
server_port=$4
web_port=$5
cd "$checkout"
# Both compared commits have the same version. Change only server build metadata
# to exercise a real published 0.1.8 -> 0.1.9 update with the 0.1.9 web client.
python3 - <<'PY'
import json
from pathlib import Path
p=Path('apps/server/package.json')
d=json.loads(p.read_text()); d['version']='0.1.8'
p.write_text(json.dumps(d,indent=2)+'\n')
PY
(cd apps/server && node scripts/cli.ts build-exe --target linux-x64)
T3CODE_HOME="$scratch/$side/install" T3CODE_INSTALL_BIN_DIR="$scratch/$side/bin" T3CODE_VERSION=0.1.8 sh scripts/install.sh
cp apps/server/dist-exe/t3-linux-x64 "$scratch/$side/install/runtime/versions/0.1.8/t3"
T3CODE_DEV_SEED=0 node scripts/dev-runner.ts dev:web --port "$server_port" --dev-url "http://127.0.0.1:$web_port" -- --host 127.0.0.1 > "$scratch/$side-web.log" 2>&1 &
echo "Web PID: $!; read the actual web port from this log."
T3CODE_DEV_SEED=0 T3CODE_AUTO_BOOTSTRAP_PROJECT_FROM_CWD=0 T3CODE_TELEMETRY_ENABLED=false "$scratch/$side/bin/t3" serve --host 127.0.0.1 --port "$server_port" --base-dir "$scratch/$side/install" --dev-url "http://127.0.0.1:$web_port" > "$scratch/$side-server.log" 2>&1 &
echo "Server PID: $!; use its pairing link locally, never publish it."
# Browser driver (same for both sides):
# 1280x800 CSS viewport, light theme. Pair through the actual web origin.
# Uncheck history import, click Start; open /settings/access.
# Capture two identical action screenshots separated by at least 500ms.
# Record: click Copy [relaunch|update] command, show the success toast.
# Capture two identical toast screenshots separated by at least 500ms.
# Paste clipboard into a temporary offscreen textarea; record its value, remove it.
# Base clipboard: npx t3@0.1.9. Invoke `npx --yes t3@0.1.9 --version`;
# original launcher --version and readlink still report 0.1.8.
# Head clipboard: '<launcher>' update --base-dir '<installation-home>' 0.1.9.
# Execute it verbatim. Stop only the captured server PID. Restart with the
# original launcher and identical serve flags; GET /.well-known/t3/environment
# must report serverVersion 0.1.9. Inspect/redact/upload evidence, then clean up.
