Files
image-workbench-desktop/tests/vault-registry-test.js
T
qiqi 177e3fcfe3
Verify (Gitea) / gate (build · unit · facts) (push) Failing after 1m3s
feat(vault): 多数据库面板 + 启动安全(v3.26.263)
- 左下导航「数据库」面板:登记/切换/新建/打开已有/重命名/移除(只删登记)
- 启动安全:库缺失/无 data.json/损坏不再自动建空库,走「库不可用」页
- 登记表存 WebView 原生 localStorage;切换前 flush、失败取消、reload 未发生回滚指针
- 新增 src/vault-registry.js / vault-actions.js / ui-vault-panel.js / ui-boot-recovery.js
- 单测:registry 6、actions 7、启动守卫 4

棘轮上调说明(B3):app.js 9,542→9,555(+13,2026-12-06,v3.26.263 多库面板装配:boot 恢复页分派 + vaultCtl + NavRail 传参,逻辑在 src);Workbench 2,716→2,726(+10,2026-12-06,需读 Workbench 内 showToast)
2026-12-06 13:11:40 +08:00

91 lines
3.5 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.
'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', 'vault-registry.js'), 'utf8')
function load(platform) {
const store = new Map()
const g = {
navigator: { platform: platform || 'MacIntel' },
localStorage: {
getItem: (k) => (store.has(k) ? store.get(k) : null),
setItem: (k, v) => store.set(k, String(v)),
removeItem: (k) => store.delete(k),
},
}
new Function('window', 'globalThis', SRC)(g, g)
return { g, R: g.IWBVaultRegistry, store }
}
test('normalizePath:尾斜杠/反斜杠/空串', () => {
const { R } = load()
assert.equal(R.normalizePath('/a/b/'), '/a/b')
assert.equal(R.normalizePath('D:\\sync\\IWB'), 'D:/sync/IWB')
assert.equal(R.normalizePath(''), '')
assert.equal(R.normalizePath('//server/share/x'), '//server/share/x')
})
test('pathKey:mac 大小写不敏感', () => {
const { R } = load('MacIntel')
assert.equal(R.pathKey('/Volumes/A/B'), R.pathKey('/volumes/a/b'))
})
test('baseName 与 uniqueName', () => {
const { R } = load()
assert.equal(R.baseName('/Users/x/ImageWorkbench'), 'ImageWorkbench')
assert.equal(R.uniqueName([], '库'), '库')
assert.equal(R.uniqueName([{ name: '库' }], '库'), '库 (2)')
assert.equal(R.uniqueName([{ name: '库' }, { name: '库 (2)' }], '库'), '库 (3)')
})
test('isSyncPath:verysync / 云盘', () => {
const { R } = load()
assert.equal(R.isSyncPath('/Volumes/sync/verysync/设计同步/ImageWorkbench'), true)
assert.equal(R.isSyncPath('/Users/x/Library/Mobile Documents/iCloud Drive/IWB'), true)
assert.equal(R.isSyncPath('/Users/x/Documents/IWB-local'), false)
})
test('add 去重 + setCurrent 更新 lastUsedAt + remove/rename', () => {
const { R } = load()
const reg = R.create({ storage: globalThis.localStorage, now: () => 1000 })
// 用假 store(load 里的 localStorage 才是我们的)
const rr = R.create({ storage: null })
let doc = R.create({ storage: null }).empty()
const a1 = R.create({ storage: null }).add(doc, '/Users/x/A', 'A库')
doc = a1.doc
assert.equal(a1.added, true)
const a2 = R.create({ storage: null }).add(doc, '/Users/x/A/', '别的名字')
assert.equal(a2.added, false)
assert.equal(a2.item.name, 'A库')
doc = a2.doc
doc = R.create({ storage: null }).setCurrent(doc, '/Users/x/A')
assert.equal(doc.current, '/Users/x/A')
assert.equal(doc.items[0].lastUsedAt > 0, true)
const rn = R.create({ storage: null }).rename(doc, doc.items[0].id, '新名')
assert.equal(rn.ok, true)
assert.equal(rn.doc.items[0].name, '新名')
const rm = R.create({ storage: null }).remove(rn.doc, doc.items[0].id)
assert.equal(rm.removed, true)
assert.equal(rm.doc.items.length, 0)
})
test('read:损坏的登记表 → 空表;写入后可读回', () => {
const { R, store } = load()
store.set('iwb_vaults_v1', '{not json')
const reg = R.create({ storage: globalThis.__store__ || null })
// 使用真实假 store
const g = load()
g.store.set('iwb_vaults_v1', '{not json')
const reg2 = g.R.create({ storage: g.g.localStorage, now: () => 5 })
assert.deepEqual(reg2.read().items, [])
const added = g.R.create({ storage: g.g.localStorage, now: () => 5 }).add(reg2.read(), '/Users/x/A', 'A')
g.R.create({ storage: g.g.localStorage, now: () => 5 }).write(added.doc)
const back = g.R.create({ storage: g.g.localStorage, now: () => 5 }).read()
assert.equal(back.items.length, 1)
assert.equal(back.items[0].name, 'A')
})