The Mizan codegen substrate moves off JavaScript template-literal emission
onto a compiled Rust binary that consumes the same OpenAPI + x-mizan-* IR
the JS substrate consumed. Three structural wins fall out of one move:
1. Moat closes. The codegen logic (how `affects` becomes auto-invalidation,
how named contexts collapse onto bundled fetches, how the registry-to-
Provider mapping is shaped) ships compiled instead of as source bytes
in every consumer's node_modules.
2. Pattern F (lines.push append-walls) becomes structurally unauthorable.
The emit substrate is askama templates in templates/<target>/*.j2 —
actual target-language files with {{ ... }} substitution markers,
syntax-highlighted natively, type-checked against the render context
structs at compile time. The Rust emit modules build typed render
contexts and call .render(); no string-builder surface exists.
3. OpenAPI `default`-bearing fields now emit as non-optional in TS / Python
/ Rust — the server always populates them, so consumer code reads them
without nullable checks. Surfaced by Blazr's typecheck on regeneration.
Layout:
frontends/mizan-rust/ — Rust port of @mizan/base; #[cfg(feature="pyo3")]
exposes PyMizanClient for the Python target.
protocol/mizan-codegen/ — codegen binary source + askama templates.
protocol/mizan-generate/ — npm-package shim. bin/launcher.mjs dispatches
to the platform-appropriate prebuilt binary.
Old generator/ JS tree deleted.
tests/rust/ — wire-parity drivers. drive_kernel exercises
raw client.call() / fetch_context(); drive_emitted
exercises the typed crate the codegen emits.
tests/afi/afi_codegen_app.py — codegen entrypoint module (imports + registers).
backends/mizan-fastapi/.../schema.py — adds outputNullable so the Rust
codegen can wrap T | None responses in Option<T>.
Verification:
- 20 mizan-codegen tests green (IR deserialization, byte-equivalent
parity vs JS baseline for stage1/rust/python/react/vue/svelte,
structural test for channels).
- tests/rust/run_wire_parity.py — 12/12 probes green via the Rust binary
driving the FastAPI fixture end-to-end.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
81 lines
2.9 KiB
Rust
81 lines
2.9 KiB
Rust
//! Smoke test for the channels target against a synthetic fixture.
|
|
//! The JS channels.mjs runs types through `openapi-typescript` which the
|
|
//! Rust codegen replaces with direct interface emission; byte-equivalence
|
|
//! against the JS baseline is intentionally not the gate. Instead this
|
|
//! test checks structural properties of the emitted output.
|
|
|
|
use std::collections::BTreeMap;
|
|
use std::path::PathBuf;
|
|
|
|
use mizan_codegen::config::{Config, SourceConfig};
|
|
use mizan_codegen::emit::CodegenTarget;
|
|
use mizan_codegen::emit::channels::ChannelsTarget;
|
|
use mizan_codegen::fetch::parse_ir_from_str;
|
|
|
|
|
|
fn fixture_config() -> Config {
|
|
Config {
|
|
project_id: None,
|
|
output: PathBuf::from("/tmp"),
|
|
targets: vec!["channels".to_string()],
|
|
source: SourceConfig { fastapi: None, django: None },
|
|
rust_kernel: None,
|
|
rust_crate_name: None,
|
|
}
|
|
}
|
|
|
|
|
|
#[test]
|
|
fn channels_target_emits_expected_files() {
|
|
let raw = std::fs::read_to_string(
|
|
PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("tests/fixtures/channels_schema.json"),
|
|
).unwrap();
|
|
let ir = parse_ir_from_str(&raw).unwrap();
|
|
|
|
let files = ChannelsTarget.emit(&ir, &fixture_config());
|
|
assert_eq!(files.len(), 2, "channels target emits 2 files when channels present");
|
|
|
|
let by_path: BTreeMap<PathBuf, &str> =
|
|
files.iter().map(|f| (f.rel_path.clone(), f.content.as_str())).collect();
|
|
|
|
let ts = by_path.get(&PathBuf::from("channels.ts"))
|
|
.expect("channels.ts emitted");
|
|
for expected in [
|
|
"export interface ChatChannelParams",
|
|
"export interface ChatReactMessage",
|
|
"export interface ChatDjangoMessage",
|
|
"export interface NotificationsDjangoMessage",
|
|
"export const CHANNELS = {",
|
|
"chat: {",
|
|
"notifications: {",
|
|
"hasParams: true",
|
|
"hasParams: false",
|
|
] {
|
|
assert!(ts.contains(expected), "channels.ts must contain {expected:?}");
|
|
}
|
|
|
|
let hooks = by_path.get(&PathBuf::from("channels.hooks.tsx"))
|
|
.expect("channels.hooks.tsx emitted");
|
|
for expected in [
|
|
"import { useChannel, type ChannelSubscription } from 'mizan/channels'",
|
|
"export function useChatChannel(params: ChatChannelParams)",
|
|
"export function useNotificationsChannel()",
|
|
"ChannelSubscription<ChatChannelParams, ChatDjangoMessage, ChatReactMessage>",
|
|
"ChannelSubscription<Record<string, never>, NotificationsDjangoMessage, never>",
|
|
] {
|
|
assert!(hooks.contains(expected), "channels.hooks.tsx must contain {expected:?}");
|
|
}
|
|
}
|
|
|
|
|
|
#[test]
|
|
fn channels_target_emits_nothing_when_empty() {
|
|
// AFI fixture has no channels — target should produce zero files.
|
|
let raw = std::fs::read_to_string(
|
|
PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("tests/fixtures/afi_schema.json"),
|
|
).unwrap();
|
|
let ir = parse_ir_from_str(&raw).unwrap();
|
|
let files = ChannelsTarget.emit(&ir, &fixture_config());
|
|
assert!(files.is_empty(), "no channels → no files");
|
|
}
|