// 契约:Service Worker **未控制页面**时(首次安装窗口 / 清站点数据后 / 被策略拦截 / 隐私模式), // 参考图缩略图的 **不得**指向 SW-only 的 /__iwb_cache__/ 虚拟路径。 // // 根因(用户实测:图片节点缩略图显示约 1 秒后消失,双击仍能打开图): // wThumbSrc 在 iwbCacheWrite 完成后(本会话 cacheDoneUrls 命中)会把 从内联 dataURL // 换成 /__iwb_cache__/。该路径**只有 sw.js** 能直出(未命中返回 Response.error)⇒ 无 controller // 时该 URL 走网络 → 404 → naturalWidth=0 → 缩略图永久空白;且 wDisplayThumbOf 的 onerror 会把它 // 永久负缓存,于是**刷新才恢复**(刷新走 wResolveImg → iwbCacheBlob → blob:,不经 SW)。 // wResolveImg 早在 v3.87.64 就因同一原因弃用该虚拟路径;wThumbSrc 缺同款门控。 // // 本 spec 用 serviceWorkers:'block' 复现「无 SW 控制」,断言缩略图仍以 dataURL 显示(naturalWidth > 0)。 // 修复前(wThumbSrc 无 wCacheUrlServable 门控)本 spec 必须失败。 const { test, expect } = require('./_pw-coverage') const baseURL = process.env.IWB_BASE_URL || 'http://127.0.0.1:8787' // 关键:阻断 Service Worker,等价于 navigator.serviceWorker.controller === null test.use({ baseURL, viewport: { width: 1440, height: 1000 }, serviceWorkers: 'block' }) const CFG = { providers: [], canvas: {} } // 一张真实可解码的小 PNG(64×64 棋盘格)——保证「能显示」时 naturalWidth > 0 const PNG_DATAURL = (() => { const zlib = require('zlib') const w = 64 const h = 64 const raw = Buffer.alloc((w * 4 + 1) * h) for (let y = 0; y < h; y++) { for (let x = 0; x < w; x++) { const o = y * (w * 4 + 1) + 1 + x * 4 const v = (x + y) & 1 ? 255 : 0 raw[o] = v raw[o + 1] = v raw[o + 2] = v raw[o + 3] = 255 } } const crcT = [] for (let n = 0; n < 256; n++) { let c = n for (let k = 0; k < 8; k++) c = c & 1 ? 0xedb88320 ^ (c >>> 1) : c >>> 1 crcT[n] = c >>> 0 } const crc32 = (b) => { let c = 0xffffffff for (let i = 0; i < b.length; i++) c = crcT[(c ^ b[i]) & 0xff] ^ (c >>> 8) return (c ^ 0xffffffff) >>> 0 } const chunk = (type, data) => { const l = Buffer.alloc(4) l.writeUInt32BE(data.length) const t = Buffer.from(type, 'latin1') const c = Buffer.alloc(4) c.writeUInt32BE(crc32(Buffer.concat([t, data]))) return Buffer.concat([l, t, data, c]) } const ihdr = Buffer.alloc(13) ihdr.writeUInt32BE(w, 0) ihdr.writeUInt32BE(h, 4) ihdr[8] = 8 ihdr[9] = 6 const png = Buffer.concat([ Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]), chunk('IHDR', ihdr), chunk('IDAT', zlib.deflateSync(raw)), chunk('IEND', Buffer.alloc(0)), ]) return 'data:image/png;base64,' + png.toString('base64') })() const WS = { schemaVersion: 1, rev: 1, activeId: 'w1', workspaces: [ { id: 'w1', name: 'W', nodes: [{ id: 'r1', type: 'ref', x: 160, y: 160, data: { url: '', data: PNG_DATAURL, name: 'r.png', localName: null } }], edges: [], groups: [], }, ], } test('SW 未控制页面时参考图缩略图不指向 /__iwb_cache__/ 且不空白', async ({ page }) => { await page.addInitScript((d) => { localStorage.clear() localStorage.setItem('iwb_web_cfg', JSON.stringify(d.cfg)) localStorage.setItem('iwb_web_workspaces', JSON.stringify(d.ws)) localStorage.setItem('iwb_ws_active', 'w1') localStorage.setItem('iwb_ws_w1', JSON.stringify(d.ws.workspaces[0])) }, { cfg: CFG, ws: WS }) await page.goto('/', { waitUntil: 'domcontentloaded' }) await page.waitForTimeout(800) await page.locator('.iwb-home-card:not(.iwb-home-new)').first().dblclick() await expect(page.locator('.iwb-canvas:not(.iwb-canvas-hidden)')).toBeVisible() const img = page.locator('.iwb-node[data-node-id="r1"] img.iwb-ref-single') await expect(img).toBeVisible() // 初次渲染必须能显示(dataURL) await expect.poll(async () => img.evaluate((el) => el.naturalWidth), { timeout: 15000 }).toBeGreaterThan(0) // 等 iwbCacheWrite 落地(cacheDoneUrls 命中)后强制重渲染:拖动节点 ⇒ 状态更新 + 触发保存 await page.waitForTimeout(1500) const box = await page.locator('.iwb-node[data-node-id="r1"]').boundingBox() if (box) { await page.mouse.move(box.x + 30, box.y + 10) await page.mouse.down() await page.mouse.move(box.x + 140, box.y + 90, { steps: 10 }) await page.mouse.up() } await page.waitForTimeout(1500) const info = await img.evaluate((el) => ({ src: String(el.getAttribute('src') || ''), nw: el.naturalWidth })) // 修复前:src 变成 http://127.0.0.1:8787/__iwb_cache__/ ⇒ 无 SW ⇒ 404 ⇒ nw=0 expect(info.src.includes('/__iwb_cache__/'), `缩略图 src 不得为 SW-only 虚拟路径(无 SW 控制时必然 404)。实际 src 前缀=${info.src.slice(0, 48)}`).toBe(false) expect(info.nw, '缩略图应能正常显示(naturalWidth > 0)').toBeGreaterThan(0) })