import unittest from datetime import datetime, timedelta, timezone from scripts.reconcile import plan_reconciliation class ReconcilePlanTests(unittest.TestCase): def test_only_stale_orphans_and_old_unreferenced_rows_are_candidates(self): now = datetime.now(timezone.utc) old = now - timedelta(days=2) fresh = now - timedelta(minutes=2) metadata = [ {"id": "used", "object_key": "attachments/used", "created_at": old, "referenced": True}, {"id": "unreferenced", "object_key": "attachments/missing", "created_at": old, "referenced": False}, {"id": "fresh", "object_key": "attachments/fresh-missing", "created_at": fresh, "referenced": False}, ] objects = [ {"Key": "attachments/used", "LastModified": old}, {"Key": "attachments/orphan-old", "LastModified": old}, {"Key": "attachments/orphan-fresh", "LastModified": fresh}, {"Key": "unrelated/keep", "LastModified": old}, ] plan = plan_reconciliation(metadata, objects, now, timedelta(hours=24)) self.assertEqual(plan["orphan_old"], ["attachments/orphan-old"]) self.assertEqual(plan["orphan_fresh"], ["attachments/orphan-fresh"]) self.assertEqual(plan["missing_unreferenced_old"], ["unreferenced"]) self.assertEqual(plan["missing_protected"], ["fresh"]) def test_repeat_after_repair_has_no_candidates(self): now = datetime.now(timezone.utc) plan = plan_reconciliation([], [], now, timedelta(hours=24)) self.assertEqual(plan["orphan_old"], []) self.assertEqual(plan["missing_unreferenced_old"], []) if __name__ == "__main__": unittest.main()