Files
webstudio/packages/react-sdk/src/collection-utils.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

139 lines
3.5 KiB
TypeScript

import { expect, test } from "vitest";
import {
getCollectionEntries,
generateCollectionIterationCode,
} from "./collection-utils";
test("getCollectionEntries handles arrays", () => {
expect(getCollectionEntries(["a", "b", "c"])).toEqual([
[0, "a"],
[1, "b"],
[2, "c"],
]);
});
test("getCollectionEntries handles objects", () => {
expect(getCollectionEntries({ first: "a", second: "b" })).toEqual([
["first", "a"],
["second", "b"],
]);
});
test("getCollectionEntries handles null/undefined", () => {
expect(getCollectionEntries(null)).toEqual([]);
expect(getCollectionEntries(undefined)).toEqual([]);
});
test("getCollectionEntries handles non-objects", () => {
expect(getCollectionEntries(42)).toEqual([]);
expect(getCollectionEntries("string")).toEqual([]);
expect(getCollectionEntries(true)).toEqual([]);
});
test("generateCollectionIterationCode produces correct template", () => {
const code = generateCollectionIterationCode({
dataExpression: "myData",
keyVariable: "index",
itemVariable: "item",
});
expect(code).toBe(`Object.entries(
// @ts-ignore
myData ?? {}
).map(([_key, item]: any) => {
const index = Array.isArray(myData) ? Number(_key) : _key;
return`);
});
test("generateCollectionIterationCode omits redundant fallback for static arrays", () => {
const code = generateCollectionIterationCode({
dataExpression: `["a","b"]`,
keyVariable: "index",
itemVariable: "item",
});
expect(code).toBe(`Object.entries(
// @ts-ignore
["a","b"]
).map(([_key, item]: any) => {
const index = Array.isArray(["a","b"]) ? Number(_key) : _key;
return`);
});
test("generateCollectionIterationCode omits redundant fallback for static objects", () => {
const code = generateCollectionIterationCode({
dataExpression: `({"first":"a"})`,
keyVariable: "key",
itemVariable: "item",
});
expect(code).toBe(`Object.entries(
// @ts-ignore
({"first":"a"})
).map(([_key, item]: any) => {
const key = Array.isArray(({"first":"a"})) ? Number(_key) : _key;
return`);
});
test("generated code works with arrays at runtime", () => {
// @ts-expect-error - used in eval below
const data = ["apple", "banana", "orange"];
const result: Array<[string, string]> = [];
// Simulate generated code
eval(`
Object.entries(data ?? {}).map(([index, item]) => {
result.push([index, item]);
});
`);
expect(result).toEqual([
["0", "apple"],
["1", "banana"],
["2", "orange"],
]);
});
test("generated code works with objects at runtime", () => {
// @ts-expect-error - used in eval below
const data = { first: "apple", second: "banana" };
const result: Array<[string, string]> = [];
// Simulate generated code
eval(`
Object.entries(data ?? {}).map(([index, item]) => {
result.push([index, item]);
});
`);
expect(result).toEqual([
["first", "apple"],
["second", "banana"],
]);
});
test("generated code handles null/undefined", () => {
// @ts-expect-error - used in eval below
let data: null | undefined = null;
const resultNull: Array<[string, unknown]> = [];
eval(`
Object.entries(data ?? {}).map(([index, item]) => {
resultNull.push([index, item]);
});
`);
expect(resultNull).toEqual([]);
data = undefined;
const resultUndefined: Array<[string, unknown]> = [];
eval(`
Object.entries(data ?? {}).map(([index, item]) => {
resultUndefined.push([index, item]);
});
`);
expect(resultUndefined).toEqual([]);
});