11import { describe , it , expect , beforeEach , vi } from 'vitest' ;
2+ import { http , HttpResponse } from 'msw' ;
23import { PlatformSDKHttp } from './platform-sdk.js' ;
34import { AuthenticationError } from './errors.js' ;
4- import { setMockScenario } from './test-utils/http-mocks.js' ;
5+ import { setMockScenario , server } from './test-utils/http-mocks.js' ;
56import { ItemState , ItemTerminationReason } from './generated/api.js' ;
67
78describe ( 'PlatformSDK' , ( ) => {
@@ -552,7 +553,7 @@ describe('PlatformSDK', () => {
552553 await expect ( sdk . downloadArtifact ( 'test-run-id' , 'test-artifact-id' ) ) . rejects . toThrow (
553554 'Resource not found: '
554555 ) ;
555- } , 30000 ) ;
556+ } ) ;
556557
557558 it ( 'should handle no token for download artifact' , async ( ) => {
558559 mockTokenProvider . mockResolvedValue ( null ) ;
@@ -561,4 +562,43 @@ describe('PlatformSDK', () => {
561562 AuthenticationError
562563 ) ;
563564 } ) ;
565+
566+ it ( 'should retry on transient 500 error and succeed on second attempt' , async ( ) => {
567+ mockTokenProvider . mockResolvedValue ( 'mocked-token' ) ;
568+
569+ let callCount = 0 ;
570+ server . use (
571+ http . get ( '*/v1/runs/:runId/artifacts/:artifactId/file' , ( ) => {
572+ callCount ++ ;
573+ if ( callCount === 1 ) {
574+ return HttpResponse . json ( { } , { status : 500 } ) ;
575+ }
576+ return new HttpResponse ( new ArrayBuffer ( 8 ) , {
577+ status : 200 ,
578+ headers : { 'Content-Type' : 'application/octet-stream' } ,
579+ } ) ;
580+ } )
581+ ) ;
582+
583+ const result = await sdk . downloadArtifact ( 'test-run-id' , 'test-artifact-id' ) ;
584+ expect ( result . byteLength ) . toBe ( 8 ) ;
585+ expect ( callCount ) . toBe ( 2 ) ;
586+ } , 10_000 ) ;
587+
588+ it ( 'should not retry on 404 and make exactly one request' , async ( ) => {
589+ mockTokenProvider . mockResolvedValue ( 'mocked-token' ) ;
590+
591+ let callCount = 0 ;
592+ server . use (
593+ http . get ( '*/v1/runs/:runId/artifacts/:artifactId/file' , ( ) => {
594+ callCount ++ ;
595+ return HttpResponse . json ( { } , { status : 404 } ) ;
596+ } )
597+ ) ;
598+
599+ await expect ( sdk . downloadArtifact ( 'test-run-id' , 'test-artifact-id' ) ) . rejects . toThrow (
600+ 'Resource not found:'
601+ ) ;
602+ expect ( callCount ) . toBe ( 1 ) ;
603+ } ) ;
564604} ) ;
0 commit comments