"""Общая обвязка собственных проверок лабораторной 3. Публичный клиент из tests/ переиспользуется, а не копируется: он уже проверяет схему ответа, X-Request-Id, X-Instance-Id, конверт ошибки и challenge. Здесь живут только помощники, специфичные для балансировки и идемпотентности. """ from __future__ import annotations import os import sys import time import uuid from pathlib import Path ROOT = Path(__file__).resolve().parents[1] TESTS = ROOT / "tests" HERE = Path(__file__).resolve().parent for candidate in (str(TESTS), str(HERE)): if candidate not in sys.path: sys.path.insert(0, candidate) from client import Client, Failure # noqa: E402 DEFAULT_BASE_URL = "http://127.0.0.1:8080" DEFAULT_TIMEOUT = 10.0 def client(timeout: float = DEFAULT_TIMEOUT) -> Client: return Client(os.environ.get("BASE_URL", DEFAULT_BASE_URL), None, timeout) def instance_ids(api: Client, actor: dict, count: int = 40) -> set[str]: """Собирает X-Instance-Id разных ответов через LB, не доверяя одному запросу.""" seen: set[str] = set() for _ in range(count): seen.add( api.call("GET", "/api/v1/users/me", actor=actor, schema="User")["headers"][ "X-Instance-Id" ] ) return seen def wait_ready(timeout: float = 120.0) -> None: api = client(timeout=5) deadline = time.monotonic() + timeout last = "no attempts" while time.monotonic() < deadline: try: api.call("GET", "/health/ready", schema="Health") return except (Failure, OSError) as exc: last = str(exc) time.sleep(0.5) raise Failure("stand did not become ready within %.0fs: %s" % (timeout, last)) def history(api: Client, actor: dict, chat_id: str) -> list[dict]: items: list[dict] = [] cursor = None for _ in range(1000): path = "/api/v1/chats/%s/messages?limit=100" % chat_id if cursor is not None: path += "&cursor=" + cursor page = api.call("GET", path, actor=actor, schema="MessagePage")["json"] items.extend(page["items"]) cursor = page["next_cursor"] if cursor is None: return items raise Failure("history pagination exceeded safety bound") def unique_key() -> str: return uuid.uuid4().hex