echo"PKGCACHE_CA=$T/ca.crt" >"$T/pkgcache-host.conf"# no ENABLED => OFF
# ---- default OFF ----
st '^setting: OFF'&& st '^mode: off'&& ok "default: setting OFF, mode off"|| bad "default status"
if"$R" apply >/dev/null 2>&1;then bad "apply without URL must refuse while OFF";else ok "apply without URL refuses while OFF";fi
"$R" sync >/dev/null; same "$T/repos/almalinux-baseos.repo""$T/orig-alma"&& ok "sync while OFF leaves stock repos untouched"|| bad "sync OFF touched repos"
[ ! -e "$T/units/pkgcache-host-health.timer"]&& ok "sync while OFF: no health timer"|| bad "timer present while OFF"
grep -q '^baseurl=https://cache.test/almalinux/\$releasever/BaseOS/\$basearch/os/ https://repo.almalinux.org/almalinux/'"$T/repos/almalinux-baseos.repo"&& ok "sync ON: cache-first baseurl with origin as 2nd URL"|| bad "sync ON rewrite"
same "$T/repos/custom.repo""$T/orig-custom"&& ok "unmanaged repo untouched"|| bad "unmanaged repo changed"
[ -e "$T/units/pkgcache-host-health.timer"]&& grep -q 'OnUnitActiveSec=5min'"$T/units/pkgcache-host-health.timer"&& ok "health timer installed (5 min)"|| bad "timer missing"
st '^mode: cache-first'&& ok "status: mode cache-first"|| bad "status cache-first"
"$R" sync >/dev/null;["$(ls -d "$T"/bk/pkgcache-repo-backup-* | wc -l)"=1]&& ok "second sync does not take a 2nd (poisoned) backup"|| bad "double backup"
# ---- health: fallback after 2 failures, restore after 3 successes ----
FAKE_CACHE=empty "$HL" run 2>/dev/null;"$HL" mode | grep -q cache-first && ok "1 failure: still cache-first"|| bad "1 failure"
FAKE_CACHE=empty "$HL" run 2>/dev/null;["$("$HL" mode)"= origin-only ]&& ok "2 failures in a row: origin-only"|| bad "no fallback"
grep -q '^baseurl=https://repo.almalinux.org/almalinux/\$releasever/BaseOS/\$basearch/os/$'"$T/repos/almalinux-baseos.repo"&& grep -q '^#pkgcache-cachefirst baseurl=https://cache.test/'"$T/repos/almalinux-baseos.repo"&& ok "origin-only rewrite keeps the marker line"|| bad "origin-only content"
same "$T/repos/custom.repo""$T/orig-custom"&& ok "fallback never touches unmanaged repos"|| bad "fallback touched custom.repo"
st '^mode: origin-only'&& ok "status: mode origin-only"|| bad "status origin-only"
"$HL" run 2>/dev/null;"$HL" run 2>/dev/null;["$("$HL" mode)"= origin-only ]&& ok "2 successes: still origin-only"|| bad "restored too early"
FAKE_CACHE=noasc "$HL" run 2>/dev/null;"$HL" run 2>/dev/null;"$HL" run 2>/dev/null;["$("$HL" mode)"= origin-only ]&& ok "a bad .asc resets the success streak"|| bad "streak not reset"
"$HL" run 2>/dev/null;["$("$HL" mode)"= cache-first ]&& ok "3 successes: cache-first again"|| bad "no restore"
ls "$T"/repos/.pkgcache-* >/dev/null 2>&1&& bad "temp file left behind"|| ok "no temp files left (atomic rename)"
grep -q 'repomd.xml.asc'"$T/curl.log"&& ok "probe fetched repomd.xml.asc"|| bad ".asc not probed"
# both down => not counted
FAKE_CACHE=refused FAKE_ORIGIN=down "$HL" run 2>/dev/null;FAKE_CACHE=refused FAKE_ORIGIN=down "$HL" run 2>/dev/null;FAKE_CACHE=refused FAKE_ORIGIN=down "$HL" run 2>/dev/null
["$("$HL" mode)"= cache-first ]&& ok "cache+origin both down: no fallback"|| bad "network outage caused a fallback"
FAKE_CACHE=refused "$HL" run 2>/dev/null;FAKE_CACHE=refused "$HL" run 2>/dev/null;["$("$HL" mode)"= origin-only ]&& ok "connection refused counts as failure"|| bad "refused not counted"
same "$T/repos/almalinux-baseos.repo""$T/orig-alma"&& same "$T/repos/custom.repo""$T/orig-custom"&& ok "setting OFF: stock repos restored byte-identical (from origin-only)"|| bad "revert not identical"
[ ! -e "$T/units/pkgcache-host-health.timer"]&&[ ! -e "$T/state/state"]&& ok "setting OFF: timer + state removed"|| bad "timer/state left"
st '^mode: off'&& ok "status: mode off after revert"|| bad "status after revert"