Files

74 lines
5.0 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
'use strict'
// 图片准入判据(src/image-admit.js)行为测试 —— 独立文件
// 背景(v3.24.74):同一件事(这个文件算不算图片)原本有三份不一致的判据,
// · 粘贴按 MIME 前缀 ⇒ MIME 为空的 PNG 被拒,提示还写「图片请先转成 PNG/JPG」;
// · 拖放 `continue` ⇒ 拖 PDF 进画布一声不响;
// · 文件选择框不拦 HEIC ⇒ 收进来是一张打不开的坏图。
// 本测试钉住一张「准入矩阵」:什么样的文件必须收、什么样必须拒、拒的时候话要说清转法。
const fs = require('fs')
const path = require('path')
const root = path.join(__dirname, '..')
const SRC = fs.readFileSync(path.join(root, 'src/image-admit.js'), 'utf8')
const box = {}
new Function('window', 'globalThis', SRC)(box, box)
const A = box.IWBImageAdmit
let pass = 0, fail = 0
const ok = (name, cond) => { if (cond) { pass++; console.log(' ✅ ' + name) } else { fail++; console.log(' ❌ ' + name) } }
const eq = (name, got, want) => ok(name + '(' + JSON.stringify(got) + ' === ' + JSON.stringify(want) + ')', got === want)
const f = (name, type) => ({ name: name, type: type == null ? '' : type, size: 2048 })
ok('模块加载并导出 IWBImageAdmit', !!A && typeof A.of === 'function' && typeof A.why === 'function')
ok('判据表是冻结的(外部改不动)', Object.isFrozen(A) && Object.isFrozen(A.IMAGE_EXT) && Object.isFrozen(A.BLOCKED_EXT))
// ---- ① 正常图片:MIME 合规 ----
eq('image/png → image', A.of(f('a.png', 'image/png')), 'image')
eq('image/jpeg → image', A.of(f('a.jpg', 'image/jpeg')), 'image')
eq('image/svg+xml → image(wImportImage 内部会光栅化)', A.of(f('a.svg', 'image/svg+xml')), 'image')
eq('image/webp → image', A.of(f('a.webp', 'image/webp')), 'image')
eq('带参数的 MIME(image/png;charset=x)→ image', A.of(f('a.png', 'image/png;charset=x')), 'image')
eq('无文件名但有 MIME(剪贴板截图常见)→ image', A.of(f('', 'image/png')), 'image')
// ---- ② MIME 缺失 / octet-stream:按扩展名承认(本次修复的病灶之一)----
// 实测场景:Windows 资源管理器、部分网盘客户端「复制文件」时根本不填 type。
eq('type 为空 + a.png → image(旧判据当非图片拒绝)', A.of(f('a.png', '')), 'image')
eq('application/octet-stream + a.png → image', A.of(f('a.png', 'application/octet-stream')), 'image')
eq('大小写扩展名 .JPEG 也认', A.of(f('IMG_0001.JPEG', '')), 'image')
eq('type 为空 + 无扩展名 → other(不能瞎猜)', A.of(f('download', '')), 'other')
eq('type 为空 + 空文件名 → other', A.of(f('', '')), 'other')
// ---- ③ 浏览器解不了的格式:准入阶段就拒 ----
eq('image/heic → blocked', A.of(f('IMG_0001.HEIC', 'image/heic')), 'blocked')
eq('type 为空 + .heic → blocked(扩展名兜底)', A.of(f('IMG_0001.heic', '')), 'blocked')
eq('image/heif → blocked', A.of(f('a.heif', 'image/heif')), 'blocked')
eq('image/tiff → blocked', A.of(f('scan.tif', 'image/tiff')), 'blocked')
eq('.psd → blocked', A.of(f('poster.psd', '')), 'blocked')
eq('.cr2(相机 RAW)→ blocked', A.of(f('IMG_0002.CR2', '')), 'blocked')
// 拒的理由必须**可执行**(说清转成什么),不是复述文件类型
ok('HEIC 的理由指明「兼容性最佳 / 另存 JPG」', /兼容性最佳|JPG/.test(A.why(f('IMG_0001.HEIC', 'image/heic'))))
ok('TIFF 的理由指明转 PNG / JPG', /PNG/.test(A.why(f('scan.tif', 'image/tiff'))))
ok('PSD 的理由指明导出成 PNG', /PNG/.test(A.why(f('poster.psd', ''))))
ok('RAW 的理由指明导出成 JPG', /JPG/.test(A.why(f('x.raw', ''))))
// 兜底分支:MIME 明确是解不了的图,但扩展名认不出来(改过名 / 无扩展名)⇒ 仍要说人话
ok('blocked MIME + 陌生扩展名 → 兜底一句人话', /无法解码/.test(A.why(f('photo.bin', 'image/heic'))))
// ---- ④ 其它文件:other,理由里点名文件 + 支持的格式 ----
eq('PDF → other', A.of(f('合同.pdf', 'application/pdf')), 'other')
eq('视频 → other', A.of(f('a.mp4', 'video/mp4')), 'other')
eq('压缩包 → other', A.of(f('a.zip', 'application/zip')), 'other')
eq('JSON → other', A.of(f('canvas.iwb.json', 'application/json')), 'other')
ok('other 的理由带文件名', A.why(f('合同.pdf', 'application/pdf')).indexOf('合同.pdf') >= 0)
ok('other 的理由列支持格式', /PNG/.test(A.why(f('合同.pdf', 'application/pdf'))))
ok('无文件名的 other 不出现空括号', A.why(f('', 'application/pdf')).indexOf('该文件') >= 0)
eq('image 不出理由(调用方不必再判)', A.why(f('a.png', 'image/png')), '')
// ---- ⑤ extOf 边界 ----
eq('extOf 取最后一段', A.extOf('a.b.jpeg'), 'jpeg')
eq('extOf 不吃版本号之外的怪串', A.extOf('v1.2'), '2')
eq('extOf 无扩展名 → 空串', A.extOf('README'), '')
eq('extOf 非字符串不炸', A.extOf(null), '')
eq('of 非对象不炸(null)', A.of(null), 'other')
console.log('\nimage-admit: ' + pass + ' 通过 / ' + fail + ' 失败')
process.exit(fail ? 1 : 0)