|
| 1 | +/** |
| 2 | + * Copyright 2025 Redpanda Data, Inc. |
| 3 | + * |
| 4 | + * Use of this software is governed by the Business Source License |
| 5 | + * included in the file https://github.com/redpanda-data/redpanda/blob/dev/licenses/bsl.md |
| 6 | + * |
| 7 | + * As of the Change Date specified in that file, in accordance with |
| 8 | + * the Business Source License, use of this software will be governed |
| 9 | + * by the Apache License, Version 2.0 |
| 10 | + */ |
| 11 | + |
| 12 | +import { create } from '@bufbuild/protobuf'; |
| 13 | +import { createRouterTransport } from '@connectrpc/connect'; |
| 14 | +import { renderHook, waitFor } from '@testing-library/react'; |
| 15 | +import { AIAgentSchema, ListAIAgentsResponseSchema } from 'protogen/redpanda/api/dataplane/v1alpha3/ai_agent_pb'; |
| 16 | +import { listAIAgents } from 'protogen/redpanda/api/dataplane/v1alpha3/ai_agent-AIAgentService_connectquery'; |
| 17 | +import { connectQueryWrapper } from 'test-utils'; |
| 18 | +import { describe, expect, test } from 'vitest'; |
| 19 | + |
| 20 | +import { useListAIAgentsQuery } from './ai-agent'; |
| 21 | + |
| 22 | +describe('useListAIAgentsQuery', () => { |
| 23 | + test('fetches all pages and flattens agents into a single array', async () => { |
| 24 | + let callCount = 0; |
| 25 | + |
| 26 | + const transport = createRouterTransport(({ rpc }) => { |
| 27 | + rpc(listAIAgents, (req) => { |
| 28 | + callCount += 1; |
| 29 | + const pageToken = req.pageToken; |
| 30 | + |
| 31 | + if (pageToken === '') { |
| 32 | + return create(ListAIAgentsResponseSchema, { |
| 33 | + aiAgents: [create(AIAgentSchema, { id: 'agent-1', displayName: 'Agent 1' })], |
| 34 | + nextPageToken: 'page2', |
| 35 | + }); |
| 36 | + } |
| 37 | + if (pageToken === 'page2') { |
| 38 | + return create(ListAIAgentsResponseSchema, { |
| 39 | + aiAgents: [create(AIAgentSchema, { id: 'agent-2', displayName: 'Agent 2' })], |
| 40 | + nextPageToken: 'page3', |
| 41 | + }); |
| 42 | + } |
| 43 | + return create(ListAIAgentsResponseSchema, { |
| 44 | + aiAgents: [create(AIAgentSchema, { id: 'agent-3', displayName: 'Agent 3' })], |
| 45 | + nextPageToken: '', |
| 46 | + }); |
| 47 | + }); |
| 48 | + }); |
| 49 | + |
| 50 | + const { wrapper } = connectQueryWrapper({ defaultOptions: { queries: { retry: false } } }, transport); |
| 51 | + |
| 52 | + const { result } = renderHook(() => useListAIAgentsQuery(), { wrapper }); |
| 53 | + |
| 54 | + await waitFor(() => { |
| 55 | + expect(result.current.isLoading).toBe(false); |
| 56 | + expect(result.current.data.aiAgents).toHaveLength(3); |
| 57 | + }); |
| 58 | + |
| 59 | + expect(callCount).toBe(3); |
| 60 | + expect(result.current.data.aiAgents.map((a) => a.id)).toEqual(['agent-1', 'agent-2', 'agent-3']); |
| 61 | + }); |
| 62 | + |
| 63 | + test('with page size 1, fetches once per record', async () => { |
| 64 | + let callCount = 0; |
| 65 | + |
| 66 | + const transport = createRouterTransport(({ rpc }) => { |
| 67 | + rpc(listAIAgents, (req) => { |
| 68 | + callCount += 1; |
| 69 | + const pageToken = req.pageToken; |
| 70 | + |
| 71 | + if (pageToken === '') { |
| 72 | + return create(ListAIAgentsResponseSchema, { |
| 73 | + aiAgents: [create(AIAgentSchema, { id: 'agent-1', displayName: 'Agent 1' })], |
| 74 | + nextPageToken: 'page2', |
| 75 | + }); |
| 76 | + } |
| 77 | + // Last page |
| 78 | + return create(ListAIAgentsResponseSchema, { |
| 79 | + aiAgents: [create(AIAgentSchema, { id: 'agent-2', displayName: 'Agent 2' })], |
| 80 | + nextPageToken: '', |
| 81 | + }); |
| 82 | + }); |
| 83 | + }); |
| 84 | + |
| 85 | + const { wrapper } = connectQueryWrapper({ defaultOptions: { queries: { retry: false } } }, transport); |
| 86 | + |
| 87 | + const { result } = renderHook(() => useListAIAgentsQuery(), { wrapper }); |
| 88 | + |
| 89 | + await waitFor(() => { |
| 90 | + expect(result.current.isLoading).toBe(false); |
| 91 | + expect(result.current.data.aiAgents).toHaveLength(2); |
| 92 | + }); |
| 93 | + |
| 94 | + // With page size 1 and 2 records, the query should execute exactly twice |
| 95 | + expect(callCount).toBe(2); |
| 96 | + expect(result.current.data.aiAgents.map((a) => a.id)).toEqual(['agent-1', 'agent-2']); |
| 97 | + }); |
| 98 | + |
| 99 | + test('returns all data in a single page when no nextPageToken', async () => { |
| 100 | + let callCount = 0; |
| 101 | + |
| 102 | + const transport = createRouterTransport(({ rpc }) => { |
| 103 | + rpc(listAIAgents, () => { |
| 104 | + callCount += 1; |
| 105 | + return create(ListAIAgentsResponseSchema, { |
| 106 | + aiAgents: [ |
| 107 | + create(AIAgentSchema, { id: 'agent-1', displayName: 'Agent 1' }), |
| 108 | + create(AIAgentSchema, { id: 'agent-2', displayName: 'Agent 2' }), |
| 109 | + ], |
| 110 | + nextPageToken: '', |
| 111 | + }); |
| 112 | + }); |
| 113 | + }); |
| 114 | + |
| 115 | + const { wrapper } = connectQueryWrapper({ defaultOptions: { queries: { retry: false } } }, transport); |
| 116 | + |
| 117 | + const { result } = renderHook(() => useListAIAgentsQuery(), { wrapper }); |
| 118 | + |
| 119 | + await waitFor(() => { |
| 120 | + expect(result.current.isLoading).toBe(false); |
| 121 | + expect(result.current.data.aiAgents).toHaveLength(2); |
| 122 | + }); |
| 123 | + |
| 124 | + expect(callCount).toBe(1); |
| 125 | + }); |
| 126 | + |
| 127 | + test('handles empty result', async () => { |
| 128 | + let callCount = 0; |
| 129 | + |
| 130 | + const transport = createRouterTransport(({ rpc }) => { |
| 131 | + rpc(listAIAgents, () => { |
| 132 | + callCount += 1; |
| 133 | + return create(ListAIAgentsResponseSchema, { |
| 134 | + aiAgents: [], |
| 135 | + nextPageToken: '', |
| 136 | + }); |
| 137 | + }); |
| 138 | + }); |
| 139 | + |
| 140 | + const { wrapper } = connectQueryWrapper({ defaultOptions: { queries: { retry: false } } }, transport); |
| 141 | + |
| 142 | + const { result } = renderHook(() => useListAIAgentsQuery(), { wrapper }); |
| 143 | + |
| 144 | + await waitFor(() => { |
| 145 | + expect(result.current.isLoading).toBe(false); |
| 146 | + }); |
| 147 | + |
| 148 | + expect(callCount).toBe(1); |
| 149 | + expect(result.current.data.aiAgents).toHaveLength(0); |
| 150 | + }); |
| 151 | +}); |
0 commit comments