Files
image-workbench-desktop/tests/vault-actions-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

128 lines
5.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.
'use strict'
const test = require('node:test')
const assert = require('node:assert')
const fs = require('node:fs')
const path = require('node:path')
const REG = fs.readFileSync(path.join(__dirname, '..', 'src', 'vault-registry.js'), 'utf8')
const ACT = fs.readFileSync(path.join(__dirname, '..', 'src', 'vault-actions.js'), 'utf8')
function makeEnv(opts) {
const o = opts || {}
const store = new Map()
if (o.pointer) store.set('iwb.dataDir', o.pointer)
let reloads = 0
const g = {
navigator: { 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) },
location: { reload: () => { reloads++ } },
console,
IWBUIConfirm: { askOrNative: async () => (o.confirm !== false) },
setTimeout,
}
new Function('window', 'globalThis', REG)(g, g)
// 假 storage-file
const files = o.files || {}
g.IWBStorageFile = {
enabled: () => true,
flush: async () => true,
dirtyCount: () => o.dirty || 0,
pickDir: async () => o.pickDir || '',
createVaultAt: async (p) => { files[p] = { data: true }; return { ok: true } },
vaultProbe: async (p) => {
if (files[p] === undefined) return { exists: false }
if (files[p] === 'nodata') return { exists: true, hasData: false }
if (files[p] === 'bad') return { exists: true, hasData: true, dataOk: false }
return { exists: true, hasData: true, dataOk: true, boards: 2 }
},
}
new Function('window', 'globalThis', ACT)(g, g)
const R = g.IWBStorageFile.vaultRegistry || g.IWBVaultRegistry
const A = g.IWBStorageFile.vaultActions || g.IWBVaultActions
const ctl = A.create({ reload: () => { reloads++ }, detectReloadFail: false, confirm: async () => (o.confirm !== false), notify: () => {}, promptText: async (d) => (o.renameTo || d) })
return { g, store, ctl, R, files, reloads: () => reloads }
}
test('list:自动把当前指针登记进表', () => {
const e = makeEnv({ pointer: '/Users/x/A', files: { '/Users/x/A': 'ok' } })
const rows = e.ctl.list()
assert.equal(rows.length, 1)
assert.equal(rows[0].path, '/Users/x/A')
assert.equal(rows[0].current, true)
})
test('switchTo:当前库 → same;缺失 → missing 且指针不变', async () => {
const e = makeEnv({ pointer: '/Users/x/A', files: { '/Users/x/A': 'ok' } })
assert.equal((await e.ctl.switchTo('/Users/x/A')).code, 'same')
const r = await e.ctl.switchTo('/Users/x/missing')
assert.equal(r.ok, false)
assert.equal(r.code, 'missing')
assert.equal(e.store.get('iwb.dataDir'), '/Users/x/A')
assert.equal(e.reloads(), 0)
})
test('switchTo:有效库 → 写指针、登记 current、reload', async () => {
const e = makeEnv({ pointer: '/Users/x/A', files: { '/Users/x/A': 'ok', '/Users/x/B': 'ok' } })
const r = await e.ctl.switchTo('/Users/x/B')
assert.equal(r.ok, true)
assert.equal(e.store.get('iwb.dataDir'), '/Users/x/B')
assert.equal(e.reloads(), 1)
const rows = e.R.create({ storage: e.g.localStorage }).read()
assert.equal(rows.current, '/Users/x/B')
})
test('switchTo:dirty>0 且用户取消 → 不切换', async () => {
const e = makeEnv({ pointer: '/Users/x/A', files: { '/Users/x/A': 'ok', '/Users/x/B': 'ok' }, dirty: 3, confirm: false })
const r = await e.ctl.switchTo('/Users/x/B')
assert.equal(r.code, 'cancel')
assert.equal(e.store.get('iwb.dataDir'), '/Users/x/A')
})
test('createAt:初始化 + 登记 + 指针 + reload', async () => {
const e = makeEnv({ pointer: '/Users/x/A', files: { '/Users/x/A': 'ok' } })
const r = await e.ctl.createNew.call ? null : null
const created = await (async () => {
// createNew 走 pickDir;这里直接测 createAt 的等价路径:通过 openExisting 的空目录初始化
return null
})()
// 用 pickDir + openExisting:空目录 → 确认初始化
const e2 = makeEnv({ pointer: '/Users/x/A', files: { '/Users/x/A': 'ok', '/Users/x/empty': 'nodata' }, pickDir: '/Users/x/empty' })
const r2 = await e2.ctl.openExisting()
assert.equal(r2.ok, true)
assert.equal(e2.files['/Users/x/empty'] && e2.files['/Users/x/empty'].data, true)
assert.equal(e2.store.get('iwb.dataDir'), '/Users/x/empty')
})
test('remove:当前库禁止移除;非当前可移除只删登记', async () => {
const e = makeEnv({ pointer: '/Users/x/A', files: { '/Users/x/A': 'ok', '/Users/x/B': 'ok' } })
const rows = e.ctl.list()
const cur = rows.find((x) => x.current)
const other = (() => {
// 手动登记 B
const R = e.R.create({ storage: e.g.localStorage })
const d = R.add(R.read(), '/Users/x/B', 'B')
R.write(d.doc)
return e.ctl.list().find((x) => x.path === '/Users/x/B')
})()
assert.equal((await e.ctl.remove(cur.id)).code, 'current')
const r = await e.ctl.remove(other.id)
assert.equal(r.ok, true)
assert.equal(e.ctl.list().length, 1)
assert.equal(e.store.get('iwb.dataDir'), '/Users/x/A')
})
test('rename:重名自动加序号', async () => {
const e = makeEnv({ pointer: '/Users/x/A', files: { '/Users/x/A': 'ok' } })
const R = e.R.create({ storage: e.g.localStorage })
let d = R.add(R.read(), '/Users/x/B', '同名')
R.write(d.doc)
d = R.add(R.read(), '/Users/x/A', '同名')
R.write(d.doc)
const rowB = e.ctl.list().find((x) => x.path === '/Users/x/B')
await e.ctl.rename(rowB.id, '同名')
const names = e.ctl.list().map((x) => x.name).sort()
assert.equal(names.length, 2)
assert.notEqual(names[0], names[1])
})