@@ -269,6 +269,112 @@ describe('FlexQueryClient', () => {
269269 expect ( mockGet ) . toHaveBeenCalledTimes ( 3 ) ;
270270 } ) ;
271271
272+ it . each ( [
273+ [ '1001' , 'Statement could not be generated at this time. Please try again shortly.' ] ,
274+ [ '1009' , 'The server is under heavy load and statement could not be generated at this time. Please try again shortly.' ] ,
275+ [ '1019' , 'Statement generation in progress. Please try again shortly.' ] ,
276+ [ '1021' , 'Statement could not be retrieved at this time. Please try again shortly.' ] ,
277+ ] ) ( 'should retry on transient GetStatement error code %s' , async ( code , message ) => {
278+ const sendResponseXml = `<?xml version="1.0" encoding="UTF-8"?>
279+ <FlexStatementResponse timestamp="26 August, 2023 01:59 PM EDT">
280+ <Status>Success</Status>
281+ <ReferenceCode>${ mockReferenceCode } </ReferenceCode>
282+ </FlexStatementResponse>` ;
283+
284+ const transientXml = `<?xml version="1.0" encoding="UTF-8"?>
285+ <FlexStatementResponse timestamp="26 August, 2023 02:00 PM EDT">
286+ <Status>Fail</Status>
287+ <ErrorCode>${ code } </ErrorCode>
288+ <ErrorMessage>${ message } </ErrorMessage>
289+ </FlexStatementResponse>` ;
290+
291+ const statementXml = `<?xml version="1.0" encoding="UTF-8"?>
292+ <FlexQueryResponse queryName="Test Query" type="AF">
293+ <FlexStatements count="1">
294+ <FlexStatement accountId="U12345" />
295+ </FlexStatements>
296+ </FlexQueryResponse>` ;
297+
298+ const mockGet = vi . fn ( )
299+ . mockResolvedValueOnce ( { data : sendResponseXml } )
300+ . mockResolvedValueOnce ( { data : transientXml } )
301+ . mockResolvedValueOnce ( { data : statementXml } ) ;
302+
303+ ( axios . create as any ) . mockReturnValue ( { get : mockGet } ) ;
304+ client = new FlexQueryClient ( { token : mockToken } ) ;
305+
306+ const result = await client . executeQuery ( mockQueryId , 3 , 10 ) ;
307+
308+ expect ( result ) . toEqual ( { data : statementXml } ) ;
309+ expect ( mockGet ) . toHaveBeenCalledTimes ( 3 ) ;
310+ } ) ;
311+
312+ it ( 'should not retry on terminal GetStatement errors (e.g. 1015 invalid token)' , async ( ) => {
313+ const sendResponseXml = `<?xml version="1.0" encoding="UTF-8"?>
314+ <FlexStatementResponse timestamp="26 August, 2023 01:59 PM EDT">
315+ <Status>Success</Status>
316+ <ReferenceCode>${ mockReferenceCode } </ReferenceCode>
317+ </FlexStatementResponse>` ;
318+
319+ const terminalXml = `<?xml version="1.0" encoding="UTF-8"?>
320+ <FlexStatementResponse timestamp="26 August, 2023 02:00 PM EDT">
321+ <Status>Fail</Status>
322+ <ErrorCode>1015</ErrorCode>
323+ <ErrorMessage>Token is invalid.</ErrorMessage>
324+ </FlexStatementResponse>` ;
325+
326+ const mockGet = vi . fn ( )
327+ . mockResolvedValueOnce ( { data : sendResponseXml } )
328+ . mockResolvedValueOnce ( { data : terminalXml } ) ;
329+
330+ ( axios . create as any ) . mockReturnValue ( { get : mockGet } ) ;
331+ client = new FlexQueryClient ( { token : mockToken } ) ;
332+
333+ const result = await client . executeQuery ( mockQueryId , 5 , 10 ) ;
334+
335+ expect ( result ) . toEqual ( {
336+ error : 'Token is invalid.' ,
337+ errorCode : '1015' ,
338+ } ) ;
339+ // Should bail after the first GetStatement attempt — no further retries
340+ expect ( mockGet ) . toHaveBeenCalledTimes ( 2 ) ;
341+ } ) ;
342+
343+ it ( 'should retry when fallback transient message casing differs' , async ( ) => {
344+ const sendResponseXml = `<?xml version="1.0" encoding="UTF-8"?>
345+ <FlexStatementResponse timestamp="26 August, 2023 01:59 PM EDT">
346+ <Status>Success</Status>
347+ <ReferenceCode>${ mockReferenceCode } </ReferenceCode>
348+ </FlexStatementResponse>` ;
349+
350+ const transientXml = `<?xml version="1.0" encoding="UTF-8"?>
351+ <FlexStatementResponse timestamp="26 August, 2023 02:00 PM EDT">
352+ <Status>Fail</Status>
353+ <ErrorCode>9999</ErrorCode>
354+ <ErrorMessage>Statement could not be retrieved. Try Again Shortly.</ErrorMessage>
355+ </FlexStatementResponse>` ;
356+
357+ const statementXml = `<?xml version="1.0" encoding="UTF-8"?>
358+ <FlexQueryResponse queryName="Test Query" type="AF">
359+ <FlexStatements count="1">
360+ <FlexStatement accountId="U12345" />
361+ </FlexStatements>
362+ </FlexQueryResponse>` ;
363+
364+ const mockGet = vi . fn ( )
365+ . mockResolvedValueOnce ( { data : sendResponseXml } )
366+ . mockResolvedValueOnce ( { data : transientXml } )
367+ . mockResolvedValueOnce ( { data : statementXml } ) ;
368+
369+ ( axios . create as any ) . mockReturnValue ( { get : mockGet } ) ;
370+ client = new FlexQueryClient ( { token : mockToken } ) ;
371+
372+ const result = await client . executeQuery ( mockQueryId , 3 , 10 ) ;
373+
374+ expect ( result ) . toEqual ( { data : statementXml } ) ;
375+ expect ( mockGet ) . toHaveBeenCalledTimes ( 3 ) ;
376+ } ) ;
377+
272378 it ( 'should return error when sendRequest fails' , async ( ) => {
273379 const sendResponseXml = `<?xml version="1.0" encoding="UTF-8"?>
274380<FlexStatementResponse timestamp="26 August, 2023 01:59 PM EDT">
@@ -375,4 +481,3 @@ describe('FlexQueryClient', () => {
375481 } ) ;
376482 } ) ;
377483} ) ;
378-
0 commit comments