Files
mtusi2026lab6/service-tests/unit/test_attachment_replay.py
T
ricoromaandClaude Opus 5.5 9cc6c0c7a6 chore: port lab 5 solution into the lab 6 skeleton
Source: mtusi2026lab5-safronov, solution/bvt2301-safronov, 046ec9e.
Contracts, public tests and evidence of lab 5 are not carried over.
The compose project, image and CI prefix are renamed to lab6; the first
broker start defect is recorded in KNOWN_ISSUES.md; PLAN.md outlines the
OpenSpec changes of lab 6.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-11 04:46:56 +03:00

242 lines
7.1 KiB
Python

"""`AttachmentReplayService`: таблица отказов, аудит одним блоком, показ."""
from uuid import UUID, uuid4
import pytest
from builders import NOW, attachment_dto, job_dto
from fakes import FakeDatabase, FakeUnitOfWork
from src.core import exceptions
from src.core.services.attachment_replay import (
REFUSAL_ALREADY_READY,
REFUSAL_NOT_FAILED,
REFUSAL_NOT_FOUND,
REFUSAL_SOURCE_GONE,
AttachmentReplayService,
)
from src.schemas.enums import (
AttachmentErrorCode,
AttachmentJobReason,
AttachmentJobState,
AttachmentStatus,
)
from src.utils.constants import ATTACHMENT_PIPELINE_VERSION
pytestmark = pytest.mark.anyio
@pytest.fixture
def service(uow: FakeUnitOfWork) -> AttachmentReplayService:
return AttachmentReplayService(uow)
async def _row(
uow: FakeUnitOfWork,
*,
status: AttachmentStatus = AttachmentStatus.QUEUED,
job: bool = True,
failed: bool = False,
staging: bool = True,
code: AttachmentErrorCode = AttachmentErrorCode.INVALID_IMAGE,
reason: AttachmentJobReason = AttachmentJobReason.UNDECODABLE,
) -> UUID:
row = attachment_dto(
chat_id=uuid4(),
owner_id=uuid4(),
status=status,
staging=staging,
)
async with uow:
await uow.attachments.insert_attachment(attachment=row)
if job:
created = job_dto(
attachment_id=row.id,
pipeline_version=ATTACHMENT_PIPELINE_VERSION,
)
await uow.jobs.insert_job(job=created)
if failed:
await uow.attachments.commit_failed(
attachment_id=row.id,
error_code=code,
updated_at=NOW,
)
await uow.jobs.finish_job(
job_id=created.id,
state=AttachmentJobState.FAILED,
reason=reason,
now=NOW,
)
await uow.commit()
return row.id
async def _replay(service: AttachmentReplayService, attachment_id: UUID):
return await service.replay_one(
attachment_id=attachment_id,
apply=True,
operator="op",
)
async def test_refusals_change_nothing(
service: AttachmentReplayService,
uow: FakeUnitOfWork,
database: FakeDatabase,
) -> None:
ready = await _row(uow, status=AttachmentStatus.READY)
no_job = await _row(uow, job=False)
queued = await _row(uow)
gone = await _row(uow, failed=True, staging=False)
before = database.snapshot()
assert (await _replay(service, uuid4())).refusal == REFUSAL_NOT_FOUND
assert (await _replay(service, no_job)).refusal == REFUSAL_NOT_FOUND
assert (await _replay(service, ready)).refusal == REFUSAL_ALREADY_READY
assert (await _replay(service, queued)).refusal == REFUSAL_NOT_FAILED
assert (await _replay(service, gone)).refusal == REFUSAL_SOURCE_GONE
assert database.snapshot() == before
assert "commit" not in database.events[-20:]
async def test_apply_returns_job_with_audit_in_one_block(
service: AttachmentReplayService,
uow: FakeUnitOfWork,
database: FakeDatabase,
) -> None:
attachment_id = await _row(uow, failed=True)
database.events.clear()
result = await _replay(service, attachment_id)
assert result.refusal is None
assert result.error_code == AttachmentErrorCode.INVALID_IMAGE.value
assert result.reason == AttachmentJobReason.UNDECODABLE.value
assert database.events == [
"enter",
"jobs.get_attachment_job",
"jobs.lock_job",
"attachments.lock_attachment",
"attachments.replay_failed",
"jobs.replay_job",
"job_replays.insert_replay",
"commit",
"exit",
]
row = database.attachments[attachment_id]
job = next(iter(database.jobs.values()))
audit = next(iter(database.job_replays.values()))
assert row.status == AttachmentStatus.QUEUED
assert row.error_code is None
assert job.state == AttachmentJobState.QUEUED
assert job.attempt_base == job.attempt
assert audit.operator == "op"
assert audit.error_code == AttachmentErrorCode.INVALID_IMAGE.value
async def test_audit_failure_rolls_back_all(
service: AttachmentReplayService,
uow: FakeUnitOfWork,
database: FakeDatabase,
) -> None:
attachment_id = await _row(uow, failed=True)
database.fail_on["job_replays.insert_replay"] = (
exceptions.ServiceUnavailable()
)
with pytest.raises(exceptions.ServiceUnavailable):
await _replay(service, attachment_id)
assert database.attachments[attachment_id].status == (
AttachmentStatus.FAILED
)
assert next(iter(database.jobs.values())).state == (
AttachmentJobState.FAILED
)
async def test_show_without_apply_changes_nothing(
service: AttachmentReplayService,
uow: FakeUnitOfWork,
database: FakeDatabase,
) -> None:
await _row(uow, failed=True)
before = database.snapshot()
result = await service.replay_pass(
attachment_ids=[],
error_code=None,
reason=None,
limit=10,
apply=False,
operator="op",
)
assert (result.shown, result.replayed, result.refused) == (1, 0, 0)
assert database.snapshot() == before
async def test_selection_by_code_and_reason(
service: AttachmentReplayService,
uow: FakeUnitOfWork,
database: FakeDatabase,
) -> None:
invalid = await _row(uow, failed=True)
other = await _row(
uow,
failed=True,
code=AttachmentErrorCode.PROCESSING_FAILED,
reason=AttachmentJobReason.SOURCE_MISSING,
)
result = await service.replay_pass(
attachment_ids=[],
error_code=AttachmentErrorCode.INVALID_IMAGE.value,
reason=None,
limit=10,
apply=True,
operator="op",
)
assert result.replayed == 1
assert database.attachments[invalid].status == AttachmentStatus.QUEUED
assert database.attachments[other].status == AttachmentStatus.FAILED
result = await service.replay_pass(
attachment_ids=[],
error_code=None,
reason=AttachmentJobReason.SOURCE_MISSING.value,
limit=10,
apply=False,
operator="op",
)
assert result.shown == 1
async def test_by_id_counts_refusals_and_limit(
service: AttachmentReplayService,
uow: FakeUnitOfWork,
) -> None:
good = await _row(uow, failed=True)
bad = await _row(uow)
third = await _row(uow, failed=True)
result = await service.replay_pass(
attachment_ids=[good, bad, third],
error_code=None,
reason=None,
limit=2,
apply=True,
operator="op",
)
assert (result.replayed, result.refused) == (1, 1)
async def test_stop_signal_interrupts(
service: AttachmentReplayService,
uow: FakeUnitOfWork,
database: FakeDatabase,
) -> None:
attachment_id = await _row(uow, failed=True)
result = await service.replay_pass(
attachment_ids=[attachment_id],
error_code=None,
reason=None,
limit=10,
apply=True,
operator="op",
stop_requested=lambda: True,
)
assert result.interrupted
assert database.attachments[attachment_id].status == (
AttachmentStatus.FAILED
)