From 393d83550c7a971ef1d6892268ad5f74b865fde5 Mon Sep 17 00:00:00 2001 From: qiqi Date: Fri, 20 Nov 2026 12:21:19 +0800 Subject: [PATCH] =?UTF-8?q?test(gallery):=20=E8=A6=86=E7=9B=96=E5=9D=8F?= =?UTF-8?q?=E5=9B=BE=E4=B8=8D=E7=95=99=E7=99=BD=E5=9D=97=20+=20=E9=9D=A2?= =?UTF-8?q?=E6=9D=BF=E5=8F=AF=E8=A7=81=E8=87=AA=E5=8A=A8=E6=81=A2=E5=A4=8D?= =?UTF-8?q?/=E9=98=B2=E6=8A=96/=E6=89=8B=E5=8A=A8=E5=88=B7=E6=96=B0?= =?UTF-8?q?=EF=BC=881.3.0=20=E5=9B=9E=E5=BD=92=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- package.json | 2 +- tests/plugin-gallery-recovery-test.js | 103 ++++++++++++++++++++++++++ 2 files changed, 104 insertions(+), 1 deletion(-) create mode 100644 tests/plugin-gallery-recovery-test.js diff --git a/package.json b/package.json index 0588d49..413c325 100644 --- a/package.json +++ b/package.json @@ -10,7 +10,7 @@ "app:build": "tauri build", "lint": "eslint src app.js", "test": "node tests/relay-task-graph-fixture-test.js && node tests/plugin-style-node-test.js && node tests/inherit-text-test.js", - "test:unit": "node tests/unit-test.js && node tests/canvas-actions-test.js && node tests/canvas-performance-test.js && node tests/canvas-drag-performance-test.js && node tests/resize-observer-performance-test.js && node tests/node-drag-jank-test.js && node tests/edge-graph-performance-test.js && node tests/persistence-export-images-test.js && node tests/settings-hotkeys-size-presets-test.js && node tests/custom-size-validation-test.js && node tests/image-node-performance-test.js && node tests/drag-frame-performance-test.js && node tests/canvas-edge-memo-test.js && node tests/canvas-node-transform-test.js && node tests/canvas-dot-zoom-test.js && node tests/duplicate-ws-fresh-source-test.js && node tests/single-canvas-shell-test.js && node tests/navigation-layout-test.js && node tests/home-canvas-library-test.js && node tests/left-resource-layout-test.js && node tests/interface-hierarchy-test.js && node tests/three-page-layout-test.js && node tests/page-chrome-regression-test.js && node tests/task-cache-test.js && node tests/storage-config-shape-test.js && node tests/storage-zip-test.js && node tests/canvas-node-factory-test.js && node tests/runtime-error-test.js && node tests/runtime-lifecycle-test.js && node tests/runtime-fetch-test.js && node tests/runtime-channel-test.js && node tests/runtime-boundary-test.js && node tests/runtime-record-test.js && node tests/runtime-recovery-test.js && node tests/runtime-diagnostic-record-test.js && node tests/runtime-metrics-test.js && node tests/runtime-run-context-test.js && node tests/runtime-execution-context-test.js && node tests/storage-gc-canvas-refs-test.js && node tests/storage-health-test.js && node tests/generation-state-test.js && node tests/canvas-store-test.js && node tests/storage-result-test.js && node tests/storage-local-test.js && node tests/storage-domain-test.js && node tests/storage-runtime-test.js && node tests/storage-indexeddb-test.js && node tests/generation-scheduler-test.js && node tests/gallery-query-test.js && node tests/gallery-hydration-test.js && node tests/image-blob-store-test.js && node tests/image-preview-derive-test.js && node tests/image-format-test.js && node tests/clipboard-graph-test.js && node tests/clipboard-graph-fallback-test.js && node tests/image-admit-test.js && node tests/image-dedupe-test.js && node tests/clipboard-kind-test.js && node tests/clipboard-paste-test.js && node tests/clipboard-copy-test.js && node tests/paste-once-test.js && node tests/list-e2e-specs-test.js && node tests/desktop-csp-loopback-test.js && node tests/architecture-boundary-guard-test.js && node tests/plugin-host-test.js && node tests/plugin-data-test.js && node tests/plugin-loader-test.js && node tests/plugin-bundle-test.js && node tests/plugin-install-test.js && node tests/plugin-gallery-render-test.js && node tests/image-compress-test.js && node tests/storage-fallback-indexeddb-test.js && node tests/canvas-normalize-test.js && node tests/canvas-geometry-test.js && node tests/canvas-history-test.js && node tests/canvas-ops-test.js && node tests/canvas-text-test.js && node tests/history-store-test.js && node tests/plugin-integrity-test.js && node tests/workspace-sessions-store-test.js && node tests/workspace-list-test.js && node tests/workspace-purge-test.js && node tests/provider-ping-test.js && node tests/ui-groups-sidebar-test.js && node tests/ui-lib-sidebar-test.js && node tests/runtime-diagnostics-hotpath-test.js && node tests/storage-log-failure-test.js && node tests/storage-convergence-test.js && node tests/swallow-wiring-test.js && node tests/workbench-logic-test.js && node tests/storage-autobackup-test.js && node tests/storage-outputs-mutate-test.js && node tests/sync-facts-guard-test.js && node tests/e2e-fixed-waits-ratchet-test.js && node tests/shell-render-behavior-test.js && node tests/canvas-meta-notice-behavior-test.js && node tests/source-text-ratchet-test.js && node tests/prompt-md-roundtrip-test.js && node tests/generation-results-store-test.js && node tests/out-groups-order-test.js && node tests/ui-workbench-hooks-test.js", + "test:unit": "node tests/unit-test.js && node tests/canvas-actions-test.js && node tests/canvas-performance-test.js && node tests/canvas-drag-performance-test.js && node tests/resize-observer-performance-test.js && node tests/node-drag-jank-test.js && node tests/edge-graph-performance-test.js && node tests/persistence-export-images-test.js && node tests/settings-hotkeys-size-presets-test.js && node tests/custom-size-validation-test.js && node tests/image-node-performance-test.js && node tests/drag-frame-performance-test.js && node tests/canvas-edge-memo-test.js && node tests/canvas-node-transform-test.js && node tests/canvas-dot-zoom-test.js && node tests/duplicate-ws-fresh-source-test.js && node tests/single-canvas-shell-test.js && node tests/navigation-layout-test.js && node tests/home-canvas-library-test.js && node tests/left-resource-layout-test.js && node tests/interface-hierarchy-test.js && node tests/three-page-layout-test.js && node tests/page-chrome-regression-test.js && node tests/task-cache-test.js && node tests/storage-config-shape-test.js && node tests/storage-zip-test.js && node tests/canvas-node-factory-test.js && node tests/runtime-error-test.js && node tests/runtime-lifecycle-test.js && node tests/runtime-fetch-test.js && node tests/runtime-channel-test.js && node tests/runtime-boundary-test.js && node tests/runtime-record-test.js && node tests/runtime-recovery-test.js && node tests/runtime-diagnostic-record-test.js && node tests/runtime-metrics-test.js && node tests/runtime-run-context-test.js && node tests/runtime-execution-context-test.js && node tests/storage-gc-canvas-refs-test.js && node tests/storage-health-test.js && node tests/generation-state-test.js && node tests/canvas-store-test.js && node tests/storage-result-test.js && node tests/storage-local-test.js && node tests/storage-domain-test.js && node tests/storage-runtime-test.js && node tests/storage-indexeddb-test.js && node tests/generation-scheduler-test.js && node tests/gallery-query-test.js && node tests/gallery-hydration-test.js && node tests/image-blob-store-test.js && node tests/image-preview-derive-test.js && node tests/image-format-test.js && node tests/clipboard-graph-test.js && node tests/clipboard-graph-fallback-test.js && node tests/image-admit-test.js && node tests/image-dedupe-test.js && node tests/clipboard-kind-test.js && node tests/clipboard-paste-test.js && node tests/clipboard-copy-test.js && node tests/paste-once-test.js && node tests/list-e2e-specs-test.js && node tests/desktop-csp-loopback-test.js && node tests/architecture-boundary-guard-test.js && node tests/plugin-host-test.js && node tests/plugin-data-test.js && node tests/plugin-loader-test.js && node tests/plugin-bundle-test.js && node tests/plugin-install-test.js && node tests/plugin-gallery-render-test.js && node tests/plugin-gallery-recovery-test.js && node tests/image-compress-test.js && node tests/storage-fallback-indexeddb-test.js && node tests/canvas-normalize-test.js && node tests/canvas-geometry-test.js && node tests/canvas-history-test.js && node tests/canvas-ops-test.js && node tests/canvas-text-test.js && node tests/history-store-test.js && node tests/plugin-integrity-test.js && node tests/workspace-sessions-store-test.js && node tests/workspace-list-test.js && node tests/workspace-purge-test.js && node tests/provider-ping-test.js && node tests/ui-groups-sidebar-test.js && node tests/ui-lib-sidebar-test.js && node tests/runtime-diagnostics-hotpath-test.js && node tests/storage-log-failure-test.js && node tests/storage-convergence-test.js && node tests/swallow-wiring-test.js && node tests/workbench-logic-test.js && node tests/storage-autobackup-test.js && node tests/storage-outputs-mutate-test.js && node tests/sync-facts-guard-test.js && node tests/e2e-fixed-waits-ratchet-test.js && node tests/shell-render-behavior-test.js && node tests/canvas-meta-notice-behavior-test.js && node tests/source-text-ratchet-test.js && node tests/prompt-md-roundtrip-test.js && node tests/generation-results-store-test.js && node tests/out-groups-order-test.js && node tests/ui-workbench-hooks-test.js", "test:diagnostics": "node tests/runtime-diagnostics-hotpath-test.js", "test:facts": "node tests/architecture-facts-guard-test.js && node tests/release-records-test.js", "plugins:gc": "node scripts/plugins-publish.mjs --gc", diff --git a/tests/plugin-gallery-recovery-test.js b/tests/plugin-gallery-recovery-test.js new file mode 100644 index 0000000..6e54fe3 --- /dev/null +++ b/tests/plugin-gallery-recovery-test.js @@ -0,0 +1,103 @@ +'use strict' +// 图库插件「可靠性」回归(gallery 1.3.0) +// +// 1) 坏图/截断缩略图(naturalWidth=0)不得留白块:重试后必须落到可点击重试的占位文案。 +// 2) 自动恢复:面板重新可见(visibilitychange / focus)且距上次拉取 >5s 时重拉列表; +// 5s 内不重复拉(防抖),底部「刷新」按钮可强制重拉。 +const assert = require('assert') +const fs = require('fs') +const path = require('path') +const vm = require('vm') +const { JSDOM } = require('jsdom') + +const src = fs.readFileSync(path.resolve(__dirname, '..', '..', 'iwb-plugins-desktop', 'gallery', 'src', 'index.js'), 'utf8') + +const wait = (ms) => new Promise((r) => setTimeout(r, ms)) + +async function boot ({ items, thumb, clock }) { + const dom = new JSDOM('
', { pretendToBeVisual: true }) + const { window } = dom + window.ResizeObserver = class { observe () {} disconnect () {} } + window.confirm = () => true + + const registered = [] + window.IWBPluginHost = { plugins: [], register (manifest, factory) { registered.push({ manifest, factory }) } } + + class FakeDate extends Date { static now () { return clock.now } } + + const sandbox = { window, document: window.document, console, setTimeout, clearTimeout, Promise, Object, Array, JSON, Math, Date: FakeDate } + vm.runInNewContext(src, sandbox) + + let mounted = null + const calls = { list: 0, thumb: 0 } + const ctx = { + ui: { registerPanel (p) { mounted = p.mount } }, + data: { read: async () => ({ cfg: { tile: 130 } }), patch: async () => true }, + gallery: { + list: async () => { calls.list++; return { items: items.slice(), total: items.length } }, + thumb: async (n) => { calls.thumb++; return thumb(n) }, + preview: () => {}, + download: async () => ({ ok: true }), + insert: async () => ({ ok: true }), + remove: async () => ({ ok: true }), + }, + } + registered[0].factory(ctx) + const node = window.document.getElementById('host') + mounted(node) + await wait(60) + return { window, document: window.document, node, calls, clock } +} + +;(async () => { + // ---- 1) 坏图不留白块 ---- + { + const clock = { now: 1000 } + const env = await boot({ + items: [{ name: 'bad.png', w: 200, h: 300 }], + thumb: () => 'data:image/png;base64,AAAA', + clock, + }) + const box = env.node.querySelector('.iwb-history-item') + assert.ok(box, '应渲染出格子') + // jsdom 不解码图片:手动触发 load 模拟「拿到坏图」 + for (let i = 0; i < 6; i++) { + const img = box.querySelector('img') + if (img) img.dispatchEvent(new env.window.Event('load')) + await wait(450) + } + const holder = box.querySelector('.iwb-history-missing') + const visibleImg = Array.from(box.querySelectorAll('img')).filter((i) => i.style.display !== 'none') + assert.strictEqual(visibleImg.length, 0, '坏图不得以可见 img 留在格子里(白块)') + assert.ok(holder && /失败/.test(holder.textContent), '坏图应显示可点击重试文案,实际:' + (holder && holder.textContent)) + } + + // ---- 2) 自动恢复 + 防抖 + 手动刷新 ---- + { + const clock = { now: 10_000 } + const good = 'data:image/svg+xml;utf8,' + encodeURIComponent('') + const env = await boot({ items: [{ name: 'a.png', w: 200, h: 300 }], thumb: () => good, clock }) + assert.strictEqual(env.calls.list, 1, '初次挂载应拉一次列表') + + // 5s 内重新可见:不重拉(防抖) + clock.now += 1000 + env.document.dispatchEvent(new env.window.Event('visibilitychange')) + await wait(60) + assert.strictEqual(env.calls.list, 1, '5s 内再次可见不应重复拉取') + + // 超过 5s 后重新可见:自动重拉(图床恢复后无需手动操作) + clock.now += 6000 + env.document.dispatchEvent(new env.window.Event('visibilitychange')) + await wait(60) + assert.strictEqual(env.calls.list, 2, '面板重新可见且间隔 >5s 应自动重拉,实际 ' + env.calls.list) + + // 底部「刷新」按钮:随时强制重拉 + const btn = Array.from(env.node.querySelectorAll('.iwb-gal-foot button')).find((b) => b.textContent === '刷新') + assert.ok(btn, '底部应有「刷新」按钮') + btn.dispatchEvent(new env.window.MouseEvent('click', { bubbles: true })) + await wait(60) + assert.strictEqual(env.calls.list, 3, '点「刷新」应重拉列表,实际 ' + env.calls.list) + } + + console.log('plugin-gallery recovery checks passed(坏图不留白块 / 自动恢复 / 防抖 / 手动刷新)') +})().catch((e) => { console.error(e); process.exit(1) }) \ No newline at end of file