Skip to content

Commit

Permalink
[8.x] [Obs AI Assistant] Chat history details in conversation list (#…
Browse files Browse the repository at this point in the history
…207426) (#208605)

# Backport

This will backport the following commits from `main` to `8.x`:
- [[Obs AI Assistant] Chat history details in conversation list
(#207426)](#207426)

<!--- Backport version: 9.4.3 -->

### Questions ?
Please refer to the [Backport tool
documentation](https://github.com/sqren/backport)

<!--BACKPORT [{"author":{"name":"Viduni
Wickramarachchi","email":"[email protected]"},"sourceCommit":{"committedDate":"2025-01-28T18:50:18Z","message":"[Obs
AI Assistant] Chat history details in conversation list
(#207426)\n\nCloses
https://github.com/elastic/kibana/issues/176295\r\n\r\n##
Summary\r\n\r\nCategorizes the chat history based on `lastUpdated` date
of the\r\nconversation.\r\n\r\n### Checklist\r\n\r\n- [x] Any text added
follows [EUI's
writing\r\nguidelines](https://elastic.github.io/eui/#/guidelines/writing),
uses\r\nsentence case text and includes
[i18n\r\nsupport](https://github.com/elastic/kibana/blob/main/src/platform/packages/shared/kbn-i18n/README.md)\r\n-
[x] [Unit or
functional\r\ntests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)\r\nwere
updated or added to match the most common scenarios\r\n- [x] The PR
description includes the appropriate Release Notes section,\r\nand the
correct `release_note:*` label is applied per
the\r\n[guidelines](https://www.elastic.co/guide/en/kibana/master/contributing.html#kibana-release-notes-process)\r\n\r\n---------\r\n\r\nCo-authored-by:
kibanamachine
<[email protected]>","sha":"67cf170a24c056b743b5fae229ff05f86913cb3d","branchLabelMapping":{"^v9.0.0$":"main","^v8.18.0$":"8.x","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["v9.0.0","release_note:feature","Team:Obs
AI
Assistant","ci:project-deploy-observability","backport:version","v8.18.0"],"title":"[Obs
AI Assistant] Chat history details in conversation
list","number":207426,"url":"https://github.com/elastic/kibana/pull/207426","mergeCommit":{"message":"[Obs
AI Assistant] Chat history details in conversation list
(#207426)\n\nCloses
https://github.com/elastic/kibana/issues/176295\r\n\r\n##
Summary\r\n\r\nCategorizes the chat history based on `lastUpdated` date
of the\r\nconversation.\r\n\r\n### Checklist\r\n\r\n- [x] Any text added
follows [EUI's
writing\r\nguidelines](https://elastic.github.io/eui/#/guidelines/writing),
uses\r\nsentence case text and includes
[i18n\r\nsupport](https://github.com/elastic/kibana/blob/main/src/platform/packages/shared/kbn-i18n/README.md)\r\n-
[x] [Unit or
functional\r\ntests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)\r\nwere
updated or added to match the most common scenarios\r\n- [x] The PR
description includes the appropriate Release Notes section,\r\nand the
correct `release_note:*` label is applied per
the\r\n[guidelines](https://www.elastic.co/guide/en/kibana/master/contributing.html#kibana-release-notes-process)\r\n\r\n---------\r\n\r\nCo-authored-by:
kibanamachine
<[email protected]>","sha":"67cf170a24c056b743b5fae229ff05f86913cb3d"}},"sourceBranch":"main","suggestedTargetBranches":["8.x"],"targetPullRequestStates":[{"branch":"main","label":"v9.0.0","branchLabelMappingKey":"^v9.0.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/207426","number":207426,"mergeCommit":{"message":"[Obs
AI Assistant] Chat history details in conversation list
(#207426)\n\nCloses
https://github.com/elastic/kibana/issues/176295\r\n\r\n##
Summary\r\n\r\nCategorizes the chat history based on `lastUpdated` date
of the\r\nconversation.\r\n\r\n### Checklist\r\n\r\n- [x] Any text added
follows [EUI's
writing\r\nguidelines](https://elastic.github.io/eui/#/guidelines/writing),
uses\r\nsentence case text and includes
[i18n\r\nsupport](https://github.com/elastic/kibana/blob/main/src/platform/packages/shared/kbn-i18n/README.md)\r\n-
[x] [Unit or
functional\r\ntests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)\r\nwere
updated or added to match the most common scenarios\r\n- [x] The PR
description includes the appropriate Release Notes section,\r\nand the
correct `release_note:*` label is applied per
the\r\n[guidelines](https://www.elastic.co/guide/en/kibana/master/contributing.html#kibana-release-notes-process)\r\n\r\n---------\r\n\r\nCo-authored-by:
kibanamachine
<[email protected]>","sha":"67cf170a24c056b743b5fae229ff05f86913cb3d"}},{"branch":"8.x","label":"v8.18.0","branchLabelMappingKey":"^v8.18.0$","isSourceBranch":false,"state":"NOT_CREATED"}]}]
BACKPORT-->

Co-authored-by: Viduni Wickramarachchi <[email protected]>
  • Loading branch information
kibanamachine and viduni94 authored Jan 28, 2025
1 parent 84b4692 commit 81ceef2
Show file tree
Hide file tree
Showing 14 changed files with 736 additions and 101 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export function NewChatButton(
</EuiButton>
) : (
<EuiButtonIcon
data-test-subj="observabilityAiAssistantNewChatButtonButton"
data-test-subj="observabilityAiAssistantNewChatButton"
iconType="newChat"
size="xs"
{...nextProps}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,204 @@
/*
* 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 React from 'react';
import { i18n } from '@kbn/i18n';
import { render, screen, fireEvent } from '@testing-library/react';
import { DATE_CATEGORY_LABELS } from '../i18n';
import { ConversationList } from './conversation_list';
import { UseConversationListResult } from '../hooks/use_conversation_list';
import { useConversationsByDate } from '../hooks/use_conversations_by_date';

jest.mock('../hooks/use_conversations_by_date', () => ({
useConversationsByDate: jest.fn(),
}));

jest.mock('../hooks/use_confirm_modal', () => ({
useConfirmModal: jest.fn().mockReturnValue({
element: <div data-test-subj="confirmModal" />,
confirm: jest.fn(() => Promise.resolve(true)),
}),
}));

const mockConversations: UseConversationListResult['conversations'] = {
value: {
conversations: [
{
conversation: {
id: '1',
title: "Today's Conversation",
last_updated: '2025-01-21T10:00:00Z',
},
'@timestamp': '2025-01-21T10:00:00Z',
labels: {},
numeric_labels: {},
messages: [],
namespace: 'namespace-1',
public: true,
},
{
conversation: {
id: '2',
title: "Yesterday's Conversation",
last_updated: '2025-01-20T10:00:00Z',
},
'@timestamp': '2025-01-20T10:00:00Z',
labels: {},
numeric_labels: {},
messages: [],
namespace: 'namespace-2',
public: true,
},
],
},
error: undefined,
loading: false,
refresh: jest.fn(),
};

const mockCategorizedConversations = {
TODAY: [
{
id: '1',
label: "Today's Conversation",
lastUpdated: '2025-01-21T10:00:00Z',
href: '/conversation/1',
},
],
YESTERDAY: [
{
id: '2',
label: "Yesterday's Conversation",
lastUpdated: '2025-01-20T10:00:00Z',
href: '/conversation/2',
},
],
THIS_WEEK: [],
LAST_WEEK: [],
THIS_MONTH: [],
LAST_MONTH: [],
THIS_YEAR: [],
OLDER: [],
};

const defaultProps = {
conversations: mockConversations,
isLoading: false,
selectedConversationId: undefined,
onConversationSelect: jest.fn(),
onConversationDeleteClick: jest.fn(),
newConversationHref: '/conversation/new',
getConversationHref: (id: string) => `/conversation/${id}`,
};

describe('ConversationList', () => {
beforeEach(() => {
jest.clearAllMocks();
(useConversationsByDate as jest.Mock).mockReturnValue(mockCategorizedConversations);
});

it('renders the component without errors', () => {
render(<ConversationList {...defaultProps} />);

const todayCategoryLabel = screen.getByText(/today/i, {
selector: 'div.euiText',
});
expect(todayCategoryLabel).toBeInTheDocument();

const yesterdayCategoryLabel = screen.getByText(/yesterday/i, {
selector: 'div.euiText',
});
expect(yesterdayCategoryLabel).toBeInTheDocument();

expect(screen.queryByRole('progressbar')).not.toBeInTheDocument();
expect(
screen.queryByText(
i18n.translate('xpack.aiAssistant.conversationList.errorMessage', {
defaultMessage: 'Failed to load',
})
)
).not.toBeInTheDocument();

expect(
screen.queryByText(
i18n.translate('xpack.aiAssistant.conversationList.noConversations', {
defaultMessage: 'No conversations',
})
)
).not.toBeInTheDocument();

expect(screen.getByTestId('observabilityAiAssistantNewChatButton')).toBeInTheDocument();
});

it('displays loading state', () => {
render(<ConversationList {...defaultProps} isLoading={true} />);
expect(screen.getByRole('progressbar')).toBeInTheDocument();
});

it('displays error state', () => {
const errorProps = {
...defaultProps,
conversations: { ...mockConversations, error: new Error('An error occurred') },
};
render(<ConversationList {...errorProps} />);
expect(
screen.getByText(
i18n.translate('xpack.aiAssistant.conversationList.errorMessage', {
defaultMessage: 'Failed to load',
})
)
).toBeInTheDocument();
});

it('renders categorized conversations', () => {
render(<ConversationList {...defaultProps} />);
Object.entries(mockCategorizedConversations).forEach(([category, conversationList]) => {
if (conversationList.length > 0) {
expect(screen.getByText(DATE_CATEGORY_LABELS[category])).toBeInTheDocument();
conversationList.forEach((conversation) => {
expect(screen.getByText(conversation.label)).toBeInTheDocument();
});
}
});
});

it('calls onConversationSelect when a conversation is clicked', () => {
render(<ConversationList {...defaultProps} />);
const todayConversation = screen.getByText("Today's Conversation");
fireEvent.click(todayConversation);
expect(defaultProps.onConversationSelect).toHaveBeenCalledWith('1');
});

it('calls onConversationDeleteClick when delete icon is clicked', async () => {
render(<ConversationList {...defaultProps} />);
const deleteButtons = screen.getAllByLabelText('Delete');
await fireEvent.click(deleteButtons[0]);
expect(defaultProps.onConversationDeleteClick).toHaveBeenCalledWith('1');
});

it('renders a new chat button and triggers onConversationSelect when clicked', () => {
render(<ConversationList {...defaultProps} />);
const newChatButton = screen.getByTestId('observabilityAiAssistantNewChatButton');
fireEvent.click(newChatButton);
expect(defaultProps.onConversationSelect).toHaveBeenCalledWith(undefined);
});

it('renders "no conversations" message when there are no conversations', () => {
const emptyProps = {
...defaultProps,
conversations: { ...mockConversations, value: { conversations: [] } },
};
render(<ConversationList {...emptyProps} />);
expect(
screen.getByText(
i18n.translate('xpack.aiAssistant.conversationList.noConversations', {
defaultMessage: 'No conversations',
})
)
).toBeInTheDocument();
});
});
Loading

0 comments on commit 81ceef2

Please sign in to comment.