Files
mtusi2026lab5/student-tests/test_chat_cache.py
T
ricoromaandClaude Opus 5.5 04f859282d chore: port lab 4 solution into the lab 5 skeleton
Source: mtusi2026lab4-safronov, solution/bvt2301-safronov, 2546deb.
Contracts, public tests and evidence of lab 4 are not carried over.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-31 03:09:12 +03:00

79 lines
2.7 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""Спек `chat-cache`: что видно по HTTP при кэше карточки чата.
Попадание в кэш, срок записи и отказ Valkey по HTTP не различить: это
исключения в `spec_coverage.py` с evidence.
"""
from typing import Any
import pytest
from api_client import API, Actor, Api
from contract import assert_error, assert_ok
@pytest.mark.spec("chat-cache", "Повторное чтение чата")
def test_repeated_reads_match_creation(
api: Api,
alice: Actor,
bob: Actor,
eve: Actor,
) -> None:
# порядок участников не совпадает ни с сортировкой, ни с порядком
# регистрации: кэш обязан сохранить его как есть
created = api.create_chat(bob, eve, alice, title="Кэш «карточки»")
path = f"{API}/chats/{created['id']}"
first = assert_ok(api.request("GET", path, actor=alice), 200, "Chat")
second = assert_ok(api.request("GET", path, actor=eve), 200, "Chat")
assert first == second == created
assert first["member_ids"] == [bob.id, eve.id, alice.id]
@pytest.mark.spec("chat-cache", "Проверка членства по карточке")
def test_membership_from_new_chat(
api: Api,
alice: Actor,
bob: Actor,
eve: Actor,
) -> None:
chat = api.create_chat(alice, bob)
api.send_message(bob, chat["id"], "сразу после создания")
path = f"{API}/chats/{chat['id']}"
assert_error(api.request("GET", path, actor=eve), 404)
assert_error(
api.post_message(eve, chat["id"], json={"text": "посторонний"}),
404,
)
@pytest.mark.spec("chat-cache", "Сообщение видно сразу")
def test_message_visible_at_once(
api: Api,
alice: Actor,
bob: Actor,
chat: dict[str, Any],
) -> None:
# история уже прочитана: если бы она кэшировалась, второе чтение
# отдало бы старую страницу
assert api.all_messages(bob, chat["id"]) == []
message = api.send_message(alice, chat["id"], "видно сразу")
assert api.all_messages(bob, chat["id"]) == [message]
@pytest.mark.spec("chat-cache", "Новый чат в списке")
def test_new_chat_listed_at_once(
api: Api,
alice: Actor,
bob: Actor,
chat: dict[str, Any],
) -> None:
listed = [
item["id"] for page in api.chat_pages(bob) for item in page["items"]
]
assert listed == [chat["id"]]
second = api.create_chat(alice, bob, title="Второй")
listed = [
item["id"] for page in api.chat_pages(bob) for item in page["items"]
]
assert listed == [chat["id"], second["id"]]