import { describe, expect, it } from 'vitest'; import { parseDeclaredUploadSize } from '@/lib/upload-size'; const MAX = BigInt(5) * BigInt(1024) * BigInt(1024) * BigInt(1024); function size(result: ReturnType): bigint | null { return 'sizeBytes' in result ? result.sizeBytes : null; } describe('parseDeclaredUploadSize', () => { it('accepts a size sent as a string, which is how a client sends bytes it cannot hold in a number', () => { expect(size(parseDeclaredUploadSize('4294967296', MAX))).toBe(BigInt(4294967296)); }); it('accepts a size sent as a number', () => { expect(size(parseDeclaredUploadSize(1024, MAX))).toBe(BigInt(1024)); }); it('rejects a missing size', () => { expect(parseDeclaredUploadSize(undefined, MAX)).toEqual({ error: 'sizeBytes must be a positive integer', }); }); // Zero was the old behaviour of every Bunny init: it asked the quota whether // it could store nothing, and the answer was always yes. it('rejects zero', () => { expect(parseDeclaredUploadSize(0, MAX)).toEqual({ error: 'sizeBytes must be a positive integer', }); }); it('rejects a negative size', () => { expect(parseDeclaredUploadSize(-1, MAX)).toEqual({ error: 'sizeBytes must be a positive integer', }); }); it('rejects a fractional size rather than rounding it', () => { expect(parseDeclaredUploadSize(1.5, MAX)).toEqual({ error: 'sizeBytes must be a positive integer', }); }); it('rejects text that is not a number', () => { expect(parseDeclaredUploadSize('a lot', MAX)).toEqual({ error: 'sizeBytes must be a positive integer', }); }); it('rejects a size over the ceiling', () => { expect(parseDeclaredUploadSize(MAX + BigInt(1), MAX)).toEqual({ error: 'File exceeds the maximum allowed upload size', }); }); it('accepts a size exactly at the ceiling', () => { expect(size(parseDeclaredUploadSize(MAX, MAX))).toBe(MAX); }); });