Files
image-workbench-desktop/tests/snapshot-store-test.js
T
qiqi 5987e96ecc
Verify (Gitea) / gate (build · unit · facts) (push) Failing after 2m7s
feat(storage): 本机快照 + data.json 多代备份 + 启动只读(v3.26.270)
- src/snapshot-store.js:内容寻址快照(画板 30 份/7 天、data.json 10 份),失败降级
- flush 写前/删除前拍快照;data.json 写前轮转
- loadInto 不再用 data.json 残留副本重写画板(只清残留键 + 强制重写一次 data.json);recoverFromWebStorage 仅恢复缺失/空文件
- data.json 损坏 → initFail bad-data;恢复页列备份并可一键恢复(损坏文件另存 .corrupt-*)
- 「更多 → 画板历史…」:列表/恢复/已删除恢复为新画板(src/ui-board-history.js)
- 单测:snapshot-store 5、启动守卫 8;棘轮:app 9,637→9,693、Workbench 2,807→2,862(装配)
2026-12-06 22:19:31 +08:00

102 lines
4.1 KiB
JavaScript

'use strict'
const test = require('node:test')
const assert = require('node:assert')
const fs = require('node:fs')
const path = require('node:path')
const SRC = fs.readFileSync(path.join(__dirname, '..', 'src', 'snapshot-store.js'), 'utf8')
function load() {
const g = {}
new Function('window', 'globalThis', SRC)(g, g)
return g.IWBStorageFile.snapshotStore
}
function memFs(initial) {
const files = new Map(Object.entries(initial || {}))
const dirs = new Set()
const fsApi = {
readTextFile: async (p) => { if (!files.has(p)) throw new Error('ENOENT'); return files.get(p) },
writeTextFile: async (p, t) => { files.set(p, String(t)) },
rename: async (a, b) => { if (!files.has(a)) throw new Error('ENOENT'); files.set(b, files.get(a)); files.delete(a) },
mkdir: async (p) => { dirs.add(p) },
remove: async (p) => {
if (files.delete(p)) return
for (const k of Array.from(files.keys())) if (k.indexOf(p + '/') === 0) files.delete(k)
},
exists: async (p) => files.has(p) || dirs.has(p),
readDir: async (p) => {
const out = []
const seen = new Set()
for (const k of files.keys()) {
if (k.indexOf(p + '/') !== 0) continue
const rest = k.slice(p.length + 1)
if (rest.indexOf('/') >= 0) { const name = rest.split('/')[0]; if (!seen.has(name)) { seen.add(name); out.push({ name, isDirectory: true }) } }
else out.push({ name: rest, isDirectory: false })
}
return out
},
stat: async (p) => {
if (files.has(p)) return { size: files.get(p).length, mtime: new Date(1000) }
throw new Error('ENOENT')
},
}
return { fsApi, files }
}
test('saveCanvas:同内容去重,只存一份文件、一条索引', async () => {
const S = load()
const { fsApi, files } = memFs()
const store = S.create({ fs: fsApi, join: (...p) => path.posix.join(...p), root: '/snap', now: () => 1000 })
const a = await store.saveCanvas('ws1', '{"nodes":[{"id":"a"}]}', { name: 'A' })
const b = await store.saveCanvas('ws1', '{"nodes":[{"id":"a"}]}', { name: 'A' })
assert.equal(a, b)
const idx = await store.listCanvas('ws1')
assert.equal(idx.length, 1)
const dataFiles = Array.from(files.keys()).filter((k) => k.indexOf('/canvas/ws1/') >= 0 && k.endsWith('.json') && !k.endsWith('index.json'))
assert.equal(dataFiles.length, 1)
})
test('saveCanvas:超过 30 份只保留最新 30', async () => {
const S = load()
const { fsApi } = memFs()
let t = 1000
const store = S.create({ fs: fsApi, join: (...p) => path.posix.join(...p), root: '/snap', now: () => (t += 1000) })
for (let i = 0; i < 35; i++) await store.saveCanvas('ws1', JSON.stringify({ nodes: [{ id: 'n' + i }] }))
const idx = await store.listCanvas('ws1')
assert.equal(idx.length, 30)
assert.equal(idx[0].nodes, 1)
})
test('readCanvas / listCanvas 取回内容', async () => {
const S = load()
const { fsApi } = memFs()
const store = S.create({ fs: fsApi, join: (...p) => path.posix.join(...p), root: '/snap', now: () => 1000 })
const sha = await store.saveCanvas('ws1', '{"nodes":[]}')
const text = await store.readCanvas('ws1', sha)
assert.equal(text, '{"nodes":[]}')
const list = await store.listCanvas('ws1')
assert.equal(list[0].sha, sha)
})
test('saveData:同内容不重复,超过 10 份轮转', async () => {
const S = load()
const { fsApi } = memFs()
let t = new Date(2026, 11, 6, 10, 0, 0).getTime()
const store = S.create({ fs: fsApi, join: (...p) => path.posix.join(...p), root: '/snap', now: () => (t += 60000) })
await store.saveData('{"v":1,"keys":{}}')
await store.saveData('{"v":1,"keys":{}}')
assert.equal((await store.listData()).length, 1)
for (let i = 0; i < 15; i++) await store.saveData('{"v":1,"keys":{"k' + i + '":"v"}}')
assert.equal((await store.listData()).length, 10)
})
test('未就绪(缺 fs/root)时所有方法安全返回', async () => {
const S = load()
const store = S.create({})
assert.equal(store.ready(), false)
assert.equal(await store.saveCanvas('ws1', '{"nodes":[]}'), null)
assert.deepEqual(await store.listCanvas('ws1'), [])
assert.equal(await store.saveData('{}'), null)
})