|
| 1 | +import { describe, expect, beforeEach, it, vi } from 'vitest'; |
| 2 | +import { MastraClient } from '../client'; |
| 3 | + |
| 4 | +// Mock fetch globally |
| 5 | +global.fetch = vi.fn(); |
| 6 | + |
| 7 | +describe('ToolProvider Resource', () => { |
| 8 | + let client: MastraClient; |
| 9 | + const clientOptions = { |
| 10 | + baseUrl: 'http://localhost:4111', |
| 11 | + headers: { |
| 12 | + Authorization: 'Bearer test-key', |
| 13 | + 'x-mastra-client-type': 'js', |
| 14 | + }, |
| 15 | + }; |
| 16 | + |
| 17 | + const mockFetchResponse = (data: any) => { |
| 18 | + const response = new Response(undefined, { |
| 19 | + status: 200, |
| 20 | + statusText: 'OK', |
| 21 | + headers: new Headers({ 'Content-Type': 'application/json' }), |
| 22 | + }); |
| 23 | + response.json = () => Promise.resolve(data); |
| 24 | + (global.fetch as any).mockResolvedValueOnce(response); |
| 25 | + }; |
| 26 | + |
| 27 | + beforeEach(() => { |
| 28 | + vi.clearAllMocks(); |
| 29 | + client = new MastraClient(clientOptions); |
| 30 | + }); |
| 31 | + |
| 32 | + it('listToolProviders hits the registry endpoint', async () => { |
| 33 | + const mockResponse = { |
| 34 | + providers: [ |
| 35 | + { |
| 36 | + id: 'composio', |
| 37 | + displayName: 'Composio', |
| 38 | + capabilities: { |
| 39 | + multipleConnectionsPerToolkit: true, |
| 40 | + batchConnectionStatus: true, |
| 41 | + reauthorizeReusesConnectionId: true, |
| 42 | + }, |
| 43 | + }, |
| 44 | + ], |
| 45 | + }; |
| 46 | + mockFetchResponse(mockResponse); |
| 47 | + |
| 48 | + const result = await client.listToolProviders(); |
| 49 | + expect(result).toEqual(mockResponse); |
| 50 | + expect(global.fetch).toHaveBeenCalledWith( |
| 51 | + `${clientOptions.baseUrl}/api/tool-providers`, |
| 52 | + expect.objectContaining({ |
| 53 | + headers: expect.objectContaining(clientOptions.headers), |
| 54 | + }), |
| 55 | + ); |
| 56 | + }); |
| 57 | + |
| 58 | + describe('getToolProvider("composio")', () => { |
| 59 | + const providerId = 'composio'; |
| 60 | + let provider: ReturnType<typeof client.getToolProvider>; |
| 61 | + |
| 62 | + beforeEach(() => { |
| 63 | + provider = client.getToolProvider(providerId); |
| 64 | + }); |
| 65 | + |
| 66 | + it('listToolkits', async () => { |
| 67 | + const mockResponse = { data: [{ slug: 'gmail', name: 'Gmail' }] }; |
| 68 | + mockFetchResponse(mockResponse); |
| 69 | + |
| 70 | + const result = await provider.listToolkits(); |
| 71 | + expect(result).toEqual(mockResponse); |
| 72 | + expect(global.fetch).toHaveBeenCalledWith( |
| 73 | + `${clientOptions.baseUrl}/api/tool-providers/composio/toolkits`, |
| 74 | + expect.objectContaining({ |
| 75 | + headers: expect.objectContaining(clientOptions.headers), |
| 76 | + }), |
| 77 | + ); |
| 78 | + }); |
| 79 | + |
| 80 | + it('listTools with no params', async () => { |
| 81 | + const mockResponse = { data: [], pagination: { page: 1, hasMore: false } }; |
| 82 | + mockFetchResponse(mockResponse); |
| 83 | + |
| 84 | + const result = await provider.listTools(); |
| 85 | + expect(result).toEqual(mockResponse); |
| 86 | + expect(global.fetch).toHaveBeenCalledWith( |
| 87 | + `${clientOptions.baseUrl}/api/tool-providers/composio/tools`, |
| 88 | + expect.objectContaining({ |
| 89 | + headers: expect.objectContaining(clientOptions.headers), |
| 90 | + }), |
| 91 | + ); |
| 92 | + }); |
| 93 | + |
| 94 | + it('listTools with filters + pagination', async () => { |
| 95 | + const mockResponse = { |
| 96 | + data: [{ slug: 'gmail.fetch', name: 'Fetch', toolkit: 'gmail' }], |
| 97 | + pagination: { page: 2, perPage: 10, hasMore: true }, |
| 98 | + }; |
| 99 | + mockFetchResponse(mockResponse); |
| 100 | + |
| 101 | + const result = await provider.listTools({ |
| 102 | + toolkit: 'gmail', |
| 103 | + search: 'fetch', |
| 104 | + page: 2, |
| 105 | + perPage: 10, |
| 106 | + }); |
| 107 | + expect(result).toEqual(mockResponse); |
| 108 | + |
| 109 | + const callUrl = (global.fetch as any).mock.calls[0][0] as string; |
| 110 | + expect(callUrl).toContain(`${clientOptions.baseUrl}/api/tool-providers/composio/tools?`); |
| 111 | + expect(callUrl).toContain('toolkit=gmail'); |
| 112 | + expect(callUrl).toContain('search=fetch'); |
| 113 | + expect(callUrl).toContain('page=2'); |
| 114 | + expect(callUrl).toContain('perPage=10'); |
| 115 | + }); |
| 116 | + |
| 117 | + it('authorize POSTs the body and returns redirect + authId', async () => { |
| 118 | + const mockResponse = { url: 'https://oauth/redirect', authId: 'auth-123' }; |
| 119 | + mockFetchResponse(mockResponse); |
| 120 | + |
| 121 | + const body = { toolkit: 'gmail', connectionId: 'conn-1', toolName: 'gmail.fetch' }; |
| 122 | + const result = await provider.authorize(body); |
| 123 | + expect(result).toEqual(mockResponse); |
| 124 | + expect(global.fetch).toHaveBeenCalledWith( |
| 125 | + `${clientOptions.baseUrl}/api/tool-providers/composio/authorize`, |
| 126 | + expect.objectContaining({ |
| 127 | + method: 'POST', |
| 128 | + body: JSON.stringify(body), |
| 129 | + }), |
| 130 | + ); |
| 131 | + }); |
| 132 | + |
| 133 | + it('getAuthStatus polls the auth-status endpoint', async () => { |
| 134 | + const mockResponse = { status: 'completed' }; |
| 135 | + mockFetchResponse(mockResponse); |
| 136 | + |
| 137 | + const result = await provider.getAuthStatus('auth-123'); |
| 138 | + expect(result).toEqual(mockResponse); |
| 139 | + expect(global.fetch).toHaveBeenCalledWith( |
| 140 | + `${clientOptions.baseUrl}/api/tool-providers/composio/auth-status/auth-123`, |
| 141 | + expect.objectContaining({ |
| 142 | + headers: expect.objectContaining(clientOptions.headers), |
| 143 | + }), |
| 144 | + ); |
| 145 | + }); |
| 146 | + |
| 147 | + it('getConnectionStatus POSTs items', async () => { |
| 148 | + const mockResponse = { |
| 149 | + items: { |
| 150 | + 'conn-1': { connected: true }, |
| 151 | + 'conn-2': { connected: false }, |
| 152 | + }, |
| 153 | + }; |
| 154 | + mockFetchResponse(mockResponse); |
| 155 | + |
| 156 | + const body = { |
| 157 | + items: [ |
| 158 | + { connectionId: 'conn-1', toolkit: 'gmail' }, |
| 159 | + { connectionId: 'conn-2', toolkit: 'gmail' }, |
| 160 | + ], |
| 161 | + }; |
| 162 | + const result = await provider.getConnectionStatus(body); |
| 163 | + expect(result).toEqual(mockResponse); |
| 164 | + expect(global.fetch).toHaveBeenCalledWith( |
| 165 | + `${clientOptions.baseUrl}/api/tool-providers/composio/connection-status`, |
| 166 | + expect.objectContaining({ |
| 167 | + method: 'POST', |
| 168 | + body: JSON.stringify(body), |
| 169 | + }), |
| 170 | + ); |
| 171 | + }); |
| 172 | + |
| 173 | + it('getHealth hits the health endpoint', async () => { |
| 174 | + const mockResponse = { ok: true }; |
| 175 | + mockFetchResponse(mockResponse); |
| 176 | + |
| 177 | + const result = await provider.getHealth(); |
| 178 | + expect(result).toEqual(mockResponse); |
| 179 | + expect(global.fetch).toHaveBeenCalledWith( |
| 180 | + `${clientOptions.baseUrl}/api/tool-providers/composio/health`, |
| 181 | + expect.objectContaining({ |
| 182 | + headers: expect.objectContaining(clientOptions.headers), |
| 183 | + }), |
| 184 | + ); |
| 185 | + }); |
| 186 | + }); |
| 187 | +}); |
0 commit comments