P0(宿主): - 备份 ZIP 新增 plugin-files/<插件id>/<文件名> 通道,恢复时写回沙箱; 此前只备份 JSON ⇒ 换机后「提示词都在、封面全是灰块」,而状态行写着「已恢复」 - IWBStorageFile.pluginFiles 增 readBytes / listAll(不 mkdir、只读字节) - IWBPluginData 增 listFiles / readFile / restoreFiles:清单一次给出、字节逐条读写 (插件文件单插件上限 64MB,攒数组会 OOM) - 校验和口径统一:images / imagesData / pluginFiles / pluginFilesData 全部排除; JSON 兜底导出按自身内容重算 checksum,避免「换个打包形态就报校验不一致」 - 备份二次确认的体积/张数把插件文件计入;恢复摘要显示「含 N 个插件文件(封面等)」 - tests/plugin-data-test:新增插件文件清单/读字节/写回 三条行为断言 P1(插件 promptlib 1.11.0,随包同步): - 封面保存/上传即落成文件(JSON 只留文件名)、孤儿封面可回收 (启动后一次 + 删词条后一次 + 设置页手动入口;空集合/非插件数据来源一律不删) - 四处内联 djb2 哈希合并为 coverFileName,同图不再存两份 - 样式散值归零(4px→6px、margin 13px→12px),插件尺度棘轮基线保持 0
125 lines
6.7 KiB
JavaScript
125 lines
6.7 KiB
JavaScript
'use strict'
|
||
// 插件数据层行为测试(src/plugin-data.js,v3.25.42 / M2)
|
||
//
|
||
// 为什么要有:ctx.data 是"插件唯一能放心存业务数据"的地方,
|
||
// 一旦串行队列写错(并发 patch 互相覆盖)或上限判定失效(静默截断),用户数据会悄悄丢。
|
||
// 这里用注入 backend(内存 Map)跑纯逻辑,不依赖 Tauri / 浏览器。
|
||
const assert = require('assert')
|
||
const fs = require('fs')
|
||
const path = require('path')
|
||
const vm = require('vm')
|
||
|
||
const root = path.resolve(__dirname, '..')
|
||
const lsMap = new Map()
|
||
// 存储走 IWBStorageLocal 门面(src 内禁止直调 localStorage)
|
||
const store = {
|
||
rawGet: (k, fb) => (lsMap.has(k) ? lsMap.get(k) : fb),
|
||
rawSet: (k, v) => { lsMap.set(k, String(v)); return true },
|
||
remove: (k) => { lsMap.delete(k); return true },
|
||
keys: () => Array.from(lsMap.keys()),
|
||
}
|
||
const sandbox = { TextEncoder, IWBStorageLocal: { getStore: () => store } }
|
||
vm.runInNewContext(fs.readFileSync(path.join(root, 'src', 'plugin-data.js'), 'utf8'), sandbox)
|
||
const PD = sandbox.IWBPluginData
|
||
assert.ok(PD, 'IWBPluginData 未导出')
|
||
// vm 里造出来的对象原型不同,deepStrictEqual 会误报 ⇒ 统一用 JSON 比较
|
||
const eq = (actual, expected, msg) => assert.strictEqual(JSON.stringify(actual), JSON.stringify(expected), msg)
|
||
|
||
function memBackend () {
|
||
const store = new Map()
|
||
return {
|
||
store,
|
||
read: async (id) => (store.has(id) ? store.get(id) : null),
|
||
write: async (id, text) => { store.set(id, text); return true },
|
||
remove: async (id) => { store.delete(id); return true },
|
||
list: async () => Array.from(store.keys()).map((id) => ({ id })),
|
||
}
|
||
}
|
||
|
||
;(async () => {
|
||
const be = memBackend()
|
||
const d = PD.create('test-a', { backend: be, maxBytes: 2048, schemaVersion: 3 }) // 下限 1KB,这里取 2KB 方便测上限
|
||
|
||
eq(await d.read(), {}, '空数据应返回 {}')
|
||
assert.strictEqual(d.schemaVersion, 3)
|
||
await d.write({ x: 1 })
|
||
eq(await d.read(), { x: 1 })
|
||
|
||
await d.patch({ y: 2 })
|
||
eq(await d.read(), { x: 1, y: 2 })
|
||
await d.patch((o) => Object.assign({}, o, { x: 9 }))
|
||
eq(await d.read(), { x: 9, y: 2 })
|
||
|
||
// 并发 20 次 patch:串行队列必须一个都不丢
|
||
await Promise.all(Array.from({ length: 20 }, (_, i) => d.patch((o) => Object.assign({}, o, { ['k' + i]: i }))))
|
||
const all = await d.read()
|
||
for (let i = 0; i < 20; i++) assert.strictEqual(all['k' + i], i, '并发 patch 丢了 k' + i)
|
||
assert.strictEqual(all.x, 9)
|
||
|
||
// 超上限:抛错、且不破坏上一次成功写入
|
||
let err = null
|
||
try { await d.write({ big: 'x'.repeat(4000) }) } catch (e) { err = e }
|
||
assert.ok(err && err.code === 'plugin-data-too-large', '超上限应抛 plugin-data-too-large')
|
||
eq(await d.read(), all, '写失败不得改动已有数据')
|
||
|
||
assert.ok((await d.size()) > 0, 'size 应大于 0')
|
||
await d.remove()
|
||
eq(await d.read(), {}, 'remove 后应回到空')
|
||
|
||
// stripLegacy:旧 iwb_plugins[id].data 搬到暂存,首次 read 再落盘
|
||
lsMap.set('iwb_plugins', JSON.stringify({ legacy1: { enabled: true, data: { a: 1, b: 'x' } }, plain: { enabled: false } }))
|
||
const moved = PD.stripLegacy()
|
||
assert.strictEqual(moved, 1, '应搬走 1 个插件的旧数据')
|
||
const stateAfter = JSON.parse(lsMap.get('iwb_plugins'))
|
||
assert.strictEqual(stateAfter.legacy1.data, undefined, '旧 data 必须从宿主状态移除(避免双份)')
|
||
assert.ok(stateAfter.legacy1.migratedAt > 0, '应记录 migratedAt')
|
||
assert.strictEqual(stateAfter.plain.enabled, false, '其它字段不得被改动')
|
||
|
||
const legacyBackend = memBackend()
|
||
const dLegacy = PD.create('legacy1', { backend: legacyBackend })
|
||
eq(await dLegacy.read(), { a: 1, b: 'x' }, '首次读取应拿到迁移过来的旧数据')
|
||
assert.ok(legacyBackend.store.has('legacy1'), '迁移应落到新后端')
|
||
const legacyLeft = JSON.parse(lsMap.get('iwb_plugin_data_legacy') || '{}')
|
||
assert.strictEqual(legacyLeft.legacy1, undefined, '迁移完成后应清掉暂存')
|
||
|
||
// 备份:collectAll / restoreAll(无注入 backend ⇒ 走内存兜底)
|
||
const dMem = PD.create('backup-a')
|
||
await dMem.write({ keep: [1, 2, 3] })
|
||
const collected = await PD.collectAll()
|
||
const hit = collected.find((x) => x.id === 'backup-a')
|
||
assert.ok(hit && Array.isArray(hit.data.keep), 'collectAll 应包含插件数据')
|
||
await PD.restoreAll([{ id: 'restored-x', data: { ok: true } }])
|
||
eq(await PD.create('restored-x').read(), { ok: true }, 'restoreAll 应写回数据')
|
||
|
||
// ---- 插件文件(v3.26.59 / P0):备份必须带上 plugin-data/<id>/files/ 的字节 ----
|
||
// 动机:只备份 JSON 时,提示词封面这类图片文件换机后全丢,用户看到「已恢复」却是灰块。
|
||
// 这里用内存假 FS 跑行为断言(不依赖 Tauri),覆盖「清单 / 读字节 / 写回」三条路。
|
||
const fakeFiles = { promptlib: { 'cabc.jpg': Buffer.from('JPEGDATA', 'utf8') }, 'other-plugin': { 'cxyz.webp': Buffer.from([1, 2, 3]) } }
|
||
const putLog = []
|
||
sandbox.IWBStorageFile = {
|
||
pluginFiles: {
|
||
listAll: async () => Object.keys(fakeFiles).map((id) => ({ id, files: Object.keys(fakeFiles[id]).map((name) => ({ name, size: fakeFiles[id][name].length })) })),
|
||
readBytes: async (id, name) => (fakeFiles[id] && fakeFiles[id][name]) || null,
|
||
put: async (id, name, data) => { putLog.push({ id, name, data }); return { ok: true } },
|
||
},
|
||
}
|
||
const files = await PD.listFiles()
|
||
assert.strictEqual(files.length, 2, 'listFiles 应枚举所有插件的插件文件')
|
||
assert.strictEqual(JSON.stringify(files.map((f) => f.id + '/' + f.name).sort()), '["other-plugin/cxyz.webp","promptlib/cabc.jpg"]', 'listFiles 的 id/name 必须成对正确')
|
||
assert.strictEqual(files.find((f) => f.name === 'cabc.jpg').size, 8, 'listFiles 应带 size(体积预估用)')
|
||
const bytes = await PD.readFile('promptlib', 'cabc.jpg')
|
||
assert.ok(bytes && Buffer.from(bytes).toString('utf8') === 'JPEGDATA', 'readFile 应返回原始字节')
|
||
assert.strictEqual(await PD.readFile('promptlib', 'missing.jpg'), null, '不存在的文件返回 null(不抛)')
|
||
|
||
const n = await PD.restoreFiles([
|
||
{ id: 'promptlib', name: 'cabc.jpg', data: 'data:image/jpeg;base64,QUJD' },
|
||
{ id: '', name: 'bad.jpg', data: 'x' },
|
||
{ id: 'promptlib', name: 'nofile.jpg' },
|
||
])
|
||
assert.strictEqual(n, 1, 'restoreFiles 只写合法条目(缺 id/name/data 的跳过)')
|
||
assert.strictEqual(putLog.length, 1, 'restoreFiles 不应多写')
|
||
assert.strictEqual(putLog[0].id, 'promptlib', 'restoreFiles 应带插件 id')
|
||
assert.ok(putLog[0].data.indexOf('data:image/jpeg;base64,') === 0, 'restoreFiles 应把 dataURL 交给 pluginFiles.put')
|
||
|
||
console.log('plugin-data checks passed(串行队列 / 上限 / 迁移 / 备份 / 插件文件)')
|
||
})().catch((e) => { console.error(e); process.exit(1) }) |