Simplify Custom JSX v2 authoring
This commit is contained in:
177 files changed
+4775
-43426
No files matched your search
@@ -1,218 +1,164 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
|
||||
import {
|
||||
analyzeJsxTemplate,
|
||||
analyzeCustomWidgetAccessibility,
|
||||
analyzeRequestManifest,
|
||||
appendDataPath,
|
||||
createResponseTreeNode,
|
||||
CUSTOM_JSX_TEMPLATE_LIMIT,
|
||||
DEFAULT_CUSTOM_WIDGET_FORM_VALUES,
|
||||
customWidgetFormSchema,
|
||||
renameCustomWidgetRequest,
|
||||
} from "../workbench";
|
||||
import { CUSTOM_WIDGET_STARTER } from "../core/examples";
|
||||
import { CUSTOM_WIDGET_STARTER, customWidgetDefinitionSchema } from "../core";
|
||||
import {
|
||||
CUSTOM_WIDGET_OPTIONS_EXAMPLES,
|
||||
CUSTOM_WIDGET_REQUEST_EXAMPLES,
|
||||
getCustomWidgetDefaultOptionsJsonSchema,
|
||||
getCustomWidgetOptionsJsonSchema,
|
||||
getCustomWidgetRequestsJsonSchema,
|
||||
} from "../core/schema-references";
|
||||
import {
|
||||
customJsxSafeBlockCompletions,
|
||||
getCustomJsxComponentPropCompletions,
|
||||
getCustomJsxLocalConstCompletions,
|
||||
} from "../workbench/code-language";
|
||||
analyzeCustomWidgetAccessibility,
|
||||
analyzeJsxTemplate,
|
||||
analyzeRequestManifest,
|
||||
appendDataPath,
|
||||
createResponseTreeNode,
|
||||
CUSTOM_JSX_TEMPLATE_LIMIT,
|
||||
customWidgetFormSchema,
|
||||
DEFAULT_CUSTOM_WIDGET_FORM_VALUES,
|
||||
renameCustomWidgetOption,
|
||||
renameCustomWidgetRequest,
|
||||
} from "../workbench";
|
||||
import { getCustomJsxComponentPropCompletions } from "../workbench/code-language";
|
||||
|
||||
describe("Custom Widget workbench contracts", () => {
|
||||
it("always provides a valid starter template", () => {
|
||||
expect(DEFAULT_CUSTOM_WIDGET_FORM_VALUES.template.trim()).not.toBe("");
|
||||
expect(
|
||||
customWidgetFormSchema.safeParse({
|
||||
...DEFAULT_CUSTOM_WIDGET_FORM_VALUES,
|
||||
name: "Example",
|
||||
url: "https://example.com/api",
|
||||
}).success,
|
||||
).toBe(true);
|
||||
it("provides valid lean starter form values", () => {
|
||||
expect(customWidgetFormSchema.safeParse(DEFAULT_CUSTOM_WIDGET_FORM_VALUES).success).toBe(true);
|
||||
expect(JSON.parse(DEFAULT_CUSTOM_WIDGET_FORM_VALUES.sources)).toHaveProperty("default");
|
||||
});
|
||||
|
||||
it("accepts 50,000 template characters and rejects 50,001", () => {
|
||||
expect(analyzeJsxTemplate("x".repeat(CUSTOM_JSX_TEMPLATE_LIMIT))).not.toEqual(
|
||||
it("validates keyed request manifests", () => {
|
||||
expect(analyzeRequestManifest(JSON.stringify(CUSTOM_WIDGET_REQUEST_EXAMPLES.full))).toEqual([]);
|
||||
expect(analyzeRequestManifest("[]")).toEqual(
|
||||
expect.arrayContaining([expect.objectContaining({ code: "invalidRequestManifest" })]),
|
||||
);
|
||||
expect(getCustomWidgetRequestsJsonSchema()).toMatchObject({ type: "object" });
|
||||
expect(getCustomWidgetOptionsJsonSchema()).toMatchObject({ type: "object" });
|
||||
expect(CUSTOM_WIDGET_OPTIONS_EXAMPLES.full.limit.default).toBe(20);
|
||||
});
|
||||
|
||||
it("enforces the template size and named request references", () => {
|
||||
expect(analyzeJsxTemplate("x".repeat(CUSTOM_JSX_TEMPLATE_LIMIT + 1))).toEqual(
|
||||
expect.arrayContaining([expect.objectContaining({ code: "templateTooLong" })]),
|
||||
);
|
||||
expect(analyzeJsxTemplate("x".repeat(CUSTOM_JSX_TEMPLATE_LIMIT + 1))).toEqual(
|
||||
expect.arrayContaining([expect.objectContaining({ code: "templateTooLong", severity: "error" })]),
|
||||
);
|
||||
});
|
||||
|
||||
it("validates full request manifests rather than accepting any JSON array", () => {
|
||||
expect(analyzeRequestManifest("[]")).toEqual([]);
|
||||
expect(analyzeRequestManifest('[{"id":"broken"}]')).toEqual(
|
||||
expect.arrayContaining([expect.objectContaining({ code: "invalidRequestManifest", severity: "error" })]),
|
||||
);
|
||||
});
|
||||
|
||||
it("generates schema references and examples from canonical validators", () => {
|
||||
expect(getCustomWidgetRequestsJsonSchema()).toMatchObject({ type: "array" });
|
||||
expect(getCustomWidgetOptionsJsonSchema()).toMatchObject({ type: "object" });
|
||||
expect(getCustomWidgetDefaultOptionsJsonSchema()).toMatchObject({ type: "object" });
|
||||
expect(analyzeRequestManifest(JSON.stringify(CUSTOM_WIDGET_REQUEST_EXAMPLES.full))).toEqual([]);
|
||||
expect(CUSTOM_WIDGET_OPTIONS_EXAMPLES.full.defaults).toMatchObject({ limit: 20 });
|
||||
});
|
||||
|
||||
it("validates named request references and rejects inline network capabilities", () => {
|
||||
expect(analyzeJsxTemplate('<SubFetch requestId="known" />', { requestIds: ["known"] })).toEqual([]);
|
||||
expect(analyzeJsxTemplate('<SubFetch requestId="missing" url="/api" />', { requestIds: ["known"] })).toEqual(
|
||||
expect.arrayContaining([
|
||||
expect.objectContaining({ code: "inlineRequestProps" }),
|
||||
expect.objectContaining({ code: "unknownRequest", value: "missing" }),
|
||||
expect.objectContaining({ code: "unknownRequest" }),
|
||||
]),
|
||||
);
|
||||
});
|
||||
|
||||
it("accepts named temporary inputs and diagnoses dynamic bindings", () => {
|
||||
expect(analyzeJsxTemplate('<DatePicker bind="selectedDate" />')).toEqual([]);
|
||||
expect(analyzeJsxTemplate("<DatePicker bind={inputs.selectedDate} />")).toEqual(
|
||||
it("supports temporary inputs and diagnoses incompatible bindings", () => {
|
||||
expect(analyzeJsxTemplate('<TextInput bind="filter"/><Switch bind="filter"/>')).toEqual(
|
||||
expect.arrayContaining([expect.objectContaining({ code: "bindingTypeConflict" })]),
|
||||
);
|
||||
expect(analyzeJsxTemplate("<TextInput bind={inputs.filter} />")).toEqual(
|
||||
expect.arrayContaining([expect.objectContaining({ code: "dynamicInputBinding" })]),
|
||||
);
|
||||
expect(analyzeJsxTemplate('<Calendar bind="selectedDate" />')).toEqual(
|
||||
});
|
||||
|
||||
it("does not advertise statement blocks or IIFEs", () => {
|
||||
expect(
|
||||
analyzeJsxTemplate("<Text>{data.items.map(item => { const name = item.name; return name; })}</Text>"),
|
||||
).toEqual(
|
||||
expect.arrayContaining([
|
||||
expect.objectContaining({ code: "ast", value: expect.stringContaining("BINDING_UNAVAILABLE") }),
|
||||
expect.objectContaining({ code: "ast", value: expect.stringContaining("UNSUPPORTED_BLOCK_STATEMENT") }),
|
||||
]),
|
||||
);
|
||||
expect(analyzeJsxTemplate('<TextInput bind="filter"/><Switch bind="filter"/>')).toEqual(
|
||||
expect.arrayContaining([expect.objectContaining({ code: "bindingTypeConflict", value: "filter" })]),
|
||||
expect(analyzeJsxTemplate("<Text>{(() => data.name)()}</Text>")).toEqual(
|
||||
expect.arrayContaining([expect.objectContaining({ code: "ast", value: expect.stringContaining("IIFEs") })]),
|
||||
);
|
||||
});
|
||||
|
||||
it("completes only earlier immutable locals from the active block, even with unrelated syntax errors", () => {
|
||||
const source = `{data.first.map((item) => { const sibling = ; return sibling; })}
|
||||
{data.second.map((item) => {
|
||||
const current = item;
|
||||
return <Text>{current}</Text>;
|
||||
const future = item;
|
||||
})}`;
|
||||
const cursor = source.indexOf("return <Text>");
|
||||
const labels = getCustomJsxLocalConstCompletions(source, cursor).map(({ label }) => label);
|
||||
expect(labels).toContain("current");
|
||||
expect(labels).not.toContain("sibling");
|
||||
expect(labels).not.toContain("future");
|
||||
});
|
||||
|
||||
it("does not complete the active or future declarator", () => {
|
||||
const source = `{data.items.map((item) => {
|
||||
const first = item, current = fir, future = current;
|
||||
return future;
|
||||
})}`;
|
||||
const cursor = source.indexOf("= fir") + "= fir".length;
|
||||
const labels = getCustomJsxLocalConstCompletions(source, cursor).map(({ label }) => label);
|
||||
expect(labels).toContain("first");
|
||||
expect(labels).not.toContain("current");
|
||||
expect(labels).not.toContain("future");
|
||||
});
|
||||
|
||||
it("completes callback parameters and publishes safe-block snippets", () => {
|
||||
const source = `{data.items.map((item, index, collection) => {
|
||||
const value = item;
|
||||
return <Text>{value}</Text>;
|
||||
})}`;
|
||||
const cursor = source.indexOf("return <Text>");
|
||||
const labels = getCustomJsxLocalConstCompletions(source, cursor).map(({ label }) => label);
|
||||
expect(labels).toEqual(expect.arrayContaining(["item", "index", "collection", "value"]));
|
||||
expect(customJsxSafeBlockCompletions.map(({ label }) => label)).toEqual(["map block", "derived value IIFE"]);
|
||||
});
|
||||
|
||||
it("uses canonical component prop types for contextual JSX completions", () => {
|
||||
const textInput = getCustomJsxComponentPropCompletions("TextInput");
|
||||
expect(textInput).toEqual(
|
||||
expect.arrayContaining([
|
||||
expect.objectContaining({ label: "label=", detail: expect.stringContaining("ReactNode") }),
|
||||
expect.objectContaining({ label: "bind=", detail: "string" }),
|
||||
]),
|
||||
it("supports compound names and contextual prop completions", () => {
|
||||
expect(analyzeJsxTemplate("<Card.Section><Tabs.List /></Card.Section>")).toEqual([]);
|
||||
expect(getCustomJsxComponentPropCompletions("TextInput")).toEqual(
|
||||
expect.arrayContaining([expect.objectContaining({ label: "bind=" })]),
|
||||
);
|
||||
expect(getCustomJsxComponentPropCompletions("Portal")).toEqual([]);
|
||||
});
|
||||
|
||||
it("uses the AST security policy for computed reflective access while accepting benign fetch text", () => {
|
||||
expect(analyzeJsxTemplate("<Text>{data['con' + 'structor']}</Text>")).toEqual(
|
||||
expect.arrayContaining([expect.objectContaining({ code: "ast", severity: "error" })]),
|
||||
);
|
||||
expect(analyzeJsxTemplate("<Text>fetch the latest status</Text>")).toEqual([]);
|
||||
});
|
||||
|
||||
it("builds safe response-tree paths for identifiers, arrays, and arbitrary object keys", () => {
|
||||
expect(appendDataPath("data", "items", false)).toBe("data.items");
|
||||
expect(appendDataPath("data.items", "0", true)).toBe("data.items[0]");
|
||||
expect(appendDataPath("data", "first-name", false)).toBe('data["first-name"]');
|
||||
const tree = createResponseTreeNode({ items: [{ name: "one" }] }, "data", "data");
|
||||
expect(tree.children?.[0]?.children?.[0]?.children?.[0]?.value).toBe("data.items[0].name");
|
||||
});
|
||||
|
||||
it("renames a request and every canonical reference atomically", () => {
|
||||
it("renames keyed request references atomically", () => {
|
||||
const renamed = renameCustomWidgetRequest(
|
||||
{
|
||||
...CUSTOM_WIDGET_STARTER,
|
||||
requests: [
|
||||
{
|
||||
id: "items",
|
||||
sourceId: "default",
|
||||
...customWidgetDefinitionSchema.parse(CUSTOM_WIDGET_STARTER),
|
||||
requests: {
|
||||
items: {
|
||||
source: "default",
|
||||
kind: "query",
|
||||
method: "GET",
|
||||
pathTemplate: "/items",
|
||||
parameters: {},
|
||||
auth: "none",
|
||||
minimumBoardPermission: "view",
|
||||
trigger: "manual",
|
||||
path: "/items",
|
||||
auth: "inherit",
|
||||
permission: "view",
|
||||
trigger: "load",
|
||||
confirmation: undefined,
|
||||
},
|
||||
{
|
||||
id: "refresh",
|
||||
sourceId: "default",
|
||||
refresh: {
|
||||
source: "default",
|
||||
kind: "action",
|
||||
method: "POST",
|
||||
pathTemplate: "/refresh",
|
||||
parameters: {},
|
||||
auth: "none",
|
||||
minimumBoardPermission: "modify",
|
||||
path: "/refresh",
|
||||
auth: "inherit",
|
||||
permission: "modify",
|
||||
trigger: "manual",
|
||||
confirmation: undefined,
|
||||
invalidates: ["items"],
|
||||
},
|
||||
],
|
||||
optionsSchema: {
|
||||
type: "object",
|
||||
properties: {
|
||||
item: {
|
||||
type: "string",
|
||||
"x-homarr": {
|
||||
control: "select",
|
||||
optionsSource: { requestId: "items", valuePath: "$.id", labelPath: "$.name" },
|
||||
},
|
||||
},
|
||||
},
|
||||
additionalProperties: false,
|
||||
},
|
||||
template: '<Stack><SubFetch requestId="items" />{data["items"]}{status.items.ok}</Stack>',
|
||||
options: {
|
||||
item: {
|
||||
label: "Item",
|
||||
control: "select",
|
||||
default: "",
|
||||
choicesFrom: { request: "items", valuePath: "id", labelPath: "name" },
|
||||
},
|
||||
},
|
||||
template: '<Stack>{data["items"]}{status.items.ok}</Stack>',
|
||||
},
|
||||
"items",
|
||||
"inventory",
|
||||
);
|
||||
|
||||
expect(renamed.requests[0]?.id).toBe("inventory");
|
||||
expect(renamed.requests[1]?.invalidates).toEqual(["inventory"]);
|
||||
expect(JSON.stringify(renamed.optionsSchema)).toContain('"requestId":"inventory"');
|
||||
expect(renamed.template).toBe(
|
||||
'<Stack><SubFetch requestId="inventory" />{data["inventory"]}{status.inventory.ok}</Stack>',
|
||||
);
|
||||
expect(renamed.requests.inventory).toBeDefined();
|
||||
expect(renamed.requests.refresh?.invalidates).toEqual(["inventory"]);
|
||||
expect(renamed.options.item?.choicesFrom?.request).toBe("inventory");
|
||||
expect(renamed.template).toContain("inventory");
|
||||
});
|
||||
|
||||
it("reports common accessibility omissions in preview", () => {
|
||||
it("renames option references atomically", () => {
|
||||
const renamed = renameCustomWidgetOption(
|
||||
customWidgetDefinitionSchema.parse({
|
||||
...CUSTOM_WIDGET_STARTER,
|
||||
options: { room: { label: "Room", control: "text", default: "office" } },
|
||||
requests: {
|
||||
status: {
|
||||
path: "/rooms/{option:room}",
|
||||
query: { selected: { $option: "room" } },
|
||||
body: { room: { $option: "room" } },
|
||||
},
|
||||
},
|
||||
template: '<Text>{options.room}{options["room"]}</Text>',
|
||||
}),
|
||||
"room",
|
||||
"roomId",
|
||||
);
|
||||
expect(renamed.options.roomId?.default).toBe("office");
|
||||
expect(renamed.requests.status?.path).toBe("/rooms/{option:roomId}");
|
||||
expect(renamed.requests.status?.query).toEqual({ selected: { $option: "roomId" } });
|
||||
expect(renamed.requests.status?.body).toEqual({ room: { $option: "roomId" } });
|
||||
expect(renamed.template).toBe('<Text>{options.roomId}{options["roomId"]}</Text>');
|
||||
});
|
||||
|
||||
it("builds response paths and accessibility diagnostics", () => {
|
||||
expect(appendDataPath("data", "first-name", false)).toBe('data["first-name"]');
|
||||
expect(
|
||||
analyzeCustomWidgetAccessibility('<Stack><Image src="/image.png"/><ActionIcon/><TextInput /></Stack>'),
|
||||
).toEqual(["imageAlt", "actionIconLabel", "inputLabel"]);
|
||||
expect(
|
||||
analyzeCustomWidgetAccessibility(
|
||||
'<Stack><Image src="/image.png" alt="Preview"/><ActionIcon aria-label="Refresh"/><TextInput label="Search" /></Stack>',
|
||||
),
|
||||
).toEqual([]);
|
||||
createResponseTreeNode({ items: [{ name: "one" }] }, "data", "data").children?.[0]?.children?.[0]?.children?.[0]
|
||||
?.value,
|
||||
).toBe("data.items[0].name");
|
||||
expect(analyzeCustomWidgetAccessibility('<Image src="/image.png"/><ActionIcon/><TextInput />')).toEqual([
|
||||
"imageAlt",
|
||||
"actionIconLabel",
|
||||
"inputLabel",
|
||||
]);
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user