-
Notifications
You must be signed in to change notification settings - Fork 8.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Security Solution] Store last conversation in localstorage #6993 #161373
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
35c6ecb
[Security Solution] Store last conversation in localstorage #6993
lgestc 5af7de9
Merge branch 'main' into bugfix-6993
lgestc adfd242
[CI] Auto-commit changed files from 'node scripts/lint_ts_projects --…
kibanamachine 51af36a
resolve conflict
stephmilovic 99bb10d
PR fix up
stephmilovic ebae5f7
Merge branch 'main' into bugfix-6993
stephmilovic 00af2e7
change import to type
stephmilovic cf8e8b2
[CI] Auto-commit changed files from 'node scripts/lint_ts_projects --…
kibanamachine File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
163 changes: 163 additions & 0 deletions
163
x-pack/packages/kbn-elastic-assistant/impl/assistant/index.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,163 @@ | ||
/* | ||
* 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 { act, fireEvent, render, screen } from '@testing-library/react'; | ||
import { Assistant } from '.'; | ||
import { Conversation } from '../assistant_context/types'; | ||
import type { IHttpFetchError } from '@kbn/core/public'; | ||
import { ActionConnector } from '@kbn/triggers-actions-ui-plugin/public'; | ||
|
||
import { useLoadConnectors } from '../connectorland/use_load_connectors'; | ||
import { useConnectorSetup } from '../connectorland/connector_setup'; | ||
|
||
import { UseQueryResult } from '@tanstack/react-query'; | ||
import { WELCOME_CONVERSATION_TITLE } from './use_conversation/translations'; | ||
|
||
import { useLocalStorage } from 'react-use'; | ||
import { PromptEditor } from './prompt_editor'; | ||
import { QuickPrompts } from './quick_prompts/quick_prompts'; | ||
import { TestProviders } from '../mock/test_providers/test_providers'; | ||
|
||
jest.mock('../connectorland/use_load_connectors'); | ||
jest.mock('../connectorland/connector_setup'); | ||
jest.mock('react-use'); | ||
|
||
jest.mock('./prompt_editor', () => ({ PromptEditor: jest.fn() })); | ||
jest.mock('./quick_prompts/quick_prompts', () => ({ QuickPrompts: jest.fn() })); | ||
|
||
const MOCK_CONVERSATION_TITLE = 'electric sheep'; | ||
|
||
const getInitialConversations = (): Record<string, Conversation> => ({ | ||
[WELCOME_CONVERSATION_TITLE]: { | ||
id: WELCOME_CONVERSATION_TITLE, | ||
messages: [], | ||
apiConfig: {}, | ||
}, | ||
[MOCK_CONVERSATION_TITLE]: { | ||
id: MOCK_CONVERSATION_TITLE, | ||
messages: [], | ||
apiConfig: {}, | ||
}, | ||
}); | ||
|
||
const renderAssistant = () => | ||
render( | ||
<TestProviders getInitialConversations={getInitialConversations}> | ||
<Assistant isAssistantEnabled /> | ||
</TestProviders> | ||
); | ||
|
||
describe('Assistant', () => { | ||
beforeAll(() => { | ||
jest.mocked(useConnectorSetup).mockReturnValue({ | ||
comments: [], | ||
prompt: <></>, | ||
}); | ||
|
||
jest.mocked(PromptEditor).mockReturnValue(null); | ||
jest.mocked(QuickPrompts).mockReturnValue(null); | ||
}); | ||
|
||
let persistToLocalStorage: jest.Mock; | ||
|
||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
persistToLocalStorage = jest.fn(); | ||
|
||
jest | ||
.mocked(useLocalStorage) | ||
.mockReturnValue([undefined, persistToLocalStorage] as unknown as ReturnType< | ||
typeof useLocalStorage | ||
>); | ||
}); | ||
|
||
describe('when selected conversation changes and some connectors are loaded', () => { | ||
it('should persist the conversation id to local storage', async () => { | ||
const connectors: unknown[] = [{}]; | ||
|
||
jest.mocked(useLoadConnectors).mockReturnValue({ | ||
isSuccess: true, | ||
data: connectors, | ||
} as unknown as UseQueryResult<ActionConnector[], IHttpFetchError>); | ||
|
||
renderAssistant(); | ||
|
||
expect(persistToLocalStorage).toHaveBeenCalled(); | ||
|
||
expect(persistToLocalStorage).toHaveBeenLastCalledWith(WELCOME_CONVERSATION_TITLE); | ||
|
||
const previousConversationButton = screen.getByLabelText('Previous conversation'); | ||
|
||
expect(previousConversationButton).toBeInTheDocument(); | ||
await act(async () => { | ||
fireEvent.click(previousConversationButton); | ||
}); | ||
|
||
expect(persistToLocalStorage).toHaveBeenLastCalledWith('electric sheep'); | ||
}); | ||
|
||
it('should not persist the conversation id to local storage when excludeFromLastConversationStorage flag is indicated', async () => { | ||
const connectors: unknown[] = [{}]; | ||
|
||
jest.mocked(useLoadConnectors).mockReturnValue({ | ||
isSuccess: true, | ||
data: connectors, | ||
} as unknown as UseQueryResult<ActionConnector[], IHttpFetchError>); | ||
|
||
const { getByLabelText } = render( | ||
<TestProviders | ||
getInitialConversations={() => ({ | ||
[WELCOME_CONVERSATION_TITLE]: { | ||
id: WELCOME_CONVERSATION_TITLE, | ||
messages: [], | ||
apiConfig: {}, | ||
}, | ||
[MOCK_CONVERSATION_TITLE]: { | ||
id: MOCK_CONVERSATION_TITLE, | ||
messages: [], | ||
apiConfig: {}, | ||
excludeFromLastConversationStorage: true, | ||
}, | ||
})} | ||
> | ||
<Assistant isAssistantEnabled /> | ||
</TestProviders> | ||
); | ||
|
||
expect(persistToLocalStorage).toHaveBeenCalled(); | ||
|
||
expect(persistToLocalStorage).toHaveBeenLastCalledWith(WELCOME_CONVERSATION_TITLE); | ||
|
||
const previousConversationButton = getByLabelText('Previous conversation'); | ||
|
||
expect(previousConversationButton).toBeInTheDocument(); | ||
|
||
await act(async () => { | ||
fireEvent.click(previousConversationButton); | ||
}); | ||
expect(persistToLocalStorage).toHaveBeenLastCalledWith(WELCOME_CONVERSATION_TITLE); | ||
}); | ||
}); | ||
|
||
describe('when no connectors are loaded', () => { | ||
it('should clear conversation id in local storage', async () => { | ||
const emptyConnectors: unknown[] = []; | ||
|
||
jest.mocked(useLoadConnectors).mockReturnValue({ | ||
isSuccess: true, | ||
data: emptyConnectors, | ||
} as unknown as UseQueryResult<ActionConnector[], IHttpFetchError>); | ||
|
||
renderAssistant(); | ||
|
||
expect(persistToLocalStorage).toHaveBeenCalled(); | ||
expect(persistToLocalStorage).toHaveBeenLastCalledWith(''); | ||
}); | ||
}); | ||
}); |
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
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
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 |
---|---|---|
|
@@ -28,5 +28,6 @@ | |
"@kbn/i18n-react", | ||
"@kbn/ui-theme", | ||
"@kbn/core-doc-links-browser", | ||
"@kbn/core", | ||
] | ||
} |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🙌