Demonstration milestone. The substrate work earlier in the session established that mizan-fastapi can dispatch RPC, bundle context fetches, and emit invalidation envelopes via TestClient (in-process ASGI). This commit closes the demonstration gap: a real FastAPI server on port 8001 + a real React harness on port 5175 + Playwright in real Chromium, exercising generated hooks. What ships: backends/mizan-fastapi/src/mizan_fastapi/cli.py — schema-export CLI: - `python -m mizan_fastapi.cli <module>` imports the named module (triggering @client decorations + register() side effects), then prints the OpenAPI schema to stdout. Mirrors mizan-django's `manage.py export_mizan_schema` so the codegen consumes either backend the same subprocess way. backends/mizan-django/generate/generator/lib/fetch.mjs — codegen now dispatches on source.django vs source.fastapi. Refactored the subprocess plumbing into a shared runSubprocess helper. The codegen package is still named "mizan-django" by historical accident — it's the framework-agnostic CLI now (a rename for later). backends/mizan-fastapi/src/mizan_fastapi/executor.py — bug fix: mizan_core's @client decorator normalizes auth=True to meta['auth']='required'. The executor's match was only handling True, not 'required', so any auth-required endpoint failed with INTERNAL_ERROR. Now matches both. Caught when wiring up the FastAPI example backend's whoami fixture; would have surfaced first time any real FastAPI app used auth=True. backends/mizan-fastapi/tests/test_dispatch.py — added AuthTests covering the auth=True path so the bug fix has unit coverage. Suite now 12/12. examples/fastapi-react-site/ — parallel to examples/django-react-site/: - backend/main.py: FastAPI app with 11 @client fixtures matching the harness surface (echo, add, multiply, whoami, staff/superuser/ verified-only, notImplementedFn, buggyFn, permissionCheckFn, current_user context). Drops Django-only stuff (forms, channels, ws-whoami, session-bound JWT). - harness/: vite proxy → FastAPI on 8001; generated api/ produced by the codegen against fastapi.config.mjs. - mizan.spec.ts: Playwright suite, 14 tests covering the same axes as Django minus channel-chat. - ContextCurrentUser fixture renders 'loading' until data arrives rather than emitting <pre>null</pre> — fixes a race the Django harness has too (just doesn't trip in practice). Verified: - mizan-fastapi unit: 12/12 (incl. new auth=True coverage) - mizan-fastapi e2e: 14/14 (Playwright via real Chromium) - mizan-core unit: 15/15 - mizan-django unit: 348 pass, 21 skip - AFI conformance: 3/3 - mizan-django e2e: 14/15 (1 skip — channels, deferred) What remains for FastAPI side: - Dockerfile.test + docker-compose.test.yml so CI can run the e2e in the same containerized way as the Django example. - Makefile test-integration target for symmetry. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
174 lines
6.0 KiB
Python
174 lines
6.0 KiB
Python
"""End-to-end dispatch tests against a real FastAPI app + TestClient."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import pytest
|
|
from fastapi import FastAPI
|
|
from fastapi.exceptions import RequestValidationError
|
|
from fastapi.testclient import TestClient
|
|
from pydantic import BaseModel
|
|
|
|
from mizan_core.client.function import client
|
|
from mizan_core.registry import clear_registry, register
|
|
from mizan_fastapi import (
|
|
MizanError,
|
|
mizan_exception_handler,
|
|
mizan_validation_handler,
|
|
router as mizan_router,
|
|
)
|
|
|
|
|
|
# ─── Fixtures ───────────────────────────────────────────────────────────────
|
|
|
|
|
|
class EchoOutput(BaseModel):
|
|
message: str
|
|
|
|
|
|
class SumOutput(BaseModel):
|
|
total: int
|
|
|
|
|
|
class UserOutput(BaseModel):
|
|
email: str
|
|
authenticated: bool
|
|
|
|
|
|
@pytest.fixture
|
|
def app():
|
|
"""Build a fresh FastAPI app + Mizan router with a few @client functions."""
|
|
clear_registry()
|
|
|
|
@client
|
|
def echo(request, text: str) -> EchoOutput:
|
|
return EchoOutput(message=f"echo: {text}")
|
|
|
|
@client
|
|
def add(request, a: int, b: int) -> SumOutput:
|
|
return SumOutput(total=a + b)
|
|
|
|
@client(context="user")
|
|
def current_user(request) -> UserOutput:
|
|
return UserOutput(email="anon@example.com", authenticated=False)
|
|
|
|
@client(context="user")
|
|
def user_count(request) -> SumOutput:
|
|
return SumOutput(total=42)
|
|
|
|
@client(affects="user")
|
|
def update_email(request, email: str) -> EchoOutput:
|
|
return EchoOutput(message=f"updated: {email}")
|
|
|
|
@client(auth=True)
|
|
def whoami(request) -> UserOutput:
|
|
return UserOutput(email="real@example.com", authenticated=True)
|
|
|
|
register(echo, "echo")
|
|
register(add, "add")
|
|
register(current_user, "current_user")
|
|
register(user_count, "user_count")
|
|
register(update_email, "update_email")
|
|
register(whoami, "whoami")
|
|
|
|
fastapi_app = FastAPI()
|
|
fastapi_app.include_router(mizan_router, prefix="/api/mizan")
|
|
fastapi_app.add_exception_handler(MizanError, mizan_exception_handler)
|
|
fastapi_app.add_exception_handler(RequestValidationError, mizan_validation_handler)
|
|
|
|
yield fastapi_app
|
|
|
|
clear_registry()
|
|
|
|
|
|
@pytest.fixture
|
|
def http(app):
|
|
return TestClient(app)
|
|
|
|
|
|
# ─── RPC dispatch ───────────────────────────────────────────────────────────
|
|
|
|
|
|
class FunctionCallTests:
|
|
def test_simple_call_returns_result(self, http):
|
|
r = http.post("/api/mizan/call/", json={"fn": "echo", "args": {"text": "hi"}})
|
|
assert r.status_code == 200
|
|
body = r.json()
|
|
assert body["result"]["message"] == "echo: hi"
|
|
assert body["invalidate"] == []
|
|
|
|
def test_call_with_typed_input(self, http):
|
|
r = http.post("/api/mizan/call/", json={"fn": "add", "args": {"a": 2, "b": 3}})
|
|
assert r.status_code == 200
|
|
assert r.json()["result"]["total"] == 5
|
|
|
|
def test_unknown_function_returns_not_found(self, http):
|
|
r = http.post("/api/mizan/call/", json={"fn": "ghost"})
|
|
assert r.status_code == 404
|
|
assert r.json()["error"]["code"] == "NOT_FOUND"
|
|
|
|
def test_validation_error_returns_422(self, http):
|
|
r = http.post("/api/mizan/call/", json={"fn": "add", "args": {"a": "not-int", "b": 3}})
|
|
assert r.status_code == 422
|
|
assert r.json()["error"]["code"] == "VALIDATION_ERROR"
|
|
|
|
def test_missing_required_input_returns_validation_error(self, http):
|
|
r = http.post("/api/mizan/call/", json={"fn": "add", "args": {}})
|
|
assert r.status_code == 422
|
|
assert r.json()["error"]["code"] == "VALIDATION_ERROR"
|
|
|
|
def test_missing_fn_field_returns_400(self, http):
|
|
r = http.post("/api/mizan/call/", json={})
|
|
assert r.status_code == 400
|
|
assert r.json()["error"]["code"] == "BAD_REQUEST"
|
|
|
|
def test_invalid_json_returns_400(self, http):
|
|
r = http.post("/api/mizan/call/", content=b"not json", headers={"content-type": "application/json"})
|
|
assert r.status_code == 400
|
|
|
|
def test_response_carries_no_store(self, http):
|
|
r = http.post("/api/mizan/call/", json={"fn": "echo", "args": {"text": "x"}})
|
|
assert r.headers.get("cache-control") == "no-store"
|
|
|
|
|
|
# ─── Context bundling ───────────────────────────────────────────────────────
|
|
|
|
|
|
class ContextFetchTests:
|
|
def test_context_returns_bundled_results(self, http):
|
|
r = http.get("/api/mizan/ctx/user/")
|
|
assert r.status_code == 200
|
|
body = r.json()
|
|
assert "current_user" in body
|
|
assert "user_count" in body
|
|
assert body["current_user"]["email"] == "anon@example.com"
|
|
assert body["user_count"]["total"] == 42
|
|
|
|
def test_unknown_context_returns_not_found(self, http):
|
|
r = http.get("/api/mizan/ctx/ghost/")
|
|
assert r.status_code == 404
|
|
assert r.json()["error"]["code"] == "NOT_FOUND"
|
|
|
|
|
|
# ─── Invalidation ───────────────────────────────────────────────────────────
|
|
|
|
|
|
class AuthTests:
|
|
"""The decorator normalizes auth=True → meta['auth']='required'; executor must match both."""
|
|
|
|
def test_anonymous_request_to_auth_required_returns_401(self, http):
|
|
r = http.post("/api/mizan/call/", json={"fn": "whoami", "args": {}})
|
|
assert r.status_code == 401
|
|
assert r.json()["error"]["code"] == "UNAUTHORIZED"
|
|
|
|
|
|
class InvalidationTests:
|
|
def test_mutation_emits_invalidate_list(self, http):
|
|
r = http.post(
|
|
"/api/mizan/call/",
|
|
json={"fn": "update_email", "args": {"email": "new@example.com"}},
|
|
)
|
|
assert r.status_code == 200
|
|
body = r.json()
|
|
# affects='user' is a context-name string → invalidate list contains 'user'
|
|
assert "user" in body["invalidate"]
|