'use strict' const test = require('node:test') const assert = require('node:assert') const fs = require('node:fs') const path = require('node:path') const SRC_DIR = path.join(__dirname, '..', 'src') const loadSrc = (g, file) => new Function('window', 'globalThis', fs.readFileSync(path.join(SRC_DIR, file), 'utf8'))(g, g) function makeEnv() { const g = { console, setTimeout, btoa: (s) => Buffer.from(s, 'binary').toString('base64'), atob: (s) => Buffer.from(s, 'base64').toString('binary') } ;['vault-browse.js', 'board-transfer.js', 'vault-browse-actions.js'].forEach((f) => loadSrc(g, f)) const vault = '/fake/vault' const files = { [vault + '/data.json']: JSON.stringify({ keys: {}, assets: {} }), [vault + '/canvases/a.json']: JSON.stringify({ id: 'a', name: 'A', nodes: [{ id: 'n1', type: 'prompt', data: { text: 'hi' } }], edges: [], groups: [] }), [vault + '/canvases/b.json']: JSON.stringify({ id: 'b', name: 'B', nodes: [{ id: 'n2', type: 'prompt', data: { text: 'yo' } }], edges: [], groups: [] }), } const tauri = { fs: { readTextFile: async (p) => { if (!(p in files)) throw new Error('ENOENT'); return files[p] }, readDir: async (p) => Object.keys(files).filter((k) => k.indexOf(p + '/') === 0 && k.slice(p.length + 1).indexOf('/') < 0).map((k) => ({ name: k.split('/').pop(), isDirectory: false })), readFile: async () => new Uint8Array([1, 2, 3]), }, core: { invoke: async (cmd, args) => { if (cmd === 'read_text_many') return (args.rels || []).map((rel) => ({ rel, text: files[vault + '/' + rel] })) if (cmd === 'stat_many') return (args.rels || []).map((rel) => ({ rel, exists: true, size: 1, mtime: 1000 })) if (cmd === 'read_files_b64') return [] return true }, }, } const calls = { create: [], persist: 0 } let n = 0 const actions = g.IWBStorageFile.vaultBrowseActions.create({ tauri, grantDir: async () => true, writeImage: async () => ({ __blob: 'imgblob_x' }), createBoard: (b, opts) => { calls.create.push({ name: b.name, opts }); return { id: 'ws' + (++n), list: [{ id: 'ws' + n }] } }, commitList: async () => { calls.persist++ }, flush: async () => {}, uid: (p) => p + (++n), currentBoardNames: () => [], }) return { g, actions, calls, vault } } test('copyMany:逐个 silent 建板 + 只 persist 一次', async () => { const { actions, calls, vault } = makeEnv() const r = await actions.copyMany(vault, [{ id: 'a', name: 'A' }, { id: 'b', name: 'B' }]) assert.equal(r.length, 2) assert.equal(r.every((x) => x.ok), true) assert.equal(calls.create.length, 2) assert.equal(calls.create[0].opts.silent, true) assert.equal(calls.create[0].opts.deferState, true) assert.equal(calls.create[1].opts.silent, true) assert.equal(calls.create[1].opts.deferState, true) assert.equal(calls.persist, 1) }) test('copyOne(单个):走切换路径(silent=false)', async () => { const { actions, calls, vault } = makeEnv() const r = await actions.copyOne(vault, 'a', 'A') assert.equal(r.ok, true) assert.equal(calls.create.length, 1) assert.equal(calls.create[0].opts.silent, false) })