from datetime import datetime, timedelta, timezone from uuid import uuid4 from app.storage import StoredObject from app.tools.reconcile_attachments import AttachmentSnapshot, build_plan def test_plan_classifies_both_sides_and_protects_fresh_items(): """Классифицирует orphan/missing и защищает свежие расхождения.""" now = datetime.now(timezone.utc) cutoff = now - timedelta(minutes=1) correct_key = f'originals/{uuid4()}' missing = AttachmentSnapshot(uuid4(), f'originals/{uuid4()}', 'ready', cutoff) fresh_missing = AttachmentSnapshot(uuid4(), f'originals/{uuid4()}', 'ready', now) already_failed = AttachmentSnapshot(uuid4(), f'originals/{uuid4()}', 'failed', cutoff) attachments = ( AttachmentSnapshot(uuid4(), correct_key, 'ready', cutoff), missing, fresh_missing, already_failed, ) old_orphan = StoredObject(f'originals/{uuid4()}', cutoff) fresh_orphan = StoredObject(f'originals/{uuid4()}', now) plan = build_plan( attachments, (StoredObject(correct_key, cutoff), old_orphan, fresh_orphan), cutoff, ) assert plan.correct == 1 assert plan.orphan == (old_orphan,) assert plan.fresh_orphan == (fresh_orphan,) assert plan.missing == (missing,) assert plan.fresh_missing == (fresh_missing,) def test_failed_metadata_still_protects_its_object_from_orphan_cleanup(): """Проверяет защиту объекта существующей failed-metadata.""" now = datetime.now(timezone.utc) key = f'originals/{uuid4()}' failed = AttachmentSnapshot(uuid4(), key, 'failed', now - timedelta(hours=2)) plan = build_plan( (failed,), (StoredObject(key, now - timedelta(hours=2)),), now - timedelta(hours=1), ) assert plan.correct == 1 assert plan.orphan == () assert plan.missing == ()