Files
image-workbench-desktop/tests/image-download-contract-test.js
T

42 lines
2.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), '任务队列预览在本地无图时给出「未下载」提示')
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(取源本地优先 / 命名统一图库行名 / 预览不直连远程)')