Files
qiqi 9745454094
Verify (Gitea) / gate (build · unit · facts) (push) Failing after 43s
fix(album): 1.1.6 相册宫格重建后缩略图全空 / 透明图黑底
- applyThumb 不再判 img.isConnected(命中内存缓存时元素尚未 appendChild,判活必假 ⇒ src 从不赋值)
- 透明像素走 PNG(JPEG 把 alpha 压成纯黑),照片仍走 JPEG 0.85
- 缓存名带 t2- 版本代号,读时双后缀、写成功再清旧缓存
- 导入收尾不再强制整块重建
- 新增 e2e tests/iwb-album-thumbs.spec.js(五条路径断言每格有 src),门禁 spec 53→54
- 插件源码真源 iwb-plugins-desktop 已同版提交,市场 1.1.6 已上线
2026-11-26 10:28:15 +08:00

174 lines
8.2 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.
// 相册插件缩略图回归(2026-11-26,album 1.1.6)
//
// 背景(真机事故,用户报障):1.1.5 把「元素存活判断」提进了公共 helper applyThumb,
// 而命中**内存缓存**的那条分支在 `btn.appendChild(img)` **之前**就调用了它 ——
// 那一刻 img.isConnected 必然是 false ⇒ 直接 return,src 永远赋不上。
// 结果:每次宫格重建(首次导入 / 追加 / 翻页 / 切密度 / 拖拽排序)之后所有格子空白,
// 而「切走画布再回来」却正常(那时内存缓存是空的,走的是异步分支)。
// 磁盘里缩略图其实是好的 —— 只是没被贴到 <img> 上,所以这条 bug 极易被误判成「读图失败」。
//
// 本用例不启动应用,直接在浏览器里用假宿主 ctx 装插件,按用户四条路径断言
// 「每一格 <img> 都有 src」,并顺带钉住「透明图必须存 PNG(alpha 保留)」——
// 1.1.5 一律编码 JPEG,透明区会变成纯黑。
const fs = require('fs')
const path = require('path')
const { test, expect } = require('./_pw-coverage')
const SRC = fs.readFileSync(path.join(__dirname, '..', 'plugins', 'album', 'main.js'), 'utf8')
test.use({ viewport: { width: 900, height: 800 } })
test('相册缩略图:重建后每格都要有 src,透明图要保留 alpha', async ({ page }) => {
await page.setContent('<body style="margin:0"></body>')
const r = await page.evaluate(async (src) => {
const wait = (ms) => new Promise((res) => setTimeout(res, ms))
// 源图:一张带 alpha 的 PNG(模拟抠图)、一张不透明图(模拟照片)
const mkTransparent = () => {
const cv = document.createElement('canvas'); cv.width = 200; cv.height = 200
const c = cv.getContext('2d')
c.fillStyle = '#e02020'; c.fillRect(50, 50, 100, 100)
return cv.toDataURL('image/png')
}
const mkOpaque = () => {
const cv = document.createElement('canvas'); cv.width = 200; cv.height = 200
const c = cv.getContext('2d')
for (let y = 0; y < 200; y += 4) for (let x = 0; x < 200; x += 4) { c.fillStyle = 'hsl(' + ((x * 3 + y * 7) % 360) + ',70%,55%)'; c.fillRect(x, y, 4, 4) }
return cv.toDataURL('image/png')
}
const transparent = mkTransparent()
const opaque = mkOpaque()
let setup = null
window.IWBPluginHost = { register: (m, fn) => { setup = fn } }
// eslint-disable-next-line no-new-func
new Function(src)()
const defs = {}
const files = new Map()
const picks = []
const ctx = {
settings: { get: () => true },
nodes: { register: (d) => { defs.album = d } },
images: {
canReference: () => true,
dataUrl: async (s) => (String((s && s.__file) || '').indexOf('photo') >= 0 ? opaque : transparent),
reference: async (pth) => ({ ok: true, item: { path: pth, name: pth.split('/').pop(), size: 1234, mtime: 1 } }),
stat: async () => ({ ok: true, exists: true }),
pick: async () => (picks.shift() || []),
listImages: async () => [], importPath: async () => ({}), importDataUrl: async () => ({}),
},
files: {
get: async (n) => (files.has(n) ? { ok: true, url: files.get(n) } : { ok: false }),
put: async (n, u) => { files.set(n, u); return { ok: true } },
remove: async (n) => { files.delete(n); return { ok: true } },
list: async () => [], stat: async () => ({ ok: true, files: [], bytes: 0, count: 0 }),
},
}
setup(ctx)
const node = { id: 'n1', w: 300, h: 470, data: { outKind: 'images', albumMode: 'per-image', density: 'S', page: 0, onlyPage: false, items: [], keepOriginal: true } }
const host = document.createElement('div')
host.style.cssText = 'position:relative;width:300px;height:470px'
document.body.appendChild(host)
const props = { onUpdate: (id, patch) => Object.assign(node.data, patch), onSnapshot: () => {} }
const render = (h) => defs.album.render(node, props, h || host)
const addBtn = () => Array.from(host.querySelectorAll('.iwb-album-btn')).find((b) => /添加图片/.test(b.textContent || ''))
const cellsOf = (h) => Array.from((h || host).querySelectorAll('.iwb-album-cell:not(.iwb-album-add)'))
const emptySrc = (h) => cellsOf(h).filter((c) => {
const im = c.querySelector('img')
return !im || (im.getAttribute('src') || '').length < 30
}).length
const idle = async () => {
for (let i = 0; i < 120; i++) {
await wait(50)
const b = addBtn()
if (b && !/导入|缩略图|选择中|复制/.test(b.textContent || '')) { await wait(150); return }
}
}
const state = []
const snap = (label) => state.push({ label: label, cells: cellsOf().length, empty: emptySrc() })
render()
// A 首次导入 6 张
picks.push(['/tmp/a.png', '/tmp/b.png', '/tmp/c.png', '/tmp/d.png', '/tmp/e.png', '/tmp/f.png'])
addBtn().click()
await idle()
snap('首次导入 6 张')
// B 追加 1 张(不透明图,顺带验证 jpg 分支)
picks.push(['/tmp/photo.png'])
addBtn().click()
await idle()
snap('追加 1 张')
// C 切密度(每一档都会整块重建)
for (const k of ['M', 'L', 'S']) {
const seg = Array.from(host.querySelectorAll('.iwb-album-seg span')).find((x) => x.textContent === k)
seg.click()
await wait(250)
snap('切密度 ' + k)
}
// D 压矮节点造成 2 页 → 翻页来回
node.h = 210
render()
await wait(300)
const pager = host.querySelector('.iwb-album-pg')
const pageText = pager ? pager.textContent : ''
if (pager) {
const spans = pager.querySelectorAll('span')
if (spans[1]) { spans[1].click(); await wait(300); snap('翻到第 2 页') }
if (spans[0]) { spans[0].click(); await wait(300); snap('翻回第 1 页') }
}
// E 换宿主 = 切画布再回来(内存缓存清空,只能走插件文件缓存)
node.h = 470
const host2 = document.createElement('div')
host2.style.cssText = 'position:relative;width:300px;height:470px'
document.body.appendChild(host2)
render(host2)
for (let i = 0; i < 60; i++) {
await wait(50)
if (cellsOf(host2).length && emptySrc(host2) === 0) break
}
state.push({ label: '换宿主(切画布)', cells: cellsOf(host2).length, empty: emptySrc(host2) })
// F 透明图必须 PNG 且 alpha 保留;不透明图走 JPEG
const firstSrc = (cellsOf(host2)[0].querySelector('img') || {}).src || ''
// 第 7 张是不透明图(追加那张);换宿主后格子在 0..6,按前缀找 JPEG 更稳
const allSrc = cellsOf(host2).map((c) => (c.querySelector('img') || {}).src || '')
const photoSrc = allSrc.find((x) => x.indexOf('data:image/jpeg') === 0) || ''
const cornerOf = (url) => new Promise((res) => {
const im = new Image(); im.onload = () => {
const cv = document.createElement('canvas'); cv.width = im.width; cv.height = im.height
const c = cv.getContext('2d'); c.drawImage(im, 0, 0)
const d = c.getImageData(2, 2, 1, 1).data
res([d[0], d[1], d[2], d[3]])
}; im.onerror = () => res(null); im.src = url
})
return {
state: state,
pageText: pageText,
firstPrefix: firstSrc.slice(0, 22),
photoPrefix: photoSrc.slice(0, 22),
corner: await cornerOf(firstSrc),
cacheNames: Array.from(files.keys()).sort(),
}
}, SRC)
// ① 每条路径的格子都必须有 src(1.1.5 的回归点)
for (const s of r.state) {
expect(s.cells, s.label + ' 应渲染出格子').toBeGreaterThan(0)
expect(s.empty, s.label + ':有 ' + s.empty + ' 格没有缩略图 src').toBe(0)
}
// ② 翻页确实发生了(否则 D 步骤形同虚设)
expect(r.pageText).toMatch(/1\/2/)
// ③ 透明图 → PNG(alpha 保留,不是黑底);不透明图 → JPEG
expect(r.firstPrefix).toBe('data:image/png;base64,')
expect(r.photoPrefix.indexOf('data:image/jpeg')).toBe(0)
expect(r.corner, '透明缩略图左上角应保持透明(旧版 JPEG 是 rgba(0,0,0,255) 黑底)').toEqual([0, 0, 0, 0])
// ④ 缓存文件名带版本代号,后缀与真实格式一致(旧 t-*.jpg 不再命中)
expect(r.cacheNames.length).toBeGreaterThan(0)
for (const n of r.cacheNames) expect(n).toMatch(/^t2-[0-9a-f]{8}\.(png|jpg)$/)
})