1717 * under the License.
1818 */
1919
20+ import { of } from 'rxjs' ;
2021import type { SavedObject , SavedObjectsClientContract } from 'kibana/server' ;
22+ import type { SearchStrategyDependencies } from '../types' ;
2123import { savedObjectsClientMock } from '../../../../../core/server/mocks' ;
2224import { BackgroundSessionStatus } from '../../../common' ;
2325import { BACKGROUND_SESSION_TYPE } from '../../saved_objects' ;
@@ -28,6 +30,7 @@ describe('BackgroundSessionService', () => {
2830 let savedObjectsClient : jest . Mocked < SavedObjectsClientContract > ;
2931 let service : BackgroundSessionService ;
3032
33+ const sessionId = 'd7170a35-7e2c-48d6-8dec-9a056721b489' ;
3134 const mockSavedObject : SavedObject = {
3235 id : 'd7170a35-7e2c-48d6-8dec-9a056721b489' ,
3336 type : BACKGROUND_SESSION_TYPE ,
@@ -45,9 +48,13 @@ describe('BackgroundSessionService', () => {
4548 service = new BackgroundSessionService ( ) ;
4649 } ) ;
4750
48- it ( 'save throws if `name` is not provided' , ( ) => {
49- const sessionId = 'd7170a35-7e2c-48d6-8dec-9a056721b489' ;
51+ it ( 'search throws if `name` is not provided' , ( ) => {
52+ expect ( ( ) => service . save ( sessionId , { } , { savedObjectsClient } ) ) . rejects . toMatchInlineSnapshot (
53+ `[Error: Name is required]`
54+ ) ;
55+ } ) ;
5056
57+ it ( 'save throws if `name` is not provided' , ( ) => {
5158 expect ( ( ) => service . save ( sessionId , { } , { savedObjectsClient } ) ) . rejects . toMatchInlineSnapshot (
5259 `[Error: Name is required]`
5360 ) ;
@@ -56,7 +63,6 @@ describe('BackgroundSessionService', () => {
5663 it ( 'get calls saved objects client' , async ( ) => {
5764 savedObjectsClient . get . mockResolvedValue ( mockSavedObject ) ;
5865
59- const sessionId = 'd7170a35-7e2c-48d6-8dec-9a056721b489' ;
6066 const response = await service . get ( sessionId , { savedObjectsClient } ) ;
6167
6268 expect ( response ) . toBe ( mockSavedObject ) ;
@@ -93,7 +99,6 @@ describe('BackgroundSessionService', () => {
9399 } ;
94100 savedObjectsClient . update . mockResolvedValue ( mockUpdateSavedObject ) ;
95101
96- const sessionId = 'd7170a35-7e2c-48d6-8dec-9a056721b489' ;
97102 const attributes = { name : 'new_name' } ;
98103 const response = await service . update ( sessionId , attributes , { savedObjectsClient } ) ;
99104
@@ -108,19 +113,87 @@ describe('BackgroundSessionService', () => {
108113 it ( 'delete calls saved objects client' , async ( ) => {
109114 savedObjectsClient . delete . mockResolvedValue ( { } ) ;
110115
111- const sessionId = 'd7170a35-7e2c-48d6-8dec-9a056721b489' ;
112116 const response = await service . delete ( sessionId , { savedObjectsClient } ) ;
113117
114118 expect ( response ) . toEqual ( { } ) ;
115119 expect ( savedObjectsClient . delete ) . toHaveBeenCalledWith ( BACKGROUND_SESSION_TYPE , sessionId ) ;
116120 } ) ;
117121
122+ describe ( 'search' , ( ) => {
123+ const mockSearch = jest . fn ( ) . mockReturnValue ( of ( { } ) ) ;
124+ const mockStrategy = { search : mockSearch } ;
125+ const mockDeps = { } as SearchStrategyDependencies ;
126+
127+ beforeEach ( ( ) => {
128+ mockSearch . mockClear ( ) ;
129+ } ) ;
130+
131+ it ( 'searches using the original request if not restoring' , async ( ) => {
132+ const searchRequest = { params : { } } ;
133+ const options = { sessionId, isStored : false , isRestore : false } ;
134+
135+ await service . search ( mockStrategy , searchRequest , options , mockDeps ) . toPromise ( ) ;
136+
137+ expect ( mockSearch ) . toBeCalledWith ( searchRequest , options , mockDeps ) ;
138+ } ) ;
139+
140+ it ( 'searches using the original request if `id` is provided' , async ( ) => {
141+ const searchId = 'FnpFYlBpeXdCUTMyZXhCLTc1TWFKX0EbdDFDTzJzTE1Sck9PVTBIcW1iU05CZzo4MDA0' ;
142+ const searchRequest = { id : searchId , params : { } } ;
143+ const options = { sessionId, isStored : true , isRestore : true } ;
144+
145+ await service . search ( mockStrategy , searchRequest , options , mockDeps ) . toPromise ( ) ;
146+
147+ expect ( mockSearch ) . toBeCalledWith ( searchRequest , options , mockDeps ) ;
148+ } ) ;
149+
150+ it ( 'searches by looking up an `id` if restoring and `id` is not provided' , async ( ) => {
151+ const searchRequest = { params : { } } ;
152+ const options = { sessionId, isStored : true , isRestore : true } ;
153+ const spyGetId = jest . spyOn ( service , 'getId' ) . mockResolvedValueOnce ( 'my_id' ) ;
154+
155+ await service . search ( mockStrategy , searchRequest , options , mockDeps ) . toPromise ( ) ;
156+
157+ expect ( mockSearch ) . toBeCalledWith ( { ...searchRequest , id : 'my_id' } , options , mockDeps ) ;
158+
159+ spyGetId . mockRestore ( ) ;
160+ } ) ;
161+
162+ it ( 'calls `trackId` once if the response contains an `id` and not restoring' , async ( ) => {
163+ const searchRequest = { params : { } } ;
164+ const options = { sessionId, isStored : false , isRestore : false } ;
165+ const spyTrackId = jest . spyOn ( service , 'trackId' ) . mockResolvedValue ( ) ;
166+ mockSearch . mockReturnValueOnce ( of ( { id : 'my_id' } , { id : 'my_id' } ) ) ;
167+
168+ await service . search ( mockStrategy , searchRequest , options , mockDeps ) . toPromise ( ) ;
169+
170+ expect ( spyTrackId ) . toBeCalledTimes ( 1 ) ;
171+ expect ( spyTrackId ) . toBeCalledWith ( searchRequest , 'my_id' , options , mockDeps ) ;
172+
173+ spyTrackId . mockRestore ( ) ;
174+ } ) ;
175+
176+ it ( 'does not call `trackId` if restoring' , async ( ) => {
177+ const searchRequest = { params : { } } ;
178+ const options = { sessionId, isStored : true , isRestore : true } ;
179+ const spyGetId = jest . spyOn ( service , 'getId' ) . mockResolvedValueOnce ( 'my_id' ) ;
180+ const spyTrackId = jest . spyOn ( service , 'trackId' ) . mockResolvedValue ( ) ;
181+ mockSearch . mockReturnValueOnce ( of ( { id : 'my_id' } ) ) ;
182+
183+ await service . search ( mockStrategy , searchRequest , options , mockDeps ) . toPromise ( ) ;
184+
185+ expect ( spyTrackId ) . not . toBeCalled ( ) ;
186+
187+ spyGetId . mockRestore ( ) ;
188+ spyTrackId . mockRestore ( ) ;
189+ } ) ;
190+ } ) ;
191+
118192 describe ( 'trackId' , ( ) => {
119193 it ( 'stores hash in memory when `isStored` is `false` for when `save` is called' , async ( ) => {
120194 const searchRequest = { params : { } } ;
121195 const requestHash = createRequestHash ( searchRequest . params ) ;
122196 const searchId = 'FnpFYlBpeXdCUTMyZXhCLTc1TWFKX0EbdDFDTzJzTE1Sck9PVTBIcW1iU05CZzo4MDA0' ;
123- const sessionId = 'd7170a35-7e2c-48d6-8dec-9a056721b489' ;
124197 const isStored = false ;
125198 const name = 'my saved background search session' ;
126199 const appId = 'my_app_id' ;
@@ -164,7 +237,6 @@ describe('BackgroundSessionService', () => {
164237 const searchRequest = { params : { } } ;
165238 const requestHash = createRequestHash ( searchRequest . params ) ;
166239 const searchId = 'FnpFYlBpeXdCUTMyZXhCLTc1TWFKX0EbdDFDTzJzTE1Sck9PVTBIcW1iU05CZzo4MDA0' ;
167- const sessionId = 'd7170a35-7e2c-48d6-8dec-9a056721b489' ;
168240 const isStored = true ;
169241
170242 await service . trackId (
@@ -191,7 +263,6 @@ describe('BackgroundSessionService', () => {
191263
192264 it ( 'throws if there is not a saved object' , ( ) => {
193265 const searchRequest = { params : { } } ;
194- const sessionId = 'd7170a35-7e2c-48d6-8dec-9a056721b489' ;
195266
196267 expect ( ( ) =>
197268 service . getId ( searchRequest , { sessionId, isStored : false } , { savedObjectsClient } )
@@ -202,7 +273,6 @@ describe('BackgroundSessionService', () => {
202273
203274 it ( 'throws if not restoring a saved session' , ( ) => {
204275 const searchRequest = { params : { } } ;
205- const sessionId = 'd7170a35-7e2c-48d6-8dec-9a056721b489' ;
206276
207277 expect ( ( ) =>
208278 service . getId (
@@ -219,7 +289,6 @@ describe('BackgroundSessionService', () => {
219289 const searchRequest = { params : { } } ;
220290 const requestHash = createRequestHash ( searchRequest . params ) ;
221291 const searchId = 'FnpFYlBpeXdCUTMyZXhCLTc1TWFKX0EbdDFDTzJzTE1Sck9PVTBIcW1iU05CZzo4MDA0' ;
222- const sessionId = 'd7170a35-7e2c-48d6-8dec-9a056721b489' ;
223292 const mockSession = {
224293 id : 'd7170a35-7e2c-48d6-8dec-9a056721b489' ,
225294 type : BACKGROUND_SESSION_TYPE ,
0 commit comments