import { describe, expect, it } from "vitest"; import { COLOCATED_CLIENT_STRATEGIES } from "@dathra/shared"; import { transform } from "../index"; describe("transform", () => { it("should transform simple JSX element", () => { const code = ` const element =
Hello
; `; const result = transform(code); expect(result.code).toContain("fromTree"); expect(result.code).toContain("div"); expect(result.code).toContain("Hello"); }); it("should transform JSX with static attributes", () => { const code = ` const element = ; `; const result = transform(code); expect(result.code).toContain("class"); expect(result.code).toContain("btn"); expect(result.code).toContain("disabled"); expect(result.code).toContain("markup:"); expect(result.code).toContain("button class"); expect(result.code).toContain("Click"); }); it("should transform JSX with onClick handler", () => { const code = ` const handler = () => {}; const element = ; `; const result = transform(code); expect(result.code).toContain("event"); expect(result.code).toContain("click"); }); it("should transform JSX with dynamic text", () => { const code = ` const count = signal(0); const element = Count: {count.value}; `; const result = transform(code); expect(result.code).toContain("templateEffect"); expect(result.code).toContain("setText"); expect(result.code).toContain("count.value"); }); it("should transform JSX with dynamic attribute", () => { const code = ` const isActive = signal(false); const element =
Content
; `; const result = transform(code); expect(result.code).toContain("setAttr"); expect(result.code).toContain("class"); expect(result.code).toContain("isActive.value"); }); it("should add runtime imports", () => { const code = ` const element =
Hello
; `; const result = transform(code); expect(result.code).toContain("import"); expect(result.code).toContain("@dathra/runtime"); expect(result.code).toContain("fromTree"); }); it("should transform nested JSX elements", () => { const code = ` const element = (
Hello World
); `; const result = transform(code); expect(result.code).toContain("fromTree"); expect(result.code).toContain("div"); expect(result.code).toContain("span"); }); it("should transform JSX fragment", () => { const code = ` const element = ( <>
First
Second
); `; const result = transform(code); expect(result.code).toContain("fromTree"); expect(result.code).toContain("div"); }); it("should handle multiple event handlers", () => { const code = ` const onClick = () => {}; const onMouseEnter = () => {}; const element = ; `; const result = transform(code); expect(result.code).toContain("event"); expect(result.code).toContain("click"); expect(result.code).toContain("mouseenter"); }); it("should generate source map when requested", () => { const code = ` const element =
Hello
; `; const result = transform(code, { sourceMap: true, filename: "test.tsx" }); expect(result.map).toBeDefined(); expect(typeof result.map).toBe("string"); }); it("should not generate source map by default", () => { const code = ` const element =
Hello
; `; const result = transform(code); expect(result.map).toBeUndefined(); }); it("should preserve non-JSX code", () => { const code = ` const count = 0; function increment() { return count + 1; } const element =
Hello
; `; const result = transform(code); expect(result.code).toContain("const count = 0"); expect(result.code).toContain("function increment"); expect(result.code).toContain("return count + 1"); }); it("should transform spread attributes on HTML element using spread() and templateEffect", () => { const code = ` const props = { class: "foo" }; const element =
Content
; `; const result = transform(code); // Spread on HTML element should use spread() + templateEffect for reactivity expect(result.code).toContain("spread"); expect(result.code).toContain("templateEffect"); expect(result.code).toContain("props"); }); it("should transform conditional rendering (ternary) using insert() and templateEffect", () => { const code = ` const show = signal(true); const element =
{show.value ? Yes : No}
; `; const result = transform(code); // Conditional expression should use insert() wrapped in templateEffect expect(result.code).toContain("insert"); expect(result.code).toContain("templateEffect"); expect(result.code).toContain("show.value"); }); it("should transform list rendering (.map()) using insert() and templateEffect", () => { const code = ` const items = ["a", "b", "c"]; const element = ; `; const result = transform(code); // .map() expression should use insert() wrapped in templateEffect expect(result.code).toContain("insert"); expect(result.code).toContain("templateEffect"); expect(result.code).toContain("items"); expect(result.code).toContain("map"); }); it("should insert runtime imports after existing import declarations", () => { const code = ` import { signal } from "@dathra/reactivity"; const element =
Hello
; `; const result = transform(code); // Runtime imports should appear after the existing import const importIndex = result.code.indexOf("@dathra/reactivity"); const runtimeIndex = result.code.indexOf("@dathra/runtime"); expect(importIndex).toBeGreaterThanOrEqual(0); expect(runtimeIndex).toBeGreaterThanOrEqual(0); // Both imports should be present expect(result.code).toContain("import"); expect(result.code).toContain("fromTree"); }); it("should transform nested Fragment in CSR mode", () => { const code = ` const element = ( <>
First
<> Second Third ); `; const result = transform(code); // Outer fragment should be transformed, inner Fragment is processed as part of tree expect(result.code).toContain("fromTree"); expect(result.code).toContain("div"); expect(result.code).toContain("span"); }); describe("Component elements", () => { it("should transform component element to function call in CSR mode", () => { const code = ` const element = ; `; const result = transform(code, { mode: "csr" }); // Component should be converted to function call expect(result.code).toContain("Counter"); expect(result.code).toContain("initialCount"); expect(result.code).toContain("5"); // Should NOT contain fromTree for component expect(result.code).not.toContain("fromTree"); }); it("should transform component element to function call in SSR mode", () => { const code = ` const element = ; `; const result = transform(code, { mode: "ssr" }); // Component should be converted to function call expect(result.code).toContain("Counter"); expect(result.code).toContain("initialCount"); expect(result.code).toContain("10"); // Should NOT contain renderToString for component-only expect(result.code).not.toContain("renderToString"); }); it("should handle nested component elements with insert (no templateEffect)", () => { const code = ` const element = (
); `; const result = transform(code); // Should contain fromTree for outer div expect(result.code).toContain("fromTree"); expect(result.code).toContain("div"); // Should contain insert for nested component expect(result.code).toContain("insert"); expect(result.code).toContain("Counter"); expect(result.code).toContain("initialCount"); // Components are inserted directly (NOT wrapped in templateEffect) // to prevent re-creation on every signal change expect(result.code).not.toContain("templateEffect"); }); it("should handle root component element without tree generation", () => { const code = ` function App() { return ; } `; const result = transform(code); // Should be direct function call, no fromTree expect(result.code).toContain("Counter"); expect(result.code).toContain("initialCount"); expect(result.code).not.toContain("fromTree"); expect(result.code).not.toContain("templateEffect"); }); it("should handle JSXMemberExpression as component", () => { const code = ` const element = ; `; const result = transform(code); // Should treat Foo.Bar as component (function call) expect(result.code).toContain("Foo"); expect(result.code).toContain("Bar"); expect(result.code).toContain("baz"); expect(result.code).toContain("qux"); // Should NOT use fromTree for component expect(result.code).not.toContain("fromTree"); }); it("should distinguish lowercase HTML elements from uppercase components", () => { const code = ` const element = (
); `; const result = transform(code); // HTML elements should be in tree expect(result.code).toContain("fromTree"); expect(result.code).toContain("div"); expect(result.code).toContain("button"); // Component should be inserted expect(result.code).toContain("Counter"); expect(result.code).toContain("insert"); }); it("should pass children as children prop to components", () => { const code = ` const element = (
Content
); `; const result = transform(code); // Component should receive children prop expect(result.code).toContain("Panel"); expect(result.code).toContain("title"); expect(result.code).toContain("children"); // Children should still be processed (HTML element) expect(result.code).toContain("div"); }); it("should handle component with spread props", () => { const code = ` const props = { count: 5, label: "Counter" }; const element = ; `; const result = transform(code); // Should spread props in function call expect(result.code).toContain("Counter"); expect(result.code).toContain("props"); expect(result.code).toContain("..."); }); it("should normalize client:visible directive into island metadata", () => { const code = ` const element = ; `; const result = transform(code, { mode: "csr" }); expect(result.code).toContain('"data-dh-island": "visible"'); expect(result.code).not.toContain("client:visible"); }); it("should preserve client:interaction values as island metadata", () => { const code = ` const element = ; `; const result = transform(code, { mode: "ssr" }); expect(result.code).toContain('"data-dh-island": "interaction"'); expect(result.code).toContain('"data-dh-island-value": "mouseenter"'); }); it("should default bare client:interaction to click metadata", () => { const code = ` const element = ; `; const result = transform(code, { mode: "csr" }); expect(result.code).toContain('"data-dh-island": "interaction"'); expect(result.code).toContain('"data-dh-island-value": "click"'); }); it("should preserve the canonical host metadata contract keys", () => { const code = ` const element = ; `; const result = transform(code, { mode: "ssr" }); expect(result.code).toContain('"data-dh-island"'); expect(result.code).toContain('"data-dh-island-value"'); expect(result.code).toContain('"media"'); expect(result.code).toContain('"(max-width: 720px)"'); }); it("should keep explicit nested island metadata on child components", () => { const code = ` const element = ; `; const result = transform(code, { mode: "csr" }); expect(result.code).toContain('"data-dh-island": "visible"'); expect(result.code).toContain('"data-dh-island": "load"'); }); it("should attach compiler-generated hydration metadata to defineComponent render functions", () => { const code = ` const CounterCard = defineComponent( "counter-card", ({ props }) => , ); `; const result = transform(code, { mode: "csr" }); expect(result.code).toContain("Object.assign"); expect(result.code).toContain("__hydrationMetadata__"); expect(result.code).toContain('kind: "generic-plan"'); expect(result.code).toContain("planFactory"); expect(result.code).toContain('kind: "attr"'); expect(result.code).toContain('kind: "event"'); expect(result.code).toContain('kind: "text"'); expect(result.code).not.toContain("boundaryRefs"); expect(result.code).not.toContain("artifact:"); }); it("should include nested boundary refs in compiler-generated hydration metadata", () => { const code = ` const OuterCard = defineComponent( "outer-card", ({ props }) =>
, ); `; const result = transform(code, { mode: "ssr" }); expect(result.code).toContain("nestedBoundaries"); expect(result.code).toContain('tagName: "InnerCard"'); expect(result.code).toContain('islandStrategy: "load"'); expect(result.code).not.toContain("boundaryRefs"); }); it("should skip hydration metadata emission for unsupported imperative setup bodies", () => { const code = ` const ImperativeCard = defineComponent( "imperative-card", ({ host }) => { host.setAttribute("data-ready", "yes"); return
ready
; }, ); `; const result = transform(code, { mode: "csr" }); expect(result.code).toContain("__hydrationMetadata__"); expect(result.code).toContain("Object.assign"); expect(result.code).toContain( 'unsupportedReason: "imperative-dom-query"', ); expect(result.code).not.toContain("planFactory: null"); }); it("should classify runtime branching setup bodies as dispatch plan (supported)", () => { const code = ` const BranchingCard = defineComponent( "branching-card", ({ props }) => (props.ready.value ?
ready
: waiting), ); `; const result = transform(code, { mode: "csr" }); expect(result.code).toContain("__hydrationMetadata__"); expect(result.code).toContain("dispatch"); expect(result.code).toContain("planFactory"); }); it("should classify opaque helper returns as unsupported", () => { const code = ` const renderCard = (label, suffix) =>
{label}{suffix}
; const OpaqueCard = defineComponent( "opaque-card", ({ props }) => renderCard(props.label.value), ); `; const result = transform(code, { mode: "csr" }); expect(result.code).toContain("__hydrationMetadata__"); expect(result.code).toContain('unsupportedReason: "opaque-helper-call"'); expect(result.code).not.toContain("planFactory"); }); it("should support typeof document and window environment probes in setup prelude", () => { const code = ` const EnvCard = defineComponent( "env-card", ({ props }) => { const renderMode = typeof document === "undefined" ? "SSR" : "CSR"; const runtimeKind = typeof window === "undefined" ? "server" : "browser"; return
{runtimeKind}:{props.label.value}
; }, ); `; const result = transform(code, { mode: "csr" }); expect(result.code).toContain("__hydrationMetadata__"); expect(result.code).toContain("planFactory"); expect(result.code).not.toContain("unsupportedReason"); expect(result.code).toContain( 'const renderMode = typeof document === "undefined" ? "SSR" : "CSR"', ); expect(result.code).toContain( 'const runtimeKind = typeof window === "undefined" ? "server" : "browser"', ); expect(result.code).toContain('kind: "attr"'); expect(result.code).toContain('kind: "text"'); }); it("should support local zero-arg helpers that directly return JSX", () => { const code = ` const ready = signal("ready"); const renderCard = () =>
{ready.value}
; const SupportedCard = defineComponent( "supported-card", () => renderCard(), ); `; const result = transform(code, { mode: "csr" }); expect(result.code).toContain("__hydrationMetadata__"); expect(result.code).toContain("planFactory"); expect(result.code).not.toContain("unsupportedReason"); expect(result.code).toContain('kind: "text"'); expect(result.code).toContain("ready.value"); }); it("should support local transparent thunk wrappers around JSX", () => { const code = ` const withBoundary = (render) => render(); const WrappedCard = defineComponent( "wrapped-card", ({ props }) => withBoundary(() =>
{props.label.value}
), ); `; const result = transform(code, { mode: "csr" }); expect(result.code).toContain("__hydrationMetadata__"); expect(result.code).toContain("planFactory"); expect(result.code).not.toContain("unsupportedReason"); expect(result.code).toContain("const { props } = __dh_ctx"); expect(result.code).toContain('kind: "text"'); }); it("should support local transparent thunk wrappers around local helper chains", () => { const code = ` const renderCard = (label) =>
{label}
; const withBoundary = (render) => render(); const WrappedCard = defineComponent( "wrapped-card", ({ props }) => withBoundary(() => renderCard(props.label.value)), ); `; const result = transform(code, { mode: "csr" }); expect(result.code).toContain("__hydrationMetadata__"); expect(result.code).toContain("planFactory"); expect(result.code).not.toContain("unsupportedReason"); expect(result.code).toContain("const label = props.label.value"); expect(result.code).toContain('kind: "text"'); }); it("should support known imported transparent thunk wrappers around JSX", () => { const code = ` import { withStore } from "@dathra/core"; const store = createStore(); const WrappedCard = defineComponent( "wrapped-card", ({ props }) => withStore(store, () =>
{props.label.value}
), ); `; const result = transform(code, { mode: "csr" }); expect(result.code).toContain("__hydrationMetadata__"); expect(result.code).toContain("planFactory"); expect(result.code).not.toContain("unsupportedReason"); expect(result.code).toContain("const { props } = __dh_ctx"); expect(result.code).toContain('kind: "text"'); }); it("should support aliased known imported transparent thunk wrappers", () => { const code = ` import { withStore as bindStore } from "@dathra/store"; const store = createStore(); const WrappedCard = defineComponent( "wrapped-card", ({ props }) => bindStore(store, () =>
{props.label.value}
), ); `; const result = transform(code, { mode: "csr" }); expect(result.code).toContain("__hydrationMetadata__"); expect(result.code).toContain("planFactory"); expect(result.code).not.toContain("unsupportedReason"); expect(result.code).toContain("const { props } = __dh_ctx"); expect(result.code).toContain('kind: "text"'); }); it("should support SSRAppRoot-shaped transparent wrappers around root component elements with function props", () => { const code = ` import { withStore } from "@dathra/core"; const PlaygroundShell = (props) =>
{props.renderPage()}
; const OverviewPage = () =>
overview
; const RuntimePage = () =>
runtime
; const renderOverviewPage = () => ; const renderRuntimePage = () => ; const pageRenderers = { "/": renderOverviewPage, "/islands-runtime": renderRuntimePage, }; const resolvePage = (routePath) => pageRenderers[routePath](); const AppRoot = defineComponent( "app-root", ({ props }) => { const routePath = props.routePath.value; const pageContent = resolvePage(routePath); return withStore(store, () => ( pageContent} /> )); }, ); `; const result = transform(code, { mode: "csr" }); expect(result.code).toContain("__hydrationMetadata__"); expect(result.code).toContain("planFactory"); expect(result.code).not.toContain("unsupportedReason"); expect(result.code).toContain("const routePath = props.routePath.value"); expect(result.code).toContain( "const pageContent = resolvePage(routePath)", ); expect(result.code).toContain("renderPage: () => pageContent"); expect(result.code).toContain("PlaygroundShell({"); }); it("should keep unknown imported thunk wrappers unsupported", () => { const code = ` import { withTheme } from "./theme"; const WrappedCard = defineComponent( "wrapped-card", ({ props }) => withTheme("mint", () =>
{props.label.value}
), ); `; const result = transform(code, { mode: "csr" }); expect(result.code).toContain("__hydrationMetadata__"); expect(result.code).toContain('unsupportedReason: "opaque-helper-call"'); expect(result.code).not.toContain("planFactory"); }); it("should keep same-named imports from unknown sources unsupported", () => { const code = ` import { withStore } from "./custom-store"; const store = createStore(); const WrappedCard = defineComponent( "wrapped-card", ({ props }) => withStore(store, () =>
{props.label.value}
), ); `; const result = transform(code, { mode: "csr" }); expect(result.code).toContain("__hydrationMetadata__"); expect(result.code).toContain('unsupportedReason: "opaque-helper-call"'); expect(result.code).not.toContain("planFactory"); }); it("should support trivial helper argument forwarding into JSX", () => { const code = ` const renderCard = (label) =>
{label}
; const ForwardedCard = defineComponent( "forwarded-card", ({ props }) => renderCard(props.label.value), ); `; const result = transform(code, { mode: "csr" }); expect(result.code).toContain("__hydrationMetadata__"); expect(result.code).toContain("planFactory"); expect(result.code).not.toContain("unsupportedReason"); expect(result.code).toContain('kind: "text"'); expect(result.code).toContain("const label = props.label.value"); }); it("should support helper-local prelude before returning JSX", () => { const code = ` const renderCard = (label) => { const text = label.toUpperCase(); return
{text}
; }; const PreludeCard = defineComponent( "prelude-card", ({ props }) => renderCard(props.label.value), ); `; const result = transform(code, { mode: "csr" }); expect(result.code).toContain("__hydrationMetadata__"); expect(result.code).toContain("planFactory"); expect(result.code).not.toContain("unsupportedReason"); expect(result.code).toContain("const text = label.toUpperCase()"); expect(result.code).toContain('kind: "text"'); }); it("should support multi-level local helper chains that end in JSX", () => { const code = ` const renderLeaf = (label) =>
{label}
; const renderMiddle = (label) => renderLeaf(label.toUpperCase()); const renderTop = (label) => renderMiddle(label); const ChainedCard = defineComponent( "chained-card", ({ props }) => renderTop(props.label.value), ); `; const result = transform(code, { mode: "csr" }); expect(result.code).toContain("__hydrationMetadata__"); expect(result.code).toContain("planFactory"); expect(result.code).not.toContain("unsupportedReason"); expect(result.code).toContain("const label = props.label.value"); expect(result.code).toContain("const label_1 = label"); expect(result.code).toContain("const label_2 = label_1.toUpperCase()"); expect(result.code).toContain('kind: "text"'); }); it("should support destructured helper param forwarding", () => { const code = ` const renderCard = ({ label, suffix }) =>
{label}{suffix}
; const DestructuredCard = defineComponent( "destructured-card", ({ props }) => renderCard({ label: props.label.value, suffix: props.suffix.value }), ); `; const result = transform(code, { mode: "csr" }); expect(result.code).toContain("__hydrationMetadata__"); expect(result.code).toContain("planFactory"); expect(result.code).not.toContain("unsupportedReason"); expect(result.code).toContain("const { label, suffix } = {"); expect(result.code).toContain('kind: "text"'); }); it("should avoid helper-chain binding collisions in generated planFactory prelude", () => { const code = ` const renderInner = (label) => { const text = label.toUpperCase(); return
{text}
; }; const renderOuter = (label) => { const text = label + "!"; return renderInner(text); }; const CollisionCard = defineComponent( "collision-card", ({ props: { label } }) => renderOuter(label.value), ); `; const result = transform(code, { mode: "csr" }); expect(result.code).toContain("__hydrationMetadata__"); expect(result.code).toContain("planFactory"); expect(result.code).not.toContain("unsupportedReason"); expect(result.code).toContain("const { props: { label } } = __dh_ctx"); expect(result.code).toContain("const label_1 = label.value"); expect(result.code).toContain('const text = label_1 + "!"'); expect(result.code).toContain("const label_2 = text"); expect(result.code).toContain("const text_1 = label_2.toUpperCase()"); }); it("should avoid helper-local function declaration collisions in generated planFactory prelude", () => { const code = ` const renderInner = (label) => { function format(value) { return value.toUpperCase(); } return
{format(label)}
; }; const renderOuter = (label) => { function format(value) { return value + "!"; } return renderInner(format(label)); }; const FunctionCollisionCard = defineComponent( "function-collision-card", ({ props: { label } }) => renderOuter(label.value), ); `; const result = transform(code, { mode: "csr" }); expect(result.code).toContain("__hydrationMetadata__"); expect(result.code).toContain("planFactory"); expect(result.code).not.toContain("unsupportedReason"); expect(result.code).toContain("const { props: { label } } = __dh_ctx"); expect(result.code).toContain("const label_1 = label.value"); expect(result.code).toContain("function format(value)"); expect(result.code).toContain("function format_1(value)"); expect(result.code).toContain("const label_2 = format(label_1)"); expect(result.code).toContain("format_1(label_2)"); }); it("should support normalizable spread merges from top-level serializable bindings", () => { const code = ` const extraProps = { role: "status" }; const SpreadCard = defineComponent( "spread-card", () =>
ready
, ); `; const result = transform(code, { mode: "csr" }); expect(result.code).toContain("__hydrationMetadata__"); expect(result.code).toContain("planFactory"); expect(result.code).toContain('kind: "spread"'); expect(result.code).not.toContain("unsupportedReason"); }); it("should classify node identity creation outside JSX tree as unsupported", () => { const code = ` const NodeCard = defineComponent( "node-card", () => { const node = document.createElement("strong"); node.textContent = "ready"; return
{node}
; }, ); `; const result = transform(code, { mode: "csr" }); expect(result.code).toContain("__hydrationMetadata__"); expect(result.code).toContain('unsupportedReason: "node-identity-reuse"'); expect(result.code).not.toContain("planFactory"); }); it("should support WebComponentSSR-shaped setup with store.ref, typeof document, onClick and slot", () => { const code = ` import { countAtom } from "./demoStore"; const SSRStoreCounter = defineComponent( "dathra-ssr-store-counter", ({ props, store }) => { const count = store.ref(countAtom); const mode = typeof document === "undefined" ? "SSR" : "CSR"; return (
{mode}

{props.headline.value}

{props.note.value}

{count.value}
); }, ); `; const result = transform(code, { mode: "csr" }); expect(result.code).toContain("__hydrationMetadata__"); expect(result.code).toContain("planFactory"); expect(result.code).not.toContain("unsupportedReason"); expect(result.code).toContain('kind: "attr"'); expect(result.code).toContain('kind: "text"'); expect(result.code).toContain('kind: "event"'); expect(result.code).toContain("count.value"); expect(result.code).toContain("store.ref(countAtom)"); expect(result.code).toContain( 'const mode = typeof document === "undefined" ? "SSR" : "CSR"', ); }); it("should support colocated card-shaped setup with signal(0) prelude and client.strategy", () => { const code = ` const PlaygroundColocatedLoadCard = defineComponent( "playground-colocated-load-card", ({ client }) => { const count = signal(0); return (

{client.strategy ?? "none"}

{count.value}
); }, ); `; const result = transform(code, { mode: "csr" }); expect(result.code).toContain("__hydrationMetadata__"); expect(result.code).toContain("planFactory"); expect(result.code).not.toContain("unsupportedReason"); expect(result.code).toContain("const count = signal(0)"); expect(result.code).toContain('kind: "text"'); }); it("should support nested inner island setup with signal(0) and onClick", () => { const code = ` const PlaygroundNestedInnerIsland = defineComponent( "playground-nested-inner-island", ({ client }) => { const count = signal(0); return (

{client.strategy ?? "none"}

{count.value}
); }, ); `; const result = transform(code, { mode: "csr" }); expect(result.code).toContain("__hydrationMetadata__"); expect(result.code).toContain("planFactory"); expect(result.code).not.toContain("unsupportedReason"); expect(result.code).toContain("const count = signal(0)"); expect(result.code).toContain('kind: "event"'); expect(result.code).toContain('kind: "text"'); }); it("should support nested outer island setup with props and nested client:load child", () => { const code = ` const PlaygroundNestedOuterIsland = defineComponent( "playground-nested-outer-island", ({ client, props }) => { return (
{client.strategy ?? "none"} {props.label.value}
); }, ); `; const result = transform(code, { mode: "csr" }); expect(result.code).toContain("__hydrationMetadata__"); expect(result.code).toContain("planFactory"); expect(result.code).not.toContain("unsupportedReason"); expect(result.code).toContain("nestedBoundaries"); expect(result.code).toContain('tagName: "PlaygroundNestedInnerIsland"'); expect(result.code).toContain('islandStrategy: "load"'); }); it("should support exported defineComponent declarations", () => { const code = ` export const ExportedCard = defineComponent( "exported-card", ({ props }) =>
{props.label.value}
, ); `; const result = transform(code, { mode: "csr" }); expect(result.code).toContain("__hydrationMetadata__"); expect(result.code).toContain("planFactory"); expect(result.code).not.toContain("unsupportedReason"); expect(result.code).toContain('kind: "text"'); }); it("should support multiple defineComponent declarations in one const statement", () => { const code = ` const CardA = defineComponent( "card-a", ({ props }) =>
{props.a.value}
, ), CardB = defineComponent( "card-b", ({ props }) => {props.b.value}, ); `; const result = transform(code, { mode: "csr" }); expect(result.code).toContain("__hydrationMetadata__"); expect(result.code).toContain("planFactory"); expect(result.code).toContain("props.a.value"); expect(result.code).toContain("props.b.value"); }); it("should support props-only components that return direct JSX without block body", () => { const code = ` const GlobalStyleCard = defineComponent( "playground-global-style-card", ({ props }) => (

{props.title.value}

{props.body.value}

), ); `; const result = transform(code, { mode: "csr" }); expect(result.code).toContain("__hydrationMetadata__"); expect(result.code).toContain("planFactory"); expect(result.code).not.toContain("unsupportedReason"); expect(result.code).toContain('kind: "attr"'); expect(result.code).toContain('kind: "text"'); expect(result.code).toContain("props.tone.value"); }); it("should classify non-function component arg as no metadata", () => { const code = ` const NonFnCard = defineComponent( "non-fn-card", "not-a-function", ); `; const result = transform(code, { mode: "csr" }); expect(result.code).not.toContain("__hydrationMetadata__"); expect(result.code).not.toContain("planFactory"); }); it("should classify FunctionDeclaration component arg as supported when returning JSX", () => { const code = ` const FnDeclCard = defineComponent( "fn-decl-card", function setup({ props }) { return
{props.label.value}
; }, ); `; const result = transform(code, { mode: "csr" }); expect(result.code).toContain("__hydrationMetadata__"); expect(result.code).toContain("planFactory"); expect(result.code).not.toContain("unsupportedReason"); }); it("should classify helper chain ending in non-JSX expression as resolvedAnalysis null with fallback to getUnsupportedHydrationReason", () => { const code = ` const renderText = (label) => label.toUpperCase(); const TextCard = defineComponent( "text-card", ({ props }) => renderText(props.label.value), ); `; const result = transform(code, { mode: "csr" }); expect(result.code).toContain("__hydrationMetadata__"); expect(result.code).toContain("unsupportedReason"); expect(result.code).not.toContain("planFactory"); }); it("should classify setup body with IfStatement as dispatch plan (supported)", () => { const code = ` const BranchCard = defineComponent( "branch-card", ({ props }) => { if (props.ready.value) { return
ready
; } return waiting; }, ); `; const result = transform(code, { mode: "csr" }); expect(result.code).toContain("__hydrationMetadata__"); expect(result.code).toContain("dispatch"); expect(result.code).toContain("planFactory"); }); it("should classify setup body with SwitchStatement as dispatch plan (supported)", () => { const code = ` const SwitchCard = defineComponent( "switch-card", ({ props }) => { switch (props.mode.value) { case "a": return
A
; default: return
B
; } }, ); `; const result = transform(code, { mode: "csr" }); expect(result.code).toContain("__hydrationMetadata__"); expect(result.code).toContain("dispatch"); expect(result.code).toContain("planFactory"); }); it("should classify setup body with return conditional expression in block as dispatch plan", () => { const code = ` const RetCondCard = defineComponent( "ret-cond-card", ({ props }) => { const x = props.value.value; return x > 0 ?
pos
: neg; }, ); `; const result = transform(code, { mode: "csr" }); expect(result.code).toContain("__hydrationMetadata__"); expect(result.code).toContain("dispatch"); expect(result.code).toContain("planFactory"); }); it("should classify setup body with return LogicalExpression in block as dispatch plan", () => { const code = ` const RetLogCard = defineComponent( "ret-log-card", ({ props }) => { const x = props.value.value; return x &&
truthy
; }, ); `; const result = transform(code, { mode: "csr" }); expect(result.code).toContain("__hydrationMetadata__"); expect(result.code).toContain("dispatch"); expect(result.code).toContain("planFactory"); expect(result.code).not.toContain("unsupportedReason"); }); it("should generate dispatch plan for guard-return pattern with static JSX branches", () => { const code = ` const BranchCard = defineComponent( "branch-card", ({ props }) => { if (props.loading.value) { return
Loading...
; } return
{props.message.value}
; }, ); `; const result = transform(code, { mode: "csr" }); expect(result.code).toContain("__hydrationMetadata__"); expect(result.code).toContain("dispatch"); expect(result.code).toContain("planFactory"); expect(result.code).toContain("condition"); // Should have two plans (one per branch) expect(result.code).toContain("shapeHash"); }); it("should generate dispatch plan for if/else pattern with static JSX branches", () => { const code = ` const IfElseCard = defineComponent( "if-else-card", ({ props }) => { if (props.error.value) { return
{props.error.value}
; } else { return
{props.message.value}
; } }, ); `; const result = transform(code, { mode: "csr" }); expect(result.code).toContain("__hydrationMetadata__"); expect(result.code).toContain("dispatch"); expect(result.code).toContain("planFactory"); }); it("should generate different shapeHash for branches with different element types", () => { const code = ` const DiffShape = defineComponent( "diff-shape", ({ props }) => { if (props.alt.value) { return alt; } return
main
; }, ); `; const result = transform(code, { mode: "csr" }); expect(result.code).toContain("dispatch"); // Both branches should have shapeHash entries const shapeHashMatches = result.code.match(/shapeHash/g); expect(shapeHashMatches).not.toBeNull(); expect((shapeHashMatches ?? []).length).toBeGreaterThanOrEqual(2); }); it("should generate same shapeHash for branches with same element types", () => { const code = ` const SameShape = defineComponent( "same-shape", ({ props }) => { if (props.alt.value) { return
alt
; } return
main
; }, ); `; const result = transform(code, { mode: "csr" }); expect(result.code).toContain("dispatch"); }); it("should fall back to unsupported when one branch is non-static JSX", () => { const code = ` const MixedBranch = defineComponent( "mixed-branch", ({ props }) => { if (props.alt.value) { return
static
; } return renderSomething(props.message.value); }, ); `; const result = transform(code, { mode: "csr" }); expect(result.code).toContain("__hydrationMetadata__"); expect(result.code).toContain("unsupportedReason"); expect(result.code).not.toContain("dispatch"); }); it("should generate dispatch plan for nested if statements", () => { const code = ` const NestedIf = defineComponent( "nested-if", ({ props }) => { if (props.a.value) { if (props.b.value) { return
both
; } return
a only
; } return
none
; }, ); `; const result = transform(code, { mode: "csr" }); expect(result.code).toContain("__hydrationMetadata__"); expect(result.code).toContain("dispatch"); expect(result.code).toContain("planFactory"); }); it("should generate dispatch plan for switch statements", () => { const code = ` const SwitchComp = defineComponent( "switch-comp", ({ props }) => { switch (props.mode.value) { case "a": return
A
; case "b": return
B
; default: return
C
; } }, ); `; const result = transform(code, { mode: "csr" }); expect(result.code).toContain("__hydrationMetadata__"); expect(result.code).toContain("dispatch"); expect(result.code).toContain("planFactory"); }); it("should track nested islands in dispatch plan branches", () => { const code = ` const OuterDispatch = defineComponent( "outer-dispatch", ({ props }) => { if (props.loading.value) { return
Loading...
; } return (
); }, ); `; const result = transform(code, { mode: "csr" }); expect(result.code).toContain("dispatch"); expect(result.code).toContain("nestedBoundaries"); }); it("should generate dispatch plan for nested-if component with colocated directive", () => { const code = ` const NestedIfColocated = defineComponent( "nested-if-colocated", ({ props }) => { if (props.a.value) { if (props.b.value) { return
both
; } return
a only
; } return (
); }, ); `; // Nested if is now a supported dispatch pattern, so colocated directive is fine const result = transform(code, { mode: "csr" }); expect(result.code).toContain("dispatch"); expect(result.code).toContain("planFactory"); }); it("should classify return of conditional expression as dispatch plan (ternary in return)", () => { const code = ` const TernaryReturn = defineComponent( "ternary-return", ({ props }) => { return props.cond.value ?
yes
:
no
; }, ); `; const result = transform(code, { mode: "csr" }); expect(result.code).toContain("dispatch"); expect(result.code).toContain("planFactory"); }); it("should classify return of conditional expression with parenthesized JSX branches as dispatch plan", () => { const code = ` const TernaryReturnWrapped = defineComponent( "ternary-return-wrapped", ({ props }) => { const count = signal(0); return props.cond.value ? (
{count.value}
) : (
{count.value}
); }, ); `; const result = transform(code, { mode: "csr" }); expect(result.code).toContain("dispatch"); expect(result.code).toContain("planFactory"); expect(result.code).not.toContain( 'unsupportedReason: "runtime-branching"', ); }); it("should classify return of logical expression as dispatch plan", () => { const code = ` const LogicalReturn = defineComponent( "logical-return", ({ props }) => { return props.value.value &&
truthy
; }, ); `; const result = transform(code, { mode: "csr" }); expect(result.code).toContain("__hydrationMetadata__"); expect(result.code).toContain("dispatch"); expect(result.code).toContain("planFactory"); expect(result.code).not.toContain("unsupportedReason"); }); it("should generate svg namespace when root element is svg", () => { const code = ` const SvgCard = defineComponent( "svg-card", () => , ); `; const result = transform(code, { mode: "csr" }); expect(result.code).toContain("__hydrationMetadata__"); expect(result.code).toContain("planFactory"); expect(result.code).toContain('namespace: "svg"'); }); it("should generate math namespace when root element is math", () => { const code = ` const MathCard = defineComponent( "math-card", () => 42, ); `; const result = transform(code, { mode: "csr" }); expect(result.code).toContain("__hydrationMetadata__"); expect(result.code).toContain("planFactory"); expect(result.code).toContain('namespace: "math"'); }); it("should generate html namespace for fragment root", () => { const code = ` const FragCard = defineComponent( "frag-card", ({ props }) => <>
{props.a.value}
{props.b.value}, ); `; const result = transform(code, { mode: "csr" }); expect(result.code).toContain("__hydrationMetadata__"); expect(result.code).toContain("planFactory"); expect(result.code).toContain('namespace: "html"'); }); it("should support multiple signals in prelude", () => { const code = ` const MultiSigCard = defineComponent( "multi-sig-card", () => { const count = signal(0); const name = signal("world"); const flag = signal(true); return
{name.value}: {count.value}
; }, ); `; const result = transform(code, { mode: "csr" }); expect(result.code).toContain("__hydrationMetadata__"); expect(result.code).toContain("planFactory"); expect(result.code).not.toContain("unsupportedReason"); expect(result.code).toContain("const count = signal(0)"); expect(result.code).toContain('const name = signal("world")'); expect(result.code).toContain("const flag = signal(true)"); }); it("should support template literal prelude values", () => { const code = ` const TplCard = defineComponent( "tpl-card", ({ props }) => { const greeting = \`Hello, \${props.name.value}!\`; return
{greeting}
; }, ); `; const result = transform(code, { mode: "csr" }); expect(result.code).toContain("__hydrationMetadata__"); expect(result.code).toContain("planFactory"); expect(result.code).not.toContain("unsupportedReason"); }); it("should support normalizable spread merges in nested child elements", () => { const code = ` const extras = { role: "alert" }; const NestedSpreadCard = defineComponent( "nested-spread-card", () => (
nested
), ); `; const result = transform(code, { mode: "csr" }); expect(result.code).toContain("__hydrationMetadata__"); expect(result.code).toContain("planFactory"); expect(result.code).toContain('kind: "spread"'); expect(result.code).not.toContain("unsupportedReason"); }); it("should support normalizable spread merges inside fragment children", () => { const code = ` const extras = { role: "alert" }; const FragSpreadCard = defineComponent( "frag-spread-card", () => ( <>
frag child
), ); `; const result = transform(code, { mode: "csr" }); expect(result.code).toContain("__hydrationMetadata__"); expect(result.code).toContain("planFactory"); expect(result.code).toContain('kind: "spread"'); expect(result.code).not.toContain("unsupportedReason"); }); it("should classify setup with void body (no return) as unsupported-component-body", () => { const code = ` const VoidCard = defineComponent( "void-card", () => {}, ); `; const result = transform(code, { mode: "csr" }); expect(result.code).toContain("__hydrationMetadata__"); expect(result.code).toContain( 'unsupportedReason: "unsupported-component-body"', ); expect(result.code).not.toContain("planFactory"); }); it("should support setup with null return as no metadata", () => { const code = ` const NullCard = defineComponent( "null-card", () => { return null; }, ); `; const result = transform(code, { mode: "csr" }); expect(result.code).not.toContain("planFactory"); }); it("should classify setup with prelude containing JSX as non-extractable", () => { const code = ` const JsxPreludeCard = defineComponent( "jsx-prelude-card", () => { const header =

header

; return
{header}
; }, ); `; const result = transform(code, { mode: "csr" }); expect(result.code).not.toContain("planFactory"); }); it("should classify expression statement in prelude as non-extractable", () => { const code = ` const ExprStmtCard = defineComponent( "expr-stmt-card", () => { console.log("setup"); return
ready
; }, ); `; const result = transform(code, { mode: "csr" }); expect(result.code).not.toContain("planFactory"); }); it("should handle return that is not the last statement in block", () => { const code = ` const EarlyReturnCard = defineComponent( "early-return-card", () => { return
early
; const dead = "unreachable"; }, ); `; const result = transform(code, { mode: "csr" }); expect(result.code).not.toContain("planFactory"); }); it("should support exported defineComponent with helper lookup", () => { const code = ` const renderContent = (label) =>
{label}
; export const ExportedHelperCard = defineComponent( "exported-helper-card", ({ props }) => renderContent(props.label.value), ); `; const result = transform(code, { mode: "csr" }); expect(result.code).toContain("__hydrationMetadata__"); expect(result.code).toContain("planFactory"); expect(result.code).not.toContain("unsupportedReason"); }); it("should collect top-level exported function declarations as helpers", () => { const code = ` export function renderLayout(content) { return
{content}
; } const LayoutCard = defineComponent( "layout-card", ({ props }) => renderLayout(props.content.value), ); `; const result = transform(code, { mode: "csr" }); expect(result.code).toContain("__hydrationMetadata__"); expect(result.code).toContain("planFactory"); expect(result.code).not.toContain("unsupportedReason"); }); it("should collect top-level function declarations (non-exported) as helpers", () => { const code = ` function renderLayout(content) { return
{content}
; } const LayoutCard = defineComponent( "layout-card", ({ props }) => renderLayout(props.content.value), ); `; const result = transform(code, { mode: "csr" }); expect(result.code).toContain("__hydrationMetadata__"); expect(result.code).toContain("planFactory"); expect(result.code).not.toContain("unsupportedReason"); }); it("should handle NewExpression DOM constructor (Text) as node-identity-reuse", () => { const code = ` const NewCard = defineComponent( "new-card", () => { const el = new Text("hello"); return
{el}
; }, ); `; const result = transform(code, { mode: "csr" }); expect(result.code).toContain("__hydrationMetadata__"); expect(result.code).toContain('unsupportedReason: "node-identity-reuse"'); }); it("should skip defineComponent calls without second argument", () => { const code = ` const NoArgCard = defineComponent("no-arg-card"); `; const result = transform(code, { mode: "csr" }); expect(result.code).not.toContain("__hydrationMetadata__"); }); it("should support helper with array destructured parameter", () => { const code = ` const renderPair = ([first, second]) =>
{first}:{second}
; const PairCard = defineComponent( "pair-card", ({ props }) => renderPair([props.a.value, props.b.value]), ); `; const result = transform(code, { mode: "csr" }); expect(result.code).toContain("__hydrationMetadata__"); expect(result.code).toContain("planFactory"); expect(result.code).not.toContain("unsupportedReason"); }); it("should classify helper with default parameter called with zero args as opaque due to arity mismatch", () => { const code = ` const renderGreeting = (name = "world") =>
Hello {name}
; const DefaultCard = defineComponent( "default-card", () => renderGreeting(), ); `; const result = transform(code, { mode: "csr" }); expect(result.code).toContain("__hydrationMetadata__"); // Arity mismatch: 0 args vs 1 param → opaque-helper-call expect(result.code).toContain("unsupportedReason"); expect(result.code).not.toContain("planFactory"); }); it("should support helper with rest parameter", () => { const code = ` const renderList = (...items) =>
    {items}
; const RestCard = defineComponent( "rest-card", () => renderList("a", "b"), ); `; const result = transform(code, { mode: "csr" }); // Rest params have different count than call args, so arity check should prevent planFactory expect(result.code).not.toContain("planFactory"); }); it("should handle recursive helper references as visited helper bailout", () => { const code = ` const renderA = (x) => renderB(x); const renderB = (x) => renderA(x); const RecCard = defineComponent( "rec-card", ({ props }) => renderA(props.label.value), ); `; const result = transform(code, { mode: "csr" }); // Cycle detection should prevent infinite recursion expect(result.code).toContain("__hydrationMetadata__"); }); it("should handle helper with mismatched arity (more args than params)", () => { const code = ` const renderLabel = (label) =>
{label}
; const ArityCard = defineComponent( "arity-card", ({ props }) => renderLabel(props.a.value, props.b.value), ); `; const result = transform(code, { mode: "csr" }); // Arity mismatch should prevent helper resolution expect(result.code).toContain("__hydrationMetadata__"); expect(result.code).toContain("unsupportedReason"); expect(result.code).not.toContain("planFactory"); }); it("should handle local transparent thunk wrapper with non-thunk argument", () => { const code = ` const withBoundary = (render) => render(); const NonThunkCard = defineComponent( "non-thunk-card", ({ props }) => withBoundary(props.renderFn.value), ); `; const result = transform(code, { mode: "csr" }); // Non-thunk argument to wrapper should be unsupported expect(result.code).toContain("__hydrationMetadata__"); expect(result.code).toContain("unsupportedReason"); expect(result.code).not.toContain("planFactory"); }); it("should handle imported transparent thunk wrapper with non-thunk last argument", () => { const code = ` import { withStore } from "@dathra/core"; const NonThunkImportCard = defineComponent( "non-thunk-import-card", ({ props }) => withStore(store, props.renderFn.value), ); `; const result = transform(code, { mode: "csr" }); expect(result.code).toContain("__hydrationMetadata__"); expect(result.code).toContain("unsupportedReason"); expect(result.code).not.toContain("planFactory"); }); it("should handle member expression callee in getComponentDisplayName", () => { const code = ` const OuterNs = defineComponent( "outer-ns", () =>
, ); `; const result = transform(code, { mode: "csr" }); expect(result.code).toContain("__hydrationMetadata__"); expect(result.code).toContain("planFactory"); expect(result.code).toContain('tagName: "Foo.Bar"'); }); it("should handle walker inJSX re-entry for JSXElement inside expression container", () => { const code = ` const flag = signal(true); const element =
{flag.value ? yes : no}
; `; const result = transform(code); // The walker should not double-transform nested JSX inside an expression container expect(result.code).toContain("fromTree"); expect(result.code).toContain("insert"); }); it("should handle walker inJSX re-entry for JSXFragment inside expression container", () => { const code = ` const flag = signal(true); const element =
{flag.value ? <>yes : <>no}
; `; const result = transform(code); expect(result.code).toContain("fromTree"); expect(result.code).toContain("insert"); }); it("should classify non-function non-FunctionDeclaration component body in getUnsupportedHydrationReason as null", () => { const code = ` const helper = 42; const OpaqueReturn = defineComponent( "opaque-return", ({ props }) => helper, ); `; const result = transform(code, { mode: "csr" }); // Helper is a number literal, not a function, so resolveTopLevelHelperNode won't resolve it // The component returns a non-JSX, non-function-call expression expect(result.code).not.toContain("planFactory"); }); it("should support prelude with function declaration before JSX return", () => { const code = ` const FnPreludeCard = defineComponent( "fn-prelude-card", ({ props }) => { function formatLabel(raw) { return raw.toUpperCase(); } const label = formatLabel(props.label.value); return
{label}
; }, ); `; const result = transform(code, { mode: "csr" }); expect(result.code).toContain("__hydrationMetadata__"); expect(result.code).toContain("planFactory"); expect(result.code).not.toContain("unsupportedReason"); expect(result.code).toContain("function formatLabel(raw)"); }); it("should cover collectBindingNames ObjectPattern RestElement branch via helper with object rest destructuring", () => { const code = ` const renderObj = ({a, ...rest}) =>
{a}{rest.b}
; const RestObjCard = defineComponent( "rest-obj-card", ({ props }) => renderObj({a: props.a.value, b: props.b.value}), ); `; const result = transform(code, { mode: "csr" }); expect(result.code).toContain("__hydrationMetadata__"); expect(result.code).toContain("planFactory"); expect(result.code).not.toContain("unsupportedReason"); }); it("should cover collectBindingNames ArrayPattern branch via helper with array destructuring", () => { const code = ` const renderArr = ([x, y]) =>
{x}:{y}
; const ArrCard = defineComponent( "arr-card", ({ props }) => renderArr([props.x.value, props.y.value]), ); `; const result = transform(code, { mode: "csr" }); expect(result.code).toContain("__hydrationMetadata__"); expect(result.code).toContain("planFactory"); }); it("should cover collectBindingNames AssignmentPattern branch via helper with default destructured param", () => { const code = ` const renderDefault = ({label = "default"}) =>
{label}
; const DefaultDestructCard = defineComponent( "default-destruct-card", ({ props }) => renderDefault({label: props.label.value}), ); `; const result = transform(code, { mode: "csr" }); expect(result.code).toContain("__hydrationMetadata__"); expect(result.code).toContain("planFactory"); }); it("should cover collectBindingNames top-level RestElement via helper with rest param pattern", () => { const code = ` const renderRest = (...items) =>
    {items}
; const TopRestCard = defineComponent( "top-rest-card", () => renderRest("a"), ); `; const result = transform(code, { mode: "csr" }); // Rest param: 1 param pattern vs 1 arg — but RestElement has length 1 and args length 1 // collectBindingNames should visit the RestElement branch expect(result.code).toContain("__hydrationMetadata__"); }); it("should cover renamePatternWithCollisions duplicate binding name in pattern", () => { // A helper whose destructured pattern has a name collision with __dh_host (reserved) const code = ` const renderReserved = ({__dh_host}) =>
{__dh_host}
; const ReservedCard = defineComponent( "reserved-card", ({ props }) => renderReserved({__dh_host: props.label.value}), ); `; const result = transform(code, { mode: "csr" }); expect(result.code).toContain("__hydrationMetadata__"); expect(result.code).toContain("planFactory"); // The reserved name should be renamed to avoid collision expect(result.code).not.toContain("unsupportedReason"); }); it("should cover renameStatementWithCollisions for FunctionDeclaration with reserved name collision", () => { const code = ` const FnCollisionCard = defineComponent( "fn-collision-card", ({ props }) => { function __dh_host(x) { return x; } const label = __dh_host(props.label.value); return
{label}
; }, ); `; const result = transform(code, { mode: "csr" }); expect(result.code).toContain("__hydrationMetadata__"); expect(result.code).toContain("planFactory"); // The function declaration named __dh_host should be renamed }); it("should cover renameStatementWithCollisions fallback for non-VariableDeclaration non-FunctionDeclaration", () => { // ExpressionStatement in prelude is not supported, so this won't produce planFactory // but the line 403 is the fallback for applyRenameMapToStatementReferences // We need a statement type that IS a supported prelude type (VariableDeclaration or FunctionDeclaration) // Line 403 is actually unreachable for supported prelude since only VariableDeclaration and FunctionDeclaration pass isSupportedPlanPreludeStatement // Let's instead test line 374: renameStatementWithCollisions for non-VariableDeclaration non-FunctionDeclaration // Line 374 returns cloneNode(statement) - this is only reachable if the statement is not VariableDeclaration and not FunctionDeclaration // But buildCollisionSafePlanAnalysis only passes preludeStatements which are already filtered by isSupportedPlanPreludeStatement // So lines 374 and 403 may be structurally unreachable in the current code path. Let's skip these and focus on reachable lines. // Instead, test a helper chain where a param name collides with an existing prelude variable const code = ` const renderInner = (count) => {count}; const WrapperCard = defineComponent( "wrapper-card", ({ props }) => { const count = signal(0); return renderInner(count.value); }, ); `; const result = transform(code, { mode: "csr" }); expect(result.code).toContain("__hydrationMetadata__"); expect(result.code).toContain("planFactory"); // count in prelude and count as helper param should not collide }); it("should cover containsJSXNode JSXFragment detection in prelude", () => { const code = ` const FragPreludeCard = defineComponent( "frag-prelude-card", () => { const frag = <>fragment; return
{frag}
; }, ); `; const result = transform(code, { mode: "csr" }); // JSXFragment in prelude should prevent planFactory extraction expect(result.code).not.toContain("planFactory"); }); it("should classify deeply nested IfStatement hidden in an IIFE as unsupported-component-body", () => { const code = ` const DeepIfCard = defineComponent( "deep-if-card", () => { console.log("init"); const val = (() => { if (true) return 1; return 2; })(); return
{val}
; }, ); `; const result = transform(code, { mode: "csr" }); expect(result.code).toContain("__hydrationMetadata__"); expect(result.code).toContain( 'unsupportedReason: "unsupported-component-body"', ); expect(result.code).not.toContain("planFactory"); }); it("should classify deeply nested SwitchStatement hidden in an IIFE as unsupported-component-body", () => { const code = ` const DeepSwitchCard = defineComponent( "deep-switch-card", () => { console.log("init"); const val = (() => { switch(1) { case 1: return "a"; default: return "b"; } })(); return
{val}
; }, ); `; const result = transform(code, { mode: "csr" }); expect(result.code).toContain("__hydrationMetadata__"); expect(result.code).toContain( 'unsupportedReason: "unsupported-component-body"', ); }); it("should cover getExplicitNestedBoundary with JSX component child having client:load (nested island)", () => { const code = ` const OuterIsland = defineComponent( "outer-island", () =>
, ); `; const result = transform(code, { mode: "csr" }); expect(result.code).toContain("__hydrationMetadata__"); expect(result.code).toContain("planFactory"); expect(result.code).toContain("nestedBoundaries"); expect(result.code).toContain('"InnerComp"'); expect(result.code).toContain('"load"'); }); it("should cover getExplicitNestedBoundary with component child having no client directive", () => { // Component child without client:* → getExplicitNestedBoundary returns null (line 1110) const code = ` const NoDirectiveOuter = defineComponent( "no-directive-outer", () =>
, ); `; const result = transform(code, { mode: "csr" }); expect(result.code).toContain("__hydrationMetadata__"); expect(result.code).toContain("planFactory"); expect(result.code).toContain("nestedBoundaries: []"); }); it("should cover getExplicitNestedBoundary with SpreadElement in component props (line 1085)", () => { const code = ` const extras = { role: "main" }; const SpreadIslandOuter = defineComponent( "spread-island-outer", () =>
, ); `; const result = transform(code, { mode: "csr" }); expect(result.code).toContain("__hydrationMetadata__"); expect(result.code).toContain("planFactory"); // SpreadElement in props → property.type !== "Property" → continue (line 1085) }); it("should cover resolveTopLevelHelperNode thunk wrapper with null frame (line 738)", () => { const code = ` import { withStore } from "@dathra/core"; const NullFrameCard = defineComponent( "null-frame-card", () => withStore(store, () => { console.log("side-effect"); }), ); `; const result = transform(code, { mode: "csr" }); expect(result.code).toContain("__hydrationMetadata__"); expect(result.code).toContain("unsupportedReason"); }); it("should cover resolveTopLevelHelperNode helper with no render frame (line 767)", () => { const code = ` const setupHelper = (ctx) => { ctx.init(); }; const NoFrameCard = defineComponent( "no-frame-card", ({ props }) => setupHelper(props), ); `; const result = transform(code, { mode: "csr" }); expect(result.code).toContain("__hydrationMetadata__"); expect(result.code).toContain("unsupportedReason"); }); it("should cover createPlanBinding spread type in direct plan extraction", () => { const code = ` const SpreadCard = defineComponent( "spread-card", ({ props }) =>
content
, ); `; const result = transform(code, { mode: "csr" }); expect(result.code).toContain("__hydrationMetadata__"); expect(result.code).toContain("planFactory"); expect(result.code).toContain('"spread"'); }); it("should cover resolveComponentRenderAnalysis null finalFrame from branching helper", () => { const code = ` const innerHelper = (x) => { x.toString(); }; const outerHelper = (val) => innerHelper(val); const NullFinalCard = defineComponent( "null-final-card", ({ props }) => outerHelper(props.label.value), ); `; const result = transform(code, { mode: "csr" }); expect(result.code).toContain("__hydrationMetadata__"); expect(result.code).toContain("unsupportedReason"); }); it("should cover extractFunctionRenderFrame null return argument (line 985)", () => { const code = ` const NullRetCard = defineComponent( "null-ret-card", () => { const x = 1; return; }, ); `; const result = transform(code, { mode: "csr" }); expect(result.code).toContain("__hydrationMetadata__"); expect(result.code).toContain("unsupportedReason"); }); it("should cover createPlanBinding spread type through helper extraction", () => { const code = ` const SpreadCard = defineComponent( "spread-card", ({ props }) =>
content
, ); `; const result = transform(code, { mode: "csr" }); expect(result.code).toContain("__hydrationMetadata__"); expect(result.code).toContain("planFactory"); expect(result.code).toContain('"spread"'); }); it("should cover resolveComponentRenderAnalysis null finalFrame from object spread helper", () => { const code = ` const innerHelper = (x) => { x.toString(); }; const outerHelper = (val) => innerHelper(val); const NullFinalCard = defineComponent( "null-final-card", ({ props }) => outerHelper(props.label.value), ); `; const result = transform(code, { mode: "csr" }); expect(result.code).toContain("__hydrationMetadata__"); expect(result.code).toContain("unsupportedReason"); }); it("should cover getUnsupportedHydrationReason containsNodeType IfStatement fallback (line 1242)", () => { // This test verifies that NestedIfCard with an IIFE containing IfStatement // but no ExpressionStatement in prelude → extractFunctionRenderFrame succeeds // → resolveComponentRenderAnalysis produces a valid analysis → planFactory is generated. // The IfStatement inside the IIFE doesn't affect planFactory generation because // it's inside a VariableDeclaration initializer, not a top-level branching statement. const code = ` const NestedIfCard = defineComponent( "nested-if-card", () => { const result = (() => { if (true) return "a"; return "b"; })(); return
{result}
; }, ); `; const result = transform(code, { mode: "csr" }); // extractFunctionRenderFrame succeeds (VariableDeclaration + ReturnStatement) // resolveComponentRenderAnalysis returns a valid analysis → planFactory expect(result.code).toContain("__hydrationMetadata__"); expect(result.code).toContain("planFactory"); expect(result.code).not.toContain("unsupportedReason"); }); it("should cover resolveComponentRenderAnalysis null finalFrame from helper chain without return", () => { // A helper chain where the final helper has no return expression const code = ` const innerHelper = (x) => { x.toString(); }; const outerHelper = (val) => innerHelper(val); const NullFinalCard = defineComponent( "null-final-card", ({ props }) => outerHelper(props.label.value), ); `; const result = transform(code, { mode: "csr" }); // innerHelper has no return → resolveComponentRenderAnalysis returns null expect(result.code).toContain("__hydrationMetadata__"); expect(result.code).toContain("unsupportedReason"); }); it("should cover walker inJSX guard for nested JSXElement (lines 1649-1650)", () => { // The walker enters JSXElement, sets inJSX=true, then encounters a nested JSXElement // This happens naturally when processing
text
// The guard at line 1648-1650 prevents double-transformation const code = ` const element =
deep
; `; const result = transform(code); expect(result.code).toContain("fromTree"); // Should produce a single template, not nested transforms expect(result.code).toContain("div"); expect(result.code).toContain("span"); expect(result.code).toContain("em"); }); it("should cover walker inJSX guard for nested JSXFragment (lines 1676-1677)", () => { // After entering a JSXElement, encountering a JSXFragment as child const code = ` const element =
<>nested fragment
; `; const result = transform(code); expect(result.code).toContain("fromTree"); expect(result.code).toContain("nested fragment"); }); it("should cover isJSXElement/isJSXFragment type guard after inJSX check (lines 1653, 1680)", () => { // These lines are type guards that should always pass after the walker visitor fires // They are covered when any JSX is processed through the walker // Testing with a simple component that has both element and fragment children const code = ` const mixed =
el
<>frag
; `; const result = transform(code); expect(result.code).toContain("fromTree"); }); it("should throw when client:media is missing a string literal value", () => { const code = ` const element = ; `; expect(() => transform(code)).toThrow( "client:media requires a string literal media query", ); }); it("should throw when valueless directives receive a value", () => { const code = ` const element = ; `; expect(() => transform(code)).toThrow( "client:visible does not accept a value", ); }); it("should throw for client directives on html elements", () => { const code = ` const element =
bad
; `; expect(() => transform(code)).toThrow( "client:* directives are only supported on component elements", ); }); it("should throw for multiple client directives on one component", () => { const code = ` const element = ; `; expect(() => transform(code)).toThrow( "Multiple client:* directives are not allowed", ); }); it("should throw for unknown client directives", () => { const code = ` const element = ; `; expect(() => transform(code)).toThrow("Unknown client:* directive"); }); it("should throw when client directives collide with reserved island metadata props", () => { const code = ` const element = ; `; expect(() => transform(code)).toThrow( "client:* directives cannot be combined with explicit data-dh-island metadata", ); }); it("should transform load:onClick on html elements into client target metadata plus click binding", () => { const code = ` const element = ; `; const result = transform(code, { mode: "csr" }); expect(result.code).toContain("data-dh-client-target="); expect(result.code).toContain("data-dh-client-strategy="); expect(result.code).toContain("load"); expect(result.code).toContain("event"); expect(result.code).toContain('"click"'); }); it("should transform interaction:onClick on html elements into interaction target metadata plus click binding", () => { const code = ` const element = ; `; const result = transform(code, { mode: "ssr" }); expect(result.code).toContain("data-dh-client-target="); expect(result.code).toContain("data-dh-client-strategy="); expect(result.code).toContain("interaction"); }); it("should transform visible:onClick on html elements into visible target metadata plus click binding", () => { const code = ` const element = ; `; const result = transform(code, { mode: "csr" }); expect(result.code).toContain("data-dh-client-target="); expect(result.code).toContain("data-dh-client-strategy="); expect(result.code).toContain("visible"); expect(result.code).toContain('"click"'); expect(result.code).not.toContain("visible:onClick"); }); it("should transform idle:onClick on html elements into idle target metadata plus click binding", () => { const code = ` const element = ; `; const result = transform(code, { mode: "ssr" }); expect(result.code).toContain("data-dh-client-target="); expect(result.code).toContain("data-dh-client-strategy="); expect(result.code).toContain("idle"); expect(result.code).not.toContain("idle:onClick"); }); it("should preserve the canonical colocated metadata contract keys", () => { const code = ` const element = ; `; const result = transform(code, { mode: "csr" }); expect(result.code).toContain("data-dh-client-target="); expect(result.code).toContain("data-dh-client-strategy="); expect(result.code).toContain("idle"); }); it("should support all canonical colocated strategies for onClick", () => { for (const strategy of COLOCATED_CLIENT_STRATEGIES) { const code = ` const element = ; `; const result = transform(code, { mode: "csr" }); expect(result.code).toContain("data-dh-client-target="); expect(result.code).toContain("data-dh-client-strategy="); expect(result.code).toContain(strategy); expect(result.code).toContain('"click"'); } }); it("should throw when load:onClick and interaction:onClick are mixed in one jsx root", () => { const code = ` const element = (
); `; expect(() => transform(code)).toThrow( "Mixed colocated client strategies are not supported in one JSX root", ); }); it("should throw when visible:onClick and idle:onClick are mixed in one jsx root", () => { const code = ` const element = (
); `; expect(() => transform(code)).toThrow( "Mixed colocated client strategies are not supported in one JSX root", ); }); it("should throw when visible:onClick and load:onClick are mixed in one jsx root", () => { const code = ` const element = (
); `; expect(() => transform(code)).toThrow( "Mixed colocated client strategies are not supported in one JSX root", ); }); it("should throw when idle:onClick and interaction:onClick are mixed in one jsx root", () => { const code = ` const element = (
); `; expect(() => transform(code)).toThrow( "Mixed colocated client strategies are not supported in one JSX root", ); }); it("should throw when host level client directives mix with colocated client directives in one component render subtree", () => { const clientDirectiveCode = ` const element = ( ); `; const explicitMetadataCode = ` const element = ( ); `; expect(() => transform(clientDirectiveCode)).toThrow( "host-level client:* directives or data-dh-island metadata cannot be combined with colocated client directives in the same component render subtree", ); expect(() => transform(explicitMetadataCode)).toThrow( "host-level client:* directives or data-dh-island metadata cannot be combined with colocated client directives in the same component render subtree", ); }); it("should throw when author supplies compiler reserved client metadata", () => { const targetCode = ` const element = ; `; const strategyCode = ` const element = ; `; const eventCode = ` const element = ; `; expect(() => transform(targetCode)).toThrow( "data-dh-client-target is compiler-reserved metadata and cannot be authored directly", ); expect(() => transform(strategyCode)).toThrow( "data-dh-client-strategy is compiler-reserved metadata and cannot be authored directly", ); expect(() => transform(eventCode)).toThrow( "data-dh-client-event is compiler-reserved metadata and cannot be authored directly", ); }); it("should throw when colocated directives are used on svg elements", () => { const code = ` const element = doThing()}>; `; expect(() => transform(code)).toThrow( "visible:onClick is only supported on HTML elements", ); }); it("should transform load:onMouseEnter on html elements into load target metadata plus mouseenter binding", () => { const code = ` const element = ; `; const result = transform(code, { mode: "csr" }); expect(result.code).toContain("data-dh-client-target="); expect(result.code).toContain("data-dh-client-strategy="); expect(result.code).toContain("load"); expect(result.code).toContain('"mouseenter"'); }); it("should transform visible:onFocus and idle:onScroll on html elements", () => { const visibleCode = ` const element = ; `; const idleCode = ` const element = ; `; const visibleResult = transform(visibleCode, { mode: "csr" }); const idleResult = transform(idleCode, { mode: "csr" }); expect(visibleResult.code).toContain("visible"); expect(visibleResult.code).toContain('"focus"'); expect(idleResult.code).toContain("idle"); expect(idleResult.code).toContain('"scroll"'); }); it("should keep interaction colocated directives limited to onClick", () => { const code = ` const element = ; `; const result = transform(code, { mode: "csr" }); expect(result.code).toContain("interaction"); expect(result.code).toContain('"keydown"'); expect(result.code).toContain("data-dh-client-event="); }); it("should throw when mixed interaction event types are used in one jsx root", () => { const code = ` const element = (
); `; expect(() => transform(code)).toThrow( "Mixed colocated interaction event types are not supported in one JSX root", ); }); it("should transform component load:onClick into host metadata plus client action registration", () => { const code = ` const handleClick = () => doThing(); const element = ; `; const result = transform(code, { mode: "csr" }); expect(result.code).toContain( 'registerClientAction("dh-ca-1", (__dh_payload, __dh_host) =>', ); expect(result.code).toContain("return handleClick;"); expect(result.code).toContain('"data-dh-island": "load"'); expect(result.code).toContain('"data-dh-client-actions"'); expect(result.code).toContain("dh-ca-1"); expect(result.code).not.toContain("load:onClick"); }); it("should transform component interaction:onKeyDown into host metadata plus client action registration", () => { const code = ` const handleKeyDown = (event) => report(event.key); const element = ; `; const result = transform(code, { mode: "csr" }); expect(result.code).toContain( 'registerClientAction("dh-ca-1", (__dh_payload, __dh_host) =>', ); expect(result.code).toContain("return handleKeyDown;"); expect(result.code).toContain('"data-dh-island": "interaction"'); expect(result.code).toContain('"data-dh-island-value": "keydown"'); expect(result.code).toContain('"data-dh-client-actions"'); expect(result.code).toContain("dh-ca-1"); expect(result.code).not.toContain("interaction:onKeyDown"); }); it("should reject component-target colocated handlers that capture local bindings", () => { const code = ` const Parent = defineComponent("x-parent", () => { const localCount = signal(0); return bump(localCount)} />; }); `; expect(() => transform(code)).toThrow( "[dathra] load:onClick component-target colocated handlers cannot capture local bindings: bump, localCount", ); }); it("should serialize local const captures for component-target inline handlers", () => { const code = ` function report(value) { return value; } const Parent = defineComponent("x-parent", () => { const label = "captured-label"; return report(label)} />; }); `; const result = transform(code, { mode: "csr" }); expect(result.code).toContain('registerClientAction("dh-ca-1"'); expect(result.code).toContain("__dh_payload"); expect(result.code).toContain('"data-dh-client-actions"'); expect(result.code).toContain('payload: { label: "captured-label" }'); expect(result.code).toContain('"captured-label"'); }); it("should reject component-target interaction:onFocus because child host cannot observe focus", () => { const code = ` const element = ; `; expect(() => transform(code)).toThrow( "[dathra] interaction:onFocus is not supported on component targets because the child host cannot observe that event without an explicit host re-emit", ); }); it("should throw when mixed colocated strategies appear across nested jsx transforms", () => { const code = ` const element = (
); `; expect(() => transform(code)).toThrow( "Mixed colocated client strategies are not supported in one JSX root", ); }); // --- Semantic coverage: async / generator / loop / try-catch / class / exotic patterns --- it("should classify async arrow expression body as unsupported-component-body", () => { const code = ` const AsyncCard = defineComponent( "async-card", async () =>
hi
, ); `; const result = transform(code, { mode: "csr" }); expect(result.code).toContain("__hydrationMetadata__"); expect(result.code).toContain( 'unsupportedReason: "unsupported-component-body"', ); expect(result.code).not.toContain("planFactory"); }); it("should classify async arrow block body with await as unsupported-component-body", () => { const code = ` const AsyncBlockCard = defineComponent( "async-block-card", async () => { const data = await fetchData(); return
{data}
; }, ); `; const result = transform(code, { mode: "csr" }); expect(result.code).toContain("__hydrationMetadata__"); expect(result.code).toContain( 'unsupportedReason: "unsupported-component-body"', ); expect(result.code).not.toContain("planFactory"); }); it("should classify async function expression as unsupported-component-body", () => { const code = ` const AsyncFnCard = defineComponent( "async-fn-card", async function setup() { return
hi
; }, ); `; const result = transform(code, { mode: "csr" }); expect(result.code).toContain("__hydrationMetadata__"); expect(result.code).toContain( 'unsupportedReason: "unsupported-component-body"', ); expect(result.code).not.toContain("planFactory"); }); it("should classify generator function expression as unsupported-component-body", () => { const code = ` const GenCard = defineComponent( "gen-card", function* () { yield
hi
; }, ); `; const result = transform(code, { mode: "csr" }); expect(result.code).toContain("__hydrationMetadata__"); expect(result.code).toContain( 'unsupportedReason: "unsupported-component-body"', ); expect(result.code).not.toContain("planFactory"); }); it("should classify for-loop in setup body as unsupported-component-body", () => { const code = ` const LoopCard = defineComponent( "loop-card", () => { const items = []; for (let i = 0; i < 5; i++) items.push(i); return
    {items}
; }, ); `; const result = transform(code, { mode: "csr" }); expect(result.code).toContain("__hydrationMetadata__"); expect(result.code).toContain( 'unsupportedReason: "unsupported-component-body"', ); expect(result.code).not.toContain("planFactory"); }); it("should classify while-loop in setup body as unsupported-component-body", () => { const code = ` const WhileCard = defineComponent( "while-card", () => { let count = 0; while (count < 3) count++; return
{count}
; }, ); `; const result = transform(code, { mode: "csr" }); expect(result.code).toContain("__hydrationMetadata__"); expect(result.code).toContain( 'unsupportedReason: "unsupported-component-body"', ); expect(result.code).not.toContain("planFactory"); }); it("should classify do-while loop in setup body as unsupported-component-body", () => { const code = ` const DoWhileCard = defineComponent( "do-while-card", () => { let count = 0; do { count++; } while (count < 3); return
{count}
; }, ); `; const result = transform(code, { mode: "csr" }); expect(result.code).toContain("__hydrationMetadata__"); expect(result.code).toContain( 'unsupportedReason: "unsupported-component-body"', ); expect(result.code).not.toContain("planFactory"); }); it("should classify for-in loop in setup body as unsupported-component-body", () => { const code = ` const ForInCard = defineComponent( "for-in-card", () => { const obj = { a: 1 }; for (const key in obj) console.log(key); return
done
; }, ); `; const result = transform(code, { mode: "csr" }); expect(result.code).toContain("__hydrationMetadata__"); expect(result.code).toContain( 'unsupportedReason: "unsupported-component-body"', ); expect(result.code).not.toContain("planFactory"); }); it("should classify for-of loop in setup body as unsupported-component-body", () => { const code = ` const ForOfCard = defineComponent( "for-of-card", () => { const items = [1, 2, 3]; for (const item of items) console.log(item); return
done
; }, ); `; const result = transform(code, { mode: "csr" }); expect(result.code).toContain("__hydrationMetadata__"); expect(result.code).toContain( 'unsupportedReason: "unsupported-component-body"', ); expect(result.code).not.toContain("planFactory"); }); it("should classify try-catch in setup body as unsupported-component-body", () => { const code = ` const TryCatchCard = defineComponent( "try-catch-card", () => { try { riskyOp(); } catch (e) { console.error(e); } return
safe
; }, ); `; const result = transform(code, { mode: "csr" }); expect(result.code).toContain("__hydrationMetadata__"); expect(result.code).toContain( 'unsupportedReason: "unsupported-component-body"', ); expect(result.code).not.toContain("planFactory"); }); it("should classify try-finally in setup body as unsupported-component-body", () => { const code = ` const TryFinallyCard = defineComponent( "try-finally-card", () => { try { init(); } finally { cleanup(); } return
done
; }, ); `; const result = transform(code, { mode: "csr" }); expect(result.code).toContain("__hydrationMetadata__"); expect(result.code).toContain( 'unsupportedReason: "unsupported-component-body"', ); expect(result.code).not.toContain("planFactory"); }); it("should classify class expression component arg as no metadata", () => { const code = ` const ClassCard = defineComponent( "class-card", class { render() { return "
hi
"; } }, ); `; const result = transform(code, { mode: "csr" }); expect(result.code).not.toContain("__hydrationMetadata__"); expect(result.code).not.toContain("planFactory"); }); it("should classify deeply nested if-statement inside function declaration prelude as supported planFactory", () => { const code = ` const DeepBranchCard = defineComponent( "deep-branch-card", ({ props }) => { const items = []; function buildItems() { if (props.ready.value) { items.push("ready"); } } return
{items}
; }, ); `; const result = transform(code, { mode: "csr" }); expect(result.code).toContain("__hydrationMetadata__"); // Function declaration with branching inside is a valid prelude statement; // the JSX shape is static, so planFactory is generated expect(result.code).toContain("planFactory"); expect(result.code).not.toContain("unsupportedReason"); }); it("should classify throw statement in setup as unsupported-component-body", () => { const code = ` const ThrowCard = defineComponent( "throw-card", () => { throw new Error("not implemented"); }, ); `; const result = transform(code, { mode: "csr" }); expect(result.code).toContain("__hydrationMetadata__"); expect(result.code).toContain( 'unsupportedReason: "unsupported-component-body"', ); expect(result.code).not.toContain("planFactory"); }); it("should classify labeled statement in setup as unsupported-component-body", () => { const code = ` const LabeledCard = defineComponent( "labeled-card", () => { outer: for (let i = 0; i < 3; i++) break outer; return
done
; }, ); `; const result = transform(code, { mode: "csr" }); expect(result.code).toContain("__hydrationMetadata__"); expect(result.code).toContain( 'unsupportedReason: "unsupported-component-body"', ); expect(result.code).not.toContain("planFactory"); }); it("should support method-like helper (object property) when the object literal is static", () => { const code = ` const helpers = { render: (label) =>
{label}
}; const MethodCard = defineComponent( "method-card", ({ props }) => helpers.render(props.label.value), ); `; const result = transform(code, { mode: "csr" }); expect(result.code).toContain("__hydrationMetadata__"); expect(result.code).toContain("planFactory"); expect(result.code).not.toContain("unsupportedReason"); expect(result.code).toContain('kind: "text"'); }); it("should classify closure-captured helper as opaque-helper-call", () => { const code = ` const makeRenderer = () => (label) =>
{label}
; const render = makeRenderer(); const ClosureCard = defineComponent( "closure-card", ({ props }) => render(props.label.value), ); `; const result = transform(code, { mode: "csr" }); expect(result.code).toContain("__hydrationMetadata__"); // render is a VariableDeclarator with init = makeRenderer() (CallExpression) // not a function-like => resolveTopLevelHelperNode should bail => opaque expect(result.code).toContain('unsupportedReason: "opaque-helper-call"'); expect(result.code).not.toContain("planFactory"); }); it("should classify HOC factory pattern (CallExpression arg) as no metadata", () => { const code = ` const withTheme = (theme) => (ctx) =>
{ctx.children}
; const HOCCard = defineComponent( "hoc-card", withTheme("dark"), ); `; const result = transform(code, { mode: "csr" }); // withTheme("dark") is a CallExpression, not function-like → no metadata at all expect(result.code).not.toContain("__hydrationMetadata__"); expect(result.code).not.toContain("planFactory"); }); it("should classify runtime-selected helper as opaque-helper-call", () => { const code = ` const renderA = (x) =>
{x}
; const renderB = (x) => {x}; const RuntimeCard = defineComponent( "runtime-card", ({ props }) => (props.mode.value === "a" ? renderA : renderB)(props.label.value), ); `; const result = transform(code, { mode: "csr" }); expect(result.code).toContain("__hydrationMetadata__"); // The callee is a ConditionalExpression — cannot resolve to a known helper expect(result.code).toContain('unsupportedReason: "opaque-helper-call"'); expect(result.code).not.toContain("planFactory"); }); it("should handle onMouseEnter event in component setup", () => { const code = ` const HoverCard = defineComponent( "hover-card", ({ props }) =>
props.onHover.value()}>hover me
, ); `; const result = transform(code, { mode: "csr" }); expect(result.code).toContain("__hydrationMetadata__"); expect(result.code).toContain("planFactory"); expect(result.code).toContain('kind: "event"'); }); it("should handle onKeyDown event in component setup", () => { const code = ` const KeyCard = defineComponent( "key-card", ({ props }) => props.onKey.value()} />, ); `; const result = transform(code, { mode: "csr" }); expect(result.code).toContain("__hydrationMetadata__"); expect(result.code).toContain("planFactory"); expect(result.code).toContain('kind: "event"'); }); it("should handle onFocus and onBlur events in component setup", () => { const code = ` const FocusCard = defineComponent( "focus-card", ({ props }) => props.onFocus.value()} onBlur={() => props.onBlur.value()} />, ); `; const result = transform(code, { mode: "csr" }); expect(result.code).toContain("__hydrationMetadata__"); expect(result.code).toContain("planFactory"); expect(result.code).toContain('kind: "event"'); }); it("should handle onInput event in component setup", () => { const code = ` const InputCard = defineComponent( "input-card", ({ props }) => props.onChange.value(e.target.value)} />, ); `; const result = transform(code, { mode: "csr" }); expect(result.code).toContain("__hydrationMetadata__"); expect(result.code).toContain("planFactory"); expect(result.code).toContain('kind: "event"'); }); it("should support setup with deeply nested switch inside function declaration prelude", () => { const code = ` const DeepSwitchCard = defineComponent( "deep-switch-card", () => { function buildContent(mode) { switch (mode) { case "a": return "A"; default: return "B"; } } return
{buildContent("a")}
; }, ); `; const result = transform(code, { mode: "csr" }); expect(result.code).toContain("__hydrationMetadata__"); // Function declaration with switch inside is valid prelude; JSX shape is static expect(result.code).toContain("planFactory"); expect(result.code).not.toContain("unsupportedReason"); }); it("should support setup with deeply nested if inside variable declaration prelude", () => { const code = ` const DeepIfCard = defineComponent( "deep-if-card", () => { const compute = () => { if (true) return 1; return 0; }; return
{compute()}
; }, ); `; const result = transform(code, { mode: "csr" }); expect(result.code).toContain("__hydrationMetadata__"); // Variable declaration with branching inside arrow is valid prelude; JSX shape is static expect(result.code).toContain("planFactory"); expect(result.code).not.toContain("unsupportedReason"); }); // --- Batch 2: spread merges, nested islands, TS syntax, exotic edge cases --- it("should support deeply nested spread merges when the merged binding is serializable", () => { const code = ` const extras = { role: "alert" }; const DeepSpreadCard = defineComponent( "deep-spread-card", () => (

deeply nested spread

), ); `; const result = transform(code, { mode: "csr" }); expect(result.code).toContain("__hydrationMetadata__"); expect(result.code).toContain("planFactory"); expect(result.code).toContain('kind: "spread"'); expect(result.code).not.toContain("unsupportedReason"); }); it("should allow normalizable spread (no SpreadElement inside ObjectExpression) to produce planFactory", () => { const code = ` const SimpleSpreadCard = defineComponent( "simple-spread-card", ({ props }) =>
spread ok
, ); `; const result = transform(code, { mode: "csr" }); expect(result.code).toContain("__hydrationMetadata__"); // Simple spread (not ObjectExpression with SpreadElement) is allowed expect(result.code).toContain("planFactory"); expect(result.code).toContain('kind: "spread"'); }); it("should support multi-level nested islands with client:load + client:visible", () => { const code = ` const OuterCard = defineComponent( "outer-card", ({ props }) => (
), ); `; const result = transform(code, { mode: "csr" }); expect(result.code).toContain("__hydrationMetadata__"); expect(result.code).toContain("planFactory"); expect(result.code).toContain("nestedBoundaries"); expect(result.code).toContain('"MiddleIsland"'); expect(result.code).toContain('"load"'); expect(result.code).toContain('"BottomIsland"'); expect(result.code).toContain('"visible"'); }); it("should support nested island inside fragment child", () => { const code = ` const FragIslandCard = defineComponent( "frag-island-card", () => ( <>
content
), ); `; const result = transform(code, { mode: "csr" }); expect(result.code).toContain("__hydrationMetadata__"); expect(result.code).toContain("planFactory"); expect(result.code).toContain("nestedBoundaries"); expect(result.code).toContain('"InnerIsland"'); expect(result.code).toContain('"idle"'); }); it("should support nested island with client:interaction", () => { const code = ` const InteractionOuter = defineComponent( "interaction-outer", () =>
, ); `; const result = transform(code, { mode: "csr" }); expect(result.code).toContain("__hydrationMetadata__"); expect(result.code).toContain("planFactory"); expect(result.code).toContain("nestedBoundaries"); expect(result.code).toContain('"InteractiveChild"'); expect(result.code).toContain('"interaction"'); }); it("should handle TypeScript type assertion in prelude as expression statement (unsupported prelude)", () => { const code = ` const TSAsCard = defineComponent( "ts-as-card", () => { const x = getSomething() as string; return
{x}
; }, ); `; const result = transform(code, { mode: "csr" }); expect(result.code).toContain("__hydrationMetadata__"); // TSAsExpression is inside a VariableDeclaration which IS a supported prelude. // After TS transform (esbuild strips types), it becomes a normal VariableDeclaration. // So this should produce a planFactory. expect(result.code).toContain("planFactory"); }); it("should handle multiple const declarations in prelude", () => { const code = ` const MultiConstCard = defineComponent( "multi-const-card", ({ props }) => { const a = props.x.value; const b = props.y.value; const c = a + b; return
{c}
; }, ); `; const result = transform(code, { mode: "csr" }); expect(result.code).toContain("__hydrationMetadata__"); expect(result.code).toContain("planFactory"); expect(result.code).not.toContain("unsupportedReason"); }); it("should handle let declaration in prelude as unsupported (only const/function allowed)", () => { const code = ` const LetCard = defineComponent( "let-card", () => { let x = 0; return
{x}
; }, ); `; const result = transform(code, { mode: "csr" }); // VariableDeclaration with kind "let" is still a VariableDeclaration // isSupportedPlanPreludeStatement checks for VariableDeclaration // so it should work... expect(result.code).toContain("__hydrationMetadata__"); }); it("should handle empty return in block body as no planFactory", () => { const code = ` const EmptyReturnCard = defineComponent( "empty-return-card", () => { return; }, ); `; const result = transform(code, { mode: "csr" }); // return without argument → extractFunctionRenderFrame returns null expect(result.code).not.toContain("planFactory"); }); it("should classify setup with only non-JSX return as unsupported-component-body", () => { const code = ` const StringReturnCard = defineComponent( "string-return-card", () => { return "not jsx"; }, ); `; const result = transform(code, { mode: "csr" }); expect(result.code).toContain("__hydrationMetadata__"); expect(result.code).toContain( 'unsupportedReason: "unsupported-component-body"', ); expect(result.code).not.toContain("planFactory"); }); it("should classify setup returning a number literal as unsupported-component-body", () => { const code = ` const NumberReturnCard = defineComponent( "number-return-card", () => { return 42; }, ); `; const result = transform(code, { mode: "csr" }); expect(result.code).toContain("__hydrationMetadata__"); expect(result.code).toContain( 'unsupportedReason: "unsupported-component-body"', ); expect(result.code).not.toContain("planFactory"); }); it("should classify setup returning an array literal as unsupported-component-body", () => { const code = ` const ArrayReturnCard = defineComponent( "array-return-card", () => { return [1, 2, 3]; }, ); `; const result = transform(code, { mode: "csr" }); expect(result.code).toContain("__hydrationMetadata__"); expect(result.code).toContain( 'unsupportedReason: "unsupported-component-body"', ); expect(result.code).not.toContain("planFactory"); }); it("should classify setup returning an identifier as unsupported-component-body", () => { const code = ` const IdentReturnCard = defineComponent( "ident-return-card", () => { return someVariable; }, ); `; const result = transform(code, { mode: "csr" }); expect(result.code).toContain("__hydrationMetadata__"); expect(result.code).toContain( 'unsupportedReason: "unsupported-component-body"', ); expect(result.code).not.toContain("planFactory"); }); it("should classify setup with window reference inside typeof as supported (environment probe)", () => { const code = ` const TypeofWindowCard = defineComponent( "typeof-window-card", () => { const isSSR = typeof window === "undefined"; return
{isSSR ? "server" : "client"}
; }, ); `; const result = transform(code, { mode: "csr" }); expect(result.code).toContain("__hydrationMetadata__"); // typeof window is an environment probe, NOT imperative-dom-query. // BUT the return contains ConditionalExpression → runtime-branching expect(result.code).not.toContain( 'unsupportedReason: "imperative-dom-query"', ); }); it("should handle document reference NOT inside typeof as imperative-dom-query", () => { const code = ` const DocQueryCard = defineComponent( "doc-query-card", () => { const el = document.getElementById("root"); return
found
; }, ); `; const result = transform(code, { mode: "csr" }); expect(result.code).toContain("__hydrationMetadata__"); expect(result.code).toContain( 'unsupportedReason: "imperative-dom-query"', ); }); it("should handle shadowRoot reference as imperative-dom-query", () => { const code = ` const ShadowCard = defineComponent( "shadow-card", ({ host }) => { const shadow = host.shadowRoot; return
shadow
; }, ); `; const result = transform(code, { mode: "csr" }); expect(result.code).toContain("__hydrationMetadata__"); expect(result.code).toContain( 'unsupportedReason: "imperative-dom-query"', ); }); it("should not classify shadowed local document binding as imperative-dom-query", () => { const code = ` const LocalDocumentCard = defineComponent( "local-document-card", () => { const document = { label: "local" }; return
{document.label}
; }, ); `; const result = transform(code, { mode: "csr" }); expect(result.code).toContain("__hydrationMetadata__"); expect(result.code).toContain("planFactory"); expect(result.code).not.toContain( 'unsupportedReason: "imperative-dom-query"', ); }); it("should still classify aliases to global document as imperative-dom-query", () => { const code = ` const AliasDocumentCard = defineComponent( "alias-document-card", () => { const document = globalThis.document; return
{document.getElementById("root")?.id}
; }, ); `; const result = transform(code, { mode: "csr" }); expect(result.code).toContain("__hydrationMetadata__"); expect(result.code).toContain( 'unsupportedReason: "imperative-dom-query"', ); }); it("should ignore nested shadowed document binding inside IIFE", () => { const code = ` const NestedShadowDocumentCard = defineComponent( "nested-shadow-document-card", () => { const value = (() => { const document = { label: "nested" }; return document.label; })(); return
{value}
; }, ); `; const result = transform(code, { mode: "csr" }); expect(result.code).toContain("planFactory"); expect(result.code).not.toContain( 'unsupportedReason: "imperative-dom-query"', ); }); it("should classify new Text() as node-identity-reuse", () => { const code = ` const TextNodeCard = defineComponent( "text-node-card", () => { const t = new Text("hello"); return
{t}
; }, ); `; const result = transform(code, { mode: "csr" }); expect(result.code).toContain("__hydrationMetadata__"); expect(result.code).toContain('unsupportedReason: "node-identity-reuse"'); }); it("should classify new Comment() as node-identity-reuse", () => { const code = ` const CommentNodeCard = defineComponent( "comment-node-card", () => { const c = new Comment("marker"); return
{c}
; }, ); `; const result = transform(code, { mode: "csr" }); expect(result.code).toContain("__hydrationMetadata__"); expect(result.code).toContain('unsupportedReason: "node-identity-reuse"'); }); it("should classify new DocumentFragment() as node-identity-reuse", () => { const code = ` const FragNodeCard = defineComponent( "frag-node-card", () => { const f = new DocumentFragment(); return
{f}
; }, ); `; const result = transform(code, { mode: "csr" }); expect(result.code).toContain("__hydrationMetadata__"); expect(result.code).toContain('unsupportedReason: "node-identity-reuse"'); }); it("should classify document.createElement as node-identity-reuse", () => { const code = ` const CreateElCard = defineComponent( "create-el-card", () => { const el = document.createElement("span"); return
{el}
; }, ); `; const result = transform(code, { mode: "csr" }); expect(result.code).toContain("__hydrationMetadata__"); expect(result.code).toContain('unsupportedReason: "node-identity-reuse"'); }); it("should not classify shadowed local Text binding as node-identity-reuse", () => { const code = ` const LocalTextCard = defineComponent( "local-text-card", () => { const Text = String; return
{new Text("hello")}
; }, ); `; const result = transform(code, { mode: "csr" }); expect(result.code).toContain("__hydrationMetadata__"); expect(result.code).toContain("planFactory"); expect(result.code).not.toContain( 'unsupportedReason: "node-identity-reuse"', ); }); it("should still classify aliases to global Text as node-identity-reuse", () => { const code = ` const AliasTextCard = defineComponent( "alias-text-card", () => { const Text = globalThis.Text; return
{new Text("hello")}
; }, ); `; const result = transform(code, { mode: "csr" }); expect(result.code).toContain("__hydrationMetadata__"); expect(result.code).toContain('unsupportedReason: "node-identity-reuse"'); }); it("should ignore nested shadowed Text binding inside IIFE", () => { const code = ` const NestedShadowTextCard = defineComponent( "nested-shadow-text-card", () => { const value = (() => { const Text = String; return new Text("hello"); })(); return
{value}
; }, ); `; const result = transform(code, { mode: "csr" }); expect(result.code).toContain("planFactory"); expect(result.code).not.toContain( 'unsupportedReason: "node-identity-reuse"', ); }); it("should support setup with function expression (not arrow) returning JSX", () => { const code = ` const FnExprCard = defineComponent( "fn-expr-card", function ({ props }) { return
{props.label.value}
; }, ); `; const result = transform(code, { mode: "csr" }); expect(result.code).toContain("__hydrationMetadata__"); expect(result.code).toContain("planFactory"); expect(result.code).not.toContain("unsupportedReason"); }); it("should handle setup with multiple nested component children (some with client:, some without)", () => { const code = ` const MixedChildrenCard = defineComponent( "mixed-children-card", ({ props }) => (
), ); `; const result = transform(code, { mode: "csr" }); expect(result.code).toContain("__hydrationMetadata__"); expect(result.code).toContain("planFactory"); expect(result.code).toContain("nestedBoundaries"); // Only IslandChild has client:load expect(result.code).toContain('"IslandChild"'); expect(result.code).toContain('"load"'); }); // --- Batch 3: SSR mode variants, more edge cases, multiple defineComponent --- it("should attach hydration metadata in SSR mode for supported setup", () => { const code = ` const SSRCard = defineComponent( "ssr-card", ({ props }) =>
{props.label.value}
, ); `; const result = transform(code, { mode: "ssr" }); expect(result.code).toContain("__hydrationMetadata__"); expect(result.code).toContain("planFactory"); expect(result.code).not.toContain("unsupportedReason"); }); it("should attach unsupported metadata in SSR mode for imperative setup", () => { const code = ` const SSRImperativeCard = defineComponent( "ssr-imperative-card", ({ host }) => { host.setAttribute("data-ready", "yes"); return
ready
; }, ); `; const result = transform(code, { mode: "ssr" }); expect(result.code).toContain("__hydrationMetadata__"); expect(result.code).toContain( 'unsupportedReason: "imperative-dom-query"', ); }); it("should attach unsupported metadata in SSR mode for async setup", () => { const code = ` const SSRAsyncCard = defineComponent( "ssr-async-card", async () =>
hi
, ); `; const result = transform(code, { mode: "ssr" }); expect(result.code).toContain("__hydrationMetadata__"); expect(result.code).toContain( 'unsupportedReason: "unsupported-component-body"', ); }); it("should handle multiple defineComponent calls in one file", () => { const code = ` const CardA = defineComponent( "card-a", ({ props }) =>
{props.a.value}
, ); const CardB = defineComponent( "card-b", ({ props }) => {props.b.value}, ); `; const result = transform(code, { mode: "csr" }); expect(result.code).toContain("card-a"); expect(result.code).toContain("card-b"); // Both should have metadata const metadataCount = (result.code.match(/__hydrationMetadata__/g) ?? []) .length; expect(metadataCount).toBe(2); const planFactoryCount = (result.code.match(/planFactory/g) ?? []).length; expect(planFactoryCount).toBeGreaterThanOrEqual(2); }); it("should handle multiple defineComponent where one is supported and another unsupported", () => { const code = ` const OkCard = defineComponent( "ok-card", ({ props }) =>
{props.label.value}
, ); const BadCard = defineComponent( "bad-card", () => { try { return
ok
; } catch (e) { return
error
; } }, ); `; const result = transform(code, { mode: "csr" }); expect(result.code).toContain("planFactory"); expect(result.code).toContain("unsupportedReason"); }); it("should support setup with no params (zero-arg component)", () => { const code = ` const NoParamCard = defineComponent( "no-param-card", () =>
static content
, ); `; const result = transform(code, { mode: "csr" }); expect(result.code).toContain("__hydrationMetadata__"); expect(result.code).toContain("planFactory"); expect(result.code).not.toContain("unsupportedReason"); }); it("should support setup returning JSX fragment directly", () => { const code = ` const FragCard = defineComponent( "frag-card", ({ props }) => <>{props.a.value}{props.b.value}, ); `; const result = transform(code, { mode: "csr" }); expect(result.code).toContain("__hydrationMetadata__"); expect(result.code).toContain("planFactory"); }); it("should support setup returning JSX from block body with fragment", () => { const code = ` const BlockFragCard = defineComponent( "block-frag-card", () => { const label = "hello"; return <>
{label}
; }, ); `; const result = transform(code, { mode: "csr" }); expect(result.code).toContain("__hydrationMetadata__"); expect(result.code).toContain("planFactory"); }); it("should handle setup with complex object destructuring in params", () => { const code = ` const DestructCard = defineComponent( "destruct-card", ({ props: { label, count } }) =>
{label.value} {count.value}
, ); `; const result = transform(code, { mode: "csr" }); expect(result.code).toContain("__hydrationMetadata__"); // Nested destructuring should still work expect(result.code).toContain("planFactory"); }); it("should handle setup with array destructuring in params", () => { const code = ` const ArrayDestructCard = defineComponent( "array-destruct-card", ([first, second]) =>
{first}{second}
, ); `; const result = transform(code, { mode: "csr" }); expect(result.code).toContain("__hydrationMetadata__"); expect(result.code).toContain("planFactory"); }); it("should handle setup with default param value", () => { const code = ` const DefaultParamCard = defineComponent( "default-param-card", (ctx = {}) =>
hello
, ); `; const result = transform(code, { mode: "csr" }); expect(result.code).toContain("__hydrationMetadata__"); expect(result.code).toContain("planFactory"); }); it("should support setup with svg root element", () => { const code = ` const SvgCard = defineComponent( "svg-card", ({ props }) => , ); `; const result = transform(code, { mode: "csr" }); expect(result.code).toContain("__hydrationMetadata__"); expect(result.code).toContain("planFactory"); expect(result.code).toContain('"svg"'); }); it("should support setup with math root element", () => { const code = ` const MathCard = defineComponent( "math-card", () => x, ); `; const result = transform(code, { mode: "csr" }); expect(result.code).toContain("__hydrationMetadata__"); expect(result.code).toContain("planFactory"); expect(result.code).toContain('"math"'); }); it("should handle typeof document environment probe as NOT imperative-dom-query", () => { const code = ` const EnvProbeCard = defineComponent( "env-probe-card", () => { const isBrowser = typeof document !== "undefined"; return
hello
; }, ); `; const result = transform(code, { mode: "csr" }); expect(result.code).toContain("__hydrationMetadata__"); // typeof document is an environment probe, not an imperative access expect(result.code).not.toContain( 'unsupportedReason: "imperative-dom-query"', ); expect(result.code).toContain("planFactory"); }); it("should handle setup with static JSX (no dynamic parts)", () => { const code = ` const StaticCard = defineComponent( "static-card", () =>
helloworld
, ); `; const result = transform(code, { mode: "csr" }); expect(result.code).toContain("__hydrationMetadata__"); expect(result.code).toContain("planFactory"); // No dynamic bindings expect(result.code).toContain("bindings: []"); }); it("should handle imported transparent thunk wrapper (withStore) in SSR mode when wrapped in arrow", () => { const code = ` import { withStore } from "@dathra/core"; const StoreCard = defineComponent( "store-card", ({ props }) => withStore(myStore, () =>
{props.label.value}
), ); `; const result = transform(code, { mode: "ssr" }); // withStore is a transparent thunk wrapper — the inner JSX is resolved expect(result.code).toContain("__hydrationMetadata__"); expect(result.code).toContain("planFactory"); }); it("should produce no metadata for bare withStore() call as direct component arg", () => { const code = ` import { withStore } from "@dathra/core"; const StoreCard = defineComponent( "store-card", withStore(myStore, () =>
store content
), ); `; const result = transform(code, { mode: "ssr" }); // Bare CallExpression as component arg (not wrapped in a function) — // the transformer cannot extract a render frame, so no metadata is attached expect(result.code).not.toContain("__hydrationMetadata__"); expect(result.code).not.toContain("planFactory"); }); }); describe("Fragment dynamic content", () => { it("should generate templateEffect and setText for dynamic text inside Fragment", () => { const code = ` const count = signal(0); const element = ( <> {count.value} ); `; const result = transform(code); // Fragment with dynamic text must produce templateEffect + setText expect(result.code).toContain("templateEffect"); expect(result.code).toContain("setText"); expect(result.code).toContain("count.value"); }); it("should generate insert for component inside Fragment", () => { const code = ` const element = ( <> ); `; const result = transform(code); // Fragment with component child must produce insert expect(result.code).toContain("insert"); expect(result.code).toContain("Counter"); expect(result.code).toContain("initialCount"); }); }); describe("Attribute name edge cases", () => { it("should use string literal key for hyphenated attribute names", () => { const code = ` const element =
Content
; `; const result = transform(code); // Hyphenated attribute names must be quoted in the output object expect(result.code).toContain("data-foo="); expect(result.code).toContain("aria-label="); expect(result.code).toContain("bar"); expect(result.code).toContain("test"); }); it("should not wrap static expression attribute (no reactive access) in templateEffect", () => { const code = ` function getClass() { return "foo"; } const element =
Content
; `; const result = transform(code); // Non-reactive expression attribute should avoid templateEffect and use one-time setAttr expect(result.code).not.toContain("templateEffect"); expect(result.code).toContain("setAttr"); expect(result.code).toContain("getClass"); }); it("should preserve local identifiers used by static expression attributes", () => { const code = ` function App() { const modeLabel = typeof document === "undefined" ? "SSR" : "CSR"; return

Current render mode

; } `; const result = transform(code); expect(result.code).toContain("setAttr"); expect(result.code).toContain("modeLabel"); expect(result.code).not.toContain('{ ["data-render-mode"]: modeLabel }'); expect(result.code).not.toContain("templateEffect"); }); it("should support namespaced attributes like xlink:href", () => { const code = ` const element = ; `; const result = transform(code); expect(result.code).toContain("xlink:href="); expect(result.code).toContain("#icon"); }); }); describe("Expression container edge cases", () => { it("should transform logical expression rendering using insert() and templateEffect", () => { const code = ` const visible = signal(true); const element =
{visible.value && Shown}
; `; const result = transform(code); expect(result.code).toContain("insert"); expect(result.code).toContain("templateEffect"); expect(result.code).toContain("visible.value"); expect(result.code).toContain("span"); }); it("should transform expressions containing nested JSX via insert()", () => { const code = ` const element =
{[A, B]}
; `; const result = transform(code); expect(result.code).toContain("insert"); expect(result.code).toContain("em"); expect(result.code).toContain("strong"); }); it("should transform JSXSpreadChild as insert dynamic part", () => { const code = ` const items = [
  • A
  • ,
  • B
  • ]; const element =
      {...items}
    ; `; const result = transform(code); expect(result.code).toContain("insert"); expect(result.code).toContain("items"); }); it("should transform logical expression JSX branches with renderToString in SSR mode", () => { const code = ` const visible = true; const element =
    {visible && SSR}
    ; `; const result = transform(code, { mode: "ssr" }); expect(result.code).not.toContain("fromTree"); expect(result.code).toContain("renderDynamicInsert"); }); }); describe("SSR mode conditional rendering", () => { it("should transform conditional JSX branches with renderToString in SSR mode (not fromTree)", () => { const code = ` const flag = true; const element =
    {flag ? Yes : No}
    ; `; const result = transform(code, { mode: "ssr" }); // SSR mode: no DOM-dependent fromTree must appear expect(result.code).not.toContain("fromTree"); expect(result.code).toContain("renderDynamicInsert"); }); it("should not use setText/templateEffect for SSR conditional branches", () => { const code = ` const flag = true; const element =
    {flag ? A : B}
    ; `; const result = transform(code, { mode: "ssr" }); // SSR mode: no DOM mutation helpers expect(result.code).not.toContain("fromTree"); expect(result.code).not.toContain("firstChild"); expect(result.code).not.toContain("nextSibling"); expect(result.code).not.toContain("setText"); }); it("should transform .map() JSX branches with renderToString in SSR mode", () => { const code = ` const items = ["a", "b"]; const element =
      {items.map(i =>
    • {i}
    • )}
    ; `; const result = transform(code, { mode: "ssr" }); // SSR mode: no DOM-dependent fromTree must appear expect(result.code).not.toContain("fromTree"); expect(result.code).toContain("renderDynamicEach"); }); }); // ========================================================================== // Batch 4: Comprehensive semantic JS/TSX pattern coverage // ========================================================================== describe("JS control flow constructs (unsupported prelude / body)", () => { it("should classify for-loop in setup body as unsupported-component-body", () => { const code = ` const LoopCard = defineComponent( "loop-card", () => { for (let i = 0; i < 10; i++) {} return
    loop
    ; }, ); `; const result = transform(code, { mode: "csr" }); expect(result.code).toContain( 'unsupportedReason: "unsupported-component-body"', ); }); it("should classify while-loop in setup body as unsupported-component-body", () => { const code = ` const WhileCard = defineComponent( "while-card", () => { while (false) {} return
    while
    ; }, ); `; const result = transform(code, { mode: "csr" }); expect(result.code).toContain( 'unsupportedReason: "unsupported-component-body"', ); }); it("should classify do-while in setup body as unsupported-component-body", () => { const code = ` const DoCard = defineComponent( "do-card", () => { do {} while (false); return
    do
    ; }, ); `; const result = transform(code, { mode: "csr" }); expect(result.code).toContain( 'unsupportedReason: "unsupported-component-body"', ); }); it("should classify for-in in setup body as unsupported-component-body", () => { const code = ` const ForInCard = defineComponent( "for-in-card", () => { const obj = { a: 1 }; for (const k in obj) {} return
    for-in
    ; }, ); `; const result = transform(code, { mode: "csr" }); expect(result.code).toContain( 'unsupportedReason: "unsupported-component-body"', ); }); it("should classify for-of in setup body as unsupported-component-body", () => { const code = ` const ForOfCard = defineComponent( "for-of-card", () => { const arr = [1, 2, 3]; for (const v of arr) {} return
    for-of
    ; }, ); `; const result = transform(code, { mode: "csr" }); expect(result.code).toContain( 'unsupportedReason: "unsupported-component-body"', ); }); it("should classify try-catch in setup body as unsupported-component-body", () => { const code = ` const TryCard = defineComponent( "try-card", () => { try { JSON.parse("bad"); } catch (e) {} return
    try
    ; }, ); `; const result = transform(code, { mode: "csr" }); expect(result.code).toContain( 'unsupportedReason: "unsupported-component-body"', ); }); it("should classify try-finally in setup body as unsupported-component-body", () => { const code = ` const TryFinallyCard = defineComponent( "try-finally-card", () => { try { doSomething(); } finally { cleanup(); } return
    finally
    ; }, ); `; const result = transform(code, { mode: "csr" }); expect(result.code).toContain( 'unsupportedReason: "unsupported-component-body"', ); }); it("should classify try-catch-finally in setup body as unsupported-component-body", () => { const code = ` const TryCatchFinallyCard = defineComponent( "try-catch-finally-card", () => { try { init(); } catch (e) { fallback(); } finally { done(); } return
    tcf
    ; }, ); `; const result = transform(code, { mode: "csr" }); expect(result.code).toContain( 'unsupportedReason: "unsupported-component-body"', ); }); it("should classify throw statement in setup body as unsupported-component-body", () => { const code = ` const ThrowCard = defineComponent( "throw-card", () => { throw new Error("no"); }, ); `; const result = transform(code, { mode: "csr" }); // throw is not a supported prelude statement and has no return expect(result.code).toContain( 'unsupportedReason: "unsupported-component-body"', ); }); it("should classify labeled statement in setup body as unsupported-component-body", () => { const code = ` const LabelCard = defineComponent( "label-card", () => { outer: for (let i = 0; i < 5; i++) { break outer; } return
    label
    ; }, ); `; const result = transform(code, { mode: "csr" }); expect(result.code).toContain( 'unsupportedReason: "unsupported-component-body"', ); }); it("should classify async arrow setup as unsupported-component-body", () => { const code = ` const AsyncCard = defineComponent( "async-card", async () =>
    async
    , ); `; const result = transform(code, { mode: "csr" }); expect(result.code).toContain( 'unsupportedReason: "unsupported-component-body"', ); }); it("should classify async arrow block body setup as unsupported-component-body", () => { const code = ` const AsyncBlockCard = defineComponent( "async-block-card", async () => { const data = await fetchData(); return
    {data}
    ; }, ); `; const result = transform(code, { mode: "csr" }); expect(result.code).toContain( 'unsupportedReason: "unsupported-component-body"', ); }); it("should classify async function expression setup as unsupported-component-body", () => { const code = ` const AsyncFnCard = defineComponent( "async-fn-card", async function() { return
    async-fn
    ; }, ); `; const result = transform(code, { mode: "csr" }); expect(result.code).toContain( 'unsupportedReason: "unsupported-component-body"', ); }); it("should classify generator function setup as unsupported-component-body", () => { const code = ` const GenCard = defineComponent( "gen-card", function* () { yield
    gen
    ; }, ); `; const result = transform(code, { mode: "csr" }); expect(result.code).toContain( 'unsupportedReason: "unsupported-component-body"', ); }); it("should classify IIFE containing complex branch in setup body as unsupported", () => { const code = ` const IIFECard = defineComponent( "iife-card", () => { (() => { if (true) console.log("side-effect"); })(); return
    iife
    ; }, ); `; const result = transform(code, { mode: "csr" }); expect(result.code).toContain( 'unsupportedReason: "unsupported-component-body"', ); }); it("should classify setup with with-statement as unsupported-component-body", () => { // Note: 'with' is not valid in strict mode / modules, but parse may still create the node // The important thing is it's not a VariableDeclaration or FunctionDeclaration const code = ` const WithCard = defineComponent( "with-card", () => { console.log("side-effect"); return
    with
    ; }, ); `; const result = transform(code, { mode: "csr" }); // ExpressionStatement in prelude is unsupported expect(result.code).toContain( 'unsupportedReason: "unsupported-component-body"', ); }); it("should classify setup with debugger statement as unsupported-component-body", () => { const code = ` const DebugCard = defineComponent( "debug-card", () => { const x = 1; debugger; return
    {x}
    ; }, ); `; const result = transform(code, { mode: "csr" }); expect(result.code).toContain( 'unsupportedReason: "unsupported-component-body"', ); }); it("should classify setup returning conditional expression as dispatch plan", () => { const code = ` const CondCard = defineComponent( "cond-card", ({ props }) => props.flag.value ?
    a
    : b, ); `; const result = transform(code, { mode: "csr" }); expect(result.code).toContain("dispatch"); expect(result.code).toContain("planFactory"); }); it("should classify setup returning logical expression as dispatch plan", () => { const code = ` const LogicalCard = defineComponent( "logical-card", ({ props }) => props.show.value &&
    shown
    , ); `; const result = transform(code, { mode: "csr" }); expect(result.code).toContain("dispatch"); expect(result.code).toContain("planFactory"); }); it("should classify if-statement in block body as dispatch plan (supported)", () => { const code = ` const IfCard = defineComponent( "if-card", () => { if (Math.random() > 0.5) return
    a
    ; return
    b
    ; }, ); `; const result = transform(code, { mode: "csr" }); expect(result.code).toContain("dispatch"); expect(result.code).toContain("planFactory"); }); it("should classify switch-statement in block body as dispatch plan", () => { const code = ` const SwitchCard = defineComponent( "switch-card", () => { switch (getMode()) { case "a": return
    a
    ; default: return
    b
    ; } }, ); `; const result = transform(code, { mode: "csr" }); expect(result.code).toContain("dispatch"); expect(result.code).toContain("planFactory"); }); it("should classify return of conditional in block body as dispatch plan", () => { const code = ` const RetCondCard = defineComponent( "ret-cond-card", () => { const x = 1; return x > 0 ?
    positive
    :
    non-positive
    ; }, ); `; const result = transform(code, { mode: "csr" }); expect(result.code).toContain("dispatch"); expect(result.code).toContain("planFactory"); }); it("should classify return of logical expression in block body as dispatch plan", () => { const code = ` const RetLogicalCard = defineComponent( "ret-logical-card", () => { const flag = true; return flag &&
    yes
    ; }, ); `; const result = transform(code, { mode: "csr" }); expect(result.code).toContain("dispatch"); expect(result.code).toContain("planFactory"); expect(result.code).not.toContain("unsupportedReason"); }); it("should classify early return (not last statement) as unsupported-component-body", () => { const code = ` const EarlyReturnCard = defineComponent( "early-return-card", () => { return
    early
    ; const dead = "unreachable"; }, ); `; const result = transform(code, { mode: "csr" }); // return is not the last statement, so extractFunctionRenderFrame returns null expect(result.code).toContain( 'unsupportedReason: "unsupported-component-body"', ); }); }); describe("Prelude / body statement breadth", () => { it("should classify ExpressionStatement in prelude as unsupported-component-body", () => { const code = ` const ExprCard = defineComponent( "expr-card", () => { console.log("init"); return
    expr
    ; }, ); `; const result = transform(code, { mode: "csr" }); expect(result.code).toContain( 'unsupportedReason: "unsupported-component-body"', ); }); it("should classify class declaration in prelude as unsupported-component-body", () => { const code = ` const ClassDeclCard = defineComponent( "class-decl-card", () => { class Helper { run() { return 1; } } return
    class
    ; }, ); `; const result = transform(code, { mode: "csr" }); expect(result.code).toContain( 'unsupportedReason: "unsupported-component-body"', ); }); it("should produce no metadata for class expression as component arg", () => { const code = ` const ClassExprCard = defineComponent( "class-expr-card", class { render() { return "
    class
    "; } }, ); `; const result = transform(code, { mode: "csr" }); // ClassExpression is not function-like, no metadata expect(result.code).not.toContain("__hydrationMetadata__"); }); it("should classify prelude with assignment expression statement as unsupported", () => { const code = ` const AssignCard = defineComponent( "assign-card", () => { let x; x = 42; return
    {x}
    ; }, ); `; const result = transform(code, { mode: "csr" }); // 'let x' is VariableDeclaration (supported prelude), but 'x = 42' is ExpressionStatement (not supported) expect(result.code).toContain( 'unsupportedReason: "unsupported-component-body"', ); }); it("should support multiple const declarations in prelude", () => { const code = ` const MultiConstCard = defineComponent( "multi-const-card", () => { const a = 1; const b = 2; const c = a + b; return
    {c}
    ; }, ); `; const result = transform(code, { mode: "csr" }); expect(result.code).toContain("__hydrationMetadata__"); expect(result.code).toContain("planFactory"); expect(result.code).not.toContain("unsupportedReason"); }); it("should support function declaration in prelude", () => { const code = ` const FnDeclCard = defineComponent( "fn-decl-card", () => { function format(x) { return x.toString(); } return
    {format(42)}
    ; }, ); `; const result = transform(code, { mode: "csr" }); expect(result.code).toContain("__hydrationMetadata__"); expect(result.code).toContain("planFactory"); }); it("should support mixed const and function declarations in prelude", () => { const code = ` const MixedCard = defineComponent( "mixed-card", () => { const multiplier = 2; function double(x) { return x * multiplier; } const result = double(21); return
    {result}
    ; }, ); `; const result = transform(code, { mode: "csr" }); expect(result.code).toContain("__hydrationMetadata__"); expect(result.code).toContain("planFactory"); }); it("should classify prelude containing JSX in const init as unsupported", () => { const code = ` const JsxConstCard = defineComponent( "jsx-const-card", () => { const header =

    Header

    ; return
    {header}
    ; }, ); `; const result = transform(code, { mode: "csr" }); // containsJSXNode(statement) detects JSX in prelude const expect(result.code).toContain( 'unsupportedReason: "unsupported-component-body"', ); }); it("should classify prelude containing JSX inside function declaration as unsupported", () => { const code = ` const JsxFnCard = defineComponent( "jsx-fn-card", () => { function renderIcon() { return icon; } return
    {renderIcon()}
    ; }, ); `; const result = transform(code, { mode: "csr" }); // JSX inside prelude function declaration expect(result.code).toContain( 'unsupportedReason: "unsupported-component-body"', ); }); it("should classify empty block body (no return) as unsupported-component-body", () => { const code = ` const EmptyCard = defineComponent( "empty-card", () => {}, ); `; const result = transform(code, { mode: "csr" }); expect(result.code).toContain( 'unsupportedReason: "unsupported-component-body"', ); }); it("should classify body with only const but no return as unsupported-component-body", () => { const code = ` const NoReturnCard = defineComponent( "no-return-card", () => { const x = 1; }, ); `; const result = transform(code, { mode: "csr" }); expect(result.code).toContain( 'unsupportedReason: "unsupported-component-body"', ); }); }); describe("Params and destructuring completeness", () => { it("should support rest parameter in setup", () => { const code = ` const RestCard = defineComponent( "rest-card", ({ props, ...rest }) =>
    {props.label.value}
    , ); `; const result = transform(code, { mode: "csr" }); expect(result.code).toContain("__hydrationMetadata__"); expect(result.code).toContain("planFactory"); }); it("should support deeply nested destructuring in params", () => { const code = ` const DeepCard = defineComponent( "deep-card", ({ props: { label: { value: labelText } } }) =>
    {labelText}
    , ); `; const result = transform(code, { mode: "csr" }); expect(result.code).toContain("__hydrationMetadata__"); expect(result.code).toContain("planFactory"); }); it("should support default value in destructured param", () => { const code = ` const DefaultCard = defineComponent( "default-card", ({ props: { color = "red" } = {} }) =>
    colored
    , ); `; const result = transform(code, { mode: "csr" }); expect(result.code).toContain("__hydrationMetadata__"); expect(result.code).toContain("planFactory"); }); it("should support array destructuring in param", () => { const code = ` const ArrayParamCard = defineComponent( "array-param-card", ([first, second]) =>
    {first}{second}
    , ); `; const result = transform(code, { mode: "csr" }); expect(result.code).toContain("__hydrationMetadata__"); expect(result.code).toContain("planFactory"); }); it("should support array destructuring with rest in param", () => { const code = ` const ArrayRestCard = defineComponent( "array-rest-card", ([first, ...others]) =>
    {first}
    , ); `; const result = transform(code, { mode: "csr" }); expect(result.code).toContain("__hydrationMetadata__"); expect(result.code).toContain("planFactory"); }); it("should support array destructuring with skipped elements", () => { const code = ` const SkipCard = defineComponent( "skip-card", ([, second]) =>
    {second}
    , ); `; const result = transform(code, { mode: "csr" }); expect(result.code).toContain("__hydrationMetadata__"); expect(result.code).toContain("planFactory"); }); it("should support no params (zero-arg component)", () => { const code = ` const ZeroArgCard = defineComponent( "zero-arg-card", () =>
    static
    , ); `; const result = transform(code, { mode: "csr" }); expect(result.code).toContain("__hydrationMetadata__"); expect(result.code).toContain("planFactory"); expect(result.code).not.toContain("unsupportedReason"); }); it("should support assignment pattern (default param value) at top level", () => { const code = ` const DefaultTopCard = defineComponent( "default-top-card", (ctx = {}) =>
    default
    , ); `; const result = transform(code, { mode: "csr" }); expect(result.code).toContain("__hydrationMetadata__"); expect(result.code).toContain("planFactory"); }); it("should handle alpha-renaming for __dh_host collision", () => { const code = ` const CollisionCard = defineComponent( "collision-card", ({ props }) => { const __dh_host = "collision"; return
    {props.x.value}{__dh_host}
    ; }, ); `; const result = transform(code, { mode: "csr" }); expect(result.code).toContain("__hydrationMetadata__"); expect(result.code).toContain("planFactory"); // alpha-renaming should produce a suffixed name (e.g. __dh_host_1) expect(result.code).toContain("__dh_host_1"); }); it("should handle alpha-renaming for __dh_ctx collision", () => { const code = ` const CtxCollisionCard = defineComponent( "ctx-collision-card", ({ props }) => { const __dh_ctx = "collision"; return
    {props.x.value}{__dh_ctx}
    ; }, ); `; const result = transform(code, { mode: "csr" }); expect(result.code).toContain("__hydrationMetadata__"); expect(result.code).toContain("planFactory"); expect(result.code).toContain("__dh_ctx_1"); }); }); describe("Spread semantics generalization", () => { it("should classify spread with nested SpreadElement as non-normalizable-spread", () => { const code = ` const SpreadMergeCard = defineComponent( "spread-merge-card", ({ props }) =>
    merged
    , ); `; const result = transform(code, { mode: "csr" }); expect(result.code).toContain( 'unsupportedReason: "non-normalizable-spread"', ); }); it("should support simple spread (no nested SpreadElement) as planFactory", () => { const code = ` const SimpleSpreadCard = defineComponent( "simple-spread-card", ({ props }) =>
    spread
    , ); `; const result = transform(code, { mode: "csr" }); expect(result.code).toContain("__hydrationMetadata__"); expect(result.code).toContain("planFactory"); expect(result.code).not.toContain("unsupportedReason"); }); it("should support JSX spread with object literal (no SpreadElement inside) as planFactory", () => { const code = ` const ObjSpreadCard = defineComponent( "obj-spread-card", ({ props }) =>
    ok
    , ); `; const result = transform(code, { mode: "csr" }); expect(result.code).toContain("__hydrationMetadata__"); expect(result.code).toContain("planFactory"); expect(result.code).not.toContain("unsupportedReason"); }); it("should classify deeply nested non-normalizable spread in child", () => { const code = ` const DeepSpreadCard = defineComponent( "deep-spread-card", ({ props }) => (
    deep
    ), ); `; const result = transform(code, { mode: "csr" }); expect(result.code).toContain( 'unsupportedReason: "non-normalizable-spread"', ); }); it("should support spread on fragment children", () => { const code = ` const FragSpreadCard = defineComponent( "frag-spread-card", ({ props }) => ( <>
    child
    ), ); `; const result = transform(code, { mode: "csr" }); expect(result.code).toContain("__hydrationMetadata__"); expect(result.code).toContain("planFactory"); expect(result.code).not.toContain("unsupportedReason"); }); }); describe("Helper resolution generalization", () => { it("should resolve top-level helper returning JSX", () => { const code = ` function renderBody(label) { return
    {label}
    ; } const HelperCard = defineComponent( "helper-card", ({ props }) => renderBody(props.label.value), ); `; const result = transform(code, { mode: "csr" }); expect(result.code).toContain("__hydrationMetadata__"); expect(result.code).toContain("planFactory"); }); it("should resolve const arrow helper returning JSX", () => { const code = ` const renderBody = (label) =>
    {label}
    ; const HelperArrowCard = defineComponent( "helper-arrow-card", ({ props }) => renderBody(props.label.value), ); `; const result = transform(code, { mode: "csr" }); expect(result.code).toContain("__hydrationMetadata__"); expect(result.code).toContain("planFactory"); }); it("should resolve multi-level helper chain", () => { const code = ` const renderInner = (text) => {text}; const renderOuter = (label) => renderInner(label); const ChainCard = defineComponent( "chain-card", ({ props }) => renderOuter(props.label.value), ); `; const result = transform(code, { mode: "csr" }); expect(result.code).toContain("__hydrationMetadata__"); expect(result.code).toContain("planFactory"); }); it("should classify method-style helper call as opaque-helper-call", () => { const code = ` const helpers = { render: (x) => "
    " + x + "
    " }; const MethodCard = defineComponent( "method-card", ({ props }) => helpers.render(props.label.value), ); `; const result = transform(code, { mode: "csr" }); expect(result.code).toContain('unsupportedReason: "opaque-helper-call"'); }); it("should classify closure-captured helper as opaque-helper-call", () => { const code = ` const makeRenderer = () => (text) => "
    " + text + "
    "; const render = makeRenderer(); const ClosureCard = defineComponent( "closure-card", ({ props }) => render(props.label.value), ); `; const result = transform(code, { mode: "csr" }); // 'render' init is a CallExpression, not function-like, so not in helperLookup expect(result.code).toContain('unsupportedReason: "opaque-helper-call"'); }); it("should classify runtime-selected helper (conditional callee) as opaque", () => { const code = ` const renderA = (x) =>
    {x}
    ; const renderB = (x) => {x}; const RuntimeCard = defineComponent( "runtime-card", ({ props }) => (props.flag.value ? renderA : renderB)(props.label.value), ); `; const result = transform(code, { mode: "csr" }); // ConditionalExpression callee — can't resolve expect(result.code).toContain('unsupportedReason: "opaque-helper-call"'); }); it("should classify arbitrary imported function call as opaque-helper-call", () => { const code = ` import { renderFancy } from "./utils"; const ImportedCard = defineComponent( "imported-card", ({ props }) => renderFancy(props.label.value), ); `; const result = transform(code, { mode: "csr" }); // renderFancy is not in helperLookup (imported, not local function def) expect(result.code).toContain('unsupportedReason: "opaque-helper-call"'); }); it("should handle helper with mismatched argument count as opaque-helper-call", () => { const code = ` const renderBody = (a, b) =>
    {a}{b}
    ; const MismatchCard = defineComponent( "mismatch-card", ({ props }) => renderBody(props.label.value), ); `; const result = transform(code, { mode: "csr" }); // 1 arg passed, but helper expects 2 expect(result.code).toContain('unsupportedReason: "opaque-helper-call"'); }); it("should handle circular helper reference gracefully", () => { const code = ` function helperA(x) { return helperB(x); } function helperB(x) { return helperA(x); } const CircularCard = defineComponent( "circular-card", ({ props }) => helperA(props.x.value), ); `; const result = transform(code, { mode: "csr" }); // Circular reference: visitedHelpers prevents infinite loop expect(result.code).toContain('unsupportedReason: "opaque-helper-call"'); }); it("should resolve helper with prelude statements", () => { const code = ` function renderBody(label) { const formatted = label + "!"; return
    {formatted}
    ; } const PreludeHelperCard = defineComponent( "prelude-helper-card", ({ props }) => renderBody(props.label.value), ); `; const result = transform(code, { mode: "csr" }); expect(result.code).toContain("__hydrationMetadata__"); expect(result.code).toContain("planFactory"); }); it("should classify local thunk wrapper as opaque-helper-call (not in known list)", () => { const code = ` function withTheme(theme, fn) { return fn(); } const ThunkCard = defineComponent( "thunk-card", () => withTheme("dark", () =>
    themed
    ), ); `; const result = transform(code, { mode: "csr" }); expect(result.code).toContain("__hydrationMetadata__"); // Local functions are not in KNOWN_IMPORTED_TRANSPARENT_THUNK_WRAPPERS expect(result.code).toContain('unsupportedReason: "opaque-helper-call"'); }); it("should resolve aliased imported transparent thunk wrapper", () => { const code = ` import { withStore as useStore } from "@dathra/store"; const AliasCard = defineComponent( "alias-card", ({ props }) => useStore(myStore, () =>
    {props.x.value}
    ), ); `; const result = transform(code, { mode: "csr" }); expect(result.code).toContain("__hydrationMetadata__"); expect(result.code).toContain("planFactory"); }); it("should classify unknown imported thunk wrapper as opaque", () => { const code = ` import { withStore } from "./custom-store"; const UnknownCard = defineComponent( "unknown-card", ({ props }) => withStore(store, () =>
    {props.label.value}
    ), ); `; const result = transform(code, { mode: "csr" }); // Not from @dathra/core or @dathra/store expect(result.code).toContain('unsupportedReason: "opaque-helper-call"'); }); }); describe("Nested islands deep combinations", () => { it("should track nested boundary for component with client:load", () => { const code = ` const OuterCard = defineComponent( "outer-card", () => (
    ), ); `; const result = transform(code, { mode: "csr" }); expect(result.code).toContain("__hydrationMetadata__"); expect(result.code).toContain("nestedBoundaries"); expect(result.code).toContain('"load"'); }); it("should track multiple nested boundaries at same level", () => { const code = ` const MultiCard = defineComponent( "multi-card", () => ( ), ); `; const result = transform(code, { mode: "csr" }); expect(result.code).toContain("__hydrationMetadata__"); expect(result.code).toContain('"load"'); expect(result.code).toContain('"visible"'); }); it("should track deeply nested island boundary", () => { const code = ` const DeepIslandCard = defineComponent( "deep-island-card", () => (
    ), ); `; const result = transform(code, { mode: "csr" }); expect(result.code).toContain("__hydrationMetadata__"); expect(result.code).toContain('"idle"'); expect(result.code).toContain("nestedBoundaries"); }); it("should handle island with member expression component tag", () => { const code = ` const MemberCard = defineComponent( "member-card", () => (
    ), ); `; const result = transform(code, { mode: "csr" }); expect(result.code).toContain("__hydrationMetadata__"); expect(result.code).toContain('"load"'); // Component display name should be tracked expect(result.code).toContain("UI.Button"); }); it("should handle island with client:interaction and custom event", () => { const code = ` const InteractionCard = defineComponent( "interaction-card", () => (
    ), ); `; const result = transform(code, { mode: "csr" }); expect(result.code).toContain("__hydrationMetadata__"); expect(result.code).toContain('"interaction"'); }); it("should handle island with client:media query", () => { const code = ` const MediaCard = defineComponent( "media-card", () => (
    ), ); `; const result = transform(code, { mode: "csr" }); expect(result.code).toContain("__hydrationMetadata__"); expect(result.code).toContain('"media"'); }); it("should handle component without client:* as non-island (no boundary)", () => { const code = ` const NonIslandCard = defineComponent( "non-island-card", () => (
    ), ); `; const result = transform(code, { mode: "csr" }); expect(result.code).toContain("__hydrationMetadata__"); // No island metadata → nestedBoundaries should be empty expect(result.code).toContain("nestedBoundaries: []"); }); it("should handle mixed island and non-island children", () => { const code = ` const MixedIslandCard = defineComponent( "mixed-island-card", () => (
    ), ); `; const result = transform(code, { mode: "csr" }); expect(result.code).toContain("__hydrationMetadata__"); expect(result.code).toContain('"load"'); // Only one nested boundary (Island, not Plain/AnotherPlain) }); }); describe("Component arg exotic forms", () => { it("should produce no metadata for number literal as component arg", () => { const code = ` const NumCard = defineComponent("num-card", 42); `; const result = transform(code, { mode: "csr" }); expect(result.code).not.toContain("__hydrationMetadata__"); }); it("should produce no metadata for string literal as component arg", () => { const code = ` const StrCard = defineComponent("str-card", "template"); `; const result = transform(code, { mode: "csr" }); expect(result.code).not.toContain("__hydrationMetadata__"); }); it("should produce no metadata for identifier as component arg", () => { const code = ` const MyCard = defineComponent("my-card", setupFn); `; const result = transform(code, { mode: "csr" }); expect(result.code).not.toContain("__hydrationMetadata__"); }); it("should produce no metadata for object literal as component arg", () => { const code = ` const ObjCard = defineComponent("obj-card", { render: () => "
    " }); `; const result = transform(code, { mode: "csr" }); expect(result.code).not.toContain("__hydrationMetadata__"); }); it("should produce no metadata for array literal as component arg", () => { const code = ` const ArrCard = defineComponent("arr-card", [() => "
    "]); `; const result = transform(code, { mode: "csr" }); expect(result.code).not.toContain("__hydrationMetadata__"); }); it("should produce no metadata for template literal as component arg", () => { const code = ` const TplCard = defineComponent("tpl-card", \`template\`); `; const result = transform(code, { mode: "csr" }); expect(result.code).not.toContain("__hydrationMetadata__"); }); it("should produce no metadata for new expression as component arg", () => { const code = ` const NewCard = defineComponent("new-card", new ComponentClass()); `; const result = transform(code, { mode: "csr" }); expect(result.code).not.toContain("__hydrationMetadata__"); }); it("should classify HOC factory call as no metadata (not function-like)", () => { const code = ` const HocCard = defineComponent("hoc-card", withTheme("dark")); `; const result = transform(code, { mode: "csr" }); expect(result.code).not.toContain("__hydrationMetadata__"); }); it("should support function expression (not arrow) as component arg", () => { const code = ` const FnExprCard = defineComponent( "fn-expr-card", function({ props }) { return
    {props.x.value}
    ; }, ); `; const result = transform(code, { mode: "csr" }); expect(result.code).toContain("__hydrationMetadata__"); expect(result.code).toContain("planFactory"); }); it("should support named function expression as component arg", () => { const code = ` const NamedFnCard = defineComponent( "named-fn-card", function setup({ props }) { return
    {props.x.value}
    ; }, ); `; const result = transform(code, { mode: "csr" }); expect(result.code).toContain("__hydrationMetadata__"); expect(result.code).toContain("planFactory"); }); }); describe("TypeScript syntax breadth", () => { it("should handle TS type assertion (as) in setup body return", () => { const code = ` const AsCard = defineComponent( "as-card", () => { const text = "hello" as string; return
    {text}
    ; }, ); `; const result = transform(code, { mode: "csr" }); expect(result.code).toContain("__hydrationMetadata__"); expect(result.code).toContain("planFactory"); }); it("should handle as const in prelude", () => { const code = ` const AsConstCard = defineComponent( "as-const-card", () => { const items = ["a", "b", "c"] as const; return
    {items[0]}
    ; }, ); `; const result = transform(code, { mode: "csr" }); expect(result.code).toContain("__hydrationMetadata__"); expect(result.code).toContain("planFactory"); }); it("should handle satisfies in prelude", () => { const code = ` type Config = { color: string }; const SatisfiesCard = defineComponent( "satisfies-card", () => { const cfg = { color: "red" } satisfies Config; return
    satisfies
    ; }, ); `; const result = transform(code, { mode: "csr" }); expect(result.code).toContain("__hydrationMetadata__"); expect(result.code).toContain("planFactory"); }); it("should handle type-only import (should not interfere with transform)", () => { const code = ` import type { FC } from "some-lib"; const TypeImportCard = defineComponent( "type-import-card", () =>
    type-only
    , ); `; const result = transform(code, { mode: "csr" }); expect(result.code).toContain("__hydrationMetadata__"); expect(result.code).toContain("planFactory"); }); it("should handle generic type params on arrow function setup", () => { const code = ` const GenericCard = defineComponent( "generic-card", ({ props }: { props: { label: { value: T } } }) =>
    {props.label.value}
    , ); `; const result = transform(code, { mode: "csr" }); expect(result.code).toContain("__hydrationMetadata__"); expect(result.code).toContain("planFactory"); }); it("should handle TS enum declaration before defineComponent", () => { const code = ` enum Color { Red, Blue } const EnumCard = defineComponent( "enum-card", () =>
    {Color.Red}
    , ); `; const result = transform(code, { mode: "csr" }); // Enum is a top-level statement, not inside setup — should not affect plan expect(result.code).toContain("__hydrationMetadata__"); expect(result.code).toContain("planFactory"); }); it("should handle TS namespace declaration before defineComponent", () => { const code = ` namespace Utils { export function format(s: string) { return s; } } const NSCard = defineComponent( "ns-card", () =>
    {Utils.format("hello")}
    , ); `; const result = transform(code, { mode: "csr" }); expect(result.code).toContain("__hydrationMetadata__"); }); it("should handle interface declaration before defineComponent", () => { const code = ` interface CardProps { label: string; } const IfaceCard = defineComponent( "iface-card", () =>
    iface
    , ); `; const result = transform(code, { mode: "csr" }); expect(result.code).toContain("__hydrationMetadata__"); expect(result.code).toContain("planFactory"); }); it("should handle type alias declaration before defineComponent", () => { const code = ` type Label = string; const TypeAliasCard = defineComponent( "type-alias-card", () =>
    typed
    , ); `; const result = transform(code, { mode: "csr" }); expect(result.code).toContain("__hydrationMetadata__"); expect(result.code).toContain("planFactory"); }); it("should handle non-null assertion in prelude", () => { const code = ` const NonNullCard = defineComponent( "non-null-card", () => { const el = getElement()!; return
    {el}
    ; }, ); `; const result = transform(code, { mode: "csr" }); expect(result.code).toContain("__hydrationMetadata__"); expect(result.code).toContain("planFactory"); }); it("should handle TS const enum before defineComponent", () => { const code = ` const enum Direction { Up, Down, Left, Right } const ConstEnumCard = defineComponent( "const-enum-card", () =>
    {Direction.Up}
    , ); `; const result = transform(code, { mode: "csr" }); expect(result.code).toContain("__hydrationMetadata__"); }); }); describe("Event and directive breadth", () => { it("should handle onMouseEnter event on HTML element", () => { const code = ` const handler = () => {}; const element =
    hover me
    ; `; const result = transform(code); expect(result.code).toContain("event("); expect(result.code).toContain('"mouseenter"'); }); it("should handle onMouseLeave event on HTML element", () => { const code = ` const handler = () => {}; const element =
    leave me
    ; `; const result = transform(code); expect(result.code).toContain("event("); expect(result.code).toContain('"mouseleave"'); }); it("should handle onKeyDown event on HTML element", () => { const code = ` const handler = () => {}; const element =
    key
    ; `; const result = transform(code); expect(result.code).toContain("event("); expect(result.code).toContain('"keydown"'); }); it("should handle onKeyUp event on HTML element", () => { const code = ` const handler = () => {}; const element =
    key
    ; `; const result = transform(code); expect(result.code).toContain("event("); expect(result.code).toContain('"keyup"'); }); it("should handle onFocus event on HTML element", () => { const code = ` const handler = () => {}; const element = ; `; const result = transform(code); expect(result.code).toContain("event("); expect(result.code).toContain('"focus"'); }); it("should handle onBlur event on HTML element", () => { const code = ` const handler = () => {}; const element = ; `; const result = transform(code); expect(result.code).toContain("event("); expect(result.code).toContain('"blur"'); }); it("should handle onInput event on HTML element", () => { const code = ` const handler = () => {}; const element = ; `; const result = transform(code); expect(result.code).toContain("event("); expect(result.code).toContain('"input"'); }); it("should handle onChange event on HTML element", () => { const code = ` const handler = () => {}; const element = ; `; const result = transform(code); expect(result.code).toContain("event("); expect(result.code).toContain('"change"'); }); it("should handle onSubmit event on form element", () => { const code = ` const handler = () => {}; const element =
    ; `; const result = transform(code); expect(result.code).toContain("event("); expect(result.code).toContain('"submit"'); }); it("should handle onScroll event on HTML element", () => { const code = ` const handler = () => {}; const element =
    scroll
    ; `; const result = transform(code); expect(result.code).toContain("event("); expect(result.code).toContain('"scroll"'); }); it("should handle onDblClick event on HTML element", () => { const code = ` const handler = () => {}; const element =
    double click
    ; `; const result = transform(code); expect(result.code).toContain("event("); expect(result.code).toContain('"dblclick"'); }); it("should handle onContextMenu event on HTML element", () => { const code = ` const handler = () => {}; const element =
    right click
    ; `; const result = transform(code); expect(result.code).toContain("event("); expect(result.code).toContain('"contextmenu"'); }); it("should handle onTouchStart event on HTML element", () => { const code = ` const handler = () => {}; const element =
    touch
    ; `; const result = transform(code); expect(result.code).toContain("event("); expect(result.code).toContain('"touchstart"'); }); it("should handle onDragStart event on HTML element", () => { const code = ` const handler = () => {}; const element =
    drag
    ; `; const result = transform(code); expect(result.code).toContain("event("); expect(result.code).toContain('"dragstart"'); }); it("should handle onPointerDown event on HTML element", () => { const code = ` const handler = () => {}; const element =
    pointer
    ; `; const result = transform(code); expect(result.code).toContain("event("); expect(result.code).toContain('"pointerdown"'); }); it("should handle onAnimationEnd event on HTML element", () => { const code = ` const handler = () => {}; const element =
    anim
    ; `; const result = transform(code); expect(result.code).toContain("event("); expect(result.code).toContain('"animationend"'); }); it("should handle onTransitionEnd event on HTML element", () => { const code = ` const handler = () => {}; const element =
    transition
    ; `; const result = transform(code); expect(result.code).toContain("event("); expect(result.code).toContain('"transitionend"'); }); it("should handle onWheel event on HTML element", () => { const code = ` const handler = () => {}; const element =
    wheel
    ; `; const result = transform(code); expect(result.code).toContain("event("); expect(result.code).toContain('"wheel"'); }); it("should handle onCopy event on HTML element", () => { const code = ` const handler = () => {}; const element =
    copy
    ; `; const result = transform(code); expect(result.code).toContain("event("); expect(result.code).toContain('"copy"'); }); it("should handle onPaste event on HTML element", () => { const code = ` const handler = () => {}; const element =
    paste
    ; `; const result = transform(code); expect(result.code).toContain("event("); expect(result.code).toContain('"paste"'); }); it("should handle multiple events on single element", () => { const code = ` const handler = () => {}; const element = ; `; const result = transform(code); expect(result.code).toContain('"click"'); expect(result.code).toContain('"mouseenter"'); expect(result.code).toContain('"focus"'); }); it("should transform colocated load:onMouseEnter", () => { const code = ` const handler = () => {}; const element =
    hover
    ; `; const result = transform(code); expect(result.code).toContain("load"); expect(result.code).toContain('"mouseenter"'); }); it("should keep colocated interaction:onKeyDown unsupported", () => { const code = ` const handler = () => {}; const element =
    key
    ; `; const result = transform(code); expect(result.code).toContain("interaction"); expect(result.code).toContain('"keydown"'); expect(result.code).toContain("data-dh-client-event="); }); it("should transform colocated visible:onFocus", () => { const code = ` const handler = () => {}; const element = ; `; const result = transform(code); expect(result.code).toContain("visible"); expect(result.code).toContain('"focus"'); }); it("should transform colocated idle:onScroll", () => { const code = ` const handler = () => {}; const element =
    scroll
    ; `; const result = transform(code); expect(result.code).toContain("idle"); expect(result.code).toContain('"scroll"'); }); }); describe("SSR mode variants for all categories", () => { it("should produce SSR metadata for unsupported setup in SSR mode (loop)", () => { const code = ` const SSRLoopCard = defineComponent( "ssr-loop-card", () => { for (let i = 0; i < 10; i++) {} return
    loop
    ; }, ); `; const result = transform(code, { mode: "ssr" }); expect(result.code).toContain("__hydrationMetadata__"); expect(result.code).toContain( 'unsupportedReason: "unsupported-component-body"', ); }); it("should produce SSR metadata for runtime-branching (try/catch) in SSR mode", () => { const code = ` const SSRBranchCard = defineComponent( "ssr-branch-card", () => { try { return
    a
    ; } catch (e) { return
    b
    ; } }, ); `; const result = transform(code, { mode: "ssr" }); expect(result.code).toContain("__hydrationMetadata__"); expect(result.code).toContain("unsupportedReason"); }); it("should produce SSR metadata for node-identity-reuse in SSR mode", () => { const code = ` const SSRNodeCard = defineComponent( "ssr-node-card", () => { const t = new Text("hello"); return
    text
    ; }, ); `; const result = transform(code, { mode: "ssr" }); expect(result.code).toContain("__hydrationMetadata__"); expect(result.code).toContain('unsupportedReason: "node-identity-reuse"'); }); it("should produce SSR metadata for opaque-helper-call in SSR mode", () => { const code = ` import { renderFancy } from "./utils"; const SSROpaqueCard = defineComponent( "ssr-opaque-card", ({ props }) => renderFancy(props.label.value), ); `; const result = transform(code, { mode: "ssr" }); expect(result.code).toContain("__hydrationMetadata__"); expect(result.code).toContain('unsupportedReason: "opaque-helper-call"'); }); it("should produce SSR metadata for non-normalizable-spread in SSR mode", () => { const code = ` const SSRSpreadCard = defineComponent( "ssr-spread-card", ({ props }) =>
    spread
    , ); `; const result = transform(code, { mode: "ssr" }); expect(result.code).toContain("__hydrationMetadata__"); expect(result.code).toContain( 'unsupportedReason: "non-normalizable-spread"', ); }); it("should produce SSR planFactory for supported helper in SSR mode", () => { const code = ` function renderBody(label) { return
    {label}
    ; } const SSRHelperCard = defineComponent( "ssr-helper-card", ({ props }) => renderBody(props.label.value), ); `; const result = transform(code, { mode: "ssr" }); expect(result.code).toContain("__hydrationMetadata__"); expect(result.code).toContain("planFactory"); expect(result.code).not.toContain("unsupportedReason"); }); it("should handle multiple defineComponent in SSR mode", () => { const code = ` const A = defineComponent("a-card", () =>
    a
    ); const B = defineComponent("b-card", () => b); `; const result = transform(code, { mode: "ssr" }); // Both should get metadata expect(result.code).toContain("__hydrationMetadata__"); // Count occurrences of planFactory const planFactoryCount = (result.code.match(/planFactory/g) ?? []).length; expect(planFactoryCount).toBeGreaterThanOrEqual(2); }); it("should handle SSR mode with TypeScript type annotations", () => { const code = ` type Props = { label: { value: string } }; const SSRTypedCard = defineComponent( "ssr-typed-card", ({ props }: { props: Props }) =>
    {props.label.value}
    , ); `; const result = transform(code, { mode: "ssr" }); expect(result.code).toContain("__hydrationMetadata__"); expect(result.code).toContain("planFactory"); }); it("should handle SSR mode with nested islands", () => { const code = ` const SSRIslandCard = defineComponent( "ssr-island-card", () => (
    ), ); `; const result = transform(code, { mode: "ssr" }); expect(result.code).toContain("__hydrationMetadata__"); expect(result.code).toContain("planFactory"); expect(result.code).toContain('"load"'); expect(result.code).toContain('"visible"'); }); it("should handle SSR mode with export named declaration", () => { const code = ` export const SSRExportCard = defineComponent( "ssr-export-card", () =>
    exported
    , ); `; const result = transform(code, { mode: "ssr" }); expect(result.code).toContain("__hydrationMetadata__"); expect(result.code).toContain("planFactory"); }); }); describe("SSR mode + colocated directive output", () => { it("should produce SSR planFactory for supported component with load:onClick", () => { const code = ` import { defineComponent, signal } from "@dathra/core"; const Comp = defineComponent("x-comp", () => { const count = signal(0); return (
    {count.value}
    ); }); `; const result = transform(code, { mode: "ssr" }); expect(result.code).toContain("__hydrationMetadata__"); expect(result.code).toContain("planFactory"); expect(result.code).not.toContain("unsupportedReason"); }); it("should produce SSR planFactory for supported component with interaction:onClick", () => { const code = ` import { defineComponent } from "@dathra/core"; const Comp = defineComponent("x-comp", () => { return
    ; }); `; const result = transform(code, { mode: "ssr" }); expect(result.code).toContain("__hydrationMetadata__"); expect(result.code).toContain("planFactory"); expect(result.code).not.toContain("unsupportedReason"); }); it("should produce SSR planFactory for supported component with visible:onClick", () => { const code = ` import { defineComponent } from "@dathra/core"; const Comp = defineComponent("x-comp", () => { return
    ; }); `; const result = transform(code, { mode: "ssr" }); expect(result.code).toContain("__hydrationMetadata__"); expect(result.code).toContain("planFactory"); }); it("should produce SSR planFactory for supported component with idle:onClick", () => { const code = ` import { defineComponent } from "@dathra/core"; const Comp = defineComponent("x-comp", () => { return
    ; }); `; const result = transform(code, { mode: "ssr" }); expect(result.code).toContain("__hydrationMetadata__"); expect(result.code).toContain("planFactory"); }); it("should include colocated metadata attributes in SSR output for load:onClick", () => { const code = ` import { defineComponent } from "@dathra/core"; const Comp = defineComponent("x-comp", () => { return
    ; }); `; const result = transform(code, { mode: "ssr" }); expect(result.code).toContain("data-dh-client-target="); expect(result.code).toContain("data-dh-client-strategy="); expect(result.code).toContain("load"); }); it("should remove colocated syntax and keep click binding in SSR planFactory", () => { const code = ` import { defineComponent, signal } from "@dathra/core"; const Comp = defineComponent("x-comp", () => { const count = signal(0); return (
    ); }); `; const result = transform(code, { mode: "ssr" }); // The raw colocated syntax should be removed expect(result.code).not.toContain("load:onClick"); // The click event should be preserved as a regular binding expect(result.code).toContain("click"); }); it("should throw in SSR mode for unsupported (try/catch) + colocated combination", () => { const code = ` import { defineComponent } from "@dathra/core"; const Comp = defineComponent("x-comp", () => { try { return
    ; } catch (e) { return error; } }); `; expect(() => transform(code, { mode: "ssr" })).toThrow( /Colocated client directives.*cannot be used.*unsupported/, ); }); }); describe("client:interaction event type variants", () => { it("should handle client:interaction with mouseenter event type", () => { const code = ` import { defineComponent } from "@dathra/core"; const Comp = defineComponent("x-comp", () => (
    )); `; const result = transform(code, { mode: "csr" }); expect(result.code).toContain('"interaction"'); expect(result.code).toContain('"mouseenter"'); }); it("should handle client:interaction with focus event type", () => { const code = ` import { defineComponent } from "@dathra/core"; const Comp = defineComponent("x-comp", () => (
    )); `; const result = transform(code, { mode: "csr" }); expect(result.code).toContain('"interaction"'); expect(result.code).toContain('"focus"'); }); it("should handle client:interaction with pointerdown event type", () => { const code = ` import { defineComponent } from "@dathra/core"; const Comp = defineComponent("x-comp", () => (
    )); `; const result = transform(code, { mode: "csr" }); expect(result.code).toContain('"interaction"'); expect(result.code).toContain('"pointerdown"'); }); it("should default bare client:interaction to click", () => { const code = ` import { defineComponent } from "@dathra/core"; const Comp = defineComponent("x-comp", () => (
    )); `; const result = transform(code, { mode: "csr" }); expect(result.code).toContain('"interaction"'); expect(result.code).toContain('"click"'); }); it("should handle client:interaction event type in SSR mode", () => { const code = ` import { defineComponent } from "@dathra/core"; const Comp = defineComponent("x-comp", () => (
    )); `; const result = transform(code, { mode: "ssr" }); expect(result.code).toContain('"interaction"'); expect(result.code).toContain('"mouseover"'); }); it("should reject non-string-literal client:interaction value", () => { const code = ` const element = ; `; expect(() => transform(code, { mode: "csr" })).toThrow( /client:interaction accepts only string literal event types/, ); }); }); describe("Colocated directive + nested islands combination", () => { it("should support colocated directive alongside nested island child", () => { const code = ` import { defineComponent, signal } from "@dathra/core"; const Parent = defineComponent("x-parent", () => { const count = signal(0); return (
    {count.value}
    ); }); `; const result = transform(code, { mode: "csr" }); expect(result.code).toContain("__hydrationMetadata__"); expect(result.code).toContain("planFactory"); expect(result.code).toContain("nestedBoundaries"); }); it("should track nested boundary even with colocated directive present", () => { const code = ` import { defineComponent } from "@dathra/core"; const Parent = defineComponent("x-parent", () => (
    )); `; const result = transform(code, { mode: "csr" }); expect(result.code).toContain("planFactory"); expect(result.code).toContain("nestedBoundaries"); // Both nested islands should be tracked expect(result.code).toContain('"idle"'); expect(result.code).toContain('"visible"'); }); it("should support colocated directive + nested island in SSR mode", () => { const code = ` import { defineComponent, signal } from "@dathra/core"; const Parent = defineComponent("x-parent", () => { const count = signal(0); return (
    {count.value}
    ); }); `; const result = transform(code, { mode: "ssr" }); expect(result.code).toContain("__hydrationMetadata__"); expect(result.code).toContain("planFactory"); expect(result.code).toContain("nestedBoundaries"); }); it("should throw for client:* on non-imported component inside defineComponent", () => { // Inside defineComponent's hydration analysis, the JSX tree walk // cannot resolve `Panel` as a component (not imported), so client:* // on it hits the HTML-element guard before assertNoHostIslandsMixing. // The standalone JSX equivalent (line ~2308) DOES trigger the mixing // guard because the outer tree walk recognises capitalized tags. const code = ` import { defineComponent } from "@dathra/core"; const Comp = defineComponent("x-comp", () => ( )); `; expect(() => transform(code, { mode: "csr" })).toThrow( /client:\* directives are only supported on component elements/, ); }); it("should support multiple colocated directives with same strategy alongside nested island", () => { const code = ` import { defineComponent, signal } from "@dathra/core"; const Parent = defineComponent("x-parent", () => { const a = signal(0); const b = signal(0); return (
    {a.value} / {b.value}
    ); }); `; const result = transform(code, { mode: "csr" }); expect(result.code).toContain("planFactory"); expect(result.code).toContain("nestedBoundaries"); // Two colocated targets should both have metadata expect(result.code).toContain("data-dh-client-target"); }); }); describe("Self-closing and void elements", () => { it("should handle self-closing br element", () => { const code = ` const element =
    line1
    line2
    ; `; const result = transform(code); expect(result.code).toContain("fromTree"); expect(result.code).toContain("
    "); }); it("should handle self-closing hr element", () => { const code = ` const element =

    ; `; const result = transform(code); expect(result.code).toContain("fromTree"); expect(result.code).toContain("
    "); }); it("should handle img with dynamic src", () => { const code = ` const src = signal("photo.jpg"); const element = photo; `; const result = transform(code); expect(result.code).toContain("fromTree"); expect(result.code).toContain(" { const code = ` const val = signal(""); const element = ; `; const result = transform(code); expect(result.code).toContain("fromTree"); expect(result.code).toContain(" { const code = ` const element = ; `; const result = transform(code); expect(result.code).toContain("fromTree"); expect(result.code).toContain(" { const code = ` const element = ; `; const result = transform(code); expect(result.code).toContain("fromTree"); expect(result.code).toContain(" { it("should handle template literal as JSX text child", () => { const code = ` const name = signal("world"); const element =
    {\`hello \${name.value}\`}
    ; `; const result = transform(code); expect(result.code).toContain("fromTree"); expect(result.code).toContain("templateEffect"); }); it("should handle template literal as attribute value", () => { const code = ` const id = signal("box"); const element =
    tpl attr
    ; `; const result = transform(code); expect(result.code).toContain("fromTree"); expect(result.code).toContain("templateEffect"); }); it("should handle tagged template literal in JSX expression", () => { const code = ` const name = signal("world"); const element =
    {String.raw\`hello \${name.value}\`}
    ; `; const result = transform(code); expect(result.code).toContain("fromTree"); }); it("should handle complex computed expression as text child", () => { const code = ` const count = signal(0); const element =
    {count.value * 2 + 1}
    ; `; const result = transform(code); expect(result.code).toContain("fromTree"); expect(result.code).toContain("templateEffect"); expect(result.code).toContain("setText"); }); it("should handle function call as text child", () => { const code = ` const count = signal(0); const element =
    {Math.floor(count.value)}
    ; `; const result = transform(code); expect(result.code).toContain("fromTree"); expect(result.code).toContain("templateEffect"); }); it("should handle nullish coalescing in JSX expression", () => { const code = ` const val = signal(null); const element =
    {val.value ?? "default"}
    ; `; const result = transform(code); expect(result.code).toContain("fromTree"); }); it("should handle optional chaining in JSX expression", () => { const code = ` const obj = signal({ a: { b: "c" } }); const element =
    {obj.value?.a?.b}
    ; `; const result = transform(code); expect(result.code).toContain("fromTree"); expect(result.code).toContain("templateEffect"); }); it("should handle comma expression in JSX attribute", () => { const code = ` const x = signal(1); const element =
    comma
    ; `; const result = transform(code); expect(result.code).toContain("fromTree"); }); }); describe("Boolean and special attribute handling", () => { it("should handle boolean attribute (true)", () => { const code = ` const element = ; `; const result = transform(code); expect(result.code).toContain("fromTree"); expect(result.code).toContain("disabled"); }); it("should handle boolean attribute (explicit true)", () => { const code = ` const element = ; `; const result = transform(code); expect(result.code).toContain("fromTree"); }); it("should handle boolean attribute (explicit false)", () => { const code = ` const element = ; `; const result = transform(code); expect(result.code).toContain("fromTree"); }); it("should handle dynamic boolean attribute", () => { const code = ` const isDisabled = signal(false); const element = ; `; const result = transform(code); expect(result.code).toContain("fromTree"); expect(result.code).toContain("templateEffect"); }); it("should handle data-* attribute", () => { const code = ` const element =
    data attrs
    ; `; const result = transform(code); expect(result.code).toContain("fromTree"); expect(result.code).toContain("data-testid="); }); it("should handle aria-* attribute", () => { const code = ` const element = ; `; const result = transform(code); expect(result.code).toContain("fromTree"); expect(result.code).toContain("aria-label="); }); it("should handle style attribute as string", () => { const code = ` const element =
    styled
    ; `; const result = transform(code); expect(result.code).toContain("fromTree"); expect(result.code).toContain("color: red; font-size: 14px;"); }); it("should handle className attribute", () => { const code = ` const cls = signal("active"); const element =
    classed
    ; `; const result = transform(code); expect(result.code).toContain("fromTree"); expect(result.code).toContain("templateEffect"); }); }); describe("Export patterns", () => { it("should handle export const defineComponent", () => { const code = ` export const ExCard = defineComponent( "ex-card", () =>
    exported
    , ); `; const result = transform(code, { mode: "csr" }); expect(result.code).toContain("__hydrationMetadata__"); expect(result.code).toContain("planFactory"); }); it("should handle multiple exports with defineComponent", () => { const code = ` export const A = defineComponent("a-card", () =>
    a
    ); export const B = defineComponent("b-card", () => b); `; const result = transform(code, { mode: "csr" }); const planCount = (result.code.match(/planFactory/g) ?? []).length; expect(planCount).toBeGreaterThanOrEqual(2); }); it("should handle defineComponent not at top level (no plan)", () => { const code = ` function factory() { const Inner = defineComponent("inner-card", () =>
    inner
    ); return Inner; } `; const result = transform(code, { mode: "csr" }); // defineComponent inside a function is not at top-level VariableDeclaration expect(result.code).not.toContain("__hydrationMetadata__"); }); }); describe("Unsupported hydration + colocated directive combination guard", () => { it("should throw when runtime-branching (try/catch) component has load:onClick", () => { const code = ` import { defineComponent } from "@dathra/core"; const Comp = defineComponent("x-comp", (props) => { try { return
    ; } catch (e) { return error; } }); `; expect(() => transform(code, { mode: "csr" })).toThrow( /Colocated client directives.*cannot be used.*unsupported.*reason: unsupported-component-body/, ); }); it("should throw when imperative-dom-query component has interaction:onClick", () => { const code = ` import { defineComponent } from "@dathra/core"; const Comp = defineComponent("x-comp", (props, { host }) => { const el = host.shadowRoot.querySelector(".foo"); return
    ; }); `; expect(() => transform(code, { mode: "csr" })).toThrow( /Colocated client directives.*cannot be used.*unsupported.*reason: imperative-dom-query/, ); }); it("should throw when opaque-helper-call component has visible:onClick", () => { const code = ` import { defineComponent } from "@dathra/core"; import { unknownHelper } from "./helpers"; const Comp = defineComponent("x-comp", (props) => { return unknownHelper(() =>
    ); }); `; expect(() => transform(code, { mode: "csr" })).toThrow( /Colocated client directives.*cannot be used.*unsupported.*reason: opaque-helper-call/, ); }); it("should throw when unsupported-component-body (loop) has idle:onClick", () => { const code = ` import { defineComponent } from "@dathra/core"; const Comp = defineComponent("x-comp", (props) => { for (let i = 0; i < 3; i++) {} return
    ; }); `; expect(() => transform(code, { mode: "csr" })).toThrow( /Colocated client directives.*cannot be used.*unsupported.*reason: unsupported-component-body/, ); }); it("should throw when unsupported-component-body (async) has load:onClick", () => { const code = ` import { defineComponent } from "@dathra/core"; const Comp = defineComponent("x-comp", async (props) => { return
    ; }); `; expect(() => transform(code, { mode: "csr" })).toThrow( /Colocated client directives.*cannot be used.*unsupported.*reason: unsupported-component-body/, ); }); it("should throw when node-identity-reuse component has load:onClick", () => { const code = ` import { defineComponent } from "@dathra/core"; const Comp = defineComponent("x-comp", (props) => { const el = document.createElement("div"); return
    ; }); `; expect(() => transform(code, { mode: "csr" })).toThrow( /Colocated client directives.*cannot be used.*unsupported.*reason: node-identity-reuse/, ); }); it("should throw when non-normalizable-spread component has interaction:onClick", () => { const code = ` import { defineComponent } from "@dathra/core"; const Comp = defineComponent("x-comp", (props) => { const base = { a: 1 }; return
    ; }); `; expect(() => transform(code, { mode: "csr" })).toThrow( /Colocated client directives.*cannot be used.*unsupported.*reason: non-normalizable-spread/, ); }); it("should NOT throw when unsupported component has NO colocated directive", () => { const code = ` import { defineComponent } from "@dathra/core"; const Comp = defineComponent("x-comp", (props) => { try { return
    A
    ; } catch (e) { return B; } }); `; // Should not throw — just produces unsupportedReason metadata const result = transform(code, { mode: "csr" }); expect(result.code).toContain("unsupportedReason"); }); it("should throw in SSR mode when unsupported (try/catch) + colocated combine", () => { const code = ` import { defineComponent } from "@dathra/core"; const Comp = defineComponent("x-comp", (props) => { try { return
    ; } catch (e) { return error; } }); `; expect(() => transform(code, { mode: "ssr" })).toThrow( /Colocated client directives.*cannot be used.*unsupported/, ); }); it("should throw when colocated directive is deeply nested in unsupported (try/catch) component", () => { const code = ` import { defineComponent } from "@dathra/core"; const Comp = defineComponent("x-comp", (props) => { try { return
    ; } catch (e) { return error; } }); `; expect(() => transform(code, { mode: "csr" })).toThrow( /Colocated client directives.*cannot be used.*unsupported/, ); }); for (const strategy of COLOCATED_CLIENT_STRATEGIES) { it(`should throw for ${strategy}:onClick in unsupported (try/catch) component`, () => { const code = ` import { defineComponent } from "@dathra/core"; const Comp = defineComponent("x-comp", (props) => { try { return
    ; } catch (e) { return error; } }); `; expect(() => transform(code, { mode: "csr" })).toThrow( /Colocated client directives.*cannot be used.*unsupported/, ); }); } }); });