import { expect, test } from "bun:test";
import { createProductionHandlers, type ProdManifest } from "../src/prod.ts";
const manifest: ProdManifest = {
pages: [
{
raw: "/",
mod: { default: () => "Production artifact", meta: { title: "Prod" } },
},
{
raw: "/partial",
staticShell:
'Build shell',
mod: {
default: () =>
'Request shellUser 42',
meta: { title: "Partial" },
__wrnexusRender: "partial-static",
},
},
{
raw: "/async",
mod: {
default: () => "Async page",
meta: { title: "Async" },
__wrnexusClientLoad: async () => ({ users: [{ id: 1, name: "Ada" }] }),
},
},
],
api: [],
realtime: [],
middleware: [],
components: [],
layouts: [],
};
const server = { upgrade: () => false } as never;
test("supervised production runtime injects reconnecting DOM-morph support", async () => {
const handlers = createProductionHandlers(manifest, { developmentRuntime: true });
const response = await handlers.fetch(new Request("http://localhost/"), server);
expect(response).toBeInstanceOf(Response);
expect(await (response as Response).text()).toContain("/__wrnexus/hmr");
});
test("production streams request regions into the build-time static shell", async () => {
const handlers = createProductionHandlers(manifest, {});
const response = (await handlers.fetch(
new Request("http://localhost/partial"),
server,
)) as Response;
const html = await response.text();
expect(response.headers.get("x-wrnexus-static-shell")).toBe("build");
expect(html).toContain("Build shell");
expect(html).not.toContain("Request shell");
expect(html).toContain("User 42");
});
test("normal production output remains free of development HMR", async () => {
const handlers = createProductionHandlers(manifest, {});
const response = await handlers.fetch(new Request("http://localhost/"), server);
expect(await (response as Response).text()).not.toContain("/__wrnexus/hmr");
});
test("production client-load endpoint returns only the requested named result", async () => {
const handlers = createProductionHandlers(manifest, {});
const response = (await handlers.fetch(
new Request("http://localhost/__wrnexus/client-load?route=%2Fasync&name=users"),
server,
)) as Response;
expect(response.status).toBe(200);
expect(response.headers.get("cache-control")).toBe("private, no-store");
expect(await response.json()).toEqual({ data: [{ id: 1, name: "Ada" }] });
});