@@ -905,15 +905,96 @@ describe('S3Adapter tests', () => {
905
905
const options = {
906
906
bucket : 'bucket-1' ,
907
907
generateKey : ( ) => {
908
- throw 'Generate key failed' ;
908
+ throw new Error ( 'Generate key failed' ) ;
909
909
}
910
910
} ;
911
911
const s3 = new S3Adapter ( options ) ;
912
912
s3 . _s3Client = s3ClientMock ;
913
913
914
914
await expectAsync (
915
915
s3 . createFile ( 'file.txt' , 'hello world' , 'text/utf8' , { } )
916
- ) . toBeRejectedWith ( 'Generate key failed' ) ;
916
+ ) . toBeRejectedWithError ( 'Generate key failed' ) ;
917
+ } ) ;
918
+
919
+ it ( 'should handle async generateKey function' , async ( ) => {
920
+ const options = {
921
+ bucket : 'bucket-1' ,
922
+ generateKey : async ( filename ) => {
923
+ // Simulate async operation
924
+ await new Promise ( resolve => setTimeout ( resolve , 10 ) ) ;
925
+ return `async-${ filename } ` ;
926
+ }
927
+ } ;
928
+ const s3 = new S3Adapter ( options ) ;
929
+ s3ClientMock . send . and . returnValue ( Promise . resolve ( { } ) ) ;
930
+ s3 . _s3Client = s3ClientMock ;
931
+
932
+ await s3 . createFile ( 'file.txt' , 'hello world' , 'text/utf8' , { } ) ;
933
+
934
+ expect ( s3ClientMock . send ) . toHaveBeenCalledTimes ( 2 ) ;
935
+ const commands = s3ClientMock . send . calls . all ( ) ;
936
+ const commandArg = commands [ 1 ] . args [ 0 ] ;
937
+ expect ( commandArg ) . toBeInstanceOf ( PutObjectCommand ) ;
938
+ expect ( commandArg . input . Key ) . toBe ( 'async-file.txt' ) ;
939
+ } ) ;
940
+
941
+ it ( 'should handle generateKey that returns a Promise' , async ( ) => {
942
+ const options = {
943
+ bucket : 'bucket-1' ,
944
+ generateKey : ( filename ) => {
945
+ return Promise . resolve ( `promise-${ filename } ` ) ;
946
+ }
947
+ } ;
948
+ const s3 = new S3Adapter ( options ) ;
949
+ s3ClientMock . send . and . returnValue ( Promise . resolve ( { } ) ) ;
950
+ s3 . _s3Client = s3ClientMock ;
951
+
952
+ await s3 . createFile ( 'file.txt' , 'hello world' , 'text/utf8' , { } ) ;
953
+
954
+ expect ( s3ClientMock . send ) . toHaveBeenCalledTimes ( 2 ) ;
955
+ const commands = s3ClientMock . send . calls . all ( ) ;
956
+ const commandArg = commands [ 1 ] . args [ 0 ] ;
957
+ expect ( commandArg ) . toBeInstanceOf ( PutObjectCommand ) ;
958
+ expect ( commandArg . input . Key ) . toBe ( 'promise-file.txt' ) ;
959
+ } ) ;
960
+
961
+ it ( 'should validate generateKey returns a non-empty string' , async ( ) => {
962
+ const options = {
963
+ bucket : 'bucket-1' ,
964
+ generateKey : ( ) => ''
965
+ } ;
966
+ const s3 = new S3Adapter ( options ) ;
967
+ s3 . _s3Client = s3ClientMock ;
968
+
969
+ await expectAsync (
970
+ s3 . createFile ( 'file.txt' , 'hello world' , 'text/utf8' , { } )
971
+ ) . toBeRejectedWithError ( 'generateKey must return a non-empty string' ) ;
972
+ } ) ;
973
+
974
+ it ( 'should validate generateKey returns a string (not number)' , async ( ) => {
975
+ const options = {
976
+ bucket : 'bucket-1' ,
977
+ generateKey : ( ) => 12345
978
+ } ;
979
+ const s3 = new S3Adapter ( options ) ;
980
+ s3 . _s3Client = s3ClientMock ;
981
+
982
+ await expectAsync (
983
+ s3 . createFile ( 'file.txt' , 'hello world' , 'text/utf8' , { } )
984
+ ) . toBeRejectedWithError ( 'generateKey must return a non-empty string' ) ;
985
+ } ) ;
986
+
987
+ it ( 'should validate async generateKey returns a string' , async ( ) => {
988
+ const options = {
989
+ bucket : 'bucket-1' ,
990
+ generateKey : async ( ) => null
991
+ } ;
992
+ const s3 = new S3Adapter ( options ) ;
993
+ s3 . _s3Client = s3ClientMock ;
994
+
995
+ await expectAsync (
996
+ s3 . createFile ( 'file.txt' , 'hello world' , 'text/utf8' , { } )
997
+ ) . toBeRejectedWithError ( 'generateKey must return a non-empty string' ) ;
917
998
} ) ;
918
999
} ) ;
919
1000
0 commit comments