#!/usr/bin/env bash
# The caps in the prune unit are a disk budget, not decoration: if a disk's caps
# plus the trees no pruner bounds exceed it, the boxes fill up again. Each box
# has two 110G disks; a root is budgeted against the disk it physically lands on
# (~/cdx-offload is a symlink into the scratch disk, so it spends scratch).
set -uo pipefail

MOD="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
UNIT="$MOD/user-config/systemd/buildbox-build-cache-prune.service"
PRUNER="$MOD/user-config/bin/buildbox-build-cache-prune.sh"
fail=0
t() { if [ "$2" = "$3" ]; then printf 'ok    %s\n' "$1"; else printf 'FAIL  %s: expected %s, got %s\n' "$1" "$2" "$3"; fail=$((fail + 1)); fi; }

DISK_GB=110
FLOOR_GB=15
# Measured on the fleet 2026-08-14: trees with retention but no size cap.
SCRATCH_UNCAPPED_GB=$((13 + 15 + 9 + 3 + 2))   # runs, runner-work, pnpm-store, playwright, npm+misc
ROOT_UNCAPPED_GB=$((10 + 8 + 4 + 3))           # OS, ~/.local, ~/.cache, runners+toolchains

cap_gb() { # unit-line -> max cap in GiB
  printf '%s\n' "$1" | grep -oE 'BUILDBOX_CACHE_MAX_BYTES=[0-9]+' | cut -d= -f2 | awk '{print int($1/1024/1024/1024)}'
}

scratch_gb=0
root_gb=0
env_roots=0
while IFS= read -r line; do
  case "$line" in *BUILDBOX_BUILDS_ROOT=*) ;; *) continue ;; esac
  root=$(printf '%s\n' "$line" | grep -oE 'BUILDBOX_BUILDS_ROOT=[^ ]+' | cut -d= -f2)
  max=$(printf '%s\n' "$line" | grep -oE 'BUILDBOX_CACHE_MAX_BYTES=[0-9]+' | cut -d= -f2)
  target=$(printf '%s\n' "$line" | grep -oE 'BUILDBOX_CACHE_TARGET_BYTES=[0-9]+' | cut -d= -f2)
  [ -n "$max" ] && [ -n "$target" ] || { printf 'FAIL  %s declares no cap pair\n' "$root"; fail=$((fail + 1)); continue; }
  [ "$target" -lt "$max" ] || { printf 'FAIL  %s target is not below max\n' "$root"; fail=$((fail + 1)); }
  case "$root" in %h|%h/) printf 'FAIL  %s is the home directory itself\n' "$root"; fail=$((fail + 1)) ;; esac
  env_roots=$((env_roots + 1))
  case "$root" in
    *cdx-offload*) scratch_gb=$((scratch_gb + $(cap_gb "$line"))) ;;
    *) root_gb=$((root_gb + $(cap_gb "$line"))) ;;
  esac
done <"$UNIT"

t "the unit caps the workspace and offload trees" 2 "$env_roots"

# The build mirror cache is the script's default root and lives on scratch.
builds_gb=$(grep -oE 'BUILDBOX_CACHE_MAX_BYTES:-\$\(\([0-9]+' "$PRUNER" | grep -oE '[0-9]+$' | head -1)
[ -n "$builds_gb" ] || { printf 'FAIL  cannot read the default cap\n'; fail=$((fail + 1)); builds_gb=0; }
scratch_gb=$((scratch_gb + builds_gb))

check_disk() { # name capped uncapped
  local total=$(( $2 + $3 + FLOOR_GB ))
  if [ "$total" -le "$DISK_GB" ]; then
    printf 'ok    %s budget fits (%dG capped + %dG uncapped + %dG floor = %dG of %dG)\n' "$1" "$2" "$3" "$FLOOR_GB" "$total" "$DISK_GB"
  else
    printf 'FAIL  %s budget overcommits: %dG capped + %dG uncapped + %dG floor = %dG > %dG\n' "$1" "$2" "$3" "$FLOOR_GB" "$total" "$DISK_GB"
    fail=$((fail + 1))
  fi
}

check_disk scratch "$scratch_gb" "$SCRATCH_UNCAPPED_GB"
check_disk root "$root_gb" "$ROOT_UNCAPPED_GB"

[ "$fail" = 0 ] || { printf '\n%d failed\n' "$fail"; exit 1; }
printf '\nall passed\n'
