Files
image-workbench-desktop/tests/board-transfer-test.js
T
qiqi f7888a5c16
Verify (Gitea) / gate (build · unit · facts) (push) Failing after 42s
feat(board): 画板导入/导出入口 + 迁移/重映射/图片转资产(v3.26.266)
- src/board-transfer.js:导出命名(.iwb.json 双后缀、-2/-3)、导入校验、id 重映射(含 promptId 等引用)、生成结果剥离、图片引用解析/内联转资产
- src/board-transfer-actions.js:导出/导入到当前/导入为新画板的动作层(依赖注入)
- 「更多」菜单三项(仅画布视图);导入为新画板自动切换、名称去重
- 单测 7 条;棘轮上调:app.js 9,564→9,598、Workbench 2,735→2,769(2026-12-06,装配依赖注入,逻辑在 src)
2026-12-06 17:01:41 +08:00

105 lines
4.0 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', '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')
})