#!/usr/bin/env bash
# Run the full test suite in a throwaway Linux container with no network.
#
# Why not just `cargo test`: the suite starts brokers, workers and a WireGuard
# sidecar. Run on a machine that is part of a mesh, it has taken that machine's
# tunnel down. The container gives it a kernel that is not yours to break, and
# `--network none` makes "it only touched loopback" a property rather than a
# hope.
#
# Usage:
#   scripts/test-sandbox.sh                       # the whole workspace
#   scripts/test-sandbox.sh -E 'test(router)'     # anything else goes to nextest
#   CPUS=8 scripts/test-sandbox.sh                # more cores
#
# The compiled artifacts live in a Docker volume named after this checkout, so
# parallel worktrees do not fight over one target dir and a second run is warm.

set -euo pipefail
cd "$(dirname "$0")/.."
SRC="$(pwd)"

IMAGE="${IMAGE:-rust:1-bookworm}"
CPUS="${CPUS:-4}"
# One target volume per checkout: `zc-target-<dirname>-<hash>`, stable across
# runs and distinct between worktrees.
SLUG="$(basename "$SRC" | tr -c 'a-zA-Z0-9_.-' '-')"
HASH="$(printf '%s' "$SRC" | cksum | cut -d' ' -f1)"
TARGET_VOL="zc-target-${SLUG}-${HASH}"
TOOLS_VOL="zc-sandbox-tools"

command -v docker >/dev/null || {
  echo "error: docker is not installed or not on PATH" >&2
  exit 1
}
docker info >/dev/null 2>&1 || {
  echo "error: the Docker daemon is not reachable — is Docker Desktop running?" >&2
  exit 1
}

docker volume create "$TARGET_VOL" >/dev/null
docker volume create "$TOOLS_VOL" >/dev/null

# cargo-nextest cannot be installed with the network off, so it is installed
# once into a shared volume and reused. This is the only networked step.
if ! docker run --rm -v "$TOOLS_VOL:/tools" "$IMAGE" test -x /tools/bin/cargo-nextest; then
  echo "==> installing cargo-nextest into the $TOOLS_VOL volume (once)"
  docker run --rm \
    -v "$TOOLS_VOL:/tools" \
    -v "$HOME/.cargo/registry:/usr/local/cargo/registry" \
    "$IMAGE" cargo install cargo-nextest --locked --root /tools
fi

echo "==> running the suite: image=$IMAGE cpus=$CPUS target=$TARGET_VOL"

# `--init` is not optional. Without a PID 1 to reap, a SIGKILLed process stays
# a zombie, `kill(-pid, 0)` keeps succeeding on it, and every test that waits
# for a process group to die waits forever (zc#277).
#
# `RUSTUP_TOOLCHAIN` is resolved from the image because an offline rustup
# cannot look up what `stable` means.
exec docker run --rm --init \
  --network none \
  --cpus "$CPUS" \
  -e CARGO_HOME=/cargo \
  -e CARGO_TARGET_DIR=/target \
  -e HOME=/tmp/home \
  -e ZAKURO_HOME=/tmp/zakuro \
  -v "$HOME/.cargo/registry:/cargo/registry" \
  -v "$HOME/.cargo/git:/cargo/git" \
  -v "$TOOLS_VOL:/tools:ro" \
  -v "$SRC:/src:ro" \
  -v "$TARGET_VOL:/target" \
  -w /src \
  "$IMAGE" \
  bash -c '
    set -e
    export RUSTUP_TOOLCHAIN="$(rustup toolchain list | head -1 | cut -d" " -f1)"
    export PATH="/tools/bin:$PATH"
    mkdir -p /tmp/home /tmp/zakuro
    if [ "$#" -eq 0 ]; then
      exec cargo nextest run --offline --workspace --profile ci
    fi
    exec cargo nextest run --offline --workspace --profile ci "$@"
  ' -- "$@"
