"""OpenAPI encodings: the checking side of the contract. `albedo/contract/openapi_encodings.gleam` sends batches of values the daemon's wire encoders produced, each named with the docs/openapi.yaml component schema it claims to match. ``PYTHON_OPS`` hands each batch to one long-lived Go validator, `cli/internal/testwire/wirecheck`, which uses the same schema resolution as the Go end-to-end contract, and answers each case's validation error, or "" when it matches. """ from __future__ import annotations from collections.abc import Callable import json import os from pathlib import Path import subprocess import tempfile ROOT = Path(__file__).resolve().parents[2] _checker: subprocess.Popen[str] | None = None def _start() -> subprocess.Popen[str]: global _checker if _checker is None: binary = Path(tempfile.mkdtemp(prefix="wirecheck-")) / "wirecheck" subprocess.run( ["go", "build", "-o", str(binary), "./internal/testwire/wirecheck"], cwd=ROOT / "cli", check=True, env=dict(os.environ, CGO_ENABLED="0"), ) _checker = subprocess.Popen( [str(binary), str(ROOT)], stdin=subprocess.PIPE, stdout=subprocess.PIPE, text=True, ) return _checker def _validate(cases: list[object]) -> list[object]: checker = _start() assert checker.stdin is not None and checker.stdout is not None checker.stdin.write(json.dumps(cases, separators=(",", ":")) + "\n") checker.stdin.flush() answer = json.loads(checker.stdout.readline()) assert isinstance(answer, list) and len(answer) == len(cases) return answer PYTHON_OPS: dict[str, Callable[[list[object]], list[object]]] = { "validate": _validate, }