Skip to content

Commit b2e3264

Browse files
committed
test: add progress reporting for file uploads in network facade
1 parent 1a80eeb commit b2e3264

File tree

1 file changed

+65
-1
lines changed

1 file changed

+65
-1
lines changed

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

Lines changed: 65 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import { DownloadService } from '../../../src/services/network/download.service'
1010
import { Readable } from 'node:stream';
1111
import axios from 'axios';
1212
import { fail } from 'node:assert';
13+
import crypto from 'node:crypto';
14+
import { HashStream } from '../../../src/utils/hash.utils';
1315

1416
describe('Network Facade Service', () => {
1517
beforeEach(() => {
@@ -77,6 +79,68 @@ describe('Network Facade Service', () => {
7779
expect(uploadResult.fileId).to.be.equal('uploaded_file_id');
7880
});
7981

82+
it('When a file is uploaded, then it should report progress', async () => {
83+
const bucket = 'f1858bc9675f9e4f7ab29429';
84+
const networkMock = getNetworkMock();
85+
86+
const sut = new NetworkFacade(
87+
networkMock,
88+
UploadService.instance,
89+
DownloadService.instance,
90+
CryptoService.instance,
91+
);
92+
const file = crypto.randomBytes(16).toString('hex');
93+
const readStream = new Readable({
94+
read() {
95+
this.push(file);
96+
this.push(null);
97+
},
98+
});
99+
const options = {
100+
progressCallback: vi.fn(),
101+
abortController: new AbortController(),
102+
};
103+
104+
vi.spyOn(HashStream.prototype, 'getHash').mockImplementation(() => Buffer.from(''));
105+
106+
vi.spyOn(axios, 'put').mockImplementation((_, __, config) => {
107+
config?.onUploadProgress?.({
108+
loaded: file.length,
109+
total: file.length,
110+
bytes: file.length,
111+
lengthComputable: true,
112+
});
113+
return Promise.resolve({
114+
data: readStream,
115+
headers: {
116+
etag: 'any-etag',
117+
},
118+
});
119+
});
120+
121+
vi.spyOn(networkMock, 'startUpload').mockResolvedValue({
122+
uploads: [{ index: 0, url: 'any-url', uuid: 'any-uuid', urls: [] }],
123+
});
124+
125+
vi.spyOn(networkMock, 'finishUpload')
126+
// @ts-expect-error - We only mock the properties we need
127+
.mockResolvedValue({
128+
id: 'any-id',
129+
});
130+
131+
const [executeUpload] = await sut.uploadFromStream(
132+
bucket,
133+
'animal fog wink trade december thumb sight cousin crunch plunge captain enforce letter creek text',
134+
file.length,
135+
readStream,
136+
options,
137+
);
138+
139+
await executeUpload;
140+
141+
expect(options.progressCallback).toHaveBeenCalledWith(100);
142+
});
143+
80144
it('When a file is downloaded, should write it to a stream', async () => {
81145
const encryptedContent = Buffer.from('b6ccfa381c150f3a4b65245bffa4d84087', 'hex');
82146
const bucket = 'cd8abd7e8b13081660b58dbe';
@@ -220,7 +284,7 @@ describe('Network Facade Service', () => {
220284
loaded: encryptedContent.length,
221285
total: encryptedContent.length,
222286
bytes: encryptedContent.length,
223-
lengthComputable: true
287+
lengthComputable: true,
224288
});
225289
return Promise.resolve({ data: readableContent });
226290
});

0 commit comments

Comments
 (0)