Copy the committed lab 1 sources as-is: src/, student-tests/, bench/, Dockerfile, docker-compose.yml, pyproject.toml, uv.lock, .python-version, config_example.toml, AGENTS.md, CLAUDE.md, .claude/, openspec/, .graphifyignore and KNOWN_ISSUES.md. Lab 1 evidence stays in the lab 1 repository. Merge the new skeleton files with own sections: Makefile (lab 2 targets plus own run, docker, bench and common targets), .gitignore, .dockerignore and .env.example. Rename the image to mtusi-messenger:lab2 and point AGENTS.md at lab 2. The application is still the in-memory lab 1 implementation. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
111 lines
3.1 KiB
Python
111 lines
3.1 KiB
Python
"""Спек `messages`: отправка, валидация текста, история."""
|
|
|
|
from typing import Any
|
|
|
|
import httpx
|
|
import pytest
|
|
|
|
from api_client import API, Actor, Api, order_key
|
|
from contract import assert_error, assert_ok
|
|
|
|
|
|
def send_raw(
|
|
api: Api,
|
|
actor: Actor,
|
|
chat_id: str,
|
|
body: dict[str, Any],
|
|
) -> httpx.Response:
|
|
return api.request(
|
|
"POST",
|
|
f"{API}/chats/{chat_id}/messages",
|
|
actor=actor,
|
|
json=body,
|
|
)
|
|
|
|
|
|
@pytest.mark.spec("messages", "Участник отправляет сообщение")
|
|
def test_member_sends(
|
|
api: Api,
|
|
alice: Actor,
|
|
chat: dict[str, Any],
|
|
) -> None:
|
|
resp = send_raw(api, alice, chat["id"], {"text": "Привет, мир 👋"})
|
|
message = assert_ok(resp, 201, "Message")
|
|
assert message["sender_id"] == alice.id
|
|
assert message["chat_id"] == chat["id"]
|
|
assert message["text"] == "Привет, мир 👋"
|
|
|
|
|
|
@pytest.mark.spec("messages", "Граница длины")
|
|
def test_text_length(api: Api, alice: Actor, chat: dict[str, Any]) -> None:
|
|
# кириллица: 2 байта на символ, считаются code points
|
|
text = "я" * 4000
|
|
message = assert_ok(
|
|
send_raw(api, alice, chat["id"], {"text": text}),
|
|
201,
|
|
"Message",
|
|
)
|
|
assert message["text"] == text
|
|
assert_error(send_raw(api, alice, chat["id"], {"text": text + "я"}), 400)
|
|
assert [item["id"] for item in api.all_messages(alice, chat["id"])] == [
|
|
message["id"],
|
|
]
|
|
|
|
|
|
@pytest.mark.spec("messages", "Попытка подменить отправителя")
|
|
def test_sender_id_in_body(
|
|
api: Api,
|
|
alice: Actor,
|
|
bob: Actor,
|
|
chat: dict[str, Any],
|
|
) -> None:
|
|
body = {"text": "от Боба", "sender_id": bob.id}
|
|
assert_error(send_raw(api, alice, chat["id"], body), 400)
|
|
assert api.all_messages(bob, chat["id"]) == []
|
|
|
|
|
|
@pytest.mark.spec("messages", "Два участника, две страницы")
|
|
def test_two_members_two_pages(
|
|
api: Api,
|
|
alice: Actor,
|
|
bob: Actor,
|
|
chat: dict[str, Any],
|
|
) -> None:
|
|
sent = [
|
|
api.send_message(alice, chat["id"], "от Алисы"),
|
|
api.send_message(bob, chat["id"], "от Боба"),
|
|
]
|
|
first = assert_ok(
|
|
api.messages_page(bob, chat["id"], limit=1),
|
|
200,
|
|
"MessagePage",
|
|
)
|
|
assert len(first["items"]) == 1
|
|
assert first["next_cursor"]
|
|
second = assert_ok(
|
|
api.messages_page(
|
|
bob,
|
|
chat["id"],
|
|
limit=1,
|
|
cursor=first["next_cursor"],
|
|
),
|
|
200,
|
|
"MessagePage",
|
|
)
|
|
assert len(second["items"]) == 1
|
|
assert second["next_cursor"] is None
|
|
assert first["items"] + second["items"] == sorted(sent, key=order_key)
|
|
|
|
|
|
@pytest.mark.spec("messages", "Чтение после записи")
|
|
def test_read_after_write(
|
|
api: Api,
|
|
alice: Actor,
|
|
bob: Actor,
|
|
chat: dict[str, Any],
|
|
) -> None:
|
|
for number in range(5):
|
|
message = api.send_message(alice, chat["id"], f"сразу {number}")
|
|
history = api.all_messages(bob, chat["id"])
|
|
assert message in history
|