'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', 'board-transfer.js'), 'utf8') function load() { const g = {} new Function('window', 'globalThis', SRC)(g, g) return g.IWBStorageFile.boardTransfer } test('boardFileName:时间戳 + 非法字符替换', () => { const T = load() assert.equal(T.boardFileName('爱奇艺 KV', new Date(2026, 11, 6, 14, 30)), '爱奇艺 KV-20261206-1430.iwb.json') assert.equal(T.boardFileName('a/b:c', new Date(2026, 0, 2, 3, 4)), 'a_b_c-20260102-0304.iwb.json') }) test('uniqueFileName:在 .iwb.json 前加序号', () => { const T = load() const existing = ['a-20261206-1430.iwb.json', 'a-20261206-1430-2.iwb.json'] assert.equal(T.uniqueFileName('a-20261206-1430.iwb.json', existing), 'a-20261206-1430-3.iwb.json') assert.equal(T.uniqueFileName('b.iwb.json', []), 'b.iwb.json') }) test('validateImport:app 标记 / 节点上限 / 空节点', () => { const T = load() assert.equal(T.validateImport(null).ok, false) assert.equal(T.validateImport({ app: 'x', nodes: [] }).ok, false) assert.equal(T.validateImport({ app: 'image-workbench', nodes: [] }).ok, false) assert.equal(T.validateImport({ app: 'image-workbench', nodes: [{ id: 'a', type: 'prompt' }] }).ok, true) const many = new Array(T.LIMITS.nodes + 1).fill({ id: 'a', type: 'prompt' }) assert.equal(T.validateImport({ app: 'image-workbench', nodes: many }).ok, false) }) test('remapBoard:id 全换新,promptId 跟随,普通文本不动', () => { const T = load() let i = 0 const uid = (p) => 'new' + p + (++i) const src = { nodes: [ { id: 'n1', type: 'prompt', data: { text: 'n1' } }, { id: 'n2', type: 'gen', data: { promptId: 'n1' } }, ], edges: [{ id: 'e1', from: 'n1', to: 'n2', batchSrcId: 'n1' }], groups: [{ id: 'g1', memberIds: ['n1', 'n2'] }], } const r = T.remapBoard(src, uid) const ids = r.nodes.map((n) => n.id) assert.equal(new Set(ids).size, 2) assert.notEqual(ids[0], 'n1') assert.equal(r.nodes[0].data.text, 'n1') // 普通文本不被改写 assert.equal(r.nodes[1].data.promptId, ids[0]) // promptId 跟随 assert.equal(r.edges[0].from, ids[0]) assert.equal(r.edges[0].to, ids[1]) assert.equal(r.edges[0].batchSrcId, ids[0]) assert.deepEqual(r.groups[0].memberIds, ids) }) test('remapBoard:过滤悬空连线与单成员分组', () => { const T = load() let i = 0 const uid = (p) => 'new' + p + (++i) const r = T.remapBoard({ nodes: [{ id: 'n1', type: 'prompt' }], edges: [{ id: 'e1', from: 'n1', to: 'missing' }], groups: [{ id: 'g1', memberIds: ['n1'] }], }, uid) assert.equal(r.edges.length, 0) assert.equal(r.groups.length, 0) }) test('stripResults:gen/out 只去 result', () => { const T = load() const b = { nodes: [ { id: 'g', type: 'gen', data: { model: 'x', result: [{ ok: true }] } }, { id: 'o', type: 'out', data: { result: [1] } }, { id: 'p', type: 'prompt', data: { text: 'keep' } }, ] } const r = T.stripResults(b) assert.equal(r.nodes[0].data.result, undefined) assert.equal(r.nodes[0].data.model, 'x') assert.equal(r.nodes[1].data.result, undefined) assert.equal(r.nodes[2].data.text, 'keep') }) test('collectImageRefs / resolveBoardImages:解析失败计数', async () => { const T = load() const b = { nodes: [ { id: 'n1', type: 'ref', data: { data: 'data:image/png;base64,AAAA' } }, { id: 'n2', type: 'ref', data: { localRef: { __blob: 'imgblob_x' } } }, { id: 'n3', type: 'ref', data: { data: { __file: '/abs/a.png' } } }, ] } const refs = T.collectImageRefs(b) assert.equal(refs.length, 3) const r = await T.resolveBoardImages(b, async (ref) => { if (typeof ref === 'object' && ref.__blob === 'imgblob_x') return 'data:image/png;base64,BBBB' return null }) assert.equal(r.missing, 1) // dataURL 直接可用(不计入),__blob 成功,__file 失败 assert.equal(r.board.nodes[1].data.localRef, 'data:image/png;base64,BBBB') })