|
| 1 | +"""Tests for ``adcp.webhooks.to_wire_dict``. |
| 2 | +
|
| 3 | +The seam exists so adopters wrapping ``create_a2a_webhook_payload`` and |
| 4 | +``create_mcp_webhook_payload`` can serialize either return shape with one |
| 5 | +call. The load-bearing properties: |
| 6 | +
|
| 7 | +* a2a protobuf round-trips to camelCase keys (``id``, ``contextId``, |
| 8 | + ``artifactId``) so external A2A receivers see the on-wire shape. |
| 9 | +* MCP dicts pass through with the snake_case keys the MCP webhook |
| 10 | + schema specifies (``task_id``, ``task_type``). |
| 11 | +* Pydantic models dump to JSON-mode dicts so sub-models serialize too. |
| 12 | +* Unsupported types raise ``TypeError`` at the seam — silent fallthrough |
| 13 | + to ``str(payload)`` or similar would mask integration bugs. |
| 14 | +""" |
| 15 | + |
| 16 | +from __future__ import annotations |
| 17 | + |
| 18 | +from datetime import datetime, timezone |
| 19 | + |
| 20 | +import pytest |
| 21 | + |
| 22 | +from adcp.types import GeneratedTaskStatus |
| 23 | +from adcp.types.generated_poc.core.mcp_webhook_payload import McpWebhookPayload |
| 24 | +from adcp.webhooks import ( |
| 25 | + create_a2a_webhook_payload, |
| 26 | + create_mcp_webhook_payload, |
| 27 | + to_wire_dict, |
| 28 | +) |
| 29 | + |
| 30 | + |
| 31 | +def test_a2a_task_round_trips_to_camelcase_wire_keys() -> None: |
| 32 | + """Terminated A2A status returns a Task → camelCase wire keys.""" |
| 33 | + payload = create_a2a_webhook_payload( |
| 34 | + task_id="task_123", |
| 35 | + status=GeneratedTaskStatus.completed, |
| 36 | + context_id="ctx_456", |
| 37 | + result={"media_buy_id": "mb_1"}, |
| 38 | + timestamp=datetime(2026, 5, 8, 12, 0, 0, tzinfo=timezone.utc), |
| 39 | + ) |
| 40 | + |
| 41 | + wire = to_wire_dict(payload) |
| 42 | + |
| 43 | + assert wire["id"] == "task_123" |
| 44 | + assert wire["contextId"] == "ctx_456" |
| 45 | + assert wire["status"]["state"] == "completed" |
| 46 | + assert wire["artifacts"][0]["artifactId"] == "task_123_result" |
| 47 | + # Inner DataPart preserves the AdCP response payload verbatim. |
| 48 | + assert wire["artifacts"][0]["parts"][0]["data"] == {"media_buy_id": "mb_1"} |
| 49 | + |
| 50 | + |
| 51 | +def test_a2a_status_update_event_round_trips_to_camelcase_wire_keys() -> None: |
| 52 | + """Intermediate A2A status returns a TaskStatusUpdateEvent.""" |
| 53 | + payload = create_a2a_webhook_payload( |
| 54 | + task_id="task_789", |
| 55 | + status=GeneratedTaskStatus.working, |
| 56 | + context_id="ctx_789", |
| 57 | + result={"current_step": "processing", "percentage": 50}, |
| 58 | + ) |
| 59 | + |
| 60 | + wire = to_wire_dict(payload) |
| 61 | + |
| 62 | + assert wire["taskId"] == "task_789" |
| 63 | + assert wire["contextId"] == "ctx_789" |
| 64 | + assert wire["status"]["state"] == "working" |
| 65 | + assert wire["status"]["message"]["role"] == "agent" |
| 66 | + assert wire["status"]["message"]["parts"][0]["data"] == { |
| 67 | + "current_step": "processing", |
| 68 | + "percentage": 50, |
| 69 | + } |
| 70 | + |
| 71 | + |
| 72 | +def test_mcp_dict_passes_through_with_snake_case_keys() -> None: |
| 73 | + """MCP wire shape is snake_case per mcp-webhook-payload.json.""" |
| 74 | + payload = create_mcp_webhook_payload( |
| 75 | + task_id="task_123", |
| 76 | + task_type="create_media_buy", |
| 77 | + status="completed", |
| 78 | + result={"media_buy_id": "mb_1"}, |
| 79 | + idempotency_key="whk_01HW9D2T3VXQ5M7K9N1P3R5S7U", |
| 80 | + ) |
| 81 | + |
| 82 | + wire = to_wire_dict(payload) |
| 83 | + |
| 84 | + assert wire["task_id"] == "task_123" |
| 85 | + assert wire["task_type"] == "create_media_buy" |
| 86 | + assert wire["status"] == "completed" |
| 87 | + assert wire["result"] == {"media_buy_id": "mb_1"} |
| 88 | + assert wire["idempotency_key"] == "whk_01HW9D2T3VXQ5M7K9N1P3R5S7U" |
| 89 | + |
| 90 | + |
| 91 | +def test_mcp_pydantic_model_dumps_to_snake_case_wire_keys() -> None: |
| 92 | + """Adopters that construct ``McpWebhookPayload`` directly get the |
| 93 | + same wire shape as the dict path — single seam, no per-shape branch. |
| 94 | + """ |
| 95 | + model = McpWebhookPayload( |
| 96 | + idempotency_key="whk_01HW9D2T3VXQ5M7K9N1P3R5S7U", |
| 97 | + task_id="task_456", |
| 98 | + task_type="create_media_buy", |
| 99 | + status="completed", |
| 100 | + timestamp=datetime(2026, 5, 8, 12, 0, 0, tzinfo=timezone.utc), |
| 101 | + ) |
| 102 | + |
| 103 | + wire = to_wire_dict(model) |
| 104 | + |
| 105 | + assert wire["task_id"] == "task_456" |
| 106 | + assert wire["task_type"] == "create_media_buy" |
| 107 | + assert wire["status"] == "completed" |
| 108 | + # ``mode="json", exclude_none=True`` is load-bearing — None fields |
| 109 | + # would otherwise pollute the wire body. |
| 110 | + assert "operation_id" not in wire |
| 111 | + assert "context_id" not in wire |
| 112 | + |
| 113 | + |
| 114 | +def test_plain_dict_passes_through_unchanged() -> None: |
| 115 | + """Hand-built dicts (legacy adopter passthrough) round-trip verbatim.""" |
| 116 | + raw = {"task_id": "t1", "status": "working", "extra": {"nested": True}} |
| 117 | + |
| 118 | + wire = to_wire_dict(raw) |
| 119 | + |
| 120 | + assert wire == raw |
| 121 | + # Defensive copy — caller mutating the returned dict must not |
| 122 | + # mutate the input. |
| 123 | + assert wire is not raw |
| 124 | + |
| 125 | + |
| 126 | +def test_unsupported_type_raises_type_error() -> None: |
| 127 | + """Silent fallthrough would mask integration bugs — fail loud.""" |
| 128 | + with pytest.raises(TypeError, match="Unsupported webhook payload type"): |
| 129 | + to_wire_dict("not a payload") # type: ignore[arg-type] |
0 commit comments