@copilotkit/react-native 1.57.0-canary.1778116520
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/LICENSE +21 -0
- package/README.md +54 -0
- package/dist/index.cjs +203 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +51 -0
- package/dist/index.d.mts +51 -0
- package/dist/index.mjs +101 -0
- package/dist/index.mjs.map +1 -0
- package/dist/polyfills/crypto.cjs +15 -0
- package/dist/polyfills/crypto.cjs.map +1 -0
- package/dist/polyfills/crypto.d.cts +1 -0
- package/dist/polyfills/crypto.d.mts +1 -0
- package/dist/polyfills/crypto.mjs +15 -0
- package/dist/polyfills/crypto.mjs.map +1 -0
- package/dist/polyfills/dom.cjs +59 -0
- package/dist/polyfills/dom.cjs.map +1 -0
- package/dist/polyfills/dom.d.cts +1 -0
- package/dist/polyfills/dom.d.mts +1 -0
- package/dist/polyfills/dom.mjs +59 -0
- package/dist/polyfills/dom.mjs.map +1 -0
- package/dist/polyfills/encoding.cjs +19 -0
- package/dist/polyfills/encoding.cjs.map +1 -0
- package/dist/polyfills/encoding.d.cts +1 -0
- package/dist/polyfills/encoding.d.mts +1 -0
- package/dist/polyfills/encoding.mjs +19 -0
- package/dist/polyfills/encoding.mjs.map +1 -0
- package/dist/polyfills/location.cjs +18 -0
- package/dist/polyfills/location.cjs.map +1 -0
- package/dist/polyfills/location.d.cts +1 -0
- package/dist/polyfills/location.d.mts +1 -0
- package/dist/polyfills/location.mjs +18 -0
- package/dist/polyfills/location.mjs.map +1 -0
- package/dist/polyfills/streams.cjs +20 -0
- package/dist/polyfills/streams.cjs.map +1 -0
- package/dist/polyfills/streams.d.cts +1 -0
- package/dist/polyfills/streams.d.mts +1 -0
- package/dist/polyfills/streams.mjs +20 -0
- package/dist/polyfills/streams.mjs.map +1 -0
- package/dist/polyfills.cjs +196 -0
- package/dist/polyfills.cjs.map +1 -0
- package/dist/polyfills.d.cts +1 -0
- package/dist/polyfills.d.mts +1 -0
- package/dist/polyfills.mjs +196 -0
- package/dist/polyfills.mjs.map +1 -0
- package/package.json +92 -0
- package/project.json +18 -0
- package/src/CopilotKitProvider.tsx +188 -0
- package/src/__tests__/copilotkit-provider.test.tsx +424 -0
- package/src/__tests__/headless-integration.test.tsx +378 -0
- package/src/__tests__/polyfills.test.ts +242 -0
- package/src/__tests__/setup.ts +9 -0
- package/src/__tests__/streaming-fetch.test.ts +486 -0
- package/src/index.ts +44 -0
- package/src/polyfills/crypto.ts +34 -0
- package/src/polyfills/dom.ts +85 -0
- package/src/polyfills/encoding.ts +20 -0
- package/src/polyfills/location.ts +30 -0
- package/src/polyfills/streams.ts +27 -0
- package/src/polyfills.ts +22 -0
- package/src/streaming-fetch.ts +336 -0
- package/src/types.d.ts +23 -0
- package/tsconfig.json +16 -0
- package/tsdown.config.ts +25 -0
- package/vitest.config.mjs +17 -0
|
@@ -0,0 +1,378 @@
|
|
|
1
|
+
import React from "react";
|
|
2
|
+
import { render } from "@testing-library/react";
|
|
3
|
+
import { describe, it, expect, beforeEach, afterEach, vi } from "vitest";
|
|
4
|
+
|
|
5
|
+
// ─── Mocks ────────────────────────────────────────────────────────────────────
|
|
6
|
+
|
|
7
|
+
// vi.hoisted runs before vi.mock factories, making these available to both
|
|
8
|
+
const hoisted = vi.hoisted(() => {
|
|
9
|
+
const _React = require("react");
|
|
10
|
+
return {
|
|
11
|
+
RealContext: _React.createContext(null),
|
|
12
|
+
MockCoreConstructor: vi.fn(),
|
|
13
|
+
};
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
let _capturedSubscriber: Record<string, (...args: any[]) => void>;
|
|
17
|
+
let unsubscribeMock: ReturnType<typeof vi.fn>;
|
|
18
|
+
|
|
19
|
+
function createMockCore() {
|
|
20
|
+
return {
|
|
21
|
+
subscribe: vi.fn((subscriber: any) => {
|
|
22
|
+
_capturedSubscriber = subscriber;
|
|
23
|
+
return { 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
|
+
setProperties: vi.fn(),
|
|
32
|
+
getAgent: vi.fn(() => undefined),
|
|
33
|
+
runtimeUrl: "https://api.test",
|
|
34
|
+
runtimeTransport: "auto",
|
|
35
|
+
runtimeConnectionStatus: "Disconnected",
|
|
36
|
+
headers: {},
|
|
37
|
+
agents: {},
|
|
38
|
+
defaultThrottleMs: undefined,
|
|
39
|
+
addTool: vi.fn(),
|
|
40
|
+
removeTool: vi.fn(),
|
|
41
|
+
getTool: vi.fn(() => undefined),
|
|
42
|
+
addHookRenderToolCall: vi.fn(),
|
|
43
|
+
registerThreadStore: vi.fn(),
|
|
44
|
+
unregisterThreadStore: vi.fn(),
|
|
45
|
+
intelligence: undefined,
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
let mockCoreInstance: ReturnType<typeof createMockCore>;
|
|
50
|
+
|
|
51
|
+
vi.mock("@copilotkit/react-core/v2/headless", () => {
|
|
52
|
+
// Regular function (not arrow) so it's new-able
|
|
53
|
+
function CopilotKitCoreReact(this: any, ...args: any[]) {
|
|
54
|
+
hoisted.MockCoreConstructor(...args);
|
|
55
|
+
const instance = hoisted.MockCoreConstructor.mock.results.at(-1)?.value;
|
|
56
|
+
if (instance) Object.assign(this, instance);
|
|
57
|
+
}
|
|
58
|
+
return {
|
|
59
|
+
CopilotKitCoreReact,
|
|
60
|
+
useAgent: () => {
|
|
61
|
+
const ctx = require("react").useContext(hoisted.RealContext);
|
|
62
|
+
if (!ctx) {
|
|
63
|
+
throw new Error("useCopilotKit must be used within CopilotKitProvider");
|
|
64
|
+
}
|
|
65
|
+
return { agent: {} };
|
|
66
|
+
},
|
|
67
|
+
useFrontendTool: (_tool: any) => {
|
|
68
|
+
const ctx = require("react").useContext(hoisted.RealContext);
|
|
69
|
+
if (!ctx) {
|
|
70
|
+
throw new Error("useCopilotKit must be used within CopilotKitProvider");
|
|
71
|
+
}
|
|
72
|
+
},
|
|
73
|
+
useComponent: () => {
|
|
74
|
+
const ctx = require("react").useContext(hoisted.RealContext);
|
|
75
|
+
if (!ctx) {
|
|
76
|
+
throw new Error("useCopilotKit must be used within CopilotKitProvider");
|
|
77
|
+
}
|
|
78
|
+
},
|
|
79
|
+
useHumanInTheLoop: () => {
|
|
80
|
+
const ctx = require("react").useContext(hoisted.RealContext);
|
|
81
|
+
if (!ctx) {
|
|
82
|
+
throw new Error("useCopilotKit must be used within CopilotKitProvider");
|
|
83
|
+
}
|
|
84
|
+
},
|
|
85
|
+
useInterrupt: () => {
|
|
86
|
+
const ctx = require("react").useContext(hoisted.RealContext);
|
|
87
|
+
if (!ctx) {
|
|
88
|
+
throw new Error("useCopilotKit must be used within CopilotKitProvider");
|
|
89
|
+
}
|
|
90
|
+
},
|
|
91
|
+
useSuggestions: () => {
|
|
92
|
+
const ctx = require("react").useContext(hoisted.RealContext);
|
|
93
|
+
if (!ctx) {
|
|
94
|
+
throw new Error("useCopilotKit must be used within CopilotKitProvider");
|
|
95
|
+
}
|
|
96
|
+
},
|
|
97
|
+
useConfigureSuggestions: () => {
|
|
98
|
+
const ctx = require("react").useContext(hoisted.RealContext);
|
|
99
|
+
if (!ctx) {
|
|
100
|
+
throw new Error("useCopilotKit must be used within CopilotKitProvider");
|
|
101
|
+
}
|
|
102
|
+
},
|
|
103
|
+
useAgentContext: () => {
|
|
104
|
+
const ctx = require("react").useContext(hoisted.RealContext);
|
|
105
|
+
if (!ctx) {
|
|
106
|
+
throw new Error("useCopilotKit must be used within CopilotKitProvider");
|
|
107
|
+
}
|
|
108
|
+
},
|
|
109
|
+
useThreads: (_input: any) => {
|
|
110
|
+
const ctx = require("react").useContext(hoisted.RealContext);
|
|
111
|
+
if (!ctx) {
|
|
112
|
+
throw new Error("useCopilotKit must be used within CopilotKitProvider");
|
|
113
|
+
}
|
|
114
|
+
return {
|
|
115
|
+
threads: [],
|
|
116
|
+
isLoading: false,
|
|
117
|
+
error: null,
|
|
118
|
+
hasMoreThreads: false,
|
|
119
|
+
isFetchingMoreThreads: false,
|
|
120
|
+
fetchMoreThreads: () => {},
|
|
121
|
+
renameThread: async () => {},
|
|
122
|
+
archiveThread: async () => {},
|
|
123
|
+
deleteThread: async () => {},
|
|
124
|
+
};
|
|
125
|
+
},
|
|
126
|
+
CopilotChatConfigurationProvider: ({ children }: any) => children,
|
|
127
|
+
useCopilotChatConfiguration: () => null,
|
|
128
|
+
CopilotChatDefaultLabels: {},
|
|
129
|
+
};
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
vi.mock("@copilotkit/react-core/v2/context", () => {
|
|
133
|
+
const _React = require("react");
|
|
134
|
+
return {
|
|
135
|
+
CopilotKitContext: hoisted.RealContext,
|
|
136
|
+
LicenseContext: _React.createContext({
|
|
137
|
+
status: null,
|
|
138
|
+
license: null,
|
|
139
|
+
checkFeature: () => true,
|
|
140
|
+
getLimit: () => null,
|
|
141
|
+
}),
|
|
142
|
+
useCopilotKit: () => {
|
|
143
|
+
const ctx = _React.useContext(hoisted.RealContext);
|
|
144
|
+
if (!ctx) {
|
|
145
|
+
throw new Error("useCopilotKit must be used within CopilotKitProvider");
|
|
146
|
+
}
|
|
147
|
+
return ctx;
|
|
148
|
+
},
|
|
149
|
+
useLicenseContext: () => ({
|
|
150
|
+
status: null,
|
|
151
|
+
license: null,
|
|
152
|
+
checkFeature: () => true,
|
|
153
|
+
getLimit: () => null,
|
|
154
|
+
}),
|
|
155
|
+
};
|
|
156
|
+
});
|
|
157
|
+
|
|
158
|
+
vi.mock("@copilotkit/shared", () => ({
|
|
159
|
+
createLicenseContextValue: () => ({
|
|
160
|
+
status: null,
|
|
161
|
+
license: null,
|
|
162
|
+
checkFeature: () => true,
|
|
163
|
+
getLimit: () => null,
|
|
164
|
+
}),
|
|
165
|
+
}));
|
|
166
|
+
|
|
167
|
+
// Import after mocks
|
|
168
|
+
import { CopilotKitProvider } from "../CopilotKitProvider";
|
|
169
|
+
import {
|
|
170
|
+
useAgent,
|
|
171
|
+
useFrontendTool,
|
|
172
|
+
useHumanInTheLoop,
|
|
173
|
+
useInterrupt,
|
|
174
|
+
useThreads,
|
|
175
|
+
} from "../index";
|
|
176
|
+
|
|
177
|
+
// ─── Tests ────────────────────────────────────────────────────────────────────
|
|
178
|
+
|
|
179
|
+
describe("Headless integration", () => {
|
|
180
|
+
beforeEach(() => {
|
|
181
|
+
unsubscribeMock = vi.fn();
|
|
182
|
+
mockCoreInstance = createMockCore();
|
|
183
|
+
hoisted.MockCoreConstructor.mockClear();
|
|
184
|
+
hoisted.MockCoreConstructor.mockReturnValue(mockCoreInstance);
|
|
185
|
+
});
|
|
186
|
+
|
|
187
|
+
afterEach(() => {
|
|
188
|
+
vi.clearAllMocks();
|
|
189
|
+
});
|
|
190
|
+
|
|
191
|
+
// ── Provider + hooks integration ──────────────────────────────────────
|
|
192
|
+
|
|
193
|
+
describe("provider + hooks integration", () => {
|
|
194
|
+
it("useAgent returns expected shape when called inside provider", () => {
|
|
195
|
+
let result: any = null;
|
|
196
|
+
|
|
197
|
+
function TestComponent() {
|
|
198
|
+
result = useAgent();
|
|
199
|
+
return null;
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
render(
|
|
203
|
+
<CopilotKitProvider runtimeUrl="https://api.test">
|
|
204
|
+
<TestComponent />
|
|
205
|
+
</CopilotKitProvider>,
|
|
206
|
+
);
|
|
207
|
+
|
|
208
|
+
expect(result).not.toBeNull();
|
|
209
|
+
expect(result).toHaveProperty("agent");
|
|
210
|
+
expect(typeof result.agent).toBe("object");
|
|
211
|
+
});
|
|
212
|
+
|
|
213
|
+
it("useFrontendTool can register a tool without error", () => {
|
|
214
|
+
function TestComponent() {
|
|
215
|
+
useFrontendTool({
|
|
216
|
+
name: "test-tool",
|
|
217
|
+
description: "A test tool",
|
|
218
|
+
parameters: {},
|
|
219
|
+
handler: async () => "done",
|
|
220
|
+
});
|
|
221
|
+
return null;
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
// Should not throw
|
|
225
|
+
expect(() => {
|
|
226
|
+
render(
|
|
227
|
+
<CopilotKitProvider runtimeUrl="https://api.test">
|
|
228
|
+
<TestComponent />
|
|
229
|
+
</CopilotKitProvider>,
|
|
230
|
+
);
|
|
231
|
+
}).not.toThrow();
|
|
232
|
+
});
|
|
233
|
+
|
|
234
|
+
it("useThreads returns thread state when called inside provider", () => {
|
|
235
|
+
let result: any = null;
|
|
236
|
+
|
|
237
|
+
function TestComponent() {
|
|
238
|
+
result = useThreads({ agentId: "test-agent" });
|
|
239
|
+
return null;
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
render(
|
|
243
|
+
<CopilotKitProvider runtimeUrl="https://api.test">
|
|
244
|
+
<TestComponent />
|
|
245
|
+
</CopilotKitProvider>,
|
|
246
|
+
);
|
|
247
|
+
|
|
248
|
+
expect(result).not.toBeNull();
|
|
249
|
+
expect(result).toHaveProperty("threads");
|
|
250
|
+
expect(result).toHaveProperty("isLoading");
|
|
251
|
+
expect(result).toHaveProperty("error");
|
|
252
|
+
expect(result).toHaveProperty("renameThread");
|
|
253
|
+
expect(result).toHaveProperty("archiveThread");
|
|
254
|
+
expect(result).toHaveProperty("deleteThread");
|
|
255
|
+
expect(result).toHaveProperty("hasMoreThreads");
|
|
256
|
+
expect(result).toHaveProperty("isFetchingMoreThreads");
|
|
257
|
+
expect(result).toHaveProperty("fetchMoreThreads");
|
|
258
|
+
expect(Array.isArray(result.threads)).toBe(true);
|
|
259
|
+
expect(typeof result.renameThread).toBe("function");
|
|
260
|
+
expect(typeof result.archiveThread).toBe("function");
|
|
261
|
+
expect(typeof result.deleteThread).toBe("function");
|
|
262
|
+
});
|
|
263
|
+
|
|
264
|
+
it("multiple hooks can coexist in the same provider tree", () => {
|
|
265
|
+
let agentResult: any = null;
|
|
266
|
+
let threadsResult: any = null;
|
|
267
|
+
|
|
268
|
+
function TestComponent() {
|
|
269
|
+
agentResult = useAgent();
|
|
270
|
+
threadsResult = useThreads({ agentId: "test-agent" });
|
|
271
|
+
useFrontendTool({
|
|
272
|
+
name: "multi-tool",
|
|
273
|
+
description: "Another tool",
|
|
274
|
+
parameters: {},
|
|
275
|
+
handler: async () => "ok",
|
|
276
|
+
});
|
|
277
|
+
return null;
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
expect(() => {
|
|
281
|
+
render(
|
|
282
|
+
<CopilotKitProvider runtimeUrl="https://api.test">
|
|
283
|
+
<TestComponent />
|
|
284
|
+
</CopilotKitProvider>,
|
|
285
|
+
);
|
|
286
|
+
}).not.toThrow();
|
|
287
|
+
|
|
288
|
+
expect(agentResult).toHaveProperty("agent");
|
|
289
|
+
expect(threadsResult).toHaveProperty("threads");
|
|
290
|
+
});
|
|
291
|
+
});
|
|
292
|
+
|
|
293
|
+
// ── Provider error boundary ───────────────────────────────────────────
|
|
294
|
+
|
|
295
|
+
describe("provider error boundary", () => {
|
|
296
|
+
it("useAgent throws when called outside CopilotKitProvider", () => {
|
|
297
|
+
function TestComponent() {
|
|
298
|
+
useAgent();
|
|
299
|
+
return null;
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
// Suppress React error boundary console output
|
|
303
|
+
const spy = vi.spyOn(console, "error").mockImplementation(() => {});
|
|
304
|
+
|
|
305
|
+
expect(() => {
|
|
306
|
+
render(<TestComponent />);
|
|
307
|
+
}).toThrow("useCopilotKit must be used within CopilotKitProvider");
|
|
308
|
+
|
|
309
|
+
spy.mockRestore();
|
|
310
|
+
});
|
|
311
|
+
|
|
312
|
+
it("useFrontendTool throws when called outside CopilotKitProvider", () => {
|
|
313
|
+
function TestComponent() {
|
|
314
|
+
useFrontendTool({
|
|
315
|
+
name: "orphan-tool",
|
|
316
|
+
description: "No provider",
|
|
317
|
+
parameters: {},
|
|
318
|
+
handler: async () => "fail",
|
|
319
|
+
});
|
|
320
|
+
return null;
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
const spy = vi.spyOn(console, "error").mockImplementation(() => {});
|
|
324
|
+
|
|
325
|
+
expect(() => {
|
|
326
|
+
render(<TestComponent />);
|
|
327
|
+
}).toThrow("useCopilotKit must be used within CopilotKitProvider");
|
|
328
|
+
|
|
329
|
+
spy.mockRestore();
|
|
330
|
+
});
|
|
331
|
+
|
|
332
|
+
it("useThreads throws when called outside CopilotKitProvider", () => {
|
|
333
|
+
function TestComponent() {
|
|
334
|
+
useThreads({ agentId: "test-agent" });
|
|
335
|
+
return null;
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
const spy = vi.spyOn(console, "error").mockImplementation(() => {});
|
|
339
|
+
|
|
340
|
+
expect(() => {
|
|
341
|
+
render(<TestComponent />);
|
|
342
|
+
}).toThrow("useCopilotKit must be used within CopilotKitProvider");
|
|
343
|
+
|
|
344
|
+
spy.mockRestore();
|
|
345
|
+
});
|
|
346
|
+
|
|
347
|
+
it("useHumanInTheLoop throws when called outside CopilotKitProvider", () => {
|
|
348
|
+
function TestComponent() {
|
|
349
|
+
useHumanInTheLoop();
|
|
350
|
+
return null;
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
const spy = vi.spyOn(console, "error").mockImplementation(() => {});
|
|
354
|
+
|
|
355
|
+
expect(() => {
|
|
356
|
+
render(<TestComponent />);
|
|
357
|
+
}).toThrow("useCopilotKit must be used within CopilotKitProvider");
|
|
358
|
+
|
|
359
|
+
spy.mockRestore();
|
|
360
|
+
});
|
|
361
|
+
|
|
362
|
+
it("useInterrupt throws when called outside CopilotKitProvider", () => {
|
|
363
|
+
function TestComponent() {
|
|
364
|
+
useInterrupt();
|
|
365
|
+
return null;
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
const spy = vi.spyOn(console, "error").mockImplementation(() => {});
|
|
369
|
+
|
|
370
|
+
expect(() => {
|
|
371
|
+
render(<TestComponent />);
|
|
372
|
+
}).toThrow("useCopilotKit must be used within CopilotKitProvider");
|
|
373
|
+
|
|
374
|
+
spy.mockRestore();
|
|
375
|
+
});
|
|
376
|
+
});
|
|
377
|
+
|
|
378
|
+
});
|
|
@@ -0,0 +1,242 @@
|
|
|
1
|
+
import { describe, it, expect, beforeEach, afterEach, vi } from "vitest";
|
|
2
|
+
|
|
3
|
+
// Mock streaming-fetch to isolate polyfill tests
|
|
4
|
+
vi.mock("../streaming-fetch", () => ({
|
|
5
|
+
installStreamingFetch: vi.fn(),
|
|
6
|
+
}));
|
|
7
|
+
|
|
8
|
+
// ─── Global save/restore ──────────────────────────────────────────────────────
|
|
9
|
+
|
|
10
|
+
const savedGlobals: Record<string, any> = {};
|
|
11
|
+
|
|
12
|
+
function saveGlobal(name: string) {
|
|
13
|
+
savedGlobals[name] = (globalThis as any)[name];
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
function deleteGlobal(name: string) {
|
|
17
|
+
saveGlobal(name);
|
|
18
|
+
delete (globalThis as any)[name];
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function restoreGlobals() {
|
|
22
|
+
for (const [name, value] of Object.entries(savedGlobals)) {
|
|
23
|
+
if (value === undefined) {
|
|
24
|
+
delete (globalThis as any)[name];
|
|
25
|
+
} else {
|
|
26
|
+
(globalThis as any)[name] = value;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
// Clear the saved state
|
|
30
|
+
for (const key of Object.keys(savedGlobals)) {
|
|
31
|
+
delete savedGlobals[key];
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
async function importPolyfills() {
|
|
36
|
+
vi.resetModules();
|
|
37
|
+
await import("../polyfills");
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
// ─── Tests ────────────────────────────────────────────────────────────────────
|
|
41
|
+
|
|
42
|
+
describe("polyfills", () => {
|
|
43
|
+
afterEach(() => {
|
|
44
|
+
restoreGlobals();
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
// ── ReadableStream / WritableStream / TransformStream ───────────────────
|
|
48
|
+
|
|
49
|
+
describe("ReadableStream / WritableStream / TransformStream", () => {
|
|
50
|
+
it("installs when missing", async () => {
|
|
51
|
+
deleteGlobal("ReadableStream");
|
|
52
|
+
deleteGlobal("WritableStream");
|
|
53
|
+
deleteGlobal("TransformStream");
|
|
54
|
+
await importPolyfills();
|
|
55
|
+
expect(typeof globalThis.ReadableStream).toBe("function");
|
|
56
|
+
expect(typeof (globalThis as any).WritableStream).toBe("function");
|
|
57
|
+
expect(typeof (globalThis as any).TransformStream).toBe("function");
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
it("preserves existing ReadableStream", async () => {
|
|
61
|
+
const sentinel = function Sentinel() {};
|
|
62
|
+
saveGlobal("ReadableStream");
|
|
63
|
+
(globalThis as any).ReadableStream = sentinel;
|
|
64
|
+
await importPolyfills();
|
|
65
|
+
expect((globalThis as any).ReadableStream).toBe(sentinel);
|
|
66
|
+
});
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
// ── TextEncoder / TextDecoder ───────────────────────────────────────────
|
|
70
|
+
|
|
71
|
+
describe("TextEncoder / TextDecoder", () => {
|
|
72
|
+
it("installs TextEncoder when missing", async () => {
|
|
73
|
+
deleteGlobal("TextEncoder");
|
|
74
|
+
await importPolyfills();
|
|
75
|
+
expect(typeof globalThis.TextEncoder).toBe("function");
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
it("installs TextDecoder when missing", async () => {
|
|
79
|
+
deleteGlobal("TextDecoder");
|
|
80
|
+
await importPolyfills();
|
|
81
|
+
expect(typeof globalThis.TextDecoder).toBe("function");
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
it("preserves existing TextEncoder", async () => {
|
|
85
|
+
const sentinel = function Sentinel() {};
|
|
86
|
+
saveGlobal("TextEncoder");
|
|
87
|
+
(globalThis as any).TextEncoder = sentinel;
|
|
88
|
+
await importPolyfills();
|
|
89
|
+
expect((globalThis as any).TextEncoder).toBe(sentinel);
|
|
90
|
+
});
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
// ── crypto.getRandomValues ──────────────────────────────────────────────
|
|
94
|
+
|
|
95
|
+
describe("crypto.getRandomValues", () => {
|
|
96
|
+
it("installs when crypto is undefined", async () => {
|
|
97
|
+
deleteGlobal("crypto");
|
|
98
|
+
vi.spyOn(console, "warn").mockImplementation(() => {});
|
|
99
|
+
await importPolyfills();
|
|
100
|
+
expect(typeof globalThis.crypto.getRandomValues).toBe("function");
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
it("installs when crypto exists but getRandomValues is missing", async () => {
|
|
104
|
+
saveGlobal("crypto");
|
|
105
|
+
(globalThis as any).crypto = {};
|
|
106
|
+
vi.spyOn(console, "warn").mockImplementation(() => {});
|
|
107
|
+
await importPolyfills();
|
|
108
|
+
expect(typeof globalThis.crypto.getRandomValues).toBe("function");
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
it("preserves existing getRandomValues", async () => {
|
|
112
|
+
const sentinel = vi.fn();
|
|
113
|
+
saveGlobal("crypto");
|
|
114
|
+
(globalThis as any).crypto = { getRandomValues: sentinel };
|
|
115
|
+
await importPolyfills();
|
|
116
|
+
expect(globalThis.crypto.getRandomValues).toBe(sentinel);
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
it("produces a filled Uint8Array with values in [0, 255]", async () => {
|
|
120
|
+
deleteGlobal("crypto");
|
|
121
|
+
vi.spyOn(console, "warn").mockImplementation(() => {});
|
|
122
|
+
await importPolyfills();
|
|
123
|
+
|
|
124
|
+
const arr = new Uint8Array(32);
|
|
125
|
+
const result = globalThis.crypto.getRandomValues(arr);
|
|
126
|
+
expect(result).toBe(arr);
|
|
127
|
+
// With 32 random bytes, at least some should be non-zero
|
|
128
|
+
expect(arr.some((v) => v > 0)).toBe(true);
|
|
129
|
+
expect(arr.every((v) => v >= 0 && v <= 255)).toBe(true);
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
it("logs a security warning", async () => {
|
|
133
|
+
deleteGlobal("crypto");
|
|
134
|
+
const warnSpy = vi.spyOn(console, "warn").mockImplementation(() => {});
|
|
135
|
+
await importPolyfills();
|
|
136
|
+
expect(warnSpy).toHaveBeenCalledWith(
|
|
137
|
+
expect.stringContaining("non-cryptographic"),
|
|
138
|
+
);
|
|
139
|
+
});
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
// ── DOMException ────────────────────────────────────────────────────────
|
|
143
|
+
|
|
144
|
+
describe("DOMException", () => {
|
|
145
|
+
it("installs when missing", async () => {
|
|
146
|
+
deleteGlobal("DOMException");
|
|
147
|
+
await importPolyfills();
|
|
148
|
+
expect(typeof (globalThis as any).DOMException).toBe("function");
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
it("polyfill supports name parameter (e.g. AbortError)", async () => {
|
|
152
|
+
deleteGlobal("DOMException");
|
|
153
|
+
await importPolyfills();
|
|
154
|
+
const err = new (globalThis as any).DOMException("msg", "AbortError");
|
|
155
|
+
expect(err).toBeInstanceOf(Error);
|
|
156
|
+
expect(err.name).toBe("AbortError");
|
|
157
|
+
expect(err.message).toBe("msg");
|
|
158
|
+
});
|
|
159
|
+
|
|
160
|
+
it("preserves existing DOMException", async () => {
|
|
161
|
+
const sentinel = function Sentinel() {};
|
|
162
|
+
saveGlobal("DOMException");
|
|
163
|
+
(globalThis as any).DOMException = sentinel;
|
|
164
|
+
await importPolyfills();
|
|
165
|
+
expect((globalThis as any).DOMException).toBe(sentinel);
|
|
166
|
+
});
|
|
167
|
+
});
|
|
168
|
+
|
|
169
|
+
// ── Headers ─────────────────────────────────────────────────────────────
|
|
170
|
+
|
|
171
|
+
describe("Headers polyfill", () => {
|
|
172
|
+
it("installs when missing", async () => {
|
|
173
|
+
deleteGlobal("Headers");
|
|
174
|
+
await importPolyfills();
|
|
175
|
+
expect(typeof (globalThis as any).Headers).toBe("function");
|
|
176
|
+
});
|
|
177
|
+
|
|
178
|
+
it("supports get/set/has/delete/append", async () => {
|
|
179
|
+
deleteGlobal("Headers");
|
|
180
|
+
await importPolyfills();
|
|
181
|
+
const h = new Headers();
|
|
182
|
+
h.set("X-Key", "val1");
|
|
183
|
+
expect(h.get("x-key")).toBe("val1");
|
|
184
|
+
expect(h.has("x-key")).toBe(true);
|
|
185
|
+
h.append("x-key", "val2");
|
|
186
|
+
expect(h.get("x-key")).toBe("val1, val2");
|
|
187
|
+
h.delete("x-key");
|
|
188
|
+
expect(h.has("x-key")).toBe(false);
|
|
189
|
+
});
|
|
190
|
+
|
|
191
|
+
it("normalizes header names to lowercase", async () => {
|
|
192
|
+
deleteGlobal("Headers");
|
|
193
|
+
await importPolyfills();
|
|
194
|
+
const h = new Headers({ "Content-Type": "text/html" });
|
|
195
|
+
expect(h.get("content-type")).toBe("text/html");
|
|
196
|
+
});
|
|
197
|
+
|
|
198
|
+
it("is iterable", async () => {
|
|
199
|
+
deleteGlobal("Headers");
|
|
200
|
+
await importPolyfills();
|
|
201
|
+
const h = new Headers({ a: "1", b: "2" });
|
|
202
|
+
const entries = [...h];
|
|
203
|
+
expect(entries).toEqual(
|
|
204
|
+
expect.arrayContaining([
|
|
205
|
+
["a", "1"],
|
|
206
|
+
["b", "2"],
|
|
207
|
+
]),
|
|
208
|
+
);
|
|
209
|
+
});
|
|
210
|
+
|
|
211
|
+
it("preserves existing Headers", async () => {
|
|
212
|
+
const sentinel = function Sentinel() {};
|
|
213
|
+
saveGlobal("Headers");
|
|
214
|
+
(globalThis as any).Headers = sentinel;
|
|
215
|
+
await importPolyfills();
|
|
216
|
+
expect((globalThis as any).Headers).toBe(sentinel);
|
|
217
|
+
});
|
|
218
|
+
});
|
|
219
|
+
|
|
220
|
+
// ── window.location ─────────────────────────────────────────────────────
|
|
221
|
+
|
|
222
|
+
describe("window.location", () => {
|
|
223
|
+
it("installs with react-native.invalid hostname when missing", async () => {
|
|
224
|
+
if (typeof window !== "undefined") {
|
|
225
|
+
saveGlobal("location");
|
|
226
|
+
delete (window as any).location;
|
|
227
|
+
await importPolyfills();
|
|
228
|
+
expect((window as any).location.hostname).toBe("react-native.invalid");
|
|
229
|
+
}
|
|
230
|
+
});
|
|
231
|
+
});
|
|
232
|
+
|
|
233
|
+
// ── streaming fetch integration ─────────────────────────────────────────
|
|
234
|
+
|
|
235
|
+
describe("streaming fetch integration", () => {
|
|
236
|
+
it("calls installStreamingFetch()", async () => {
|
|
237
|
+
await importPolyfills();
|
|
238
|
+
const { installStreamingFetch } = await import("../streaming-fetch");
|
|
239
|
+
expect(installStreamingFetch).toHaveBeenCalled();
|
|
240
|
+
});
|
|
241
|
+
});
|
|
242
|
+
});
|