#!/usr/bin/env bash
set -euo pipefail
# Run the app separately with T3CODE_DEV_SEED=0 vp run dev.
checkout=$1
origin=$2
workspace=$3
run_dir=$4
driver=$5
mkdir -p "$run_dir"
git clone --depth 1 --filter=blob:none --sparse https://github.com/juliusmarminge/t3code.git "$workspace"
git -C "$workspace" remote add upstream https://github.com/pingdotgg/t3code.git
git -C "$workspace" fetch --depth 1 upstream main
git -C "$workspace" remote set-head upstream main
cd "$checkout"
node apps/server/src/bin.ts auth session issue --base-dir .t3 --token-only > "$run_dir/auth"
project_id=$(node apps/server/src/bin.ts project add "$workspace" --title 'Fork status timing' --base-dir .t3 | awk '/Added project/{print $3}')
cd apps/server
PROJECT_ID="$project_id" node --input-type=module - "$origin" "$run_dir/auth" "$workspace" seed "$run_dir/seed.json" < "$driver" | tee "$run_dir/seed.json"
thread_id=$(node -e 'console.log(JSON.parse(require("fs").readFileSync(process.argv[1],"utf8")).threadId)' "$run_dir/seed.json")
cd "$checkout"
node apps/server/src/bin.ts thread send "$thread_id" "Check the repository status." --base-dir .t3 --wait --json > "$run_dir/turn.json"
cd apps/server
# Open the printed thread in the paired web app. Run the same replay turn on both sides.
# Hover Thread details > Git actions when its options button is disabled on pristine main.
# For another measurement, wait >15 seconds, then:
# node --input-type=module - "$origin" "$run_dir/auth" "$workspace" refresh "$run_dir/sample.json" < "$driver"
