From b66a80d3a9c7541882d60ba9f7a7a58c1ac6dc47 Mon Sep 17 00:00:00 2001 From: Yan Savitski Date: Wed, 20 Nov 2024 19:32:11 +0100 Subject: [PATCH 01/11] Add useIndicesValidation hook --- .../public/hooks/use_indices_validation.ts | 24 +++++++++++++++++++ .../public/hooks/use_query_indices.ts | 4 ++-- .../public/providers/form_provider.tsx | 24 ++++++++++++++++--- 3 files changed, 47 insertions(+), 5 deletions(-) create mode 100644 x-pack/plugins/search_playground/public/hooks/use_indices_validation.ts diff --git a/x-pack/plugins/search_playground/public/hooks/use_indices_validation.ts b/x-pack/plugins/search_playground/public/hooks/use_indices_validation.ts new file mode 100644 index 0000000000000..eab80a684ff1f --- /dev/null +++ b/x-pack/plugins/search_playground/public/hooks/use_indices_validation.ts @@ -0,0 +1,24 @@ +/* + * 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 { useEffect, useState } from 'react'; +import { useQueryIndices } from './use_query_indices'; + +export const useIndicesValidation = (unvalidatedIndices: string[]) => { + const [isValidated, setIsValidated] = useState(false); + const [validIndices, setValidIndices] = useState([]); + const { indices, isFetched: isIndicesLoaded } = useQueryIndices(); + + useEffect(() => { + if (isIndicesLoaded) { + setValidIndices(indices.filter((index) => unvalidatedIndices.includes(index))); + setIsValidated(true); + } + }, [unvalidatedIndices, indices, isIndicesLoaded]); + + return { isValidated, validIndices }; +}; diff --git a/x-pack/plugins/search_playground/public/hooks/use_query_indices.ts b/x-pack/plugins/search_playground/public/hooks/use_query_indices.ts index d011b471a68d6..bbc06f13daab1 100644 --- a/x-pack/plugins/search_playground/public/hooks/use_query_indices.ts +++ b/x-pack/plugins/search_playground/public/hooks/use_query_indices.ts @@ -15,7 +15,7 @@ export const useQueryIndices = ( ): { indices: IndexName[]; isLoading: boolean } => { const { services } = useKibana(); - const { data, isLoading } = useQuery({ + const { data, isLoading, isFetched } = useQuery({ queryKey: ['indices', query], queryFn: async () => { const response = await services.http.get<{ @@ -32,5 +32,5 @@ export const useQueryIndices = ( initialData: [], }); - return { indices: data, isLoading }; + return { indices: data, isLoading, isFetched }; }; diff --git a/x-pack/plugins/search_playground/public/providers/form_provider.tsx b/x-pack/plugins/search_playground/public/providers/form_provider.tsx index f352688fe89f1..e1b27e66a98df 100644 --- a/x-pack/plugins/search_playground/public/providers/form_provider.tsx +++ b/x-pack/plugins/search_playground/public/providers/form_provider.tsx @@ -7,6 +7,7 @@ import { FormProvider as ReactHookFormProvider, useForm } from 'react-hook-form'; import React, { useEffect, useMemo } from 'react'; import { useSearchParams } from 'react-router-dom-v5-compat'; +import { useIndicesValidation } from '../hooks/use_indices_validation'; import { useLoadFieldsByIndices } from '../hooks/use_load_fields_by_indices'; import { ChatForm, ChatFormFields } from '../types'; import { useLLMsModels } from '../hooks/use_llms_models'; @@ -53,16 +54,27 @@ export const FormProvider: React.FC> }) => { const models = useLLMsModels(); const [searchParams] = useSearchParams(); - const index = useMemo(() => searchParams.get('default-index'), [searchParams]); + const defaultIndex = useMemo(() => { + const index = searchParams.get('default-index'); + + return index ? [index] : null; + }, [searchParams]); const sessionState = useMemo(() => getLocalSession(storage), [storage]); const form = useForm({ defaultValues: { ...sessionState, - indices: index ? [index] : sessionState.indices, + indices: [], search_query: '', }, }); - useLoadFieldsByIndices({ watch: form.watch, setValue: form.setValue, getValues: form.getValues }); + const { isValidated: isValidatedIndices, validIndices } = useIndicesValidation( + defaultIndex || sessionState.indices || [] + ); + useLoadFieldsByIndices({ + watch: form.watch, + setValue: form.setValue, + getValues: form.getValues, + }); useEffect(() => { const subscription = form.watch((values) => @@ -80,5 +92,11 @@ export const FormProvider: React.FC> } }, [form, models]); + useEffect(() => { + if (isValidatedIndices) { + form.setValue(ChatFormFields.indices, validIndices); + } + }, [form, isValidatedIndices, validIndices]); + return {children}; }; From 9f119f599d0ddc95cba3605e6220f3cdd41d12be Mon Sep 17 00:00:00 2001 From: Yan Savitski Date: Wed, 20 Nov 2024 20:49:20 +0100 Subject: [PATCH 02/11] Add ftr test --- .../search/search_playground/playground_overview.ts | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/x-pack/test_serverless/functional/test_suites/search/search_playground/playground_overview.ts b/x-pack/test_serverless/functional/test_suites/search/search_playground/playground_overview.ts index 946afe08a0b73..4b89b49f54369 100644 --- a/x-pack/test_serverless/functional/test_suites/search/search_playground/playground_overview.ts +++ b/x-pack/test_serverless/functional/test_suites/search/search_playground/playground_overview.ts @@ -106,7 +106,7 @@ export default function ({ getPageObjects, getService }: FtrProviderContext) { }); describe('without any indices', () => { - it('hide no create index button when index added', async () => { + it('hide create index button when index added', async () => { await createIndex(); await pageObjects.searchPlayground.PlaygroundStartChatPage.expectOpenFlyoutAndSelectIndex(); }); @@ -129,6 +129,14 @@ export default function ({ getPageObjects, getService }: FtrProviderContext) { await pageObjects.searchPlayground.PlaygroundStartChatPage.expectToSelectIndicesAndLoadChat(); }); + it('load start page after removing selected index', async () => { + await pageObjects.searchPlayground.PlaygroundStartChatPage.expectToSelectIndicesAndLoadChat(); + await esArchiver.unload(esArchiveIndex); + await browser.refresh(); + await pageObjects.searchPlayground.PlaygroundStartChatPage.expectPlaygroundStartChatPageComponentsToExist(); + await pageObjects.searchPlayground.PlaygroundStartChatPage.expectCreateIndexButtonToExists(); + }); + after(async () => { await removeOpenAIConnector?.(); await esArchiver.unload(esArchiveIndex); From b7522518b59ef90efd0b880a261db1af12145426 Mon Sep 17 00:00:00 2001 From: Yan Savitski Date: Thu, 21 Nov 2024 00:37:10 +0100 Subject: [PATCH 03/11] Fix types --- .../public/hooks/use_indices_validation.ts | 6 +++--- .../search_playground/public/hooks/use_query_indices.ts | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/x-pack/plugins/search_playground/public/hooks/use_indices_validation.ts b/x-pack/plugins/search_playground/public/hooks/use_indices_validation.ts index eab80a684ff1f..76146ced57247 100644 --- a/x-pack/plugins/search_playground/public/hooks/use_indices_validation.ts +++ b/x-pack/plugins/search_playground/public/hooks/use_indices_validation.ts @@ -9,13 +9,13 @@ import { useEffect, useState } from 'react'; import { useQueryIndices } from './use_query_indices'; export const useIndicesValidation = (unvalidatedIndices: string[]) => { - const [isValidated, setIsValidated] = useState(false); - const [validIndices, setValidIndices] = useState([]); + const [isValidated, setIsValidated] = useState(false); + const [validIndices, setValidIndices] = useState([]); const { indices, isFetched: isIndicesLoaded } = useQueryIndices(); useEffect(() => { if (isIndicesLoaded) { - setValidIndices(indices.filter((index) => unvalidatedIndices.includes(index))); + setValidIndices(indices.filter((index) => unvalidatedIndices.includes(index))); setIsValidated(true); } }, [unvalidatedIndices, indices, isIndicesLoaded]); diff --git a/x-pack/plugins/search_playground/public/hooks/use_query_indices.ts b/x-pack/plugins/search_playground/public/hooks/use_query_indices.ts index bbc06f13daab1..983455243b0d3 100644 --- a/x-pack/plugins/search_playground/public/hooks/use_query_indices.ts +++ b/x-pack/plugins/search_playground/public/hooks/use_query_indices.ts @@ -12,7 +12,7 @@ import { APIRoutes } from '../types'; export const useQueryIndices = ( query: string = '' -): { indices: IndexName[]; isLoading: boolean } => { +): { indices: IndexName[]; isLoading: boolean; isFetched: boolean } => { const { services } = useKibana(); const { data, isLoading, isFetched } = useQuery({ From f86dc37c59c93bde3002cc6b92dc9c7abb05092b Mon Sep 17 00:00:00 2001 From: Yan Savitski Date: Thu, 21 Nov 2024 01:30:21 +0100 Subject: [PATCH 04/11] Fix filter type --- .../search_playground/public/hooks/use_indices_validation.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/x-pack/plugins/search_playground/public/hooks/use_indices_validation.ts b/x-pack/plugins/search_playground/public/hooks/use_indices_validation.ts index 76146ced57247..d2d3b363f21b9 100644 --- a/x-pack/plugins/search_playground/public/hooks/use_indices_validation.ts +++ b/x-pack/plugins/search_playground/public/hooks/use_indices_validation.ts @@ -15,7 +15,7 @@ export const useIndicesValidation = (unvalidatedIndices: string[]) => { useEffect(() => { if (isIndicesLoaded) { - setValidIndices(indices.filter((index) => unvalidatedIndices.includes(index))); + setValidIndices(indices.filter((index) => unvalidatedIndices.includes(index))); setIsValidated(true); } }, [unvalidatedIndices, indices, isIndicesLoaded]); From 77a1e84ba7dfc3b0f3cb96f311257f97748ea076 Mon Sep 17 00:00:00 2001 From: Yan Savitski Date: Thu, 21 Nov 2024 14:43:46 +0100 Subject: [PATCH 05/11] Fix types in test --- .../public/components/select_indices_flyout.test.tsx | 1 + 1 file changed, 1 insertion(+) diff --git a/x-pack/plugins/search_playground/public/components/select_indices_flyout.test.tsx b/x-pack/plugins/search_playground/public/components/select_indices_flyout.test.tsx index 246d7b2ebf246..59a606fddafd5 100644 --- a/x-pack/plugins/search_playground/public/components/select_indices_flyout.test.tsx +++ b/x-pack/plugins/search_playground/public/components/select_indices_flyout.test.tsx @@ -49,6 +49,7 @@ describe('SelectIndicesFlyout', () => { mockedUseQueryIndices.mockReturnValue({ indices: ['index1', 'index2', 'index3'], isLoading: false, + isFetched: true, }); }); From 7373c780e12f449859db05bfa35980efdaace830 Mon Sep 17 00:00:00 2001 From: Yan Savitski Date: Thu, 21 Nov 2024 17:36:59 +0100 Subject: [PATCH 06/11] Add target indices to getIndices request --- .../public/components/select_indices_flyout.tsx | 2 +- .../public/hooks/use_indices_validation.ts | 4 +++- .../public/hooks/use_query_indices.ts | 13 +++++++++---- x-pack/plugins/search_playground/server/routes.ts | 14 ++++++++++---- 4 files changed, 23 insertions(+), 10 deletions(-) diff --git a/x-pack/plugins/search_playground/public/components/select_indices_flyout.tsx b/x-pack/plugins/search_playground/public/components/select_indices_flyout.tsx index d98a6fc9de8b7..fa3868cb392c8 100644 --- a/x-pack/plugins/search_playground/public/components/select_indices_flyout.tsx +++ b/x-pack/plugins/search_playground/public/components/select_indices_flyout.tsx @@ -35,7 +35,7 @@ interface SelectIndicesFlyout { export const SelectIndicesFlyout: React.FC = ({ onClose }) => { const [query, setQuery] = useState(''); - const { indices, isLoading: isIndicesLoading } = useQueryIndices(query); + const { indices, isLoading: isIndicesLoading } = useQueryIndices({ query }); const { indices: selectedIndices, setIndices: setSelectedIndices } = useSourceIndicesFields(); const [selectedTempIndices, setSelectedTempIndices] = useState(selectedIndices); const handleSelectOptions = (options: EuiSelectableOption[]) => { diff --git a/x-pack/plugins/search_playground/public/hooks/use_indices_validation.ts b/x-pack/plugins/search_playground/public/hooks/use_indices_validation.ts index d2d3b363f21b9..cfab5f064868f 100644 --- a/x-pack/plugins/search_playground/public/hooks/use_indices_validation.ts +++ b/x-pack/plugins/search_playground/public/hooks/use_indices_validation.ts @@ -11,7 +11,9 @@ import { useQueryIndices } from './use_query_indices'; export const useIndicesValidation = (unvalidatedIndices: string[]) => { const [isValidated, setIsValidated] = useState(false); const [validIndices, setValidIndices] = useState([]); - const { indices, isFetched: isIndicesLoaded } = useQueryIndices(); + const { indices, isFetched: isIndicesLoaded } = useQueryIndices({ + targetIndices: unvalidatedIndices, + }); useEffect(() => { if (isIndicesLoaded) { diff --git a/x-pack/plugins/search_playground/public/hooks/use_query_indices.ts b/x-pack/plugins/search_playground/public/hooks/use_query_indices.ts index 983455243b0d3..0b334d8e11595 100644 --- a/x-pack/plugins/search_playground/public/hooks/use_query_indices.ts +++ b/x-pack/plugins/search_playground/public/hooks/use_query_indices.ts @@ -10,19 +10,24 @@ import { IndexName } from '@elastic/elasticsearch/lib/api/typesWithBodyKey'; import { useKibana } from './use_kibana'; import { APIRoutes } from '../types'; -export const useQueryIndices = ( - query: string = '' -): { indices: IndexName[]; isLoading: boolean; isFetched: boolean } => { +export const useQueryIndices = ({ + query, + targetIndices = [], +}: { + query?: string; + targetIndices?: string[]; +} = {}): { indices: IndexName[]; isLoading: boolean; isFetched: boolean } => { const { services } = useKibana(); const { data, isLoading, isFetched } = useQuery({ - queryKey: ['indices', query], + queryKey: ['indices', query, targetIndices.join(',')], queryFn: async () => { const response = await services.http.get<{ indices: string[]; }>(APIRoutes.GET_INDICES, { query: { search_query: query, + target_indices: targetIndices.join(','), size: 10, }, }); diff --git a/x-pack/plugins/search_playground/server/routes.ts b/x-pack/plugins/search_playground/server/routes.ts index 3cdebe11c02c2..119f7d8ea6620 100644 --- a/x-pack/plugins/search_playground/server/routes.ts +++ b/x-pack/plugins/search_playground/server/routes.ts @@ -198,22 +198,28 @@ export function defineRoutes({ query: schema.object({ search_query: schema.maybe(schema.string()), size: schema.number({ defaultValue: 10, min: 0 }), + target_indices: schema.maybe(schema.string()), }), }, }, errorHandler(logger)(async (context, request, response) => { - const { search_query: searchQuery, size } = request.query; + const { search_query: searchQuery, target_indices: targetIndices, size } = request.query; const { client: { asCurrentUser }, } = (await context.core).elasticsearch; + const targetIndexArray = targetIndices ? targetIndices.split(',') : []; const { indexNames } = await fetchIndices(asCurrentUser, searchQuery); - - const indexNameSlice = indexNames.slice(0, size).filter(isNotNullish); + const validTargetIndices = targetIndexArray.filter((targetIndex) => + indexNames.includes(targetIndex) + ); + const combinedIndices = Array.from(new Set([...validTargetIndices, ...indexNames])) + .slice(0, size) + .filter(isNotNullish); return response.ok({ body: { - indices: indexNameSlice, + indices: combinedIndices, }, headers: { 'content-type': 'application/json' }, }); From 1bf281f5aaed8d2a1705cb400cda8c7585f01dd5 Mon Sep 17 00:00:00 2001 From: Yan Savitski Date: Fri, 22 Nov 2024 12:42:35 +0100 Subject: [PATCH 07/11] Add validation by exact field --- .../public/hooks/use_indices_validation.ts | 3 +- .../public/hooks/use_query_indices.ts | 50 +++++++++++-------- .../server/lib/fetch_indices.ts | 5 +- .../search_playground/server/routes.ts | 16 ++---- 4 files changed, 40 insertions(+), 34 deletions(-) diff --git a/x-pack/plugins/search_playground/public/hooks/use_indices_validation.ts b/x-pack/plugins/search_playground/public/hooks/use_indices_validation.ts index cfab5f064868f..45b3848bb85e5 100644 --- a/x-pack/plugins/search_playground/public/hooks/use_indices_validation.ts +++ b/x-pack/plugins/search_playground/public/hooks/use_indices_validation.ts @@ -12,7 +12,8 @@ export const useIndicesValidation = (unvalidatedIndices: string[]) => { const [isValidated, setIsValidated] = useState(false); const [validIndices, setValidIndices] = useState([]); const { indices, isFetched: isIndicesLoaded } = useQueryIndices({ - targetIndices: unvalidatedIndices, + query: unvalidatedIndices.join(','), + exact: true, }); useEffect(() => { diff --git a/x-pack/plugins/search_playground/public/hooks/use_query_indices.ts b/x-pack/plugins/search_playground/public/hooks/use_query_indices.ts index 0b334d8e11595..64f76be36c799 100644 --- a/x-pack/plugins/search_playground/public/hooks/use_query_indices.ts +++ b/x-pack/plugins/search_playground/public/hooks/use_query_indices.ts @@ -10,32 +10,42 @@ import { IndexName } from '@elastic/elasticsearch/lib/api/typesWithBodyKey'; import { useKibana } from './use_kibana'; import { APIRoutes } from '../types'; -export const useQueryIndices = ({ - query, - targetIndices = [], -}: { - query?: string; - targetIndices?: string[]; -} = {}): { indices: IndexName[]; isLoading: boolean; isFetched: boolean } => { +export const useQueryIndices = ( + { + query, + exact, + }: { + query?: string; + exact?: boolean; + } = { query: '', exact: false } +): { indices: IndexName[]; isLoading: boolean; isFetched: boolean; isError: boolean } => { const { services } = useKibana(); - const { data, isLoading, isFetched } = useQuery({ - queryKey: ['indices', query, targetIndices.join(',')], + const { data, isLoading, isFetched, isError } = useQuery({ + queryKey: ['indices', query], queryFn: async () => { - const response = await services.http.get<{ - indices: string[]; - }>(APIRoutes.GET_INDICES, { - query: { - search_query: query, - target_indices: targetIndices.join(','), - size: 10, - }, - }); + try { + const response = await services.http.get<{ + indices: string[]; + }>(APIRoutes.GET_INDICES, { + query: { + search_query: query, + exact, + size: 10, + }, + }); - return response.indices; + return response.indices; + } catch (err) { + if (err?.response?.status === 404) { + return []; + } + + throw err; + } }, initialData: [], }); - return { indices: data, isLoading, isFetched }; + return { indices: data, isLoading, isFetched, isError }; }; diff --git a/x-pack/plugins/search_playground/server/lib/fetch_indices.ts b/x-pack/plugins/search_playground/server/lib/fetch_indices.ts index 51b72790028c2..c60e6b5082610 100644 --- a/x-pack/plugins/search_playground/server/lib/fetch_indices.ts +++ b/x-pack/plugins/search_playground/server/lib/fetch_indices.ts @@ -21,11 +21,12 @@ function isClosed(index: IndicesIndexState): boolean { export const fetchIndices = async ( client: ElasticsearchClient, - searchQuery: string | undefined + searchQuery: string | undefined, + { exact }: { exact?: boolean } = { exact: false } ): Promise<{ indexNames: string[]; }> => { - const indexPattern = searchQuery ? `*${searchQuery}*` : '*'; + const indexPattern = exact && searchQuery ? searchQuery : searchQuery ? `*${searchQuery}*` : '*'; const allIndexMatches = await client.indices.get({ expand_wildcards: ['open'], // for better performance only compute aliases and settings of indices but not mappings diff --git a/x-pack/plugins/search_playground/server/routes.ts b/x-pack/plugins/search_playground/server/routes.ts index 119f7d8ea6620..115b55d34146a 100644 --- a/x-pack/plugins/search_playground/server/routes.ts +++ b/x-pack/plugins/search_playground/server/routes.ts @@ -198,28 +198,22 @@ export function defineRoutes({ query: schema.object({ search_query: schema.maybe(schema.string()), size: schema.number({ defaultValue: 10, min: 0 }), - target_indices: schema.maybe(schema.string()), + exact: schema.maybe(schema.boolean({ defaultValue: false })), }), }, }, errorHandler(logger)(async (context, request, response) => { - const { search_query: searchQuery, target_indices: targetIndices, size } = request.query; + const { search_query: searchQuery, exact, size } = request.query; const { client: { asCurrentUser }, } = (await context.core).elasticsearch; - const targetIndexArray = targetIndices ? targetIndices.split(',') : []; - const { indexNames } = await fetchIndices(asCurrentUser, searchQuery); - const validTargetIndices = targetIndexArray.filter((targetIndex) => - indexNames.includes(targetIndex) - ); - const combinedIndices = Array.from(new Set([...validTargetIndices, ...indexNames])) - .slice(0, size) - .filter(isNotNullish); + const { indexNames } = await fetchIndices(asCurrentUser, searchQuery, { exact }); + const indexNameSlice = indexNames.slice(0, size).filter(isNotNullish); return response.ok({ body: { - indices: combinedIndices, + indices: indexNameSlice, }, headers: { 'content-type': 'application/json' }, }); From b5a82384d0bb35e3842c5b0f4867597cfb72a510 Mon Sep 17 00:00:00 2001 From: Yan Savitski Date: Fri, 22 Nov 2024 18:24:57 +0100 Subject: [PATCH 08/11] Fix types --- .../search_playground/public/hooks/use_query_indices.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/x-pack/plugins/search_playground/public/hooks/use_query_indices.ts b/x-pack/plugins/search_playground/public/hooks/use_query_indices.ts index 64f76be36c799..26a00f0a76668 100644 --- a/x-pack/plugins/search_playground/public/hooks/use_query_indices.ts +++ b/x-pack/plugins/search_playground/public/hooks/use_query_indices.ts @@ -18,10 +18,10 @@ export const useQueryIndices = ( query?: string; exact?: boolean; } = { query: '', exact: false } -): { indices: IndexName[]; isLoading: boolean; isFetched: boolean; isError: boolean } => { +): { indices: IndexName[]; isLoading: boolean; isFetched: boolean } => { const { services } = useKibana(); - const { data, isLoading, isFetched, isError } = useQuery({ + const { data, isLoading, isFetched } = useQuery({ queryKey: ['indices', query], queryFn: async () => { try { @@ -47,5 +47,5 @@ export const useQueryIndices = ( initialData: [], }); - return { indices: data, isLoading, isFetched, isError }; + return { indices: data, isLoading, isFetched }; }; From 4c68106704db677c437899cf15b384169da518a3 Mon Sep 17 00:00:00 2001 From: Yan Savitski Date: Fri, 22 Nov 2024 18:26:37 +0100 Subject: [PATCH 09/11] Return 50 indices to prevent unselect index bug --- .../plugins/search_playground/public/hooks/use_query_indices.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/x-pack/plugins/search_playground/public/hooks/use_query_indices.ts b/x-pack/plugins/search_playground/public/hooks/use_query_indices.ts index 26a00f0a76668..cc5812306097c 100644 --- a/x-pack/plugins/search_playground/public/hooks/use_query_indices.ts +++ b/x-pack/plugins/search_playground/public/hooks/use_query_indices.ts @@ -31,7 +31,7 @@ export const useQueryIndices = ( query: { search_query: query, exact, - size: 10, + size: 50, }, }); From 465d075806ee8cee7deabe50fca649ecd8f186a9 Mon Sep 17 00:00:00 2001 From: Yan Savitski Date: Mon, 25 Nov 2024 00:18:12 +0100 Subject: [PATCH 10/11] Fix tests --- .../public/providers/form_provider.test.tsx | 12 +++++++++--- .../search/search_playground/playground_overview.ts | 3 +++ 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/x-pack/plugins/search_playground/public/providers/form_provider.test.tsx b/x-pack/plugins/search_playground/public/providers/form_provider.test.tsx index 96c86ad184931..e0205f01f1e6a 100644 --- a/x-pack/plugins/search_playground/public/providers/form_provider.test.tsx +++ b/x-pack/plugins/search_playground/public/providers/form_provider.test.tsx @@ -19,6 +19,9 @@ jest.mock('../hooks/use_llms_models'); jest.mock('react-router-dom-v5-compat', () => ({ useSearchParams: jest.fn(() => [{ get: jest.fn() }]), })); +jest.mock('../hooks/use_indices_validation', () => ({ + useIndicesValidation: jest.fn((indices) => ({ isValidated: true, validIndices: indices })), +})); let formHookSpy: jest.SpyInstance; @@ -216,6 +219,7 @@ describe('FormProvider', () => { }); it('updates indices from search params', async () => { + expect.assertions(1); const mockSearchParams = new URLSearchParams(); mockSearchParams.get = jest.fn().mockReturnValue('new-index'); mockUseSearchParams.mockReturnValue([mockSearchParams]); @@ -237,10 +241,12 @@ describe('FormProvider', () => { ); - const { getValues } = formHookSpy.mock.results[0].value; + await act(async () => { + const { getValues } = formHookSpy.mock.results[0].value; - await waitFor(() => { - expect(getValues(ChatFormFields.indices)).toEqual(['new-index']); + await waitFor(() => { + expect(getValues(ChatFormFields.indices)).toEqual(['new-index']); + }); }); }); }); diff --git a/x-pack/test_serverless/functional/test_suites/search/search_playground/playground_overview.ts b/x-pack/test_serverless/functional/test_suites/search/search_playground/playground_overview.ts index 4b89b49f54369..b37b185f47e72 100644 --- a/x-pack/test_serverless/functional/test_suites/search/search_playground/playground_overview.ts +++ b/x-pack/test_serverless/functional/test_suites/search/search_playground/playground_overview.ts @@ -121,6 +121,9 @@ export default function ({ getPageObjects, getService }: FtrProviderContext) { before(async () => { await createConnector(); await createIndex(); + }); + + beforeEach(async () => { await pageObjects.searchPlayground.session.setSession(); await browser.refresh(); }); From 9f61325534006da0038b2d5de57390a4d7102279 Mon Sep 17 00:00:00 2001 From: Yan Savitski Date: Mon, 25 Nov 2024 11:47:05 +0100 Subject: [PATCH 11/11] Fix validation test --- x-pack/test/functional/page_objects/search_playground_page.ts | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/x-pack/test/functional/page_objects/search_playground_page.ts b/x-pack/test/functional/page_objects/search_playground_page.ts index 3a47da067097f..26828e42e05b5 100644 --- a/x-pack/test/functional/page_objects/search_playground_page.ts +++ b/x-pack/test/functional/page_objects/search_playground_page.ts @@ -55,6 +55,10 @@ export function SearchPlaygroundPageProvider({ getService }: FtrProviderContext) }, }, PlaygroundStartChatPage: { + async expectPlaygroundStartChatPageToBeLoaded() { + await testSubjects.existOrFail('setupPage'); + }, + async expectPlaygroundStartChatPageComponentsToExist() { await testSubjects.existOrFail('setupPage'); await testSubjects.existOrFail('connectLLMButton');