import test from 'node:test' import assert from 'node:assert/strict' import { normalizeSupplier, supplierLicenseError, supplierLicenseMaxBytes, validSupplierEmail, validSupplierLicenseId } from '../../src/utils/supplier-documents.js' test('supplier email is optional, trimmed and checked with the backend email rule', () => { for (const email of ['', ' ', ' sales+office@example.com ', 'first.last@example.co.uk', 'a_b@example.com']) assert.equal(validSupplierEmail(email), true, email) for (const email of ['a@localhost', 'a..b@example.com', 'a b@example.com', 'a@-example.com', 'a@example..com', '采购@example.com', '\u00a0a@example.com\u00a0', `${'a'.repeat(65)}@example.com`, `${'a'.repeat(64)}@${'b'.repeat(64)}.com`]) assert.equal(validSupplierEmail(email), false, email) }) test('supplier edit normalization retains previous email and opaque attachment metadata', () => { const id = '11111111-1111-4111-8111-111111111111' const old = { id: 7, version: 2, email: 'old@example.com', business_license_id: id, businessLicenseName: '执照.pdf' } const edited = normalizeSupplier(old) assert.equal(edited.email, 'old@example.com') assert.equal(edited.businessLicenseId, id) assert.equal(edited.businessLicenseName, '执照.pdf') assert.equal(edited.version, 2) assert.equal(normalizeSupplier({ ...old, businessLicenseId: '' }).businessLicenseId, '') assert.equal(normalizeSupplier().email, '') }) test('licenses accept only nonempty images or PDFs of at most 10 MB', () => { for (const name of ['营业执照.PNG', '营业执照.jpg', '营业执照.jpeg', '营业执照.PDF']) assert.equal(supplierLicenseError({ name, size: supplierLicenseMaxBytes }), '') for (const file of [{ name: '执照.svg', size: 10 }, { name: '执照.html', size: 10 }, { name: '执照.png', size: 0 }, { name: '执照.pdf', size: supplierLicenseMaxBytes + 1 }]) assert.ok(supplierLicenseError(file)) }) test('license preview IDs cannot inject remote URLs or filesystem paths', () => { assert.equal(validSupplierLicenseId('11111111-1111-4111-8111-111111111111'), true) for (const id of ['', '../secret', 'https://example.com/a.pdf', '/profile/a.png', '11111111-1111-4111-8111-111111111111/../secret']) assert.equal(validSupplierLicenseId(id), false) })