-
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.
[Security Solution] AI Assistant telemetry (#162653)
- Loading branch information
1 parent
bd1e64a
commit 1b21230
Showing
31 changed files
with
848 additions
and
52 deletions.
There are no files selected for viewing
85 changes: 85 additions & 0 deletions
85
x-pack/packages/kbn-elastic-assistant/impl/assistant/assistant_overlay/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,85 @@ | ||
/* | ||
* 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 { fireEvent, render } from '@testing-library/react'; | ||
import { AssistantOverlay } from '.'; | ||
import { TestProviders } from '../../mock/test_providers/test_providers'; | ||
|
||
const reportAssistantInvoked = jest.fn(); | ||
const assistantTelemetry = { | ||
reportAssistantInvoked, | ||
reportAssistantMessageSent: () => {}, | ||
reportAssistantQuickPrompt: () => {}, | ||
}; | ||
describe('AssistantOverlay', () => { | ||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
it('renders when isAssistantEnabled prop is true and keyboard shortcut is pressed', () => { | ||
const { getByTestId } = render( | ||
<TestProviders providerContext={{ assistantTelemetry }}> | ||
<AssistantOverlay isAssistantEnabled={true} /> | ||
</TestProviders> | ||
); | ||
fireEvent.keyDown(document, { key: ';', ctrlKey: true }); | ||
const modal = getByTestId('ai-assistant-modal'); | ||
expect(modal).toBeInTheDocument(); | ||
}); | ||
|
||
it('modal closes when close button is clicked', () => { | ||
const { getByLabelText, queryByTestId } = render( | ||
<TestProviders> | ||
<AssistantOverlay isAssistantEnabled={true} /> | ||
</TestProviders> | ||
); | ||
fireEvent.keyDown(document, { key: ';', ctrlKey: true }); | ||
const closeButton = getByLabelText('Closes this modal window'); | ||
fireEvent.click(closeButton); | ||
const modal = queryByTestId('ai-assistant-modal'); | ||
expect(modal).not.toBeInTheDocument(); | ||
}); | ||
|
||
it('Assistant invoked from shortcut tracking happens on modal open only (not close)', () => { | ||
render( | ||
<TestProviders providerContext={{ assistantTelemetry }}> | ||
<AssistantOverlay isAssistantEnabled={true} /> | ||
</TestProviders> | ||
); | ||
fireEvent.keyDown(document, { key: ';', ctrlKey: true }); | ||
expect(reportAssistantInvoked).toHaveBeenCalledTimes(1); | ||
expect(reportAssistantInvoked).toHaveBeenCalledWith({ | ||
invokedBy: 'shortcut', | ||
conversationId: 'Welcome', | ||
}); | ||
fireEvent.keyDown(document, { key: ';', ctrlKey: true }); | ||
expect(reportAssistantInvoked).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
it('modal closes when shortcut is pressed and modal is already open', () => { | ||
const { queryByTestId } = render( | ||
<TestProviders> | ||
<AssistantOverlay isAssistantEnabled={true} /> | ||
</TestProviders> | ||
); | ||
fireEvent.keyDown(document, { key: ';', ctrlKey: true }); | ||
fireEvent.keyDown(document, { key: ';', ctrlKey: true }); | ||
const modal = queryByTestId('ai-assistant-modal'); | ||
expect(modal).not.toBeInTheDocument(); | ||
}); | ||
|
||
it('modal does not open when incorrect shortcut is pressed', () => { | ||
const { queryByTestId } = render( | ||
<TestProviders> | ||
<AssistantOverlay isAssistantEnabled={true} /> | ||
</TestProviders> | ||
); | ||
fireEvent.keyDown(document, { key: 'a', ctrlKey: true }); | ||
const modal = queryByTestId('ai-assistant-modal'); | ||
expect(modal).not.toBeInTheDocument(); | ||
}); | ||
}); |
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
75 changes: 75 additions & 0 deletions
75
x-pack/packages/kbn-elastic-assistant/impl/assistant/quick_prompts/quick_prompts.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,75 @@ | ||
/* | ||
* 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 { fireEvent, render } from '@testing-library/react'; | ||
import { QuickPrompts } from './quick_prompts'; | ||
import { TestProviders } from '../../mock/test_providers/test_providers'; | ||
import { MOCK_QUICK_PROMPTS } from '../../mock/quick_prompt'; | ||
import { QUICK_PROMPTS_TAB } from '../settings/assistant_settings'; | ||
|
||
const setInput = jest.fn(); | ||
const setIsSettingsModalVisible = jest.fn(); | ||
const trackPrompt = jest.fn(); | ||
const testProps = { | ||
setInput, | ||
setIsSettingsModalVisible, | ||
trackPrompt, | ||
}; | ||
const setSelectedSettingsTab = jest.fn(); | ||
const mockUseAssistantContext = { | ||
setSelectedSettingsTab, | ||
promptContexts: {}, | ||
allQuickPrompts: MOCK_QUICK_PROMPTS, | ||
}; | ||
|
||
const testTitle = 'SPL_QUERY_CONVERSION_TITLE'; | ||
const testPrompt = 'SPL_QUERY_CONVERSION_PROMPT'; | ||
const customTitle = 'A_CUSTOM_OPTION'; | ||
|
||
jest.mock('../../assistant_context', () => ({ | ||
...jest.requireActual('../../assistant_context'), | ||
useAssistantContext: () => mockUseAssistantContext, | ||
})); | ||
|
||
describe('QuickPrompts', () => { | ||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
it('onClickAddQuickPrompt calls setInput with the prompt, and trackPrompt with the prompt title', () => { | ||
const { getByText } = render( | ||
<TestProviders> | ||
<QuickPrompts {...testProps} /> | ||
</TestProviders> | ||
); | ||
fireEvent.click(getByText(testTitle)); | ||
|
||
expect(setInput).toHaveBeenCalledWith(testPrompt); | ||
expect(trackPrompt).toHaveBeenCalledWith(testTitle); | ||
}); | ||
it('onClickAddQuickPrompt calls trackPrompt with "Custom" when isDefault=false prompt is chosen', () => { | ||
const { getByText } = render( | ||
<TestProviders> | ||
<QuickPrompts {...testProps} /> | ||
</TestProviders> | ||
); | ||
fireEvent.click(getByText(customTitle)); | ||
|
||
expect(trackPrompt).toHaveBeenCalledWith('Custom'); | ||
}); | ||
|
||
it('clicking "Add quick prompt" button opens the settings modal', () => { | ||
const { getByTestId } = render( | ||
<TestProviders> | ||
<QuickPrompts {...testProps} /> | ||
</TestProviders> | ||
); | ||
fireEvent.click(getByTestId('addQuickPrompt')); | ||
expect(setIsSettingsModalVisible).toHaveBeenCalledWith(true); | ||
expect(setSelectedSettingsTab).toHaveBeenCalledWith(QUICK_PROMPTS_TAB); | ||
}); | ||
}); |
Oops, something went wrong.