import { describe, expect, it } from 'vitest';
import { checkLogoUpload, NEXTCLOUD_LOGO_MAX_BYTES } from './nextcloud-logo-rules';
const PNG = [0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a];
const JPEG = [0xff, 0xd8, 0xff, 0xe0];
const GIF = [0x47, 0x49, 0x46, 0x38, 0x39, 0x61];
const WEBP = [0x52, 0x49, 0x46, 0x46, 0, 0, 0, 0, 0x57, 0x45, 0x42, 0x50];
describe('checkLogoUpload', () => {
it.each([
['PNG', PNG, 'image/png'],
['JPEG', JPEG, 'image/jpeg'],
['GIF', GIF, 'image/gif'],
['WebP', WEBP, 'image/webp'],
])('erkennt %s', (_name, bytes, mime) => {
expect(checkLogoUpload(Buffer.from([...bytes, 1, 2, 3]))).toBe(mime);
});
it('lehnt SVG-Text, PDF, leere Dateien und umbenanntes HTML ab', () => {
expect(
checkLogoUpload(Buffer.from('')),
).toBeNull();
expect(checkLogoUpload(Buffer.from('%PDF-1.7 ...'))).toBeNull();
expect(checkLogoUpload(Buffer.alloc(0))).toBeNull();
expect(checkLogoUpload(Buffer.from(''))).toBeNull();
});
it('genau 1 MiB ist erlaubt, ein Byte mehr nicht', () => {
const ok = Buffer.concat([
Buffer.from(PNG),
Buffer.alloc(NEXTCLOUD_LOGO_MAX_BYTES - PNG.length),
]);
expect(ok.length).toBe(NEXTCLOUD_LOGO_MAX_BYTES);
expect(checkLogoUpload(ok)).toBe('image/png');
expect(checkLogoUpload(Buffer.concat([ok, Buffer.from([0])]))).toBeNull();
});
});