#!/usr/bin/env bash
# Exercise ci-scratch-prune only against fresh fixtures; never real roots.
set -euo pipefail

REPO="$(cd "$(dirname "${BASH_SOURCE[0]}")/../../.." && pwd)"
PRUNER="$REPO/modules/buildbox/user-config/bin/ci-scratch-prune.sh"
WORK="$(mktemp -d "$REPO/.ci-scratch-prune-fixture.XXXXXX")"
trap 'rm -rf "$WORK"' EXIT
fail() { printf 'FAIL %s\n' "$1" >&2; exit 1; }

FAKE_HOME="$WORK/home"
mkdir -p "$FAKE_HOME/.local/bin" "$FAKE_HOME/.config/systemd/user" "$FAKE_HOME/.cache" \
  "$WORK/tmp" "$WORK/workspaces" "$WORK/runs"

# workspaces: idle entry goes, entry with ONE fresh file deep inside stays.
# Files are created before directory mtimes are set: file creation bumps the
# parent dir mtime, which would silently undo an earlier backdate.
mkdir -p "$WORK/workspaces/idle/deep" "$WORK/workspaces/live/deep"
touch -d '30 days ago' "$WORK/workspaces/idle/deep/f"
touch -d '30 days ago' "$WORK/workspaces/idle" "$WORK/workspaces/idle/deep"
touch "$WORK/workspaces/live/deep/fresh"
touch -d '30 days ago' "$WORK/workspaces/live" "$WORK/workspaces/live/deep"

# runs: 3-day window; hidden entries are stashes and never candidates
mkdir -p "$WORK/runs/old" "$WORK/runs/recent" "$WORK/runs/.stash"
touch -d '10 days ago' "$WORK/runs/old"
find "$WORK/runs/old" -exec touch -d '10 days ago' {} +
find "$WORK/runs/.stash" -exec touch -d '30 days ago' {} +
touch "$WORK/runs/recent"

# staging orphans: stale one goes, fresh one stays, unrelated dotfile stays
touch -d '2 days ago' "$FAKE_HOME/.local/bin/.tool.staging-123-abc"
touch "$FAKE_HOME/.local/bin/.tool.staging-456-def"
touch -d '2 days ago' "$FAKE_HOME/.local/bin/.unrelated-dotfile"
touch -d '2 days ago' "$FAKE_HOME/.config/systemd/user/.unit.timer.staging-789-aaa"

# offload: reached through a symlink, as a relocated tree is on the boxes
mkdir -p "$WORK/offload-store/idle" "$WORK/offload-store/live"
touch -d '30 days ago' "$WORK/offload-store/idle"
find "$WORK/offload-store/idle" -exec touch -d '30 days ago' {} +
touch "$WORK/offload-store/live"
ln -sfnT "$WORK/offload-store" "$FAKE_HOME/cdx-offload"

HOME="$FAKE_HOME" CI_TMP_ROOT="$WORK/tmp" \
  SANDBOX_WORKSPACES_ROOT="$WORK/workspaces" BUILDBOX_RUNS_ROOT="$WORK/runs" \
  bash "$PRUNER" >/dev/null 2>&1 || true

[ ! -e "$WORK/workspaces/idle" ] || fail "idle workspace retained"
[ -e "$WORK/workspaces/live/deep/fresh" ] || fail "live workspace pruned despite deep fresh file"
[ ! -e "$WORK/runs/old" ] || fail "old run retained"
[ -e "$WORK/runs/recent" ] || fail "recent run pruned"
[ -d "$WORK/runs/.stash" ] || fail "hidden stash entry pruned"
[ ! -e "$FAKE_HOME/.local/bin/.tool.staging-123-abc" ] || fail "stale staging orphan retained"
[ -e "$FAKE_HOME/.local/bin/.tool.staging-456-def" ] || fail "fresh staging file pruned"
[ -e "$FAKE_HOME/.local/bin/.unrelated-dotfile" ] || fail "unrelated dotfile pruned"
[ ! -e "$FAKE_HOME/.config/systemd/user/.unit.timer.staging-789-aaa" ] || fail "stale unit staging orphan retained"
[ ! -e "$WORK/offload-store/idle" ] || fail "idle offload checkout retained"
[ -e "$WORK/offload-store/live" ] || fail "recent offload checkout pruned"
[ -L "$FAKE_HOME/cdx-offload" ] || fail "offload relocation symlink was replaced"

printf 'PASS ci-scratch-prune behavior\n'
