forked from mtusi2026/mtusi2026lab5
Upload now stages the checked original under staging/, and one transaction moves the intent to queued, inserts the job (pipeline version 1, attempt 1), its outbox row and an audit event; the 202 is the in-memory snapshot, never a reread. Content answers 409 until the attachment is ready, the thumbnail variant is served from the published version and absent variants are 404. The relay publishes outbox rows through FastStream with publisher confirms, mandatory routing and persistent delivery, marks a row only after the broker acked it, returns jobs whose lease expired and deletes the staging object of ready attachments. The worker consumes the quorum queue through a FastStream subscriber that declares nothing, takes prefetch from WORKER_CONCURRENCY and acks manually after the result commit, rejects messages off jobs.schema.json to the dead letter exchange and requeues after an infrastructure error. A job is claimed under a lease token; the original is copied to originals/ and a 256 px JPEG thumbnail is written under a content-addressed key; the commit is fenced by the token, inserts variants with ON CONFLICT DO NOTHING and publishes only a newer pipeline version. A transient error reschedules the same job with an exponential delay and a new outbox row, the last attempt fails with retry_exhausted, an undecodable image fails at once with invalid_image, and a redelivered finished job is a recorded no-op. The worker and the relay are assembled in worker.py and relay.py with the dishka FastStream integration and the platform providers of the API; the shared core imports neither FastStream nor aio-pika. Tests cover the thumbnail geometry and rendering, the job message against the contract, the pipeline against PostgreSQL (duplicates, parallel copies, retries, the attempt bound, leases and fencing) and the broker adapters against a throwaway RabbitMQ with the stand's topology and permissions. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
290 lines
9.6 KiB
Python
290 lines
9.6 KiB
Python
import ast
|
|
import re
|
|
from pathlib import Path
|
|
|
|
SOURCE = Path(__file__).resolve().parents[2] / "src" / "messenger"
|
|
IDENTITY = "messenger.identity"
|
|
MESSAGING = "messenger.messaging"
|
|
ATTACHMENTS = "messenger.attachments"
|
|
ZONES = ("identity", "messaging", "attachments")
|
|
|
|
|
|
def imports_of(path: Path) -> list[str]:
|
|
tree = ast.parse(path.read_text(), filename=str(path))
|
|
found: list[str] = []
|
|
for node in ast.walk(tree):
|
|
if isinstance(node, ast.ImportFrom) and node.module:
|
|
found.append(node.module)
|
|
elif isinstance(node, ast.Import):
|
|
found.extend(alias.name for alias in node.names)
|
|
return found
|
|
|
|
|
|
def modules_under(*parts: str) -> list[Path]:
|
|
root = SOURCE.joinpath(*parts)
|
|
return [path for path in root.rglob("*.py") if path.name != "__init__.py"]
|
|
|
|
|
|
def test_identity_does_not_know_about_messaging() -> None:
|
|
offenders = {
|
|
str(path.relative_to(SOURCE)): [m for m in imports_of(path) if m.startswith(MESSAGING)]
|
|
for path in modules_under("identity")
|
|
}
|
|
assert not {k: v for k, v in offenders.items() if v}
|
|
|
|
|
|
def test_messaging_touches_identity_only_through_events() -> None:
|
|
allowed = f"{IDENTITY}.domain.events"
|
|
offenders: dict[str, list[str]] = {}
|
|
for path in modules_under("messaging"):
|
|
borrowed = [
|
|
module
|
|
for module in imports_of(path)
|
|
if module.startswith(IDENTITY) and module != allowed
|
|
]
|
|
if borrowed:
|
|
offenders[str(path.relative_to(SOURCE))] = borrowed
|
|
assert not offenders
|
|
|
|
|
|
def test_attachments_and_other_zones_do_not_import_each_other() -> None:
|
|
offenders: dict[str, list[str]] = {}
|
|
for zone, foreign in (
|
|
("attachments", (IDENTITY, MESSAGING)),
|
|
("identity", (ATTACHMENTS,)),
|
|
("messaging", (ATTACHMENTS,)),
|
|
):
|
|
for path in modules_under(zone):
|
|
borrowed = [m for m in imports_of(path) if m.startswith(foreign)]
|
|
if borrowed:
|
|
offenders[str(path.relative_to(SOURCE))] = borrowed
|
|
assert not offenders
|
|
|
|
|
|
def test_domains_do_not_import_infrastructure() -> None:
|
|
offenders: dict[str, list[str]] = {}
|
|
for zone in (*ZONES, "shared"):
|
|
for layer in ("domain", "application"):
|
|
for path in modules_under(zone, layer):
|
|
borrowed = [m for m in imports_of(path) if ".infrastructure" in m]
|
|
if borrowed:
|
|
offenders[str(path.relative_to(SOURCE))] = borrowed
|
|
assert not offenders
|
|
|
|
|
|
def test_core_layers_do_not_import_pydantic() -> None:
|
|
offenders: dict[str, list[str]] = {}
|
|
for zone in (*ZONES, "shared"):
|
|
for layer in ("domain", "application"):
|
|
for path in modules_under(zone, layer):
|
|
borrowed = [m for m in imports_of(path) if m.split(".")[0] == "pydantic"]
|
|
if borrowed:
|
|
offenders[str(path.relative_to(SOURCE))] = borrowed
|
|
assert not offenders, "схемы pydantic живут на границе: в entrypoints/ и config.py"
|
|
|
|
|
|
def test_shared_knows_nothing_about_zones() -> None:
|
|
offenders: dict[str, list[str]] = {}
|
|
for path in modules_under("shared"):
|
|
borrowed = [
|
|
module
|
|
for module in imports_of(path)
|
|
if module.startswith((IDENTITY, MESSAGING, ATTACHMENTS))
|
|
]
|
|
if borrowed:
|
|
offenders[str(path.relative_to(SOURCE))] = borrowed
|
|
assert not offenders
|
|
|
|
|
|
TRANSPORT = ("fastapi", "starlette", "uvicorn", "gunicorn")
|
|
|
|
|
|
def test_shared_core_is_free_of_transport() -> None:
|
|
offenders: dict[str, list[str]] = {}
|
|
for layer in ("domain", "application", "infrastructure"):
|
|
for path in modules_under("shared", layer):
|
|
borrowed = [module for module in imports_of(path) if module.split(".")[0] in TRANSPORT]
|
|
if borrowed:
|
|
offenders[str(path.relative_to(SOURCE))] = borrowed
|
|
assert not offenders, "ядро shared обязано жить без веб-фреймворка"
|
|
|
|
|
|
def test_shared_core_holds_only_platform() -> None:
|
|
names = {
|
|
path.name
|
|
for layer in ("domain", "application", "infrastructure")
|
|
for path in modules_under("shared", layer)
|
|
}
|
|
assert names == {
|
|
"errors.py",
|
|
"events.py",
|
|
"commands.py",
|
|
"messagebus.py",
|
|
"ports.py",
|
|
"clock.py",
|
|
"ids.py",
|
|
"logging.py",
|
|
"postgres.py",
|
|
}
|
|
|
|
|
|
def test_shared_entrypoints_hold_only_transport() -> None:
|
|
names = {path.name for path in modules_under("shared", "entrypoints")}
|
|
assert names == {
|
|
"actor.py",
|
|
"config.py",
|
|
"errors.py",
|
|
"health.py",
|
|
"ioc.py",
|
|
"pipeline.py",
|
|
"schemas.py",
|
|
}
|
|
|
|
|
|
def test_every_zone_owns_its_errors_events_and_commands() -> None:
|
|
for zone in ZONES:
|
|
assert (SOURCE / zone / "domain" / "errors.py").is_file()
|
|
assert (SOURCE / zone / "domain" / "events.py").is_file()
|
|
assert (SOURCE / zone / "application" / "commands.py").is_file()
|
|
|
|
|
|
def test_every_zone_owns_its_http_layer() -> None:
|
|
for zone in ZONES:
|
|
for part in ("http.py", "ioc.py", "errors.py", "journal.py"):
|
|
assert (SOURCE / zone / "entrypoints" / part).is_file(), f"{zone}: нет {part}"
|
|
|
|
|
|
PROCESSES = {"relay.py": (ATTACHMENTS,), "worker.py": (ATTACHMENTS,)}
|
|
|
|
|
|
def test_only_main_knows_several_zones() -> None:
|
|
offenders: dict[str, list[str]] = {}
|
|
for path in SOURCE.rglob("*.py"):
|
|
relative = path.relative_to(SOURCE)
|
|
if path.name in {"__init__.py", "main.py"} or relative.parts[0] in ZONES:
|
|
continue
|
|
allowed = PROCESSES.get(str(relative), ())
|
|
borrowed = [
|
|
module
|
|
for module in imports_of(path)
|
|
if module.startswith((IDENTITY, MESSAGING, ATTACHMENTS))
|
|
and not module.startswith(allowed or ("-",))
|
|
]
|
|
if borrowed:
|
|
offenders[str(relative)] = borrowed
|
|
assert not offenders
|
|
|
|
|
|
def test_broker_framework_stays_out_of_shared_and_core_layers() -> None:
|
|
offenders: dict[str, list[str]] = {}
|
|
for path in [
|
|
*modules_under("shared"),
|
|
*(
|
|
p
|
|
for zone in ZONES
|
|
for layer in ("domain", "application")
|
|
for p in modules_under(zone, layer)
|
|
),
|
|
]:
|
|
borrowed = [
|
|
m for m in imports_of(path) if m.split(".")[0] in {"faststream", "aio_pika", "aiormq"}
|
|
]
|
|
if borrowed:
|
|
offenders[str(path.relative_to(SOURCE))] = borrowed
|
|
assert not offenders
|
|
|
|
|
|
def test_application_has_no_separate_entrypoints_root() -> None:
|
|
assert not (SOURCE / "entrypoints").exists()
|
|
assert (SOURCE / "main.py").is_file()
|
|
|
|
|
|
ZONE_WORDS = (
|
|
"username",
|
|
"display_name",
|
|
"chat",
|
|
"member",
|
|
"history",
|
|
"cursor",
|
|
"sender",
|
|
"message_id",
|
|
"message_text",
|
|
"attachment",
|
|
"filename",
|
|
)
|
|
|
|
|
|
def test_shared_source_mentions_no_domain_vocabulary() -> None:
|
|
offenders: dict[str, list[str]] = {}
|
|
for path in modules_under("shared"):
|
|
source = path.read_text().lower()
|
|
hits = sorted({word for word in ZONE_WORDS if word in source})
|
|
if hits:
|
|
offenders[str(path.relative_to(SOURCE))] = hits
|
|
assert not offenders
|
|
|
|
|
|
def test_shared_declares_no_concrete_commands_or_events() -> None:
|
|
for path in modules_under("shared"):
|
|
tree = ast.parse(path.read_text(), filename=str(path))
|
|
declared = [n.name for n in ast.walk(tree) if isinstance(n, ast.ClassDef)]
|
|
concrete = [
|
|
name
|
|
for name in declared
|
|
if name not in {"Command", "DomainEvent", "DomainError"}
|
|
and (
|
|
name.endswith("Event")
|
|
or name in {"RegisterUser", "CreateChat", "PostMessage", "UploadAttachment"}
|
|
)
|
|
]
|
|
assert not concrete, f"{path.name}: {concrete}"
|
|
|
|
|
|
def test_shared_has_no_test_doubles() -> None:
|
|
for path in modules_under("shared"):
|
|
source = path.read_text()
|
|
for marker in ("Frozen", "Fake", "Stub", "Null", "Dummy"):
|
|
assert marker not in source, f"{path.name} содержит подделку {marker}"
|
|
|
|
|
|
def test_zone_errors_do_not_leak_transport_codes() -> None:
|
|
for zone in (*ZONES, "shared"):
|
|
source = (SOURCE / zone / "domain" / "errors.py").read_text()
|
|
assert "http_status" not in source
|
|
assert "invalid_request" not in source
|
|
assert "404" not in source
|
|
|
|
|
|
IDENTITY_TABLES = ("users", "sessions")
|
|
MESSAGING_TABLES = ("chats", "chat_members", "messages", "idempotency_keys", "message_attachments")
|
|
ATTACHMENT_TABLES = ("attachments", "attachment_variants", "jobs", "outbox", "job_events")
|
|
|
|
|
|
def sql_tables(zone: str, tables: tuple[str, ...]) -> set[str]:
|
|
words: set[str] = set()
|
|
for path in modules_under(zone):
|
|
tree = ast.parse(path.read_text(), filename=str(path))
|
|
for node in ast.walk(tree):
|
|
if isinstance(node, ast.Constant) and isinstance(node.value, str):
|
|
words.update(re.findall(r"\b(?:FROM|JOIN|INTO|UPDATE)\s+(\w+)", node.value))
|
|
return words & set(tables)
|
|
|
|
|
|
def test_sql_of_each_zone_reads_only_its_tables() -> None:
|
|
owned = {
|
|
"identity": IDENTITY_TABLES,
|
|
"messaging": MESSAGING_TABLES,
|
|
"attachments": ATTACHMENT_TABLES,
|
|
}
|
|
every = {table for tables in owned.values() for table in tables}
|
|
for zone, tables in owned.items():
|
|
assert sql_tables(zone, tuple(every)) == set(tables), zone
|
|
|
|
|
|
def test_messaging_does_not_import_identity_at_all() -> None:
|
|
borrowed = {
|
|
str(path.relative_to(SOURCE)): [m for m in imports_of(path) if m.startswith(IDENTITY)]
|
|
for path in modules_under("messaging")
|
|
}
|
|
assert not {key: value for key, value in borrowed.items() if value}
|