-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into messageAction
- Loading branch information
Showing
7 changed files
with
702 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import { Boom } from '@hapi/boom'; | ||
import { OpenSearchDashboardsRequest, Router } from '../../../../src/core/server/http/router'; | ||
import { enhanceWithContext, triggerHandler } from './router.mock'; | ||
import { resetMocks } from '../services/storage/agent_framework_storage_service.mock'; | ||
import { httpServerMock } from '../../../../src/core/server/http/http_server.mocks'; | ||
import { loggerMock } from '../../../../src/core/server/logging/logger.mock'; | ||
import { AGENT_API } from '../../common/constants/llm'; | ||
import { AssistantClient } from '../services/assistant_client'; | ||
import { RequestHandlerContext } from '../../../../src/core/server'; | ||
import { registerAgentRoutes } from './agent_routes'; | ||
const mockedLogger = loggerMock.create(); | ||
|
||
export const createMockedAssistantClient = ( | ||
request: OpenSearchDashboardsRequest | ||
): AssistantClient => { | ||
return new AssistantClient(request, {} as RequestHandlerContext); | ||
}; | ||
|
||
const mockedAssistantClient = createMockedAssistantClient({} as OpenSearchDashboardsRequest); | ||
|
||
describe('test execute agent route', () => { | ||
const router = new Router( | ||
'', | ||
mockedLogger, | ||
enhanceWithContext({ | ||
assistant_plugin: { | ||
logger: mockedLogger, | ||
}, | ||
}) | ||
); | ||
registerAgentRoutes(router, { | ||
getScopedClient: jest.fn( | ||
(request: OpenSearchDashboardsRequest, context: RequestHandlerContext) => { | ||
return mockedAssistantClient; | ||
} | ||
), | ||
}); | ||
const executeAgentRequest = (payload: {}, query: {}) => | ||
triggerHandler(router, { | ||
method: 'post', | ||
path: AGENT_API.EXECUTE, | ||
req: httpServerMock.createRawRequest({ | ||
payload: JSON.stringify(payload), | ||
query, | ||
}), | ||
}); | ||
beforeEach(() => { | ||
loggerMock.clear(mockedLogger); | ||
resetMocks(); | ||
}); | ||
|
||
it('return 4xx when execute agent throws 4xx error', async () => { | ||
mockedAssistantClient.executeAgentByConfigName = jest.fn().mockRejectedValue({ | ||
statusCode: 429, | ||
body: { | ||
status: 429, | ||
error: { | ||
type: 'OpenSearchStatusException', | ||
reason: 'System Error', | ||
details: 'Request is throttled at model level.', | ||
}, | ||
}, | ||
}); | ||
const result = (await executeAgentRequest( | ||
{}, | ||
{ | ||
agentConfigName: 'os_insight', | ||
} | ||
)) as Boom; | ||
expect(result.output).toMatchInlineSnapshot(` | ||
Object { | ||
"headers": Object {}, | ||
"payload": Object { | ||
"error": "Too Many Requests", | ||
"message": "{\\"status\\":429,\\"error\\":{\\"type\\":\\"OpenSearchStatusException\\",\\"reason\\":\\"System Error\\",\\"details\\":\\"Request is throttled at model level.\\"}}", | ||
"statusCode": 429, | ||
}, | ||
"statusCode": 429, | ||
} | ||
`); | ||
}); | ||
|
||
it('return 4xx when executeAgent throws 4xx error in string format', async () => { | ||
mockedAssistantClient.executeAgentByConfigName = jest.fn().mockRejectedValue({ | ||
statusCode: 429, | ||
body: 'Request is throttled at model level', | ||
}); | ||
const result = (await executeAgentRequest( | ||
{}, | ||
{ | ||
agentConfigName: 'os_insight', | ||
} | ||
)) as Boom; | ||
expect(result.output).toMatchInlineSnapshot(` | ||
Object { | ||
"headers": Object {}, | ||
"payload": Object { | ||
"error": "Too Many Requests", | ||
"message": "Request is throttled at model level", | ||
"statusCode": 429, | ||
}, | ||
"statusCode": 429, | ||
} | ||
`); | ||
}); | ||
|
||
it('return 5xx when executeAgent throws 5xx error', async () => { | ||
mockedAssistantClient.executeAgentByConfigName = jest.fn().mockRejectedValue({ | ||
statusCode: 500, | ||
body: 'Server error', | ||
}); | ||
const result = (await executeAgentRequest( | ||
{}, | ||
{ | ||
agentConfigName: 'os_insight', | ||
} | ||
)) as Boom; | ||
expect(result.output).toMatchInlineSnapshot(` | ||
Object { | ||
"headers": Object {}, | ||
"payload": Object { | ||
"error": "Internal Server Error", | ||
"message": "Execute agent failed!", | ||
"statusCode": 500, | ||
}, | ||
"statusCode": 500, | ||
} | ||
`); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.