#!/usr/bin/env bash
set -u
ROOT=/c/Users/decid/Documents/projects/spt-core
W=$ROOT/.worktrees/gate-302-a593ece0
G=$ROOT/.spt/preserved/302/gate-231-a593ece0
POOL='C:\Users\decid\Documents\projects\spt-core\.worktrees\302-meet-offload-field\target'
unset OWL_SESSION_ID SPT_AGENT_ID SPT_ENDPOINT_ID SPT_HOME SPT_RC_HITCH_DIAG SPT_NET_DIAG_SYMBOL_DIR
env | grep -E '^(SPT_|OWL_)' > "$G/env-scrub-check.txt" || true
while ! grep -q "^end" "$G/attempt1-pool-refusal/status.txt" 2>/dev/null; do sleep 10; done; echo "start $(date -u +%FT%TZ)" | tee "$G/status.txt"
cd "$ROOT"
if [ ! -d "$W" ]; then git worktree add --detach "$W" a593ece06d5b67afa7fcdd23824b585675d7cc04 2>&1 | tee "$G/worktree.log"; fi
cd "$W" && git rev-parse HEAD | tee "$G/head.txt"
export CARGO_TARGET_DIR="$POOL"
SPT_POOL_UNCHECKED=1 cargo run -p xtask -- pool-claim --pool "$POOL" --label gate-231-a593ece0 --foreign-pool 2>&1 | tee "$G/pool-claim.log"; echo "pool-claim exit ${PIPESTATUS[0]}" | tee -a "$G/status.txt"
cargo build --workspace --bins 2>&1 | tee "$G/build.log"; echo "build exit ${PIPESTATUS[0]}" | tee -a "$G/status.txt"
cargo nextest run -p spt-daemon --lib -E 'test(/^pairhost::meet_runtime_tests::/)' --no-fail-fast 2>&1 | tee "$G/meet.log"; echo "meet exit ${PIPESTATUS[0]}" | tee -a "$G/status.txt"
cargo nextest run -p spt-daemon --test net_worker_starve --no-fail-fast 2>&1 | tee "$G/starve.log"; echo "starve exit ${PIPESTATUS[0]}" | tee -a "$G/status.txt"
cargo nextest run -p spt-net --lib -E 'test(/pairing|meet|rendezvous|endpoint/)' --no-fail-fast 2>&1 | tee "$G/spt-net.log"; echo "spt-net exit ${PIPESTATUS[0]}" | tee -a "$G/status.txt"
cargo nextest run -p spt-daemon -E 'binary(netbroker) | binary(netstream) | binary(attach) | binary(mesh_recovery)' --no-fail-fast 2>&1 | tee "$G/daemon-int.log"; echo "daemon-int exit ${PIPESTATUS[0]}" | tee -a "$G/status.txt"
cargo clippy --workspace --all-targets -- -D warnings 2>&1 | tail -30 | tee "$G/clippy.log"; echo "clippy exit ${PIPESTATUS[0]}" | tee -a "$G/status.txt"
traceable-reqs check 2>&1 | tail -5 | tee "$G/trace.log"; echo "trace exit ${PIPESTATUS[0]}" | tee -a "$G/status.txt"
cargo run -p xtask -- pool-release --pool "$POOL" 2>&1 | tail -3 | tee "$G/pool-release.log"; echo "pool-release exit ${PIPESTATUS[0]}" | tee -a "$G/status.txt"
grep -hoE 'Summary \[[^]]*\] +[0-9]+ tests? run: [0-9]+ passed[^,]*(, [0-9]+ failed)?(, [0-9]+ skipped)?' "$G"/*.log | tee "$G/summaries.txt"
echo "end $(date -u +%FT%TZ)" | tee -a "$G/status.txt"
