- carry the lab 2 solution over and plan lab 3 (PLAN.md, OpenSpec change instance-id-idempotency) - rename the compose project to mtusi-lab3 and the image to :lab3 - add X-Instance-Id to every response and instance_id to every log record ([app] instance_id, hostname by default) - require Idempotency-Key on POST messages: keys table in Postgres, key and message in one transaction, replay returns the same 201, another body gets 409, TTL from [idempotency] ttl_seconds - send the key from student-tests, k6 scenarios and the skill driver; add idempotency, X-Instance-Id, key race and fault tests Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
107 lines
3.1 KiB
Python
107 lines
3.1 KiB
Python
"""Спек `messages`: отправка, валидация текста, история."""
|
|
|
|
from typing import Any
|
|
|
|
import httpx
|
|
import pytest
|
|
|
|
from api_client import 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.post_message(actor, chat_id, 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
|