Forward the lab 5 foundation while preserving lab 6 telemetry
Source: immutable lab5 snapshot f88e9ba43cdfd7eb6a5a7ae5feb4247b4e820071; forward the allowed diff from 64d5c67. Carry SQLAlchemy, Alembic, reliability fixes, versioned jobs, infrastructure and student tests without changing protected coursework files.
Validation: make check, validate, lint; 515 student tests passed with one Linux-only skip; public smoke with telemetry enabled and repeat migration passed. Stop the owned stack and retain volumes. Preserve the existing OTel work in progress; grade implementation remains a separate phase.
This commit is contained in:
@@ -9,13 +9,12 @@ from messenger.identity.domain.errors import InvalidUserData
|
||||
from messenger.identity.domain.models import Session, User, token_digest
|
||||
from messenger.identity.domain.values import DisplayName, Password, Username
|
||||
from messenger.messaging.domain.errors import (
|
||||
ChatNotFound,
|
||||
IdempotencyConflict,
|
||||
InvalidAttachmentList,
|
||||
InvalidChatData,
|
||||
InvalidMessageText,
|
||||
)
|
||||
from messenger.messaging.domain.models import Chat, Receipt
|
||||
from messenger.messaging.domain.models import Chat, Message, Receipt, attachment_list
|
||||
from messenger.messaging.domain.values import MessageText
|
||||
from messenger.shared.infrastructure.clock import MonotonicUtcClock
|
||||
|
||||
@@ -90,17 +89,6 @@ def test_message_text_rejects_whitespace() -> None:
|
||||
assert MessageText("").value == ""
|
||||
|
||||
|
||||
def make_chat(*members: object) -> Chat:
|
||||
ids = [uuid4() for _ in members] if members else [uuid4()]
|
||||
return Chat.create(
|
||||
chat_id=uuid4(),
|
||||
title="Чат",
|
||||
member_ids=ids,
|
||||
creator_id=ids[0],
|
||||
created_at=MOMENT,
|
||||
)
|
||||
|
||||
|
||||
def test_chat_requires_creator_in_members() -> None:
|
||||
members = [uuid4(), uuid4()]
|
||||
with pytest.raises(InvalidChatData):
|
||||
@@ -137,87 +125,38 @@ def test_chat_rejects_more_than_hundred_members() -> None:
|
||||
)
|
||||
|
||||
|
||||
def test_outsider_cannot_post() -> None:
|
||||
chat = make_chat(1)
|
||||
with pytest.raises(ChatNotFound):
|
||||
chat.post(message_id=uuid4(), sender_id=uuid4(), text="привет", now=MOMENT)
|
||||
|
||||
|
||||
def test_post_never_moves_history_back() -> None:
|
||||
chat = make_chat(1)
|
||||
sender = chat.member_ids[0]
|
||||
first = chat.post(
|
||||
message_id=uuid4(), sender_id=sender, text="первое", now=MOMENT + timedelta(seconds=1)
|
||||
)
|
||||
second = chat.post(message_id=uuid4(), sender_id=sender, text="второе", now=MOMENT)
|
||||
assert second.created_at == first.created_at + timedelta(microseconds=1)
|
||||
assert chat.last_message_at == second.created_at
|
||||
|
||||
|
||||
def test_post_keeps_clock_when_it_is_ahead() -> None:
|
||||
chat = make_chat(1)
|
||||
sender = chat.member_ids[0]
|
||||
first = chat.post(message_id=uuid4(), sender_id=sender, text="первое", now=MOMENT)
|
||||
later = MOMENT + timedelta(seconds=5)
|
||||
second = chat.post(message_id=uuid4(), sender_id=sender, text="второе", now=later)
|
||||
assert first.created_at == MOMENT
|
||||
assert second.created_at == later
|
||||
|
||||
|
||||
def test_post_validates_membership_before_text() -> None:
|
||||
chat = make_chat(1)
|
||||
with pytest.raises(ChatNotFound):
|
||||
chat.post(message_id=uuid4(), sender_id=uuid4(), text="", now=MOMENT)
|
||||
|
||||
|
||||
def test_empty_text_needs_an_attachment() -> None:
|
||||
chat = make_chat(1)
|
||||
sender = chat.member_ids[0]
|
||||
with pytest.raises(InvalidMessageText):
|
||||
chat.post(message_id=uuid4(), sender_id=sender, text="", now=MOMENT)
|
||||
ids = (uuid4(), uuid4())
|
||||
message = chat.post(
|
||||
message_id=uuid4(), sender_id=sender, text="", now=MOMENT, attachment_ids=ids
|
||||
)
|
||||
assert message.attachment_ids == ids
|
||||
with pytest.raises(InvalidMessageText):
|
||||
chat.post(message_id=uuid4(), sender_id=sender, text=" ", now=MOMENT, attachment_ids=ids)
|
||||
attachment_list(MessageText(""), ())
|
||||
assert attachment_list(MessageText(""), ids) == ids
|
||||
|
||||
|
||||
@pytest.mark.parametrize("ids", [[uuid4() for _ in range(11)], [UUID_TWICE, UUID_TWICE]])
|
||||
def test_attachment_list_is_bounded_and_unique(ids: list[UUID]) -> None:
|
||||
chat = make_chat(1)
|
||||
with pytest.raises(InvalidAttachmentList):
|
||||
chat.post(
|
||||
message_id=uuid4(),
|
||||
sender_id=chat.member_ids[0],
|
||||
text="x",
|
||||
now=MOMENT,
|
||||
attachment_ids=ids,
|
||||
)
|
||||
|
||||
|
||||
def test_membership_is_checked_before_attachments() -> None:
|
||||
chat = make_chat(1)
|
||||
with pytest.raises(ChatNotFound):
|
||||
chat.post(
|
||||
message_id=uuid4(),
|
||||
sender_id=uuid4(),
|
||||
text="",
|
||||
now=MOMENT,
|
||||
attachment_ids=[uuid4() for _ in range(11)],
|
||||
)
|
||||
attachment_list(MessageText("x"), ids)
|
||||
|
||||
|
||||
def test_receipt_replays_only_the_same_request() -> None:
|
||||
chat = make_chat(1)
|
||||
message = chat.post(message_id=uuid4(), sender_id=chat.member_ids[0], text="раз", now=MOMENT)
|
||||
message = Message(uuid4(), uuid4(), uuid4(), MessageText("раз"), MOMENT)
|
||||
receipt = Receipt("key-1", b"a" * 32, message)
|
||||
assert receipt.replay(b"a" * 32) is message
|
||||
with pytest.raises(IdempotencyConflict):
|
||||
receipt.replay(b"b" * 32)
|
||||
|
||||
|
||||
def test_posted_message_describes_itself() -> None:
|
||||
message = Message(uuid4(), uuid4(), uuid4(), MessageText("пять"), MOMENT)
|
||||
event = message.posted()
|
||||
assert (event.message_id, event.chat_id, event.sender_id) == (
|
||||
message.id,
|
||||
message.chat_id,
|
||||
message.sender_id,
|
||||
)
|
||||
assert (event.text_length, event.created_at, event.name) == (4, MOMENT, "message.posted")
|
||||
|
||||
|
||||
def test_user_registration_emits_event() -> None:
|
||||
user, event = User.register(
|
||||
user_id=uuid4(),
|
||||
|
||||
Reference in New Issue
Block a user