validate / validate (push) Canceled after 0s
- 抽 coverFileName():迁移/保存/导入四处的内联 djb2 哈希合并为一处,
同一张图不会再存出两个文件(文件名 = 内容哈希,天然去重)
- 上传封面、保存词条、另存副本都先 ensureCoverFile() 落成文件,
数据 JSON 里只留 { img: 文件名 };失败退回内联 base64(宁可不省体积,不丢封面)
- 新增 plpGcCoverFiles():回收不再被任何词条引用的封面文件
安全线:数据不是从插件 JSON 读来的不删 / keep 为空不删 / 只删图片扩展名 /
刚 put 未引用的名字进 pending 跳过 / 两张词条共用一个封面天然保留
- 触发:面板加载后延迟 4s 自动一次 + 删词条后一次 + 设置页「清理未引用封面」手动入口
- 设置页占用行改成「封面 N 张 / 文件 M 个」,两者不等即说明有孤儿
- 样式散值归零(4px→6px、margin 13px→12px),插件尺度棘轮基线保持 0
- harness 31 → 44 项(含 plpGcCoverFiles 行为断言:只删未引用 / 空集合不删 / 无 FS 跳过)
154 lines
9.1 KiB
JavaScript
154 lines
9.1 KiB
JavaScript
// promptlib 本地契约测试(纯 node):
|
||
// - v1.9.2:分享带封面开关
|
||
// - v1.9.3:编辑区变量浅主色底色(方案 C,只画色块不改输入)
|
||
// - v1.10.0:文本分享 / 文件分享双路径 + 顶部三按钮 + 右键去分类 + 设置去导入入口
|
||
// - v1.10.1:移除「分享时附带封面图片」开关 + 详情底部分享/插入组间竖向分割线
|
||
// 运行:node promptlib/tools/harness.mjs (退出码 0 = 全过)
|
||
import fs from 'node:fs'
|
||
import path from 'node:path'
|
||
import { fileURLToPath } from 'node:url'
|
||
import vm from 'node:vm'
|
||
|
||
const HERE = path.dirname(fileURLToPath(import.meta.url))
|
||
const ROOT = path.resolve(HERE, '..')
|
||
const src = fs.readFileSync(path.join(ROOT, 'src/index.js'), 'utf8')
|
||
const css = fs.readFileSync(path.join(ROOT, 'styles.css'), 'utf8')
|
||
let failed = 0
|
||
const ok = (name, cond) => {
|
||
if (!cond) failed++
|
||
console.log((cond ? '✓ ' : '✗ ') + name)
|
||
}
|
||
|
||
// ---- 1) 版本 / manifest ----
|
||
ok("manifest.version = 1.11.0", /version:\s*'1\.11\.0'/.test(src))
|
||
ok('package.json version = 1.11.0',
|
||
JSON.parse(fs.readFileSync(path.join(ROOT, 'package.json'), 'utf8')).version === '1.11.0')
|
||
ok('manifest 已移除「分享时附带封面图片」开关声明',
|
||
!/shareWithCover:\s*\{\s*type:\s*'boolean'/.test(src))
|
||
ok('源码不再定义/读取 shareWithCover 设置',
|
||
!/function shareWithCover \(\)/.test(src) && !/S\.get\('shareWithCover'\)/.test(src))
|
||
|
||
// ---- 2) sharePayload 两条显式分支 ----
|
||
const m = src.match(/ function sharePayload \(p, withCover\) \{[\s\S]*?\n \}\n function shareFileName/)
|
||
ok('从 src 找到 sharePayload(p, withCover)', !!m)
|
||
const fnSrc = m ? m[0].replace(/\n function shareFileName$/, '') : ''
|
||
const sandbox = {
|
||
SHARE_TAG: 'IWB-PROMPT-SHARE',
|
||
varsOf: () => [],
|
||
coverToDataUrl: () => { throw new Error('不该读封面') },
|
||
Promise, JSON, String, Number, Array, Object,
|
||
}
|
||
vm.createContext(sandbox)
|
||
vm.runInContext(fnSrc, sandbox)
|
||
const p = { name: '测试词', text: '一只猫', group: '分组A', tags: ['x'], cover: { img: 'a.jpg' } }
|
||
|
||
const textPlain = await sandbox.sharePayload(p, false)
|
||
const bodyPlain = JSON.parse(textPlain.split('\n')[1])
|
||
ok('文本分享(false):分享文本无 cover 字段', !('cover' in bodyPlain.sharing))
|
||
ok('文本分享(false):不读封面(stub 抛错未触发)', true)
|
||
ok('文本分享(false):名字/正文/分组仍在',
|
||
bodyPlain.sharing.name === '测试词' && bodyPlain.sharing.text === '一只猫' && bodyPlain.sharing.group === '分组A')
|
||
|
||
sandbox.coverToDataUrl = () => Promise.resolve('data:image/jpeg;base64,AAAA')
|
||
const textFile = await sandbox.sharePayload(p, true)
|
||
const bodyFile = JSON.parse(textFile.split('\n')[1])
|
||
ok('文件分享(true):分享文本带 cover', bodyFile.sharing.cover === 'data:image/jpeg;base64,AAAA')
|
||
|
||
// ---- 3) v1.10.0 UI 契约 ----
|
||
ok('文本分享复制纯文字(doShareText 用 api.copy)',
|
||
/function doShareText \(p\)[\s\S]{0,260}api\.copy\(text\)/.test(src))
|
||
ok('文件分享导出为文件(doShareFile 用 api.exportText)',
|
||
/function doShareFile \(p\)[\s\S]{0,260}api\.exportText\(shareFileName\(p\), text\)/.test(src))
|
||
ok('文件名做安全化(过滤 \\/:*?"<>|)',
|
||
/replace\(\/\[\\\\\/:\*\?"<>\|]\/g, '_'\)/.test(src))
|
||
ok('剪贴板导入与文件导入共用 importShareItem(1 定义 + 2 调用)',
|
||
(src.match(/importShareItem\s*\(item\)/g) || []).length >= 3)
|
||
ok('文件导入走 api.importText()',
|
||
/function importShareFromFile \(\)[\s\S]{0,160}api\.importText\(\)/.test(src))
|
||
ok('顶部三按钮:文件 / 文本 / 新建',
|
||
/text: '文件'[\s\S]{0,200}text: '文本'[\s\S]{0,200}text: '新建'/.test(src))
|
||
ok('右键菜单已去掉「移动到分组」分类',
|
||
!/移动到分组/.test(src))
|
||
ok('右键菜单含 文本分享 / 文件分享(iwb-ctx-item 项)',
|
||
/iwb-ctx-item', text: '文本分享'[\s\S]{0,260}iwb-ctx-item', text: '文件分享'/.test(src))
|
||
ok('详情底部含 文本分享 / 文件分享 两按钮',
|
||
/text: '文本分享'/.test(src) && /text: '文件分享'/.test(src))
|
||
ok('详情底部分享组与插入组之间是竖向分割线(DOM 顺序:文件分享 → 竖线 → 复制全文)',
|
||
/footEl\.appendChild\(shareFile\)[\s\S]{0,240}iwb-plp-foot-vsep[\s\S]{0,160}footEl\.appendChild\(cp\)/.test(src))
|
||
ok('竖线样式:1px 宽 / 18px 高 / 边框色 / 居中',
|
||
/\.iwb-plp-foot-vsep\s*\{[^}]*width:\s*1px[^}]*height:\s*18px[^}]*align-self:\s*center[^}]*background:\s*var\(--iwb-border\)/.test(css))
|
||
ok('设置面板不再有单条导入入口(impShareBtn 已移除)',
|
||
!/impShareBtn/.test(src))
|
||
ok('旧的「分享…」按钮(带图完整版)已删除',
|
||
!/text: '分享'/.test(src) && !/function doShare \(p\)/.test(src))
|
||
|
||
// ---- 4) v1.9.3 变量着色样式不变量(回归保护)----
|
||
ok('高亮层恢复显示(不再 display:none)',
|
||
/\.iwb-plp-ewrap \.iwb-plp-hl\{display:block!important/.test(css))
|
||
ok('高亮层整层文字透明(不与 textarea 重影)',
|
||
/\.iwb-plp-ewrap \.iwb-plp-hl\{[^}]*color:transparent!important[^}]*-webkit-text-fill-color:transparent!important/.test(css))
|
||
ok('变量 span 只画主色背景、不画文字',
|
||
/\.iwb-plp-hlvar\{[^}]*color:transparent!important[^}]*background:color-mix\(in srgb, var\(--iwb-plp-accent/.test(css))
|
||
ok('变量 span 不加粗(避开点击偏移根因)',
|
||
/\.iwb-plp-hlvar\{font-weight:400!important/.test(css))
|
||
ok('textarea 文字仍可见(-webkit-text-fill-color 兜底)',
|
||
/\.iwb-plp-etext\{color:var\(--iwb-text[^}]*-webkit-text-fill-color:var\(--iwb-text/.test(css))
|
||
ok('高亮层与 textarea 滚动条占位一致',
|
||
/\.iwb-plp-emid \.iwb-plp-ewrap \.iwb-plp-hl\{overflow:hidden!important;scrollbar-gutter:stable\}/.test(css))
|
||
ok('高亮层跟随 textarea 横向滚动',
|
||
/hl\.scrollTop = textIn\.scrollTop; hl\.scrollLeft = textIn\.scrollLeft/.test(src))
|
||
ok('右侧变量名与详情变量用主色呼应',
|
||
/\.iwb-plp-vname\{color:var\(--iwb-plp-accent/.test(css) && /\.iwb-plp-dvar\{color:var\(--iwb-plp-accent/.test(css))
|
||
|
||
// ---- 5) v1.11.0 封面文件治理(P1)----
|
||
// 目标:① 保存/上传时把内联 base64 落成文件;② 孤儿封面能被回收;③ 回收不能误删。
|
||
// 纯文本断言跑不了真实 FS ⇒ 这里用「函数抽出来 + 假 fileApi」跑行为。
|
||
ok('封面命名统一走 coverFileName(哈希只在它内部实现一次)',
|
||
/function coverFileName \(dataUrl\)/.test(src) && (src.match(/var h = 5381/g) || []).length === 1 && (src.match(/coverFileName\(/g) || []).length >= 6)
|
||
ok('保存路径先落文件(ensureCoverFile 包住 doSave)',
|
||
/function doSave \(\)[\s\S]{0,600}ensureCoverFile\(c\.cover\)/.test(src))
|
||
ok('上传封面即落文件(不再把 dataURL 写回 draft.cover)',
|
||
/api\.putCover\([\s\S]{0,400}ensureCoverFile\(r\.ref\)/.test(src))
|
||
ok('删词条后安排一次回收', /已删除'[\s\S]{0,80}scheduleCoverGc\(/.test(src))
|
||
ok('启动加载后安排一次回收(等 convertInlineCovers 写完)',
|
||
/convertInlineCovers\(\)[\s\S]{0,120}scheduleCoverGc\(4000\)/.test(src))
|
||
ok('自动回收有「数据来源」闸门(不是插件 JSON 读来的就不删)',
|
||
/!o\.manual && !dataFromPlugin\) return Promise\.resolve\(\{ removed: 0, bytes: 0, skipped: 'no-source' \}\)/.test(src))
|
||
ok('keep 为空时不删(空集合 = 全删的形状)',
|
||
/if \(!kept && !o\.manual\) return Promise\.resolve\(\{ removed: 0, bytes: 0, skipped: 'empty' \}\)/.test(src))
|
||
ok('只删图片扩展名文件', src.includes('/\\.(jpg|jpeg|png|webp)$/i.test(name)'))
|
||
ok('设置页有显式清理入口 + 现读一次数据',
|
||
/text: '清理未引用封面'/.test(src) && /plpGcCoverFiles\(FA, plpCoverNamesOf\(state\.prompts\), \{ manual: true \}\)/.test(src))
|
||
ok('pending(刚 put 未被引用)不会被回收',
|
||
/Object\.keys\(pendingCoverNames\)\.forEach\(function \(n\) \{ names\.push\(n\) \}\)/.test(src))
|
||
|
||
// 行为:抽出 plpGcCoverFiles 本体,用假 fileApi 验证「只删未被引用的图片文件」
|
||
const gcMatch = src.match(/ function plpGcCoverFiles \(FA, keepNames, opts\) \{[\s\S]*?\n \}\n/)
|
||
ok('从 src 抽出 plpGcCoverFiles', !!gcMatch)
|
||
if (gcMatch) {
|
||
const removed = []
|
||
const fakeFA = {
|
||
list: async () => [
|
||
{ name: 'cused1.jpg', size: 100 },
|
||
{ name: 'corphan1.jpg', size: 200 },
|
||
{ name: 'cshared.jpg', size: 300 },
|
||
{ name: 'notes.txt', size: 1 },
|
||
],
|
||
remove: async (name) => { removed.push(name); return { ok: true } },
|
||
}
|
||
const gcSandbox = { Promise, JSON, String, Number, Array, Object, RegExp, console }
|
||
vm.createContext(gcSandbox)
|
||
vm.runInContext('var PLP_GC_BUSY = false\n' + gcMatch[0] + '\nthis.gc = plpGcCoverFiles', gcSandbox)
|
||
const r1 = await gcSandbox.gc(fakeFA, ['cused1.jpg'], {})
|
||
ok('回收:只删未引用图片(保留引用项与非图片文件)',
|
||
JSON.stringify(removed) === '["corphan1.jpg","cshared.jpg"]')
|
||
ok('回收:返回删除数与字节数', r1.removed === 2 && r1.bytes === 500)
|
||
const r2 = await gcSandbox.gc(fakeFA, [], {})
|
||
ok('回收:keep 为空且非人工 ⇒ 不删(skipped=empty)', r2.skipped === 'empty' && r2.removed === 0)
|
||
const r3 = await gcSandbox.gc(null, [], {})
|
||
ok('回收:无文件能力 ⇒ skipped=no-fs', r3.skipped === 'no-fs')
|
||
}
|
||
|
||
console.log(failed ? ('✗ ' + failed + ' 项失败') : '✓ 全部通过')
|
||
if (failed) process.exitCode = 1
|