Files
webstudio/packages/cli/src/project-session-api.test.ts
T
Oleg Isonen d291e4b671 feat: Builder api runtime contracts guardrails for MCP (#5857)
This PR expands Webstudio MCP and CLI into a more complete agent
authoring interface. Agents can discover project capabilities, edit real
Webstudio content, use data/resources, verify visual output, and report
useful issues when workflows fail.

## MCP User Capabilities

- Create, update, duplicate, reorder, and delete pages.
- Create pages from templates and manage reusable page templates.
- Inspect project pages, instances, props, styles, assets, variables,
resources, and metadata.
- Insert Webstudio components and authored JSX fragments into pages.
- Edit text, props, styles, breakpoints, design tokens, and CSS
variables.
- Update page metadata, SEO fields, redirects, marketplace metadata, and
project settings.
- Create and use static data variables.
- Create and use dynamic resources fetched over HTTP.
- Use REST, GraphQL, and built-in system resources in authored pages.
- Bind data variables and resources to visible page content.
- Create resource-backed pages such as sitemap XML, structured SEO data,
and API-driven content sections.
- Preview current project state without requiring a publish step.
- Capture screenshots for visual verification.
- Verify responsive designs across desktop, tablet, and mobile
breakpoints.
- Use screenshot diff and OCR-style checks to inspect rendered output.
- Reset and refresh local MCP project state when needed.
- Discover available CLI/MCP commands from generated manuals.
- Use clearer examples for resources, data variables, JSX authoring,
previewing, and visual checks.
- Read marketplace product metadata without requiring a full project
snapshot.
- Get safer guidance when using low-level patch tools.
- Get a ready-to-copy Discord report template when MCP/CLI behavior is
confusing or broken.
2026-07-10 02:13:22 +02:00

246 lines
6.7 KiB
TypeScript

import { describe, expect, test, vi } from "vitest";
import { executeProjectSessionApiOperation } from "./project-session-api";
type CreateProjectSession = NonNullable<
Parameters<
typeof executeProjectSessionApiOperation
>[0]["createProjectSession"]
>;
describe("project session api adapter", () => {
test("routes local-capable commands through project session runtime", async () => {
const session = {
initialize: vi.fn(async () => undefined),
read: vi.fn(async () => ({
operationId: "pages.list",
projectId: "project-1",
source: "local",
result: ["page"],
state: { committed: false, freshness: {} },
namespaces: {
read: ["pages"],
write: [],
invalidated: [],
missing: [],
},
diagnostics: [],
})),
mutate: vi.fn(),
refresh: vi.fn(),
executeServerOperation: vi.fn(),
};
const createProjectSession = vi.fn(
() => session
) as unknown as CreateProjectSession;
const result = await executeProjectSessionApiOperation({
command: "list-pages",
input: { includeFolders: true },
connection: {
projectId: "project-1",
origin: "https://example.com",
authToken: "token",
},
createProjectSession,
});
expect(session.read).toHaveBeenCalledWith(
"pages.list",
{ projectId: "project-1", includeFolders: true },
{ permit: "view" }
);
expect(result.result).toEqual(["page"]);
});
test("keeps configured project id for local-capable commands", async () => {
const session = {
initialize: vi.fn(async () => undefined),
read: vi.fn(async () => ({
operationId: "pages.list",
projectId: "project-1",
source: "local",
result: [],
state: { committed: false, freshness: {} },
namespaces: {
read: ["pages"],
write: [],
invalidated: [],
missing: [],
},
diagnostics: [],
})),
mutate: vi.fn(),
refresh: vi.fn(),
executeServerOperation: vi.fn(),
};
await executeProjectSessionApiOperation({
command: "list-pages",
input: { projectId: "other-project" },
connection: {
projectId: "project-1",
origin: "https://example.com",
authToken: "token",
},
createProjectSession: vi.fn(
() => session
) as unknown as CreateProjectSession,
});
expect(session.read).toHaveBeenCalledWith(
"pages.list",
{ projectId: "project-1" },
{ permit: "view" }
);
});
test("routes server-only commands through project session transport", async () => {
const session = {
initialize: vi.fn(async () => undefined),
read: vi.fn(),
mutate: vi.fn(),
executeServerOperation: vi.fn(async () => ({
operationId: "auth.me",
projectId: "project-1",
source: "server",
result: { actor: "token" },
state: { committed: true, freshness: {} },
namespaces: { read: [], write: [], invalidated: [], missing: [] },
diagnostics: [],
})),
refresh: vi.fn(),
};
const createProjectSession = vi.fn(
() => session
) as unknown as CreateProjectSession;
const result = await executeProjectSessionApiOperation({
command: "whoami",
input: {},
connection: {
projectId: "project-1",
origin: "https://example.com",
authToken: "token",
},
createProjectSession,
});
expect(session.executeServerOperation).toHaveBeenCalledWith(
{
id: "auth.me",
invalidatesNamespaces: [],
refetchInvalidatedNamespaces: false,
},
{}
);
expect(result.result).toEqual({ actor: "token" });
});
test("refreshes local namespaces before local-capable commands when requested", async () => {
const session = {
initialize: vi.fn(async () => undefined),
refresh: vi.fn(async () => undefined),
read: vi.fn(async () => ({
operationId: "pages.list",
projectId: "project-1",
source: "local",
result: ["page"],
state: { committed: false, freshness: {} },
namespaces: {
read: ["pages"],
write: [],
invalidated: [],
missing: [],
},
diagnostics: [],
})),
mutate: vi.fn(),
executeServerOperation: vi.fn(),
};
await executeProjectSessionApiOperation({
command: "list-pages",
input: {},
connection: {
projectId: "project-1",
origin: "https://example.com",
authToken: "token",
},
createProjectSession: vi.fn(
() => session
) as unknown as CreateProjectSession,
refresh: true,
});
expect(session.refresh).toHaveBeenCalledWith(["pages"]);
});
test("explains dry-run is only for local-capable mutations", async () => {
const createProjectSession = vi.fn() as unknown as CreateProjectSession;
await expect(
executeProjectSessionApiOperation({
command: "list-pages",
input: {},
connection: {
projectId: "project-1",
origin: "https://example.com",
authToken: "token",
},
createProjectSession,
dryRun: true,
})
).rejects.toThrow(
"list-pages does not support --dry-run. Use --dry-run only with local-capable mutation tools; omit it for read or server-only tools."
);
expect(createProjectSession).not.toHaveBeenCalled();
});
test("preserves project session diagnostic code on operation errors", async () => {
const session = {
initialize: vi.fn(async () => undefined),
read: vi.fn(async () => ({
operationId: "pages.list",
projectId: "project-1",
source: "local",
result: undefined,
state: { committed: false, freshness: {} },
namespaces: {
read: ["pages"],
write: [],
invalidated: [],
missing: [],
},
diagnostics: [
{
level: "error",
code: "PROJECT_SESSION_BUSY",
message: "Project session snapshot changed on disk.",
},
],
})),
mutate: vi.fn(),
refresh: vi.fn(),
executeServerOperation: vi.fn(),
};
await expect(
executeProjectSessionApiOperation({
command: "list-pages",
input: {},
connection: {
projectId: "project-1",
origin: "https://example.com",
authToken: "token",
},
createProjectSession: vi.fn(
() => session
) as unknown as CreateProjectSession,
})
).rejects.toMatchObject({
code: "PROJECT_SESSION_BUSY",
message: "Project session snapshot changed on disk.",
});
});
});