fix(dispatch): publish to /dispatch/envelopes with event-card profile (doc 23)

The client posted a body-only envelope to {DISPATCH_API_URL}/messages — an
endpoint that does not exist. Now: /dispatch/envelopes, mandatory DispatchCard
block per the platform pydantic model (int schema_version=1/state_code=0,
task_id=0, int4-safe epoch-second sequence_number), payload under card.event,
UUID card_uid (stable uuid5 for ULID-style asset ids). Inbox accepts both
card.event and legacy root body.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
Vadim Malanov
2026-07-08 23:23:47 +03:00
parent 6fbc778c1f
commit 65a96f3962
2 changed files with 74 additions and 13 deletions

View File

@@ -11,14 +11,39 @@ from fastapi.testclient import TestClient
from app.integrations import dispatch_client
def test_build_envelope_has_required_fields():
def test_build_envelope_matches_event_card_profile():
env = dispatch_client.build_envelope("LegacyhubDocumentIndexed", "card-1", {"x": 1}, event_id="e1")
assert env["message_type"] == "LegacyhubDocumentIndexed"
assert env["card_uid"] == "card-1"
assert env["event_id"] == "e1"
assert env["participant_code"] == "legacyhub"
assert "message_id" in env and "created_at" in env
assert env["body"] == {"x": 1}
# Doc 23 event profile against the platform pydantic model: UUID card_uid,
# int task_id/state_code/schema_version, payload under card.event.
import uuid as _uuid
_uuid.UUID(env["card_uid"]) # non-UUID business id mapped to stable uuid5
assert env["task_id"] == 0
card = env["card"]
assert card["card_uid"] == env["card_uid"]
assert card["schema_version"] == 1
assert card["task_id"] == 0
assert card["state_code"] == 0
assert card["specialist_status"] == "n/a"
assert isinstance(card["sequence_number"], int)
assert 0 < card["sequence_number"] < 2**31 # int4 column in dispatch-db
assert card["created_at"] == env["created_at"] == card["updated_at"]
assert card["event"] == {"x": 1}
assert env["media_files"] == []
assert env["media_file_count"] == 0
def test_card_uid_stable_for_non_uuid_ids():
a = dispatch_client.build_envelope("E", "asset_9", {})["card_uid"]
b = dispatch_client.build_envelope("E", "asset_9", {})["card_uid"]
c = dispatch_client.build_envelope("E", "asset_10", {})["card_uid"]
assert a == b != c
passthrough = "0f1e2d3c-4b5a-6978-8796-a5b4c3d2e1f0"
assert dispatch_client.build_envelope("E", passthrough, {})["card_uid"] == passthrough
def test_deterministic_event_id_is_stable_and_distinct():
@@ -60,7 +85,7 @@ def test_publish_posts_when_enabled(monkeypatch):
env = dispatch_client.publish("LegacyhubDocumentIndexed", "card-1", {"x": 1}, event_id="e1")
assert env is not None
assert len(fake.calls) == 1
assert fake.calls[0]["url"].endswith("/messages")
assert fake.calls[0]["url"].endswith("/dispatch/envelopes")
assert fake.calls[0]["headers"]["X-API-Key"] == "k"
@@ -74,9 +99,9 @@ def test_publish_document_indexed_uses_asset_id_as_card(monkeypatch):
"doc-uuid", asset_metadata={"asset_id": "asset_9", "owner_module": "qms"}
)
assert env is not None
assert env["card_uid"] == "asset_9"
assert env["body"]["asset_id"] == "asset_9"
assert env["body"]["owner_module"] == "qms"
assert env["card_uid"] == dispatch_client._card_uid("asset_9")
assert env["card"]["event"]["asset_id"] == "asset_9"
assert env["card"]["event"]["owner_module"] == "qms"
# ---- inbox route ----