Files
image-workbench-desktop/tests/image-download-contract-test.js
T
qiqi b12463cf0a
Verify (Gitea) / gate (build · unit · facts) (push) Failing after 1m17s
feat(download): 文件版支持取消/一次回退/URL 去重 key
2026-12-04 12:37:16 +08:00

63 lines
5.4 KiB
JavaScript
Raw 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.
#!/usr/bin/env node
'use strict'
/* 图片读取/下载口径守卫(2026-12-03)
*
* 规则来源:docs/standards/image-read-and-download.md
* ① 下载命名一律用图库行名(wOutNameOf),写死的 image.png 只允许做未落地兜底;
* ② 预览只读本地:任务队列 preview 不得直连远程,也不得把 320 缩略图当原图;
* ③ 老记录原图必须能从 images/gallery 读(storage-file 暴露 galleryOriginalOf/Blob)。
*/
const fs = require('fs')
const path = require('path')
const root = path.resolve(__dirname, '..')
const read = (p) => fs.readFileSync(path.join(root, p), 'utf8')
let fail = 0
const ok = (cond, msg) => { if (cond) console.log(' PASS ' + msg); else { fail++; console.error(' FAIL ' + msg) } }
const doc = read('docs/standards/image-read-and-download.md')
ok(/取源顺序/.test(doc) && /wOutNameOf/.test(doc), '规范文档存在且写了取源顺序与命名')
const sf = read('src/storage-file.js')
ok(/galleryOriginalOf/.test(sf) && /galleryOriginalBlob/.test(sf), 'storage-file 暴露本地原图读取(预览/下载)')
const app = read('app.js')
ok(/function wOutNameOf \(url\)/.test(app), '宿主提供 wOutNameOf(按 url 查图库行名)')
ok(/downloadUrl\(u, wOutNameOf\(u\)/.test(app), '批量下载用图库行名')
ok(/galleryOriginalBlob\(item\)/.test(app), '下载取源(wResolveOutputSrc)本地优先')
const qBlock = app.slice(app.indexOf('preview: async (id) => {'), app.indexOf('copyLink: (id) => {'))
ok(qBlock.length > 0 && !/\^https\?:/.test(qBlock), '任务队列预览不直连远程')
ok(!/IDB\.get\(row\.idb\)/.test(qBlock), '任务队列预览不把 320 缩略图当原图')
ok(/not-local/.test(qBlock), '任务队列预览在本地无图时给出「未下载」提示')
ok(!/wTauri\.http\.fetch/.test(app), '图片取字节不再走 http 插件(受域名白名单限制),统一用 fetch_image_b64')
ok(/mime0\.indexOf\('image\/'\) !== 0/.test(app), 'wRustFetchBlob 校验 Content-Type(拒绝把 HTML 错误页当图片)')
ok(read('src/image-display-thumb.js').indexOf('^https?:') >= 0 && read('src/image-display-thumb.js').indexOf('fetchBlob') >= 0, '显示缩略图只对远程地址走 Rust(data:/blob: 不浪费 IPC)')
ok(/async function wRustFetchBlob \(url\)/.test(app), '宿主提供统一 Rust 取字节入口 wRustFetchBlob')
ok(/wRustFetchBlob\(fkEarly\)/.test(app) && /wRustFetchBlob\(su\)/.test(app), 'storeOutputThumb / downloadUrl 走系统网络栈兜底')
ok(/desktopFetch: async \(u\) => \{ const b = await wRustFetchBlob/.test(app), 'gallery-sync desktopFetch 走 Rust(不再用白名单 http.fetch)')
ok(/path-outside-base/.test(read('src-tauri/src/lib.rs')) && /tokio::fs/.test(read('src-tauri/src/lib.rs')), 'fetch_image_to 限制在资料目录内 + tokio 异步写盘')
ok(/cancel_fetch/.test(read('src-tauri/src/lib.rs')) && /__baseFallback/.test(read('src/image-ingest.js')), '文件版支持取消 + 一次性 base64 回退')
ok(/imgblob_f/.test(read('src/image-ingest.js')), '文件版引用 key 按 URL 去重')
ok(/displayThumbDataUrlOfRef/.test(read('src/storage-file.js')) && /cleanupCacheTmp/.test(read('src/storage-file.js')), '文件接管后有缩略图预热,临时目录有清理')
ok(/rustFileDownload: draft\.canvas\.rustFileDownload === true/.test(read('src/ui-settings-sections.js')), '设置保存保留 rustFileDownload 开关')
ok(/fetch_image_to/.test(read('src-tauri/src/lib.rs')), 'Rust 提供 fetch_image_to(流式写文件)')
ok(/putFileFrom/.test(read('src/storage-file.js')), '存储层支持接管已下载文件(不经过 base64)')
ok(/fetchFile\s*:/.test(app) && /writeFileRef\s*:/.test(app), 'ingest 接线文件版通道(默认关闭)')
ok(/IWB_RUST_FILE/.test(app), '文件版下载有显式开关(默认关)')
ok(/cacheUiRef\.current\.loaded && cacheUiRef\.current\.shellView === 'canvas'/.test(app), '远程参考图缓存用 ref 读当前视图(修首帧闭包导致 enabled 恒 false)')
ok(/fetchBlob/.test(read('src/image-remote-cache.js')) && /fetchBlob/.test(read('src/image-display-thumb.js')) && /fetchBlob/.test(read('src/runtime-download-auto.js')), '三个模块支持注入 Rust 取字节')
ok(/async function wOutThumbEnsure \(url\)/.test(app), '输出节点缩略图兜底:按图库同一套本地取源补缩略图')
ok(/wOutThumbEnsure\(r\.url\)/.test(read('src/ui-node-bodies.js')), '输出节点在无本地缩略图时补取(thumbHash → 磁盘原图 320)')
ok(/function wPreviewEntryOf \(url\)/.test(app), '输出节点预览按 url 查图库行(__gallery 条目)')
ok(/previewEntryOf\(x\.url\)/.test(read('src/ui-node-bodies.js')) && /wPreviewEntryOf/.test(read('src/ui-node-bodies.js')), '输出节点双击预览使用图库条目(本地原图优先)')
const bodies = read('src/ui-node-bodies.js')
ok(/downloadUrl\(src, \(typeof wOutNameOf/.test(bodies), '输出节点下载用图库行名')
const view = read('src/ui-canvas-view.js')
ok(/wOutNameOf\(entry && entry\.url\)/.test(view), '预览下载用图库行名')
ok(/galleryOriginalOf/.test(view) && /galleryOriginalBlob/.test(view), '预览按行名读本地原图(不退回缩略图/远程)')
if (fail) { console.error('image-download contract: ' + fail + ' 项不符(见 docs/standards/image-read-and-download.md)'); process.exit(1) }
console.log('image-download contract checks passed(取源本地优先 / 命名统一图库行名 / 预览不直连远程)')