Open source website builder and Webflow alternative. Webstudio is an advanced visual builder that connects to any headless CMS, supports all CSS properties, and can be hosted anywhere, including with us.
TypeScript accessibilityalternativecloudflarecloudflare-workersdesignfreefrontendopen-sourceradix-uireactremixweb-developmentwebflowwebsite-builderwebsite-developmentwebsiteswebstudio
import { DocumentGraphResolutionError } from "@webstudio-is/content-engine";
import { afterEach, describe, expect, test, vi } from "vitest";
import { bundleVersion } from "@webstudio-is/protocol";
import { createPublishedProjectBundleFixture } from "@webstudio-is/protocol/fixtures";
import { buildRouter, __testing__ } from "./build-router.server";
import { authorizeProject } from "@webstudio-is/trpc-interface/index.server";
import { getApiCompatibilityPayload } from "@webstudio-is/trpc-interface/api-compatibility";
import {
createAssetIndex,
createCanonicalAssetFileEntry,
} from "@webstudio-is/content-engine/compiler";
const {
assertCliBundleVersion,
createImportProjectBundleHandler,
createLoadProjectBundleByBuildIdHandler,
loadContentDatabasePublishDiagnostics,
prepareProjectBundleForClient,
} = __testing__;
afterEach(() => {
vi.restoreAllMocks();
});
describe("build router project bundle compatibility", () => {
test("passes client content preparation to the build bundle loader", async () => {
const bundle = createPublishedProjectBundleFixture();
const loadProjectBundleByBuildId = vi.fn().mockResolvedValue(bundle);
const prepareProjectBundleForClient = vi.fn((_, value) => value);
const handler = createLoadProjectBundleByBuildIdHandler({
loadProjectBundleByBuildId: loadProjectBundleByBuildId as never,
prepareProjectBundleForClient: prepareProjectBundleForClient as never,
});
const context = { apiClient: { type: "cli" } } as never;
await expect(
handler(context, {
buildId: bundle.build.id,
bundleVersion,
contentIndex: "client",
})
).resolves.toBe(bundle);
expect(loadProjectBundleByBuildId).toHaveBeenCalledWith(
bundle.build.id,
context,
{ contentIndex: "client" }
);
expect(prepareProjectBundleForClient).toHaveBeenCalledWith(context, bundle);
});
test("requires the current bundle contract from CLI clients", () => {
const ctx = { apiClient: { type: "cli" } } as never;
let error: unknown;
try {
assertCliBundleVersion(ctx, undefined);
} catch (caught) {
error = caught;
}
expect(getApiCompatibilityPayload(error)).toMatchObject({
reason: "clientVersionUnsupported",
target: "cli",
message: `This version of the Webstudio CLI is incompatible with the current API. Expected bundle version ${bundleVersion}, received missing.`,
action: { type: "updateCli" },
});
expect(() => assertCliBundleVersion(ctx, bundleVersion)).not.toThrow();
});
test("reports expected and received bundle contract versions", () => {
const ctx = { apiClient: { type: "cli" } } as never;
const message = `This version of the Webstudio CLI is incompatible with the current API. Expected bundle version ${bundleVersion}, received bundle-client.`;
let error: unknown;
try {
assertCliBundleVersion(ctx, "bundle-client");
} catch (caught) {
error = caught;
}
expect(error).toMatchObject({ message });
expect(getApiCompatibilityPayload(error)?.message).toBe(message);
});
test("removes agent instructions from non-CLI bundles", async () => {
const bundle = createPublishedProjectBundleFixture({
build: {
projectSettings: {
meta: { siteName: "Acme", agentInstructions: "Internal guidance" },
compiler: {},
},
},
});
const result = await prepareProjectBundleForClient(
{ apiClient: { type: "browser" } } as never,
bundle
);
expect(result.build.projectSettings?.meta).toEqual({ siteName: "Acme" });
expect(bundle.build.projectSettings?.meta.agentInstructions).toBe(
"Internal guidance"
);
});
test("keeps agent instructions for authorized CLI bundles", async () => {
vi.spyOn(authorizeProject, "hasProjectPermit").mockResolvedValue(true);
const bundle = createPublishedProjectBundleFixture({
build: {
projectSettings: {
meta: { agentInstructions: "Internal guidance" },
compiler: {},
},
},
});
const result = await prepareProjectBundleForClient(
{ apiClient: { type: "cli" } } as never,
bundle
);
expect(result.build.projectSettings?.meta.agentInstructions).toBe(
"Internal guidance"
);
});
});
describe("content database publish diagnostics", () => {
test("uses MDX omissions collected while preparing the publication artifact", async () => {
const bundle = createPublishedProjectBundleFixture();
bundle.assets = [
{
id: "article",
projectId: bundle.build.projectId,
name: "article-storage.mdx",
filename: "article",
type: "file",
format: "mdx",
size: 1,
meta: {},
createdAt: "2026-09-17T00:00:00.000Z",
},
];
const loadProjectBundle = vi.fn(async (_projectId, _ctx, options) => {
options.onMdxTemplateOmissions([
{
assetId: "article",
blockInstanceId: "content-block",
templateName: "Missing",
},
]);
return bundle;
});
await expect(
loadContentDatabasePublishDiagnostics("project-id", {} as never, {
loadProjectBundleByProjectId: loadProjectBundle as never,
})
).resolves.toMatchObject({
mdxOmissions: [
{
assetId: "article",
blockInstanceId: "content-block",
filename: "article.mdx",
templateName: "Missing",
},
],
});
expect(loadProjectBundle).toHaveBeenCalledOnce();
});
test("reports publish-time MDX errors with the user-facing asset filename", async () => {
const bundle = createPublishedProjectBundleFixture();
const blockId = bundle.build.pages.pages[0].rootInstanceId;
const source = `1. item\n\n <ws.element ws:tag="li">nested item</ws.element>`;
bundle.build.instances = [
[
blockId,
{
id: blockId,
type: "instance",
component: "ws:block",
children: [{ type: "id", value: "templates" }],
},
],
[
"templates",
{
id: "templates",
type: "instance",
component: "ws:block-template",
children: [],
},
],
];
bundle.build.props = [
[
"content-source",
{
id: "content-source",
instanceId: blockId,
name: "src",
type: "asset",
value: "article",
},
],
];
bundle.assets = [
{
id: "article",
projectId: bundle.build.projectId,
name: "article-storage.mdx",
filename: "article",
type: "file",
format: "mdx",
size: new TextEncoder().encode(source).byteLength,
meta: {},
createdAt: "2026-09-17T00:00:00.000Z",
},
];
bundle.assetIndex = {
...(await createAssetIndex({
projectId: bundle.build.projectId,
entries: [
createCanonicalAssetFileEntry({
projectId: bundle.build.projectId,
document: {
_id: "article",
_type: "asset.file",
name: "article.mdx",
path: "blog/article.mdx",
key: "article",
extension: "mdx",
mimeType: "text/mdx",
size: new TextEncoder().encode(source).byteLength,
revision: "revision",
contentRef: "article-storage.mdx",
properties: {},
},
}),
],
maxBytes: 5000,
})),
contents: { "article-storage.mdx": source },
};
const loadProjectBundle = vi.fn(async (_projectId, _ctx, options) => {
options.onMdxBlockInstanceId(blockId);
return bundle;
});
const result = await loadContentDatabasePublishDiagnostics(
"project-id",
{} as never,
{ loadProjectBundleByProjectId: loadProjectBundle as never }
);
expect(result.mdxErrors).toEqual([
expect.objectContaining({
filename: "article.mdx",
diagnostic: expect.objectContaining({
code: "invalid-mdx",
severity: "error",
blockInstanceId: blockId,
assetId: "article",
message: "Placing <li> element inside a <li> violates HTML spec.",
}),
}),
]);
});
test("uses the same edit permit as publishing", async () => {
const hasProjectPermit = vi
.spyOn(authorizeProject, "hasProjectPermit")
.mockResolvedValue(false);
const caller = buildRouter.createCaller({} as never);
await expect(
caller.contentDatabasePublishDiagnostics({ projectId: "project-id" })
).rejects.toThrow("permission to edit this project");
expect(hasProjectPermit).toHaveBeenCalledWith(
{ projectId: "project-id", permit: "edit" },
expect.anything()
);
});
});
describe("build router jsx fragment conversion", () => {
test("converts Webstudio JSX through the shared runtime converter", async () => {
vi.spyOn(authorizeProject, "hasProjectPermit").mockResolvedValue(true);
const caller = buildRouter.createCaller({} as never);
await expect(
caller.createJsxFragment({
projectId: "project-id",
source: `<Box><Heading>Title</Heading></Box>`,
})
).resolves.toEqual(
expect.objectContaining({
instances: [
expect.objectContaining({ component: "Box" }),
expect.objectContaining({ component: "Heading" }),
],
})
);
});
test("rejects invalid Webstudio JSX with runtime validation messages", async () => {
vi.spyOn(authorizeProject, "hasProjectPermit").mockResolvedValue(true);
const caller = buildRouter.createCaller({} as never);
await expect(
caller.createJsxFragment({
projectId: "project-id",
source: `<Box {...{"ws:id": "0"}} />`,
})
).rejects.toThrow("Do not set ws:id in JSX fragments");
});
test("requires project access before converting JSX", async () => {
vi.spyOn(authorizeProject, "hasProjectPermit").mockResolvedValue(false);
const caller = buildRouter.createCaller({} as never);
await expect(
caller.createJsxFragment({
projectId: "project-id",
source: `<Box />`,
})
).rejects.toThrow("You don't have access to this project");
});
});
describe("build router project bundle import", () => {
test("imports staged project bundle data and removes the upload", async () => {
const data = createPublishedProjectBundleFixture();
const importPublishedProjectBundle = vi.fn().mockResolvedValue({
version: 2,
});
const readStagedUploadText = vi
.fn()
.mockResolvedValue(JSON.stringify(data));
const removeStagedUpload = vi.fn().mockResolvedValue(undefined);
const handler = createImportProjectBundleHandler({
importPublishedProjectBundle,
readStagedUploadText,
removeStagedUpload,
});
const ctx = { context: true } as never;
await expect(
handler({
ctx,
input: {
ignoreVersionCheck: true,
projectId: "project-id",
uploadId: "upload-id",
},
})
).resolves.toEqual({ version: 2 });
expect(readStagedUploadText).toHaveBeenCalledWith({
projectId: "project-id",
uploadId: "upload-id",
});
expect(importPublishedProjectBundle).toHaveBeenCalledWith({
ctx,
data,
ignoreVersionCheck: true,
projectId: "project-id",
});
expect(removeStagedUpload).toHaveBeenCalledWith("upload-id");
});
test("removes staged upload when staged data is invalid", async () => {
const importPublishedProjectBundle = vi.fn();
const readStagedUploadText = vi.fn().mockResolvedValue("{");
const removeStagedUpload = vi.fn().mockResolvedValue(undefined);
const handler = createImportProjectBundleHandler({
importPublishedProjectBundle,
readStagedUploadText,
removeStagedUpload,
});
await expect(
handler({
ctx: {} as never,
input: {
projectId: "project-id",
uploadId: "upload-id",
},
})
).rejects.toThrow();
expect(importPublishedProjectBundle).not.toHaveBeenCalled();
expect(removeStagedUpload).toHaveBeenCalledWith("upload-id");
});
test("keeps direct project bundle import unchanged", async () => {
const data = createPublishedProjectBundleFixture();
const importPublishedProjectBundle = vi.fn().mockResolvedValue({
version: 3,
});
const readStagedUploadText = vi.fn();
const removeStagedUpload = vi.fn();
const handler = createImportProjectBundleHandler({
importPublishedProjectBundle,
readStagedUploadText,
removeStagedUpload,
});
const ctx = { context: true } as never;
await expect(
handler({
ctx,
input: {
data,
projectId: "project-id",
},
})
).resolves.toEqual({ version: 3 });
expect(importPublishedProjectBundle).toHaveBeenCalledWith({
ctx,
data,
ignoreVersionCheck: undefined,
projectId: "project-id",
});
expect(readStagedUploadText).not.toHaveBeenCalled();
expect(removeStagedUpload).not.toHaveBeenCalled();
});
});
test("preserves document load failure ids for publish validation", async () => {
const error = new DocumentGraphResolutionError({
code: "DOCUMENT_LOAD_FAILED",
documentId: "asset-id",
message: "Storage is temporarily unavailable",
});
await expect(
loadContentDatabasePublishDiagnostics("project-id", {} as never, {
loadProjectBundleByProjectId: vi
.fn()
.mockRejectedValue(
new Error("Compilation failed", { cause: error })
) as never,
})
).rejects.toMatchObject({
code: "BAD_REQUEST",
cause: {
webstudioCode: "DOCUMENT_LOAD_FAILED",
issues: [
expect.objectContaining({
code: "DOCUMENT_LOAD_FAILED",
assetId: "asset-id",
}),
],
},
});
});
test.each([true, false])(
"only advertises import capabilities to an authorized caller: %s",
async (allowed) => {
vi.spyOn(authorizeProject, "hasProjectPermit").mockResolvedValue(allowed);
const result = buildRouter
.createCaller({} as never)
.checkProjectBuildPermission({ projectId: "project-id" });
if (allowed) {
await expect(result).resolves.toEqual({
supportsStructuredImportErrors: true,
});
} else {
await expect(result).rejects.toThrow();
}
}
);