#!/usr/bin/env bash# Run the thirteen-database phone guard against a synthetic serving household.# COTTON_HEADLESS must name a current host binary; no installed app is used.# COTTON_SIM optionally selects a lane-owned simulator.set -euo pipefailROOT="$(cd "$(dirname "$0")/.." && pwd)"# A walk's builds go ahead of other local runs waiting on the build lock# (`with-build-lock.sh`, "A WALK GOES BEFORE OTHER LOCAL RUNS").. "$ROOT/scripts/lib/caller-class.sh"rank_as_walkcd "$ROOT"export COTTON_FLAVOR=dev: "${COTTON_HEADLESS:?Set COTTON_HEADLESS to a current cotton-headless host binary}"FIXTURE="$ROOT/.local/recopy-sim"if [ -e "$FIXTURE" ]; then echo "recopy-sim: $FIXTURE exists; remove this lane's previous synthetic fixture before rerunning" >&2exit2fimkdir -p "$FIXTURE"server_pid=""cleanup() {if [ -n "$server_pid" ]; then kill -TERM "$server_pid"2>/dev/null || true wait "$server_pid"2>/dev/null || truefi}trap cleanup EXIT"$COTTON_HEADLESS" seed --data-dir "$FIXTURE/serving" --size one > "$FIXTURE/seed.json"for n in {1..12}; do"$COTTON_HEADLESS" add-database --data-dir "$FIXTURE/serving" \ --database "Synthetic project $n" --size one --seed "$n" > "$FIXTURE/project-$n.json"done"$COTTON_HEADLESS" serve --data-dir "$FIXTURE/serving" > "$FIXTURE/server.log"2>&1 &server_pid=$!for n in {1..100}; doif rg -q '^BOUND '"$FIXTURE/server.log"; then break; fi kill -0"$server_pid" || { echo "recopy-sim: synthetic server stopped" >&2; exit1; } sleep 0.1doneport="$(sed -n 's/^BOUND //p' "$FIXTURE/server.log" | head -1)"[ -n "$port" ] || { echo "recopy-sim: server did not bind" >&2; exit1; }python3 scripts/recopy-sim-fixture.py "$FIXTURE/serving" --server-url "http://127.0.0.1:$port"bash scripts/build-xcframework.sh --debug --simulator-onlyif [ -z "${COTTON_SIM:-}" ]; then COTTON_SIM="$(COTTON_WALK_SIM_HOST=recopy-sim bash scripts/ios-walk-sim.sh)" COTTON_SIM="${COTTON_SIM%%|*}"export COTTON_SIMfi# Keep the result bundle: it holds the app's Debug output as attachments.bash scripts/gen-ios-project.shsource scripts/lib/derived-data.shBUILD_LOCK_LABEL=recopy-sim bash scripts/with-build-lock.sh \ xcodebuild test -project apps/cotton/Cotton.xcodeproj -scheme tinmen-cotton \ -destination "platform=iOS Simulator,id=$COTTON_SIM" \ -derivedDataPath "$(cotton_derived_data)" -resultBundlePath "$FIXTURE/result.xcresult" \ -only-testing:CottonUITests/HouseholdHomeReplicaTests \ CODE_SIGNING_ALLOWED=NO COTTON_FLAVOR=dev -quietxcrun xcresulttool get test-results summary --path "$FIXTURE/result.xcresult" --format json \ | python3 -c 'import json,sysresult=json.load(sys.stdin)print("recopy-sim: {} passed, {} failed, {} skipped".format(result.get("passedTests"),result.get("failedTests"),result.get("skippedTests")))if result.get("passedTests") != 2 or result.get("failedTests") != 0 or result.get("skippedTests") != 0: raise SystemExit("recopy-sim: both synthetic states must run and pass")'