Files
WorkRoot-WRNexusJSComponent/test/hosted-studio.test.ts
T

58 lines
2.4 KiB
TypeScript

import { expect, test } from "bun:test";
import { createStudioHandler } from "@wrnexus/design-studio/server";
import { CORE_DESIGN_SYSTEMS } from "@wrnexus/design-system";
import { createContext } from "@wrnexus/core";
import designStudio from "../app/middleware/design-studio.ts";
test("application middleware mounts Studio before unmatched routes and preserves normal pages", async () => {
const send = (path: string) => {
const req = new Request("https://components.example" + path);
return designStudio(
createContext(req, new URL(req.url)),
() => new Response("normal page"),
);
};
expect(await (await send("/components/button")).text()).toBe("normal page");
expect(await (await send("/design-studio-other")).text()).toBe("normal page");
const redirect = await send("/design-studio");
expect(redirect.status).toBe(308);
expect(redirect.headers.get("location")).toBe("/design-studio/");
expect(await (await send("/design-studio/")).text()).toContain(
"WrNexus Design Studio",
);
expect((await send("/design-studio/studio.js")).status).toBe(200);
expect(
(await (await send("/design-studio/api/application")).json()).enabled,
).toBe(false);
});
test("deployed Studio serves assets and isolated previews without application writes", async () => {
const handler = await createStudioHandler({
hosted: { basePath: "/design-studio" },
});
const origin = "https://components.example";
const html = await handler(new Request(origin + "/design-studio/"));
expect(await html.text()).toContain('<base href="/design-studio/">');
expect(
(await handler(new Request(origin + "/design-studio/studio.js"))).status,
).toBe(200);
const request = (path: string, from = origin) =>
handler(
new Request(origin + "/design-studio/api/" + path, {
method: "POST",
headers: { "content-type": "application/json", origin: from },
body: JSON.stringify({
system: CORE_DESIGN_SYSTEMS.soft,
base: "none",
}),
}),
);
expect((await request("apply")).status).toBe(409);
expect((await request("preview", "https://other.example")).status).toBe(403);
const preview = await request("preview");
expect(preview.status).toBe(200);
const { url } = await preview.json();
expect(url).toStartWith("/design-studio/preview/");
expect((await handler(new Request(origin + url))).status).toBe(200);
});