test_prune_runtime_cache.py 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. import os
  2. import time
  3. from pathlib import Path
  4. from scripts.prune_runtime_cache import collect_candidates, prune_candidates
  5. def _touch_dir(path: Path, mtime: float) -> None:
  6. path.mkdir(parents=True)
  7. (path / "final_output.json").write_text("{}\n", encoding="utf-8")
  8. os.utime(path, (mtime, mtime))
  9. def test_collect_candidates_only_matches_old_v1_run_dirs(tmp_path):
  10. now = time.time()
  11. old_run = tmp_path / "v1_run_old"
  12. recent_run = tmp_path / "v1_run_recent"
  13. other_dir = tmp_path / "notes"
  14. _touch_dir(old_run, now - 31 * 24 * 60 * 60)
  15. _touch_dir(recent_run, now - 2 * 24 * 60 * 60)
  16. _touch_dir(other_dir, now - 60 * 24 * 60 * 60)
  17. candidates = collect_candidates(tmp_path, retention_days=30, now=now)
  18. assert [candidate.path for candidate in candidates] == [old_run]
  19. assert candidates[0].size_bytes > 0
  20. def test_prune_candidates_dry_run_keeps_directories(tmp_path):
  21. now = time.time()
  22. old_run = tmp_path / "v1_run_old"
  23. _touch_dir(old_run, now - 31 * 24 * 60 * 60)
  24. candidates = collect_candidates(tmp_path, retention_days=30, now=now)
  25. count = prune_candidates(candidates, execute=False)
  26. assert count == 1
  27. assert old_run.exists()
  28. def test_prune_candidates_execute_deletes_directories(tmp_path):
  29. now = time.time()
  30. old_run = tmp_path / "v1_run_old"
  31. _touch_dir(old_run, now - 31 * 24 * 60 * 60)
  32. candidates = collect_candidates(tmp_path, retention_days=30, now=now)
  33. count = prune_candidates(candidates, execute=True)
  34. assert count == 1
  35. assert not old_run.exists()