diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index fd9902f..784f7ad 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -41,7 +41,7 @@ build esbuild 打包到 dist/(单 dist/app.js = src/ 模块按序 node --check app.js 与 dist/app.js 语法 check-dist.js 产物体检 lint ESLint(src/ + app.js,正确性规则) -test:unit 105 个单测文件(jsdom 契约测试;2026-11-07 新增 sync-facts-guard(事实一致性)/ _helpers/source(统一源文本);2026-11-12 新增 provider-ping(测速 no-cors 兜底)) +test:unit 106 个单测文件(jsdom 契约测试;2026-11-07 新增 sync-facts-guard(事实一致性)/ _helpers/source(统一源文本);2026-11-12 新增 provider-ping(测速 no-cors 兜底)) test relay-task-graph-fixture + iwb-v32-web-test test:facts 事实基线守门(见 §5) typecheck 渐进类型检查(tsc --noEmit;范围 = src/*.js,不含 app.js) diff --git a/package.json b/package.json index c9a8d78..f1aad5d 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/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/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/plugins/index.json b/plugins/index.json index dcf9de9..0d6205b 100644 --- a/plugins/index.json +++ b/plugins/index.json @@ -19,9 +19,9 @@ }, { "id": "gallery", - "file": "plugin-gallery.64a531ebc2e2.js", - "version": "1.0.0", - "integrity": "sha256-ZKUx68LiXYmVlIyy4K3wuF3tNwtRvtwuW6nGXFiIFqU=" + "file": "plugin-gallery.5da6614259fe.js", + "version": "1.0.1", + "integrity": "sha256-XaZhQln+duAUnDAkWPBQ2lwMWu5F4kghEwpqgAj7ZUc=" }, { "id": "image-annotate", diff --git a/plugins/runtime/plugin-gallery.64a531ebc2e2.js b/plugins/runtime/plugin-gallery.5da6614259fe.js similarity index 94% rename from plugins/runtime/plugin-gallery.64a531ebc2e2.js rename to plugins/runtime/plugin-gallery.5da6614259fe.js index cca9c96..ef4f698 100644 --- a/plugins/runtime/plugin-gallery.64a531ebc2e2.js +++ b/plugins/runtime/plugin-gallery.5da6614259fe.js @@ -141,10 +141,15 @@ function itemNode (it) { var on = !!picked[it.name] var box = el('div', { class: 'iwb-history-item' + (count() ? ' iwb-history-managed' : '') + (on ? ' iwb-history-selected' : '') }) - var url = '' - try { url = ctx.gallery.thumb(it.name) } catch (e) { url = '' } - if (url && typeof url.then === 'function') url = '' - if (url) { + // 缩略图是**异步**的(ctx.gallery.thumb 返回 Promise):先占位,拿到地址再挂 img + var holder = el('div', { class: 'iwb-history-missing', text: '加载中…' }) + box.appendChild(holder) + Promise.resolve(ctx.gallery.thumb(it.name)).then(function (url) { + if (!url || typeof url !== 'string') { + holder.textContent = '图片已丢失' + return + } + if (!box.isConnected) return var img = el('img', { src: url, loading: 'lazy', title: it.name + '(单击单选 · Shift+单击多选 · 双击放大 · 拖到画布=参考图节点)', draggable: 'true' }) if (it.w && it.h) img.setAttribute('style', 'aspect-ratio:' + it.w + ' / ' + it.h) img.addEventListener('click', function (e) { pick(it.name, !!(e && e.shiftKey)) }) @@ -156,10 +161,8 @@ e.dataTransfer.effectAllowed = 'copy' } catch (err) { /* ignore */ } }) - box.appendChild(img) - } else { - box.appendChild(el('div', { class: 'iwb-history-missing', text: '图片已丢失' })) - } + box.replaceChild(img, holder) + }).catch(function () { holder.textContent = '图片已丢失' }) var acts = el('div', { class: 'iwb-history-actions' }) function act (label, title, fn) { var b = el('button', { class: 'iwb-btn iwb-btn-sm', text: label, title: title }) @@ -217,7 +220,7 @@ global.IWBPluginHost.register({ id: 'gallery', name: '图库', - version: '1.0.0', + version: '1.0.1', apiVersion: 1, capabilities: ['panels', 'gallery'], data: { schemaVersion: 1, maxBytes: 65536 }, diff --git a/tests/plugin-gallery-render-test.js b/tests/plugin-gallery-render-test.js new file mode 100644 index 0000000..9fe6ef1 --- /dev/null +++ b/tests/plugin-gallery-render-test.js @@ -0,0 +1,67 @@ +'use strict' +// 图库插件渲染回归(防止「缩略图异步却当同步用」导致整墙"图片已丢失") +// +// 2026-11-20 真实事故:ctx.gallery.thumb() 返回 Promise,插件把 Promise 当字符串判空 ⇒ 7 张全丢。 +const assert = require('assert') +const fs = require('fs') +const path = require('path') +const { JSDOM } = require('jsdom') + +const root = path.resolve(__dirname, '..') +const src = fs.readFileSync(path.join(root, '..', 'iwb-plugins-desktop', 'gallery', 'src', 'index.js'), 'utf8') + +const dom = new JSDOM('
', { pretendToBeVisual: true }) +const { window } = dom +global.window = window +global.document = window.document + +let mounted = null +const host = { + plugins: [], + register (manifest, factory) { host.plugins.push({ manifest, factory }) }, +} +window.IWBPluginHost = host +// 插件用到的浏览器 API +window.ResizeObserver = class { observe () {} disconnect () {} } +window.confirm = () => true + +;(async () => { + // 在 vm 里跑插件源码(它直接用 window/document) + const vm = require('vm') + const sandbox = { window, document: window.document, console, setTimeout, clearTimeout, Promise, Object, Array, JSON, Math } + vm.runInNewContext(src, sandbox) + + assert.strictEqual(host.plugins.length, 1, '插件应注册一次') + const { manifest, factory } = host.plugins[0] + assert.strictEqual(manifest.id, 'gallery') + assert.ok(manifest.capabilities.indexOf('gallery') >= 0, '应声明 gallery 能力') + + 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: [{ name: 'a.png', w: 200, h: 300 }, { name: 'b.png', w: 200, h: 120 }], total: 2 } }, + thumb: async (n) => { calls.thumb++; return 'data:image/svg+xml;utf8,' + encodeURIComponent('') }, + preview: () => {}, download: async () => ({ ok: true }), insert: async () => ({ ok: true }), remove: async () => ({ ok: true }), + }, + } + factory(ctx) + assert.ok(mounted, '应注册面板 mount') + + const node = window.document.getElementById('host') + mounted(node) + + // 等 list + 2 次 thumb 的微任务跑完 + for (let i = 0; i < 20; i++) await new Promise((r) => setTimeout(r, 10)) + + const imgs = Array.from(node.querySelectorAll('.iwb-history-item img')) + const missing = Array.from(node.querySelectorAll('.iwb-history-missing')).map((x) => x.textContent).filter((t) => t !== '加载中…') + assert.strictEqual(calls.list, 1, '应只拉一次列表') + assert.strictEqual(calls.thumb, 2, '每张图各取一次缩略图') + assert.strictEqual(imgs.length, 2, '两张图都应渲染出 img,实际 ' + imgs.length) + assert.ok(imgs.every((i) => String(i.getAttribute('src')).indexOf('data:image/svg') === 0), 'img.src 应来自 thumb()') + assert.strictEqual(missing.length, 0, '不应出现「图片已丢失」:' + missing.join(',')) + + console.log('plugin-gallery checks passed(异步缩略图 / 2 张图渲染 / 无丢失)') +})().catch((e) => { console.error(e); process.exit(1) })