Files
webstudio/packages/cli/src/project-session-api.test.ts
T
Oleg Isonen 7ae2a8141e fix(cli): preflight MCP edits and preserve screenshot diagnostics (#6505)
## Summary

- Reject MCP build edits before local mutation when the negotiated
CLI/API editing contracts differ, with a direct instruction to restart
the MCP server using the current CLI. Reads, dry-runs, and legacy
servers remain unaffected.
- Preserve the browser capture's phase-specific timeout instead of
racing it with the generic MCP watchdog.
- Return stable structured diagnostics when a preview serves stale build
assets or the wrong generated project version.

## Triage

- Fixes #6479. The report records an MCP CLI contract different from the
API and says the current CLI succeeds. MCP now checks the contract
before attempting edit work and directs the agent to update/restart the
stale CLI.
- Addresses #6504. I could not reproduce the Windows timeout from this
macOS environment. The report's preview was stale and had no rendered
version; this change makes stale-project readiness and browser-phase
failures report distinct stable codes/issues. It does not claim to fix
an underlying Windows-only hang if the preview or browser process itself
never completes.

## Verification

- Full CLI suite: 72 files, 1,170 tests passed.
- CLI typecheck and Oxlint on changed files passed.
2026-10-07 11:02:12 +02:00

414 lines
12 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: { limit: 20 },
connection: {
projectId: "project-1",
origin: "https://example.com",
authToken: "token",
},
createProjectSession,
});
expect(session.read).toHaveBeenCalledWith(
"pages.list",
{ projectId: "project-1", limit: 20 },
{ 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("does not inject project id into strict audit input", async () => {
const session = {
initialize: vi.fn(async () => undefined),
read: vi.fn(async () => ({
operationId: "project.audit",
projectId: "project-1",
source: "local",
result: { findings: [] },
state: { committed: false, freshness: {} },
namespaces: {
read: [],
write: [],
invalidated: [],
missing: [],
},
diagnostics: [],
})),
mutate: vi.fn(),
refresh: vi.fn(),
executeServerOperation: vi.fn(),
};
await executeProjectSessionApiOperation({
command: "audit",
input: { scopes: ["seo"] },
connection: {
projectId: "project-1",
origin: "https://example.com",
authToken: "token",
},
createProjectSession: vi.fn(
() => session
) as unknown as CreateProjectSession,
});
expect(session.read).toHaveBeenCalledWith(
"project.audit",
{ scopes: ["seo"] },
{ 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,
getServerApiContract: async () => ({
clientVersion: "public-api:client",
serverVersion: "public-api:server",
supportedOperationIds: new Set(["auth.me"]),
missingServerOperationIds: [],
negotiated: true,
}),
});
expect(session.executeServerOperation).toHaveBeenCalledWith(
{
id: "auth.me",
method: "query",
invalidatesNamespaces: [],
refetchInvalidatedNamespaces: false,
},
{}
);
expect(result.result).toEqual({ actor: "token" });
});
test("rejects unsupported server-only commands before dispatch", async () => {
const session = {
initialize: vi.fn(async () => undefined),
read: vi.fn(),
mutate: vi.fn(),
executeServerOperation: vi.fn(),
refresh: vi.fn(),
};
await expect(
executeProjectSessionApiOperation({
command: "whoami",
input: {},
connection: {
projectId: "project-1",
origin: "https://example.com",
authToken: "token",
},
createProjectSession: vi.fn(
() => session
) as unknown as CreateProjectSession,
getServerApiContract: async () => ({
clientVersion: "public-api:client",
supportedOperationIds: new Set(),
missingServerOperationIds: ["auth.me"],
negotiated: false,
}),
})
).rejects.toThrow('does not advertise server operation "auth.me"');
expect(session.executeServerOperation).not.toHaveBeenCalled();
});
test("checks the editing contract without requiring an operation id", async () => {
const session = {
initialize: vi.fn(async () => undefined),
mutate: vi.fn(async () => ({ diagnostics: [] })),
read: vi.fn(),
refresh: vi.fn(),
executeServerOperation: vi.fn(),
};
const createProjectSession = vi.fn(
() => session
) as unknown as CreateProjectSession;
const getServerApiContract = vi.fn(async () => ({
clientVersion: "public-api:client",
serverVersion: "public-api:client",
supportedOperationIds: new Set<string>(),
missingServerOperationIds: [],
negotiated: true,
}));
await executeProjectSessionApiOperation({
command: "create-page",
input: { name: "Pricing", path: "/pricing" },
connection: {
projectId: "project-1",
origin: "https://example.com",
authToken: "token",
},
createProjectSession,
getServerApiContract,
});
expect(getServerApiContract).toHaveBeenCalledOnce();
expect(session.initialize).toHaveBeenCalledOnce();
expect(session.mutate).toHaveBeenCalledOnce();
});
test("rejects an incompatible editing contract before initializing a session", async () => {
const session = {
initialize: vi.fn(async () => undefined),
mutate: vi.fn(),
read: vi.fn(),
refresh: vi.fn(),
executeServerOperation: vi.fn(),
};
await expect(
executeProjectSessionApiOperation({
command: "create-page",
input: { name: "Pricing", path: "/pricing" },
connection: {
projectId: "project-1",
origin: "https://example.com",
authToken: "token",
},
createProjectSession: vi.fn(
() => session
) as unknown as CreateProjectSession,
getServerApiContract: async () => ({
clientVersion: "public-api:client",
serverVersion: "public-api:server",
supportedOperationIds: new Set<string>(),
missingServerOperationIds: [],
negotiated: true,
}),
})
).rejects.toMatchObject({ code: "API_CONTRACT_MISMATCH" });
expect(session.initialize).not.toHaveBeenCalled();
expect(session.mutate).not.toHaveBeenCalled();
});
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: "INVALID_INPUT",
message: "Page input is invalid.",
issues: [
{
code: "invalid_expression",
path: ["values", "title"],
message: "Invalid Webstudio expression",
constraint: "valid_webstudio_expression",
example: 'pageTitle ?? "Pricing"',
detail: "Unexpected token",
},
],
},
],
})),
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: "INVALID_INPUT",
message: "Page input is invalid.",
issues: [
expect.objectContaining({
path: ["values", "title"],
constraint: "valid_webstudio_expression",
}),
],
});
});
});