"""`AttachmentService`: порядок проверок и записи, компенсация, чтение.""" import asyncio from collections.abc import AsyncIterator from dataclasses import replace from pathlib import Path from uuid import UUID, uuid4 import pytest from builders import ( NOW, attachment_dto, chat_dto, job_dto, user_dto, variant_dto, ) from fakes import FakeCache, FakeDatabase, FakeObjectStorage, FakeUnitOfWork from src.api.errors import DOMAIN_ERRORS, RETRY_AFTER from src.api.schemas import ErrorCode from src.config import settings from src.core import exceptions from src.core.services import AttachmentService from src.infra.upload_slots import UploadSlots from src.schemas import dataclasses from src.schemas.enums import ( AttachmentErrorCode, AttachmentJobState, AttachmentStatus, AttachmentVariantName, ) from src.utils.constants import ATTACHMENT_MAX_BYTES pytestmark = pytest.mark.anyio SAMPLE = ( Path(__file__).resolve().parents[2] / "tests" / "fixtures" / "sample.png" ).read_bytes() UNAVAILABLE = exceptions.ServiceUnavailable def _slots(*, max_concurrent: int = 1, max_waiting: int = 0) -> UploadSlots: return UploadSlots( max_concurrent=max_concurrent, max_waiting=max_waiting, wait_timeout_seconds=0.01, ) async def _body(*chunks: bytes) -> AsyncIterator[bytes]: for chunk in chunks: yield chunk @pytest.fixture def service( uow: FakeUnitOfWork, cache: FakeCache, storage: FakeObjectStorage, ) -> AttachmentService: return AttachmentService(uow, cache, storage, _slots()) @pytest.fixture async def world( uow: FakeUnitOfWork, database: FakeDatabase, ) -> tuple[UUID, UUID, UUID]: alice = user_dto() bob = user_dto() chat = chat_dto(member_ids=(alice.id,)) async with uow: await uow.users.insert_user(user=alice, password_hash="h") await uow.users.insert_user(user=bob, password_hash="h") await uow.chats.insert_chat(chat=chat) await uow.chats.insert_chat_members(chat=chat) await uow.commit() database.events.clear() return alice.id, bob.id, chat.id async def _upload( service: AttachmentService, world: tuple[UUID, UUID, UUID], *, owner: UUID | None = None, media_type: str | None = "image/png", content_length: int | None = None, body: AsyncIterator[bytes] | None = None, ): alice, _, chat_id = world return await service.upload( chat_id=chat_id, owner_id=owner or alice, filename="a.png", media_type=media_type, content_length=content_length, body=body if body is not None else _body(SAMPLE), ) class _Untouched: """Тело, которое нельзя начать читать.""" def __aiter__(self): raise AssertionError("body must not be read") async def test_upload_order_and_events( service: AttachmentService, database: FakeDatabase, storage: FakeObjectStorage, world: tuple[UUID, UUID, UUID], ) -> None: attachment = await _upload(service, world) assert attachment.status == AttachmentStatus.QUEUED assert database.attachments[attachment.id].status == ( AttachmentStatus.QUEUED ) job = next(iter(database.jobs.values())) assert job.attachment_id == attachment.id assert job.state == AttachmentJobState.QUEUED assert storage.objects[attachment.staging_key][0] == SAMPLE events = [item for item in database.events if not item.startswith("cache")] assert events == [ "enter", "chats.get_chat", "exit", "enter", "attachments.insert_attachment", "commit", "exit", "storage.put", "enter", "attachments.mark_queued", "jobs.insert_job", "commit", "exit", ] assert not database.inside_block("storage.put") async def test_upload_saves_trace_context_in_job( service: AttachmentService, database: FakeDatabase, world: tuple[UUID, UUID, UUID], ) -> None: """Контекст загрузки — в задании той же транзакцией, что `queued` (design change `job-trace-propagation`, решение 4).""" alice, _, chat_id = world context = dataclasses.TraceContextDTO( traceparent="00-0af7651916cd43dd8448eb211c80319c-b7ad6b7169203331-01", tracestate="vendor=value", ) attachment = await service.upload( chat_id=chat_id, owner_id=alice, filename="a.png", media_type="image/png", content_length=None, body=_body(SAMPLE), trace_context=context, ) (job,) = database.jobs.values() assert job.attachment_id == attachment.id assert job.traceparent == context.traceparent assert job.tracestate == "vendor=value" async def test_upload_without_trace_context( service: AttachmentService, database: FakeDatabase, world: tuple[UUID, UUID, UUID], ) -> None: """Без телеметрии контекста нет — колонки задания пусты.""" await _upload(service, world) (job,) = database.jobs.values() assert job.traceparent is None assert job.tracestate is None def _fill_backlog(database: FakeDatabase, count: int) -> None: """`count` незавершённых заданий: `queued` и `processing` поровну.""" for number in range(count): state = ( AttachmentJobState.QUEUED if number % 2 == 0 else AttachmentJobState.PROCESSING ) job = job_dto(attachment_id=uuid4()) database.jobs[job.id] = replace(job, state=state) async def test_backlog_full_rejects_without_body_or_slot( uow: FakeUnitOfWork, cache: FakeCache, storage: FakeObjectStorage, database: FakeDatabase, world: tuple[UUID, UUID, UUID], monkeypatch: pytest.MonkeyPatch, ) -> None: monkeypatch.setattr(settings.uploads, "max_unfinished_jobs", 3) _fill_backlog(database, 3) slots = _slots() service = AttachmentService(uow, cache, storage, slots) # все места заняты: 429 предела приходит раньше очереди мест async with slots.slot(): with pytest.raises(exceptions.UploadBacklogFull): await _upload(service, world, body=_Untouched()) assert database.attachments == {} assert storage.objects == {} assert "jobs.count_unfinished" in database.events # отказ не занял места: занятое выше освобождено, новая берётся async with slots.slot(): pass async def test_backlog_below_limit_accepts( service: AttachmentService, database: FakeDatabase, world: tuple[UUID, UUID, UUID], monkeypatch: pytest.MonkeyPatch, ) -> None: monkeypatch.setattr(settings.uploads, "max_unfinished_jobs", 3) _fill_backlog(database, 2) attachment = await _upload(service, world) assert attachment.status == AttachmentStatus.QUEUED assert database.events.index("jobs.count_unfinished") < ( database.events.index("attachments.insert_attachment") ) # счёт — в первом блоке, рядом с членством: шаг с БД до тела один first_block = database.events[ database.events.index("enter") : database.events.index("exit") ] assert "jobs.count_unfinished" in first_block async def test_backlog_finished_jobs_not_counted( service: AttachmentService, database: FakeDatabase, world: tuple[UUID, UUID, UUID], monkeypatch: pytest.MonkeyPatch, ) -> None: monkeypatch.setattr(settings.uploads, "max_unfinished_jobs", 2) for state in (AttachmentJobState.SUCCEEDED, AttachmentJobState.FAILED): for _ in range(3): job = job_dto(attachment_id=uuid4()) database.jobs[job.id] = replace(job, state=state) attachment = await _upload(service, world) assert attachment.status == AttachmentStatus.QUEUED async def test_backlog_off_makes_no_query( service: AttachmentService, database: FakeDatabase, world: tuple[UUID, UUID, UUID], monkeypatch: pytest.MonkeyPatch, ) -> None: monkeypatch.setattr(settings.uploads, "max_unfinished_jobs", 0) _fill_backlog(database, 5) attachment = await _upload(service, world) assert attachment.status == AttachmentStatus.QUEUED assert "jobs.count_unfinished" not in database.events @pytest.mark.parametrize( ("kind", "error"), [ ("stranger", exceptions.ChatNotFound), ("type", exceptions.UnsupportedMediaType), ("size", exceptions.PayloadTooLarge), ], ) async def test_backlog_checked_after_cheap_checks( service: AttachmentService, database: FakeDatabase, world: tuple[UUID, UUID, UUID], monkeypatch: pytest.MonkeyPatch, kind: str, error: type[Exception], ) -> None: monkeypatch.setattr(settings.uploads, "max_unfinished_jobs", 1) _fill_backlog(database, 1) _, bob, _ = world arguments = { "stranger": {"owner": bob}, "type": {"media_type": "text/plain"}, "size": {"content_length": ATTACHMENT_MAX_BYTES + 1}, }[kind] with pytest.raises(error): await _upload(service, world, body=_Untouched(), **arguments) assert "jobs.count_unfinished" not in database.events async def test_backlog_error_maps_to_429_with_retry_after() -> None: assert DOMAIN_ERRORS[exceptions.UploadBacklogFull] == ( 429, ErrorCode.RATE_LIMITED, ) assert RETRY_AFTER[exceptions.UploadBacklogFull] == 5 async def test_not_member_404_before_everything( service: AttachmentService, database: FakeDatabase, world: tuple[UUID, UUID, UUID], ) -> None: _, bob, _ = world with pytest.raises(exceptions.ChatNotFound): await _upload( service, world, owner=bob, media_type="text/plain", body=_Untouched(), ) assert database.attachments == {} async def test_type_415_before_size_413( service: AttachmentService, world: tuple[UUID, UUID, UUID], ) -> None: with pytest.raises(exceptions.UnsupportedMediaType): await _upload( service, world, media_type="text/plain", content_length=ATTACHMENT_MAX_BYTES + 1, body=_Untouched(), ) async def test_declared_size_413_before_slot( uow: FakeUnitOfWork, cache: FakeCache, storage: FakeObjectStorage, world: tuple[UUID, UUID, UUID], ) -> None: slots = _slots() service = AttachmentService(uow, cache, storage, slots) async with slots.slot(): # места нет, а отказ всё равно 413: место не ждали with pytest.raises(exceptions.PayloadTooLarge): await _upload( service, world, content_length=ATTACHMENT_MAX_BYTES + 1, body=_Untouched(), ) async def test_busy_slots_429_without_reading_body( uow: FakeUnitOfWork, cache: FakeCache, storage: FakeObjectStorage, database: FakeDatabase, world: tuple[UUID, UUID, UUID], ) -> None: slots = _slots() service = AttachmentService(uow, cache, storage, slots) async with slots.slot(): with pytest.raises(exceptions.UploadsBusy): await _upload(service, world, body=_Untouched()) assert database.attachments == {} async def test_counter_413_interrupt_and_body_timeout( service: AttachmentService, database: FakeDatabase, world: tuple[UUID, UUID, UUID], ) -> None: with pytest.raises(exceptions.PayloadTooLarge): await _upload( service, world, body=_body(b"x" * ATTACHMENT_MAX_BYTES, b"x"), ) async def interrupted() -> AsyncIterator[bytes]: yield b"x" raise exceptions.UploadInterrupted with pytest.raises(exceptions.UploadInterrupted): await _upload(service, world, body=interrupted()) async def slow() -> AsyncIterator[bytes]: yield b"x" await asyncio.sleep(30) with pytest.raises(exceptions.UploadTimedOut): await _upload(service, world, body=slow()) assert database.attachments == {} @pytest.mark.parametrize( ("data", "media_type"), [ (b"not an image", "image/png"), (SAMPLE, "image/jpeg"), (b"", "image/png"), ], ids=["content", "mismatch", "empty"], ) async def test_bad_content_415_no_row( service: AttachmentService, database: FakeDatabase, world: tuple[UUID, UUID, UUID], data: bytes, media_type: str, ) -> None: with pytest.raises(exceptions.UnsupportedMediaType): await _upload( service, world, media_type=media_type, body=_body(data), ) assert database.attachments == {} async def test_put_failure_leaves_pending_without_compensation( service: AttachmentService, database: FakeDatabase, world: tuple[UUID, UUID, UUID], ) -> None: database.fail_on["storage.put"] = exceptions.ObjectStorageUnavailable() with pytest.raises(exceptions.ObjectStorageUnavailable): await _upload(service, world) row = next(iter(database.attachments.values())) assert row.status == AttachmentStatus.PENDING assert "storage.delete" not in database.events async def test_failure_on_statement_compensates( service: AttachmentService, database: FakeDatabase, storage: FakeObjectStorage, world: tuple[UUID, UUID, UUID], ) -> None: """Сценарий «Компенсация различает исход фиксации»: упало выражение.""" database.fail_on["jobs.insert_job"] = UNAVAILABLE() with pytest.raises(UNAVAILABLE): await _upload(service, world) row = next(iter(database.attachments.values())) assert row.status == AttachmentStatus.PENDING assert storage.objects == {} assert "storage.delete" in database.events assert database.jobs == {} async def test_failure_on_commit_keeps_object( service: AttachmentService, database: FakeDatabase, storage: FakeObjectStorage, world: tuple[UUID, UUID, UUID], ) -> None: """Тот же сценарий: упала фиксация, исход неизвестен, объект на месте.""" database.fail_commit = UNAVAILABLE() # первая фиксация — строка pending database.commits_before_fail = 1 with pytest.raises(UNAVAILABLE): await _upload(service, world) row = next(iter(database.attachments.values())) assert row.staging_key in storage.objects assert "storage.delete" not in database.events async def test_removed_row_compensates_and_503( uow: FakeUnitOfWork, cache: FakeCache, database: FakeDatabase, storage: FakeObjectStorage, world: tuple[UUID, UUID, UUID], ) -> None: """`mark_queued` → `None`: уборщик пометил строку.""" class Marking(FakeObjectStorage): async def put(self, key, body, *, size, content_type) -> None: await super().put(key, body, size=size, content_type=content_type) row = next(iter(database.attachments.values())) database.attachments[row.id] = replace( row, updated_at=row.created_at.replace(year=2100), ) marking = Marking(database) service = AttachmentService(uow, cache, marking, _slots()) with pytest.raises(UNAVAILABLE): await _upload(service, world) assert marking.objects == {} assert database.jobs == {} assert storage.objects == {} async def test_compensation_failure_keeps_original_error( service: AttachmentService, database: FakeDatabase, world: tuple[UUID, UUID, UUID], ) -> None: database.fail_on["jobs.insert_job"] = UNAVAILABLE("db") database.fail_on["storage.delete"] = exceptions.ObjectStorageUnavailable() with pytest.raises(UNAVAILABLE) as error: await _upload(service, world) assert not isinstance(error.value, exceptions.ObjectStorageUnavailable) async def _row( uow: FakeUnitOfWork, world: tuple[UUID, UUID, UUID], *, status: AttachmentStatus, ): alice, _, chat_id = world row = attachment_dto(chat_id=chat_id, owner_id=alice, status=status) async with uow: await uow.attachments.insert_attachment(attachment=row) await uow.commit() return row async def test_metadata_hidden_and_variants_only_ready( service: AttachmentService, uow: FakeUnitOfWork, world: tuple[UUID, UUID, UUID], ) -> None: alice, bob, _ = world pending = await _row(uow, world, status=AttachmentStatus.PENDING) queued = await _row(uow, world, status=AttachmentStatus.QUEUED) ready = await _row(uow, world, status=AttachmentStatus.READY) variant = variant_dto(attachment_id=ready.id) async with uow: await uow.variants.insert_variant( attachment_id=ready.id, variant=variant, ) await uow.variants.insert_variant( attachment_id=queued.id, variant=variant_dto(attachment_id=queued.id), ) await uow.commit() with pytest.raises(exceptions.AttachmentNotFound): await service.get_attachment(attachment_id=pending.id, user_id=alice) with pytest.raises(exceptions.AttachmentNotFound): await service.get_attachment(attachment_id=uuid4(), user_id=alice) with pytest.raises(exceptions.AttachmentNotFound): await service.get_attachment(attachment_id=ready.id, user_id=bob) shown = await service.get_attachment(attachment_id=ready.id, user_id=alice) assert shown.variants == (variant,) waiting = await service.get_attachment( attachment_id=queued.id, user_id=alice, ) assert waiting.variants == () async def test_open_content_branches( service: AttachmentService, uow: FakeUnitOfWork, database: FakeDatabase, storage: FakeObjectStorage, world: tuple[UUID, UUID, UUID], ) -> None: alice, _, _ = world queued = await _row(uow, world, status=AttachmentStatus.QUEUED) with pytest.raises(exceptions.AttachmentNotReady): await service.open_content(attachment_id=queued.id, user_id=alice) ready = await _row(uow, world, status=AttachmentStatus.READY) # нет варианта with pytest.raises(exceptions.AttachmentNotFound): await service.open_content( attachment_id=ready.id, user_id=alice, variant=AttachmentVariantName.CROP, ) # нет объекта with pytest.raises(exceptions.AttachmentContentMissing): await service.open_content(attachment_id=ready.id, user_id=alice) # другой размер storage.seed(ready.object_key, b"different size") with pytest.raises(exceptions.AttachmentContentMissing): await service.open_content(attachment_id=ready.id, user_id=alice) # целый объект storage.seed(ready.object_key, b"content") content = await service.open_content(attachment_id=ready.id, user_id=alice) assert b"".join([chunk async for chunk in content.chunks]) == b"content" await content.close() # потерянный — 404 без обращения к S3 async with uow: await uow.attachments.mark_lost( attachment_id=ready.id, object_key=ready.object_key, lost_at=NOW, ) await uow.commit() database.events.clear() with pytest.raises(exceptions.AttachmentNotFound): await service.open_content(attachment_id=ready.id, user_id=alice) assert "storage.get" not in database.events async def test_failed_attachment_metadata_has_code( service: AttachmentService, uow: FakeUnitOfWork, world: tuple[UUID, UUID, UUID], ) -> None: alice, _, _ = world row = await _row(uow, world, status=AttachmentStatus.QUEUED) async with uow: await uow.attachments.commit_failed( attachment_id=row.id, error_code=AttachmentErrorCode.INVALID_IMAGE, updated_at=NOW, ) await uow.commit() shown = await service.get_attachment(attachment_id=row.id, user_id=alice) assert shown.error_code == AttachmentErrorCode.INVALID_IMAGE with pytest.raises(exceptions.AttachmentNotReady): await service.open_content(attachment_id=row.id, user_id=alice)