'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 || '', grantDir: async () => true, 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]) }) test('relocate:重新定位只改登记路径', async () => { const e = makeEnv({ pointer: '/Users/x/A', files: { '/Users/x/A': 'ok', '/Users/x/New': 'ok' }, pickDir: '/Users/x/New' }) const R = e.R.create({ storage: e.g.localStorage }) let d = R.add(R.read(), '/Users/x/Old', '旧位置') R.write(d.doc) const oldRow = e.ctl.list().find((x) => x.path === '/Users/x/Old') const r = await e.ctl.relocate(oldRow.id) assert.equal(r.ok, true) const rows = e.ctl.list() assert.equal(rows.some((x) => x.path === '/Users/x/Old'), false) assert.equal(rows.some((x) => x.path === '/Users/x/New'), true) }) test('relocate:目标不是库 → 不修改登记', async () => { const e = makeEnv({ pointer: '/Users/x/A', files: { '/Users/x/A': 'ok', '/Users/x/Empty': 'nodata' }, pickDir: '/Users/x/Empty' }) const R = e.R.create({ storage: e.g.localStorage }) const d = R.add(R.read(), '/Users/x/Old', '旧位置'); R.write(d.doc) const row = e.ctl.list().find((x) => x.path === '/Users/x/Old') const r = await e.ctl.relocate(row.id) assert.equal(r.ok, false) assert.equal(r.code, 'no-data') assert.equal(e.ctl.list().some((x) => x.path === '/Users/x/Old'), true) }) test('relocate:目标已被另一条登记 → 合并后只留一条', async () => { const e = makeEnv({ pointer: '/Users/x/A', files: { '/Users/x/A': 'ok', '/Users/x/B': 'ok' }, pickDir: '/Users/x/B' }) const R = e.R.create({ storage: e.g.localStorage }) let d = R.add(R.read(), '/Users/x/A', 'A'); R.write(d.doc) d = R.add(R.read(), '/Users/x/Old', '旧位置'); R.write(d.doc) const row = e.ctl.list().find((x) => x.path === '/Users/x/Old') const r = await e.ctl.relocate(row.id) assert.equal(r.ok, true) const rows = e.ctl.list() assert.equal(rows.filter((x) => x.path === '/Users/x/B').length, 1) }) test('reauthorize:授权后探测通过返回 ok', async () => { const e = makeEnv({ pointer: '/Users/x/A', files: { '/Users/x/A': 'ok' } }) const rows = e.ctl.list() const r = await e.ctl.reauthorize(rows[0].id) assert.equal(r.ok, true) })