The IR called them react-message and django-message, so a FastAPI channel had to declare a DjangoMessage. They are client-message and server-message now, and the direction words hold wherever a channel is declared: Params / ClientMessage / ServerMessage, with mizan-core deriving <Pascal>Params and friends so no backend names a type itself. Django's ReactChannel and FastAPI's ReactChannel are both Channel. mizan-fastapi never registered a channels extension, so build_ir() emitted no channel at all and every payload type was invisible to codegen. It registers one now. RegistryExtension is an ABC requiring all(), which is what the IR reads — an extension that cannot enumerate its registrations no longer exists. The gate that should have caught the rename could not: tests/afi registered no channel because mizan-rust had no channel registry to register one in, so a five-package rename of the wire contract passed byte-parity without a channel byte crossing it. mizan-rust grows ChannelSlotKind, a CHANNELS slice, a #[mizan::channel] macro, and KDL emission whose wire_to_pascal matches Python's split; the AFI fixture now carries a channel with every slot and one with a single slot, so all three backends prove the contract byte for byte. MizanChannel held three Option<String> beside three has_*() predicates and unwrapped them with defaults; it holds an ordered slot vector, so an absent slot is absent rather than defaulted. The channels target emitted a React hooks file that a stage1-only consumer could not compile — react emits that now. The codegen's parity tests byte-compared emitted source against baselines without ever compiling it: they compile the generated crate and run its tests, import the generated Python package and call every method, and typecheck each TypeScript target against a consumer. Also fixed at source: app_visitor printed its import diagnostic to stdout, the stream export_mizan_ir writes KDL to, so a failed import silently corrupted the IR; the apps root was hardcoded to "apps"; _default_literal crashed build_ir on any non-JSON-serializable field default; Django and mizan-core derived Pascal names two different ways, disagreeing on every dotted channel name. ir.py builds a document and renders templates/ir/document.kdl.j2 rather than appending KDL strings with hand-tracked indentation, and named types resolve to a fixed point — a model reachable only through a union branch was referenced by a ref that no type block ever defined. The rest is the write-gate's own classifiers run over the standing tree: relative imports, silent swallows, Protocol contracts that should be ABCs, emitters hand-rendering target source, catch-all arms over closed enums, and comments narrating the project rather than the code. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
82 lines
2.7 KiB
Python
82 lines
2.7 KiB
Python
import json
|
|
from dataclasses import dataclass
|
|
from typing import Any
|
|
from urllib.request import HTTPErrorProcessor, Request, build_opener, urlopen
|
|
|
|
|
|
class _KeepErrorResponses(HTTPErrorProcessor):
|
|
# urllib's default processor converts every non-2xx into a raised
|
|
# HTTPError. The RPC endpoint carries its error envelope in the body of a
|
|
# 4xx/5xx, so the response object has to reach the caller intact.
|
|
def http_response(self, request, response):
|
|
return response
|
|
|
|
https_response = http_response
|
|
|
|
|
|
_opener = build_opener(_KeepErrorResponses)
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
class Reply:
|
|
status: int
|
|
raw: bytes
|
|
|
|
@property
|
|
def body(self) -> dict[str, Any]:
|
|
# Django's own CSRF and 500 pages are HTML, so decoding is deferred to
|
|
# the tests that actually assert on the mizan envelope.
|
|
return json.loads(self.raw)
|
|
|
|
|
|
def _send(req: Request) -> Reply:
|
|
resp = _opener.open(req)
|
|
return Reply(status=resp.status, raw=resp.read())
|
|
|
|
|
|
class LiveRPCMixin:
|
|
"""HTTP access to the mizan endpoints on a LiveServerTestCase server."""
|
|
|
|
csrf_token: str = ""
|
|
cookies: str = ""
|
|
|
|
def session_init(self) -> None:
|
|
url = f"{self.live_server_url}/api/mizan/session/"
|
|
resp = urlopen(Request(url))
|
|
for cookie in resp.headers.get_all("Set-Cookie") or []:
|
|
if "csrftoken=" in cookie:
|
|
self.csrf_token = cookie.split("csrftoken=")[1].split(";")[0]
|
|
self.cookies = f"csrftoken={self.csrf_token}"
|
|
return
|
|
self.csrf_token = ""
|
|
self.cookies = ""
|
|
|
|
def get(self, path: str) -> Reply:
|
|
return _send(Request(f"{self.live_server_url}{path}"))
|
|
|
|
def post(
|
|
self,
|
|
path: str,
|
|
body: bytes | str,
|
|
content_type: str = "application/json",
|
|
with_csrf: bool = True,
|
|
) -> Reply:
|
|
if isinstance(body, str):
|
|
body = body.encode()
|
|
req = Request(f"{self.live_server_url}{path}", data=body, method="POST")
|
|
req.add_header("Content-Type", content_type)
|
|
if with_csrf and self.csrf_token:
|
|
req.add_header("X-CSRFToken", self.csrf_token)
|
|
req.add_header("Cookie", self.cookies)
|
|
return _send(req)
|
|
|
|
def call(self, fn: str, args: dict | None = None, with_csrf: bool = True) -> Reply:
|
|
payload = json.dumps({"fn": fn, "args": args or {}})
|
|
return self.post("/api/mizan/call/", payload, with_csrf=with_csrf)
|
|
|
|
def result(self, fn: str, args: dict | None = None) -> Any:
|
|
"""The `result` payload of a call that must have succeeded."""
|
|
reply = self.call(fn, args)
|
|
self.assertEqual(reply.status, 200, reply.raw)
|
|
return reply.body["result"]
|