166 lines
5.2 KiB
TypeScript
166 lines
5.2 KiB
TypeScript
import { describe, it, expect } from 'vitest';
|
|
import { SponsorLogoViewDataBuilder } from './SponsorLogoViewDataBuilder';
|
|
import type { MediaBinaryDTO } from '@/lib/types/MediaBinaryDTO';
|
|
|
|
describe('SponsorLogoViewDataBuilder', () => {
|
|
describe('happy paths', () => {
|
|
it('should transform MediaBinaryDTO to SponsorLogoViewData correctly', () => {
|
|
const buffer = new Uint8Array([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]);
|
|
const mediaDto: MediaBinaryDTO = {
|
|
buffer: buffer.buffer,
|
|
contentType: 'image/png',
|
|
};
|
|
|
|
const result = SponsorLogoViewDataBuilder.build(mediaDto);
|
|
|
|
expect(result.buffer).toBe(Buffer.from(buffer).toString('base64'));
|
|
expect(result.contentType).toBe('image/png');
|
|
});
|
|
|
|
it('should handle JPEG sponsor logos', () => {
|
|
const buffer = new Uint8Array([0xff, 0xd8, 0xff, 0xe0]);
|
|
const mediaDto: MediaBinaryDTO = {
|
|
buffer: buffer.buffer,
|
|
contentType: 'image/jpeg',
|
|
};
|
|
|
|
const result = SponsorLogoViewDataBuilder.build(mediaDto);
|
|
|
|
expect(result.buffer).toBe(Buffer.from(buffer).toString('base64'));
|
|
expect(result.contentType).toBe('image/jpeg');
|
|
});
|
|
|
|
it('should handle SVG sponsor logos', () => {
|
|
const buffer = new TextEncoder().encode('<svg xmlns="http://www.w3.org/2000/svg"><text x="10" y="20">Sponsor</text></svg>');
|
|
const mediaDto: MediaBinaryDTO = {
|
|
buffer: buffer.buffer,
|
|
contentType: 'image/svg+xml',
|
|
};
|
|
|
|
const result = SponsorLogoViewDataBuilder.build(mediaDto);
|
|
|
|
expect(result.buffer).toBe(Buffer.from(buffer).toString('base64'));
|
|
expect(result.contentType).toBe('image/svg+xml');
|
|
});
|
|
});
|
|
|
|
describe('data transformation', () => {
|
|
it('should preserve all DTO fields in the output', () => {
|
|
const buffer = new Uint8Array([0x89, 0x50, 0x4e, 0x47]);
|
|
const mediaDto: MediaBinaryDTO = {
|
|
buffer: buffer.buffer,
|
|
contentType: 'image/png',
|
|
};
|
|
|
|
const result = SponsorLogoViewDataBuilder.build(mediaDto);
|
|
|
|
expect(result.buffer).toBeDefined();
|
|
expect(result.contentType).toBe(mediaDto.contentType);
|
|
});
|
|
|
|
it('should not modify the input DTO', () => {
|
|
const buffer = new Uint8Array([0x89, 0x50, 0x4e, 0x47]);
|
|
const mediaDto: MediaBinaryDTO = {
|
|
buffer: buffer.buffer,
|
|
contentType: 'image/png',
|
|
};
|
|
|
|
const originalDto = { ...mediaDto };
|
|
SponsorLogoViewDataBuilder.build(mediaDto);
|
|
|
|
expect(mediaDto).toEqual(originalDto);
|
|
});
|
|
|
|
it('should convert buffer to base64 string', () => {
|
|
const buffer = new Uint8Array([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]);
|
|
const mediaDto: MediaBinaryDTO = {
|
|
buffer: buffer.buffer,
|
|
contentType: 'image/png',
|
|
};
|
|
|
|
const result = SponsorLogoViewDataBuilder.build(mediaDto);
|
|
|
|
expect(typeof result.buffer).toBe('string');
|
|
expect(result.buffer).toBe(Buffer.from(buffer).toString('base64'));
|
|
});
|
|
});
|
|
|
|
describe('edge cases', () => {
|
|
it('should handle empty buffer', () => {
|
|
const buffer = new Uint8Array([]);
|
|
const mediaDto: MediaBinaryDTO = {
|
|
buffer: buffer.buffer,
|
|
contentType: 'image/png',
|
|
};
|
|
|
|
const result = SponsorLogoViewDataBuilder.build(mediaDto);
|
|
|
|
expect(result.buffer).toBe('');
|
|
expect(result.contentType).toBe('image/png');
|
|
});
|
|
|
|
it('should handle large sponsor logos', () => {
|
|
const buffer = new Uint8Array(3 * 1024 * 1024); // 3MB
|
|
const mediaDto: MediaBinaryDTO = {
|
|
buffer: buffer.buffer,
|
|
contentType: 'image/jpeg',
|
|
};
|
|
|
|
const result = SponsorLogoViewDataBuilder.build(mediaDto);
|
|
|
|
expect(result.buffer).toBe(Buffer.from(buffer).toString('base64'));
|
|
expect(result.contentType).toBe('image/jpeg');
|
|
});
|
|
|
|
it('should handle buffer with all zeros', () => {
|
|
const buffer = new Uint8Array([0x00, 0x00, 0x00, 0x00]);
|
|
const mediaDto: MediaBinaryDTO = {
|
|
buffer: buffer.buffer,
|
|
contentType: 'image/png',
|
|
};
|
|
|
|
const result = SponsorLogoViewDataBuilder.build(mediaDto);
|
|
|
|
expect(result.buffer).toBe(Buffer.from(buffer).toString('base64'));
|
|
expect(result.contentType).toBe('image/png');
|
|
});
|
|
|
|
it('should handle buffer with all ones', () => {
|
|
const buffer = new Uint8Array([0xff, 0xff, 0xff, 0xff]);
|
|
const mediaDto: MediaBinaryDTO = {
|
|
buffer: buffer.buffer,
|
|
contentType: 'image/png',
|
|
};
|
|
|
|
const result = SponsorLogoViewDataBuilder.build(mediaDto);
|
|
|
|
expect(result.buffer).toBe(Buffer.from(buffer).toString('base64'));
|
|
expect(result.contentType).toBe('image/png');
|
|
});
|
|
|
|
it('should handle different content types', () => {
|
|
const buffer = new Uint8Array([0x89, 0x50, 0x4e, 0x47]);
|
|
const contentTypes = [
|
|
'image/png',
|
|
'image/jpeg',
|
|
'image/gif',
|
|
'image/webp',
|
|
'image/svg+xml',
|
|
'image/bmp',
|
|
'image/tiff',
|
|
];
|
|
|
|
contentTypes.forEach((contentType) => {
|
|
const mediaDto: MediaBinaryDTO = {
|
|
buffer: buffer.buffer,
|
|
contentType,
|
|
};
|
|
|
|
const result = SponsorLogoViewDataBuilder.build(mediaDto);
|
|
|
|
expect(result.contentType).toBe(contentType);
|
|
});
|
|
});
|
|
});
|
|
});
|