Skip to content

Commit 74a0d49

Browse files
committed
test: add multipart file uploads in network facade service
1 parent 3599d38 commit 74a0d49

File tree

1 file changed

+73
-0
lines changed

1 file changed

+73
-0
lines changed

test/services/network/network-facade.service.test.ts

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import axios from 'axios';
1212
import { fail } from 'node:assert';
1313
import crypto from 'node:crypto';
1414
import { HashStream } from '../../../src/utils/hash.utils';
15+
import { UploadMultipartOptions } from '../../../src/types/network.types';
1516

1617
describe('Network Facade Service', () => {
1718
beforeEach(() => {
@@ -304,4 +305,76 @@ describe('Network Facade Service', () => {
304305

305306
expect(options.progressCallback).toHaveBeenCalledWith(100);
306307
});
308+
309+
it('When a file is uploaded via multipart, then it should report progress', async () => {
310+
const bucket = 'f1858bc9675f9e4f7ab29429';
311+
const networkMock = getNetworkMock();
312+
313+
const sut = new NetworkFacade(
314+
networkMock,
315+
UploadService.instance,
316+
DownloadService.instance,
317+
CryptoService.instance,
318+
);
319+
const file = crypto.randomBytes(16).toString('hex');
320+
const readStream = new Readable({
321+
read() {
322+
this.push(file);
323+
this.push(null);
324+
},
325+
});
326+
const options: UploadMultipartOptions = {
327+
progressCallback: vi.fn(),
328+
abortController: new AbortController(),
329+
parts: 2,
330+
};
331+
332+
vi.spyOn(HashStream.prototype, 'getHash').mockImplementation(() => Buffer.from(''));
333+
334+
vi.spyOn(axios, 'put').mockImplementation((_, __, config) => {
335+
config?.onUploadProgress?.({
336+
loaded: file.length,
337+
total: file.length,
338+
bytes: file.length,
339+
lengthComputable: true,
340+
});
341+
return Promise.resolve({
342+
data: readStream,
343+
headers: {
344+
etag: 'any-etag',
345+
},
346+
});
347+
});
348+
349+
vi.spyOn(networkMock, 'startUpload').mockResolvedValue({
350+
uploads: [
351+
{
352+
index: 0,
353+
url: 'any-url',
354+
uuid: 'any-uuid',
355+
UploadId: 'any-UploadId',
356+
urls: ['url_1', 'url_2'],
357+
},
358+
],
359+
});
360+
361+
vi.spyOn(networkMock, 'finishUpload')
362+
// @ts-expect-error - We only mock the properties we need
363+
.mockResolvedValue({
364+
id: 'uploaded_file_id',
365+
});
366+
367+
const [executeUpload] = await sut.uploadMultipartFromStream(
368+
bucket,
369+
'animal fog wink trade december thumb sight cousin crunch plunge captain enforce letter creek text',
370+
file.length,
371+
readStream,
372+
options,
373+
);
374+
375+
const uploadResult = await executeUpload;
376+
377+
expect(uploadResult.fileId).to.be.equal('uploaded_file_id');
378+
expect(options.progressCallback).toHaveBeenCalledWith(100);
379+
});
307380
});

0 commit comments

Comments
 (0)