|
| 1 | +"""Boot-time validation of the projected ``get_adcp_capabilities`` response. |
| 2 | +
|
| 3 | +The framework auto-projects :class:`DecisioningCapabilities` into a |
| 4 | +spec-shaped ``get_adcp_capabilities`` response (see |
| 5 | +:meth:`adcp.decisioning.handler.PlatformHandler.get_adcp_capabilities`). |
| 6 | +Adopters may also override the projection on a subclass. Either way, |
| 7 | +the response that ships on the wire must satisfy the |
| 8 | +``protocol/get-adcp-capabilities-response.json`` schema **and** the |
| 9 | +spec invariants the schema cannot fully express on its own (e.g. |
| 10 | +"``account.supported_billing`` must exist and be non-empty whenever the |
| 11 | +seller claims ``media_buy``"). |
| 12 | +
|
| 13 | +This module exercises the projection at boot — invokes |
| 14 | +``handler.get_adcp_capabilities()`` with a synthetic request and |
| 15 | +validates the returned dict — so misconfiguration surfaces as a |
| 16 | +structured :class:`AdcpError` before the server starts accepting |
| 17 | +traffic. The historical motivator is the v3 reference seller, which |
| 18 | +shipped a non-conformant capabilities response until #402 added |
| 19 | +``supported_billing`` manually; the validator below would have caught |
| 20 | +that at boot. |
| 21 | +""" |
| 22 | + |
| 23 | +from __future__ import annotations |
| 24 | + |
| 25 | +import asyncio |
| 26 | +from typing import TYPE_CHECKING, Any |
| 27 | + |
| 28 | +from adcp.decisioning.types import AdcpError |
| 29 | +from adcp.validation.schema_validator import validate_response |
| 30 | + |
| 31 | +if TYPE_CHECKING: |
| 32 | + from adcp.decisioning.handler import PlatformHandler |
| 33 | + |
| 34 | + |
| 35 | +def _invoke_capabilities(handler: PlatformHandler) -> dict[str, Any]: |
| 36 | + """Call ``handler.get_adcp_capabilities()`` synchronously. |
| 37 | +
|
| 38 | + The handler method is async but never blocks (no I/O — pure |
| 39 | + projection over ``platform.capabilities``). We drive it via |
| 40 | + :func:`asyncio.run` so this validator stays callable from the |
| 41 | + synchronous server-boot path. |
| 42 | + """ |
| 43 | + return asyncio.run(handler.get_adcp_capabilities()) |
| 44 | + |
| 45 | + |
| 46 | +def _violation(reason: str, *, details: dict[str, Any]) -> AdcpError: |
| 47 | + """Build a uniform :class:`AdcpError` for a capabilities violation. |
| 48 | +
|
| 49 | + Same shape as the other server-boot fail-fast errors in |
| 50 | + :func:`adcp.decisioning.serve.create_adcp_server_from_platform` |
| 51 | + (terminal recovery + structured ``details``). |
| 52 | + """ |
| 53 | + return AdcpError( |
| 54 | + "INVALID_REQUEST", |
| 55 | + message=( |
| 56 | + "get_adcp_capabilities response failed boot-time spec " |
| 57 | + f"validation: {reason}. Fix the platform's capabilities " |
| 58 | + "declaration (or the handler override) before starting " |
| 59 | + "the server — buyers reading this response would otherwise " |
| 60 | + "see a non-conformant capabilities envelope." |
| 61 | + ), |
| 62 | + recovery="terminal", |
| 63 | + details=details, |
| 64 | + ) |
| 65 | + |
| 66 | + |
| 67 | +def validate_capabilities_response_shape(handler: PlatformHandler) -> None: |
| 68 | + """Boot-time validator for the projected capabilities response. |
| 69 | +
|
| 70 | + Calls ``handler.get_adcp_capabilities()`` with a synthetic request, |
| 71 | + then enforces: |
| 72 | +
|
| 73 | + 1. The response validates against the bundled |
| 74 | + ``protocol/get-adcp-capabilities-response.json`` schema (via |
| 75 | + :func:`adcp.validation.schema_validator.validate_response`). |
| 76 | + 2. ``supported_protocols`` is present and non-empty |
| 77 | + (spec ``minItems: 1``; doubled-up here so the diagnostic names |
| 78 | + the invariant directly). |
| 79 | + 3. When the seller claims ``media_buy``, ``account.supported_billing`` |
| 80 | + is present and non-empty (the invariant the v3 ref seller |
| 81 | + violated pre-#402; spec |
| 82 | + ``protocol/get-adcp-capabilities-response.json`` requires |
| 83 | + ``account.required: ["supported_billing"]`` with |
| 84 | + ``minItems: 1``). |
| 85 | +
|
| 86 | + :raises AdcpError: ``INVALID_REQUEST`` with ``recovery="terminal"`` |
| 87 | + on any violation; ``details`` carry the offending response and |
| 88 | + a structured issue list so operators can index the failure |
| 89 | + programmatically. |
| 90 | + """ |
| 91 | + response = _invoke_capabilities(handler) |
| 92 | + |
| 93 | + if not isinstance(response, dict): |
| 94 | + raise _violation( |
| 95 | + "handler.get_adcp_capabilities() returned a " f"{type(response).__name__}, not a dict", |
| 96 | + details={"response_type": type(response).__name__}, |
| 97 | + ) |
| 98 | + |
| 99 | + # 1. Schema-driven validation against the bundled spec schema. |
| 100 | + outcome = validate_response("get_adcp_capabilities", response) |
| 101 | + if not outcome.valid: |
| 102 | + raise _violation( |
| 103 | + "response does not conform to " "protocol/get-adcp-capabilities-response.json", |
| 104 | + details={ |
| 105 | + "issues": [ |
| 106 | + { |
| 107 | + "pointer": issue.pointer, |
| 108 | + "message": issue.message, |
| 109 | + "keyword": issue.keyword, |
| 110 | + "schema_path": issue.schema_path, |
| 111 | + } |
| 112 | + for issue in outcome.issues |
| 113 | + ], |
| 114 | + }, |
| 115 | + ) |
| 116 | + |
| 117 | + # 2. supported_protocols invariant — minItems: 1 + required. |
| 118 | + protocols = response.get("supported_protocols") |
| 119 | + if not isinstance(protocols, list) or not protocols: |
| 120 | + raise _violation( |
| 121 | + "supported_protocols is missing or empty (spec requires " "minItems: 1)", |
| 122 | + details={"supported_protocols": protocols}, |
| 123 | + ) |
| 124 | + |
| 125 | + # 3. media_buy → account.supported_billing required + non-empty. |
| 126 | + if "media_buy" in protocols: |
| 127 | + account = response.get("account") |
| 128 | + if not isinstance(account, dict): |
| 129 | + raise _violation( |
| 130 | + "seller claims supported_protocols=['media_buy', ...] " |
| 131 | + "but the response is missing the ``account`` block " |
| 132 | + "(spec: ``account.supported_billing`` is required when " |
| 133 | + "media_buy is claimed)", |
| 134 | + details={"supported_protocols": protocols}, |
| 135 | + ) |
| 136 | + billing = account.get("supported_billing") |
| 137 | + if not isinstance(billing, list) or not billing: |
| 138 | + raise _violation( |
| 139 | + "seller claims supported_protocols=['media_buy', ...] " |
| 140 | + "but ``account.supported_billing`` is missing or empty " |
| 141 | + "(spec: minItems: 1). Set " |
| 142 | + "``DecisioningCapabilities.supported_billing=(...)`` " |
| 143 | + "with at least one of {'operator', 'agent', 'advertiser'}", |
| 144 | + details={ |
| 145 | + "supported_protocols": protocols, |
| 146 | + "account.supported_billing": billing, |
| 147 | + }, |
| 148 | + ) |
| 149 | + |
| 150 | + |
| 151 | +__all__ = ["validate_capabilities_response_shape"] |
0 commit comments