@devries-cameron20 / composio Mirror · read-only
Fork 0

Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.

Mirrored from https://github.com/Camo67/composio.git

Synced Nov 21, 04:45 UTC

next
ts 437 lines 12.2 KB
Raw
1 import { describe, it, expect, vi, beforeEach } from 'vitest';
2 import { getAllPages } from '../../src/utils/pagination';
3
4 describe('pagination', () => {
5 describe('getAllPages', () => {
6 beforeEach(() => {
7 vi.clearAllMocks();
8 });
9
10 it('should return all items from a single page response', async () => {
11 const mockFetch = vi.fn().mockResolvedValueOnce({
12 items: [{ id: 1, name: 'Item 1' }],
13 next_cursor: null,
14 });
15
16 const result = await getAllPages(mockFetch);
17
18 expect(result).toEqual([{ id: 1, name: 'Item 1' }]);
19 expect(mockFetch).toHaveBeenCalledTimes(1);
20 expect(mockFetch).toHaveBeenCalledWith({
21 limit: 100,
22 });
23 });
24
25 it('should handle multiple pages with cursor', async () => {
26 const mockFetch = vi
27 .fn()
28 .mockResolvedValueOnce({
29 items: [{ id: 1, name: 'Item 1' }],
30 next_cursor: 'cursor-1',
31 })
32 .mockResolvedValueOnce({
33 items: [{ id: 2, name: 'Item 2' }],
34 next_cursor: 'cursor-2',
35 })
36 .mockResolvedValueOnce({
37 items: [{ id: 3, name: 'Item 3' }],
38 next_cursor: null,
39 });
40
41 const result = await getAllPages(mockFetch);
42
43 expect(result).toEqual([
44 { id: 1, name: 'Item 1' },
45 { id: 2, name: 'Item 2' },
46 { id: 3, name: 'Item 3' },
47 ]);
48 expect(mockFetch).toHaveBeenCalledTimes(3);
49 expect(mockFetch).toHaveBeenNthCalledWith(1, {
50 limit: 100,
51 });
52 expect(mockFetch).toHaveBeenNthCalledWith(2, {
53 cursor: 'cursor-1',
54 limit: 100,
55 });
56 expect(mockFetch).toHaveBeenNthCalledWith(3, {
57 cursor: 'cursor-2',
58 limit: 100,
59 });
60 });
61
62 it('should handle empty response', async () => {
63 const mockFetch = vi.fn().mockResolvedValueOnce({
64 items: [],
65 next_cursor: null,
66 });
67
68 const result = await getAllPages(mockFetch);
69
70 expect(result).toEqual([]);
71 expect(mockFetch).toHaveBeenCalledTimes(1);
72 });
73
74 it('should handle response with undefined next_cursor', async () => {
75 const mockFetch = vi.fn().mockResolvedValueOnce({
76 items: [{ id: 1, name: 'Item 1' }],
77 next_cursor: undefined,
78 });
79
80 const result = await getAllPages(mockFetch);
81
82 expect(result).toEqual([{ id: 1, name: 'Item 1' }]);
83 expect(mockFetch).toHaveBeenCalledTimes(1);
84 });
85
86 it('should stop pagination when next_cursor is null', async () => {
87 const mockFetch = vi
88 .fn()
89 .mockResolvedValueOnce({
90 items: [{ id: 1 }],
91 next_cursor: 'cursor-1',
92 })
93 .mockResolvedValueOnce({
94 items: [{ id: 2 }],
95 next_cursor: null,
96 });
97
98 const result = await getAllPages(mockFetch);
99
100 expect(result).toEqual([{ id: 1 }, { id: 2 }]);
101 expect(mockFetch).toHaveBeenCalledTimes(2);
102 });
103
104 it('should stop pagination when next_cursor is undefined', async () => {
105 const mockFetch = vi
106 .fn()
107 .mockResolvedValueOnce({
108 items: [{ id: 1 }],
109 next_cursor: 'cursor-1',
110 })
111 .mockResolvedValueOnce({
112 items: [{ id: 2 }],
113 next_cursor: undefined,
114 });
115
116 const result = await getAllPages(mockFetch);
117
118 expect(result).toEqual([{ id: 1 }, { id: 2 }]);
119 expect(mockFetch).toHaveBeenCalledTimes(2);
120 });
121
122 it('should always use limit of 100', async () => {
123 const mockFetch = vi
124 .fn()
125 .mockResolvedValueOnce({
126 items: Array.from({ length: 50 }, (_, i) => ({ id: i + 1 })),
127 next_cursor: 'cursor-1',
128 })
129 .mockResolvedValueOnce({
130 items: Array.from({ length: 30 }, (_, i) => ({ id: i + 51 })),
131 next_cursor: null,
132 });
133
134 await getAllPages(mockFetch);
135
136 expect(mockFetch).toHaveBeenNthCalledWith(1, {
137 limit: 100,
138 });
139 expect(mockFetch).toHaveBeenNthCalledWith(2, {
140 cursor: 'cursor-1',
141 limit: 100,
142 });
143 });
144
145 it('should handle large datasets across many pages', async () => {
146 const pageSize = 100;
147 const totalPages = 5;
148 const mockFetch = vi.fn();
149
150 // Create 5 pages, each with 100 items
151 for (let page = 0; page < totalPages; page++) {
152 const items = Array.from({ length: pageSize }, (_, i) => ({
153 id: page * pageSize + i + 1,
154 name: `Item ${page * pageSize + i + 1}`,
155 }));
156
157 mockFetch.mockResolvedValueOnce({
158 items,
159 next_cursor: page < totalPages - 1 ? `cursor-${page + 1}` : null,
160 });
161 }
162
163 const result = await getAllPages(mockFetch);
164
165 expect(result).toHaveLength(pageSize * totalPages);
166 expect(result[0]).toEqual({ id: 1, name: 'Item 1' });
167 expect(result[result.length - 1]).toEqual({
168 id: pageSize * totalPages,
169 name: `Item ${pageSize * totalPages}`,
170 });
171 expect(mockFetch).toHaveBeenCalledTimes(totalPages);
172 });
173
174 it('should handle pages with varying item counts', async () => {
175 type Item = { id: number };
176 type PaginationParams = {
177 cursor?: string;
178 limit: number;
179 };
180
181 const mockFetch = vi
182 .fn<
183 (params: PaginationParams) => Promise<{
184 items: Array<Item>;
185 next_cursor?: string | null;
186 }>
187 >()
188 .mockResolvedValueOnce({
189 items: [{ id: 1 }, { id: 2 }, { id: 3 }],
190 next_cursor: 'cursor-1',
191 })
192 .mockResolvedValueOnce({
193 items: [{ id: 4 }],
194 next_cursor: 'cursor-2',
195 })
196 .mockResolvedValueOnce({
197 items: [{ id: 5 }, { id: 6 }, { id: 7 }, { id: 8 }, { id: 9 }],
198 next_cursor: null,
199 });
200
201 const result = await getAllPages(mockFetch);
202
203 expect(result).toHaveLength(9);
204 expect(result.map(item => item.id)).toEqual([1, 2, 3, 4, 5, 6, 7, 8, 9]);
205 });
206
207 it('should not include cursor in first request', async () => {
208 const mockFetch = vi.fn().mockResolvedValueOnce({
209 items: [{ id: 1 }],
210 next_cursor: null,
211 });
212
213 await getAllPages(mockFetch);
214
215 const firstCall = mockFetch.mock.calls[0][0];
216 expect(firstCall).not.toHaveProperty('cursor');
217 expect(firstCall).toEqual({ limit: 100 });
218 });
219
220 it('should include cursor in subsequent requests', async () => {
221 const mockFetch = vi
222 .fn()
223 .mockResolvedValueOnce({
224 items: [{ id: 1 }],
225 next_cursor: 'cursor-1',
226 })
227 .mockResolvedValueOnce({
228 items: [{ id: 2 }],
229 next_cursor: null,
230 });
231
232 await getAllPages(mockFetch);
233
234 const secondCall = mockFetch.mock.calls[1][0];
235 expect(secondCall).toHaveProperty('cursor', 'cursor-1');
236 expect(secondCall).toHaveProperty('limit', 100);
237 });
238
239 it('should handle complex item types', async () => {
240 interface ComplexItem {
241 id: string;
242 metadata: {
243 tags: string[];
244 score: number;
245 };
246 createdAt: Date;
247 }
248 type PaginationParams = {
249 cursor?: string;
250 limit: number;
251 };
252
253 const mockFetch = vi
254 .fn<
255 (params: PaginationParams) => Promise<{
256 items: Array<ComplexItem>;
257 next_cursor?: string | null;
258 }>
259 >()
260 .mockResolvedValueOnce({
261 items: [
262 {
263 id: 'item-1',
264 metadata: { tags: ['tag1', 'tag2'], score: 95 },
265 createdAt: new Date('2024-01-01'),
266 },
267 {
268 id: 'item-2',
269 metadata: { tags: ['tag3'], score: 87 },
270 createdAt: new Date('2024-01-02'),
271 },
272 ],
273 next_cursor: null,
274 });
275
276 const result = await getAllPages(mockFetch);
277
278 expect(result).toHaveLength(2);
279 expect(result[0].id).toBe('item-1');
280 expect(result[0].metadata.tags).toEqual(['tag1', 'tag2']);
281 expect(result[1].id).toBe('item-2');
282 });
283
284 it('should handle fetch function that captures closure variables', async () => {
285 const baseParam = { filter: 'active' };
286 const mockFetch = vi.fn().mockResolvedValueOnce({
287 items: [{ id: 1, status: 'active' }],
288 next_cursor: null,
289 });
290
291 await getAllPages(params => {
292 // Simulate a function that uses closure variables
293 expect(params.limit).toBe(100);
294 return mockFetch({ ...baseParam, ...params });
295 });
296
297 expect(mockFetch).toHaveBeenCalledTimes(1);
298 });
299
300 it('should handle empty items array with cursor', async () => {
301 const mockFetch = vi
302 .fn()
303 .mockResolvedValueOnce({
304 items: [],
305 next_cursor: 'cursor-1',
306 })
307 .mockResolvedValueOnce({
308 items: [{ id: 1 }],
309 next_cursor: null,
310 });
311
312 const result = await getAllPages(mockFetch);
313
314 expect(result).toEqual([{ id: 1 }]);
315 expect(mockFetch).toHaveBeenCalledTimes(2);
316 });
317
318 it('should handle all pages having empty items', async () => {
319 const mockFetch = vi
320 .fn()
321 .mockResolvedValueOnce({
322 items: [],
323 next_cursor: 'cursor-1',
324 })
325 .mockResolvedValueOnce({
326 items: [],
327 next_cursor: 'cursor-2',
328 })
329 .mockResolvedValueOnce({
330 items: [],
331 next_cursor: null,
332 });
333
334 const result = await getAllPages(mockFetch);
335
336 expect(result).toEqual([]);
337 expect(mockFetch).toHaveBeenCalledTimes(3);
338 });
339
340 it('should handle cursor with special characters', async () => {
341 const specialCursor = 'cursor-with-special-chars-!@#$%^&*()';
342 const mockFetch = vi
343 .fn()
344 .mockResolvedValueOnce({
345 items: [{ id: 1 }],
346 next_cursor: specialCursor,
347 })
348 .mockResolvedValueOnce({
349 items: [{ id: 2 }],
350 next_cursor: null,
351 });
352
353 const result = await getAllPages(mockFetch);
354
355 expect(result).toEqual([{ id: 1 }, { id: 2 }]);
356 expect(mockFetch).toHaveBeenNthCalledWith(2, {
357 cursor: specialCursor,
358 limit: 100,
359 });
360 });
361
362 it('should handle very long cursor strings', async () => {
363 const longCursor = 'a'.repeat(1000);
364 const mockFetch = vi
365 .fn()
366 .mockResolvedValueOnce({
367 items: [{ id: 1 }],
368 next_cursor: longCursor,
369 })
370 .mockResolvedValueOnce({
371 items: [{ id: 2 }],
372 next_cursor: null,
373 });
374
375 const result = await getAllPages(mockFetch);
376
377 expect(result).toEqual([{ id: 1 }, { id: 2 }]);
378 expect(mockFetch).toHaveBeenNthCalledWith(2, {
379 cursor: longCursor,
380 limit: 100,
381 });
382 });
383
384 it('should preserve item order across pages', async () => {
385 type Item = { id: number };
386 type PaginationParams = {
387 cursor?: string;
388 limit: number;
389 };
390
391 const mockFetch = vi
392 .fn<
393 (params: PaginationParams) => Promise<{
394 items: Array<Item>;
395 next_cursor?: string | null;
396 }>
397 >()
398 .mockResolvedValueOnce({
399 items: [{ id: 1 }, { id: 2 }, { id: 3 }],
400 next_cursor: 'cursor-1',
401 })
402 .mockResolvedValueOnce({
403 items: [{ id: 4 }, { id: 5 }],
404 next_cursor: 'cursor-2',
405 })
406 .mockResolvedValueOnce({
407 items: [{ id: 6 }, { id: 7 }, { id: 8 }],
408 next_cursor: null,
409 });
410
411 const result = await getAllPages(mockFetch);
412
413 expect(result.map(item => item.id)).toEqual([1, 2, 3, 4, 5, 6, 7, 8]);
414 });
415
416 it('should handle async fetch function errors', async () => {
417 const mockFetch = vi
418 .fn()
419 .mockResolvedValueOnce({
420 items: [{ id: 1 }],
421 next_cursor: 'cursor-1',
422 })
423 .mockRejectedValueOnce(new Error('Network error'));
424
425 await expect(getAllPages(mockFetch)).rejects.toThrow('Network error');
426 expect(mockFetch).toHaveBeenCalledTimes(2);
427 });
428
429 it('should handle fetch function that throws synchronously', async () => {
430 const mockFetch = vi.fn().mockImplementation(() => {
431 throw new Error('Synchronous error');
432 });
433
434 await expect(getAllPages(mockFetch)).rejects.toThrow('Synchronous error');
435 });
436 });
437 });