@copilotkit/react-native 1.57.1 → 1.57.2
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 +11 -1
- package/dist/index.cjs +752 -9
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +394 -6
- package/dist/index.d.mts +394 -6
- package/dist/index.mjs +725 -14
- package/dist/index.mjs.map +1 -1
- package/dist/polyfills.cjs +86 -73
- package/dist/polyfills.cjs.map +1 -1
- package/dist/polyfills.mjs +86 -73
- package/dist/polyfills.mjs.map +1 -1
- package/package.json +21 -5
- package/src/CopilotChat.tsx +293 -0
- package/src/CopilotKitProvider.tsx +55 -13
- package/src/CopilotModal.tsx +32 -0
- package/src/CopilotPopup.tsx +359 -0
- package/src/CopilotSidebar.tsx +357 -0
- package/src/__tests__/__mocks__/expo-document-picker.ts +13 -0
- package/src/__tests__/__mocks__/expo-file-system.ts +12 -0
- package/src/__tests__/attachments-integration.test.tsx +336 -0
- package/src/__tests__/copilot-chat-agentid.test.tsx +305 -0
- package/src/__tests__/copilot-chat-attachments.test.tsx +259 -0
- package/src/__tests__/copilot-popup.test.tsx +558 -0
- package/src/__tests__/copilot-sidebar.test.tsx +500 -0
- package/src/__tests__/copilotkit-provider.test.tsx +3 -0
- package/src/__tests__/headless-integration.test.tsx +55 -1
- package/src/__tests__/streaming-fetch.test.ts +84 -44
- package/src/__tests__/use-attachments.test.ts +410 -0
- package/src/hooks/use-attachments.ts +274 -0
- package/src/index.ts +77 -3
- package/src/polyfills.ts +3 -2
- package/src/streaming-fetch.ts +135 -104
- package/tsdown.config.ts +1 -0
- package/vitest.config.mjs +19 -0
|
@@ -0,0 +1,500 @@
|
|
|
1
|
+
import React, { createRef } from "react";
|
|
2
|
+
import { render, fireEvent, act } from "@testing-library/react";
|
|
3
|
+
import { describe, it, expect, beforeEach, afterEach, vi } from "vitest";
|
|
4
|
+
|
|
5
|
+
// ─── Mocks ────────────────────────────────────────────────────────────────────
|
|
6
|
+
|
|
7
|
+
// Track the agentId passed to useAgent across renders
|
|
8
|
+
let capturedAgentId: string | undefined;
|
|
9
|
+
|
|
10
|
+
const hoisted = vi.hoisted(() => {
|
|
11
|
+
const _React = require("react");
|
|
12
|
+
return {
|
|
13
|
+
RealContext: _React.createContext(null),
|
|
14
|
+
MockCoreConstructor: vi.fn(),
|
|
15
|
+
};
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
let unsubscribeMock: ReturnType<typeof vi.fn>;
|
|
19
|
+
|
|
20
|
+
function createMockCore() {
|
|
21
|
+
return {
|
|
22
|
+
subscribe: vi.fn((_subscriber: any) => ({
|
|
23
|
+
unsubscribe: unsubscribeMock,
|
|
24
|
+
})),
|
|
25
|
+
subscribeToAgentWithOptions: vi.fn((_agent: any, _handlers: any) => ({
|
|
26
|
+
unsubscribe: vi.fn(),
|
|
27
|
+
})),
|
|
28
|
+
setRuntimeUrl: vi.fn(),
|
|
29
|
+
setRuntimeTransport: vi.fn(),
|
|
30
|
+
setHeaders: vi.fn(),
|
|
31
|
+
setCredentials: vi.fn(),
|
|
32
|
+
setProperties: vi.fn(),
|
|
33
|
+
setDebug: vi.fn(),
|
|
34
|
+
setDefaultThrottleMs: vi.fn(),
|
|
35
|
+
getAgent: vi.fn(() => undefined),
|
|
36
|
+
runtimeUrl: "https://api.test",
|
|
37
|
+
runtimeTransport: "auto",
|
|
38
|
+
runtimeConnectionStatus: "Disconnected",
|
|
39
|
+
headers: {},
|
|
40
|
+
agents: {},
|
|
41
|
+
defaultThrottleMs: undefined,
|
|
42
|
+
addTool: vi.fn(),
|
|
43
|
+
removeTool: vi.fn(),
|
|
44
|
+
getTool: vi.fn(() => undefined),
|
|
45
|
+
addHookRenderToolCall: vi.fn(),
|
|
46
|
+
registerThreadStore: vi.fn(),
|
|
47
|
+
unregisterThreadStore: vi.fn(),
|
|
48
|
+
intelligence: undefined,
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
let mockCoreInstance: ReturnType<typeof createMockCore>;
|
|
53
|
+
|
|
54
|
+
vi.mock("@copilotkit/react-core/v2/headless", () => {
|
|
55
|
+
function CopilotKitCoreReact(this: any, ...args: any[]) {
|
|
56
|
+
hoisted.MockCoreConstructor(...args);
|
|
57
|
+
const instance = hoisted.MockCoreConstructor.mock.results.at(-1)?.value;
|
|
58
|
+
if (instance) Object.assign(this, instance);
|
|
59
|
+
}
|
|
60
|
+
return {
|
|
61
|
+
CopilotKitCoreReact,
|
|
62
|
+
useAgent: (props: any) => {
|
|
63
|
+
const ctx = require("react").useContext(hoisted.RealContext);
|
|
64
|
+
if (!ctx) {
|
|
65
|
+
throw new Error("useCopilotKit must be used within CopilotKitProvider");
|
|
66
|
+
}
|
|
67
|
+
capturedAgentId = props?.agentId;
|
|
68
|
+
return { agent: {} };
|
|
69
|
+
},
|
|
70
|
+
useFrontendTool: () => {},
|
|
71
|
+
useComponent: () => {},
|
|
72
|
+
useHumanInTheLoop: () => {},
|
|
73
|
+
useInterrupt: () => {},
|
|
74
|
+
useSuggestions: () => {},
|
|
75
|
+
useConfigureSuggestions: () => {},
|
|
76
|
+
useAgentContext: () => {},
|
|
77
|
+
useThreads: () => ({
|
|
78
|
+
threads: [],
|
|
79
|
+
isLoading: false,
|
|
80
|
+
error: null,
|
|
81
|
+
hasMoreThreads: false,
|
|
82
|
+
isFetchingMoreThreads: false,
|
|
83
|
+
fetchMoreThreads: () => {},
|
|
84
|
+
renameThread: async () => {},
|
|
85
|
+
archiveThread: async () => {},
|
|
86
|
+
deleteThread: async () => {},
|
|
87
|
+
}),
|
|
88
|
+
CopilotChatConfigurationProvider: ({ children }: any) => children,
|
|
89
|
+
useCopilotChatConfiguration: () => null,
|
|
90
|
+
CopilotChatDefaultLabels: {},
|
|
91
|
+
};
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
vi.mock("@copilotkit/react-core/v2/context", () => {
|
|
95
|
+
const _React = require("react");
|
|
96
|
+
return {
|
|
97
|
+
CopilotKitContext: hoisted.RealContext,
|
|
98
|
+
LicenseContext: _React.createContext({
|
|
99
|
+
status: null,
|
|
100
|
+
license: null,
|
|
101
|
+
checkFeature: () => true,
|
|
102
|
+
getLimit: () => null,
|
|
103
|
+
}),
|
|
104
|
+
useCopilotKit: () => {
|
|
105
|
+
const ctx = _React.useContext(hoisted.RealContext);
|
|
106
|
+
if (!ctx) {
|
|
107
|
+
throw new Error("useCopilotKit must be used within CopilotKitProvider");
|
|
108
|
+
}
|
|
109
|
+
return ctx;
|
|
110
|
+
},
|
|
111
|
+
useLicenseContext: () => ({
|
|
112
|
+
status: null,
|
|
113
|
+
license: null,
|
|
114
|
+
checkFeature: () => true,
|
|
115
|
+
getLimit: () => null,
|
|
116
|
+
}),
|
|
117
|
+
};
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
vi.mock("@copilotkit/shared", () => ({
|
|
121
|
+
DEFAULT_AGENT_ID: "default",
|
|
122
|
+
randomUUID: () => "test-uuid-" + Math.random().toString(36).slice(2, 8),
|
|
123
|
+
getModalityFromMimeType: (mimeType: string) => {
|
|
124
|
+
if (mimeType.startsWith("image/")) return "image";
|
|
125
|
+
if (mimeType.startsWith("audio/")) return "audio";
|
|
126
|
+
if (mimeType.startsWith("video/")) return "video";
|
|
127
|
+
return "document";
|
|
128
|
+
},
|
|
129
|
+
formatFileSize: (bytes: number) => `${bytes} B`,
|
|
130
|
+
createLicenseContextValue: () => ({
|
|
131
|
+
status: null,
|
|
132
|
+
license: null,
|
|
133
|
+
checkFeature: () => true,
|
|
134
|
+
getLimit: () => null,
|
|
135
|
+
}),
|
|
136
|
+
}));
|
|
137
|
+
|
|
138
|
+
vi.mock("expo-document-picker", () => ({
|
|
139
|
+
getDocumentAsync: vi.fn().mockResolvedValue({ canceled: true, assets: [] }),
|
|
140
|
+
}));
|
|
141
|
+
vi.mock("expo-file-system", () => ({
|
|
142
|
+
readAsStringAsync: vi.fn().mockResolvedValue("base64data"),
|
|
143
|
+
EncodingType: { Base64: "base64" },
|
|
144
|
+
}));
|
|
145
|
+
|
|
146
|
+
// Mock react-native since tests run in jsdom
|
|
147
|
+
vi.mock("react-native", () => {
|
|
148
|
+
const _React = require("react");
|
|
149
|
+
|
|
150
|
+
// Minimal Animated.Value mock
|
|
151
|
+
class MockAnimatedValue {
|
|
152
|
+
_value: number;
|
|
153
|
+
constructor(v: number) {
|
|
154
|
+
this._value = v;
|
|
155
|
+
}
|
|
156
|
+
setValue(v: number) {
|
|
157
|
+
this._value = v;
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
return {
|
|
162
|
+
Animated: {
|
|
163
|
+
View: _React.forwardRef((props: any, ref: any) => {
|
|
164
|
+
const { testID, ...rest } = props;
|
|
165
|
+
return _React.createElement("div", {
|
|
166
|
+
...rest,
|
|
167
|
+
ref,
|
|
168
|
+
"data-testid": testID,
|
|
169
|
+
});
|
|
170
|
+
}),
|
|
171
|
+
Value: MockAnimatedValue,
|
|
172
|
+
timing: (_value: any, _config: any) => ({
|
|
173
|
+
start: (cb?: (result: { finished: boolean }) => void) => {
|
|
174
|
+
cb?.({ finished: true });
|
|
175
|
+
},
|
|
176
|
+
}),
|
|
177
|
+
},
|
|
178
|
+
Pressable: _React.forwardRef((props: any, ref: any) => {
|
|
179
|
+
const { onPress, testID, children, ...rest } = props;
|
|
180
|
+
return _React.createElement(
|
|
181
|
+
"button",
|
|
182
|
+
{ ...rest, ref, onClick: onPress, "data-testid": testID },
|
|
183
|
+
children,
|
|
184
|
+
);
|
|
185
|
+
}),
|
|
186
|
+
View: _React.forwardRef((props: any, ref: any) => {
|
|
187
|
+
const { testID, ...rest } = props;
|
|
188
|
+
return _React.createElement("div", {
|
|
189
|
+
...rest,
|
|
190
|
+
ref,
|
|
191
|
+
"data-testid": testID,
|
|
192
|
+
});
|
|
193
|
+
}),
|
|
194
|
+
Text: _React.forwardRef((props: any, ref: any) =>
|
|
195
|
+
_React.createElement("span", { ...props, ref }),
|
|
196
|
+
),
|
|
197
|
+
Dimensions: {
|
|
198
|
+
get: () => ({ width: 400, height: 800 }),
|
|
199
|
+
},
|
|
200
|
+
StyleSheet: {
|
|
201
|
+
create: (styles: any) => styles,
|
|
202
|
+
absoluteFillObject: {
|
|
203
|
+
position: "absolute",
|
|
204
|
+
top: 0,
|
|
205
|
+
left: 0,
|
|
206
|
+
right: 0,
|
|
207
|
+
bottom: 0,
|
|
208
|
+
},
|
|
209
|
+
hairlineWidth: 1,
|
|
210
|
+
},
|
|
211
|
+
useWindowDimensions: () => ({ width: 400, height: 800 }),
|
|
212
|
+
};
|
|
213
|
+
});
|
|
214
|
+
|
|
215
|
+
// Import after mocks
|
|
216
|
+
import { CopilotKitProvider } from "../CopilotKitProvider";
|
|
217
|
+
import { CopilotSidebar } from "../CopilotSidebar";
|
|
218
|
+
import type { CopilotSidebarHandle } from "../CopilotSidebar";
|
|
219
|
+
|
|
220
|
+
// ─── Tests ────────────────────────────────────────────────────────────────────
|
|
221
|
+
|
|
222
|
+
describe("CopilotSidebar", () => {
|
|
223
|
+
beforeEach(() => {
|
|
224
|
+
capturedAgentId = undefined;
|
|
225
|
+
unsubscribeMock = vi.fn();
|
|
226
|
+
mockCoreInstance = createMockCore();
|
|
227
|
+
hoisted.MockCoreConstructor.mockClear();
|
|
228
|
+
hoisted.MockCoreConstructor.mockReturnValue(mockCoreInstance);
|
|
229
|
+
});
|
|
230
|
+
|
|
231
|
+
afterEach(() => {
|
|
232
|
+
vi.clearAllMocks();
|
|
233
|
+
});
|
|
234
|
+
|
|
235
|
+
it("renders without crashing", () => {
|
|
236
|
+
const { container } = render(
|
|
237
|
+
<CopilotKitProvider runtimeUrl="https://api.test">
|
|
238
|
+
<CopilotSidebar />
|
|
239
|
+
</CopilotKitProvider>,
|
|
240
|
+
);
|
|
241
|
+
|
|
242
|
+
expect(container).toBeTruthy();
|
|
243
|
+
});
|
|
244
|
+
|
|
245
|
+
it("renders the FAB toggle button by default when closed", () => {
|
|
246
|
+
const { getByTestId } = render(
|
|
247
|
+
<CopilotKitProvider runtimeUrl="https://api.test">
|
|
248
|
+
<CopilotSidebar />
|
|
249
|
+
</CopilotKitProvider>,
|
|
250
|
+
);
|
|
251
|
+
|
|
252
|
+
expect(getByTestId("copilot-sidebar-fab")).toBeTruthy();
|
|
253
|
+
});
|
|
254
|
+
|
|
255
|
+
it("hides the FAB when showToggleButton is false", () => {
|
|
256
|
+
const { queryByTestId } = render(
|
|
257
|
+
<CopilotKitProvider runtimeUrl="https://api.test">
|
|
258
|
+
<CopilotSidebar showToggleButton={false} />
|
|
259
|
+
</CopilotKitProvider>,
|
|
260
|
+
);
|
|
261
|
+
|
|
262
|
+
expect(queryByTestId("copilot-sidebar-fab")).toBeNull();
|
|
263
|
+
});
|
|
264
|
+
|
|
265
|
+
it("defaultOpen=true renders the drawer immediately", () => {
|
|
266
|
+
const { getByTestId } = render(
|
|
267
|
+
<CopilotKitProvider runtimeUrl="https://api.test">
|
|
268
|
+
<CopilotSidebar defaultOpen />
|
|
269
|
+
</CopilotKitProvider>,
|
|
270
|
+
);
|
|
271
|
+
|
|
272
|
+
expect(getByTestId("copilot-sidebar-drawer")).toBeTruthy();
|
|
273
|
+
expect(getByTestId("copilot-sidebar-backdrop")).toBeTruthy();
|
|
274
|
+
});
|
|
275
|
+
|
|
276
|
+
it("defaultOpen=false does not render the drawer", () => {
|
|
277
|
+
const { queryByTestId } = render(
|
|
278
|
+
<CopilotKitProvider runtimeUrl="https://api.test">
|
|
279
|
+
<CopilotSidebar defaultOpen={false} />
|
|
280
|
+
</CopilotKitProvider>,
|
|
281
|
+
);
|
|
282
|
+
|
|
283
|
+
expect(queryByTestId("copilot-sidebar-drawer")).toBeNull();
|
|
284
|
+
expect(queryByTestId("copilot-sidebar-backdrop")).toBeNull();
|
|
285
|
+
});
|
|
286
|
+
|
|
287
|
+
it("open() imperative method opens the drawer", () => {
|
|
288
|
+
const ref = createRef<CopilotSidebarHandle>();
|
|
289
|
+
|
|
290
|
+
const { queryByTestId, getByTestId } = render(
|
|
291
|
+
<CopilotKitProvider runtimeUrl="https://api.test">
|
|
292
|
+
<CopilotSidebar ref={ref} />
|
|
293
|
+
</CopilotKitProvider>,
|
|
294
|
+
);
|
|
295
|
+
|
|
296
|
+
expect(queryByTestId("copilot-sidebar-drawer")).toBeNull();
|
|
297
|
+
|
|
298
|
+
act(() => {
|
|
299
|
+
ref.current!.open();
|
|
300
|
+
});
|
|
301
|
+
|
|
302
|
+
expect(getByTestId("copilot-sidebar-drawer")).toBeTruthy();
|
|
303
|
+
});
|
|
304
|
+
|
|
305
|
+
it("close() imperative method closes the drawer", () => {
|
|
306
|
+
const ref = createRef<CopilotSidebarHandle>();
|
|
307
|
+
|
|
308
|
+
const { queryByTestId, getByTestId } = render(
|
|
309
|
+
<CopilotKitProvider runtimeUrl="https://api.test">
|
|
310
|
+
<CopilotSidebar ref={ref} defaultOpen />
|
|
311
|
+
</CopilotKitProvider>,
|
|
312
|
+
);
|
|
313
|
+
|
|
314
|
+
expect(getByTestId("copilot-sidebar-drawer")).toBeTruthy();
|
|
315
|
+
|
|
316
|
+
act(() => {
|
|
317
|
+
ref.current!.close();
|
|
318
|
+
});
|
|
319
|
+
|
|
320
|
+
expect(queryByTestId("copilot-sidebar-drawer")).toBeNull();
|
|
321
|
+
});
|
|
322
|
+
|
|
323
|
+
it("toggle() imperative method toggles the drawer", () => {
|
|
324
|
+
const ref = createRef<CopilotSidebarHandle>();
|
|
325
|
+
|
|
326
|
+
const { queryByTestId, getByTestId } = render(
|
|
327
|
+
<CopilotKitProvider runtimeUrl="https://api.test">
|
|
328
|
+
<CopilotSidebar ref={ref} />
|
|
329
|
+
</CopilotKitProvider>,
|
|
330
|
+
);
|
|
331
|
+
|
|
332
|
+
// Initially closed
|
|
333
|
+
expect(queryByTestId("copilot-sidebar-drawer")).toBeNull();
|
|
334
|
+
|
|
335
|
+
// Toggle open
|
|
336
|
+
act(() => {
|
|
337
|
+
ref.current!.toggle();
|
|
338
|
+
});
|
|
339
|
+
expect(getByTestId("copilot-sidebar-drawer")).toBeTruthy();
|
|
340
|
+
|
|
341
|
+
// Toggle closed
|
|
342
|
+
act(() => {
|
|
343
|
+
ref.current!.toggle();
|
|
344
|
+
});
|
|
345
|
+
expect(queryByTestId("copilot-sidebar-drawer")).toBeNull();
|
|
346
|
+
});
|
|
347
|
+
|
|
348
|
+
it("forwards agentId to CopilotChat", () => {
|
|
349
|
+
render(
|
|
350
|
+
<CopilotKitProvider runtimeUrl="https://api.test">
|
|
351
|
+
<CopilotSidebar agentId="sidebar-agent" defaultOpen />
|
|
352
|
+
</CopilotKitProvider>,
|
|
353
|
+
);
|
|
354
|
+
|
|
355
|
+
expect(capturedAgentId).toBe("sidebar-agent");
|
|
356
|
+
});
|
|
357
|
+
|
|
358
|
+
it("forwards agentName to CopilotChat as deprecated alias", () => {
|
|
359
|
+
render(
|
|
360
|
+
<CopilotKitProvider runtimeUrl="https://api.test">
|
|
361
|
+
<CopilotSidebar agentName="legacy-sidebar" defaultOpen />
|
|
362
|
+
</CopilotKitProvider>,
|
|
363
|
+
);
|
|
364
|
+
|
|
365
|
+
expect(capturedAgentId).toBe("legacy-sidebar");
|
|
366
|
+
});
|
|
367
|
+
|
|
368
|
+
it("agentId takes priority over agentName", () => {
|
|
369
|
+
render(
|
|
370
|
+
<CopilotKitProvider runtimeUrl="https://api.test">
|
|
371
|
+
<CopilotSidebar agentId="primary" agentName="legacy" defaultOpen />
|
|
372
|
+
</CopilotKitProvider>,
|
|
373
|
+
);
|
|
374
|
+
|
|
375
|
+
expect(capturedAgentId).toBe("primary");
|
|
376
|
+
});
|
|
377
|
+
|
|
378
|
+
it("renders custom headerTitle", () => {
|
|
379
|
+
const { getByText } = render(
|
|
380
|
+
<CopilotKitProvider runtimeUrl="https://api.test">
|
|
381
|
+
<CopilotSidebar headerTitle="My Assistant" defaultOpen />
|
|
382
|
+
</CopilotKitProvider>,
|
|
383
|
+
);
|
|
384
|
+
|
|
385
|
+
expect(getByText("My Assistant")).toBeTruthy();
|
|
386
|
+
});
|
|
387
|
+
|
|
388
|
+
it("renders default header title when not specified", () => {
|
|
389
|
+
const { getByText } = render(
|
|
390
|
+
<CopilotKitProvider runtimeUrl="https://api.test">
|
|
391
|
+
<CopilotSidebar defaultOpen />
|
|
392
|
+
</CopilotKitProvider>,
|
|
393
|
+
);
|
|
394
|
+
|
|
395
|
+
expect(getByText("Copilot")).toBeTruthy();
|
|
396
|
+
});
|
|
397
|
+
|
|
398
|
+
it("FAB click opens the drawer", () => {
|
|
399
|
+
const { getByTestId, queryByTestId } = render(
|
|
400
|
+
<CopilotKitProvider runtimeUrl="https://api.test">
|
|
401
|
+
<CopilotSidebar />
|
|
402
|
+
</CopilotKitProvider>,
|
|
403
|
+
);
|
|
404
|
+
|
|
405
|
+
expect(queryByTestId("copilot-sidebar-drawer")).toBeNull();
|
|
406
|
+
|
|
407
|
+
fireEvent.click(getByTestId("copilot-sidebar-fab"));
|
|
408
|
+
|
|
409
|
+
expect(getByTestId("copilot-sidebar-drawer")).toBeTruthy();
|
|
410
|
+
});
|
|
411
|
+
|
|
412
|
+
it("close button click closes the drawer", () => {
|
|
413
|
+
const { getByTestId, queryByTestId } = render(
|
|
414
|
+
<CopilotKitProvider runtimeUrl="https://api.test">
|
|
415
|
+
<CopilotSidebar defaultOpen />
|
|
416
|
+
</CopilotKitProvider>,
|
|
417
|
+
);
|
|
418
|
+
|
|
419
|
+
expect(getByTestId("copilot-sidebar-drawer")).toBeTruthy();
|
|
420
|
+
|
|
421
|
+
fireEvent.click(getByTestId("copilot-sidebar-close"));
|
|
422
|
+
|
|
423
|
+
expect(queryByTestId("copilot-sidebar-drawer")).toBeNull();
|
|
424
|
+
});
|
|
425
|
+
|
|
426
|
+
it("backdrop click closes the drawer", () => {
|
|
427
|
+
const { getByTestId, queryByTestId } = render(
|
|
428
|
+
<CopilotKitProvider runtimeUrl="https://api.test">
|
|
429
|
+
<CopilotSidebar defaultOpen />
|
|
430
|
+
</CopilotKitProvider>,
|
|
431
|
+
);
|
|
432
|
+
|
|
433
|
+
expect(getByTestId("copilot-sidebar-drawer")).toBeTruthy();
|
|
434
|
+
|
|
435
|
+
fireEvent.click(getByTestId("copilot-sidebar-backdrop"));
|
|
436
|
+
|
|
437
|
+
expect(queryByTestId("copilot-sidebar-drawer")).toBeNull();
|
|
438
|
+
});
|
|
439
|
+
|
|
440
|
+
it("renders children inside the drawer", () => {
|
|
441
|
+
const { getByText } = render(
|
|
442
|
+
<CopilotKitProvider runtimeUrl="https://api.test">
|
|
443
|
+
<CopilotSidebar defaultOpen>
|
|
444
|
+
<span>Custom Content</span>
|
|
445
|
+
</CopilotSidebar>
|
|
446
|
+
</CopilotKitProvider>,
|
|
447
|
+
);
|
|
448
|
+
|
|
449
|
+
expect(getByText("Custom Content")).toBeTruthy();
|
|
450
|
+
});
|
|
451
|
+
|
|
452
|
+
it("calls onOpen callback when drawer opens", () => {
|
|
453
|
+
const onOpen = vi.fn();
|
|
454
|
+
const ref = createRef<CopilotSidebarHandle>();
|
|
455
|
+
|
|
456
|
+
render(
|
|
457
|
+
<CopilotKitProvider runtimeUrl="https://api.test">
|
|
458
|
+
<CopilotSidebar ref={ref} onOpen={onOpen} />
|
|
459
|
+
</CopilotKitProvider>,
|
|
460
|
+
);
|
|
461
|
+
|
|
462
|
+
act(() => {
|
|
463
|
+
ref.current!.open();
|
|
464
|
+
});
|
|
465
|
+
|
|
466
|
+
expect(onOpen).toHaveBeenCalledTimes(1);
|
|
467
|
+
});
|
|
468
|
+
|
|
469
|
+
it("calls onClose callback when drawer closes", () => {
|
|
470
|
+
const onClose = vi.fn();
|
|
471
|
+
const ref = createRef<CopilotSidebarHandle>();
|
|
472
|
+
|
|
473
|
+
render(
|
|
474
|
+
<CopilotKitProvider runtimeUrl="https://api.test">
|
|
475
|
+
<CopilotSidebar ref={ref} defaultOpen onClose={onClose} />
|
|
476
|
+
</CopilotKitProvider>,
|
|
477
|
+
);
|
|
478
|
+
|
|
479
|
+
act(() => {
|
|
480
|
+
ref.current!.close();
|
|
481
|
+
});
|
|
482
|
+
|
|
483
|
+
expect(onClose).toHaveBeenCalledTimes(1);
|
|
484
|
+
});
|
|
485
|
+
|
|
486
|
+
describe("attachments prop forwarding", () => {
|
|
487
|
+
it("forwards attachments config to CopilotChat via rest props", () => {
|
|
488
|
+
expect(() => {
|
|
489
|
+
render(
|
|
490
|
+
<CopilotKitProvider runtimeUrl="https://api.test">
|
|
491
|
+
<CopilotSidebar
|
|
492
|
+
defaultOpen={true}
|
|
493
|
+
attachments={{ enabled: true }}
|
|
494
|
+
/>
|
|
495
|
+
</CopilotKitProvider>,
|
|
496
|
+
);
|
|
497
|
+
}).not.toThrow();
|
|
498
|
+
});
|
|
499
|
+
});
|
|
500
|
+
});
|
|
@@ -4,6 +4,58 @@ import { describe, it, expect, beforeEach, afterEach, vi } from "vitest";
|
|
|
4
4
|
|
|
5
5
|
// ─── Mocks ────────────────────────────────────────────────────────────────────
|
|
6
6
|
|
|
7
|
+
// react-native uses Flow syntax that Vitest/Rollup can't parse outside of
|
|
8
|
+
// Metro. Mock it so barrel imports from ../index don't trigger a parse error.
|
|
9
|
+
vi.mock("react-native", () => {
|
|
10
|
+
const _React = require("react");
|
|
11
|
+
const View = ({ children, style, testID }: any) =>
|
|
12
|
+
_React.createElement("div", { "data-testid": testID, style }, children);
|
|
13
|
+
const Text = ({ children, style, testID }: any) =>
|
|
14
|
+
_React.createElement("span", { "data-testid": testID, style }, children);
|
|
15
|
+
const Pressable = ({ children, onPress, testID, style }: any) =>
|
|
16
|
+
_React.createElement(
|
|
17
|
+
"div",
|
|
18
|
+
{ "data-testid": testID, onClick: onPress, style },
|
|
19
|
+
children,
|
|
20
|
+
);
|
|
21
|
+
const TouchableOpacity = ({ children, onPress, testID, style }: any) =>
|
|
22
|
+
_React.createElement(
|
|
23
|
+
"button",
|
|
24
|
+
{ "data-testid": testID, onClick: onPress, style },
|
|
25
|
+
children,
|
|
26
|
+
);
|
|
27
|
+
const Modal = ({ children, visible, testID }: any) => {
|
|
28
|
+
if (!visible) return null;
|
|
29
|
+
return _React.createElement("div", { "data-testid": testID }, children);
|
|
30
|
+
};
|
|
31
|
+
return {
|
|
32
|
+
View,
|
|
33
|
+
Text,
|
|
34
|
+
Pressable,
|
|
35
|
+
TouchableOpacity,
|
|
36
|
+
Modal,
|
|
37
|
+
Animated: {
|
|
38
|
+
View,
|
|
39
|
+
Text,
|
|
40
|
+
createAnimatedComponent: (comp: any) => comp,
|
|
41
|
+
timing: () => ({ start: (cb?: any) => cb?.() }),
|
|
42
|
+
Value: class {
|
|
43
|
+
_value: number;
|
|
44
|
+
constructor(v: number) {
|
|
45
|
+
this._value = v;
|
|
46
|
+
}
|
|
47
|
+
setValue(v: number) {
|
|
48
|
+
this._value = v;
|
|
49
|
+
}
|
|
50
|
+
},
|
|
51
|
+
},
|
|
52
|
+
Dimensions: { get: () => ({ width: 375, height: 812 }) },
|
|
53
|
+
StyleSheet: { create: (styles: any) => styles, hairlineWidth: 1 },
|
|
54
|
+
useWindowDimensions: () => ({ width: 375, height: 812 }),
|
|
55
|
+
Platform: { OS: "ios" },
|
|
56
|
+
};
|
|
57
|
+
});
|
|
58
|
+
|
|
7
59
|
// vi.hoisted runs before vi.mock factories, making these available to both
|
|
8
60
|
const hoisted = vi.hoisted(() => {
|
|
9
61
|
const _React = require("react");
|
|
@@ -28,7 +80,10 @@ function createMockCore() {
|
|
|
28
80
|
setRuntimeUrl: vi.fn(),
|
|
29
81
|
setRuntimeTransport: vi.fn(),
|
|
30
82
|
setHeaders: vi.fn(),
|
|
83
|
+
setCredentials: vi.fn(),
|
|
31
84
|
setProperties: vi.fn(),
|
|
85
|
+
setDebug: vi.fn(),
|
|
86
|
+
setDefaultThrottleMs: vi.fn(),
|
|
32
87
|
getAgent: vi.fn(() => undefined),
|
|
33
88
|
runtimeUrl: "https://api.test",
|
|
34
89
|
runtimeTransport: "auto",
|
|
@@ -374,5 +429,4 @@ describe("Headless integration", () => {
|
|
|
374
429
|
spy.mockRestore();
|
|
375
430
|
});
|
|
376
431
|
});
|
|
377
|
-
|
|
378
432
|
});
|