Files
twenty/packages/twenty-server/src/utils/__test__/stream-to-buffer.spec.ts
T
martmull 127fb2a470 Increase size of tarball upload (#20767)
- check size while reading stream instead of checking after reading all
stream
- move MAX_TARBALL_UPLOAD_SIZE_BYTES to config variables
- increase MAX_TARBALL_UPLOAD_SIZE_BYTES default from 50Mb to 100Mb
2026-05-20 12:39:11 +00:00

121 lines
3.4 KiB
TypeScript

import { PassThrough, Readable } from 'stream';
import { streamToBuffer } from 'src/utils/stream-to-buffer';
describe('streamToBuffer', () => {
describe('successful scenarios', () => {
it('should convert a stream with single chunk to buffer', async () => {
const testData = 'Hello, World!';
const stream = Readable.from([Buffer.from(testData)]);
const result = await streamToBuffer(stream);
expect(result.toString()).toBe(testData);
});
it('should convert a stream with multiple chunks to buffer', async () => {
const chunks = ['Hello, ', 'World', '!'];
const stream = Readable.from(chunks.map((chunk) => Buffer.from(chunk)));
const result = await streamToBuffer(stream);
expect(result.toString()).toBe('Hello, World!');
});
it('should handle empty stream', async () => {
const stream = Readable.from([]);
const result = await streamToBuffer(stream);
expect(result.length).toBe(0);
expect(result.toString()).toBe('');
});
});
describe('error scenarios', () => {
it('should reject when stream is already ended', async () => {
const stream = Readable.from([Buffer.from('test')]);
await streamToBuffer(stream);
await expect(streamToBuffer(stream)).rejects.toThrow(
'Stream has already ended',
);
});
it('should reject when stream is not readable (destroyed)', async () => {
const stream = new PassThrough();
stream.destroy();
await expect(streamToBuffer(stream)).rejects.toThrow(
'Stream is not readable',
);
});
it('should reject when stream emits an error', async () => {
const stream = new PassThrough();
const testError = new Error('Stream error');
const promise = streamToBuffer(stream);
stream.write(Buffer.from('partial'));
stream.emit('error', testError);
await expect(promise).rejects.toThrow('Stream error');
});
it('should reject when stream closes before end', async () => {
const stream = new PassThrough();
const promise = streamToBuffer(stream);
stream.write(Buffer.from('data'));
stream.destroy();
await expect(promise).rejects.toThrow('Stream closed before end');
});
});
describe('maxSizeBytes', () => {
it('should accept stream within size limit', async () => {
const data = 'Hello, World!';
const stream = Readable.from([Buffer.from(data)]);
const result = await streamToBuffer(stream, 100);
expect(result.toString()).toBe(data);
});
it('should reject when stream exceeds maxSizeBytes', async () => {
const stream = new PassThrough();
const promise = streamToBuffer(stream, 10);
stream.write(Buffer.from('12345'));
stream.write(Buffer.from('678901'));
await expect(promise).rejects.toThrow(
'Stream exceeds maximum allowed size of 10 bytes',
);
});
it('should reject on a single chunk exceeding maxSizeBytes', async () => {
const stream = Readable.from([Buffer.from('this is too long')]);
await expect(streamToBuffer(stream, 5)).rejects.toThrow(
'Stream exceeds maximum allowed size of 5 bytes',
);
});
it('should accept stream exactly at maxSizeBytes', async () => {
const data = '12345';
const stream = Readable.from([Buffer.from(data)]);
const result = await streamToBuffer(stream, 5);
expect(result.toString()).toBe(data);
});
});
});