来源仓库:jxc/ai-fresh-delivery 来源提交:f0b1a180e2deb7e4673b9d1c8542619f7bc0f73d 快照树: 09f679334ff38f0ff25e907961c94a8223bcaedd(与来源 HEAD 工作树逐字节一致,3801 个文件) 按约定本仓库不带任何历史提交:来源仓库的历史中含真实凭据 (生产 SSH 私钥 deploy/id_rsa_tencent、server/.env、.env.tencent/.env.production/.env.gitea、 deploy/小程序AppSecret.txt,其中 MP_APPSECRET / S3 AccessKey 经哈希比对与线上同值), 整包推送会把这些凭据永久带入本仓库,故只取「当前版本」的单提交快照。 供应商系统后续开发自本提交开始。
44 lines
1.8 KiB
TypeScript
44 lines
1.8 KiB
TypeScript
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);
|
|
});
|
|
});
|