Files
webstudio/apps/builder/app/shared/copy-paste/plugin-jsx.test.ts
T
Oleg Isonen dde30ffb54 feat: Improve MCP authoring, compact output, and agent onboarding (#5883)
## Summary

This improves how agents discover, author, inspect, and verify Webstudio
projects while reducing unnecessary MCP context usage.

## What changed

- Adds an **Agents** section to Project Settings with ready-to-run setup
commands and client configuration for Claude Code, Codex, Cursor, and VS
Code.
- Makes large list and schema responses compact and paginated by
default, with **verbose** available when complete records or diagnostics
are needed.
- Applies consistent compact output to assets, instances, styles, design
tokens, CLI schemas, and project-session metadata.
- Returns actionable expression warnings for props, resources, and
Collection bindings, including unknown variables and risky render-time
mutation resources.
- Improves Collection expression guidance so agents can bind array items
with the correct scoped variables.
- Reports rendered audit outcomes explicitly as complete, partial,
confirmation-required, or failed.
- Expands runtime output schemas so MCP clients can validate responses
reliably.
- Strengthens the packaged CLI smoke workflow with a realistic
seven-page stdio MCP scenario.

## Architecture

- Consolidates `project-build` from 65 file-oriented entrypoints to 11
architecture-owned surfaces.
- Separates the core model, contracts, runtime authoring, transfer,
state, project sessions, MCP, visual tooling, persistence codecs, and
authenticated server services.
- Isolates Node-only JSX evaluation from browser-safe transfer utilities
and authenticated server modules.
- Keeps persistence codecs independent from authenticated database
services, preventing unrelated module initialization.
- Defines shared instance-path types once and reuses them across runtime
modules.
- Preserves Immer initialization through explicit package side-effect
metadata.
- Adds package tests that prevent a return to source-file entrypoints or
generic exposure of Node-only surfaces.

## Validation

- Full repository checks, tests, typechecks, generated-file checks, and
fixtures
- All 163 Builder test files and 2,141 tests
- All 71 project-build test files and 1,794 tests
- Lint and package-boundary validation
- Full packed CLI release smoke test, including install, preview, agent
setup, seven-page stdio MCP workflow, rendered audit, and structured
commands
2026-07-15 18:11:28 +02:00

115 lines
3.8 KiB
TypeScript

import { beforeEach, describe, expect, test, vi } from "vitest";
import { createEmptyWebstudioFragment } from "@webstudio-is/project-build/runtime";
import { inspectWebstudioJsxFragmentSyntax } from "@webstudio-is/project-build/transfer";
import { isLikelyWebstudioJsx, jsx } from "./plugin-jsx";
import { $project } from "../sync/data-stores";
import { pasteHandled, pasteIgnored } from "./copy-paste";
const mocks = vi.hoisted(() => ({
createJsxFragment: vi.fn(),
insertWebstudioFragmentAt: vi.fn(),
toastError: vi.fn(),
}));
vi.mock("../trpc/trpc-client", () => ({
nativeClient: {
build: {
createJsxFragment: {
query: mocks.createJsxFragment,
},
},
},
}));
vi.mock("../instance-utils/insert", () => ({
insertWebstudioFragmentAt: mocks.insertWebstudioFragmentAt,
}));
vi.mock("../builder-api", () => ({
builderApi: {
toast: {
error: mocks.toastError,
warn: vi.fn(),
},
},
}));
describe("jsx paste plugin", () => {
beforeEach(() => {
vi.clearAllMocks();
$project.set({ id: "project-id" } as never);
});
test("detects only Webstudio JSX", () => {
expect(isLikelyWebstudioJsx("<$.Box />")).toBe(true);
expect(isLikelyWebstudioJsx("\n < $ .Box />")).toBe(true);
expect(isLikelyWebstudioJsx("< $ . Box />")).toBe(true);
expect(isLikelyWebstudioJsx('<ws.element ws:tag="section" />')).toBe(true);
expect(isLikelyWebstudioJsx("<radix.Switch />")).toBe(true);
expect(isLikelyWebstudioJsx("<animation.VideoAnimation />")).toBe(true);
expect(isLikelyWebstudioJsx("<$. />")).toBe(false);
expect(isLikelyWebstudioJsx("<$.>")).toBe(false);
expect(isLikelyWebstudioJsx("</$.Box>")).toBe(false);
expect(isLikelyWebstudioJsx("<section>HTML</section>")).toBe(false);
expect(isLikelyWebstudioJsx("## Markdown")).toBe(false);
});
test("can import JSX syntax inspection without the Node evaluator", () => {
expect(() => inspectWebstudioJsxFragmentSyntax("<$.Box />")).not.toThrow();
expect(() =>
inspectWebstudioJsxFragmentSyntax("<$.Box data-secret={process.env} />")
).toThrow('Do not access "process" in JSX fragments');
});
test("converts and inserts Webstudio JSX", async () => {
const fragment = createEmptyWebstudioFragment();
mocks.createJsxFragment.mockResolvedValue(fragment);
mocks.insertWebstudioFragmentAt.mockReturnValue(true);
await expect(jsx.onPaste?.("<$.Box />")).resolves.toEqual(pasteHandled);
expect(mocks.createJsxFragment).toHaveBeenCalledWith({
projectId: "project-id",
source: "<$.Box />",
});
expect(mocks.insertWebstudioFragmentAt).toHaveBeenCalledWith(
fragment,
undefined,
undefined,
expect.objectContaining({
onBreakpointLimitMerge: expect.any(Function),
})
);
});
test("does not claim non-Webstudio JSX text", async () => {
await expect(jsx.onPaste?.("<section>HTML</section>")).resolves.toEqual(
pasteIgnored
);
expect(mocks.createJsxFragment).not.toHaveBeenCalled();
expect(mocks.insertWebstudioFragmentAt).not.toHaveBeenCalled();
});
test("does not claim JSX without a current project", async () => {
$project.set(undefined);
await expect(jsx.onPaste?.("<$.Box />")).resolves.toEqual(pasteIgnored);
expect(mocks.createJsxFragment).not.toHaveBeenCalled();
expect(mocks.insertWebstudioFragmentAt).not.toHaveBeenCalled();
});
test("reports conversion errors and stops paste fallback", async () => {
mocks.createJsxFragment.mockRejectedValue(new Error("Invalid JSX"));
await expect(jsx.onPaste?.('<$.Box ws:id="0" />')).resolves.toEqual({
success: false,
error: "Invalid JSX",
});
expect(mocks.toastError).not.toHaveBeenCalled();
expect(mocks.insertWebstudioFragmentAt).not.toHaveBeenCalled();
});
});