-
Notifications
You must be signed in to change notification settings - Fork 924
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
[query assist] use badge to show agent errors #7998
Merged
Merged
Changes from 19 commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
6545d48
enable query assist for index patterns without MDS
joshuali925 4a5fe8e
add agent error parsing utils
joshuali925 6c07257
update ml-commons response schema processing
joshuali925 043f83b
use badge to show query assist error if possible
joshuali925 84d992a
avoid enter key triggering warning badge instead of submit button
joshuali925 f5140ea
address comments
joshuali925 898f072
update storage for upstream change
joshuali925 e3af9ac
add unit tests
joshuali925 40749a1
add i18n for badge ariaLabel
joshuali925 69e4545
change indices to indexes in unit tests
joshuali925 000bd30
Merge remote-tracking branch 'origin/main' into qa-2.17
joshuali925 d02c5a4
update sidebar
joshuali925 09354cf
add i18n for input placeholder
joshuali925 46ada24
move inline style to scss
joshuali925 8b8fb1d
change warning to error in query assist badge
joshuali925 f379ee1
calculate badge width at runtime
joshuali925 0de06f6
refactor: append badge and hide it when input is focused
joshuali925 48c42fc
avoid `display: none` when badge is focused
joshuali925 b83fdec
update unit tests
joshuali925 d948c9d
change unicode to eui icon
joshuali925 711fe18
truncate text in input when unfocused
joshuali925 e814ad2
change badge color to danger
joshuali925 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
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
47 changes: 47 additions & 0 deletions
47
..._enhancements/public/query_assist/components/__snapshots__/query_assist_bar.test.tsx.snap
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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
218 changes: 218 additions & 0 deletions
218
src/plugins/query_enhancements/public/query_assist/components/query_assist_bar.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,218 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import { fireEvent, render, screen, waitFor } from '@testing-library/react'; | ||
import React, { ComponentProps, PropsWithChildren } from 'react'; | ||
import { IntlProvider } from 'react-intl'; | ||
import { of } from 'rxjs'; | ||
import { QueryAssistBar } from '.'; | ||
import { notificationServiceMock, uiSettingsServiceMock } from '../../../../../core/public/mocks'; | ||
import { DataStorage } from '../../../../data/common'; | ||
import { QueryEditorExtensionDependencies, QueryStringContract } from '../../../../data/public'; | ||
import { dataPluginMock } from '../../../../data/public/mocks'; | ||
import { useOpenSearchDashboards } from '../../../../opensearch_dashboards_react/public'; | ||
import { setData, setStorage } from '../../services'; | ||
import { useGenerateQuery } from '../hooks'; | ||
import { AgentError, ProhibitedQueryError } from '../utils'; | ||
import { QueryAssistInput } from './query_assist_input'; | ||
|
||
jest.mock('../../../../opensearch_dashboards_react/public', () => ({ | ||
useOpenSearchDashboards: jest.fn(), | ||
withOpenSearchDashboards: jest.fn((component: React.Component) => component), | ||
})); | ||
|
||
jest.mock('../hooks', () => ({ | ||
useGenerateQuery: jest.fn().mockReturnValue({ generateQuery: jest.fn(), loading: false }), | ||
})); | ||
|
||
jest.mock('./query_assist_input', () => ({ | ||
QueryAssistInput: ({ inputRef, error }: ComponentProps<typeof QueryAssistInput>) => ( | ||
<> | ||
<input ref={inputRef} /> | ||
<div>{JSON.stringify(error)}</div> | ||
</> | ||
), | ||
})); | ||
|
||
const dataMock = dataPluginMock.createStartContract(true); | ||
const queryStringMock = dataMock.query.queryString as jest.Mocked<QueryStringContract>; | ||
const uiSettingsMock = uiSettingsServiceMock.createStartContract(); | ||
const notificationsMock = notificationServiceMock.createStartContract(); | ||
|
||
setData(dataMock); | ||
setStorage(new DataStorage(window.localStorage, 'mock-prefix')); | ||
|
||
const dependencies: QueryEditorExtensionDependencies = { | ||
language: 'PPL', | ||
onSelectLanguage: jest.fn(), | ||
isCollapsed: false, | ||
setIsCollapsed: jest.fn(), | ||
}; | ||
|
||
type Props = ComponentProps<typeof QueryAssistBar>; | ||
|
||
const IntlWrapper = ({ children }: PropsWithChildren<unknown>) => ( | ||
<IntlProvider locale="en">{children}</IntlProvider> | ||
); | ||
|
||
const renderQueryAssistBar = (overrideProps: Partial<Props> = {}) => { | ||
const props: Props = Object.assign<Props, Partial<Props>>({ dependencies }, overrideProps); | ||
const component = render(<QueryAssistBar {...props} />, { | ||
wrapper: IntlWrapper, | ||
}); | ||
return { component, props: props as jest.MockedObjectDeep<Props> }; | ||
}; | ||
|
||
describe('QueryAssistBar', () => { | ||
beforeEach(() => { | ||
(useOpenSearchDashboards as jest.Mock).mockReturnValue({ | ||
services: { | ||
data: dataMock, | ||
uiSettings: uiSettingsMock, | ||
notifications: notificationsMock, | ||
}, | ||
}); | ||
}); | ||
afterEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it('renders null if collapsed', () => { | ||
const { component } = renderQueryAssistBar({ | ||
dependencies: { ...dependencies, isCollapsed: true }, | ||
}); | ||
expect(component.container).toBeEmptyDOMElement(); | ||
}); | ||
|
||
it('matches snapshot', () => { | ||
const { component } = renderQueryAssistBar(); | ||
expect(component.container).toMatchSnapshot(); | ||
}); | ||
|
||
it('displays callout when query input is empty on submit', async () => { | ||
renderQueryAssistBar(); | ||
|
||
fireEvent.click(screen.getByRole('button')); | ||
|
||
await waitFor(() => { | ||
expect(screen.getByTestId('query-assist-empty-query-callout')).toBeInTheDocument(); | ||
}); | ||
}); | ||
|
||
it('displays callout when dataset is not selected on submit', async () => { | ||
queryStringMock.getQuery.mockReturnValueOnce({ query: '', language: 'kuery' }); | ||
queryStringMock.getUpdates$.mockReturnValueOnce(of({ query: '', language: 'kuery' })); | ||
renderQueryAssistBar(); | ||
|
||
fireEvent.change(screen.getByRole('textbox'), { target: { value: 'test query' } }); | ||
fireEvent.click(screen.getByRole('button')); | ||
|
||
await waitFor(() => { | ||
expect(screen.getByTestId('query-assist-empty-index-callout')).toBeInTheDocument(); | ||
}); | ||
}); | ||
|
||
it('displays callout for guardrail errors', async () => { | ||
const generateQueryMock = jest.fn().mockResolvedValue({ error: new ProhibitedQueryError() }); | ||
(useGenerateQuery as jest.Mock).mockReturnValue({ | ||
generateQuery: generateQueryMock, | ||
loading: false, | ||
}); | ||
|
||
renderQueryAssistBar(); | ||
|
||
fireEvent.change(screen.getByRole('textbox'), { target: { value: 'test query' } }); | ||
fireEvent.click(screen.getByRole('button')); | ||
|
||
await waitFor(() => { | ||
expect(screen.getByTestId('query-assist-guard-callout')).toBeInTheDocument(); | ||
}); | ||
}); | ||
|
||
it('passes agent errors to input', async () => { | ||
const generateQueryMock = jest.fn().mockResolvedValue({ | ||
error: new AgentError({ | ||
error: { type: 'mock-type', reason: 'mock-reason', details: 'mock-details' }, | ||
status: 303, | ||
}), | ||
}); | ||
(useGenerateQuery as jest.Mock).mockReturnValue({ | ||
generateQuery: generateQueryMock, | ||
loading: false, | ||
}); | ||
|
||
renderQueryAssistBar(); | ||
|
||
fireEvent.change(screen.getByRole('textbox'), { target: { value: 'test query' } }); | ||
fireEvent.click(screen.getByRole('button')); | ||
|
||
await waitFor(() => { | ||
expect(screen.getByText(/mock-reason/)).toBeInTheDocument(); | ||
}); | ||
}); | ||
|
||
it('displays toast for other unknown errors', async () => { | ||
const mockError = new Error('mock-error'); | ||
const generateQueryMock = jest.fn().mockResolvedValue({ | ||
error: mockError, | ||
}); | ||
(useGenerateQuery as jest.Mock).mockReturnValue({ | ||
generateQuery: generateQueryMock, | ||
loading: false, | ||
}); | ||
|
||
renderQueryAssistBar(); | ||
|
||
fireEvent.change(screen.getByRole('textbox'), { target: { value: 'test query' } }); | ||
fireEvent.click(screen.getByRole('button')); | ||
|
||
await waitFor(() => { | ||
expect(notificationsMock.toasts.addError).toHaveBeenCalledWith(mockError, { | ||
title: 'Failed to generate results', | ||
}); | ||
}); | ||
}); | ||
|
||
it('submits a valid query and updates services', async () => { | ||
const generateQueryMock = jest | ||
.fn() | ||
.mockResolvedValue({ response: { query: 'generated query' } }); | ||
(useGenerateQuery as jest.Mock).mockReturnValue({ | ||
generateQuery: generateQueryMock, | ||
loading: false, | ||
}); | ||
|
||
renderQueryAssistBar(); | ||
|
||
fireEvent.change(screen.getByRole('textbox'), { target: { value: 'test query' } }); | ||
fireEvent.click(screen.getByRole('button')); | ||
|
||
await waitFor(() => { | ||
expect(generateQueryMock).toHaveBeenCalledWith({ | ||
question: 'test query', | ||
index: 'Default Index Pattern', | ||
language: 'PPL', | ||
dataSourceId: 'mock-data-source-id', | ||
}); | ||
}); | ||
|
||
expect(queryStringMock.setQuery).toHaveBeenCalledWith({ | ||
dataset: { | ||
dataSource: { | ||
id: 'mock-data-source-id', | ||
title: 'Default Data Source', | ||
type: 'OpenSearch', | ||
}, | ||
id: 'default-index-pattern', | ||
timeFieldName: '@timestamp', | ||
title: 'Default Index Pattern', | ||
type: 'INDEX_PATTERN', | ||
}, | ||
language: 'PPL', | ||
query: 'generated query', | ||
}); | ||
expect(screen.getByTestId('query-assist-query-generated-callout')).toBeInTheDocument(); | ||
}); | ||
}); |
Oops, something went wrong.
Oops, something went wrong.
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.
nice.
but i think one of the tests will fail now. so might have to just update the test to expect this now.
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.
weird i ran
yarn test:jest
before pushing, they passed locally