From 6c622dfbc32a9c204456b584dc0d31ad07cdef02 Mon Sep 17 00:00:00 2001 From: tygao Date: Mon, 27 May 2024 21:32:46 +0800 Subject: [PATCH] test: update tests Signed-off-by: tygao --- .../hooks/__tests__/use_conversations.test.ts | 25 ++++++- public/hooks/use_chat_actions.test.tsx | 12 +++- public/hooks/use_feed_back.test.tsx | 6 +- .../conversation_load_service.test.ts | 3 +- .../__tests__/conversations_service.test.ts | 11 ++- public/services/data_source_service.mock.ts | 12 ++++ public/services/data_source_service.ts | 18 +---- .../__tests__/chat_history_page.test.tsx | 3 +- .../chat_history_search_list.test.tsx | 4 +- ...delete_conversation_confirm_modal.test.tsx | 5 +- .../services/chat/olly_chat_service.test.ts | 34 ++++----- .../agent_framework_storage_service.test.ts | 72 +++++++++---------- .../get_opensearch_client_transport.test.ts | 5 +- 13 files changed, 122 insertions(+), 88 deletions(-) create mode 100644 public/services/data_source_service.mock.ts diff --git a/public/hooks/__tests__/use_conversations.test.ts b/public/hooks/__tests__/use_conversations.test.ts index 1d39c7b0..9fd7d636 100644 --- a/public/hooks/__tests__/use_conversations.test.ts +++ b/public/hooks/__tests__/use_conversations.test.ts @@ -7,17 +7,36 @@ import { useDeleteConversation, usePatchConversation } from '../use_conversation import { renderHook, act } from '@testing-library/react-hooks'; import { useCore } from '../../contexts/core_context'; import { HttpHandler } from '../../../../../src/core/public'; - +import { DataSourceServiceMock } from '../../services/data_source_service.mock'; +import { httpServiceMock } from '../../../../../src/core/public/mocks'; jest.mock('../../contexts/core_context'); -const useCoreMocked = useCore as jest.MockedFunction; + +const setup = () => { + const useCoreMocked = useCore as jest.MockedFunction; + const mockedDataSource = new DataSourceServiceMock(); + const mockedHttp = httpServiceMock.createStartContract(); + mockedHttp.delete = jest.fn(() => Promise.resolve()); + mockedHttp.put = jest.fn(() => Promise.resolve()); + + useCoreMocked.mockReturnValue({ + services: { + dataSource: mockedDataSource, + http: mockedHttp, + }, + }); + + return useCoreMocked; +}; describe('useDeleteConversation', () => { + const useCoreMocked = setup(); it('should call delete with path and signal', async () => { const { result } = renderHook(() => useDeleteConversation()); await act(async () => { await result.current.deleteConversation('foo'); }); + expect(useCoreMocked.mock.results[0].value.services.http.delete).toHaveBeenCalledWith( '/api/assistant/conversation/foo', expect.objectContaining({ @@ -96,6 +115,8 @@ describe('useDeleteConversation', () => { }); describe('usePatchConversation', () => { + const useCoreMocked = setup(); + it('should call put with path, query and signal', async () => { const { result } = renderHook(() => usePatchConversation()); diff --git a/public/hooks/use_chat_actions.test.tsx b/public/hooks/use_chat_actions.test.tsx index aaa6c77a..58ae5ea6 100644 --- a/public/hooks/use_chat_actions.test.tsx +++ b/public/hooks/use_chat_actions.test.tsx @@ -13,6 +13,7 @@ import { ConversationLoadService } from '../services/conversation_load_service'; import * as chatStateHookExports from './use_chat_state'; import { ASSISTANT_API } from '../../common/constants/llm'; import { IMessage } from 'common/types/chat_saved_object_attributes'; +import { DataSourceServiceMock } from '../services/data_source_service.mock'; jest.mock('../services/conversations_service', () => { return { @@ -60,6 +61,7 @@ describe('useChatActions hook', () => { const setSelectedTabIdMock = jest.fn(); const pplVisualizationRenderMock = jest.fn(); const setInteractionIdMock = jest.fn(); + const dataSourceServiceMock = new DataSourceServiceMock(); const chatContextMock: chatContextHookExports.IChatContext = { selectedTabId: 'chat', @@ -88,8 +90,9 @@ describe('useChatActions hook', () => { jest.spyOn(coreHookExports, 'useCore').mockReturnValue({ services: { http: httpMock, - conversations: new ConversationsService(httpMock), - conversationLoad: new ConversationLoadService(httpMock), + conversations: new ConversationsService(httpMock, dataSourceServiceMock), + conversationLoad: new ConversationLoadService(httpMock, dataSourceServiceMock), + dataSource: dataSourceServiceMock, }, }); @@ -125,6 +128,7 @@ describe('useChatActions hook', () => { messages: [SEND_MESSAGE_RESPONSE.messages[0]], input: INPUT_MESSAGE, }), + query: dataSourceServiceMock.getDataSourceQuery(), }); // it should send dispatch `receive` action to remove the message without messageId @@ -199,6 +203,7 @@ describe('useChatActions hook', () => { messages: [], input: { type: 'input', content: 'message that send as input', contentType: 'text' }, }), + query: dataSourceServiceMock.getDataSourceQuery(), }); }); @@ -261,6 +266,7 @@ describe('useChatActions hook', () => { expect(chatStateDispatchMock).toHaveBeenCalledWith({ type: 'abort' }); expect(httpMock.post).toHaveBeenCalledWith(ASSISTANT_API.ABORT_AGENT_EXECUTION, { body: JSON.stringify({ conversationId: 'conversation_id_to_abort' }), + query: dataSourceServiceMock.getDataSourceQuery(), }); }); @@ -288,6 +294,7 @@ describe('useChatActions hook', () => { conversationId: 'conversation_id_mock', interactionId: 'interaction_id_mock', }), + query: dataSourceServiceMock.getDataSourceQuery(), }); expect(chatStateDispatchMock).toHaveBeenCalledWith( expect.objectContaining({ type: 'receive', payload: { messages: [], interactions: [] } }) @@ -323,6 +330,7 @@ describe('useChatActions hook', () => { conversationId: 'conversation_id_mock', interactionId: 'interaction_id_mock', }), + query: dataSourceServiceMock.getDataSourceQuery(), }); expect(chatStateDispatchMock).not.toHaveBeenCalledWith( expect.objectContaining({ type: 'receive' }) diff --git a/public/hooks/use_feed_back.test.tsx b/public/hooks/use_feed_back.test.tsx index 8653c480..943aab2f 100644 --- a/public/hooks/use_feed_back.test.tsx +++ b/public/hooks/use_feed_back.test.tsx @@ -12,11 +12,12 @@ import { httpServiceMock } from '../../../../src/core/public/mocks'; import * as chatContextHookExports from '../contexts/chat_context'; import { Interaction, IOutput, IMessage } from '../../common/types/chat_saved_object_attributes'; import { ASSISTANT_API } from '../../common/constants/llm'; +import { DataSourceServiceMock } from '../services/data_source_service.mock'; describe('useFeedback hook', () => { const httpMock = httpServiceMock.createStartContract(); const chatStateDispatchMock = jest.fn(); - + const dataSourceMock = new DataSourceServiceMock(); const chatContextMock = { rootAgentId: 'root_agent_id_mock', selectedTabId: 'chat', @@ -29,6 +30,7 @@ describe('useFeedback hook', () => { jest.spyOn(coreHookExports, 'useCore').mockReturnValue({ services: { http: httpMock, + dataSource: dataSourceMock, }, }); jest.spyOn(chatContextHookExports, 'useChatContext').mockReturnValue(chatContextMock); @@ -82,6 +84,7 @@ describe('useFeedback hook', () => { body: JSON.stringify({ satisfaction: true, }), + query: dataSourceMock.getDataSourceQuery(), } ); expect(result.current.feedbackResult).toBe(true); @@ -116,6 +119,7 @@ describe('useFeedback hook', () => { body: JSON.stringify({ satisfaction: true, }), + query: dataSourceMock.getDataSourceQuery(), } ); expect(result.current.feedbackResult).toBe(undefined); diff --git a/public/services/__tests__/conversation_load_service.test.ts b/public/services/__tests__/conversation_load_service.test.ts index 81c2cf37..b35dc87b 100644 --- a/public/services/__tests__/conversation_load_service.test.ts +++ b/public/services/__tests__/conversation_load_service.test.ts @@ -6,10 +6,11 @@ import { HttpHandler } from '../../../../../src/core/public'; import { httpServiceMock } from '../../../../../src/core/public/mocks'; import { ConversationLoadService } from '../conversation_load_service'; +import { DataSourceServiceMock } from '../data_source_service.mock'; const setup = () => { const http = httpServiceMock.createSetupContract(); - const conversationLoad = new ConversationLoadService(http); + const conversationLoad = new ConversationLoadService(http, new DataSourceServiceMock()); return { conversationLoad, diff --git a/public/services/__tests__/conversations_service.test.ts b/public/services/__tests__/conversations_service.test.ts index 2ed8c75f..37fd443e 100644 --- a/public/services/__tests__/conversations_service.test.ts +++ b/public/services/__tests__/conversations_service.test.ts @@ -6,14 +6,17 @@ import { HttpHandler } from '../../../../../src/core/public'; import { httpServiceMock } from '../../../../../src/core/public/mocks'; import { ConversationsService } from '../conversations_service'; +import { DataSourceServiceMock } from '../data_source_service.mock'; const setup = () => { const http = httpServiceMock.createSetupContract(); - const conversations = new ConversationsService(http); + const dataSourceServiceMock = new DataSourceServiceMock(); + const conversations = new ConversationsService(http, dataSourceServiceMock); return { conversations, http, + dataSource: dataSourceServiceMock, }; }; @@ -32,7 +35,7 @@ describe('ConversationsService', () => { }); it('should update options property and call get with passed query', () => { - const { conversations, http } = setup(); + const { conversations, http, dataSource } = setup(); expect(conversations.options).toBeFalsy(); conversations.load({ @@ -49,6 +52,7 @@ describe('ConversationsService', () => { query: { page: 1, perPage: 10, + dataSourceId: dataSource.getDataSourceQuery()?.dataSourceId, }, signal: expect.anything(), }) @@ -69,7 +73,7 @@ describe('ConversationsService', () => { }); it('should call get with same query again after reload called', async () => { - const { conversations, http } = setup(); + const { conversations, http, dataSource } = setup(); await conversations.load({ page: 1, @@ -85,6 +89,7 @@ describe('ConversationsService', () => { query: { page: 1, perPage: 10, + dataSourceId: dataSource.getDataSourceQuery()?.dataSourceId, }, signal: expect.anything(), }) diff --git a/public/services/data_source_service.mock.ts b/public/services/data_source_service.mock.ts new file mode 100644 index 00000000..16371f61 --- /dev/null +++ b/public/services/data_source_service.mock.ts @@ -0,0 +1,12 @@ +/* + * Copyright OpenSearch Contributors + * SPDX-License-Identifier: Apache-2.0 + */ + +export class DataSourceServiceMock { + constructor() {} + + getDataSourceQuery() { + return { dataSourceId: '' }; + } +} diff --git a/public/services/data_source_service.ts b/public/services/data_source_service.ts index 1607d7fa..e2681c43 100644 --- a/public/services/data_source_service.ts +++ b/public/services/data_source_service.ts @@ -7,26 +7,10 @@ import { BehaviorSubject } from 'rxjs'; import type { DataSourceManagementPluginSetup } from '../../../../src/plugins/data_source_management/public/plugin'; export class DataSourceService { - dataSourceId$ = new BehaviorSubject(null); - private dataSourceManagement: DataSourceManagementPluginSetup | undefined | null = null; - constructor() {} getDataSourceQuery() { - if (!this.dataSourceManagement) { - return {}; - } // TODO: use new handle logic to update - const dataSourceId = this.dataSourceManagement.dataSourceSelection - .getSelectionValue() - ?.values() - .next().value; - if (dataSourceId === null) { - throw new Error('No data source id'); - } - if (dataSourceId === '') { - return {}; - } - return { dataSourceId }; + return { dataSourceId: '' }; } } diff --git a/public/tabs/history/__tests__/chat_history_page.test.tsx b/public/tabs/history/__tests__/chat_history_page.test.tsx index eb948ceb..cd9cc225 100644 --- a/public/tabs/history/__tests__/chat_history_page.test.tsx +++ b/public/tabs/history/__tests__/chat_history_page.test.tsx @@ -14,6 +14,7 @@ import * as useChatStateExports from '../../../hooks/use_chat_state'; import * as chatContextExports from '../../../contexts/chat_context'; import * as coreContextExports from '../../../contexts/core_context'; import { ConversationsService } from '../../../services/conversations_service'; +import { DataSourceServiceMock } from '../../../services/data_source_service.mock'; import { ChatHistoryPage } from '../chat_history_page'; @@ -42,7 +43,7 @@ const setup = ({ services: { ...coreMock.createStart(), http, - conversations: new ConversationsService(http), + conversations: new ConversationsService(http, new DataSourceServiceMock()), conversationLoad: {}, }, }; diff --git a/public/tabs/history/__tests__/chat_history_search_list.test.tsx b/public/tabs/history/__tests__/chat_history_search_list.test.tsx index d84baf86..1792136e 100644 --- a/public/tabs/history/__tests__/chat_history_search_list.test.tsx +++ b/public/tabs/history/__tests__/chat_history_search_list.test.tsx @@ -12,6 +12,7 @@ import * as chatContextExports from '../../../contexts/chat_context'; import * as coreContextExports from '../../../contexts/core_context'; import { ChatHistorySearchList, ChatHistorySearchListProps } from '../chat_history_search_list'; +import { DataSourceServiceMock } from '../../../services/data_source_service.mock'; const setup = ({ loading = false, @@ -26,8 +27,9 @@ const setup = ({ conversationId: '1', setTitle: jest.fn(), }; + const dataSourceServiceMock = new DataSourceServiceMock(); const useCoreMock = { - services: coreMock.createStart(), + services: { ...coreMock.createStart(), dataSource: dataSourceServiceMock }, }; useCoreMock.services.http.put.mockImplementation(() => Promise.resolve()); useCoreMock.services.http.delete.mockImplementation(() => Promise.resolve()); diff --git a/public/tabs/history/__tests__/delete_conversation_confirm_modal.test.tsx b/public/tabs/history/__tests__/delete_conversation_confirm_modal.test.tsx index 764bd8a4..6e315c6f 100644 --- a/public/tabs/history/__tests__/delete_conversation_confirm_modal.test.tsx +++ b/public/tabs/history/__tests__/delete_conversation_confirm_modal.test.tsx @@ -15,10 +15,13 @@ import { DeleteConversationConfirmModalProps, } from '../delete_conversation_confirm_modal'; import { HttpHandler } from '../../../../../../src/core/public'; +import { DataSourceServiceMock } from '../../../services/data_source_service.mock'; const setup = ({ onClose, conversationId }: DeleteConversationConfirmModalProps) => { + const dataSourceServiceMock = new DataSourceServiceMock(); + const useCoreMock = { - services: coreMock.createStart(), + services: { ...coreMock.createStart(), dataSource: dataSourceServiceMock }, }; jest.spyOn(coreContextExports, 'useCore').mockReturnValue(useCoreMock); diff --git a/server/services/chat/olly_chat_service.test.ts b/server/services/chat/olly_chat_service.test.ts index 936f7fc5..49362dcb 100644 --- a/server/services/chat/olly_chat_service.test.ts +++ b/server/services/chat/olly_chat_service.test.ts @@ -6,30 +6,23 @@ import { OllyChatService } from './olly_chat_service'; import { CoreRouteHandlerContext } from '../../../../../src/core/server/core_route_handler_context'; import { coreMock, httpServerMock } from '../../../../../src/core/server/mocks'; -import { loggerMock } from '../../../../../src/core/server/logging/logger.mock'; -import { ResponseError } from '@opensearch-project/opensearch/lib/errors'; -import { ApiResponse } from '@opensearch-project/opensearch'; describe('OllyChatService', () => { const coreContext = new CoreRouteHandlerContext( coreMock.createInternalStart(), httpServerMock.createOpenSearchDashboardsRequest() ); - const mockedTransport = coreContext.opensearch.client.asCurrentUser.transport - .request as jest.Mock; - const contextMock = { - core: coreContext, - assistant_plugin: { - logger: loggerMock.create(), - }, - }; - const ollyChatService: OllyChatService = new OllyChatService(contextMock); + const mockedTransport = coreContext.opensearch.client.asCurrentUser.transport; + const mockedTransportRequest = mockedTransport.request as jest.Mock; + + const ollyChatService: OllyChatService = new OllyChatService(mockedTransport); + beforeEach(async () => { - mockedTransport.mockClear(); + mockedTransportRequest.mockClear(); }); it('requestLLM should invoke client call with correct params', async () => { - mockedTransport.mockImplementation((args) => { + mockedTransportRequest.mockImplementation((args) => { if (args.path === '/_plugins/_ml/config/os_chat') { return { body: { @@ -65,7 +58,7 @@ describe('OllyChatService', () => { }, conversationId: 'conversationId', }); - expect(mockedTransport.mock.calls).toMatchInlineSnapshot(` + expect(mockedTransportRequest.mock.calls).toMatchInlineSnapshot(` Array [ Array [ Object { @@ -102,7 +95,7 @@ describe('OllyChatService', () => { }); it('requestLLM should throw error when transport.request throws error', async () => { - mockedTransport + mockedTransportRequest .mockImplementationOnce(() => { return { body: { @@ -124,13 +117,14 @@ describe('OllyChatService', () => { contentType: 'text', content: 'content', }, + conversationId: '', }) ).rejects.toMatchInlineSnapshot(`[Error: error]`); }); it('regenerate should invoke client call with correct params', async () => { - mockedTransport.mockImplementation((args) => { + mockedTransportRequest.mockImplementation((args) => { if (args.path === '/_plugins/_ml/config/os_chat') { return { body: { @@ -161,7 +155,7 @@ describe('OllyChatService', () => { conversationId: 'conversationId', interactionId: 'interactionId', }); - expect(mockedTransport.mock.calls).toMatchInlineSnapshot(` + expect(mockedTransportRequest.mock.calls).toMatchInlineSnapshot(` Array [ Array [ Object { @@ -198,7 +192,7 @@ describe('OllyChatService', () => { }); it('regenerate should throw error when transport.request throws error', async () => { - mockedTransport + mockedTransportRequest .mockImplementationOnce(() => { return { body: { @@ -221,7 +215,7 @@ describe('OllyChatService', () => { }); it('fetching root agent id throws error', async () => { - mockedTransport.mockImplementationOnce(() => { + mockedTransportRequest.mockImplementationOnce(() => { return { body: { hits: { diff --git a/server/services/storage/agent_framework_storage_service.test.ts b/server/services/storage/agent_framework_storage_service.test.ts index e1b5781a..aa1a77b0 100644 --- a/server/services/storage/agent_framework_storage_service.test.ts +++ b/server/services/storage/agent_framework_storage_service.test.ts @@ -12,17 +12,15 @@ describe('AgentFrameworkStorageService', () => { coreMock.createInternalStart(), httpServerMock.createOpenSearchDashboardsRequest() ); - const mockedTransport = coreContext.opensearch.client.asCurrentUser.transport - .request as jest.Mock; - const agentFrameworkService = new AgentFrameworkStorageService( - coreContext.opensearch.client.asCurrentUser, - [] - ); + const mockedTransport = coreContext.opensearch.client.asCurrentUser.transport; + const mockedTransportRequest = mockedTransport.request as jest.Mock; + + const agentFrameworkService = new AgentFrameworkStorageService(mockedTransport, []); beforeEach(() => { - mockedTransport.mockReset(); + mockedTransportRequest.mockReset(); }); it('getConversation', async () => { - mockedTransport.mockImplementation(async (params) => { + mockedTransportRequest.mockImplementation(async (params) => { if (params.path.includes('/messages?max_results=1000')) { return { body: { @@ -62,7 +60,7 @@ describe('AgentFrameworkStorageService', () => { "updatedTimeMs": 0, } `); - expect(mockedTransport.mock.calls).toMatchInlineSnapshot(` + expect(mockedTransportRequest.mock.calls).toMatchInlineSnapshot(` Array [ Array [ Object { @@ -81,14 +79,14 @@ describe('AgentFrameworkStorageService', () => { }); it('should encode id when calls getConversation with non-standard params in request payload', async () => { - mockedTransport.mockResolvedValue({ + mockedTransportRequest.mockResolvedValue({ body: { messages: [], }, }); await agentFrameworkService.getConversation('../non-standard/id'); - expect(mockedTransport.mock.calls[0]).toMatchInlineSnapshot(` + expect(mockedTransportRequest.mock.calls[0]).toMatchInlineSnapshot(` Array [ Object { "method": "GET", @@ -99,7 +97,7 @@ describe('AgentFrameworkStorageService', () => { }); it('getConversations', async () => { - mockedTransport.mockImplementation(async (params) => { + mockedTransportRequest.mockImplementation(async (params) => { return { body: { hits: { @@ -165,7 +163,7 @@ describe('AgentFrameworkStorageService', () => { "total": 10, } `); - expect(mockedTransport.mock.calls).toMatchInlineSnapshot(` + expect(mockedTransportRequest.mock.calls).toMatchInlineSnapshot(` Array [ Array [ Object { @@ -214,7 +212,7 @@ describe('AgentFrameworkStorageService', () => { }); it('deleteConversation', async () => { - mockedTransport.mockImplementationOnce(async (params) => ({ + mockedTransportRequest.mockImplementationOnce(async (params) => ({ statusCode: 200, })); expect(agentFrameworkService.deleteConversation('foo')).resolves.toMatchInlineSnapshot(` @@ -222,7 +220,7 @@ describe('AgentFrameworkStorageService', () => { "success": true, } `); - mockedTransport.mockImplementationOnce(async (params) => ({ + mockedTransportRequest.mockImplementationOnce(async (params) => ({ statusCode: 404, body: { message: 'can not find conversation', @@ -235,18 +233,18 @@ describe('AgentFrameworkStorageService', () => { "success": false, } `); - mockedTransport.mockImplementationOnce(async (params) => { + mockedTransportRequest.mockImplementationOnce(async (params) => { return Promise.reject({ meta: { body: 'error' } }); }); expect(agentFrameworkService.deleteConversation('foo')).rejects.toBeDefined(); }); it('should encode id when calls deleteConversation with non-standard params in request payload', async () => { - mockedTransport.mockResolvedValueOnce({ + mockedTransportRequest.mockResolvedValueOnce({ statusCode: 200, }); await agentFrameworkService.deleteConversation('../non-standard/id'); - expect(mockedTransport.mock.calls[0]).toMatchInlineSnapshot(` + expect(mockedTransportRequest.mock.calls[0]).toMatchInlineSnapshot(` Array [ Object { "method": "DELETE", @@ -257,7 +255,7 @@ describe('AgentFrameworkStorageService', () => { }); it('updateConversation', async () => { - mockedTransport.mockImplementationOnce(async (params) => ({ + mockedTransportRequest.mockImplementationOnce(async (params) => ({ statusCode: 200, })); expect(agentFrameworkService.updateConversation('foo', 'title')).resolves @@ -266,7 +264,7 @@ describe('AgentFrameworkStorageService', () => { "success": true, } `); - mockedTransport.mockImplementationOnce(async (params) => ({ + mockedTransportRequest.mockImplementationOnce(async (params) => ({ statusCode: 404, body: { message: 'can not find conversation', @@ -280,18 +278,18 @@ describe('AgentFrameworkStorageService', () => { "success": false, } `); - mockedTransport.mockImplementationOnce(async (params) => { + mockedTransportRequest.mockImplementationOnce(async (params) => { return Promise.reject({ meta: { body: 'error' } }); }); expect(agentFrameworkService.updateConversation('foo', 'title')).rejects.toBeDefined(); }); it('should encode id when calls updateConversation with non-standard params in request payload', async () => { - mockedTransport.mockResolvedValueOnce({ + mockedTransportRequest.mockResolvedValueOnce({ statusCode: 200, }); await agentFrameworkService.updateConversation('../non-standard/id', 'title'); - expect(mockedTransport.mock.calls[0]).toMatchInlineSnapshot(` + expect(mockedTransportRequest.mock.calls[0]).toMatchInlineSnapshot(` Array [ Object { "body": Object { @@ -305,7 +303,7 @@ describe('AgentFrameworkStorageService', () => { }); it('getTraces', async () => { - mockedTransport.mockImplementationOnce(async (params) => ({ + mockedTransportRequest.mockImplementationOnce(async (params) => ({ body: { traces: [ { @@ -331,7 +329,7 @@ describe('AgentFrameworkStorageService', () => { }, ] `); - mockedTransport.mockImplementationOnce(async (params) => { + mockedTransportRequest.mockImplementationOnce(async (params) => { return Promise.reject({ meta: { body: 'error' } }); }); expect(agentFrameworkService.getTraces('foo')).rejects.toMatchInlineSnapshot(` @@ -344,7 +342,7 @@ describe('AgentFrameworkStorageService', () => { }); it('should encode id when calls getTraces with non-standard params in request payload', async () => { - mockedTransport.mockResolvedValueOnce({ + mockedTransportRequest.mockResolvedValueOnce({ body: { traces: [ { @@ -359,7 +357,7 @@ describe('AgentFrameworkStorageService', () => { }, }); await agentFrameworkService.getTraces('../non-standard/id'); - expect(mockedTransport.mock.calls[0]).toMatchInlineSnapshot(` + expect(mockedTransportRequest.mock.calls[0]).toMatchInlineSnapshot(` Array [ Object { "method": "GET", @@ -370,7 +368,7 @@ describe('AgentFrameworkStorageService', () => { }); it('updateInteraction', async () => { - mockedTransport.mockImplementationOnce(async (params) => ({ + mockedTransportRequest.mockImplementationOnce(async (params) => ({ statusCode: 200, })); expect( @@ -384,7 +382,7 @@ describe('AgentFrameworkStorageService', () => { "success": true, } `); - mockedTransport.mockImplementationOnce(async (params) => ({ + mockedTransportRequest.mockImplementationOnce(async (params) => ({ statusCode: 404, body: { message: 'can not find conversation', @@ -403,7 +401,7 @@ describe('AgentFrameworkStorageService', () => { "success": false, } `); - mockedTransport.mockImplementationOnce(async (params) => { + mockedTransportRequest.mockImplementationOnce(async (params) => { return Promise.reject({ meta: { body: 'error' } }); }); expect( @@ -422,7 +420,7 @@ describe('AgentFrameworkStorageService', () => { }); it('should encode id when calls updateInteraction with non-standard params in request payload', async () => { - mockedTransport.mockResolvedValueOnce({ + mockedTransportRequest.mockResolvedValueOnce({ statusCode: 200, }); await agentFrameworkService.updateInteraction('../non-standard/id', { @@ -430,7 +428,7 @@ describe('AgentFrameworkStorageService', () => { bar: 'foo', }, }); - expect(mockedTransport.mock.calls[0]).toMatchInlineSnapshot(` + expect(mockedTransportRequest.mock.calls[0]).toMatchInlineSnapshot(` Array [ Object { "body": Object { @@ -448,7 +446,7 @@ describe('AgentFrameworkStorageService', () => { }); it('getInteraction', async () => { - mockedTransport.mockImplementation(async (params) => ({ + mockedTransportRequest.mockImplementation(async (params) => ({ body: { input: 'input', response: 'response', @@ -460,7 +458,7 @@ describe('AgentFrameworkStorageService', () => { expect(agentFrameworkService.getInteraction('_id', '')).rejects.toMatchInlineSnapshot( `[Error: interactionId is required]` ); - expect(mockedTransport).toBeCalledTimes(0); + expect(mockedTransportRequest).toBeCalledTimes(0); expect(agentFrameworkService.getInteraction('_id', 'interaction_id')).resolves .toMatchInlineSnapshot(` Object { @@ -470,18 +468,18 @@ describe('AgentFrameworkStorageService', () => { "response": "response", } `); - expect(mockedTransport).toBeCalledTimes(1); + expect(mockedTransportRequest).toBeCalledTimes(1); }); it('should encode id when calls getInteraction with non-standard params in request payload', async () => { - mockedTransport.mockResolvedValueOnce({ + mockedTransportRequest.mockResolvedValueOnce({ body: { input: 'input', response: 'response', }, }); await agentFrameworkService.getInteraction('_id', '../non-standard/id'); - expect(mockedTransport.mock.calls[0]).toMatchInlineSnapshot(` + expect(mockedTransportRequest.mock.calls[0]).toMatchInlineSnapshot(` Array [ Object { "method": "GET", diff --git a/server/utils/get_opensearch_client_transport.test.ts b/server/utils/get_opensearch_client_transport.test.ts index 0c59c842..4b37577a 100644 --- a/server/utils/get_opensearch_client_transport.test.ts +++ b/server/utils/get_opensearch_client_transport.test.ts @@ -19,11 +19,12 @@ describe('getOpenSearchClientTransport', () => { }) ).toBe(core.opensearch.client.asCurrentUser.transport); }); - it('should data source id related opensearch transport', async () => { + it('should return data source id related opensearch transport', async () => { const transportMock = {}; const core = coreMock.createRequestHandlerContext(); const context = { core, + assistant_plugin: { logger: mockedLogger }, dataSource: { opensearch: { getClient: async (_dataSourceId: string) => ({ @@ -35,7 +36,7 @@ describe('getOpenSearchClientTransport', () => { expect( await getOpenSearchClientTransport({ - context: { core, assistant_plugin: { logger: mockedLogger } }, + context, dataSourceId: 'foo', }) ).toBe(transportMock);