import os import time from pathlib import Path from scripts.prune_runtime_cache import collect_candidates, prune_candidates def _touch_dir(path: Path, mtime: float) -> None: path.mkdir(parents=True) (path / "final_output.json").write_text("{}\n", encoding="utf-8") os.utime(path, (mtime, mtime)) def test_collect_candidates_only_matches_old_v1_run_dirs(tmp_path): now = time.time() old_run = tmp_path / "v1_run_old" recent_run = tmp_path / "v1_run_recent" other_dir = tmp_path / "notes" _touch_dir(old_run, now - 31 * 24 * 60 * 60) _touch_dir(recent_run, now - 2 * 24 * 60 * 60) _touch_dir(other_dir, now - 60 * 24 * 60 * 60) candidates = collect_candidates(tmp_path, retention_days=30, now=now) assert [candidate.path for candidate in candidates] == [old_run] assert candidates[0].size_bytes > 0 def test_prune_candidates_dry_run_keeps_directories(tmp_path): now = time.time() old_run = tmp_path / "v1_run_old" _touch_dir(old_run, now - 31 * 24 * 60 * 60) candidates = collect_candidates(tmp_path, retention_days=30, now=now) count = prune_candidates(candidates, execute=False) assert count == 1 assert old_run.exists() def test_prune_candidates_execute_deletes_directories(tmp_path): now = time.time() old_run = tmp_path / "v1_run_old" _touch_dir(old_run, now - 31 * 24 * 60 * 60) candidates = collect_candidates(tmp_path, retention_days=30, now=now) count = prune_candidates(candidates, execute=True) assert count == 1 assert not old_run.exists()