feat: carry lab 2 over and add X-Instance-Id and idempotency keys

- 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>
This commit is contained in:
2026-10-08 19:42:36 +03:00
co-authored by Claude Opus 5.5
parent 0ab3a341aa
commit af4bca08c9
294 changed files with 36586 additions and 15 deletions
+78
View File
@@ -0,0 +1,78 @@
"""Спек `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"]]