Skip to content

Commit

Permalink
fix typo and pagination
Browse files Browse the repository at this point in the history
  • Loading branch information
angorayc committed Jun 27, 2024
1 parent c78da14 commit 60f271e
Show file tree
Hide file tree
Showing 14 changed files with 337 additions and 122 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ export function AiAssistantSelectionPage() {
isDisabled={!securityAIAssistantEnabled}
title={i18n.translate(
'aiAssistantManagementSelection.aiAssistantSelectionPage.securityLabel',
{ defaultMessage: 'Elastic AI for Security' }
{ defaultMessage: 'Elastic AI Assistant for Security' }
)}
titleSize="xs"
/>
Expand Down
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);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,18 @@ export const useConversationDeleted = ({
}: Props) => {
const onConversationDeleted = useCallback(
(conversationTitle: string) => {
const conversationId =
Object.values(conversationSettings).find((c) => c.title === conversationTitle)?.id ?? '';
const conversationId = Object.values(conversationSettings).find(
(c) => c.title === conversationTitle
)?.id;
// If matching conversation is not found, do nothing
if (!conversationId) {
return;
}

const updatedConversationSettings = { ...conversationSettings };
delete updatedConversationSettings[conversationId];
setConversationSettings(updatedConversationSettings);

setConversationSettings(updatedConversationSettings);
setConversationsSettingsBulkActions({
...conversationsSettingsBulkActions,
delete: {
Expand Down
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();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import { Flyout } from '../../common/components/assistant_settings_management/fl
import { CANCEL, DELETE } from '../../settings/translations';
import { ConversationSettingsEditor } from '../conversation_settings/conversation_settings_editor';
import { useConversationChanged } from '../conversation_settings/use_conversation_changed';
import { DEFAULT_PAGE_INDEX, DEFAULT_PAGE_SIZE } from '../../settings/const';
import { DEFAULT_PAGE_SIZE } from '../../settings/const';

interface Props {
actionTypeRegistry: ActionTypeRegistryContract;
Expand Down Expand Up @@ -189,12 +189,10 @@ const ConversationSettingsManagementComponent: React.FC<Props> = ({

const pagination = useMemo(
() => ({
pageIndex: DEFAULT_PAGE_INDEX,
pageSize: DEFAULT_PAGE_SIZE,
pageSizeOptions: [DEFAULT_PAGE_SIZE],
totalItemCount: conversationOptions.length,
initialPageSize: DEFAULT_PAGE_SIZE,
pageSizeOptions: [10, DEFAULT_PAGE_SIZE, 50],
}),
[conversationOptions.length]
[]
);

if (!conversationsLoaded) {
Expand Down Expand Up @@ -223,7 +221,7 @@ const ConversationSettingsManagementComponent: React.FC<Props> = ({
onClose={onSaveCancelled}
onSaveConfirmed={onSaveConfirmed}
onSaveCancelled={onSaveCancelled}
title={selectedConversation?.title ?? i18n.CONVERSATIONS_TABLE_COLUMN_CONVERSATIONS}
title={selectedConversation?.title ?? i18n.CONVERSATIONS_FLYOUT_DEFAULT_TITLE}
>
<ConversationSettingsEditor
allSystemPrompts={allSystemPrompts}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@

import { i18n } from '@kbn/i18n';

export const CONVERSATIONS_TABLE_COLUMN_CONVERSATIONS = i18n.translate(
'xpack.elasticAssistant.assistant.conversationSettings.column.conversations',
export const CONVERSATIONS_TABLE_COLUMN_NAME = i18n.translate(
'xpack.elasticAssistant.assistant.conversationSettings.column.name',
{
defaultMessage: 'Conversations',
defaultMessage: 'Name',
}
);

Expand Down Expand Up @@ -42,6 +42,13 @@ export const CONVERSATIONS_TABLE_COLUMN_ACTIONS = i18n.translate(
}
);

export const CONVERSATIONS_FLYOUT_DEFAULT_TITLE = i18n.translate(
'xpack.elasticAssistant.assistant.conversationSettings.flyout.defaultTitle',
{
defaultMessage: 'Conversation',
}
);

export const DELETE_CONVERSATION_CONFIRMATION_DEFAULT_TITLE = i18n.translate(
'xpack.elasticAssistant.assistant.conversationSettings.deleteConfirmation.defaultTitle',
{
Expand Down
Loading

0 comments on commit 60f271e

Please sign in to comment.