11import provider , { NimS3Client } from '@nimbella/storage/providers/s3'
22import { StorageKey } from '@nimbella/storage/providers/interface'
3+ import { S3RemoteFile } from '../src/providers/s3'
4+ import { S3Client } from '@aws-sdk/client-s3'
35
46describe ( 'test prepareCredentials method' , ( ) => {
57 test ( 'should return credentials instance from input parameters' , ( ) => {
@@ -25,3 +27,39 @@ describe('test getClient method', () => {
2527 expect ( client . getBucketName ( ) ) . toEqual ( `data-${ namespace } -api-nimbella-io` )
2628 } )
2729} )
30+
31+ describe ( 'test file.exists() method' , ( ) => {
32+ test ( 'should return true if file exists' , async ( ) => {
33+ const client = { send : async ( ) => ( { storageClass : null , ContentLength : null , etag : null } ) }
34+ const file = new S3RemoteFile ( ( client as unknown ) as S3Client , '' , '' , false )
35+ expect ( await file . exists ( ) ) . toBeTruthy ( )
36+ } )
37+ test ( 'should return false if file does not exist' , async ( ) => {
38+ class ServiceError extends Error {
39+ "$metadata" : { }
40+ }
41+ const client = {
42+ send : async ( ) => {
43+ const err = new ServiceError ( 'Missing file' )
44+ err [ "$metadata" ] = { httpStatusCode : 404 }
45+ throw err
46+ }
47+ }
48+ const file = new S3RemoteFile ( ( client as unknown ) as S3Client , '' , '' , false )
49+ expect ( await file . exists ( ) ) . toBeFalsy ( )
50+ } )
51+ test ( 'should throw error otherwise' , async ( ) => {
52+ class ServiceError extends Error {
53+ "$metadata" : { }
54+ }
55+ const client = {
56+ send : async ( ) => {
57+ const err = new ServiceError ( 'Some error message' )
58+ err [ "$metadata" ] = { httpStatusCode : 500 }
59+ throw err
60+ }
61+ }
62+ const file = new S3RemoteFile ( ( client as unknown ) as S3Client , '' , '' , false )
63+ return expect ( file . exists ( ) ) . rejects . toThrow ( 'Some error message' ) ;
64+ } )
65+ } )
0 commit comments