import { describe, expect, it } from 'vitest'; import { isSupplierInvoiceFile, isSupplierInvoiceImage, isSupplierInvoicePdf, prepareSupplierInvoiceFile, SUPPLIER_INVOICE_MAX_BYTES, } from './supplierInvoiceFile'; function makeFile(name: string, type: string, size: number): File { const buf = new Uint8Array(Math.min(size, 16)); const file = new File([buf], name, { type }); Object.defineProperty(file, 'size', { value: size }); return file; } describe('supplierInvoiceFile', () => { it('accepts jpeg/png/webp and pdf by type or extension', () => { expect(isSupplierInvoiceImage(makeFile('a.jpg', 'image/jpeg', 10))).toBe(true); expect(isSupplierInvoiceImage(makeFile('a.PNG', '', 10))).toBe(true); expect(isSupplierInvoicePdf(makeFile('a.pdf', 'application/pdf', 10))).toBe(true); expect(isSupplierInvoicePdf(makeFile('scan.PDF', '', 10))).toBe(true); expect(isSupplierInvoiceFile(makeFile('a.webp', 'image/webp', 10))).toBe(true); expect(isSupplierInvoiceImage(makeFile('IMG_001.HEIC', 'image/heic', 10))).toBe(true); expect(isSupplierInvoiceImage(makeFile('a.jpg', 'image/jpg', 10))).toBe(true); expect(isSupplierInvoiceFile(makeFile('a.doc', 'application/msword', 10))).toBe(false); }); it('rejects oversized pdf without compressing', async () => { const huge = makeFile('big.pdf', 'application/pdf', SUPPLIER_INVOICE_MAX_BYTES + 1); await expect(prepareSupplierInvoiceFile(huge)).rejects.toThrow('too_large'); }); it('passes through pdf under the limit', async () => { const pdf = makeFile('ok.pdf', 'application/pdf', 1024); await expect(prepareSupplierInvoiceFile(pdf)).resolves.toBe(pdf); }); it('passes through small images without canvas', async () => { const img = makeFile('ok.jpg', 'image/jpeg', 1024); await expect(prepareSupplierInvoiceFile(img)).resolves.toBe(img); }); });