@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.
@@ -0,0 +1,259 @@
1
+ // packages/react-native/src/__tests__/copilot-chat-attachments.test.tsx
2
+ import React from "react";
3
+ import { render, act } from "@testing-library/react";
4
+ import { describe, it, expect, beforeEach, afterEach, vi } from "vitest";
5
+
6
+ // ─── Mocks ────────────────────────────────────────────────────────────────────
7
+
8
+ // Mock expo modules (useAttachments imports these)
9
+ vi.mock("expo-document-picker", () => ({
10
+ getDocumentAsync: vi.fn().mockResolvedValue({ canceled: true, assets: [] }),
11
+ }));
12
+ vi.mock("expo-file-system", () => ({
13
+ readAsStringAsync: vi.fn().mockResolvedValue("base64data"),
14
+ EncodingType: { Base64: "base64" },
15
+ }));
16
+
17
+ // Mock react-native
18
+ vi.mock("react-native", () => {
19
+ const _React = require("react");
20
+ const View = ({ children, style, testID }: any) =>
21
+ _React.createElement("div", { "data-testid": testID, style }, children);
22
+ const Text = ({ children, style, testID }: any) =>
23
+ _React.createElement("span", { "data-testid": testID, style }, children);
24
+ const Pressable = ({ children, onPress, testID, style }: any) =>
25
+ _React.createElement(
26
+ "div",
27
+ { "data-testid": testID, onClick: onPress, style },
28
+ children,
29
+ );
30
+ return {
31
+ View,
32
+ Text,
33
+ Pressable,
34
+ StyleSheet: { create: (s: any) => s, hairlineWidth: 1 },
35
+ Platform: { OS: "ios" },
36
+ };
37
+ });
38
+
39
+ const hoisted = vi.hoisted(() => {
40
+ const _React = require("react");
41
+ return {
42
+ RealContext: _React.createContext(null),
43
+ MockCoreConstructor: vi.fn(),
44
+ };
45
+ });
46
+
47
+ let mockAgent: any;
48
+ let unsubscribeMock: ReturnType<typeof vi.fn>;
49
+ let mockRunAgent: ReturnType<typeof vi.fn>;
50
+
51
+ function createMockCore() {
52
+ return {
53
+ subscribe: vi.fn((_sub: any) => ({ unsubscribe: unsubscribeMock })),
54
+ subscribeToAgentWithOptions: vi.fn(() => ({ unsubscribe: vi.fn() })),
55
+ setRuntimeUrl: vi.fn(),
56
+ setRuntimeTransport: vi.fn(),
57
+ setHeaders: vi.fn(),
58
+ setCredentials: vi.fn(),
59
+ setProperties: vi.fn(),
60
+ setDebug: vi.fn(),
61
+ setDefaultThrottleMs: vi.fn(),
62
+ getAgent: vi.fn(() => undefined),
63
+ runAgent: mockRunAgent,
64
+ runtimeUrl: "https://api.test",
65
+ runtimeTransport: "auto",
66
+ runtimeConnectionStatus: "Disconnected",
67
+ headers: {},
68
+ agents: {},
69
+ defaultThrottleMs: undefined,
70
+ addTool: vi.fn(),
71
+ removeTool: vi.fn(),
72
+ getTool: vi.fn(() => undefined),
73
+ addHookRenderToolCall: vi.fn(),
74
+ registerThreadStore: vi.fn(),
75
+ unregisterThreadStore: vi.fn(),
76
+ intelligence: undefined,
77
+ };
78
+ }
79
+
80
+ let mockCoreInstance: ReturnType<typeof createMockCore>;
81
+
82
+ vi.mock("@copilotkit/react-core/v2/headless", () => {
83
+ function CopilotKitCoreReact(this: any, ...args: any[]) {
84
+ hoisted.MockCoreConstructor(...args);
85
+ const instance = hoisted.MockCoreConstructor.mock.results.at(-1)?.value;
86
+ if (instance) Object.assign(this, instance);
87
+ }
88
+ return {
89
+ CopilotKitCoreReact,
90
+ useAgent: () => {
91
+ const ctx = require("react").useContext(hoisted.RealContext);
92
+ if (!ctx) {
93
+ throw new Error("useCopilotKit must be used within CopilotKitProvider");
94
+ }
95
+ return { agent: mockAgent };
96
+ },
97
+ useFrontendTool: () => {},
98
+ useComponent: () => {},
99
+ useHumanInTheLoop: () => {},
100
+ useInterrupt: () => {},
101
+ useSuggestions: () => {},
102
+ useConfigureSuggestions: () => {},
103
+ useAgentContext: () => {},
104
+ useThreads: () => ({
105
+ threads: [],
106
+ isLoading: false,
107
+ error: null,
108
+ hasMoreThreads: false,
109
+ isFetchingMoreThreads: false,
110
+ fetchMoreThreads: () => {},
111
+ renameThread: async () => {},
112
+ archiveThread: async () => {},
113
+ deleteThread: async () => {},
114
+ }),
115
+ CopilotChatConfigurationProvider: ({ children }: any) => children,
116
+ useCopilotChatConfiguration: () => null,
117
+ CopilotChatDefaultLabels: {},
118
+ };
119
+ });
120
+
121
+ vi.mock("@copilotkit/react-core/v2/context", () => {
122
+ const _React = require("react");
123
+ return {
124
+ CopilotKitContext: hoisted.RealContext,
125
+ LicenseContext: _React.createContext({
126
+ status: null,
127
+ license: null,
128
+ checkFeature: () => true,
129
+ getLimit: () => null,
130
+ }),
131
+ useCopilotKit: () => {
132
+ const ctx = _React.useContext(hoisted.RealContext);
133
+ if (!ctx) {
134
+ throw new Error("useCopilotKit must be used within CopilotKitProvider");
135
+ }
136
+ return { copilotkit: ctx };
137
+ },
138
+ useLicenseContext: () => ({
139
+ status: null,
140
+ license: null,
141
+ checkFeature: () => true,
142
+ getLimit: () => null,
143
+ }),
144
+ };
145
+ });
146
+
147
+ vi.mock("@copilotkit/shared", () => ({
148
+ DEFAULT_AGENT_ID: "default",
149
+ randomUUID: () => "test-uuid-" + Math.random().toString(36).slice(2, 8),
150
+ getModalityFromMimeType: (mimeType: string) => {
151
+ if (mimeType.startsWith("image/")) return "image";
152
+ if (mimeType.startsWith("audio/")) return "audio";
153
+ if (mimeType.startsWith("video/")) return "video";
154
+ return "document";
155
+ },
156
+ formatFileSize: (bytes: number) => `${bytes} B`,
157
+ createLicenseContextValue: () => ({
158
+ status: null,
159
+ license: null,
160
+ checkFeature: () => true,
161
+ getLimit: () => null,
162
+ }),
163
+ }));
164
+
165
+ import { CopilotKitProvider } from "../CopilotKitProvider";
166
+ import { CopilotChat, useCopilotChatContext } from "../CopilotChat";
167
+
168
+ // ─── Tests ────────────────────────────────────────────────────────────────────
169
+
170
+ describe("CopilotChat attachments integration", () => {
171
+ beforeEach(() => {
172
+ unsubscribeMock = vi.fn();
173
+ mockRunAgent = vi.fn().mockResolvedValue(undefined);
174
+ mockAgent = {
175
+ addMessage: vi.fn(),
176
+ isRunning: false,
177
+ messages: [],
178
+ threadId: undefined,
179
+ };
180
+ mockCoreInstance = createMockCore();
181
+ hoisted.MockCoreConstructor.mockClear();
182
+ hoisted.MockCoreConstructor.mockReturnValue(mockCoreInstance);
183
+ });
184
+
185
+ afterEach(() => {
186
+ vi.clearAllMocks();
187
+ });
188
+
189
+ it("exposes attachment state via useCopilotChatContext", () => {
190
+ let ctx: any = null;
191
+
192
+ function Consumer() {
193
+ ctx = useCopilotChatContext();
194
+ return null;
195
+ }
196
+
197
+ render(
198
+ <CopilotKitProvider runtimeUrl="https://api.test">
199
+ <CopilotChat attachments={{ enabled: true }}>
200
+ <Consumer />
201
+ </CopilotChat>
202
+ </CopilotKitProvider>,
203
+ );
204
+
205
+ expect(ctx).not.toBeNull();
206
+ expect(ctx.attachments).toEqual([]);
207
+ expect(typeof ctx.openPicker).toBe("function");
208
+ expect(typeof ctx.removeAttachment).toBe("function");
209
+ expect(typeof ctx.submitMessage).toBe("function");
210
+ expect(ctx.attachmentsEnabled).toBe(true);
211
+ });
212
+
213
+ it("does not expose attachment functions when attachments not configured", () => {
214
+ let ctx: any = null;
215
+
216
+ function Consumer() {
217
+ ctx = useCopilotChatContext();
218
+ return null;
219
+ }
220
+
221
+ render(
222
+ <CopilotKitProvider runtimeUrl="https://api.test">
223
+ <CopilotChat>
224
+ <Consumer />
225
+ </CopilotChat>
226
+ </CopilotKitProvider>,
227
+ );
228
+
229
+ expect(ctx.attachmentsEnabled).toBe(false);
230
+ });
231
+
232
+ it("submitMessage sends plain text when no attachments", async () => {
233
+ let ctx: any = null;
234
+
235
+ function Consumer() {
236
+ ctx = useCopilotChatContext();
237
+ return null;
238
+ }
239
+
240
+ render(
241
+ <CopilotKitProvider runtimeUrl="https://api.test">
242
+ <CopilotChat attachments={{ enabled: true }}>
243
+ <Consumer />
244
+ </CopilotChat>
245
+ </CopilotKitProvider>,
246
+ );
247
+
248
+ await act(async () => {
249
+ await ctx.submitMessage("Hello world");
250
+ });
251
+
252
+ expect(mockAgent.addMessage).toHaveBeenCalledWith(
253
+ expect.objectContaining({
254
+ role: "user",
255
+ content: "Hello world",
256
+ }),
257
+ );
258
+ });
259
+ });