/** * MPO-191 AC-3, mechanised. * * The claim is that a Tamagui node inside the WebView resolves knobs through * the REAL stack — `` / `` / `IntentContext` / * `useResolvedKnobs` — and lands on the same answer the host would. A * screenshot cannot prove that; two trees and a deep-equal can. * * The comparison is against the ORIGINAL CONTEXT, not against a second * envelope. Comparing envelope to envelope would pass for a knob that stops * crossing entirely, because both sides would lose it together. This way any * knob mpo adds later breaks this spec the day it is added, which is the point. * * The crossing is a real `JSON.stringify` / `JSON.parse`, so a value that * cannot survive the bridge (an `undefined`, a function, a `Map`) fails here * rather than on a phone. */ import type { ReactNode } from "react"; import { renderWithProviders } from "@multiplatform.one/test-utils"; import { IntentContext, Preset, type PresetDefinition, type Knobs, type ResolvedKnobs, Surface, type SurfaceSize, defaultKnobs, registerPreset, useResolvedKnobs, } from "@multiplatform.one/theme"; import { beforeAll, describe, expect, it } from "vitest"; import { type KnobEnvelope, knobEnvelopeSchemaVersion, parseKnobEnvelope, serializeKnobEnvelope, stableStringify, } from "./envelope"; import { KnobEnvelopeProvider } from "./KnobEnvelopeProvider"; import { useKnobEnvelope } from "./useKnobEnvelope"; // ── The named preset, whose `intents` map is what `presetName` exists to carry ── /** * Deliberately unlike `defaultIntents`. `defaultIntents.error` carries * `textAccent: "high"` and component overrides for Button/Input only, so a * GraphNode under it resolves with its base knobs; this one moves knobs a * GraphNode actually reads. If `presetName` failed to cross, the rehydrated * tree would silently fall back to `defaultIntents` and paint the wrong node. */ const graphFuzzPreset: PresetDefinition = { theme: "" as PresetDefinition["theme"], knobs: { ...defaultKnobs, borderRadius: "small", elevation: "none" }, intents: { error: { textAccent: "low", GraphNode: { borderRadius: "full", elevation: "large", borderWidth: "large" }, }, }, tints: [], }; beforeAll(() => { registerPreset("graphFuzz", graphFuzzPreset); }); // ── Seeded knob sweep ──────────────────────────────────────────────────────── /** mulberry32 — a named, reproducible PRNG, so a failure is re-runnable. */ function mulberry32(seed: number): () => number { let a = seed >>> 0; return () => { a = (a + 0x6d2b79f5) >>> 0; let t = Math.imul(a ^ (a >>> 15), 1 | a); t = (t + Math.imul(t ^ (t >>> 7), 61 | t)) ^ t; return ((t ^ (t >>> 14)) >>> 0) / 4294967296; }; } function seedFrom(label: string): number { let hash = 2166136261; for (let index = 0; index < label.length; index += 1) { hash ^= label.charCodeAt(index); hash = Math.imul(hash, 16777619); } return hash >>> 0; } const knobVocabulary = { fillStyle: ["filled", "outlined"], borderRadius: ["none", "small", "medium", "large", "full"], cornerSmoothing: ["round", "smooth"], borderWidth: ["none", "small", "medium", "large"], elevation: ["none", "small", "medium", "large"], textAccent: ["low", "medium", "high"], headingFont: ["sans-serif", "serif", "mono", "slab", "rounded"], bodyFont: ["sans-serif", "serif", "mono", "condensed"], fontWeight: ["regular", "bold"], pageTitleScale: ["moderate", "display"], animation: ["none", "quick", "bouncy", "lazy", "quickest"], } as const; const surfaceSizes: readonly (Exclude | undefined)[] = [ undefined, "3", "4", "5", ]; const intents: readonly (string | null)[] = [null, "accent", "error", "warning", "success"]; interface Combo { readonly knobs: Partial; readonly surfaceSize: Exclude | undefined; readonly intent: string | null; } function pick(rand: () => number, values: readonly T[]): T { return values[Math.floor(rand() * values.length)] as T; } function seededCombos(label: string, count: number): Combo[] { const rand = mulberry32(seedFrom(label)); const combos: Combo[] = []; for (let index = 0; index < count; index += 1) { const knobs: Record = {}; for (const [key, values] of Object.entries(knobVocabulary)) { knobs[key] = pick(rand, values as readonly unknown[]); } combos.push({ knobs: knobs as Partial, surfaceSize: pick(rand, surfaceSizes), intent: pick(rand, intents), }); } return combos; } // ── The crossing ───────────────────────────────────────────────────────────── interface Capture { envelope?: KnobEnvelope; resolved?: ResolvedKnobs; } function HostProbe({ capture, presetName, intent, }: { capture: Capture; presetName?: string; intent?: string; }) { capture.envelope = useKnobEnvelope({ presetName }); capture.resolved = useResolvedKnobs({ component: "GraphNode", intent }); return null; } function DomProbe({ capture, intent }: { capture: Capture; intent?: string }) { capture.resolved = useResolvedKnobs({ component: "GraphNode", intent }); return null; } interface CrossOptions { readonly knobs?: Partial; readonly presetName?: string; /** Passed to `useKnobEnvelope`; omit to prove the fallback to `defaultIntents`. */ readonly carryPresetName?: boolean; readonly surfaceSize?: Exclude; readonly contextIntent?: string | null; /** Explicit `useResolvedKnobs({ intent })` on both sides; omit to lean on context. */ readonly resolveIntent?: string; } interface Crossing { readonly host: ResolvedKnobs; readonly rehydrated: ResolvedKnobs; readonly envelope: KnobEnvelope; } function cross(options: CrossOptions): Crossing { const hostCapture: Capture = {}; const tree: ReactNode = ( ); const hostRender = renderWithProviders(tree); const host = hostCapture.resolved; const envelope = hostCapture.envelope; if (!host || !envelope) throw new Error("host tree never rendered the probe"); hostRender.unmount(); // The real bridge: text in, text out. const crossed = parseKnobEnvelope(serializeKnobEnvelope(envelope)); const domCapture: Capture = {}; const domRender = renderWithProviders( , ); const rehydrated = domCapture.resolved; if (!rehydrated) throw new Error("rehydrated tree never rendered the probe"); domRender.unmount(); return { host, rehydrated, envelope: crossed }; } // ── Specs ──────────────────────────────────────────────────────────────────── describe("the knob envelope crosses the stack (MPO-191 AC-3)", () => { it("resolves identically for the design's own host tree", () => { const { host, rehydrated } = cross({ knobs: { borderRadius: "large", elevation: "medium", animation: "bouncy" }, surfaceSize: "3", contextIntent: "error", resolveIntent: "error", }); expect(rehydrated).toEqual(host); }); const combos = seededCombos("MPO-191-envelope", 24); it.each(combos.map((combo, index) => [index, combo] as const))( "resolves identically for seeded combo %i", (_index, combo) => { const { host, rehydrated } = cross({ knobs: combo.knobs, surfaceSize: combo.surfaceSize, contextIntent: combo.intent, resolveIntent: combo.intent ?? undefined, }); expect(rehydrated).toEqual(host); }, ); it("resolves identically when the intent comes only from context", () => { const { host, rehydrated } = cross({ knobs: { borderRadius: "full", textAccent: "low" }, surfaceSize: "4", contextIntent: "warning", }); expect(rehydrated).toEqual(host); }); it("carries a NAMED preset's own intents across, which is what presetName is for", () => { const { host, rehydrated, envelope } = cross({ presetName: "graphFuzz", surfaceSize: "5", contextIntent: "error", resolveIntent: "error", }); expect(envelope.presetName).toBe("graphFuzz"); expect(rehydrated).toEqual(host); // And the override is live rather than inert: "warning" has no entry in // graphFuzz's intents map, so if the GraphNode override were doing nothing // these two would be identical and the test above would prove nothing. const control = cross({ presetName: "graphFuzz", surfaceSize: "5", contextIntent: "warning", resolveIntent: "warning", }); expect(host.knobProps.borderRadius).not.toEqual(control.host.knobProps.borderRadius); expect(control.rehydrated).toEqual(control.host); }); it("LOSES that intent override when presetName does not cross", () => { const { host, rehydrated } = cross({ presetName: "graphFuzz", carryPresetName: false, surfaceSize: "5", contextIntent: "error", resolveIntent: "error", }); // Falsification: without the name the rehydrated tree falls back to // `defaultIntents`, which has no GraphNode override, so the two diverge. expect(rehydrated).not.toEqual(host); }); }); describe("the envelope itself", () => { it("keeps an `unset` surface size instead of coercing it", () => { const capture: Capture = {}; const render = renderWithProviders( , ); expect(capture.envelope?.surface).toEqual({ size: "unset" }); render.unmount(); }); it("completes optional knobs so JSON cannot drop them", () => { const capture: Capture = {}; const render = renderWithProviders( , ); const knobs = capture.envelope?.knobs as Knobs; expect(knobs.cornerSmoothing).toBeDefined(); expect(knobs.pageTitleScale).toBeDefined(); const roundTripped = JSON.parse(JSON.stringify(knobs)) as Knobs; expect(Object.keys(roundTripped).sort()).toEqual(Object.keys(knobs).sort()); render.unmount(); }); it("bumps `revision` only when an input changes", () => { const capture: Capture = {}; const render = renderWithProviders( , ); const first = capture.envelope as KnobEnvelope; render.rerender( , ); expect(capture.envelope?.revision).toBe(first.revision); expect(capture.envelope).toBe(first); render.rerender( , ); expect(capture.envelope?.revision).toBe(first.revision + 1); render.unmount(); }); it("refuses an envelope from a different schema version", () => { expect(() => parseKnobEnvelope(JSON.stringify({ schemaVersion: knobEnvelopeSchemaVersion + 1 })), ).toThrow(/schemaVersion/); }); it("stringifies stably regardless of key insertion order", () => { expect(stableStringify({ a: 1, b: { d: 2, c: 3 } })).toBe( stableStringify({ b: { c: 3, d: 2 }, a: 1 }), ); expect(stableStringify({ a: 1, b: undefined })).toBe(stableStringify({ a: 1 })); }); });