-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
337 additions
and
122 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
125 changes: 125 additions & 0 deletions
125
...ant/impl/assistant/conversations/conversation_settings/use_conveersation_changed.test.tsx
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,125 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
import { renderHook, act } from '@testing-library/react-hooks'; | ||
import { useConversationChanged } from './use_conversation_changed'; | ||
import { customConvo } from '../../../mock/conversation'; | ||
import { mockConnectors } from '../../../mock/connectors'; | ||
import { mockSystemPrompts } from '../../../mock/system_prompt'; | ||
import { getDefaultSystemPrompt } from '../../use_conversation/helpers'; | ||
import { Conversation, ConversationsBulkActions } from '../../../..'; | ||
|
||
jest.mock('../../use_conversation/helpers', () => ({ | ||
getDefaultSystemPrompt: jest.fn(), | ||
})); | ||
|
||
const mockAllSystemPrompts = mockSystemPrompts; | ||
|
||
const mockDefaultConnector = mockConnectors[0]; | ||
|
||
const mockConversationSettings = {}; | ||
const mockConversationsSettingsBulkActions: ConversationsBulkActions = {}; | ||
const mockSetConversationSettings = jest.fn(); | ||
const mockSetConversationsSettingsBulkActions = jest.fn(); | ||
const mockOnSelectedConversationChange = jest.fn(); | ||
|
||
describe('useConversationChanged', () => { | ||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
(getDefaultSystemPrompt as jest.Mock).mockReturnValue(mockAllSystemPrompts[2]); | ||
}); | ||
|
||
test('should return a function', () => { | ||
const { result } = renderHook(() => | ||
useConversationChanged({ | ||
allSystemPrompts: mockAllSystemPrompts, | ||
conversationSettings: mockConversationSettings, | ||
conversationsSettingsBulkActions: mockConversationsSettingsBulkActions, | ||
defaultConnector: mockDefaultConnector, | ||
setConversationSettings: mockSetConversationSettings, | ||
setConversationsSettingsBulkActions: mockSetConversationsSettingsBulkActions, | ||
onSelectedConversationChange: mockOnSelectedConversationChange, | ||
}) | ||
); | ||
|
||
expect(typeof result.current).toBe('function'); | ||
}); | ||
|
||
test('should handle new conversation selection', () => { | ||
const newConversationTitle = 'New Conversation'; | ||
const { result } = renderHook(() => | ||
useConversationChanged({ | ||
allSystemPrompts: mockAllSystemPrompts, | ||
conversationSettings: mockConversationSettings, | ||
conversationsSettingsBulkActions: mockConversationsSettingsBulkActions, | ||
defaultConnector: mockDefaultConnector, | ||
setConversationSettings: mockSetConversationSettings, | ||
setConversationsSettingsBulkActions: mockSetConversationsSettingsBulkActions, | ||
onSelectedConversationChange: mockOnSelectedConversationChange, | ||
}) | ||
); | ||
|
||
act(() => { | ||
result.current(newConversationTitle); | ||
}); | ||
|
||
const expectedNewConversation: Conversation = { | ||
id: '', | ||
title: newConversationTitle, | ||
category: 'assistant', | ||
messages: [], | ||
replacements: {}, | ||
apiConfig: { | ||
connectorId: mockDefaultConnector.id, | ||
actionTypeId: mockDefaultConnector.actionTypeId, | ||
provider: mockDefaultConnector.apiProvider, | ||
defaultSystemPromptId: mockAllSystemPrompts[2].id, | ||
}, | ||
}; | ||
|
||
expect(mockSetConversationSettings).toHaveBeenCalledWith({ | ||
...mockConversationSettings, | ||
[newConversationTitle]: expectedNewConversation, | ||
}); | ||
expect(mockSetConversationsSettingsBulkActions).toHaveBeenCalledWith({ | ||
...mockConversationsSettingsBulkActions, | ||
create: { | ||
...(mockConversationsSettingsBulkActions.create ?? {}), | ||
[newConversationTitle]: expectedNewConversation, | ||
}, | ||
}); | ||
expect(mockOnSelectedConversationChange).toHaveBeenCalledWith({ | ||
...expectedNewConversation, | ||
id: expectedNewConversation.title, | ||
}); | ||
}); | ||
|
||
test('should handle existing conversation selection', () => { | ||
const existingConversation = { ...customConvo, id: 'mock-id' }; | ||
|
||
const { result } = renderHook(() => | ||
useConversationChanged({ | ||
allSystemPrompts: mockAllSystemPrompts, | ||
conversationSettings: mockConversationSettings, | ||
conversationsSettingsBulkActions: mockConversationsSettingsBulkActions, | ||
defaultConnector: mockDefaultConnector, | ||
setConversationSettings: mockSetConversationSettings, | ||
setConversationsSettingsBulkActions: mockSetConversationsSettingsBulkActions, | ||
onSelectedConversationChange: mockOnSelectedConversationChange, | ||
}) | ||
); | ||
|
||
act(() => { | ||
result.current(existingConversation); | ||
}); | ||
|
||
expect(mockSetConversationSettings.mock.calls[0][0](mockConversationSettings)).toEqual({ | ||
...mockConversationSettings, | ||
[existingConversation.id]: existingConversation, | ||
}); | ||
expect(mockOnSelectedConversationChange).toHaveBeenCalledWith(existingConversation); | ||
}); | ||
}); |
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
94 changes: 94 additions & 0 deletions
94
...tant/impl/assistant/conversations/conversation_settings/use_conversation_deletex.test.tsx
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,94 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
import { renderHook, act } from '@testing-library/react-hooks'; | ||
import { useConversationDeleted } from './use_conversation_deleted'; | ||
import { customConvo, alertConvo, welcomeConvo } from '../../../mock/conversation'; | ||
import { Conversation, ConversationsBulkActions } from '../../../..'; | ||
|
||
const customConveId = '1'; | ||
const alertConvoId = '2'; | ||
const welcomeConvoId = '3'; | ||
const mockConversationSettings: Record<string, Conversation> = { | ||
[customConveId]: { ...customConvo, id: customConveId }, | ||
[alertConvoId]: { ...alertConvo, id: alertConvoId }, | ||
[welcomeConvoId]: { ...welcomeConvo, id: welcomeConvoId }, | ||
}; | ||
|
||
const mockConversationsSettingsBulkActions: ConversationsBulkActions = { | ||
create: {}, | ||
update: {}, | ||
delete: { ids: [] }, | ||
}; | ||
|
||
const mockSetConversationSettings = jest.fn(); | ||
const mockSetConversationsSettingsBulkActions = jest.fn(); | ||
|
||
describe('useConversationDeleted', () => { | ||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
test('should return a function', () => { | ||
const { result } = renderHook(() => | ||
useConversationDeleted({ | ||
conversationSettings: mockConversationSettings, | ||
conversationsSettingsBulkActions: mockConversationsSettingsBulkActions, | ||
setConversationSettings: mockSetConversationSettings, | ||
setConversationsSettingsBulkActions: mockSetConversationsSettingsBulkActions, | ||
}) | ||
); | ||
|
||
expect(typeof result.current).toBe('function'); | ||
}); | ||
|
||
test('should handle conversation deletion', () => { | ||
const conversationTitleToDelete = customConvo.title; | ||
const { result } = renderHook(() => | ||
useConversationDeleted({ | ||
conversationSettings: mockConversationSettings, | ||
conversationsSettingsBulkActions: mockConversationsSettingsBulkActions, | ||
setConversationSettings: mockSetConversationSettings, | ||
setConversationsSettingsBulkActions: mockSetConversationsSettingsBulkActions, | ||
}) | ||
); | ||
|
||
act(() => { | ||
result.current(conversationTitleToDelete); | ||
}); | ||
|
||
const expectedConversationSettings = { ...mockConversationSettings }; | ||
delete expectedConversationSettings[customConveId]; | ||
expect(mockSetConversationSettings).toHaveBeenCalledWith(expectedConversationSettings); | ||
|
||
expect(mockSetConversationsSettingsBulkActions).toHaveBeenCalledWith({ | ||
...mockConversationsSettingsBulkActions, | ||
delete: { | ||
ids: [customConveId], | ||
}, | ||
}); | ||
}); | ||
|
||
test('should do nothing when no matching conversation title exists', () => { | ||
const conversationTitleToDelete = 'Non-existent Conversation'; | ||
const { result } = renderHook(() => | ||
useConversationDeleted({ | ||
conversationSettings: mockConversationSettings, | ||
conversationsSettingsBulkActions: mockConversationsSettingsBulkActions, | ||
setConversationSettings: mockSetConversationSettings, | ||
setConversationsSettingsBulkActions: mockSetConversationsSettingsBulkActions, | ||
}) | ||
); | ||
|
||
act(() => { | ||
result.current(conversationTitleToDelete); | ||
}); | ||
|
||
expect(mockSetConversationSettings).not.toHaveBeenCalled(); | ||
|
||
expect(mockSetConversationsSettingsBulkActions).not.toHaveBeenCalled(); | ||
}); | ||
}); |
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
38 changes: 0 additions & 38 deletions
38
...nt/impl/assistant/conversations/conversation_settings_management/system_prompt_column.tsx
This file was deleted.
Oops, something went wrong.
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.