Files
mizan/tests/afi/fixture.py
Ryth Azhur 7fb0c4a400 Mutation→context merge primitive across the stack
The @client(merge=[context, ...]) decorator lets a mutation patch its
return value directly into the cached context bundle by matching the
mutation's Output type against each context-function's Output type
to identify the slot, then splicing server-side. Kernel runs
splice_slot on the response to apply locally — no refetch, no
invalidate-cascade.

Lands H14, H15, H16, M19, M20 from ISSUES.md.

Backends (Django + FastAPI):
  _resolve_merges() in both executors walks @client(merge=...) targets,
  resolves the per-context slot via types_match_for_merge, and emits
  {context, slot, value, params?} entries on the response. Param
  auto-scoping mirrors _resolve_invalidation's tier-1 logic.

Frontend kernel (mizan-base):
  Response handler reads the merge[] array and applies splice_slot
  for each entry — locates the cached context bundle by name+params,
  overwrites the named slot with the new value, notifies subscribers.

Core (mizan-python):
  @client decorator extended with merge= parameter. Schema export
  threads merge metadata onto the OpenAPI x-mizan-functions entries.

Examples / fixtures:
  fastapi-react-site harness exercises merge + Playwright spec covers
  the end-to-end happy path (mutation → instant UI update without
  network refetch). AFI fixture's rename_user function is the
  canonical merge target.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-17 18:29:06 -04:00

109 lines
3.2 KiB
Python

"""
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)
@client
def find_user(request, user_id: int) -> ProfileOutput | None:
"""Optional return — exercises Pydantic `T | None` schema introspection."""
return None
@client(merge="user")
def rename_user(request, user_id: int, name: str) -> ProfileOutput:
"""Merge target — kernel splices return value into the user context."""
return ProfileOutput(user_id=user_id, name=name)
# ─── 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")
register(find_user, "find_user")
register(rename_user, "rename_user")