gongxiangp2/components/upload/image-processor.video-preview.test.ts
2026-08-07 23:28:55 +08:00

31 lines
1.1 KiB
TypeScript

import { assertVideoPreviewImageIsSafeToDecode } from './image-processor'
function createPngFile(name: string, width: number, height: number) {
const bytes = new Uint8Array(24)
bytes.set([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a])
const view = new DataView(bytes.buffer)
view.setUint32(16, width)
view.setUint32(20, height)
return new File([bytes], name, { type: 'image/png' })
}
describe('video preview image decode guard', () => {
test('允许常规预览图', async () => {
await expect(assertVideoPreviewImageIsSafeToDecode(
createPngFile('cover.png', 3840, 2160),
)).resolves.toBeUndefined()
})
test('在浏览器解码前拒绝超大像素图片', async () => {
await expect(assertVideoPreviewImageIsSafeToDecode(
createPngFile('huge.png', 10_000, 7_000),
)).rejects.toThrow('像素总量超过 60,000,000')
})
test('无法读取尺寸时给出可恢复的格式提示', async () => {
await expect(assertVideoPreviewImageIsSafeToDecode(
new File(['not-an-image'], 'broken.jpg', { type: 'image/jpeg' }),
)).rejects.toThrow('请改用 JPG、PNG 或 WebP 图片')
})
})