|
| 1 | +import type { ReactNode } from 'react'; |
| 2 | + |
| 3 | +import { QueryClient, QueryClientProvider } from '@tanstack/react-query'; |
| 4 | +import { renderHook, waitFor } from '@testing-library/react'; |
| 5 | +import { describe, expect, it, vi } from 'vitest'; |
| 6 | + |
| 7 | +import { |
| 8 | + AGENTS_PAGE_SIZE as PAGE_SIZE, |
| 9 | + MAX_AGENT_PAGES, |
| 10 | + useAgents, |
| 11 | +} from './use-agents'; |
| 12 | + |
| 13 | +import type AgentexSDK from 'agentex'; |
| 14 | +import type { Agent } from 'agentex/resources'; |
| 15 | + |
| 16 | +function makeAgents(count: number, prefix: string): Agent[] { |
| 17 | + return Array.from( |
| 18 | + { length: count }, |
| 19 | + (_, i) => |
| 20 | + ({ |
| 21 | + id: `${prefix}-${i}`, |
| 22 | + name: `${prefix}-${i}`, |
| 23 | + status: 'Ready', |
| 24 | + }) as Agent |
| 25 | + ); |
| 26 | +} |
| 27 | + |
| 28 | +function createWrapper() { |
| 29 | + const queryClient = new QueryClient({ |
| 30 | + defaultOptions: { queries: { retry: false } }, |
| 31 | + }); |
| 32 | + return function Wrapper({ children }: { children: ReactNode }) { |
| 33 | + return ( |
| 34 | + <QueryClientProvider client={queryClient}>{children}</QueryClientProvider> |
| 35 | + ); |
| 36 | + }; |
| 37 | +} |
| 38 | + |
| 39 | +function clientWith(list: ReturnType<typeof vi.fn>) { |
| 40 | + return { agents: { list } } as unknown as AgentexSDK; |
| 41 | +} |
| 42 | + |
| 43 | +describe('useAgents', () => { |
| 44 | + it('pages through every agent until a short page is returned', async () => { |
| 45 | + const list = vi |
| 46 | + .fn() |
| 47 | + .mockResolvedValueOnce(makeAgents(PAGE_SIZE, 'p1')) // full page -> keep going |
| 48 | + .mockResolvedValueOnce(makeAgents(24, 'p2')); // short page -> stop |
| 49 | + |
| 50 | + const { result } = renderHook(() => useAgents(clientWith(list)), { |
| 51 | + wrapper: createWrapper(), |
| 52 | + }); |
| 53 | + |
| 54 | + await waitFor(() => expect(result.current.isSuccess).toBe(true)); |
| 55 | + |
| 56 | + expect(result.current.data).toHaveLength(PAGE_SIZE + 24); |
| 57 | + expect(list).toHaveBeenCalledTimes(2); |
| 58 | + expect(list).toHaveBeenNthCalledWith(1, { |
| 59 | + limit: PAGE_SIZE, |
| 60 | + page_number: 1, |
| 61 | + }); |
| 62 | + expect(list).toHaveBeenNthCalledWith(2, { |
| 63 | + limit: PAGE_SIZE, |
| 64 | + page_number: 2, |
| 65 | + }); |
| 66 | + }); |
| 67 | + |
| 68 | + it('makes a single request when the first page is not full', async () => { |
| 69 | + const list = vi.fn().mockResolvedValueOnce(makeAgents(10, 'only')); |
| 70 | + |
| 71 | + const { result } = renderHook(() => useAgents(clientWith(list)), { |
| 72 | + wrapper: createWrapper(), |
| 73 | + }); |
| 74 | + |
| 75 | + await waitFor(() => expect(result.current.isSuccess).toBe(true)); |
| 76 | + |
| 77 | + expect(result.current.data).toHaveLength(10); |
| 78 | + expect(list).toHaveBeenCalledTimes(1); |
| 79 | + }); |
| 80 | + |
| 81 | + it('returns an empty list without paging when there are no agents', async () => { |
| 82 | + const list = vi.fn().mockResolvedValueOnce([]); |
| 83 | + |
| 84 | + const { result } = renderHook(() => useAgents(clientWith(list)), { |
| 85 | + wrapper: createWrapper(), |
| 86 | + }); |
| 87 | + |
| 88 | + await waitFor(() => expect(result.current.isSuccess).toBe(true)); |
| 89 | + |
| 90 | + expect(result.current.data).toEqual([]); |
| 91 | + expect(list).toHaveBeenCalledTimes(1); |
| 92 | + }); |
| 93 | + |
| 94 | + it('stops at the exact-multiple boundary without looping forever', async () => { |
| 95 | + // Total is an exact multiple of the page size: a full page followed by an empty page. |
| 96 | + const list = vi |
| 97 | + .fn() |
| 98 | + .mockResolvedValueOnce(makeAgents(PAGE_SIZE, 'p1')) |
| 99 | + .mockResolvedValueOnce([]); |
| 100 | + |
| 101 | + const { result } = renderHook(() => useAgents(clientWith(list)), { |
| 102 | + wrapper: createWrapper(), |
| 103 | + }); |
| 104 | + |
| 105 | + await waitFor(() => expect(result.current.isSuccess).toBe(true)); |
| 106 | + |
| 107 | + expect(result.current.data).toHaveLength(PAGE_SIZE); |
| 108 | + expect(list).toHaveBeenCalledTimes(2); |
| 109 | + }); |
| 110 | + |
| 111 | + it('stops at the safety bound and warns when every page stays full', async () => { |
| 112 | + const warn = vi.spyOn(console, 'warn').mockImplementation(() => {}); |
| 113 | + // Always return a full page so the loop can only stop at MAX_AGENT_PAGES. |
| 114 | + const list = vi.fn().mockResolvedValue(makeAgents(PAGE_SIZE, 'full')); |
| 115 | + |
| 116 | + const { result } = renderHook(() => useAgents(clientWith(list)), { |
| 117 | + wrapper: createWrapper(), |
| 118 | + }); |
| 119 | + |
| 120 | + await waitFor(() => expect(result.current.isSuccess).toBe(true)); |
| 121 | + |
| 122 | + expect(list).toHaveBeenCalledTimes(MAX_AGENT_PAGES); |
| 123 | + expect(result.current.data).toHaveLength(PAGE_SIZE * MAX_AGENT_PAGES); |
| 124 | + expect(warn).toHaveBeenCalledWith( |
| 125 | + expect.stringContaining('MAX_AGENT_PAGES') |
| 126 | + ); |
| 127 | + |
| 128 | + warn.mockRestore(); |
| 129 | + }); |
| 130 | + |
| 131 | + it('surfaces an error instead of a partial list when a page fails mid-loop', async () => { |
| 132 | + const list = vi |
| 133 | + .fn() |
| 134 | + .mockResolvedValueOnce(makeAgents(PAGE_SIZE, 'p1')) |
| 135 | + .mockRejectedValueOnce(new Error('boom')); |
| 136 | + |
| 137 | + const { result } = renderHook(() => useAgents(clientWith(list)), { |
| 138 | + wrapper: createWrapper(), |
| 139 | + }); |
| 140 | + |
| 141 | + await waitFor(() => expect(result.current.isError).toBe(true)); |
| 142 | + |
| 143 | + expect(result.current.data).toBeUndefined(); |
| 144 | + expect(list).toHaveBeenCalledTimes(2); |
| 145 | + }); |
| 146 | +}); |
0 commit comments