@@ -542,26 +542,23 @@ describe('PDPServer', () => {
542542 const mockUuid = '12345678-90ab-cdef-1234-567890abcdef'
543543
544544 server . use (
545- http . post < Record < string , never > , { pieceCid : string } > ( 'http://pdp.local/pdp/piece' , async ( { request } ) => {
546- try {
547- const body = await request . json ( )
548- assert . exists ( body . pieceCid )
549- return HttpResponse . text ( 'Created' , {
550- status : 201 ,
551- headers : {
552- Location : `/pdp/piece/upload/${ mockUuid } ` ,
553- } ,
554- } )
555- } catch ( error ) {
556- return HttpResponse . text ( ( error as Error ) . message , {
557- status : 400 ,
558- } )
559- }
545+ http . post ( 'http://pdp.local/pdp/piece/uploads' , async ( ) => {
546+ return HttpResponse . text ( 'Created' , {
547+ status : 201 ,
548+ headers : {
549+ Location : `/pdp/piece/uploads/${ mockUuid } ` ,
550+ } ,
551+ } )
560552 } ) ,
561- http . put ( 'http://pdp.local/pdp/piece/upload /:uuid' , async ( ) => {
553+ http . put ( 'http://pdp.local/pdp/piece/uploads /:uuid' , async ( ) => {
562554 return HttpResponse . text ( 'No Content' , {
563555 status : 204 ,
564556 } )
557+ } ) ,
558+ http . post ( 'http://pdp.local/pdp/piece/uploads/:uuid' , async ( ) => {
559+ return HttpResponse . text ( 'OK' , {
560+ status : 200 ,
561+ } )
565562 } )
566563 )
567564
@@ -577,26 +574,23 @@ describe('PDPServer', () => {
577574 const mockUuid = 'fedcba09-8765-4321-fedc-ba0987654321'
578575
579576 server . use (
580- http . post < Record < string , never > , { pieceCid : string } > ( 'http://pdp.local/pdp/piece' , async ( { request } ) => {
581- try {
582- const body = await request . json ( )
583- assert . exists ( body . pieceCid )
584- return HttpResponse . text ( 'Created' , {
585- status : 201 ,
586- headers : {
587- Location : `/pdp/piece/upload/${ mockUuid } ` ,
588- } ,
589- } )
590- } catch ( error ) {
591- return HttpResponse . text ( ( error as Error ) . message , {
592- status : 400 ,
593- } )
594- }
577+ http . post ( 'http://pdp.local/pdp/piece/uploads' , async ( ) => {
578+ return HttpResponse . text ( 'Created' , {
579+ status : 201 ,
580+ headers : {
581+ Location : `/pdp/piece/uploads/${ mockUuid } ` ,
582+ } ,
583+ } )
595584 } ) ,
596- http . put ( 'http://pdp.local/pdp/piece/upload /:uuid' , async ( ) => {
585+ http . put ( 'http://pdp.local/pdp/piece/uploads /:uuid' , async ( ) => {
597586 return HttpResponse . text ( 'No Content' , {
598587 status : 204 ,
599588 } )
589+ } ) ,
590+ http . post ( 'http://pdp.local/pdp/piece/uploads/:uuid' , async ( ) => {
591+ return HttpResponse . text ( 'OK' , {
592+ status : 200 ,
593+ } )
600594 } )
601595 )
602596
@@ -605,33 +599,42 @@ describe('PDPServer', () => {
605599 assert . equal ( result . size , 5 )
606600 } )
607601
608- it ( 'should handle existing piece (200 response) ' , async ( ) => {
602+ it ( 'should throw on create upload session error ' , async ( ) => {
609603 const testData = new Uint8Array ( [ 1 , 2 , 3 , 4 , 5 ] )
610- const mockPieceCid = 'bafkzcibcd4bdomn3tgwgrh3g532zopskstnbrd2n3sxfqbze7rxt7vqn7veigmy'
611604
612605 server . use (
613- http . post < Record < string , never > , { pieceCid : string } > ( 'http://pdp.local/pdp/piece' , async ( ) => {
614- return HttpResponse . json (
615- { pieceCid : mockPieceCid } ,
616- {
617- status : 200 ,
618- }
619- )
606+ http . post ( 'http://pdp.local/pdp/piece/uploads' , async ( ) => {
607+ return HttpResponse . text ( 'Database error' , {
608+ status : 500 ,
609+ } )
620610 } )
621611 )
622612
623- // Should not throw - existing piece is OK
624- const result = await pdpServer . uploadPiece ( testData )
625- assert . exists ( result . pieceCid )
626- assert . equal ( result . size , 5 )
613+ try {
614+ await pdpServer . uploadPiece ( testData )
615+ assert . fail ( 'Should have thrown error' )
616+ } catch ( error : any ) {
617+ assert . include ( error . message , 'Failed to create upload session' )
618+ assert . include ( error . message , '500' )
619+ assert . include ( error . message , 'Database error' )
620+ }
627621 } )
628622
629- it ( 'should throw on create upload session error' , async ( ) => {
623+ it ( 'should throw on upload error' , async ( ) => {
630624 const testData = new Uint8Array ( [ 1 , 2 , 3 , 4 , 5 ] )
625+ const mockUuid = '12345678-90ab-cdef-1234-567890abcdef'
631626
632627 server . use (
633- http . post < Record < string , never > , { pieceCid : string } > ( 'http://pdp.local/pdp/piece' , async ( ) => {
634- return HttpResponse . text ( 'Database error' , {
628+ http . post ( 'http://pdp.local/pdp/piece/uploads' , async ( ) => {
629+ return HttpResponse . text ( 'Created' , {
630+ status : 201 ,
631+ headers : {
632+ Location : `/pdp/piece/uploads/${ mockUuid } ` ,
633+ } ,
634+ } )
635+ } ) ,
636+ http . put ( 'http://pdp.local/pdp/piece/uploads/:uuid' , async ( ) => {
637+ return HttpResponse . text ( 'Upload failed' , {
635638 status : 500 ,
636639 } )
637640 } )
@@ -641,9 +644,44 @@ describe('PDPServer', () => {
641644 await pdpServer . uploadPiece ( testData )
642645 assert . fail ( 'Should have thrown error' )
643646 } catch ( error : any ) {
644- assert . include ( error . message , 'Failed to create upload session ' )
647+ assert . include ( error . message , 'Failed to upload piece ' )
645648 assert . include ( error . message , '500' )
646- assert . include ( error . message , 'Database error' )
649+ assert . include ( error . message , 'Upload failed' )
650+ }
651+ } )
652+
653+ it ( 'should throw on finalization error' , async ( ) => {
654+ const testData = new Uint8Array ( [ 1 , 2 , 3 , 4 , 5 ] )
655+ const mockUuid = '12345678-90ab-cdef-1234-567890abcdef'
656+
657+ server . use (
658+ http . post ( 'http://pdp.local/pdp/piece/uploads' , async ( ) => {
659+ return HttpResponse . text ( 'Created' , {
660+ status : 201 ,
661+ headers : {
662+ Location : `/pdp/piece/uploads/${ mockUuid } ` ,
663+ } ,
664+ } )
665+ } ) ,
666+ http . put ( 'http://pdp.local/pdp/piece/uploads/:uuid' , async ( ) => {
667+ return HttpResponse . text ( 'No Content' , {
668+ status : 204 ,
669+ } )
670+ } ) ,
671+ http . post ( 'http://pdp.local/pdp/piece/uploads/:uuid' , async ( ) => {
672+ return HttpResponse . text ( 'CommP mismatch' , {
673+ status : 400 ,
674+ } )
675+ } )
676+ )
677+
678+ try {
679+ await pdpServer . uploadPiece ( testData )
680+ assert . fail ( 'Should have thrown error' )
681+ } catch ( error : any ) {
682+ assert . include ( error . message , 'Failed to finalize upload' )
683+ assert . include ( error . message , '400' )
684+ assert . include ( error . message , 'CommP mismatch' )
647685 }
648686 } )
649687 } )
0 commit comments