n8n/packages/core/test/SerializedBuffer.test.ts
Tomi Turtiainen 0f1461f2d5
Some checks are pending
Test Master / install-and-build (push) Waiting to run
Test Master / Unit tests (18.x) (push) Blocked by required conditions
Test Master / Unit tests (20.x) (push) Blocked by required conditions
Test Master / Unit tests (22.4) (push) Blocked by required conditions
Test Master / Lint (push) Blocked by required conditions
Test Master / Notify Slack on failure (push) Blocked by required conditions
Benchmark Docker Image CI / build (push) Waiting to run
fix(core): Fix binary data helpers (like prepareBinaryData) with task runner (#12259)
2024-12-18 18:45:05 +02:00

56 lines
1.7 KiB
TypeScript

import type { SerializedBuffer } from '@/SerializedBuffer';
import { toBuffer, isSerializedBuffer } from '@/SerializedBuffer';
// Mock data for tests
const validSerializedBuffer: SerializedBuffer = {
type: 'Buffer',
data: [65, 66, 67], // Corresponds to 'ABC' in ASCII
};
describe('serializedBufferToBuffer', () => {
it('should convert a SerializedBuffer to a Buffer', () => {
const buffer = toBuffer(validSerializedBuffer);
expect(buffer).toBeInstanceOf(Buffer);
expect(buffer.toString()).toBe('ABC');
});
it('should serialize stringified buffer to the same buffer', () => {
const serializedBuffer = JSON.stringify(Buffer.from('n8n on the rocks'));
const buffer = toBuffer(JSON.parse(serializedBuffer));
expect(buffer).toBeInstanceOf(Buffer);
expect(buffer.toString()).toBe('n8n on the rocks');
});
});
describe('isSerializedBuffer', () => {
it('should return true for a valid SerializedBuffer', () => {
expect(isSerializedBuffer(validSerializedBuffer)).toBe(true);
});
test.each([
[{ data: [1, 2, 3] }],
[{ data: [1, 2, 256] }],
[{ type: 'Buffer', data: 'notAnArray' }],
[{ data: 42 }],
[{ data: 'test' }],
[{ data: true }],
[null],
[undefined],
[42],
[{}],
])('should return false for %s', (value) => {
expect(isSerializedBuffer(value)).toBe(false);
});
});
describe('Integration: serializedBufferToBuffer and isSerializedBuffer', () => {
it('should correctly validate and convert a SerializedBuffer', () => {
if (isSerializedBuffer(validSerializedBuffer)) {
const buffer = toBuffer(validSerializedBuffer);
expect(buffer.toString()).toBe('ABC');
} else {
fail('Expected validSerializedBuffer to be a SerializedBuffer');
}
});
});