Files
mes-budgets-participatifs/src/__tests__/lib/file-utils.test.ts
2025-08-29 09:43:58 +02:00

164 lines
6.1 KiB
TypeScript

import {
formatFileSize,
getFileExtension,
validateFileType,
sanitizeFileName
} from '../../lib/file-utils';
describe('File Utils', () => {
describe('formatFileSize', () => {
it('should format bytes correctly', () => {
expect(formatFileSize(0)).toBe('0 B');
expect(formatFileSize(1024)).toBe('1 KB');
expect(formatFileSize(1024 * 1024)).toBe('1 MB');
expect(formatFileSize(1024 * 1024 * 1024)).toBe('1 GB');
});
it('should handle decimal sizes', () => {
expect(formatFileSize(1500)).toBe('1.46 KB');
expect(formatFileSize(1536)).toBe('1.5 KB');
expect(formatFileSize(1024 * 1024 + 512 * 1024)).toBe('1.5 MB');
});
it('should handle large sizes', () => {
expect(formatFileSize(1024 * 1024 * 1024 * 1024)).toBe('1 TB');
expect(formatFileSize(1024 * 1024 * 1024 * 1024 * 1024)).toBe('1 PB');
});
it('should handle negative values', () => {
expect(formatFileSize(-1024)).toBe('0 B');
expect(formatFileSize(-1)).toBe('0 B');
});
});
describe('getFileExtension', () => {
it('should extract file extensions', () => {
expect(getFileExtension('file.txt')).toBe('txt');
expect(getFileExtension('document.pdf')).toBe('pdf');
expect(getFileExtension('image.jpg')).toBe('jpg');
expect(getFileExtension('archive.tar.gz')).toBe('gz');
});
it('should handle files without extensions', () => {
expect(getFileExtension('README')).toBe('');
expect(getFileExtension('file.')).toBe('');
expect(getFileExtension('')).toBe('');
});
it('should handle case sensitivity', () => {
expect(getFileExtension('file.TXT')).toBe('TXT');
expect(getFileExtension('file.PDF')).toBe('PDF');
});
it('should handle special characters', () => {
expect(getFileExtension('file-name_test.txt')).toBe('txt');
expect(getFileExtension('file@domain.com.pdf')).toBe('pdf');
});
});
describe('validateFileType', () => {
it('should validate allowed file types', () => {
const csvFile = new File([''], 'test.csv', { type: 'text/csv' });
const excelFile = new File([''], 'test.xlsx', { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' });
const odsFile = new File([''], 'test.ods', { type: 'application/vnd.oasis.opendocument.spreadsheet' });
expect(validateFileType(csvFile).isValid).toBe(true);
expect(validateFileType(excelFile).isValid).toBe(true);
expect(validateFileType(odsFile).isValid).toBe(true);
});
it('should reject disallowed file types', () => {
const txtFile = new File([''], 'test.txt', { type: 'text/plain' });
const exeFile = new File([''], 'test.exe', { type: 'application/x-msdownload' });
expect(validateFileType(txtFile).isValid).toBe(false);
expect(validateFileType(exeFile).isValid).toBe(false);
});
it('should handle case insensitive validation', () => {
const csvFile = new File([''], 'test.CSV', { type: 'text/csv' });
const xlsxFile = new File([''], 'test.XLSX', { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' });
expect(validateFileType(csvFile).isValid).toBe(true);
expect(validateFileType(xlsxFile).isValid).toBe(true);
});
it('should handle files without extensions', () => {
const fileWithoutExt = new File([''], 'test', { type: 'text/plain' });
expect(validateFileType(fileWithoutExt).isValid).toBe(false);
});
it('should handle files with null name', () => {
const fileWithNullName = new File([''], '', { type: 'text/csv' });
expect(validateFileType(fileWithNullName).isValid).toBe(true);
});
});
describe('sanitizeFileName', () => {
it('should remove special characters', () => {
expect(sanitizeFileName('file@name#test.txt')).toBe('file-name-test.txt');
expect(sanitizeFileName('document with spaces.pdf')).toBe('document-with-spaces.pdf');
expect(sanitizeFileName('file/with\\slashes.txt')).toBe('file-with-slashes.txt');
});
it('should handle accented characters', () => {
expect(sanitizeFileName('fichier-émojis.txt')).toBe('fichier-mojis.txt');
expect(sanitizeFileName('document-à-ç-ù.pdf')).toBe('document-.pdf');
});
it('should preserve file extensions', () => {
expect(sanitizeFileName('file@name.txt')).toBe('file-name.txt');
expect(sanitizeFileName('document#test.pdf')).toBe('document-test.pdf');
expect(sanitizeFileName('image$photo.jpg')).toBe('image-photo.jpg');
});
it('should handle multiple dots', () => {
expect(sanitizeFileName('file.name.test.txt')).toBe('file.name.test.txt');
expect(sanitizeFileName('archive.tar.gz')).toBe('archive.tar.gz');
});
it('should handle empty strings', () => {
expect(sanitizeFileName('')).toBe('');
expect(sanitizeFileName(' ')).toBe('');
});
it('should handle files without extensions', () => {
expect(sanitizeFileName('README')).toBe('README');
expect(sanitizeFileName('file@name')).toBe('file-name');
});
it('should limit filename length', () => {
const longName = 'a'.repeat(300) + '.txt';
const sanitized = sanitizeFileName(longName);
expect(sanitized.length).toBeLessThanOrEqual(255);
expect(sanitized).toMatch(/\.txt$/);
});
});
describe('integration tests', () => {
it('should work together for file validation', () => {
const fileName = 'document@test.pdf';
const file = new File([''], fileName, { type: 'application/pdf' });
const sanitized = sanitizeFileName(fileName);
const extension = getFileExtension(sanitized);
const validation = validateFileType(file);
expect(sanitized).toBe('document-test.pdf');
expect(extension).toBe('pdf');
expect(validation.isValid).toBe(true);
});
it('should handle file size formatting with validation', () => {
const fileSize = 1024 * 1024; // 1 MB
const formattedSize = formatFileSize(fileSize);
expect(formattedSize).toBe('1 MB');
expect(fileSize).toBeGreaterThan(0);
});
});
});