#!/usr/bin/env bash # Tests for the `serve` subcommand of setup_personal_website.sh and # setup_kcd2_dice_solver.sh: re-render the inner compose + Caddyfile and # recreate only that site's container, with no sudo, pacman, rebuild or edge # reload. # # Hermetic by necessity, not by taste. Every path in the entry scripts is a # readonly constant under $HOME and the website's container_name is fixed, so a # real `docker compose up --force-recreate` from a throwaway dir would recreate # the LIVE personal-website container. Instead the real entry scripts run with # HOME pointed at a temp dir and docker/curl stubbed on PATH. The rendered # Caddyfile itself is served by real caddy in e2e_agent_ready_caddy.sh. set -euo pipefail SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" # shellcheck source=./features_harness.sh . "$SCRIPT_DIR/features_harness.sh" FEATURES_DIR="$(cd "$FEATURES_LIB_DIR/.." && pwd)" readonly FEATURES_DIR _t_setup_env trap _t_teardown EXIT # docker records every call; `docker exec netstat` answers $FAKE_LISTENERS, # the sudo-free listener table the dice edge-conflict guard reads. cat >"$TEST_TMPDIR/bin/docker" <<'EOF' #!/usr/bin/env bash printf 'docker %s\n' "$*" >>"$TEST_TMPDIR/calls.log" if [[ $1 == exec && $* == *netstat* ]]; then printf '%s\n' "${FAKE_LISTENERS:-}" fi EOF # curl expands only the -w variables the readiness loops ask for, plus the \n # escape as real curl does. cat >"$TEST_TMPDIR/bin/curl" <<'EOF' #!/usr/bin/env bash printf 'curl %s\n' "$*" >>"$TEST_TMPDIR/calls.log" while [[ $# -gt 0 ]]; do if [[ $1 == -w ]]; then fmt="${2//'%{http_code}'/${FAKE_CODE:-200}}" printf '%b' "${fmt//'%{size_download}'/1234}" fi shift done EOF chmod +x "$TEST_TMPDIR/bin/docker" "$TEST_TMPDIR/bin/curl" # Anything serve must never reach. `sleep` only to keep the 404 case fast. for cmd in sudo pacman pnpm systemctl sleep; do _t_stub "$cmd" done readonly OK_LISTENERS="tcp 0 0 127.0.0.1:8089 0.0.0.0:* LISTEN 1/caddy" readonly WEB_SERVE="$TEST_TMPDIR/services/personal-website-serve" readonly DICE_SERVE="$TEST_TMPDIR/services/kcd2-dice-serve" make_dist() { mkdir -p "$TEST_TMPDIR/src/$1/dist" printf '\n' >"$TEST_TMPDIR/src/$1/dist/index.html" } # run_serve