@@ -210,4 +210,80 @@ describe('SplunkAPIClient unit tests', () => {
210210 } ) ;
211211 } ) ;
212212 } ) ;
213+
214+ describe ( 'oneshotSearch' , ( ) => {
215+ it ( 'throws when searchString is missing' , async ( ) => {
216+ await expect ( client . oneshotSearch ( '' ) ) . to . be . rejectedWith ( 'Missing searchString' ) ;
217+ } ) ;
218+
219+ it ( 'runs oneshot search after login' , async ( ) => {
220+ nock ( config . apiBaseUrl )
221+ . post ( '/services/auth/login' )
222+ . reply ( 200 , loginSuccessResponse , { 'Set-Cookie' : loginSuccessSetCookieHeader } ) ;
223+
224+ nock ( config . apiBaseUrl )
225+ . post ( '/servicesNS/admin/search/search/jobs' )
226+ . reply ( 200 , notFoundsResponse ) ;
227+
228+ const resp = await client . oneshotSearch ( 'search index=dx_aem_engineering | head 1' ) ;
229+ expect ( resp ) . to . deep . equal ( notFoundsResponse ) ;
230+ } ) ;
231+
232+ it ( 'reuses login across calls' , async ( ) => {
233+ nock ( config . apiBaseUrl )
234+ . post ( '/services/auth/login' )
235+ . once ( )
236+ . reply ( 200 , loginSuccessResponse , { 'Set-Cookie' : loginSuccessSetCookieHeader } ) ;
237+
238+ nock ( config . apiBaseUrl )
239+ . post ( '/servicesNS/admin/search/search/jobs' )
240+ . twice ( )
241+ . reply ( 200 , notFoundsResponse ) ;
242+
243+ await client . oneshotSearch ( 'search index=dx_aem_engineering | head 1' ) ;
244+ await client . oneshotSearch ( 'search index=dx_aem_engineering | head 1' ) ;
245+ } ) ;
246+
247+ it ( 'runs without logging in if loginObj is already set' , async ( ) => {
248+ client . loginObj = { sessionId : 'sessionKey123' , cookie : 'cookie123' } ;
249+
250+ nock ( config . apiBaseUrl )
251+ . post ( '/servicesNS/admin/search/search/jobs' )
252+ . reply ( 200 , notFoundsResponse ) ;
253+
254+ const resp = await client . oneshotSearch ( 'search index=dx_aem_engineering | head 1' ) ;
255+ expect ( resp ) . to . deep . equal ( notFoundsResponse ) ;
256+ } ) ;
257+
258+ it ( 'throws with non-200 response' , async ( ) => {
259+ nock ( config . apiBaseUrl )
260+ . post ( '/services/auth/login' )
261+ . reply ( 200 , loginSuccessResponse , { 'Set-Cookie' : loginSuccessSetCookieHeader } ) ;
262+
263+ nock ( config . apiBaseUrl )
264+ . post ( '/servicesNS/admin/search/search/jobs' )
265+ . reply ( 400 , { error : 'bad_request' } ) ;
266+
267+ await expect ( client . oneshotSearch ( 'search index=dx_aem_engineering | head 1' ) )
268+ . to . be . rejectedWith ( 'Splunk oneshot search failed. Status: 400' ) ;
269+ } ) ;
270+
271+ it ( 'propagates login parse errors (error is an Error instance)' , async ( ) => {
272+ nock ( config . apiBaseUrl )
273+ . post ( '/services/auth/login' )
274+ . reply ( 200 , invalidResponse ) ;
275+
276+ await expect ( client . oneshotSearch ( 'search index=dx_aem_engineering | head 1' ) )
277+ . to . be . rejected ;
278+ } ) ;
279+
280+ it ( 'throws when login fails (error is string)' , async ( ) => {
281+ nock ( config . apiBaseUrl )
282+ . post ( '/services/auth/login' )
283+ . reply ( 401 , loginFailedResponse ) ;
284+
285+ await expect ( client . oneshotSearch ( 'search index=dx_aem_engineering | head 1' ) )
286+ . to . be . rejectedWith ( 'Login failed' ) ;
287+ } ) ;
288+ } ) ;
213289} ) ;
0 commit comments