Files
mtusi2026lab2/student-tests/test_chats.py
T
ricoromaandClaude Opus 5.5 7b775a29a9 chore: carry over lab 1 solution into lab 2 skeleton
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>
2026-09-30 22:11:46 +03:00

127 lines
4.0 KiB
Python

"""Спек `chats`: создание, чтение, список, неизменный состав."""
import uuid
from typing import Any
import httpx
import pytest
from api_client import API, Actor, Api
from contract import assert_error, assert_ok
def create_raw(
api: Api,
actor: Actor,
title: str,
member_ids: list[str],
) -> httpx.Response:
return api.request(
"POST",
f"{API}/chats",
actor=actor,
json={"title": title, "member_ids": member_ids},
)
def listed_ids(api: Api, actor: Actor) -> set[str]:
return {
chat["id"] for page in api.chat_pages(actor) for chat in page["items"]
}
def listed_titles(api: Api, actor: Actor) -> set[str]:
return {
chat["title"]
for page in api.chat_pages(actor)
for chat in page["items"]
}
@pytest.mark.spec("chats", "Успешное создание")
def test_create_chat(api: Api, alice: Actor, bob: Actor) -> None:
resp = create_raw(api, alice, "Проект", [alice.id, bob.id])
chat = assert_ok(resp, 201, "Chat")
assert chat["member_ids"] == [alice.id, bob.id]
assert chat["title"] == "Проект"
# порядок участников сохраняется, даже если создатель не первый
resp = create_raw(api, alice, "Проект 2", [bob.id, alice.id])
reversed_chat = assert_ok(resp, 201, "Chat")
assert reversed_chat["member_ids"] == [bob.id, alice.id]
assert reversed_chat["id"] != chat["id"]
@pytest.mark.spec("chats", "Вызывающего нет в списке")
def test_caller_not_in_members(api: Api, alice: Actor, bob: Actor) -> None:
title = f"rejected-{uuid.uuid4().hex}"
assert_error(create_raw(api, alice, title, [bob.id]), 400)
# отклонённый чат не появился ни в одном списке
assert title not in listed_titles(api, alice)
assert title not in listed_titles(api, bob)
@pytest.mark.spec("chats", "Повтор участника")
def test_duplicate_member(api: Api, alice: Actor) -> None:
assert_error(create_raw(api, alice, "Dup", [alice.id, alice.id]), 400)
@pytest.mark.spec("chats", "Неизвестный участник")
def test_unknown_member(api: Api, alice: Actor) -> None:
title = f"rejected-{uuid.uuid4().hex}"
member_ids = [alice.id, str(uuid.uuid4())]
assert_error(create_raw(api, alice, title, member_ids), 400)
assert title not in listed_titles(api, alice)
@pytest.mark.spec("chats", "Пустой список")
def test_empty_members(api: Api, alice: Actor) -> None:
assert_error(create_raw(api, alice, "Empty", []), 400)
@pytest.mark.spec("chats", "Участник")
def test_member_reads_chat(
api: Api,
bob: Actor,
chat: dict[str, Any],
) -> None:
resp = api.request("GET", f"{API}/chats/{chat['id']}", actor=bob)
assert assert_ok(resp, 200, "Chat") == chat
@pytest.mark.spec("chats", "Неверный chat_id")
def test_invalid_chat_id(api: Api, alice: Actor) -> None:
assert_error(api.request("GET", f"{API}/chats/nope", actor=alice), 400)
@pytest.mark.spec("chats", "Участник видит чат")
def test_member_lists_chat(
api: Api,
bob: Actor,
chat: dict[str, Any],
) -> None:
assert chat["id"] in listed_ids(api, bob)
@pytest.mark.spec("chats", "Посторонний не видит чат")
def test_outsider_does_not_list_chat(
api: Api,
eve: Actor,
chat: dict[str, Any],
) -> None:
assert chat["id"] not in listed_ids(api, eve)
@pytest.mark.spec("chats", "Повторное чтение")
def test_members_do_not_change(
api: Api,
alice: Actor,
bob: Actor,
chat: dict[str, Any],
) -> None:
path = f"{API}/chats/{chat['id']}"
first = assert_ok(api.request("GET", path, actor=alice), 200, "Chat")
api.send_message(bob, chat["id"], "между чтениями")
api.create_chat(alice, bob)
second = assert_ok(api.request("GET", path, actor=bob), 200, "Chat")
assert first["member_ids"] == second["member_ids"] == chat["member_ids"]