import test from 'node:test' import assert from 'node:assert/strict' import { createSupplierPdfAssetFactory, createSupplierPdfCanvasFactory, supplierPdfLimits, supplierPdfRenderSize, withSupplierPdfTimeout } from '../../src/utils/supplier-pdf-preview.js' test('PDF render size fits the preview and caps high-DPI canvas allocation', () => { const normal = supplierPdfRenderSize(595, 842, 900, 1, 1) assert.ok(normal.cssWidth <= 900) for (const [width, height] of [[595, 842], [14400, 14400], [1, 14400]]) { const size = supplierPdfRenderSize(width, height, 3000, 2, 4) assert.ok(size.width * size.height <= supplierPdfLimits.maxCanvasPixels) assert.ok(size.width <= supplierPdfLimits.maxCanvasDimension) assert.ok(size.height <= supplierPdfLimits.maxCanvasDimension) } }) test('invalid or extreme page dimensions fail before creating a browser canvas', () => { for (const size of [[0, 842], [-1, 10], [NaN, 10], [Infinity, 10], [14401, 10]]) assert.throws(() => supplierPdfRenderSize(...size, 800)) }) test('scratch canvas allocations share a bounded budget and release it on destroy', () => { const document = { createElement: () => ({ width: 0, height: 0, getContext: () => ({}) }) } const Factory = createSupplierPdfCanvasFactory(document) const factory = new Factory(), canvases = [] for (let i = 0; i < 4; i++) canvases.push(factory.create(2000, 2000)) assert.throws(() => factory.create(2000, 2000)) factory.destroy(canvases[0]) assert.equal(canvases[0].canvas, null) assert.doesNotThrow(() => factory.create(2000, 2000)) assert.throws(() => factory.reset(canvases[1], 4097, 1)) assert.throws(() => factory.create(4000, 4000)) }) test('PDF font and CMap requests resolve only through the shipped asset allowlist', async () => { const calls = [], controller = new AbortController() const Factory = createSupplierPdfAssetFactory({ cMapUrl: new Map([['UniGB-UTF16-H.bcmap', '/static/bcmap/UniGB-UTF16-H-123.bcmap']]) }, { baseUrl: 'https://warehouse.example/warehouse/suppliers', signal: controller.signal, fetcher: async (...args) => { calls.push(args); return { ok: true, arrayBuffer: async () => new Uint8Array([1, 2]).buffer } } }) const factory = new Factory() assert.deepEqual(await factory.fetch({ kind: 'cMapUrl', filename: 'UniGB-UTF16-H.bcmap' }), new Uint8Array([1, 2])) assert.equal(calls[0][0], 'https://warehouse.example/static/bcmap/UniGB-UTF16-H-123.bcmap') assert.equal(calls[0][1].redirect, 'error') assert.equal(calls[0][1].mode, 'same-origin') assert.equal(calls[0][1].credentials, 'omit') assert.equal(calls[0][1].signal, controller.signal) for (const filename of ['../secret', 'https://example.com/font', 'unknown.bcmap', '__proto__']) await assert.rejects(factory.fetch({ kind: 'cMapUrl', filename })) assert.equal(calls.length, 1) }) test('PDF assets cannot redirect to external URLs or use data scripts', async () => { let requests = 0 for (const url of ['https://elsewhere.example/font.pfb', 'data:application/javascript,alert(1)', 'javascript:alert(1)']) { const Factory = createSupplierPdfAssetFactory({ standardFontDataUrl: new Map([['font.pfb', url]]) }, { baseUrl: 'https://warehouse.example/', fetcher: async () => { requests++; throw new Error('should not fetch') } }) await assert.rejects(new Factory().fetch({ kind: 'standardFontDataUrl', filename: 'font.pfb' })) } assert.equal(requests, 0) }) test('document/render deadlines cancel pending work and settled operations clear their timer', async () => { let cancelled = 0 await assert.rejects(withSupplierPdfTimeout(new Promise(() => {}), 5, () => { cancelled++ }), /超时/) assert.equal(cancelled, 1) assert.equal(await withSupplierPdfTimeout(Promise.resolve('rendered'), 5, () => { cancelled++ }), 'rendered') await new Promise(resolve => setTimeout(resolve, 10)) assert.equal(cancelled, 1) })