@copilotkit/react-native 2.0.0-rc.4 → 2.0.0-rc.40
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/README.md +334 -2
- package/USAGE.md +382 -21
- package/dist/CopilotKitProvider-Cbfrm_7u.d.mts +65 -0
- package/dist/{CopilotKitProvider-Byw3cqe2.cjs → CopilotKitProvider-CvJYytjn.cjs} +56 -42
- package/dist/CopilotKitProvider-CvJYytjn.cjs.map +1 -0
- package/dist/CopilotKitProvider-Dv3ISMKf.d.cts +65 -0
- package/dist/{CopilotKitProvider-CE4bc6CP.mjs → CopilotKitProvider-oTdmylSY.mjs} +55 -42
- package/dist/CopilotKitProvider-oTdmylSY.mjs.map +1 -0
- package/dist/{Markdown-C_9Re36O.cjs → Markdown-DLopAh5W.cjs} +10 -10
- package/dist/Markdown-DLopAh5W.cjs.map +1 -0
- package/dist/{Markdown-JsYh7oSA.d.mts → Markdown-Du-b_Qpr.d.mts} +3 -3
- package/dist/{Markdown-1ips7myr.d.cts → Markdown-UXZdDjzs.d.cts} +3 -3
- package/dist/{Markdown-C6AsLHmd.mjs → Markdown-VxtPJ_yh.mjs} +4 -4
- package/dist/Markdown-VxtPJ_yh.mjs.map +1 -0
- package/dist/{MarkdownRendererContext-CAnuohIS.mjs → MarkdownRendererContext-EobUsERg.mjs} +1 -1
- package/dist/{MarkdownRendererContext-CAnuohIS.mjs.map → MarkdownRendererContext-EobUsERg.mjs.map} +1 -1
- package/dist/MarkdownRendererContext-cDEHvbw9.cjs +40 -0
- package/dist/{MarkdownRendererContext-BRNe808d.cjs.map → MarkdownRendererContext-cDEHvbw9.cjs.map} +1 -1
- package/dist/chunk-C0xms8kb.cjs +34 -0
- package/dist/components/index.cjs +78 -175
- package/dist/components/index.cjs.map +1 -1
- package/dist/components/index.d.cts +2 -2
- package/dist/components/index.d.mts +2 -2
- package/dist/components/index.mjs +76 -174
- package/dist/components/index.mjs.map +1 -1
- package/dist/generative-ui/index.cjs +612 -0
- package/dist/generative-ui/index.cjs.map +1 -0
- package/dist/generative-ui/index.d.cts +341 -0
- package/dist/generative-ui/index.d.mts +341 -0
- package/dist/generative-ui/index.mjs +549 -0
- package/dist/generative-ui/index.mjs.map +1 -0
- package/dist/headless.cjs +2 -2
- package/dist/headless.d.cts +2 -2
- package/dist/headless.d.mts +2 -2
- package/dist/headless.mjs +1 -1
- package/dist/index.cjs +131 -162
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +36 -18
- package/dist/index.d.mts +36 -18
- package/dist/index.mjs +130 -161
- package/dist/index.mjs.map +1 -1
- package/dist/polyfills/encoding.cjs +115 -5
- package/dist/polyfills/encoding.cjs.map +1 -1
- package/dist/polyfills/encoding.d.cts +31 -1
- package/dist/polyfills/encoding.d.mts +31 -1
- package/dist/polyfills/encoding.mjs +113 -6
- package/dist/polyfills/encoding.mjs.map +1 -1
- package/dist/polyfills/streams.cjs +1 -1
- package/dist/polyfills.cjs +218 -2
- package/dist/polyfills.cjs.map +1 -1
- package/dist/polyfills.mjs +217 -1
- package/dist/polyfills.mjs.map +1 -1
- package/package.json +51 -21
- package/project.json +6 -1
- package/scripts/__tests__/verify-polyfill-barrel.test.mjs +150 -0
- package/scripts/verify-polyfill-barrel.mjs +158 -11
- package/src/CopilotChat.tsx +114 -93
- package/src/CopilotKitProvider.tsx +87 -130
- package/src/CopilotPopup.tsx +2 -2
- package/src/CopilotSidebar.tsx +7 -5
- package/src/__mocks__/react-native.ts +31 -0
- package/src/__mocks__/test-copilotkit.tsx +9 -3
- package/src/__tests__/attachments-integration.test.tsx +15 -2
- package/src/__tests__/copilot-chat-agentid.test.tsx +15 -2
- package/src/__tests__/copilot-chat-attachments.test.tsx +15 -2
- package/src/__tests__/copilot-chat-configuration.test.tsx +82 -0
- package/src/__tests__/copilot-chat-threads.test.tsx +232 -0
- package/src/__tests__/copilot-popup.test.tsx +15 -2
- package/src/__tests__/copilot-sidebar.test.tsx +15 -2
- package/src/__tests__/copilotkit-provider.test.tsx +68 -73
- package/src/__tests__/generative-ui-harness.tsx +243 -0
- package/src/__tests__/generative-ui-provider-props.test-d.tsx +106 -0
- package/src/__tests__/generative-ui-rn-mock.tsx +54 -0
- package/src/__tests__/generative-ui-wiring.test.tsx +720 -0
- package/src/__tests__/generative-ui.test.tsx +1000 -0
- package/src/__tests__/headless-entry-surface.test.ts +979 -60
- package/src/__tests__/headless-integration.test.tsx +11 -1
- package/src/__tests__/use-attachments.test.ts +27 -2
- package/src/components/CopilotChat.tsx +106 -233
- package/src/components/CopilotModal.tsx +1 -1
- package/src/components/Markdown.tsx +4 -4
- package/src/components/__tests__/CopilotChat-edge-cases.test.tsx +5 -1
- package/src/components/__tests__/CopilotChat.test.tsx +12 -4
- package/src/components/__tests__/CopilotChatInterrupt.test.tsx +208 -0
- package/src/components/__tests__/Markdown.code-alias.test.tsx +1 -1
- package/src/components/index.ts +6 -0
- package/src/components/messages/AssistantMessage.tsx +3 -3
- package/src/components/messages/TypingIndicator.tsx +4 -4
- package/src/generative-ui/GenerativeUIActivity.tsx +235 -0
- package/src/generative-ui/GenerativeUIProvider.tsx +171 -0
- package/src/generative-ui/GenerativeUISurface.tsx +303 -0
- package/src/generative-ui/host.tsx +138 -0
- package/src/generative-ui/index.ts +93 -0
- package/src/generative-ui/internal.ts +55 -0
- package/src/hooks/use-attachments.ts +95 -139
- package/src/index.ts +8 -3
- package/src/polyfills/__tests__/encoding-utf8.test.ts +112 -0
- package/src/polyfills/encoding.ts +160 -4
- package/src/types.d.ts +0 -13
- package/tsdown.config.ts +1 -0
- package/dist/CopilotKitProvider-BWjzCdHN.d.mts +0 -121
- package/dist/CopilotKitProvider-Btm10msB.d.cts +0 -121
- package/dist/CopilotKitProvider-Byw3cqe2.cjs.map +0 -1
- package/dist/CopilotKitProvider-CE4bc6CP.mjs.map +0 -1
- package/dist/Markdown-C6AsLHmd.mjs.map +0 -1
- package/dist/Markdown-C_9Re36O.cjs.map +0 -1
- package/dist/MarkdownRendererContext-BRNe808d.cjs +0 -72
- package/dist/streaming-fetch-Bm91EsRc.cjs +0 -225
- package/dist/streaming-fetch-Bm91EsRc.cjs.map +0 -1
- package/dist/streaming-fetch-Dm1oN4f2.mjs +0 -219
- package/dist/streaming-fetch-Dm1oN4f2.mjs.map +0 -1
|
@@ -0,0 +1,1000 @@
|
|
|
1
|
+
import React from "react";
|
|
2
|
+
import type { ReactNode } from "react";
|
|
3
|
+
import { act, fireEvent, render, screen } from "@testing-library/react";
|
|
4
|
+
import { afterEach, describe, expect, it, vi } from "vitest";
|
|
5
|
+
import type { AbstractAgent } from "@ag-ui/client";
|
|
6
|
+
import type {
|
|
7
|
+
A2UIComponentNode,
|
|
8
|
+
A2UIOperation,
|
|
9
|
+
GenerativeUIRejectedOperation,
|
|
10
|
+
GenerativeUISurfaceContent,
|
|
11
|
+
} from "@copilotkit/shared";
|
|
12
|
+
|
|
13
|
+
// React Native host components as DOM elements (see the mock module).
|
|
14
|
+
vi.mock("react-native", async () =>
|
|
15
|
+
(await import("./generative-ui-rn-mock")).createReactNativeMock(),
|
|
16
|
+
);
|
|
17
|
+
|
|
18
|
+
// Imported after the mock is declared (vi.mock is hoisted anyway).
|
|
19
|
+
import {
|
|
20
|
+
GenerativeUIActivity,
|
|
21
|
+
GenerativeUISurface,
|
|
22
|
+
isGenerativeUIActivity,
|
|
23
|
+
nativeGenerativeUIHost,
|
|
24
|
+
useGenerativeUIRuntime,
|
|
25
|
+
} from "../generative-ui";
|
|
26
|
+
import type {
|
|
27
|
+
GenerativeUIActivityProps,
|
|
28
|
+
GenerativeUIActivityWrapperProps,
|
|
29
|
+
GenerativeUIProviderProps,
|
|
30
|
+
GenerativeUISurfaceRootProps,
|
|
31
|
+
} from "../generative-ui";
|
|
32
|
+
import {
|
|
33
|
+
Harness,
|
|
34
|
+
S,
|
|
35
|
+
activity,
|
|
36
|
+
create,
|
|
37
|
+
flaky,
|
|
38
|
+
nodes,
|
|
39
|
+
sparse,
|
|
40
|
+
ui,
|
|
41
|
+
warningsWith,
|
|
42
|
+
} from "./generative-ui-harness";
|
|
43
|
+
|
|
44
|
+
afterEach(() => vi.restoreAllMocks());
|
|
45
|
+
|
|
46
|
+
describe("GenerativeUIProvider", () => {
|
|
47
|
+
it("gives the components below a generative UI runtime for the catalog", () => {
|
|
48
|
+
let seen: unknown;
|
|
49
|
+
function Probe() {
|
|
50
|
+
seen = useGenerativeUIRuntime()?.ui;
|
|
51
|
+
return null;
|
|
52
|
+
}
|
|
53
|
+
const agentRef = {
|
|
54
|
+
current: null,
|
|
55
|
+
} as React.MutableRefObject<AbstractAgent | null>;
|
|
56
|
+
render(<Harness agentRef={agentRef}>{() => <Probe />}</Harness>);
|
|
57
|
+
expect(seen).toBe(ui);
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
it("drops a theme from untyped callers before it reaches the runtime, and warns in development", () => {
|
|
61
|
+
const warn = vi.spyOn(console, "warn").mockImplementation(() => {});
|
|
62
|
+
const themes: unknown[] = [];
|
|
63
|
+
let runtimeTheme: unknown = "unset";
|
|
64
|
+
function Probe() {
|
|
65
|
+
runtimeTheme = useGenerativeUIRuntime()?.theme;
|
|
66
|
+
return null;
|
|
67
|
+
}
|
|
68
|
+
function SurfaceRoot({ theme, children }: GenerativeUISurfaceRootProps) {
|
|
69
|
+
themes.push(theme);
|
|
70
|
+
return <>{children}</>;
|
|
71
|
+
}
|
|
72
|
+
const agentRef = {
|
|
73
|
+
current: null,
|
|
74
|
+
} as React.MutableRefObject<AbstractAgent | null>;
|
|
75
|
+
// A JavaScript caller, or a cast, gets past the type.
|
|
76
|
+
const untyped = {
|
|
77
|
+
theme: { primary: "#ff0000" },
|
|
78
|
+
host: { ...nativeGenerativeUIHost, SurfaceRoot },
|
|
79
|
+
} as unknown as Partial<GenerativeUIProviderProps>;
|
|
80
|
+
render(
|
|
81
|
+
<Harness agentRef={agentRef} providerProps={untyped}>
|
|
82
|
+
{(agent) => (
|
|
83
|
+
<>
|
|
84
|
+
<Probe />
|
|
85
|
+
<GenerativeUIActivity
|
|
86
|
+
message={activity(
|
|
87
|
+
[
|
|
88
|
+
create,
|
|
89
|
+
nodes({
|
|
90
|
+
id: "root",
|
|
91
|
+
component: "Balance",
|
|
92
|
+
label: "T",
|
|
93
|
+
amount: 1,
|
|
94
|
+
}),
|
|
95
|
+
],
|
|
96
|
+
"ready",
|
|
97
|
+
)}
|
|
98
|
+
agent={agent}
|
|
99
|
+
/>
|
|
100
|
+
</>
|
|
101
|
+
)}
|
|
102
|
+
</Harness>,
|
|
103
|
+
);
|
|
104
|
+
expect(screen.getByTestId("balance-T")).toBeTruthy();
|
|
105
|
+
expect(runtimeTheme).toBeUndefined();
|
|
106
|
+
expect(themes.length).toBeGreaterThan(0);
|
|
107
|
+
expect(themes.every((t) => t === undefined)).toBe(true);
|
|
108
|
+
expect(
|
|
109
|
+
warn.mock.calls.filter((c) => String(c[0]).includes("theme")),
|
|
110
|
+
).toHaveLength(1);
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
it("runs the tool transport even when an untyped caller asks for markdown, and warns in development", () => {
|
|
114
|
+
const warn = vi.spyOn(console, "warn").mockImplementation(() => {});
|
|
115
|
+
let transport: unknown;
|
|
116
|
+
function Probe() {
|
|
117
|
+
transport = useGenerativeUIRuntime()?.transport;
|
|
118
|
+
return null;
|
|
119
|
+
}
|
|
120
|
+
const agentRef = {
|
|
121
|
+
current: null,
|
|
122
|
+
} as React.MutableRefObject<AbstractAgent | null>;
|
|
123
|
+
render(
|
|
124
|
+
<Harness
|
|
125
|
+
agentRef={agentRef}
|
|
126
|
+
providerProps={
|
|
127
|
+
{
|
|
128
|
+
transport: "markdown",
|
|
129
|
+
} as unknown as Partial<GenerativeUIProviderProps>
|
|
130
|
+
}
|
|
131
|
+
>
|
|
132
|
+
{() => <Probe />}
|
|
133
|
+
</Harness>,
|
|
134
|
+
);
|
|
135
|
+
expect(transport).toBe("tool");
|
|
136
|
+
expect(
|
|
137
|
+
warn.mock.calls.filter((c) => String(c[0]).includes('"markdown"')),
|
|
138
|
+
).toHaveLength(1);
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
it("warns in development when it has no ui", () => {
|
|
142
|
+
const warn = vi.spyOn(console, "warn").mockImplementation(() => {});
|
|
143
|
+
const agentRef = {
|
|
144
|
+
current: null,
|
|
145
|
+
} as React.MutableRefObject<AbstractAgent | null>;
|
|
146
|
+
const view = () => (
|
|
147
|
+
<Harness
|
|
148
|
+
agentRef={agentRef}
|
|
149
|
+
providerProps={
|
|
150
|
+
{ ui: undefined } as unknown as Partial<GenerativeUIProviderProps>
|
|
151
|
+
}
|
|
152
|
+
>
|
|
153
|
+
{() => null}
|
|
154
|
+
</Harness>
|
|
155
|
+
);
|
|
156
|
+
const { rerender } = render(view());
|
|
157
|
+
rerender(view());
|
|
158
|
+
expect(
|
|
159
|
+
warn.mock.calls.filter((c) =>
|
|
160
|
+
String(c[0]).includes("<GenerativeUIProvider> has no `ui`"),
|
|
161
|
+
),
|
|
162
|
+
).toHaveLength(1);
|
|
163
|
+
});
|
|
164
|
+
|
|
165
|
+
it("uses a host given to it instead of the React Native host", () => {
|
|
166
|
+
vi.spyOn(console, "error").mockImplementation(() => {});
|
|
167
|
+
vi.spyOn(console, "warn").mockImplementation(() => {});
|
|
168
|
+
const agentRef = {
|
|
169
|
+
current: null,
|
|
170
|
+
} as React.MutableRefObject<AbstractAgent | null>;
|
|
171
|
+
const host = {
|
|
172
|
+
...nativeGenerativeUIHost,
|
|
173
|
+
NodeFallback: ({ componentId }: { componentId: string }) => (
|
|
174
|
+
<div data-testid={`custom-fallback-${componentId}`} />
|
|
175
|
+
),
|
|
176
|
+
};
|
|
177
|
+
render(
|
|
178
|
+
<Harness agentRef={agentRef} providerProps={{ host }}>
|
|
179
|
+
{(agent) => (
|
|
180
|
+
<GenerativeUIActivity
|
|
181
|
+
message={activity(
|
|
182
|
+
[create, nodes({ id: "root", component: "Boom" })],
|
|
183
|
+
"ready",
|
|
184
|
+
)}
|
|
185
|
+
agent={agent}
|
|
186
|
+
/>
|
|
187
|
+
)}
|
|
188
|
+
</Harness>,
|
|
189
|
+
);
|
|
190
|
+
expect(screen.getByTestId("custom-fallback-root")).toBeTruthy();
|
|
191
|
+
expect(screen.queryByTestId("generative-ui-error-root")).toBeNull();
|
|
192
|
+
});
|
|
193
|
+
});
|
|
194
|
+
|
|
195
|
+
describe("GenerativeUIActivity", () => {
|
|
196
|
+
function setup(
|
|
197
|
+
initial: ReturnType<typeof activity>,
|
|
198
|
+
props: Partial<React.ComponentProps<typeof GenerativeUIActivity>> = {},
|
|
199
|
+
) {
|
|
200
|
+
const agentRef = {
|
|
201
|
+
current: null,
|
|
202
|
+
} as React.MutableRefObject<AbstractAgent | null>;
|
|
203
|
+
const view = (message: ReturnType<typeof activity>) => (
|
|
204
|
+
<Harness agentRef={agentRef}>
|
|
205
|
+
{(agent) => (
|
|
206
|
+
<GenerativeUIActivity message={message} agent={agent} {...props} />
|
|
207
|
+
)}
|
|
208
|
+
</Harness>
|
|
209
|
+
);
|
|
210
|
+
const utils = render(view(initial));
|
|
211
|
+
return {
|
|
212
|
+
rerender: (m: ReturnType<typeof activity>) => utils.rerender(view(m)),
|
|
213
|
+
};
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
it("renders the catalog's React Native components from an activity message, and streams updates in", () => {
|
|
217
|
+
const first = [
|
|
218
|
+
create,
|
|
219
|
+
nodes({ id: "root", component: "Column", children: ["a"] }),
|
|
220
|
+
nodes({ id: "a", component: "Balance", label: "Chequing", amount: 3482 }),
|
|
221
|
+
];
|
|
222
|
+
const { rerender } = setup(activity(first));
|
|
223
|
+
expect(screen.getByTestId("column")).toBeTruthy();
|
|
224
|
+
expect(screen.getByTestId("balance-Chequing").textContent).toBe(
|
|
225
|
+
"Chequing: 3482",
|
|
226
|
+
);
|
|
227
|
+
|
|
228
|
+
// A later render_ui call adds a block to the same surface.
|
|
229
|
+
rerender(
|
|
230
|
+
activity(
|
|
231
|
+
[
|
|
232
|
+
...first,
|
|
233
|
+
nodes({ id: "root", component: "Column", children: ["a", "b"] }),
|
|
234
|
+
nodes({
|
|
235
|
+
id: "b",
|
|
236
|
+
component: "Balance",
|
|
237
|
+
label: "Savings",
|
|
238
|
+
amount: 6150,
|
|
239
|
+
}),
|
|
240
|
+
],
|
|
241
|
+
"ready",
|
|
242
|
+
),
|
|
243
|
+
);
|
|
244
|
+
expect(screen.getByTestId("balance-Savings").textContent).toBe(
|
|
245
|
+
"Savings: 6150",
|
|
246
|
+
);
|
|
247
|
+
expect(screen.getByTestId("balance-Chequing")).toBeTruthy();
|
|
248
|
+
});
|
|
249
|
+
|
|
250
|
+
const homeSurface = (target: string) =>
|
|
251
|
+
activity(
|
|
252
|
+
[
|
|
253
|
+
create,
|
|
254
|
+
nodes({ id: "root", component: "Balance", label: "Home", amount: 1 }),
|
|
255
|
+
],
|
|
256
|
+
"ready",
|
|
257
|
+
target,
|
|
258
|
+
);
|
|
259
|
+
|
|
260
|
+
it("renders only chat-target surfaces by default, like the web chat", () => {
|
|
261
|
+
// A surface aimed at <GenerativeUISurface id="products"> is drawn there;
|
|
262
|
+
// drawing it in the chat list too would show it twice.
|
|
263
|
+
setup(homeSurface("products"));
|
|
264
|
+
expect(screen.queryByTestId("balance-Home")).toBeNull();
|
|
265
|
+
});
|
|
266
|
+
|
|
267
|
+
it("renders a surface of another target when the target prop names it", () => {
|
|
268
|
+
setup(homeSurface("products"), { target: "products" });
|
|
269
|
+
expect(screen.getByTestId("balance-Home")).toBeTruthy();
|
|
270
|
+
});
|
|
271
|
+
|
|
272
|
+
it("with a target prop, does not render chat-target surfaces", () => {
|
|
273
|
+
setup(homeSurface("chat"), { target: "products" });
|
|
274
|
+
expect(screen.queryByTestId("balance-Home")).toBeNull();
|
|
275
|
+
});
|
|
276
|
+
|
|
277
|
+
it.each([
|
|
278
|
+
["no surfaceId", { surfaceId: undefined }, "surfaceId"],
|
|
279
|
+
["no operations", { operations: undefined }, "operations"],
|
|
280
|
+
["operations that are not an array", { operations: "nope" }, "operations"],
|
|
281
|
+
["an unknown status", { status: "done" }, "status"],
|
|
282
|
+
["no target", { target: undefined }, "target"],
|
|
283
|
+
["rejected that is not an array", { rejected: { index: 0 } }, "rejected"],
|
|
284
|
+
[
|
|
285
|
+
"a rejected entry with a NaN index",
|
|
286
|
+
{ rejected: [{ index: Number.NaN, reason: "bad" }] },
|
|
287
|
+
"rejected[0]",
|
|
288
|
+
],
|
|
289
|
+
[
|
|
290
|
+
"a rejected entry that is not an object",
|
|
291
|
+
{ rejected: [{ index: 0, reason: "ok" }, "bad"] },
|
|
292
|
+
"rejected[1]",
|
|
293
|
+
],
|
|
294
|
+
// A hole reads as undefined, which the web content schema rejects too.
|
|
295
|
+
[
|
|
296
|
+
"a sparse rejected list",
|
|
297
|
+
{ rejected: sparse([1, { index: 1, reason: "x" }]) },
|
|
298
|
+
"rejected[0]",
|
|
299
|
+
],
|
|
300
|
+
])(
|
|
301
|
+
"renders nothing, does not throw, and warns with the issue in development, for malformed content (%s)",
|
|
302
|
+
(_label, patch, field) => {
|
|
303
|
+
const warn = vi.spyOn(console, "warn").mockImplementation(() => {});
|
|
304
|
+
const Wrapper = vi.fn(() => null);
|
|
305
|
+
const good = homeSurface("chat");
|
|
306
|
+
const bad = { ...good, content: { ...good.content, ...patch } };
|
|
307
|
+
expect(() =>
|
|
308
|
+
setup(bad as unknown as ReturnType<typeof activity>, { Wrapper }),
|
|
309
|
+
).not.toThrow();
|
|
310
|
+
expect(Wrapper).not.toHaveBeenCalled();
|
|
311
|
+
expect(screen.queryByTestId("balance-Home")).toBeNull();
|
|
312
|
+
const warnings = warningsWith(warn, "malformed");
|
|
313
|
+
expect(warnings).toHaveLength(1);
|
|
314
|
+
expect(warnings[0]).toContain(field);
|
|
315
|
+
},
|
|
316
|
+
);
|
|
317
|
+
|
|
318
|
+
it("accepts a rejected entry with extra keys, like the web content schema", () => {
|
|
319
|
+
const good = homeSurface("chat");
|
|
320
|
+
const content = {
|
|
321
|
+
...good.content,
|
|
322
|
+
rejected: [{ index: 1, reason: "bad", extra: true }],
|
|
323
|
+
};
|
|
324
|
+
setup({ ...good, content } as unknown as ReturnType<typeof activity>);
|
|
325
|
+
expect(screen.getByTestId("balance-Home")).toBeTruthy();
|
|
326
|
+
});
|
|
327
|
+
|
|
328
|
+
it.each([
|
|
329
|
+
["null", null],
|
|
330
|
+
["a string", "text"],
|
|
331
|
+
["a number", 42],
|
|
332
|
+
["an array", []],
|
|
333
|
+
])(
|
|
334
|
+
"renders nothing, and warns in development, for content that is %s",
|
|
335
|
+
(_label, content) => {
|
|
336
|
+
const warn = vi.spyOn(console, "warn").mockImplementation(() => {});
|
|
337
|
+
const Wrapper = vi.fn(() => null);
|
|
338
|
+
const good = homeSurface("chat");
|
|
339
|
+
expect(() =>
|
|
340
|
+
setup({ ...good, content } as unknown as ReturnType<typeof activity>, {
|
|
341
|
+
Wrapper,
|
|
342
|
+
}),
|
|
343
|
+
).not.toThrow();
|
|
344
|
+
expect(Wrapper).not.toHaveBeenCalled();
|
|
345
|
+
expect(screen.queryByTestId("balance-Home")).toBeNull();
|
|
346
|
+
expect(warningsWith(warn, "malformed")).toHaveLength(1);
|
|
347
|
+
},
|
|
348
|
+
);
|
|
349
|
+
|
|
350
|
+
it("treats missing rejected as an empty list", () => {
|
|
351
|
+
const good = homeSurface("chat");
|
|
352
|
+
const { rejected: _drop, ...content } = good.content;
|
|
353
|
+
setup({ ...good, content } as unknown as ReturnType<typeof activity>);
|
|
354
|
+
expect(screen.getByTestId("balance-Home")).toBeTruthy();
|
|
355
|
+
expect(screen.queryByRole("alert")).toBeNull();
|
|
356
|
+
});
|
|
357
|
+
|
|
358
|
+
const bareActivity = (agent: GenerativeUIActivityProps["agent"]) => (
|
|
359
|
+
<GenerativeUIActivity message={homeSurface("chat")} agent={agent} />
|
|
360
|
+
);
|
|
361
|
+
|
|
362
|
+
it("does not warn in production", () => {
|
|
363
|
+
vi.stubEnv("NODE_ENV", "production");
|
|
364
|
+
try {
|
|
365
|
+
const warn = vi.spyOn(console, "warn").mockImplementation(() => {});
|
|
366
|
+
render(bareActivity({ threadId: "t2" }));
|
|
367
|
+
const good = homeSurface("chat");
|
|
368
|
+
render(
|
|
369
|
+
<GenerativeUIActivity
|
|
370
|
+
message={{ ...good, content: null }}
|
|
371
|
+
agent={{ threadId: "t2" }}
|
|
372
|
+
/>,
|
|
373
|
+
);
|
|
374
|
+
expect(warn.mock.calls.map((c) => String(c[0]))).toEqual([]);
|
|
375
|
+
} finally {
|
|
376
|
+
vi.unstubAllEnvs();
|
|
377
|
+
}
|
|
378
|
+
});
|
|
379
|
+
|
|
380
|
+
it("warns once per component in development when there is no GenerativeUIProvider", () => {
|
|
381
|
+
const warn = vi.spyOn(console, "warn").mockImplementation(() => {});
|
|
382
|
+
const agent = { threadId: "t1", agentId: "a1" };
|
|
383
|
+
const { rerender, container } = render(bareActivity(agent));
|
|
384
|
+
rerender(bareActivity(agent));
|
|
385
|
+
expect(container.textContent).toBe("");
|
|
386
|
+
expect(
|
|
387
|
+
warningsWith(warn, "there is no <GenerativeUIProvider> above it"),
|
|
388
|
+
).toHaveLength(1);
|
|
389
|
+
// Another component makes the same mistake: it warns as well, whatever
|
|
390
|
+
// the tests that ran before.
|
|
391
|
+
render(bareActivity(agent));
|
|
392
|
+
expect(
|
|
393
|
+
warningsWith(warn, "there is no <GenerativeUIProvider> above it"),
|
|
394
|
+
).toHaveLength(2);
|
|
395
|
+
});
|
|
396
|
+
|
|
397
|
+
it("says the provider has no ui, not that there is no provider, when the provider has no ui", () => {
|
|
398
|
+
const warn = vi.spyOn(console, "warn").mockImplementation(() => {});
|
|
399
|
+
const agentRef = {
|
|
400
|
+
current: null,
|
|
401
|
+
} as React.MutableRefObject<AbstractAgent | null>;
|
|
402
|
+
render(
|
|
403
|
+
<Harness
|
|
404
|
+
agentRef={agentRef}
|
|
405
|
+
providerProps={
|
|
406
|
+
{ ui: undefined } as unknown as Partial<GenerativeUIProviderProps>
|
|
407
|
+
}
|
|
408
|
+
>
|
|
409
|
+
{(agent) => bareActivity(agent)}
|
|
410
|
+
</Harness>,
|
|
411
|
+
);
|
|
412
|
+
expect(warningsWith(warn, "there is no <GenerativeUIProvider>")).toEqual(
|
|
413
|
+
[],
|
|
414
|
+
);
|
|
415
|
+
expect(
|
|
416
|
+
warningsWith(
|
|
417
|
+
warn,
|
|
418
|
+
"<GenerativeUIActivity> renders nothing: the <GenerativeUIProvider> above it has no `ui`",
|
|
419
|
+
),
|
|
420
|
+
).toHaveLength(1);
|
|
421
|
+
});
|
|
422
|
+
|
|
423
|
+
it("warns in development when the agent is undefined, and again for a later, different mistake", () => {
|
|
424
|
+
const warn = vi.spyOn(console, "warn").mockImplementation(() => {});
|
|
425
|
+
const agentRef = {
|
|
426
|
+
current: null,
|
|
427
|
+
} as React.MutableRefObject<AbstractAgent | null>;
|
|
428
|
+
const view = (agent: GenerativeUIActivityProps["agent"]) => (
|
|
429
|
+
<Harness agentRef={agentRef}>{() => bareActivity(agent)}</Harness>
|
|
430
|
+
);
|
|
431
|
+
const { rerender } = render(view(undefined));
|
|
432
|
+
rerender(view(undefined));
|
|
433
|
+
expect(warningsWith(warn, "the agent is undefined")).toHaveLength(1);
|
|
434
|
+
rerender(view({ agentId: "a1" }));
|
|
435
|
+
expect(warningsWith(warn, "has no threadId")).toHaveLength(1);
|
|
436
|
+
expect(screen.queryByTestId("balance-Home")).toBeNull();
|
|
437
|
+
});
|
|
438
|
+
|
|
439
|
+
it("uses the React Native host: a failing component shows the native fallback, not a DOM alert", () => {
|
|
440
|
+
vi.spyOn(console, "error").mockImplementation(() => {});
|
|
441
|
+
vi.spyOn(console, "warn").mockImplementation(() => {});
|
|
442
|
+
setup(
|
|
443
|
+
activity(
|
|
444
|
+
[
|
|
445
|
+
create,
|
|
446
|
+
nodes({ id: "root", component: "Column", children: ["ok", "bad"] }),
|
|
447
|
+
nodes({ id: "ok", component: "Balance", label: "Fine", amount: 1 }),
|
|
448
|
+
nodes({ id: "bad", component: "Boom" }),
|
|
449
|
+
],
|
|
450
|
+
"ready",
|
|
451
|
+
),
|
|
452
|
+
);
|
|
453
|
+
// The broken node is contained; its sibling still renders.
|
|
454
|
+
expect(screen.getByTestId("balance-Fine")).toBeTruthy();
|
|
455
|
+
const fallback = screen.getByTestId("generative-ui-error-bad");
|
|
456
|
+
expect(fallback.textContent).toContain("kaboom");
|
|
457
|
+
expect(fallback.getAttribute("role")).toBe("alert");
|
|
458
|
+
// The DOM host would have rendered a div with data-generative-ui-error.
|
|
459
|
+
expect(document.querySelector("[data-generative-ui-error]")).toBeNull();
|
|
460
|
+
});
|
|
461
|
+
|
|
462
|
+
it("Retry renders the component again, and the fallback goes away once it recovers", () => {
|
|
463
|
+
vi.spyOn(console, "error").mockImplementation(() => {});
|
|
464
|
+
vi.spyOn(console, "warn").mockImplementation(() => {});
|
|
465
|
+
flaky.ok = false;
|
|
466
|
+
setup(
|
|
467
|
+
activity([create, nodes({ id: "root", component: "Flaky" })], "ready"),
|
|
468
|
+
);
|
|
469
|
+
expect(screen.getByTestId("generative-ui-error-root")).toBeTruthy();
|
|
470
|
+
expect(screen.queryByTestId("flaky-ok")).toBeNull();
|
|
471
|
+
flaky.ok = true;
|
|
472
|
+
fireEvent.click(screen.getByText("Retry"));
|
|
473
|
+
expect(screen.getByTestId("flaky-ok")).toBeTruthy();
|
|
474
|
+
expect(screen.queryByTestId("generative-ui-error-root")).toBeNull();
|
|
475
|
+
});
|
|
476
|
+
|
|
477
|
+
it("shows the default alert when the runtime rejected an update", () => {
|
|
478
|
+
const good = homeSurface("chat");
|
|
479
|
+
setup({
|
|
480
|
+
...good,
|
|
481
|
+
content: {
|
|
482
|
+
...good.content,
|
|
483
|
+
rejected: [{ index: 2, reason: "unknown component" }],
|
|
484
|
+
},
|
|
485
|
+
});
|
|
486
|
+
expect(screen.getByTestId("balance-Home")).toBeTruthy();
|
|
487
|
+
expect(screen.getByRole("alert").textContent).toBe(
|
|
488
|
+
"Part of this UI couldn't be shown.",
|
|
489
|
+
);
|
|
490
|
+
});
|
|
491
|
+
|
|
492
|
+
it("the default wrapper grows to fill its parent, and keeps the error under the surface", () => {
|
|
493
|
+
const good = homeSurface("chat");
|
|
494
|
+
setup({
|
|
495
|
+
...good,
|
|
496
|
+
content: {
|
|
497
|
+
...good.content,
|
|
498
|
+
rejected: [{ index: 2, reason: "unknown component" }],
|
|
499
|
+
},
|
|
500
|
+
});
|
|
501
|
+
const alert = screen.getByRole("alert");
|
|
502
|
+
const wrapper = alert.parentElement!;
|
|
503
|
+
// The wrapper is the view that holds both the surface and the alert.
|
|
504
|
+
expect(wrapper.contains(screen.getByTestId("balance-Home"))).toBe(true);
|
|
505
|
+
// A catalog root sized against its parent (flex: 1, a percentage
|
|
506
|
+
// height) resolves against this view, so it must not be content-sized
|
|
507
|
+
// where the parent has free space. flexGrow, not flex: 1, so a list row
|
|
508
|
+
// (no free space) keeps its content height and is never shrunk.
|
|
509
|
+
expect(JSON.parse(wrapper.getAttribute("data-style")!)).toEqual({
|
|
510
|
+
flexGrow: 1,
|
|
511
|
+
});
|
|
512
|
+
expect(wrapper.lastElementChild).toBe(alert);
|
|
513
|
+
});
|
|
514
|
+
|
|
515
|
+
it("gives a custom Wrapper the rejected operations and an error whose cause is the same list", () => {
|
|
516
|
+
const Wrapper = vi.fn(({ children }: GenerativeUIActivityWrapperProps) => (
|
|
517
|
+
<>{children}</>
|
|
518
|
+
));
|
|
519
|
+
const good = homeSurface("chat");
|
|
520
|
+
const rejected = [{ index: 2, reason: "unknown component" }];
|
|
521
|
+
setup({ ...good, content: { ...good.content, rejected } }, { Wrapper });
|
|
522
|
+
const props = Wrapper.mock.calls.at(-1)![0];
|
|
523
|
+
expect(props.rejected).toEqual(rejected);
|
|
524
|
+
expect(props.error?.message).toBe("1 UI update(s) could not be shown");
|
|
525
|
+
expect((props.error as Error & { cause?: unknown }).cause).toEqual(
|
|
526
|
+
rejected,
|
|
527
|
+
);
|
|
528
|
+
// Not enumerable, as on the web: JSON.stringify, a spread and error
|
|
529
|
+
// reporters leave the list out.
|
|
530
|
+
expect(
|
|
531
|
+
Object.prototype.propertyIsEnumerable.call(props.error, "cause"),
|
|
532
|
+
).toBe(false);
|
|
533
|
+
expect({ ...props.error }).not.toHaveProperty("cause");
|
|
534
|
+
expect(props.surfaceId).toBe(S);
|
|
535
|
+
expect(props.isStreaming).toBe(false);
|
|
536
|
+
});
|
|
537
|
+
|
|
538
|
+
it("passes on the updates rejected on the device, also when only the in-place list grew", () => {
|
|
539
|
+
vi.spyOn(console, "warn").mockImplementation(() => {});
|
|
540
|
+
const Wrapper = vi.fn(({ children }: GenerativeUIActivityWrapperProps) => (
|
|
541
|
+
<>{children}</>
|
|
542
|
+
));
|
|
543
|
+
// One `rejected` list for every content, so only the device's list,
|
|
544
|
+
// which the manager appends to in place, changes.
|
|
545
|
+
const serverRejected: GenerativeUIRejectedOperation[] = [];
|
|
546
|
+
const message = (operations: A2UIOperation[]) => ({
|
|
547
|
+
...activity(operations, "streaming"),
|
|
548
|
+
content: { ...activity(operations).content, rejected: serverRejected },
|
|
549
|
+
});
|
|
550
|
+
const ops = [
|
|
551
|
+
create,
|
|
552
|
+
nodes({ id: "root", component: "Balance", label: "Ok", amount: 1 }),
|
|
553
|
+
];
|
|
554
|
+
const bad = (id: string) =>
|
|
555
|
+
nodes({ id, component: "Balance", label: id, amount: "x" as never });
|
|
556
|
+
const { rerender } = setup(message(ops), { Wrapper });
|
|
557
|
+
expect(Wrapper.mock.calls.at(-1)![0].rejected).toEqual([]);
|
|
558
|
+
rerender(message([...ops, bad("b1")]));
|
|
559
|
+
expect(Wrapper.mock.calls.at(-1)![0].rejected).toHaveLength(1);
|
|
560
|
+
rerender(message([...ops, bad("b1"), bad("b2")]));
|
|
561
|
+
const last = Wrapper.mock.calls.at(-1)![0];
|
|
562
|
+
expect(last.rejected.map((r) => r.index)).toEqual([2, 3]);
|
|
563
|
+
expect(last.error?.message).toBe("2 UI update(s) could not be shown");
|
|
564
|
+
});
|
|
565
|
+
|
|
566
|
+
it("shows only the error, with no surface, when the agent's createSurface was rejected", () => {
|
|
567
|
+
vi.spyOn(console, "warn").mockImplementation(() => {});
|
|
568
|
+
const Wrapper = vi.fn(({ children }: GenerativeUIActivityWrapperProps) => (
|
|
569
|
+
<div data-testid="wrapper">{children}</div>
|
|
570
|
+
));
|
|
571
|
+
const badCreate: A2UIOperation = {
|
|
572
|
+
version: "1.0",
|
|
573
|
+
createSurface: { surfaceId: S, catalogId: "no-such-catalog" },
|
|
574
|
+
};
|
|
575
|
+
setup(activity([badCreate], "ready"), { Wrapper });
|
|
576
|
+
const props = Wrapper.mock.calls.at(-1)![0];
|
|
577
|
+
expect(props.children).toBeNull();
|
|
578
|
+
expect(props.rejected).toHaveLength(1);
|
|
579
|
+
expect(props.rejected[0].reason).toContain("no-such-catalog");
|
|
580
|
+
expect(screen.getByTestId("wrapper").textContent).toBe("");
|
|
581
|
+
});
|
|
582
|
+
|
|
583
|
+
it("renders nothing once the agent deleted the surface", () => {
|
|
584
|
+
const Wrapper = vi.fn(() => null);
|
|
585
|
+
setup(
|
|
586
|
+
activity(
|
|
587
|
+
[
|
|
588
|
+
create,
|
|
589
|
+
nodes({ id: "root", component: "Balance", label: "Gone", amount: 1 }),
|
|
590
|
+
{ version: "1.0", deleteSurface: { surfaceId: S } },
|
|
591
|
+
],
|
|
592
|
+
"ready",
|
|
593
|
+
),
|
|
594
|
+
{ Wrapper },
|
|
595
|
+
);
|
|
596
|
+
expect(Wrapper).not.toHaveBeenCalled();
|
|
597
|
+
expect(screen.queryByTestId("balance-Gone")).toBeNull();
|
|
598
|
+
});
|
|
599
|
+
|
|
600
|
+
it("draws no DOM surface root (no a2ui-surface element, no <style>)", () => {
|
|
601
|
+
setup(
|
|
602
|
+
activity(
|
|
603
|
+
[
|
|
604
|
+
create,
|
|
605
|
+
nodes({ id: "root", component: "Balance", label: "X", amount: 2 }),
|
|
606
|
+
],
|
|
607
|
+
"ready",
|
|
608
|
+
),
|
|
609
|
+
);
|
|
610
|
+
expect(document.querySelector(".a2ui-surface")).toBeNull();
|
|
611
|
+
expect(document.querySelector("style")).toBeNull();
|
|
612
|
+
});
|
|
613
|
+
|
|
614
|
+
it("renders nothing for a message that is not a generative UI activity", () => {
|
|
615
|
+
const agentRef = {
|
|
616
|
+
current: null,
|
|
617
|
+
} as React.MutableRefObject<AbstractAgent | null>;
|
|
618
|
+
const { container } = render(
|
|
619
|
+
<Harness agentRef={agentRef}>
|
|
620
|
+
{(agent) => (
|
|
621
|
+
<GenerativeUIActivity
|
|
622
|
+
message={{ id: "m1", role: "assistant", content: "hi" }}
|
|
623
|
+
agent={agent}
|
|
624
|
+
/>
|
|
625
|
+
)}
|
|
626
|
+
</Harness>,
|
|
627
|
+
);
|
|
628
|
+
expect(container.textContent).toBe("");
|
|
629
|
+
});
|
|
630
|
+
|
|
631
|
+
it("isGenerativeUIActivity picks out surface activity messages", () => {
|
|
632
|
+
expect(isGenerativeUIActivity(activity([create]))).toBe(true);
|
|
633
|
+
expect(isGenerativeUIActivity({ role: "assistant" })).toBe(false);
|
|
634
|
+
expect(
|
|
635
|
+
isGenerativeUIActivity({ role: "activity", activityType: "other" }),
|
|
636
|
+
).toBe(false);
|
|
637
|
+
});
|
|
638
|
+
});
|
|
639
|
+
|
|
640
|
+
describe("GenerativeUISurface", () => {
|
|
641
|
+
type Manager = NonNullable<
|
|
642
|
+
ReturnType<typeof useGenerativeUIRuntime>
|
|
643
|
+
>["manager"];
|
|
644
|
+
/** Renders `children` in the harness and hands back the surface manager. */
|
|
645
|
+
function mount(
|
|
646
|
+
children: ReactNode,
|
|
647
|
+
providerProps?: Partial<GenerativeUIProviderProps>,
|
|
648
|
+
) {
|
|
649
|
+
const box: { manager?: Manager } = {};
|
|
650
|
+
function Grab() {
|
|
651
|
+
box.manager = useGenerativeUIRuntime()?.manager;
|
|
652
|
+
return null;
|
|
653
|
+
}
|
|
654
|
+
const agentRef = {
|
|
655
|
+
current: null,
|
|
656
|
+
} as React.MutableRefObject<AbstractAgent | null>;
|
|
657
|
+
const view = (c: ReactNode) => (
|
|
658
|
+
<Harness agentRef={agentRef} providerProps={providerProps}>
|
|
659
|
+
{() => (
|
|
660
|
+
<>
|
|
661
|
+
<Grab />
|
|
662
|
+
{c}
|
|
663
|
+
</>
|
|
664
|
+
)}
|
|
665
|
+
</Harness>
|
|
666
|
+
);
|
|
667
|
+
const utils = render(view(children));
|
|
668
|
+
return {
|
|
669
|
+
manager: () => box.manager!,
|
|
670
|
+
rerender: (c: ReactNode) => utils.rerender(view(c)),
|
|
671
|
+
unmount: utils.unmount,
|
|
672
|
+
};
|
|
673
|
+
}
|
|
674
|
+
const insights = (
|
|
675
|
+
status: GenerativeUISurfaceContent["status"],
|
|
676
|
+
label = "Insight",
|
|
677
|
+
surfaceId = S,
|
|
678
|
+
) => ({
|
|
679
|
+
...activity(
|
|
680
|
+
[
|
|
681
|
+
{ version: "1.0", createSurface: { surfaceId, catalogId: "bank" } },
|
|
682
|
+
{
|
|
683
|
+
version: "1.0",
|
|
684
|
+
updateComponents: {
|
|
685
|
+
surfaceId,
|
|
686
|
+
components: [
|
|
687
|
+
{ id: "root", component: "Balance", label, amount: 7 },
|
|
688
|
+
],
|
|
689
|
+
},
|
|
690
|
+
},
|
|
691
|
+
],
|
|
692
|
+
status,
|
|
693
|
+
"insights",
|
|
694
|
+
).content,
|
|
695
|
+
surfaceId,
|
|
696
|
+
});
|
|
697
|
+
|
|
698
|
+
it("shows the fallback, then the surface the agent renders into its target", () => {
|
|
699
|
+
const onStart = vi.fn();
|
|
700
|
+
const { manager } = mount(
|
|
701
|
+
<GenerativeUISurface
|
|
702
|
+
id="insights"
|
|
703
|
+
ui={ui}
|
|
704
|
+
components={["Balance"]}
|
|
705
|
+
description="Under the balances."
|
|
706
|
+
fallback={<span>nothing yet</span>}
|
|
707
|
+
onGenerationStart={onStart}
|
|
708
|
+
/>,
|
|
709
|
+
);
|
|
710
|
+
expect(screen.getByText("nothing yet")).toBeTruthy();
|
|
711
|
+
act(() => {
|
|
712
|
+
manager().setActiveThread("t1");
|
|
713
|
+
manager().ingest("t1", "default", insights("streaming"));
|
|
714
|
+
});
|
|
715
|
+
expect(screen.getByTestId("balance-Insight")).toBeTruthy();
|
|
716
|
+
expect(screen.getByTestId("generative-ui-surface-insights")).toBeTruthy();
|
|
717
|
+
expect(screen.queryByText("nothing yet")).toBeNull();
|
|
718
|
+
expect(onStart).toHaveBeenCalledTimes(1);
|
|
719
|
+
});
|
|
720
|
+
|
|
721
|
+
it("applies style and testID while the fallback shows, too", () => {
|
|
722
|
+
const style = { flex: 1 };
|
|
723
|
+
const { manager } = mount(
|
|
724
|
+
<GenerativeUISurface
|
|
725
|
+
id="insights"
|
|
726
|
+
style={style}
|
|
727
|
+
fallback={<span>nothing yet</span>}
|
|
728
|
+
/>,
|
|
729
|
+
);
|
|
730
|
+
const box = screen.getByTestId("generative-ui-surface-insights");
|
|
731
|
+
expect(box.textContent).toBe("nothing yet");
|
|
732
|
+
expect(box.getAttribute("data-style")).toBe(JSON.stringify(style));
|
|
733
|
+
act(() => {
|
|
734
|
+
manager().setActiveThread("t1");
|
|
735
|
+
manager().ingest("t1", "default", insights("ready"));
|
|
736
|
+
});
|
|
737
|
+
const after = screen.getByTestId("generative-ui-surface-insights");
|
|
738
|
+
expect(after.getAttribute("data-style")).toBe(JSON.stringify(style));
|
|
739
|
+
expect(after.textContent).toContain("Insight");
|
|
740
|
+
});
|
|
741
|
+
|
|
742
|
+
it("reports the same failure of a new surface in its target", () => {
|
|
743
|
+
vi.spyOn(console, "error").mockImplementation(() => {});
|
|
744
|
+
const onError = vi.fn();
|
|
745
|
+
const { manager } = mount(<GenerativeUISurface id="insights" />, {
|
|
746
|
+
errorBoundary: { onError },
|
|
747
|
+
});
|
|
748
|
+
/** A surface in the target whose root always throws. */
|
|
749
|
+
const broken = (surfaceId: string) => ({
|
|
750
|
+
...activity(
|
|
751
|
+
[
|
|
752
|
+
{ version: "1.0", createSurface: { surfaceId, catalogId: "bank" } },
|
|
753
|
+
{
|
|
754
|
+
version: "1.0",
|
|
755
|
+
updateComponents: {
|
|
756
|
+
surfaceId,
|
|
757
|
+
components: [{ id: "root", component: "Boom" }],
|
|
758
|
+
},
|
|
759
|
+
},
|
|
760
|
+
],
|
|
761
|
+
"streaming",
|
|
762
|
+
"insights",
|
|
763
|
+
).content,
|
|
764
|
+
surfaceId,
|
|
765
|
+
});
|
|
766
|
+
act(() => {
|
|
767
|
+
manager().setActiveThread("t1");
|
|
768
|
+
manager().ingest("t1", "default", broken("first"));
|
|
769
|
+
});
|
|
770
|
+
act(() => manager().ingest("t1", "default", broken("second")));
|
|
771
|
+
expect(
|
|
772
|
+
onError.mock.calls.map(([info]) => [info.surfaceId, info.error.message]),
|
|
773
|
+
).toEqual([
|
|
774
|
+
["first", "kaboom"],
|
|
775
|
+
["second", "kaboom"],
|
|
776
|
+
]);
|
|
777
|
+
});
|
|
778
|
+
|
|
779
|
+
it("calls onGenerationStart for each render_ui call, also when React sees no ready render between two calls", () => {
|
|
780
|
+
const onStart = vi.fn();
|
|
781
|
+
const { manager } = mount(
|
|
782
|
+
<GenerativeUISurface id="insights" onGenerationStart={onStart} />,
|
|
783
|
+
);
|
|
784
|
+
act(() => {
|
|
785
|
+
manager().setActiveThread("t1");
|
|
786
|
+
manager().ingest("t1", "default", insights("streaming"));
|
|
787
|
+
});
|
|
788
|
+
expect(onStart).toHaveBeenCalledTimes(1);
|
|
789
|
+
// The first call ends and the second starts in the same React batch.
|
|
790
|
+
act(() => {
|
|
791
|
+
manager().ingest("t1", "default", insights("ready"));
|
|
792
|
+
manager().ingest("t1", "default", insights("streaming", "Second"));
|
|
793
|
+
});
|
|
794
|
+
expect(onStart).toHaveBeenCalledTimes(2);
|
|
795
|
+
// Updates within the same call do not count as a new start.
|
|
796
|
+
act(() => {
|
|
797
|
+
manager().ingest("t1", "default", insights("streaming", "Third"));
|
|
798
|
+
});
|
|
799
|
+
expect(onStart).toHaveBeenCalledTimes(2);
|
|
800
|
+
});
|
|
801
|
+
|
|
802
|
+
it("calls onGenerationStart when it mounts, or the thread changes, while the agent renders into it", () => {
|
|
803
|
+
const onStart = vi.fn();
|
|
804
|
+
const { manager, rerender } = mount(null);
|
|
805
|
+
act(() => {
|
|
806
|
+
manager().setActiveThread("t1");
|
|
807
|
+
manager().ingest("t1", "default", insights("streaming"));
|
|
808
|
+
manager().ingest("t2", "default", insights("streaming", "Other", "s2"));
|
|
809
|
+
});
|
|
810
|
+
rerender(<GenerativeUISurface id="insights" onGenerationStart={onStart} />);
|
|
811
|
+
expect(onStart).toHaveBeenCalledTimes(1);
|
|
812
|
+
act(() => manager().setActiveThread("t2"));
|
|
813
|
+
expect(screen.getByTestId("balance-Other")).toBeTruthy();
|
|
814
|
+
expect(onStart).toHaveBeenCalledTimes(2);
|
|
815
|
+
});
|
|
816
|
+
|
|
817
|
+
it("registers its target with its description and allowed components, and unregisters on unmount", () => {
|
|
818
|
+
const { manager, rerender } = mount(
|
|
819
|
+
<GenerativeUISurface
|
|
820
|
+
id="insights"
|
|
821
|
+
ui={ui}
|
|
822
|
+
description="Under the balances."
|
|
823
|
+
components={["Balance"]}
|
|
824
|
+
/>,
|
|
825
|
+
);
|
|
826
|
+
expect(manager().getMountedTargets()).toEqual([
|
|
827
|
+
{
|
|
828
|
+
id: "insights",
|
|
829
|
+
description: "Under the balances.",
|
|
830
|
+
components: ["Balance"],
|
|
831
|
+
},
|
|
832
|
+
]);
|
|
833
|
+
rerender(null);
|
|
834
|
+
expect(manager().getMountedTargets()).toEqual([]);
|
|
835
|
+
});
|
|
836
|
+
|
|
837
|
+
it("leaves unknown component names out of the target, and warns once in development", () => {
|
|
838
|
+
const warn = vi.spyOn(console, "warn").mockImplementation(() => {});
|
|
839
|
+
const names = ["Balance", "Nope"] as unknown as ["Balance"];
|
|
840
|
+
const view = () => (
|
|
841
|
+
<GenerativeUISurface id="insights" ui={ui} components={[...names]} />
|
|
842
|
+
);
|
|
843
|
+
const { manager, rerender } = mount(view());
|
|
844
|
+
rerender(view());
|
|
845
|
+
expect(manager().getMountedTargets()[0].components).toEqual(["Balance"]);
|
|
846
|
+
expect(warningsWith(warn, "Nope")).toHaveLength(1);
|
|
847
|
+
});
|
|
848
|
+
|
|
849
|
+
it("does not register the target again for a new array with the same names", () => {
|
|
850
|
+
const { manager, rerender } = mount(
|
|
851
|
+
<GenerativeUISurface id="insights" ui={ui} components={["Balance"]} />,
|
|
852
|
+
);
|
|
853
|
+
const register = vi.spyOn(manager(), "registerTarget");
|
|
854
|
+
rerender(
|
|
855
|
+
<GenerativeUISurface id="insights" ui={ui} components={["Balance"]} />,
|
|
856
|
+
);
|
|
857
|
+
expect(register).not.toHaveBeenCalled();
|
|
858
|
+
rerender(
|
|
859
|
+
<GenerativeUISurface
|
|
860
|
+
id="insights"
|
|
861
|
+
ui={ui}
|
|
862
|
+
components={["Balance", "Column"]}
|
|
863
|
+
/>,
|
|
864
|
+
);
|
|
865
|
+
expect(register).toHaveBeenCalledTimes(1);
|
|
866
|
+
});
|
|
867
|
+
|
|
868
|
+
it("shows the fallback, and warns that the provider has no ui, under a provider without ui", () => {
|
|
869
|
+
const warn = vi.spyOn(console, "warn").mockImplementation(() => {});
|
|
870
|
+
mount(
|
|
871
|
+
<GenerativeUISurface id="insights" fallback={<span>nothing yet</span>} />,
|
|
872
|
+
{ ui: undefined } as unknown as Partial<GenerativeUIProviderProps>,
|
|
873
|
+
);
|
|
874
|
+
expect(screen.getByText("nothing yet")).toBeTruthy();
|
|
875
|
+
expect(
|
|
876
|
+
warningsWith(
|
|
877
|
+
warn,
|
|
878
|
+
'<GenerativeUISurface id="insights"> shows its fallback: the <GenerativeUIProvider> above it has no `ui`',
|
|
879
|
+
),
|
|
880
|
+
).toHaveLength(1);
|
|
881
|
+
});
|
|
882
|
+
|
|
883
|
+
it("throws, naming GenerativeUIProvider, when there is no provider", () => {
|
|
884
|
+
vi.spyOn(console, "error").mockImplementation(() => {});
|
|
885
|
+
expect(() => render(<GenerativeUISurface id="insights" />)).toThrow(
|
|
886
|
+
/<GenerativeUIProvider>/,
|
|
887
|
+
);
|
|
888
|
+
});
|
|
889
|
+
});
|
|
890
|
+
|
|
891
|
+
describe("the React Native accessibility wrapper", () => {
|
|
892
|
+
function renderWithEnvelope(accessibility: Record<string, unknown>) {
|
|
893
|
+
const agentRef = {
|
|
894
|
+
current: null,
|
|
895
|
+
} as React.MutableRefObject<AbstractAgent | null>;
|
|
896
|
+
const message = activity(
|
|
897
|
+
[
|
|
898
|
+
create,
|
|
899
|
+
nodes({
|
|
900
|
+
id: "root",
|
|
901
|
+
component: "Balance",
|
|
902
|
+
label: "Totals",
|
|
903
|
+
amount: 5,
|
|
904
|
+
accessibility,
|
|
905
|
+
} as A2UIComponentNode),
|
|
906
|
+
],
|
|
907
|
+
"ready",
|
|
908
|
+
);
|
|
909
|
+
render(
|
|
910
|
+
<Harness agentRef={agentRef}>
|
|
911
|
+
{(agent) => <GenerativeUIActivity message={message} agent={agent} />}
|
|
912
|
+
</Harness>,
|
|
913
|
+
);
|
|
914
|
+
// The wrapper is the native view around the component.
|
|
915
|
+
return screen.getByTestId("balance-Totals").parentElement!;
|
|
916
|
+
}
|
|
917
|
+
|
|
918
|
+
it("is a real native view that is not flattened away, with no display: contents", () => {
|
|
919
|
+
const wrapper = renderWithEnvelope({ live: "polite" });
|
|
920
|
+
expect(wrapper.tagName).toBe("DIV");
|
|
921
|
+
// display: contents needs RN 0.77+ on the New Architecture, and a
|
|
922
|
+
// contents view is dropped from the native tree with its props.
|
|
923
|
+
expect(wrapper.getAttribute("data-style")).not.toContain("contents");
|
|
924
|
+
// Fabric and the Android view flattener drop a layout-only view, and
|
|
925
|
+
// its accessibility props with it, unless it is not collapsable.
|
|
926
|
+
expect(wrapper.getAttribute("data-collapsable")).toBe("false");
|
|
927
|
+
});
|
|
928
|
+
|
|
929
|
+
it("hides the component from VoiceOver and TalkBack when the envelope is hidden", () => {
|
|
930
|
+
const wrapper = renderWithEnvelope({ hidden: true });
|
|
931
|
+
expect(wrapper.getAttribute("data-accessibility-elements-hidden")).toBe(
|
|
932
|
+
"true",
|
|
933
|
+
);
|
|
934
|
+
expect(wrapper.getAttribute("data-important-for-accessibility")).toBe(
|
|
935
|
+
"no-hide-descendants",
|
|
936
|
+
);
|
|
937
|
+
expect(wrapper.getAttribute("data-aria-hidden")).toBe("true");
|
|
938
|
+
});
|
|
939
|
+
|
|
940
|
+
it("leaves the component visible to assistive technologies when it is not hidden", () => {
|
|
941
|
+
const wrapper = renderWithEnvelope({ hidden: false, live: "polite" });
|
|
942
|
+
expect(wrapper.hasAttribute("data-accessibility-elements-hidden")).toBe(
|
|
943
|
+
false,
|
|
944
|
+
);
|
|
945
|
+
expect(wrapper.hasAttribute("data-important-for-accessibility")).toBe(
|
|
946
|
+
false,
|
|
947
|
+
);
|
|
948
|
+
expect(wrapper.hasAttribute("data-aria-hidden")).toBe(false);
|
|
949
|
+
});
|
|
950
|
+
|
|
951
|
+
it.each([
|
|
952
|
+
["off", "none"],
|
|
953
|
+
["polite", "polite"],
|
|
954
|
+
["assertive", "assertive"],
|
|
955
|
+
])("maps aria-live %s to accessibilityLiveRegion %s", (live, region) => {
|
|
956
|
+
const wrapper = renderWithEnvelope({ live });
|
|
957
|
+
expect(wrapper.getAttribute("data-accessibility-live-region")).toBe(region);
|
|
958
|
+
});
|
|
959
|
+
|
|
960
|
+
it("has no layout style: it sizes to its child, so a child's flex or percentage height resolves inside it", () => {
|
|
961
|
+
// Documented in host.tsx: the wrapper cannot read the style of the
|
|
962
|
+
// component's root view, so it cannot pass flex sizing on. A component
|
|
963
|
+
// that must fill its parent sets `accessibility: "self"`.
|
|
964
|
+
const wrapper = renderWithEnvelope({ live: "polite" });
|
|
965
|
+
expect(wrapper.getAttribute("data-style")).toBe("null");
|
|
966
|
+
});
|
|
967
|
+
|
|
968
|
+
it("is left out for a component that applies the envelope itself", () => {
|
|
969
|
+
const agentRef = {
|
|
970
|
+
current: null,
|
|
971
|
+
} as React.MutableRefObject<AbstractAgent | null>;
|
|
972
|
+
const message = activity(
|
|
973
|
+
[
|
|
974
|
+
create,
|
|
975
|
+
nodes({
|
|
976
|
+
id: "root",
|
|
977
|
+
component: "Column",
|
|
978
|
+
children: ["p"],
|
|
979
|
+
}),
|
|
980
|
+
nodes({
|
|
981
|
+
id: "p",
|
|
982
|
+
component: "SelfPanel",
|
|
983
|
+
accessibility: { hidden: true },
|
|
984
|
+
} as A2UIComponentNode),
|
|
985
|
+
],
|
|
986
|
+
"ready",
|
|
987
|
+
);
|
|
988
|
+
render(
|
|
989
|
+
<Harness agentRef={agentRef}>
|
|
990
|
+
{(agent) => <GenerativeUIActivity message={message} agent={agent} />}
|
|
991
|
+
</Harness>,
|
|
992
|
+
);
|
|
993
|
+
const panel = screen.getByTestId("self-panel");
|
|
994
|
+
// Its parent is the Column, not a wrapper view.
|
|
995
|
+
expect(panel.parentElement).toBe(screen.getByTestId("column"));
|
|
996
|
+
expect(JSON.parse(panel.getAttribute("data-aria")!)).toMatchObject({
|
|
997
|
+
"aria-hidden": true,
|
|
998
|
+
});
|
|
999
|
+
});
|
|
1000
|
+
});
|