|
| 1 | +import { ApiResponse } from '@opensearch-project/opensearch'; |
| 2 | +import { ResponseError } from '@opensearch-project/opensearch/lib/errors'; |
| 3 | +import { RequestHandlerContext } from 'src/core/server'; |
| 4 | +import { CoreRouteHandlerContext } from '../../../../../src/core/server/core_route_handler_context'; |
| 5 | +import { loggerMock } from '../../../../../src/core/server/logging/logger.mock'; |
| 6 | +import { coreMock, httpServerMock } from '../../../../../src/core/server/mocks'; |
| 7 | +import { getAgentIdByConfig, requestAgentByConfig } from './agents'; |
| 8 | + |
| 9 | +describe('Agents helper functions', () => { |
| 10 | + const coreContext = new CoreRouteHandlerContext( |
| 11 | + coreMock.createInternalStart(), |
| 12 | + httpServerMock.createOpenSearchDashboardsRequest() |
| 13 | + ); |
| 14 | + const client = coreContext.opensearch.client.asCurrentUser; |
| 15 | + const mockedTransport = client.transport.request as jest.Mock; |
| 16 | + const context: RequestHandlerContext = { |
| 17 | + core: coreContext, |
| 18 | + dataSource: jest.fn(), |
| 19 | + query_assist: { dataSourceEnabled: false, logger: loggerMock.create() }, |
| 20 | + }; |
| 21 | + |
| 22 | + afterEach(() => { |
| 23 | + jest.clearAllMocks(); |
| 24 | + }); |
| 25 | + |
| 26 | + it('searches agent id by name', async () => { |
| 27 | + mockedTransport.mockResolvedValueOnce({ |
| 28 | + body: { |
| 29 | + type: 'agent', |
| 30 | + configuration: { agent_id: 'agentId' }, |
| 31 | + }, |
| 32 | + }); |
| 33 | + const id = await getAgentIdByConfig(client, 'test_agent'); |
| 34 | + expect(id).toEqual('agentId'); |
| 35 | + expect(mockedTransport.mock.calls[0]).toMatchInlineSnapshot(` |
| 36 | + Array [ |
| 37 | + Object { |
| 38 | + "method": "GET", |
| 39 | + "path": "/_plugins/_ml/config/test_agent", |
| 40 | + }, |
| 41 | + ] |
| 42 | + `); |
| 43 | + }); |
| 44 | + |
| 45 | + it('handles not found errors', async () => { |
| 46 | + mockedTransport.mockRejectedValueOnce( |
| 47 | + new ResponseError(({ |
| 48 | + body: { |
| 49 | + error: { |
| 50 | + root_cause: [ |
| 51 | + { |
| 52 | + type: 'status_exception', |
| 53 | + reason: 'Failed to find config with the provided config id: test_agent', |
| 54 | + }, |
| 55 | + ], |
| 56 | + type: 'status_exception', |
| 57 | + reason: 'Failed to find config with the provided config id: test_agent', |
| 58 | + }, |
| 59 | + status: 404, |
| 60 | + }, |
| 61 | + statusCode: 404, |
| 62 | + } as unknown) as ApiResponse) |
| 63 | + ); |
| 64 | + await expect( |
| 65 | + getAgentIdByConfig(client, 'test agent') |
| 66 | + ).rejects.toThrowErrorMatchingInlineSnapshot( |
| 67 | + `"Get agent 'test agent' failed, reason: {\\"error\\":{\\"root_cause\\":[{\\"type\\":\\"status_exception\\",\\"reason\\":\\"Failed to find config with the provided config id: test_agent\\"}],\\"type\\":\\"status_exception\\",\\"reason\\":\\"Failed to find config with the provided config id: test_agent\\"},\\"status\\":404}"` |
| 68 | + ); |
| 69 | + }); |
| 70 | + |
| 71 | + it('handles search errors', async () => { |
| 72 | + mockedTransport.mockRejectedValueOnce('request failed'); |
| 73 | + await expect( |
| 74 | + getAgentIdByConfig(client, 'test agent') |
| 75 | + ).rejects.toThrowErrorMatchingInlineSnapshot( |
| 76 | + `"Get agent 'test agent' failed, reason: request failed"` |
| 77 | + ); |
| 78 | + }); |
| 79 | + |
| 80 | + it('searches for agent id and sends request', async () => { |
| 81 | + mockedTransport |
| 82 | + .mockResolvedValueOnce({ |
| 83 | + body: { |
| 84 | + type: 'agent', |
| 85 | + configuration: { agent_id: 'new-id' }, |
| 86 | + }, |
| 87 | + }) |
| 88 | + .mockResolvedValueOnce({ |
| 89 | + body: { inference_results: [{ output: [{ result: 'test response' }] }] }, |
| 90 | + }); |
| 91 | + const response = await requestAgentByConfig({ |
| 92 | + context, |
| 93 | + configName: 'new_agent', |
| 94 | + body: { parameters: { param1: 'value1' } }, |
| 95 | + }); |
| 96 | + expect(mockedTransport).toBeCalledWith( |
| 97 | + expect.objectContaining({ path: '/_plugins/_ml/agents/new-id/_execute' }), |
| 98 | + expect.anything() |
| 99 | + ); |
| 100 | + expect(response.body.inference_results[0].output[0].result).toEqual('test response'); |
| 101 | + }); |
| 102 | +}); |
0 commit comments