cmvr-wms/ruoyi-ui-vue3/tests/unit/supplier-pdf-preview.test.mjs
lixiaolong a82258561b feat(auth): 添加认证cookie名称配置并优化表单布局
- 在 .env.production 中新增 VITE_AUTH_COOKIE_NAME 配置项用于设置认证cookie名称
- 将 AcceptanceException.vue 中的表单标签宽度从固定90px改为自动适配
- 在 ActionDialog.vue 中将多个表单项标记为必填项并调整标签宽度为自动适配
- 重构 ActionHistory.vue 中的按钮布局结构并添加表格列样式类名
- 更新 api-contract-audit.cjs 添加生产任务和BOM相关接口的契约测试
- 修改 auth.js 使用动态认证cookie键名替换硬编码的Admin-Token
- 调整系统角色授权用户页面的表单布局和表格样式类名
- 优化 ReceiptDialog Batch 组件的对话框尺寸、表单验证和分页功能
2026-10-16 15:30:10 +08:00

71 lines
3.8 KiB
JavaScript

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)
})