import { describe, test, expect, beforeEach } from 'vitest'; import { setSiteLimits, getSiteLimits, uploadSizeError, uploadFailureText, formatMegabytes } from './site-limits'; describe('site limits (server-advertised, in memory)', () => { beforeEach(() => setSiteLimits(undefined)); test('unknown limits (older backend) never refuse a file', () => { expect(getSiteLimits()).toEqual({ uploadMaxBytes: undefined, projectMaxBytes: undefined }); expect(uploadSizeError({ size: 10 ** 12, name: 'a.mp4' })).toBeNull(); }); test('adopts limits.upload_max_bytes / project_max_bytes from load', () => { setSiteLimits({ upload_max_bytes: 67108864, project_max_bytes: 20971520 }); expect(getSiteLimits()).toEqual({ uploadMaxBytes: 67108864, projectMaxBytes: 20971520 }); }); test('ignores malformed values', () => { setSiteLimits({ upload_max_bytes: '64', project_max_bytes: -1 }); expect(getSiteLimits()).toEqual({ uploadMaxBytes: undefined, projectMaxBytes: undefined }); }); test('a file over the limit is refused with a readable message; at the limit is allowed', () => { setSiteLimits({ upload_max_bytes: 64 * 1048576 }); expect(uploadSizeError({ size: 64 * 1048576, name: 'ok.png' })).toBeNull(); const msg = uploadSizeError({ size: 64 * 1048576 + 1, name: 'big.mp4' }); expect(msg).toContain('big.mp4'); expect(msg).toContain('64 MB'); }); test('uploadFailureText prefers the server error text over the status', () => { expect(uploadFailureText({ success: false, error: 'That file is too large. The site builder accepts files up to 64 MB.' }, 413)) .toBe('That file is too large. The site builder accepts files up to 64 MB.'); expect(uploadFailureText(null, 413)).toBe('Upload failed: 413'); expect(formatMegabytes(1572864)).toBe('1.5 MB'); }); });