'use strict' // 图片格式判定 / 下载名后缀纠正 / SVG 光栅化(src/image-format.js)行为测试 —— 独立文件,可异步 // 背景:上游开始返回 .svg,而下载与归档文件名曾硬编码 .png(用户 2026-11-11 反馈)。 // 关键断言:真实格式能判出、判不出**不瞎改**、文件名后缀跟着真实格式走、SVG 光栅化等比且铺白底。 const fs = require('fs') const path = require('path') const root = path.join(__dirname, '..') const SRC = fs.readFileSync(path.join(root, 'src/image-format.js'), 'utf8') const box = {} new Function('window', 'globalThis', SRC)(box, box) // 与 image-preview-derive-test.js 相同的沙箱加载方式 const F = box.IWBImageFormat 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) // 用户实际上游地址(2026-11-11 日志) const UPSTREAM_SVG = 'https://apioss16.sydney-ai.com/ximg/18ia.s0.2g.144x144/6bbb9910-8add-443c-bff4-0badf1b277f4/gi21k-e1794362145.svg' const b64 = (s) => 'data:image/svg+xml;base64,' + Buffer.from(s, 'utf8').toString('base64') ok('模块加载并导出 IWBImageFormat', !!F && typeof F === 'object') // ---- ① 真实格式判定 ---- eq('上游 .svg 地址 → mime', F.mimeOf(UPSTREAM_SVG), 'image/svg+xml') eq('上游 .svg 地址 → 后缀', F.extOf(UPSTREAM_SVG), 'svg') ok('.svg 地址 isSvg', F.isSvg(UPSTREAM_SVG) === true) eq('带 query 的 .png → 后缀', F.extOf('https://x.com/a.png?sign=1&t=2'), 'png') eq('大写后缀 .SVG → 后缀', F.extOf('https://x.com/a.SVG'), 'svg') eq('站内代理 URL 解包 → 后缀', F.extOf('/api/img?url=' + encodeURIComponent('https://x.com/b.svg')), 'svg') eq('data:image/svg+xml;base64 → 后缀', F.extOf(b64('')), 'svg') eq('无后缀地址 → 判不出', F.extOf('https://x.com/no-ext'), '') eq('非图片后缀 .php → 判不出(绝不改成 .png)', F.extOf('https://x.com/a.php'), '') eq('blob: 地址 → 判不出', F.extOf('blob:https://x.com/6f1c'), '') ok('引用对象(非路径)不当成 SVG', F.isSvg({ __blob: 'imgblob_1' }) === false) // ---- ② 下载名后缀纠正 ---- eq('image.png + .svg 地址 → image.svg', F.fileNameFor(UPSTREAM_SVG, 'image.png'), 'image.svg') eq('image.svg + .png 地址 → image.png', F.fileNameFor('https://x.com/a.png', 'image.svg'), 'image.png') eq('无文件名 + .svg 地址 → image.svg', F.fileNameFor(UPSTREAM_SVG, ''), 'image.svg') eq('无文件名 + 判不出 → image.png', F.fileNameFor('https://x.com/no-ext', ''), 'image.png') eq('无文件名 + data:svg → image.svg', F.fileNameFor(b64(''), ''), 'image.svg') eq('mime 优先于地址后缀', F.fileNameFor('https://x.com/a', 'image.png', 'image/svg+xml'), 'image.svg') eq('后缀已正确 → 原样返回', F.fileNameFor('https://x.com/a.svg', 'hero.svg'), 'hero.svg') eq('判不出格式 → 原样返回调用方名字', F.fileNameFor('https://x.com/a.php', 'image.png'), 'image.png') eq('.jpeg 与 image/jpeg 同族 → 不改成 .jpg', F.fileNameFor('https://x.com/a.jpeg', 'photo.jpeg', 'image/jpeg'), 'photo.jpeg') eq('多段文件名只换最后一段后缀', F.fileNameFor('https://x.com/a.svg', 'gen_1_2.png'), 'gen_1_2.svg') eq('名字里的路径分隔符被中和', F.fileNameFor(UPSTREAM_SVG, '../evil.png'), '.._evil.svg') // ---- ③ SVG 尺寸解析(光栅化尺寸来源)---- eq('width/height 数字', JSON.stringify(F.svgSizeOf('')), JSON.stringify({ width: 100, height: 50 })) eq('带 px 单位', JSON.stringify(F.svgSizeOf('')), JSON.stringify({ width: 100, height: 50 })) eq('回退 viewBox', JSON.stringify(F.svgSizeOf('')), JSON.stringify({ width: 1024, height: 768 })) eq('百分比尺寸量不出 → null', F.svgSizeOf(''), null) eq('无尺寸属性 → null', F.svgSizeOf(''), null) // ---- ④ SVG → PNG 光栅化 ---- function installFakeDom(dims) { const calls = { fill: 0, draw: 0, w: 0, h: 0, decode: 0 } box.Image = function FakeImage() { const img = { naturalWidth: 0, naturalHeight: 0, onload: null, onerror: null } Object.defineProperty(img, 'src', { set(v) { calls.decode++ img.naturalWidth = dims.w img.naturalHeight = dims.h setTimeout(() => { if (img.onload) img.onload() }, 0) }, get() { return '' }, }) return img } box.document = { createElement() { return { width: 0, height: 0, getContext() { return { fillStyle: '', fillRect() { calls.fill++ }, drawImage() { calls.draw++ } } }, toDataURL() { calls.w = this.width; calls.h = this.height; return 'data:image/png;base64,AAAA' }, } }, } return calls } ;(async () => { // 无 DOM 能力:必须 resolve(null),绝不抛 const bare = {} new Function('window', 'globalThis', SRC)(bare, bare) eq('无 DOM 环境 → resolve(null)', await bare.IWBImageFormat.svgToPngDataUrl(b64(''), { maxEdge: 256 }), null) const calls = installFakeDom({ w: 2048, h: 1024 }) const png = await F.svgToPngDataUrl(b64(''), { maxEdge: 512 }) eq('输出是 PNG dataURL', png, 'data:image/png;base64,AAAA') eq('解码一次', calls.decode, 1) eq('先铺白底(透明底 SVG 不会变黑)', calls.fill, 1) ok('真的画了图', calls.draw === 1) eq('长边缩到 maxEdge 且等比', calls.w + 'x' + calls.h, '512x256') // naturalWidth=0(无固有尺寸)→ 用 SVG 文本里的 viewBox 兜底 const calls2 = installFakeDom({ w: 0, h: 0 }) await F.svgToPngDataUrl(b64(''), { maxEdge: 400 }) eq('无固有尺寸时按 viewBox 定比例', calls2.w + 'x' + calls2.h, '400x200') // 解码失败 → null(不抛、不把坏图写成 PNG) box.Image = function BrokenImage() { const img = { onload: null, onerror: null } Object.defineProperty(img, 'src', { set() { setTimeout(() => { if (img.onerror) img.onerror() }, 0) }, get() { return '' } }) return img } eq('解码失败 → resolve(null)', await F.svgToPngDataUrl(b64(''), { maxEdge: 256 }), null) console.log('\n' + (fail ? '❌ ' : '✅ ') + 'image-format: ' + pass + ' passed, ' + fail + ' failed') if (fail) process.exit(1) })()