11import * as fs from 'fs' ;
22import { PassThrough } from 'stream' ;
33
4- import { afterAll , beforeAll , describe , expect , test } from 'vitest' ;
4+ import { afterAll , assert , beforeAll , describe , expect , test } from 'vitest' ;
55
6+ import { StreamRequestError } from './' ;
67import { Client } from './client' ;
78
89let client : Client ;
@@ -38,11 +39,13 @@ describe('copy', () => {
3839 } ) ;
3940
4041 test ( 'does not exist' , async ( ) => {
41- try {
42- await client . copy ( 'bad-object-1' , 'should-never-happen' ) ;
43- } catch ( error ) {
44- expect ( error . code ) . toBe ( 404 ) ;
45- }
42+ const { ok, error } = await client . copy (
43+ 'bad-object-1' ,
44+ 'should-never-happen' ,
45+ ) ;
46+ expect ( ok ) . toBeFalsy ( ) ;
47+ expect ( error ?. message ) . toBeTruthy ( ) ;
48+ expect ( error ?. statusCode ) . toBe ( 404 ) ;
4649 } ) ;
4750} ) ;
4851
@@ -61,6 +64,21 @@ describe('delete', () => {
6164 expect ( existsResult . ok ) . toBeTruthy ( ) ;
6265 expect ( existsResult . value ) . toBeFalsy ( ) ;
6366 } ) ;
67+
68+ test ( 'does not exist (default)' , async ( ) => {
69+ const { ok, error } = await client . delete ( 'bad-object-1' ) ;
70+ expect ( ok ) . toBeFalsy ( ) ;
71+ expect ( error ?. message ) . toBeTruthy ( ) ;
72+ expect ( error ?. statusCode ) . toBe ( 404 ) ;
73+ } ) ;
74+
75+ test ( 'does not exist (ignore if not exists)' , async ( ) => {
76+ const { ok, value } = await client . delete ( 'bad-object-1' , {
77+ ignoreNotFound : true ,
78+ } ) ;
79+ expect ( ok ) . toBeTruthy ( ) ;
80+ expect ( value ) . toBeNull ( ) ;
81+ } ) ;
6482} ) ;
6583
6684describe ( 'downloadAsBytes' , ( ) => {
@@ -70,8 +88,18 @@ describe('downloadAsBytes', () => {
7088
7189 const { ok, value : buffer } = await client . downloadAsBytes ( objectName ) ;
7290 expect ( ok ) . toBeTruthy ( ) ;
91+ if ( buffer === undefined ) {
92+ assert . fail ( 'buffer must be defined' ) ;
93+ }
7394 expect ( buffer . toString ( ) ) . toBe ( testFileContents ) ;
7495 } ) ;
96+
97+ test ( 'does not exist' , async ( ) => {
98+ const { ok, error } = await client . downloadAsBytes ( 'bad-object-1' ) ;
99+ expect ( ok ) . toBeFalsy ( ) ;
100+ expect ( error ?. message ) . toBeTruthy ( ) ;
101+ expect ( error ?. statusCode ) . toBe ( 404 ) ;
102+ } ) ;
75103} ) ;
76104
77105describe ( 'downloadAsText' , ( ) => {
@@ -83,6 +111,13 @@ describe('downloadAsText', () => {
83111 expect ( ok ) . toBeTruthy ( ) ;
84112 expect ( text ) . toBe ( testFileContents ) ;
85113 } ) ;
114+
115+ test ( 'does not exist' , async ( ) => {
116+ const { ok, error } = await client . downloadAsText ( 'bad-object-1' ) ;
117+ expect ( ok ) . toBeFalsy ( ) ;
118+ expect ( error ?. message ) . toBeTruthy ( ) ;
119+ expect ( error ?. statusCode ) . toBe ( 404 ) ;
120+ } ) ;
86121} ) ;
87122
88123describe ( 'downloadToFilename' , ( ) => {
@@ -101,6 +136,17 @@ describe('downloadToFilename', () => {
101136
102137 fs . rmdirSync ( localTestDir , { recursive : true } ) ;
103138 } ) ;
139+
140+ test ( 'does not exist' , async ( ) => {
141+ const filename = './download-to-filename-1.txt' ;
142+ const { ok, error } = await client . downloadToFilename (
143+ 'bad-object-1' ,
144+ filename ,
145+ ) ;
146+ expect ( ok ) . toBeFalsy ( ) ;
147+ expect ( error ?. message ) . toBeTruthy ( ) ;
148+ expect ( error ?. statusCode ) . toBe ( 404 ) ;
149+ } ) ;
104150} ) ;
105151
106152describe ( 'downloadAsStream' , ( ) => {
@@ -109,13 +155,26 @@ describe('downloadAsStream', () => {
109155 await client . uploadFromText ( objectName , testFileContents ) ;
110156
111157 let contents = '' ;
112- const { ok, value : stream } = client . downloadAsStream ( objectName ) ;
113- expect ( ok ) . toBeTruthy ( ) ;
158+ const stream = client . downloadAsStream ( objectName ) ;
114159 await stream . forEach ( ( chunk : string ) => {
115160 contents += chunk . toString ( ) ;
116161 } ) ;
117162 expect ( contents ) . toBe ( testFileContents ) ;
118163 } ) ;
164+
165+ test ( 'does not exist' , async ( ) => {
166+ const stream = client . downloadAsStream ( 'bad-object-1' ) ;
167+ stream . on ( 'error' , ( error ) => {
168+ expect ( error ) . toBeInstanceOf ( StreamRequestError ) ;
169+ const requestError = ( error as StreamRequestError ) . getRequestError ( ) ;
170+ expect ( requestError . message ) . toBeTruthy ( ) ;
171+ expect ( requestError . statusCode ) . toBe ( 404 ) ;
172+ } ) ;
173+ await new Promise ( ( resolve ) => {
174+ stream . on ( 'end' , resolve ) ;
175+ stream . on ( 'error' , resolve ) ;
176+ } ) ;
177+ } ) ;
119178} ) ;
120179
121180describe ( 'exists' , ( ) => {
@@ -214,7 +273,10 @@ describe('uploadFromStream', () => {
214273} ) ;
215274
216275afterAll ( async ( ) => {
217- const { value : files } = await client . list ( ) ;
218- const deletions = files . map ( ( file ) => client . delete ( file ) ) ;
276+ const { value : objects } = await client . list ( ) ;
277+ if ( objects == undefined ) {
278+ assert . fail ( 'failed to get objects' ) ;
279+ }
280+ const deletions = objects . map ( ( object ) => client . delete ( object . name ) ) ;
219281 await Promise . all ( deletions ) ;
220282} ) ;
0 commit comments