@copilotkit/shared 1.79.2-canary.1792172552 → 1.81.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/a2ui-envelope.cjs +154 -0
- package/dist/a2ui-envelope.cjs.map +1 -0
- package/dist/a2ui-envelope.d.cts +101 -0
- package/dist/a2ui-envelope.d.cts.map +1 -0
- package/dist/a2ui-envelope.d.mts +101 -0
- package/dist/a2ui-envelope.d.mts.map +1 -0
- package/dist/a2ui-envelope.mjs +151 -0
- package/dist/a2ui-envelope.mjs.map +1 -0
- package/dist/a2ui-prompts.cjs +139 -0
- package/dist/a2ui-prompts.cjs.map +1 -1
- package/dist/a2ui-prompts.d.cts +60 -1
- package/dist/a2ui-prompts.d.cts.map +1 -1
- package/dist/a2ui-prompts.d.mts +60 -1
- package/dist/a2ui-prompts.d.mts.map +1 -1
- package/dist/a2ui-prompts.mjs +134 -1
- package/dist/a2ui-prompts.mjs.map +1 -1
- package/dist/constants/index.cjs +7 -0
- package/dist/constants/index.cjs.map +1 -1
- package/dist/constants/index.d.cts +7 -1
- package/dist/constants/index.d.cts.map +1 -1
- package/dist/constants/index.d.mts +7 -1
- package/dist/constants/index.d.mts.map +1 -1
- package/dist/constants/index.mjs +7 -1
- package/dist/constants/index.mjs.map +1 -1
- package/dist/event-transforms/open-generative-ui-middleware.cjs +7 -6
- package/dist/event-transforms/open-generative-ui-middleware.cjs.map +1 -1
- package/dist/event-transforms/open-generative-ui-middleware.d.cts.map +1 -1
- package/dist/event-transforms/open-generative-ui-middleware.d.mts.map +1 -1
- package/dist/event-transforms/open-generative-ui-middleware.mjs +7 -6
- package/dist/event-transforms/open-generative-ui-middleware.mjs.map +1 -1
- package/dist/generative-ui/basic-functions.cjs +177 -0
- package/dist/generative-ui/basic-functions.cjs.map +1 -0
- package/dist/generative-ui/basic-functions.d.cts +108 -0
- package/dist/generative-ui/basic-functions.d.cts.map +1 -0
- package/dist/generative-ui/basic-functions.d.mts +108 -0
- package/dist/generative-ui/basic-functions.d.mts.map +1 -0
- package/dist/generative-ui/basic-functions.mjs +167 -0
- package/dist/generative-ui/basic-functions.mjs.map +1 -0
- package/dist/generative-ui/child-refs.cjs +161 -0
- package/dist/generative-ui/child-refs.cjs.map +1 -0
- package/dist/generative-ui/child-refs.d.cts +68 -0
- package/dist/generative-ui/child-refs.d.cts.map +1 -0
- package/dist/generative-ui/child-refs.d.mts +68 -0
- package/dist/generative-ui/child-refs.d.mts.map +1 -0
- package/dist/generative-ui/child-refs.mjs +152 -0
- package/dist/generative-ui/child-refs.mjs.map +1 -0
- package/dist/generative-ui/is-record.cjs +10 -0
- package/dist/generative-ui/is-record.cjs.map +1 -0
- package/dist/generative-ui/is-record.mjs +9 -0
- package/dist/generative-ui/is-record.mjs.map +1 -0
- package/dist/generative-ui/prompt.cjs +91 -0
- package/dist/generative-ui/prompt.cjs.map +1 -0
- package/dist/generative-ui/prompt.d.cts +13 -0
- package/dist/generative-ui/prompt.d.cts.map +1 -0
- package/dist/generative-ui/prompt.d.mts +13 -0
- package/dist/generative-ui/prompt.d.mts.map +1 -0
- package/dist/generative-ui/prompt.mjs +90 -0
- package/dist/generative-ui/prompt.mjs.map +1 -0
- package/dist/generative-ui/render-ui-args.cjs +160 -0
- package/dist/generative-ui/render-ui-args.cjs.map +1 -0
- package/dist/generative-ui/render-ui-args.d.cts +40 -0
- package/dist/generative-ui/render-ui-args.d.cts.map +1 -0
- package/dist/generative-ui/render-ui-args.d.mts +40 -0
- package/dist/generative-ui/render-ui-args.d.mts.map +1 -0
- package/dist/generative-ui/render-ui-args.mjs +159 -0
- package/dist/generative-ui/render-ui-args.mjs.map +1 -0
- package/dist/generative-ui/types.cjs +101 -0
- package/dist/generative-ui/types.cjs.map +1 -0
- package/dist/generative-ui/types.d.cts +270 -0
- package/dist/generative-ui/types.d.cts.map +1 -0
- package/dist/generative-ui/types.d.mts +270 -0
- package/dist/generative-ui/types.d.mts.map +1 -0
- package/dist/generative-ui/types.mjs +86 -0
- package/dist/generative-ui/types.mjs.map +1 -0
- package/dist/generative-ui/validate-operation.cjs +405 -0
- package/dist/generative-ui/validate-operation.cjs.map +1 -0
- package/dist/generative-ui/validate-operation.d.cts +86 -0
- package/dist/generative-ui/validate-operation.d.cts.map +1 -0
- package/dist/generative-ui/validate-operation.d.mts +86 -0
- package/dist/generative-ui/validate-operation.d.mts.map +1 -0
- package/dist/generative-ui/validate-operation.mjs +399 -0
- package/dist/generative-ui/validate-operation.mjs.map +1 -0
- package/dist/index.cjs +64 -0
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +11 -4
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.mts +11 -4
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +10 -3
- package/dist/index.mjs.map +1 -1
- package/dist/index.umd.js +1466 -29
- package/dist/index.umd.js.map +1 -1
- package/dist/package.cjs +1 -1
- package/dist/package.mjs +1 -1
- package/dist/telemetry/lambda-client.cjs.map +1 -1
- package/dist/telemetry/lambda-client.mjs.map +1 -1
- package/dist/telemetry/utils.cjs +0 -2
- package/dist/telemetry/utils.cjs.map +1 -1
- package/dist/telemetry/utils.mjs +0 -2
- package/dist/telemetry/utils.mjs.map +1 -1
- package/dist/utils/dashboard-url.cjs +19 -0
- package/dist/utils/dashboard-url.cjs.map +1 -0
- package/dist/utils/dashboard-url.mjs +18 -0
- package/dist/utils/dashboard-url.mjs.map +1 -0
- package/dist/utils/index.cjs +2 -1
- package/dist/utils/index.cjs.map +1 -1
- package/dist/utils/index.d.cts +1 -1
- package/dist/utils/index.d.cts.map +1 -1
- package/dist/utils/index.d.mts +1 -1
- package/dist/utils/index.d.mts.map +1 -1
- package/dist/utils/index.mjs +2 -1
- package/dist/utils/index.mjs.map +1 -1
- package/dist/utils/inspector-learning.cjs +48 -16
- package/dist/utils/inspector-learning.cjs.map +1 -1
- package/dist/utils/inspector-learning.d.cts +14 -1
- package/dist/utils/inspector-learning.d.cts.map +1 -1
- package/dist/utils/inspector-learning.d.mts +14 -1
- package/dist/utils/inspector-learning.d.mts.map +1 -1
- package/dist/utils/inspector-learning.mjs +49 -16
- package/dist/utils/inspector-learning.mjs.map +1 -1
- package/dist/utils/inspector-metadata.cjs +2 -3
- package/dist/utils/inspector-metadata.cjs.map +1 -1
- package/dist/utils/inspector-metadata.d.cts.map +1 -1
- package/dist/utils/inspector-metadata.d.mts.map +1 -1
- package/dist/utils/inspector-metadata.mjs +3 -3
- package/dist/utils/inspector-metadata.mjs.map +1 -1
- package/package.json +3 -4
- package/src/__tests__/a2ui-envelope.test.ts +434 -0
- package/src/__tests__/a2ui-prompts.test.ts +362 -0
- package/src/a2ui-envelope.ts +309 -0
- package/src/a2ui-prompts.ts +207 -0
- package/src/constants/index.ts +7 -0
- package/src/event-transforms/open-generative-ui-middleware.ts +19 -10
- package/src/generative-ui/__tests__/render-ui-args.test.ts +175 -0
- package/src/generative-ui/__tests__/validate-operation.test.ts +977 -0
- package/src/generative-ui/basic-functions.ts +282 -0
- package/src/generative-ui/child-refs.ts +210 -0
- package/src/generative-ui/index.ts +6 -0
- package/src/generative-ui/is-record.ts +4 -0
- package/src/generative-ui/prompt.ts +174 -0
- package/src/generative-ui/render-ui-args.ts +199 -0
- package/src/generative-ui/types.ts +314 -0
- package/src/generative-ui/validate-operation.ts +723 -0
- package/src/index.ts +15 -0
- package/src/telemetry/lambda-client.ts +3 -3
- package/src/telemetry/utils.ts +0 -28
- package/src/utils/dashboard-url.ts +17 -0
- package/src/utils/index.ts +7 -1
- package/src/utils/inspector-learning.test.ts +210 -0
- package/src/utils/inspector-learning.ts +117 -17
- package/src/utils/inspector-metadata.test.ts +2 -1
- package/src/utils/inspector-metadata.ts +3 -13
- package/src/utils.test.ts +71 -0
- package/tsdown.config.ts +0 -1
- package/src/telemetry/scarf-client.ts +0 -39
- package/src/telemetry/security-check.ts +0 -0
|
@@ -0,0 +1,977 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
import {
|
|
3
|
+
getOperationKind,
|
|
4
|
+
getOperationSurfaceId,
|
|
5
|
+
isIndexCall,
|
|
6
|
+
isPathBinding,
|
|
7
|
+
validateOperation,
|
|
8
|
+
} from "../validate-operation";
|
|
9
|
+
import type { ValidateOperationOptions } from "../validate-operation";
|
|
10
|
+
import { A2UI_COMPONENT_ID_REF } from "../child-refs";
|
|
11
|
+
import {
|
|
12
|
+
GENERATIVE_UI_AGENT_FUNCTION_CALLS,
|
|
13
|
+
GENERATIVE_UI_AGENT_FUNCTION_CALLS_OFF_REASON,
|
|
14
|
+
} from "../types";
|
|
15
|
+
import type { GenerativeUIManifest } from "../types";
|
|
16
|
+
|
|
17
|
+
const manifest: Pick<GenerativeUIManifest, "catalogId" | "components"> = {
|
|
18
|
+
catalogId: "shop",
|
|
19
|
+
components: {
|
|
20
|
+
ProductGrid: {
|
|
21
|
+
props: { type: "object", properties: { title: { type: "string" } } },
|
|
22
|
+
slots: { children: "multiple" },
|
|
23
|
+
},
|
|
24
|
+
ProductCard: {
|
|
25
|
+
props: {
|
|
26
|
+
type: "object",
|
|
27
|
+
properties: { name: { type: "string" }, price: { type: "number" } },
|
|
28
|
+
required: ["name", "price"],
|
|
29
|
+
},
|
|
30
|
+
events: { onAddToCart: { parameters: { type: "object" } } },
|
|
31
|
+
},
|
|
32
|
+
Frame: {
|
|
33
|
+
props: { type: "object", properties: {} },
|
|
34
|
+
slots: { child: "single" },
|
|
35
|
+
},
|
|
36
|
+
// A typed child reference inside a list, as A2UI 1.0 Tabs has it.
|
|
37
|
+
Tabs: {
|
|
38
|
+
props: {
|
|
39
|
+
type: "object",
|
|
40
|
+
properties: {
|
|
41
|
+
tabs: {
|
|
42
|
+
type: "array",
|
|
43
|
+
items: {
|
|
44
|
+
type: "object",
|
|
45
|
+
properties: {
|
|
46
|
+
title: { type: "string" },
|
|
47
|
+
child: { $ref: A2UI_COMPONENT_ID_REF },
|
|
48
|
+
},
|
|
49
|
+
},
|
|
50
|
+
},
|
|
51
|
+
},
|
|
52
|
+
},
|
|
53
|
+
},
|
|
54
|
+
},
|
|
55
|
+
};
|
|
56
|
+
const functions = {
|
|
57
|
+
addToCart: {
|
|
58
|
+
parameters: { type: "object" },
|
|
59
|
+
allowedCallers: "rendererOrAgent" as const,
|
|
60
|
+
},
|
|
61
|
+
};
|
|
62
|
+
const knownSurfaces = new Set(["s1"]);
|
|
63
|
+
|
|
64
|
+
const update = (...components: unknown[]) => ({
|
|
65
|
+
version: "v1.0",
|
|
66
|
+
updateComponents: { surfaceId: "s1", components },
|
|
67
|
+
});
|
|
68
|
+
const setData = (fields: { path?: string; value?: unknown }) => ({
|
|
69
|
+
version: "v1.0",
|
|
70
|
+
updateDataModel: { surfaceId: "s1", ...fields },
|
|
71
|
+
});
|
|
72
|
+
const create = (fields: Record<string, unknown>) => ({
|
|
73
|
+
version: "v1.0",
|
|
74
|
+
createSurface: { surfaceId: "s2", ...fields },
|
|
75
|
+
});
|
|
76
|
+
const callFunction = (fields: Record<string, unknown>) => ({
|
|
77
|
+
version: "v1.0",
|
|
78
|
+
callRendererFunction: { functionCallId: "f1", ...fields },
|
|
79
|
+
});
|
|
80
|
+
/** A valid ProductCard with `props` added or replaced. */
|
|
81
|
+
const card = (props: Record<string, unknown>) =>
|
|
82
|
+
update({ id: "c1", component: "ProductCard", name: "A", price: 1, ...props });
|
|
83
|
+
|
|
84
|
+
const reasonOf = (
|
|
85
|
+
op: unknown,
|
|
86
|
+
options?: Partial<Omit<ValidateOperationOptions, "manifest">>,
|
|
87
|
+
) => {
|
|
88
|
+
const result = validateOperation(op, { manifest, knownSurfaces, ...options });
|
|
89
|
+
return result.ok ? "ok" : result.reason;
|
|
90
|
+
};
|
|
91
|
+
|
|
92
|
+
describe("validateOperation: version and kind", () => {
|
|
93
|
+
it("rejects a v0.9 operation with a reason that names v1.0", () => {
|
|
94
|
+
expect(
|
|
95
|
+
reasonOf({ version: "v0.9", deleteSurface: { surfaceId: "s1" } }),
|
|
96
|
+
).toBe('unsupported version "v0.9"; use "v1.0"');
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
it("rejects an operation without a version", () => {
|
|
100
|
+
expect(reasonOf({ deleteSurface: { surfaceId: "s1" } })).toBe(
|
|
101
|
+
'operation needs "version":"v1.0"',
|
|
102
|
+
);
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
it("rejects ops with zero or several kinds", () => {
|
|
106
|
+
expect(reasonOf({ version: "v1.0" })).toMatch(/exactly one/);
|
|
107
|
+
expect(
|
|
108
|
+
reasonOf({
|
|
109
|
+
version: "v1.0",
|
|
110
|
+
deleteSurface: { surfaceId: "s1" },
|
|
111
|
+
updateDataModel: { surfaceId: "s1", value: 1 },
|
|
112
|
+
}),
|
|
113
|
+
).toMatch(/exactly one/);
|
|
114
|
+
});
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
describe("validateOperation: createSurface", () => {
|
|
118
|
+
it("accepts a new surface without a catalogId, or with the manifest catalogId", () => {
|
|
119
|
+
expect(reasonOf(create({}))).toBe("ok");
|
|
120
|
+
expect(reasonOf(create({ catalogId: "shop" }))).toBe("ok");
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
it("rejects a foreign catalog", () => {
|
|
124
|
+
expect(reasonOf(create({ catalogId: "other" }))).toBe(
|
|
125
|
+
'unknown catalog "other"; leave out catalogId or use "shop"',
|
|
126
|
+
);
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
it("accepts every listed catalog, and names them all when it rejects one", () => {
|
|
130
|
+
const options = {
|
|
131
|
+
manifest: { ...manifest, catalogIds: ["shop", "copilotkit/standard@1"] },
|
|
132
|
+
knownSurfaces: new Set<string>(),
|
|
133
|
+
};
|
|
134
|
+
const check = (catalogId: string) =>
|
|
135
|
+
validateOperation(
|
|
136
|
+
{ version: "v1.0", createSurface: { surfaceId: "s2", catalogId } },
|
|
137
|
+
options,
|
|
138
|
+
);
|
|
139
|
+
expect(check("copilotkit/standard@1")).toEqual({ ok: true });
|
|
140
|
+
expect(check("other")).toEqual({
|
|
141
|
+
ok: false,
|
|
142
|
+
reason:
|
|
143
|
+
'unknown catalog "other"; leave out catalogId or use one of: "shop", "copilotkit/standard@1"',
|
|
144
|
+
});
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
it("rejects a createSurface without a surfaceId", () => {
|
|
148
|
+
expect(
|
|
149
|
+
reasonOf({ version: "v1.0", createSurface: { catalogId: "shop" } }),
|
|
150
|
+
).toBe("createSurface.surfaceId is required");
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
it("rejects an existing surface", () => {
|
|
154
|
+
expect(
|
|
155
|
+
reasonOf({ version: "v1.0", createSurface: { surfaceId: "s1" } }),
|
|
156
|
+
).toBe('surface "s1" already exists');
|
|
157
|
+
});
|
|
158
|
+
|
|
159
|
+
it("rejects a surface id that was created before, also after a delete", () => {
|
|
160
|
+
expect(reasonOf(create({}), { createdSurfaces: new Set(["s2"]) })).toBe(
|
|
161
|
+
'surface id "s2" was used before; use a new surfaceId',
|
|
162
|
+
);
|
|
163
|
+
});
|
|
164
|
+
|
|
165
|
+
it("accepts inline components and a data model", () => {
|
|
166
|
+
expect(
|
|
167
|
+
reasonOf(
|
|
168
|
+
create({
|
|
169
|
+
dataModel: { products: [{ name: "Lamp" }] },
|
|
170
|
+
components: [
|
|
171
|
+
{
|
|
172
|
+
id: "root",
|
|
173
|
+
component: "ProductGrid",
|
|
174
|
+
children: { componentId: "c1", path: "/products" },
|
|
175
|
+
},
|
|
176
|
+
],
|
|
177
|
+
}),
|
|
178
|
+
),
|
|
179
|
+
).toBe("ok");
|
|
180
|
+
});
|
|
181
|
+
|
|
182
|
+
it("checks inline components like updateComponents", () => {
|
|
183
|
+
expect(
|
|
184
|
+
reasonOf(create({ components: [{ id: "x", component: "Nope" }] })),
|
|
185
|
+
).toBe('component "x": unknown component "Nope"');
|
|
186
|
+
expect(reasonOf(create({ components: [] }))).toBe(
|
|
187
|
+
"createSurface.components must be a non-empty array",
|
|
188
|
+
);
|
|
189
|
+
});
|
|
190
|
+
|
|
191
|
+
it("rejects an inline data model that is not a JSON object", () => {
|
|
192
|
+
for (const dataModel of [[1], "s", null]) {
|
|
193
|
+
expect(reasonOf(create({ dataModel }))).toBe(
|
|
194
|
+
"createSurface.dataModel must be a JSON object",
|
|
195
|
+
);
|
|
196
|
+
}
|
|
197
|
+
});
|
|
198
|
+
});
|
|
199
|
+
|
|
200
|
+
describe("validateOperation: surface updates", () => {
|
|
201
|
+
it("rejects updates to surfaces that do not exist", () => {
|
|
202
|
+
expect(
|
|
203
|
+
reasonOf({
|
|
204
|
+
version: "v1.0",
|
|
205
|
+
updateComponents: {
|
|
206
|
+
surfaceId: "missing",
|
|
207
|
+
components: [{ id: "root", component: "ProductGrid" }],
|
|
208
|
+
},
|
|
209
|
+
}),
|
|
210
|
+
).toBe('surface "missing" does not exist; send createSurface first');
|
|
211
|
+
});
|
|
212
|
+
|
|
213
|
+
it("accepts an update to a known surface that is also in createdSurfaces", () => {
|
|
214
|
+
expect(
|
|
215
|
+
reasonOf(update({ id: "root", component: "ProductGrid" }), {
|
|
216
|
+
createdSurfaces: new Set(["s1"]),
|
|
217
|
+
}),
|
|
218
|
+
).toBe("ok");
|
|
219
|
+
});
|
|
220
|
+
|
|
221
|
+
it("accepts a grid with child references and a card with literal and bound props", () => {
|
|
222
|
+
expect(
|
|
223
|
+
reasonOf(
|
|
224
|
+
update(
|
|
225
|
+
{
|
|
226
|
+
id: "root",
|
|
227
|
+
component: "ProductGrid",
|
|
228
|
+
title: "Picks",
|
|
229
|
+
children: ["c1", "c2"],
|
|
230
|
+
},
|
|
231
|
+
{
|
|
232
|
+
id: "c1",
|
|
233
|
+
component: "ProductCard",
|
|
234
|
+
name: "Headphones",
|
|
235
|
+
price: { "@path": "/prices/c1" },
|
|
236
|
+
onAddToCart: {
|
|
237
|
+
functionCall: { "@call": "addToCart", args: { productId: "p1" } },
|
|
238
|
+
},
|
|
239
|
+
},
|
|
240
|
+
),
|
|
241
|
+
{ functions },
|
|
242
|
+
),
|
|
243
|
+
).toBe("ok");
|
|
244
|
+
});
|
|
245
|
+
|
|
246
|
+
it("rejects a v0.9 path binding in a data prop with a reason that names @path", () => {
|
|
247
|
+
expect(reasonOf(card({ price: { path: "/prices/c1" } }))).toBe(
|
|
248
|
+
'component "c1": price: bind data with {"@path": "/x"}, not {"path": "/x"}',
|
|
249
|
+
);
|
|
250
|
+
});
|
|
251
|
+
|
|
252
|
+
it("rejects unknown components, unknown props and missing required props", () => {
|
|
253
|
+
expect(reasonOf(update({ id: "x", component: "Nope" }))).toMatch(
|
|
254
|
+
/unknown component "Nope"/,
|
|
255
|
+
);
|
|
256
|
+
expect(reasonOf(card({ color: "red" }))).toMatch(/unknown prop "color"/);
|
|
257
|
+
expect(
|
|
258
|
+
reasonOf(update({ id: "c1", component: "ProductCard", name: "A" })),
|
|
259
|
+
).toMatch(/missing required prop "price"/);
|
|
260
|
+
});
|
|
261
|
+
|
|
262
|
+
it("rejects a component without an id", () => {
|
|
263
|
+
expect(reasonOf(update({ component: "ProductGrid" }))).toBe(
|
|
264
|
+
"id is required",
|
|
265
|
+
);
|
|
266
|
+
});
|
|
267
|
+
|
|
268
|
+
it("accepts the A2UI component envelope: the own catalogId, accessibility and metadata", () => {
|
|
269
|
+
expect(
|
|
270
|
+
reasonOf(
|
|
271
|
+
card({
|
|
272
|
+
catalogId: "shop",
|
|
273
|
+
accessibility: {
|
|
274
|
+
label: { "@path": "/name" },
|
|
275
|
+
description: {
|
|
276
|
+
"@call": "formatString",
|
|
277
|
+
args: { value: "Adds ${/name}" },
|
|
278
|
+
},
|
|
279
|
+
live: "polite",
|
|
280
|
+
hidden: false,
|
|
281
|
+
},
|
|
282
|
+
metadata: { extensions: { "acme.track": "cart" } },
|
|
283
|
+
}),
|
|
284
|
+
),
|
|
285
|
+
).toBe("ok");
|
|
286
|
+
expect(reasonOf(card({ metadata: {} }))).toBe("ok");
|
|
287
|
+
});
|
|
288
|
+
|
|
289
|
+
it("rejects an envelope with a foreign catalog, a bad accessibility or malformed metadata", () => {
|
|
290
|
+
expect(reasonOf(card({ catalogId: "other" }))).toBe(
|
|
291
|
+
'component "c1": catalogId: unknown catalog "other"; leave out catalogId or use "shop"',
|
|
292
|
+
);
|
|
293
|
+
expect(reasonOf(card({ accessibility: "x" }))).toMatch(
|
|
294
|
+
/accessibility: must be \{ "label"/,
|
|
295
|
+
);
|
|
296
|
+
expect(reasonOf(card({ accessibility: { role: "img" } }))).toBe(
|
|
297
|
+
'component "c1": accessibility: unknown key "role"',
|
|
298
|
+
);
|
|
299
|
+
expect(reasonOf(card({ accessibility: { live: "loud" } }))).toBe(
|
|
300
|
+
'component "c1": accessibility: live must be one of off, polite, assertive',
|
|
301
|
+
);
|
|
302
|
+
expect(reasonOf(card({ accessibility: { hidden: "yes" } }))).toBe(
|
|
303
|
+
'component "c1": accessibility: hidden must be a boolean',
|
|
304
|
+
);
|
|
305
|
+
expect(reasonOf(card({ accessibility: { label: 1 } }))).toBe(
|
|
306
|
+
'component "c1": accessibility: label must be a string',
|
|
307
|
+
);
|
|
308
|
+
expect(
|
|
309
|
+
reasonOf(card({ accessibility: { label: { path: "/name" } } })),
|
|
310
|
+
).toBe(
|
|
311
|
+
'component "c1": accessibility: label: bind data with {"@path": "/x"}, not {"path": "/x"}',
|
|
312
|
+
);
|
|
313
|
+
expect(
|
|
314
|
+
reasonOf(card({ accessibility: { label: { "@call": "openUrl" } } })),
|
|
315
|
+
).toMatch(/accessibility: label: /);
|
|
316
|
+
expect(reasonOf(card({ metadata: { extensions: 1 } }))).toBe(
|
|
317
|
+
'component "c1": metadata must be { "extensions": { ... } }',
|
|
318
|
+
);
|
|
319
|
+
});
|
|
320
|
+
|
|
321
|
+
it("rejects slot values that are not id references or reference the node itself", () => {
|
|
322
|
+
expect(
|
|
323
|
+
reasonOf(
|
|
324
|
+
update({ id: "root", component: "ProductGrid", children: "c1" }),
|
|
325
|
+
),
|
|
326
|
+
).toMatch(/array/);
|
|
327
|
+
expect(
|
|
328
|
+
reasonOf(
|
|
329
|
+
update({ id: "root", component: "ProductGrid", children: ["root"] }),
|
|
330
|
+
),
|
|
331
|
+
).toMatch(/its own child/);
|
|
332
|
+
expect(
|
|
333
|
+
reasonOf(update({ id: "f", component: "Frame", child: ["a"] })),
|
|
334
|
+
).toMatch(/component ids/);
|
|
335
|
+
});
|
|
336
|
+
|
|
337
|
+
it("rejects duplicate ids within one update", () => {
|
|
338
|
+
expect(
|
|
339
|
+
reasonOf(
|
|
340
|
+
update(
|
|
341
|
+
{ id: "root", component: "ProductGrid" },
|
|
342
|
+
{ id: "root", component: "ProductGrid" },
|
|
343
|
+
),
|
|
344
|
+
),
|
|
345
|
+
).toMatch(/appears twice/);
|
|
346
|
+
});
|
|
347
|
+
});
|
|
348
|
+
|
|
349
|
+
describe("validateOperation: components of the target", () => {
|
|
350
|
+
const allowedComponents = {
|
|
351
|
+
target: "products",
|
|
352
|
+
names: new Set(["ProductGrid", "ProductCard"]),
|
|
353
|
+
};
|
|
354
|
+
const notAllowed =
|
|
355
|
+
'"Frame" is not allowed in target "products"; use one of: ProductGrid, ProductCard';
|
|
356
|
+
|
|
357
|
+
it("accepts components of the list in updateComponents and in inline createSurface components", () => {
|
|
358
|
+
const grid = { id: "root", component: "ProductGrid", children: ["c1"] };
|
|
359
|
+
expect(reasonOf(update(grid), { allowedComponents })).toBe("ok");
|
|
360
|
+
expect(
|
|
361
|
+
reasonOf(create({ components: [grid] }), { allowedComponents }),
|
|
362
|
+
).toBe("ok");
|
|
363
|
+
});
|
|
364
|
+
|
|
365
|
+
it("rejects a catalog component that is not in the list, also as a child", () => {
|
|
366
|
+
const frame = { id: "f", component: "Frame" };
|
|
367
|
+
expect(
|
|
368
|
+
reasonOf(update({ id: "root", component: "ProductGrid" }, frame), {
|
|
369
|
+
allowedComponents,
|
|
370
|
+
}),
|
|
371
|
+
).toBe(`component "f": ${notAllowed}`);
|
|
372
|
+
expect(
|
|
373
|
+
reasonOf(create({ components: [{ ...frame, id: "root" }] }), {
|
|
374
|
+
allowedComponents,
|
|
375
|
+
}),
|
|
376
|
+
).toBe(`component "root": ${notAllowed}`);
|
|
377
|
+
});
|
|
378
|
+
|
|
379
|
+
it("names the unknown component first when it is not in the catalog", () => {
|
|
380
|
+
expect(
|
|
381
|
+
reasonOf(update({ id: "x", component: "Nope" }), { allowedComponents }),
|
|
382
|
+
).toBe('component "x": unknown component "Nope"');
|
|
383
|
+
});
|
|
384
|
+
|
|
385
|
+
it("rejects every component when the list is empty", () => {
|
|
386
|
+
expect(
|
|
387
|
+
reasonOf(update({ id: "root", component: "ProductGrid" }), {
|
|
388
|
+
allowedComponents: { target: "products", names: new Set() },
|
|
389
|
+
}),
|
|
390
|
+
).toBe(
|
|
391
|
+
'component "root": "ProductGrid" is not allowed in target "products"; use one of: none',
|
|
392
|
+
);
|
|
393
|
+
});
|
|
394
|
+
});
|
|
395
|
+
|
|
396
|
+
describe("validateOperation: templates", () => {
|
|
397
|
+
const templateReason = (children: unknown) =>
|
|
398
|
+
reasonOf(update({ id: "root", component: "ProductGrid", children }));
|
|
399
|
+
|
|
400
|
+
it("accepts a template with an absolute or a relative path in a multiple slot", () => {
|
|
401
|
+
for (const path of ["/products", "items/0/variants"]) {
|
|
402
|
+
expect(templateReason({ componentId: "card", path })).toBe("ok");
|
|
403
|
+
}
|
|
404
|
+
});
|
|
405
|
+
|
|
406
|
+
it("rejects a template in a single slot", () => {
|
|
407
|
+
expect(
|
|
408
|
+
reasonOf(
|
|
409
|
+
update({
|
|
410
|
+
id: "f",
|
|
411
|
+
component: "Frame",
|
|
412
|
+
child: { componentId: "card", path: "/products" },
|
|
413
|
+
}),
|
|
414
|
+
),
|
|
415
|
+
).toBe(
|
|
416
|
+
'component "f": child: a template { "componentId", "path" } is only allowed in a "multiple" slot',
|
|
417
|
+
);
|
|
418
|
+
});
|
|
419
|
+
|
|
420
|
+
it("rejects template paths with forbidden or empty segments", () => {
|
|
421
|
+
expect(templateReason({ componentId: "card", path: "/__proto__/x" })).toBe(
|
|
422
|
+
'component "root": children: path segment "__proto__" is not allowed in template path',
|
|
423
|
+
);
|
|
424
|
+
for (const path of ["/", "a//b", "items/"]) {
|
|
425
|
+
expect(templateReason({ componentId: "card", path })).toBe(
|
|
426
|
+
'component "root": children: template path must not contain empty segments',
|
|
427
|
+
);
|
|
428
|
+
}
|
|
429
|
+
});
|
|
430
|
+
|
|
431
|
+
it("rejects templates without a component id, with extra keys, or that repeat the node itself", () => {
|
|
432
|
+
expect(templateReason({ componentId: "", path: "/products" })).toMatch(
|
|
433
|
+
/template componentId must be a component id/,
|
|
434
|
+
);
|
|
435
|
+
expect(templateReason({ componentId: "card", path: "" })).toMatch(
|
|
436
|
+
/template path must be a JSON pointer or a relative path/,
|
|
437
|
+
);
|
|
438
|
+
expect(
|
|
439
|
+
templateReason({ componentId: "card", path: "/products", limit: 3 }),
|
|
440
|
+
).toMatch(/takes only "componentId" and "path"/);
|
|
441
|
+
expect(templateReason({ componentId: "root", path: "/products" })).toMatch(
|
|
442
|
+
/its own child/,
|
|
443
|
+
);
|
|
444
|
+
});
|
|
445
|
+
});
|
|
446
|
+
|
|
447
|
+
describe("validateOperation: data props", () => {
|
|
448
|
+
it("accepts the @index call, with and without an offset", () => {
|
|
449
|
+
expect(reasonOf(card({ price: { "@call": "@index" } }))).toBe("ok");
|
|
450
|
+
expect(
|
|
451
|
+
reasonOf(card({ price: { "@call": "@index", args: { offset: 1 } } })),
|
|
452
|
+
).toBe("ok");
|
|
453
|
+
});
|
|
454
|
+
|
|
455
|
+
it("accepts calc and series, the CopilotKit value functions", () => {
|
|
456
|
+
expect(
|
|
457
|
+
reasonOf(
|
|
458
|
+
card({
|
|
459
|
+
name: {
|
|
460
|
+
"@call": "calc",
|
|
461
|
+
args: { expression: "seats * 29", seats: { "@path": "/seats" } },
|
|
462
|
+
},
|
|
463
|
+
}),
|
|
464
|
+
),
|
|
465
|
+
).toBe("ok");
|
|
466
|
+
expect(
|
|
467
|
+
reasonOf(
|
|
468
|
+
card({
|
|
469
|
+
name: { "@call": "series", args: { count: 3, item: { n: "${i}" } } },
|
|
470
|
+
}),
|
|
471
|
+
),
|
|
472
|
+
).toBe("ok");
|
|
473
|
+
});
|
|
474
|
+
|
|
475
|
+
it("accepts a basic function value, also nested and with bound args", () => {
|
|
476
|
+
expect(
|
|
477
|
+
reasonOf(
|
|
478
|
+
card({
|
|
479
|
+
name: {
|
|
480
|
+
"@call": "formatCurrency",
|
|
481
|
+
args: { value: { "@path": "/price" }, currency: "EUR" },
|
|
482
|
+
},
|
|
483
|
+
}),
|
|
484
|
+
),
|
|
485
|
+
).toBe("ok");
|
|
486
|
+
expect(
|
|
487
|
+
reasonOf(
|
|
488
|
+
card({
|
|
489
|
+
name: [
|
|
490
|
+
{ label: { "@call": "formatString", args: { value: "${/n}" } } },
|
|
491
|
+
],
|
|
492
|
+
}),
|
|
493
|
+
),
|
|
494
|
+
).toBe("ok");
|
|
495
|
+
});
|
|
496
|
+
|
|
497
|
+
it("rejects every other function call in a data prop", () => {
|
|
498
|
+
const prefix = 'component "c1": name: ';
|
|
499
|
+
const suffix =
|
|
500
|
+
"; a data prop can call only @index and the built-in functions (";
|
|
501
|
+
const cases: Array<[unknown, string]> = [
|
|
502
|
+
[
|
|
503
|
+
{ "@call": "formatPrice", args: {} },
|
|
504
|
+
'"formatPrice" is not a basic function',
|
|
505
|
+
],
|
|
506
|
+
[
|
|
507
|
+
{ "@call": "@index", args: { offset: "1" } },
|
|
508
|
+
"@index takes only an offset",
|
|
509
|
+
],
|
|
510
|
+
[
|
|
511
|
+
[{ label: { "@call": "formatPrice" } }],
|
|
512
|
+
'"formatPrice" is not a basic function',
|
|
513
|
+
],
|
|
514
|
+
[
|
|
515
|
+
{ call: "addToCart", args: {} },
|
|
516
|
+
'name the function with "@call", not "call"',
|
|
517
|
+
],
|
|
518
|
+
[
|
|
519
|
+
{ "@call": "openUrl", args: { url: "https://x" } },
|
|
520
|
+
'"openUrl" runs only from an event',
|
|
521
|
+
],
|
|
522
|
+
[
|
|
523
|
+
{ "@call": "formatNumber", args: [1] },
|
|
524
|
+
'the args of "formatNumber" must be an object',
|
|
525
|
+
],
|
|
526
|
+
[
|
|
527
|
+
{ nested: { functionCall: { "@call": "addToCart" } } },
|
|
528
|
+
"an event binding is not a value",
|
|
529
|
+
],
|
|
530
|
+
];
|
|
531
|
+
for (const [name, reason] of cases) {
|
|
532
|
+
expect(reasonOf(card({ name }))).toContain(prefix + reason + suffix);
|
|
533
|
+
}
|
|
534
|
+
});
|
|
535
|
+
});
|
|
536
|
+
|
|
537
|
+
describe("validateOperation: typed child references", () => {
|
|
538
|
+
const tabs = (tabs: unknown) => update({ id: "t1", component: "Tabs", tabs });
|
|
539
|
+
|
|
540
|
+
it("accepts component ids at a nested reference", () => {
|
|
541
|
+
expect(
|
|
542
|
+
reasonOf(
|
|
543
|
+
tabs([
|
|
544
|
+
{ title: "A", child: "a" },
|
|
545
|
+
{ title: "B", child: "b" },
|
|
546
|
+
]),
|
|
547
|
+
),
|
|
548
|
+
).toBe("ok");
|
|
549
|
+
// A bound list is checked at render.
|
|
550
|
+
expect(reasonOf(tabs({ "@path": "/tabs" }))).toBe("ok");
|
|
551
|
+
});
|
|
552
|
+
|
|
553
|
+
it("accepts a bound id at an array position of a reference", () => {
|
|
554
|
+
expect(reasonOf(tabs([{ title: "A", child: { "@path": "/panel" } }]))).toBe(
|
|
555
|
+
"ok",
|
|
556
|
+
);
|
|
557
|
+
expect(reasonOf(tabs([{ "@path": "/tab" }]))).toBe("ok");
|
|
558
|
+
});
|
|
559
|
+
|
|
560
|
+
it("rejects a value that is not an id, and the component itself", () => {
|
|
561
|
+
expect(reasonOf(tabs([{ title: "A", child: 1 }]))).toBe(
|
|
562
|
+
'component "t1": tabs.child: must reference component ids (strings)',
|
|
563
|
+
);
|
|
564
|
+
expect(reasonOf(tabs([{ title: "A", child: "t1" }]))).toBe(
|
|
565
|
+
'component "t1": tabs.child: a component cannot be its own child',
|
|
566
|
+
);
|
|
567
|
+
});
|
|
568
|
+
});
|
|
569
|
+
|
|
570
|
+
describe("validateOperation: event bindings", () => {
|
|
571
|
+
const eventReason = (event: unknown) =>
|
|
572
|
+
reasonOf(card({ onAddToCart: { event } }));
|
|
573
|
+
|
|
574
|
+
it("accepts an event with a userMessage string or binding, and a bound context", () => {
|
|
575
|
+
expect(
|
|
576
|
+
eventReason({
|
|
577
|
+
name: "choose",
|
|
578
|
+
userMessage: "I pick the Team plan",
|
|
579
|
+
context: { seats: { "@path": "/seats" }, plan: "team" },
|
|
580
|
+
}),
|
|
581
|
+
).toBe("ok");
|
|
582
|
+
expect(
|
|
583
|
+
eventReason({ name: "choose", userMessage: { "@path": "/message" } }),
|
|
584
|
+
).toBe("ok");
|
|
585
|
+
});
|
|
586
|
+
|
|
587
|
+
it("rejects an event without a name, and a binding that is not an action", () => {
|
|
588
|
+
expect(eventReason({})).toBe(
|
|
589
|
+
'component "c1": onAddToCart: event.name is required',
|
|
590
|
+
);
|
|
591
|
+
expect(reasonOf(card({ onAddToCart: "addToCart" }))).toMatch(
|
|
592
|
+
/action object/,
|
|
593
|
+
);
|
|
594
|
+
});
|
|
595
|
+
|
|
596
|
+
it("rejects a userMessage that is not a string or a binding", () => {
|
|
597
|
+
expect(eventReason({ name: "choose", userMessage: 5 })).toBe(
|
|
598
|
+
'component "c1": onAddToCart: event.userMessage must be a string, {"@path": "/x"} or a function value',
|
|
599
|
+
);
|
|
600
|
+
});
|
|
601
|
+
|
|
602
|
+
it("rejects v0.9 path bindings in userMessage and context with a reason that names @path", () => {
|
|
603
|
+
expect(eventReason({ name: "choose", userMessage: { path: "/m" } })).toBe(
|
|
604
|
+
'component "c1": onAddToCart: event.userMessage: bind data with {"@path": "/x"}, not {"path": "/x"}',
|
|
605
|
+
);
|
|
606
|
+
expect(
|
|
607
|
+
eventReason({ name: "choose", context: { seats: { path: "/seats" } } }),
|
|
608
|
+
).toBe(
|
|
609
|
+
'component "c1": onAddToCart: event.context.seats: bind data with {"@path": "/x"}, not {"path": "/x"}',
|
|
610
|
+
);
|
|
611
|
+
});
|
|
612
|
+
|
|
613
|
+
it("accepts a function value as the userMessage", () => {
|
|
614
|
+
expect(
|
|
615
|
+
eventReason({
|
|
616
|
+
name: "choose",
|
|
617
|
+
userMessage: { "@call": "formatString", args: { value: "Pick ${/n}" } },
|
|
618
|
+
}),
|
|
619
|
+
).toBe("ok");
|
|
620
|
+
expect(eventReason({ name: "choose", userMessage: 1 })).toBe(
|
|
621
|
+
'component "c1": onAddToCart: event.userMessage must be a string, {"@path": "/x"} or a function value',
|
|
622
|
+
);
|
|
623
|
+
});
|
|
624
|
+
|
|
625
|
+
it("accepts @index and a basic function in the event context, but not an app function", () => {
|
|
626
|
+
expect(
|
|
627
|
+
eventReason({ name: "choose", context: { row: { "@call": "@index" } } }),
|
|
628
|
+
).toBe("ok");
|
|
629
|
+
expect(
|
|
630
|
+
eventReason({
|
|
631
|
+
name: "choose",
|
|
632
|
+
context: {
|
|
633
|
+
label: { "@call": "formatString", args: { value: "${/name}" } },
|
|
634
|
+
},
|
|
635
|
+
}),
|
|
636
|
+
).toBe("ok");
|
|
637
|
+
expect(
|
|
638
|
+
eventReason({
|
|
639
|
+
name: "choose",
|
|
640
|
+
context: { row: { "@call": "addToCart" } },
|
|
641
|
+
}),
|
|
642
|
+
).toContain(
|
|
643
|
+
'component "c1": onAddToCart: event.context.row: "addToCart" is not a basic function',
|
|
644
|
+
);
|
|
645
|
+
});
|
|
646
|
+
});
|
|
647
|
+
|
|
648
|
+
describe("validateOperation: function-call bindings", () => {
|
|
649
|
+
const callReason = (
|
|
650
|
+
functionCall: unknown,
|
|
651
|
+
options?: Partial<ValidateOperationOptions>,
|
|
652
|
+
) => reasonOf(card({ onAddToCart: { functionCall } }), options);
|
|
653
|
+
|
|
654
|
+
it("checks function names only when the function list is provided", () => {
|
|
655
|
+
const call = { "@call": "removeFromCart" };
|
|
656
|
+
expect(callReason(call, { functions })).toBe(
|
|
657
|
+
'component "c1": onAddToCart: function "removeFromCart" is not exposed to generative UI',
|
|
658
|
+
);
|
|
659
|
+
expect(callReason(call)).toBe("ok");
|
|
660
|
+
});
|
|
661
|
+
|
|
662
|
+
it("accepts a basic function in an event binding without a declaration", () => {
|
|
663
|
+
expect(
|
|
664
|
+
callReason(
|
|
665
|
+
{ "@call": "openUrl", args: { url: "https://example.com" } },
|
|
666
|
+
{ functions },
|
|
667
|
+
),
|
|
668
|
+
).toBe("ok");
|
|
669
|
+
});
|
|
670
|
+
|
|
671
|
+
it("does not expose inherited Object.prototype keys as functions", () => {
|
|
672
|
+
expect(callReason({ "@call": "toString" }, { functions })).toMatch(
|
|
673
|
+
/function "toString" is not exposed/,
|
|
674
|
+
);
|
|
675
|
+
});
|
|
676
|
+
|
|
677
|
+
it("names @call when the model writes the v0.9 call key", () => {
|
|
678
|
+
expect(callReason({ call: "addToCart" })).toBe(
|
|
679
|
+
'component "c1": onAddToCart: functionCall names the function with "@call", not "call"',
|
|
680
|
+
);
|
|
681
|
+
});
|
|
682
|
+
|
|
683
|
+
it("rejects args that are not an object, or that hold a v0.9 path binding", () => {
|
|
684
|
+
expect(callReason({ "@call": "addToCart", args: [1] })).toBe(
|
|
685
|
+
'component "c1": onAddToCart: functionCall.args must be an object',
|
|
686
|
+
);
|
|
687
|
+
expect(
|
|
688
|
+
callReason({ "@call": "addToCart", args: { id: { path: "/id" } } }),
|
|
689
|
+
).toBe(
|
|
690
|
+
'component "c1": onAddToCart: functionCall.args.id: bind data with {"@path": "/x"}, not {"path": "/x"}',
|
|
691
|
+
);
|
|
692
|
+
});
|
|
693
|
+
});
|
|
694
|
+
|
|
695
|
+
describe("validateOperation: updateDataModel", () => {
|
|
696
|
+
it("requires a value and names null for a delete", () => {
|
|
697
|
+
expect(reasonOf(setData({ path: "/a" }))).toBe(
|
|
698
|
+
'updateDataModel.value is required; use "value":null to delete',
|
|
699
|
+
);
|
|
700
|
+
});
|
|
701
|
+
|
|
702
|
+
it("accepts null to delete the key at a path", () => {
|
|
703
|
+
expect(reasonOf(setData({ path: "/cart/item", value: null }))).toBe("ok");
|
|
704
|
+
});
|
|
705
|
+
|
|
706
|
+
it("accepts writes under /app like any other data-model path", () => {
|
|
707
|
+
expect(reasonOf(setData({ path: "/app/cart", value: 1 }))).toBe("ok");
|
|
708
|
+
expect(reasonOf(setData({ path: "/", value: { app: {} } }))).toBe("ok");
|
|
709
|
+
});
|
|
710
|
+
|
|
711
|
+
it("rejects prototype-polluting path segments", () => {
|
|
712
|
+
for (const path of [
|
|
713
|
+
"/__proto__/x",
|
|
714
|
+
"/a/constructor/b",
|
|
715
|
+
"/prototype",
|
|
716
|
+
"/a/~0/__proto__",
|
|
717
|
+
]) {
|
|
718
|
+
expect(reasonOf(setData({ path, value: 1 }))).toMatch(/not allowed/);
|
|
719
|
+
}
|
|
720
|
+
});
|
|
721
|
+
|
|
722
|
+
it("rejects empty path segments", () => {
|
|
723
|
+
for (const path of ["//a/b", "/a//b", "/a/"]) {
|
|
724
|
+
expect(reasonOf(setData({ path, value: 1 }))).toMatch(/empty segments/);
|
|
725
|
+
}
|
|
726
|
+
});
|
|
727
|
+
|
|
728
|
+
it("rejects root writes whose value is not a JSON object", () => {
|
|
729
|
+
for (const value of [5, [1], "s", null]) {
|
|
730
|
+
expect(reasonOf(setData({ path: "/", value }))).toBe(
|
|
731
|
+
"a root updateDataModel value must be a JSON object",
|
|
732
|
+
);
|
|
733
|
+
expect(reasonOf(setData({ value }))).toBe(
|
|
734
|
+
"a root updateDataModel value must be a JSON object",
|
|
735
|
+
);
|
|
736
|
+
}
|
|
737
|
+
expect(reasonOf(setData({ value: { items: [] } }))).toBe("ok");
|
|
738
|
+
});
|
|
739
|
+
});
|
|
740
|
+
|
|
741
|
+
describe("validateOperation: callRendererFunction while the agent path is off", () => {
|
|
742
|
+
it.skipIf(GENERATIVE_UI_AGENT_FUNCTION_CALLS)(
|
|
743
|
+
"rejects every callRendererFunction with the reason, whatever it names",
|
|
744
|
+
() => {
|
|
745
|
+
for (const op of [
|
|
746
|
+
callFunction({
|
|
747
|
+
callFunction: { "@call": "addToCart", args: { productId: "p1" } },
|
|
748
|
+
}),
|
|
749
|
+
callFunction({ callFunction: { "@call": "formatNumber" } }),
|
|
750
|
+
callFunction({ callFunction: {} }),
|
|
751
|
+
]) {
|
|
752
|
+
expect(reasonOf(op, { functions, knownSurfaces: new Set() })).toBe(
|
|
753
|
+
GENERATIVE_UI_AGENT_FUNCTION_CALLS_OFF_REASON,
|
|
754
|
+
);
|
|
755
|
+
}
|
|
756
|
+
},
|
|
757
|
+
);
|
|
758
|
+
});
|
|
759
|
+
|
|
760
|
+
// The agent path is off for now (GENERATIVE_UI_AGENT_FUNCTION_CALLS); these
|
|
761
|
+
// describe it for when it is on.
|
|
762
|
+
describe.skipIf(!GENERATIVE_UI_AGENT_FUNCTION_CALLS)(
|
|
763
|
+
"validateOperation: callRendererFunction",
|
|
764
|
+
() => {
|
|
765
|
+
it("accepts a call of a declared function, with no surface", () => {
|
|
766
|
+
expect(
|
|
767
|
+
reasonOf(
|
|
768
|
+
callFunction({
|
|
769
|
+
callFunction: { "@call": "addToCart", args: { productId: "p1" } },
|
|
770
|
+
}),
|
|
771
|
+
{ functions, knownSurfaces: new Set() },
|
|
772
|
+
),
|
|
773
|
+
).toBe("ok");
|
|
774
|
+
});
|
|
775
|
+
|
|
776
|
+
it("requires a non-empty functionCallId", () => {
|
|
777
|
+
for (const functionCallId of [undefined, "", 7]) {
|
|
778
|
+
expect(
|
|
779
|
+
reasonOf(
|
|
780
|
+
callFunction({
|
|
781
|
+
functionCallId,
|
|
782
|
+
callFunction: { "@call": "addToCart" },
|
|
783
|
+
}),
|
|
784
|
+
),
|
|
785
|
+
).toBe(
|
|
786
|
+
"callRendererFunction.functionCallId must be a non-empty string",
|
|
787
|
+
);
|
|
788
|
+
}
|
|
789
|
+
});
|
|
790
|
+
|
|
791
|
+
it("leaves the openUrl restriction to the dispatcher when the app functions are not known", () => {
|
|
792
|
+
expect(
|
|
793
|
+
reasonOf(
|
|
794
|
+
callFunction({
|
|
795
|
+
callFunction: { "@call": "openUrl", args: { url: "https://x" } },
|
|
796
|
+
}),
|
|
797
|
+
),
|
|
798
|
+
).toBe("ok");
|
|
799
|
+
});
|
|
800
|
+
|
|
801
|
+
it("lets the agent call an openUrl that the app declared itself", () => {
|
|
802
|
+
expect(
|
|
803
|
+
reasonOf(
|
|
804
|
+
callFunction({
|
|
805
|
+
callFunction: { "@call": "openUrl", args: { url: "https://x" } },
|
|
806
|
+
}),
|
|
807
|
+
{
|
|
808
|
+
functions: {
|
|
809
|
+
openUrl: {
|
|
810
|
+
parameters: { type: "object" },
|
|
811
|
+
allowedCallers: "rendererOrAgent",
|
|
812
|
+
},
|
|
813
|
+
},
|
|
814
|
+
},
|
|
815
|
+
),
|
|
816
|
+
).toBe("ok");
|
|
817
|
+
});
|
|
818
|
+
|
|
819
|
+
it("refuses a user-gesture function and every other built-in from the agent: they are value functions of the UI", () => {
|
|
820
|
+
expect(
|
|
821
|
+
reasonOf(
|
|
822
|
+
callFunction({
|
|
823
|
+
callFunction: { "@call": "openUrl", args: { url: "https://x" } },
|
|
824
|
+
}),
|
|
825
|
+
{ functions },
|
|
826
|
+
),
|
|
827
|
+
).toBe(
|
|
828
|
+
'function "openUrl" runs only from a user gesture; the agent cannot call it',
|
|
829
|
+
);
|
|
830
|
+
expect(
|
|
831
|
+
reasonOf(
|
|
832
|
+
callFunction({
|
|
833
|
+
callFunction: { "@call": "formatNumber", args: { value: 1 } },
|
|
834
|
+
}),
|
|
835
|
+
{ functions },
|
|
836
|
+
),
|
|
837
|
+
).toBe(
|
|
838
|
+
'function "formatNumber" runs only in the UI (allowedCallers "rendererOnly"); the agent cannot call it',
|
|
839
|
+
);
|
|
840
|
+
});
|
|
841
|
+
|
|
842
|
+
it("applies the allowedCallers of an app function, to the agent and to event bindings", () => {
|
|
843
|
+
const permissions = {
|
|
844
|
+
...functions,
|
|
845
|
+
track: {
|
|
846
|
+
parameters: { type: "object" },
|
|
847
|
+
allowedCallers: "rendererOnly" as const,
|
|
848
|
+
},
|
|
849
|
+
reload: {
|
|
850
|
+
parameters: { type: "object" },
|
|
851
|
+
allowedCallers: "agentOnly" as const,
|
|
852
|
+
},
|
|
853
|
+
share: {
|
|
854
|
+
parameters: { type: "object" },
|
|
855
|
+
requiresUserActivation: true,
|
|
856
|
+
},
|
|
857
|
+
};
|
|
858
|
+
const agentCall = (name: string) =>
|
|
859
|
+
reasonOf(callFunction({ callFunction: { "@call": name } }), {
|
|
860
|
+
functions: permissions,
|
|
861
|
+
});
|
|
862
|
+
if (GENERATIVE_UI_AGENT_FUNCTION_CALLS) {
|
|
863
|
+
expect(agentCall("addToCart")).toBe("ok");
|
|
864
|
+
expect(agentCall("reload")).toBe("ok");
|
|
865
|
+
expect(agentCall("track")).toBe(
|
|
866
|
+
'function "track" runs only in the UI (allowedCallers "rendererOnly"); the agent cannot call it',
|
|
867
|
+
);
|
|
868
|
+
expect(agentCall("share")).toBe(
|
|
869
|
+
'function "share" runs only from a user gesture; the agent cannot call it',
|
|
870
|
+
);
|
|
871
|
+
}
|
|
872
|
+
const binding = (name: string) =>
|
|
873
|
+
reasonOf(card({ onAddToCart: { functionCall: { "@call": name } } }), {
|
|
874
|
+
functions: permissions,
|
|
875
|
+
});
|
|
876
|
+
expect(binding("track")).toBe("ok");
|
|
877
|
+
expect(binding("share")).toBe("ok");
|
|
878
|
+
expect(binding("reload")).toBe(
|
|
879
|
+
'component "c1": onAddToCart: function "reload" runs only for the agent (allowedCallers "agentOnly"); the UI cannot call it',
|
|
880
|
+
);
|
|
881
|
+
});
|
|
882
|
+
|
|
883
|
+
it("requires a function name in @call", () => {
|
|
884
|
+
expect(reasonOf(callFunction({ callFunction: {} }))).toBe(
|
|
885
|
+
'callRendererFunction.callFunction["@call"] must be a function name',
|
|
886
|
+
);
|
|
887
|
+
expect(reasonOf(callFunction({}))).toBe(
|
|
888
|
+
'callRendererFunction.callFunction must be { "@call": "<function>", "args": { ... } }',
|
|
889
|
+
);
|
|
890
|
+
});
|
|
891
|
+
|
|
892
|
+
it("rejects an undeclared function when the function list is provided", () => {
|
|
893
|
+
const op = callFunction({ callFunction: { "@call": "removeFromCart" } });
|
|
894
|
+
expect(reasonOf(op, { functions })).toBe(
|
|
895
|
+
'function "removeFromCart" is not exposed to generative UI',
|
|
896
|
+
);
|
|
897
|
+
expect(reasonOf(op)).toBe("ok");
|
|
898
|
+
});
|
|
899
|
+
|
|
900
|
+
it("rejects args that are not an object and a foreign catalog", () => {
|
|
901
|
+
expect(
|
|
902
|
+
reasonOf(
|
|
903
|
+
callFunction({ callFunction: { "@call": "addToCart", args: 1 } }),
|
|
904
|
+
),
|
|
905
|
+
).toBe("callRendererFunction.callFunction.args must be an object");
|
|
906
|
+
expect(
|
|
907
|
+
reasonOf(
|
|
908
|
+
callFunction({
|
|
909
|
+
callFunction: { "@call": "addToCart", catalogId: "other" },
|
|
910
|
+
}),
|
|
911
|
+
),
|
|
912
|
+
).toBe(
|
|
913
|
+
'callRendererFunction.callFunction: unknown catalog "other"; leave out catalogId or use "shop"',
|
|
914
|
+
);
|
|
915
|
+
});
|
|
916
|
+
|
|
917
|
+
it("has a kind but no surface id", () => {
|
|
918
|
+
const op = callFunction({ callFunction: { "@call": "addToCart" } });
|
|
919
|
+
expect(getOperationKind(op)).toBe("callRendererFunction");
|
|
920
|
+
expect(getOperationSurfaceId(op)).toBeUndefined();
|
|
921
|
+
});
|
|
922
|
+
},
|
|
923
|
+
);
|
|
924
|
+
|
|
925
|
+
describe("validateOperation: Object.prototype keys", () => {
|
|
926
|
+
it("rejects component types that only exist on Object.prototype without throwing", () => {
|
|
927
|
+
expect(() =>
|
|
928
|
+
reasonOf(update({ id: "x", component: "constructor" })),
|
|
929
|
+
).not.toThrow();
|
|
930
|
+
expect(reasonOf(update({ id: "x", component: "constructor" }))).toMatch(
|
|
931
|
+
/unknown component "constructor"/,
|
|
932
|
+
);
|
|
933
|
+
expect(reasonOf(update({ id: "x", component: "__proto__" }))).toMatch(
|
|
934
|
+
/unknown component "__proto__"/,
|
|
935
|
+
);
|
|
936
|
+
});
|
|
937
|
+
|
|
938
|
+
it("does not treat an inherited Object.prototype key as an event prop", () => {
|
|
939
|
+
expect(reasonOf(card({ toString: "x" }))).toMatch(
|
|
940
|
+
/unknown prop "toString"/,
|
|
941
|
+
);
|
|
942
|
+
});
|
|
943
|
+
|
|
944
|
+
it("does not treat an inherited Object.prototype key as a slot", () => {
|
|
945
|
+
expect(
|
|
946
|
+
reasonOf(
|
|
947
|
+
update({ id: "root", component: "ProductGrid", constructor: ["c1"] }),
|
|
948
|
+
),
|
|
949
|
+
).toMatch(/unknown prop "constructor"/);
|
|
950
|
+
});
|
|
951
|
+
});
|
|
952
|
+
|
|
953
|
+
describe("isPathBinding", () => {
|
|
954
|
+
it("accepts only an object with a single @path string", () => {
|
|
955
|
+
expect(isPathBinding({ "@path": "/a" })).toBe(true);
|
|
956
|
+
expect(isPathBinding({ path: "/a" })).toBe(false);
|
|
957
|
+
expect(isPathBinding({ "@path": "/a", label: "x" })).toBe(false);
|
|
958
|
+
expect(isPathBinding({ "@path": 1 })).toBe(false);
|
|
959
|
+
});
|
|
960
|
+
});
|
|
961
|
+
|
|
962
|
+
describe("isIndexCall", () => {
|
|
963
|
+
it("accepts @index with no args or a numeric offset", () => {
|
|
964
|
+
expect(isIndexCall({ "@call": "@index" })).toBe(true);
|
|
965
|
+
expect(isIndexCall({ "@call": "@index", args: { offset: 1 } })).toBe(true);
|
|
966
|
+
expect(isIndexCall({ "@call": "@index", args: {} })).toBe(true);
|
|
967
|
+
});
|
|
968
|
+
|
|
969
|
+
it("rejects other calls, a non-numeric offset, and extra keys", () => {
|
|
970
|
+
expect(isIndexCall({ "@call": "formatPrice" })).toBe(false);
|
|
971
|
+
expect(isIndexCall({ "@call": "@index", args: { offset: "1" } })).toBe(
|
|
972
|
+
false,
|
|
973
|
+
);
|
|
974
|
+
expect(isIndexCall({ "@call": "@index", args: { step: 2 } })).toBe(false);
|
|
975
|
+
expect(isIndexCall({ "@call": "@index", extra: true })).toBe(false);
|
|
976
|
+
});
|
|
977
|
+
});
|