AFI conformance test suite
Substrate-level gate: same @client fixture registered in both backends
emits equivalent schemas, therefore the codegen produces equivalent
TypeScript regardless of which backend the frontend is generated against.
Catches adapter symmetry problems (Pydantic→OpenAPI converter divergence,
metadata leakage, ordering non-determinism) without docker, browser, or
Playwright.
What ships:
backends/mizan-fastapi/src/mizan_fastapi/schema.py — build_schema():
- Builds OpenAPI 3.0 from registered Mizan functions, mirroring the
shape mizan-django's export emits.
- Drives FastAPI's native OpenAPI generation by registering a stub POST
endpoint per function with its Input/Output Pydantic models, then
appends x-mizan-functions and x-mizan-contexts extensions.
- Param-elevation logic mirrors mizan-django/src/mizan/export/__init__.py
exactly (sharedBy tracking, required iff every function in context has
the param).
- snake_to_camel and metadata field shapes match Django for byte-equality
on the AFI surface.
tests/afi/ — the conformance harness:
- fixture.py: 5 @client functions covering the protocol axes (plain,
context, mutation+affects). No channels/forms — those aren't AFI-common.
- django_app/: minimal Django project (settings, urls, AppConfig.ready
registers the fixture). manage.py adds tests/afi/ to sys.path so both
backends import the same fixture module.
- fastapi_app.py: thin make_app() that registers fixture and mounts router.
- schema_normalizer.py: drops backend-specific framing — Ninja-vs-FastAPI
envelope differences (info/servers/tags), Django-only function fields
(form metadata), x-mizan-channels. Plus afi_subset() and
function_io_schemas() helpers for narrower comparisons.
- test_codegen_parity.py: three gates
1. x-mizan-functions match across backends
2. x-mizan-contexts match across backends
3. Per-function Input/Output OpenAPI schemas match (what codegen feeds
to openapi-typescript for type generation)
The full normalized OpenAPI envelopes do diverge — FastAPI adds
HTTPValidationError, the two converters wrap things slightly differently
in non-AFI-essential ways. That's not in the test scope. The codegen
only consumes x-mizan-functions, x-mizan-contexts, and the per-function
type schemas; those are what the test gates.
Makefile: test-afi target added; rolls into the test aggregate.
Verified: 3/3 conformance tests pass. Other surfaces unaffected —
mizan-core 15/15, mizan-django 348 pass, mizan-fastapi 11/11.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
94
tests/afi/fixture.py
Normal file
94
tests/afi/fixture.py
Normal file
@@ -0,0 +1,94 @@
|
||||
"""
|
||||
The AFI fixture — a small set of @client-decorated functions designed to
|
||||
exercise the protocol axes both backends must agree on:
|
||||
|
||||
- plain function with typed input
|
||||
- plain function with no input
|
||||
- two context functions sharing a param (proves bundling + param elevation)
|
||||
- a mutation declaring `affects` on the context
|
||||
|
||||
No channels, no forms, no shapes — those aren't AFI-common.
|
||||
|
||||
`register_fixture()` registers the functions with mizan_core.registry.
|
||||
Backend test apps import this module and call register_fixture() during
|
||||
their setup so each backend's schema export sees the same registrations.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from pydantic import BaseModel
|
||||
|
||||
from mizan_core.client.function import client
|
||||
from mizan_core.registry import register
|
||||
|
||||
|
||||
# ─── Output shapes ──────────────────────────────────────────────────────────
|
||||
|
||||
|
||||
class EchoOutput(BaseModel):
|
||||
message: str
|
||||
|
||||
|
||||
class WhoamiOutput(BaseModel):
|
||||
email: str
|
||||
authenticated: bool
|
||||
|
||||
|
||||
class ProfileOutput(BaseModel):
|
||||
user_id: int
|
||||
name: str
|
||||
|
||||
|
||||
class OrderOutput(BaseModel):
|
||||
id: int
|
||||
user_id: int
|
||||
total: int
|
||||
|
||||
|
||||
class StatusOutput(BaseModel):
|
||||
ok: bool
|
||||
|
||||
|
||||
# ─── Fixture functions ──────────────────────────────────────────────────────
|
||||
|
||||
|
||||
@client
|
||||
def echo(request, text: str) -> EchoOutput:
|
||||
"""Echoes the input back."""
|
||||
return EchoOutput(message=f"echo: {text}")
|
||||
|
||||
|
||||
@client
|
||||
def whoami(request) -> WhoamiOutput:
|
||||
"""Returns the current user identity."""
|
||||
return WhoamiOutput(email="anon@example.com", authenticated=False)
|
||||
|
||||
|
||||
@client(context="user")
|
||||
def user_profile(request, user_id: int) -> ProfileOutput:
|
||||
"""One half of the user context."""
|
||||
return ProfileOutput(user_id=user_id, name="placeholder")
|
||||
|
||||
|
||||
@client(context="user")
|
||||
def user_orders(request, user_id: int) -> list[OrderOutput]:
|
||||
"""Other half of the user context — same param, proves param elevation."""
|
||||
return []
|
||||
|
||||
|
||||
@client(affects="user")
|
||||
def update_profile(request, user_id: int, name: str) -> StatusOutput:
|
||||
"""Mutation declaring affects on the user context."""
|
||||
return StatusOutput(ok=True)
|
||||
|
||||
|
||||
# ─── Registration ───────────────────────────────────────────────────────────
|
||||
|
||||
|
||||
def register_fixture() -> None:
|
||||
"""Register every fixture function with mizan_core.registry."""
|
||||
register(echo, "echo")
|
||||
register(whoami, "whoami")
|
||||
register(user_profile, "user_profile")
|
||||
register(user_orders, "user_orders")
|
||||
register(update_profile, "update_profile")
|
||||
Reference in New Issue
Block a user