Files

131 lines
8.3 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-dedupe.js)行为测试 —— 独立文件
//
// 背景(v3.24.75):同一张截图连粘两次 / 同一文件拖两次 / 从粘贴与拖放各进一次,
// 画布上会出现内容完全相同的多个参考图节点。
//
// 这条测试守的**核心**是「两种存储形态能算到一起」:
// 大图 → wImgRefWrite → ImageBlobStore.put(dataUrl) → key = imgblob_<FNV1a(dataUrl)>_<size36>
// 小图 → 直接把同一个 dataUrl 字符串内联
// ⇒ 引用形态取 key 里的哈希段,内联形态对字符串现算同一算法,同一张图必须相等。
// 一旦有人把 image-blob-store 的哈希算法改了、或把这里的解析改了,本文件立刻打红。
const fs = require('fs')
const path = require('path')
const root = path.join(__dirname, '..')
const box = {}
// 必须先挂真实的 image-blob-store:hashOf 正常路径就从它取(不能只测 fallback,否则测的是兜底路径)
new Function('window', 'globalThis', fs.readFileSync(path.join(root, 'src/image-blob-store.js'), 'utf8'))(box, box)
new Function('window', 'globalThis', fs.readFileSync(path.join(root, 'src/image-dedupe.js'), 'utf8'))(box, box)
const D = box.IWBImageDedupe
const S = box.IWBImageBlobStore
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 ne = (name, a, b) => ok(name + '(' + JSON.stringify(a) + ' ≠ ' + JSON.stringify(b) + ')', a !== b && a !== '' && b !== '')
ok('模块加载并导出 IWBImageDedupe', !!D && typeof D.keyOf === 'function' && typeof D.index === 'function')
// ── ① 哈希必须与 image-blob-store 逐位一致 ───────────────────────────────────
// 两份实现(一份在 store 一份是 dedupe 的兜底)若漂移,内联形态与引用形态就永远算不到一起,
// 去重会**静默失效**(功能看着在,实际从不命中)。这里直接交叉验证。
{
const samples = ['', 'a', 'data:image/png;base64,AAAA', '中文提示词与标点,。!', 'x'.repeat(5000), '{"a":1}']
let same = true
for (const s of samples) if (D.hashOf(s) !== S.hashOf(s)) { same = false; console.log(' 不一致:' + JSON.stringify(s.slice(0, 20))) }
ok('hashOf 与 ImageBlobStore.hashOf 对同输入输出一致(含空串/中文/超长)', same)
ok('hashOf 返回 36 进制字符串', /^[0-9a-z]+$/.test(D.hashOf('data:image/png;base64,AAAA')))
}
// ── ② 核心:内联 dataURL 与 {__blob} 引用必须算成同一指纹 ─────────────────────
{
const url1 = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAIAAACQd1PeAAAADElEQVR4nGP4z8AAAAMBAQDJ/pLvAAAAAElFTkSuQmCC'
const url2 = 'data:image/png;base64,ZZZZdifferentbytesZZZZ'
// 模拟 ImageBlobStore.put 的 key 生成(同一算法、同一入参)
const blobKey = (s) => 'imgblob_' + S.hashOf(s) + '_' + Math.ceil(s.length * 0.75).toString(36)
const inlineA = D.keyOf(url1)
const refA = D.keyOf({ __blob: blobKey(url1) })
const inlineB = D.keyOf(url2)
const refB = D.keyOf({ __blob: blobKey(url2) })
eq('内联形态有指纹', inlineA.indexOf('img:') === 0, true)
eq('内联 ↔ 引用(同图)指纹相等', inlineA, refA)
eq('内联 ↔ 引用(另一张图)指纹相等', inlineB, refB)
ne('不同图的指纹不同', inlineA, inlineB)
// 长度不同但内容前缀相同:仍必须区分(不是抽样哈希,是全量哈希)
const longA = 'data:image/png;base64,' + 'A'.repeat(4000)
const longB = 'data:image/png;base64,' + 'A'.repeat(4000) + 'B'
ne('仅末尾多一个字符也算不同图', D.keyOf(longA), D.keyOf(longB))
}
// ── ③ 不该参与去重的形态一律返回 ''(宁可漏判,不可误判)──────────────────
{
eq('{__cache} 不计指纹(抽样 djb2 碰撞率高)', D.keyOf({ __cache: 'https://x/cache/abc123' }), '')
// 写字节失败时的随机 key(imgblob_r<时间戳><随机>_<size>)**没有内容哈希**,但也不需要特判:
// 它天然唯一(时间戳+随机),既不会与内联形态误配,也不会与其他图撞车。
// 这里真正要守的是「不会把两张不同的随机 key 图判成同图」。
ne('随机 key 之间互不相同(不会误判成同图)',
D.keyOf({ __blob: 'imgblob_r1abc2def3ghi_1f' }), D.keyOf({ __blob: 'imgblob_r1abc2def3ghj_1f' }))
eq('无哈希段的 imgblob key 不计指纹', D.keyOf({ __blob: 'imgblob_' }), '')
eq('远程图片链接不计指纹(图床可能换内容)', D.keyOf('https://x.com/a.png'), '')
eq('空串 / null / undefined 不计指纹', D.keyOf('') + '|' + D.keyOf(null) + '|' + D.keyOf(undefined), '||')
eq('数字等非字符串标量不崩', D.keyOf(42), '')
eq('数组不崩', D.keyOf([]), '')
eq('空对象不崩', D.keyOf({}), '')
eq('非 imgblob 前缀的 __blob 不计指纹', D.keyOf({ __blob: 'other_1_2' }), '')
}
// ── ④ 索引:只认 ref 节点,命中/登记/批内去重 ──────────────────────────────
{
const u1 = 'data:image/png;base64,AAA'
const u2 = 'data:image/png;base64,BBB'
const u3 = 'data:image/png;base64,CCC'
const nodes = [
{ id: 'n1', type: 'ref', data: { data: u1 } },
{ id: 'n2', type: 'prompt', data: { text: 'hi' } },
{ id: 'n3', type: 'out', data: { url: '' } },
{ id: 'n4', type: 'ref', data: { url: 'https://x/a.png', data: null } },
{ id: 'n5', type: 'ref', data: { data: u2 } },
{ id: 'n6', type: 'ref', data: { data: { __cache: 'https://x/c/1' } } },
]
const idx = D.index(nodes)
eq('只索引有指纹的 ref(u1/u2 两张)', idx.size(), 2)
eq('命中返回已有节点 id', idx.hit(u1), 'n1')
eq('命中第二张', idx.hit(u2), 'n5')
eq('未登记过的图不命中', idx.hit(u3), '')
eq('无指纹的值不命中', idx.hit('https://x.com/a.png'), '')
eq('prompt / out 节点不参与(不会被当成"原节点")', idx.hit('data:image/png;base64,' + 'zz'), '')
// 批内去重:粘贴两张一样的图,第一张登记后第二张就能拦住
eq('add 之前不命中', idx.hit(u3), '')
idx.add(u3, 'n9')
eq('add 之后命中新节点', idx.hit(u3), 'n9')
eq('add 同一张图第二次不覆盖首个 id', (idx.add(u3, 'n10'), idx.hit(u3)), 'n9')
idx.add(u3, 'n11')
eq('重复 add 不改变 size', idx.size(), 3)
// 空/异常入参
eq('空数组不崩', D.index([]).size(), 0)
eq('null 不崩', D.index(null).size(), 0)
eq('非数组不崩', D.index({ length: 1, 0: { id: 'x', type: 'ref' } }).size(), 0)
eq('节点缺 id 时跳过', D.index([{ type: 'ref', data: { data: u1 } }]).size(), 0)
eq('节点 data 缺失时跳过', D.index([{ id: 'z', type: 'ref' }]).size(), 0)
eq('节点为 null 时跳过', D.index([null, { id: 'z', type: 'ref', data: { data: u1 } }]).size(), 1)
// 同一个值对象(对象引用相同)重复出现:只登记一次
const sameRef = { __blob: 'imgblob_' + S.hashOf(u1) + '_ab' }
eq('同图两种形态同时存在 → 只算一张', D.index([{ id: 'a', type: 'ref', data: { data: u1 } }, { id: 'b', type: 'ref', data: { data: sameRef } }]).size(), 1)
}
// ── ⑤ 缺 store 时的兜底:仍然工作,且算法与 store 一致 ───────────────────────
// 单测/极端环境里 image-blob-store 可能没挂上;兜底实现必须逐位等价,否则引用形态永远对不上。
{
const solo = {}
new Function('window', 'globalThis', fs.readFileSync(path.join(root, 'src/image-dedupe.js'), 'utf8'))(solo, solo)
const D2 = solo.IWBImageDedupe
const s = 'data:image/png;base64,iVBORw0KGgo'
eq('无 store 时仍能算指纹', D2.keyOf(s), D.keyOf(s))
eq('无 store 时懒加载仍能命中引用形态', D2.keyOf({ __blob: 'imgblob_' + S.hashOf(s) + '_ab' }), D.keyOf(s))
}
console.log('')
if (fail) { console.log('❌ image-dedupe: ' + pass + ' passed, ' + fail + ' failed'); process.exit(1) }
console.log('image-dedupe checks passed(' + pass + ' 项:内容哈希跨存储形态一致、{__cache}/远程链接不参与、索引只认 ref、批内去重与兜底等价)')