Files
image-workbench-desktop/tests/inherit-text-test.js
T

215 lines
12 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'
/* 提示词 / 文本节点的「上游继承文本」+ 文本节点透传参考图(2026-11-14)。
*
* 背景(用户报障):「样式节点连到提示词/文本节点,框里是空的,是不是没做?」
* 事实是**数据层一直通**(resolveNodeTaskValues 里 `own || t.text`,自己没写字就用上游文本),
* 缺的是两处:
* ① 节点体只读自己的 data.text ⇒ 上游文本看不见(inheritedTextOf 专门回答「上游给的是什么」);
* ② nodeRefsOf 的 textout 分支只认 llm / batch / combine 三种上游 ⇒ 样式节点直连文本节点时
* 图在分层节点里丢掉(生图那条走的是 resolveNodeTaskValues,所以只有分层节点暴露这个 bug)。
*
* 这里钉住的是「文本取值 + 图片透传」两件事的纯函数行为,跑的就是宿主运行时同一份 src/*.js。
*/
const assert = require('assert')
const fs = require('fs')
const path = require('path')
const vm = require('vm')
const ROOT = path.join(__dirname, '..')
let passed = 0
const ok = (name, fn) => { fn(); passed++; console.log(' ✓ ' + name) }
const sandbox = { console, window: {}, document: {}, React: {}, ReactDOM: {} }
vm.runInNewContext(fs.readFileSync(path.join(ROOT, 'src', 'canvas-text.js'), 'utf8'), sandbox)
const CT = sandbox.window.IWBCanvasText
const sortByPosition = (arr) => arr.slice().sort((a, b) => (Number(a.x) - Number(b.x)) || (Number(a.y) - Number(b.y)))
const compareCanvasImageOrder = (a, b) => (Number(a.x) - Number(b.x)) || (Number(a.y) - Number(b.y))
const dedupeTaskRefs = (refs) => {
const seen = new Set(), out = []
for (const r of (Array.isArray(refs) ? refs : [])) {
const k = r && (r.id || r.value || r.name)
if (r && k && !seen.has(k)) { seen.add(k); out.push(r) }
}
return out
}
const refArgsOf = (n) => {
const d = (n && n.data) || {}
if (d.localName) return [{ type: 'local', name: d.localName }]
if (typeof d.url === 'string' && d.url.trim()) return [{ type: 'url', value: d.url.trim() }]
return []
}
vm.runInNewContext(fs.readFileSync(path.join(ROOT, 'src', 'generation-task-graph.js'), 'utf8'), sandbox)
const TG = sandbox.window.IWBWTaskGraph.create({
sortByPosition,
compareCanvasImageOrder,
compareCanvasTextOrder: CT.compareCanvasTextOrder,
dedupeTaskRefs,
isBatchSrc: CT.isBatchSrc,
batchJoinedText: CT.batchJoinedText,
wDedupeTaskIds: (list) => list,
refArgsOf,
batchSrcText: CT.batchSrcText,
standardSizeOf: () => '1024x1024',
})
const STYLE_TEXT = 'X光扫描风:一个手表,胶片颗粒,冷调高对比'
const styleNode = (over) => ({
id: 's1', type: 'plugin:style-source:style', x: 0, y: 0, w: 262, h: 300,
data: Object.assign({ outKind: 'prompt', text: STYLE_TEXT, name: 'X光扫描风', refOn: true, refUrl: 'https://cdn/style.webp' }, over || {}),
})
const promptNode = (over) => ({ id: 'p1', type: 'prompt', x: 300, y: 0, w: 262, h: 200, data: Object.assign({}, over || {}) })
const textoutNode = (over) => ({ id: 't1', type: 'textout', x: 300, y: 200, w: 262, h: 200, data: Object.assign({}, over || {}) })
const combineNode = (over) => ({ id: 'c1', type: 'combine', x: 300, y: 400, w: 262, h: 160, data: Object.assign({}, over || {}) })
const edge = (from, to, extra) => Object.assign({ id: from + '>' + to, from, to }, extra || {})
console.log('\n[A] inheritedTextOf:上游给的是什么')
ok('样式节点 → 提示词(空):拿到样式节点那段提示词', () => {
const r = TG.inheritedTextOf('p1', [styleNode(), promptNode()], [edge('s1', 'p1')])
assert.strictEqual(r.text, STYLE_TEXT)
assert.strictEqual(r.sourceNodeId, 's1')
assert.strictEqual(r.more, 0)
})
ok('样式节点 → 文本(空):同样拿得到(此前节点体只显示空 placeholder 的就是这条)', () => {
const r = TG.inheritedTextOf('t1', [styleNode(), textoutNode()], [edge('s1', 't1')])
assert.strictEqual(r.text, STYLE_TEXT)
assert.strictEqual(r.sourceNodeId, 's1')
})
ok('没有任何上游:空(节点体保持 placeholder)', () => {
const r = TG.inheritedTextOf('p1', [promptNode()], [])
assert.strictEqual(r.text, '')
assert.strictEqual(r.sourceNodeId, '')
assert.strictEqual(r.more, 0)
})
ok('多路上游按画布 Y 轴取第一条,另有几路记在 more 里', () => {
const top = { id: 's1', type: 'plugin:style-source:style', x: 0, y: 0, w: 262, h: 300, data: { outKind: 'prompt', text: '上面的样式文案' } }
const bottom = { id: 's2', type: 'plugin:style-source:style', x: 0, y: 500, w: 262, h: 300, data: { outKind: 'prompt', text: '下面的样式文案' } }
// 入边顺序故意倒着给:排序必须由画布位置决定,而不是按入边数组顺序
const r = TG.inheritedTextOf('p1', [top, bottom, promptNode()], [edge('s2', 'p1'), edge('s1', 'p1')])
assert.strictEqual(r.text, '上面的样式文案')
assert.strictEqual(r.sourceNodeId, 's1')
assert.strictEqual(r.more, 1)
})
ok('停用(enabled:false)的上游不参与继承', () => {
const r = TG.inheritedTextOf('p1', [styleNode({ enabled: false }), promptNode()], [edge('s1', 'p1')])
assert.strictEqual(r.text, '')
})
ok('参考图节点不算文本来源(不会把空文本当继承)', () => {
const ref = { id: 'r1', type: 'ref', x: 0, y: 0, data: { url: 'https://cdn/a.webp' } }
const r = TG.inheritedTextOf('p1', [ref, promptNode()], [edge('r1', 'p1')])
assert.strictEqual(r.text, '')
})
ok('链式上游:提示词 A ← 样式节点,B 继承到的是样式节点那段(穿过中间节点取最终文本)', () => {
const p2 = { id: 'p2', type: 'prompt', x: 600, y: 0, w: 262, h: 200, data: {} }
const r = TG.inheritedTextOf('p2', [styleNode(), promptNode(), p2], [edge('s1', 'p1'), edge('p1', 'p2')])
assert.strictEqual(r.text, STYLE_TEXT)
// 中间节点自己没写字 ⇒ 下游看到的来源仍是真正产出文案的那个节点
assert.strictEqual(r.sourceNodeId, 'p1')
})
ok('上游节点自己写了字 ⇒ 继承到的是它写的(改过的优先于更上游)', () => {
const r = TG.inheritedTextOf('p2', [styleNode(), promptNode({ text: '我在提示词节点上改过的文案' }), { id: 'p2', type: 'prompt', x: 600, y: 0, data: {} }], [edge('s1', 'p1'), edge('p1', 'p2')])
assert.strictEqual(r.text, '我在提示词节点上改过的文案')
})
console.log('\n[B] nodeRefsOf:文本节点把图片继续往下传')
ok('样式节点 → 文本节点:示例图跟着过去(修复前这里是空数组)', () => {
const refs = TG.nodeRefsOf(textoutNode(), [edge('s1', 't1')], [styleNode(), textoutNode()])
assert.strictEqual(refs.length, 1)
assert.strictEqual(refs[0].id, 's1:ref')
assert.strictEqual(refs[0].data.url, 'https://cdn/style.webp')
})
ok('样式节点 → 文本节点 → 分层节点:分层拿到的图不为空(用户实际踩到的那条路)', () => {
const layer = { id: 'l1', type: 'layer', x: 600, y: 200, data: {} }
const nodes = [styleNode(), textoutNode(), layer]
const edges = [edge('s1', 't1'), edge('t1', 'l1')]
// layerInputsOf 的取图方式:对每条入边的来源调 nodeRefsOf
const incoming = edges.filter((e) => e.to === 'l1').map((e) => nodes.find((n) => n.id === e.from))
const collected = incoming.flatMap((src) => TG.nodeRefsOf(src, edges, nodes, new Set()))
assert.strictEqual(collected.length, 1)
assert.strictEqual(collected[0].data.url, 'https://cdn/style.webp')
})
ok('文本节点直连的图片节点仍然优先保留(direct 不被丢)', () => {
const ref = { id: 'r1', type: 'ref', x: 0, y: 200, data: { url: 'https://cdn/direct.webp' } }
const refs = TG.nodeRefsOf(textoutNode(), [edge('s1', 't1'), edge('r1', 't1')], [styleNode(), textoutNode(), ref])
const urls = refs.map((r) => r.data.url)
assert.ok(urls.includes('https://cdn/direct.webp'))
assert.ok(urls.includes('https://cdn/style.webp'))
})
ok('上游是润色(llm)时仍走原分支:不重复叠加', () => {
const llm = { id: 'm1', type: 'llm', x: 0, y: 200, data: {} }
const p = { id: 'p9', type: 'prompt', x: 0, y: 100, data: { text: '原文' } }
const nodes = [llm, p, textoutNode()]
const edges = [edge('p9', 'm1'), edge('m1', 't1')]
const refs = TG.nodeRefsOf(textoutNode(), edges, nodes, new Set())
assert.strictEqual(JSON.stringify(refs.map((r) => r.id)), '[]') // 这条链上没有图,也不该凭空造
})
ok('文本节点没有任何图来源时返回空(不误报)', () => {
// vm 沙箱里的数组与本文件不同 realm,deepStrictEqual 会「同结构但非同一引用」假失败 ⇒ 比 JSON
assert.strictEqual(JSON.stringify(TG.nodeRefsOf(textoutNode(), [edge('s1', 't1')], [styleNode({ refOn: false }), textoutNode()])), '[]')
})
console.log('\n[C] 回归:生成链路取值不变')
ok('样式节点直连生图:文本 + 示例图都在', () => {
const gen = { id: 'g1', type: 'gen', x: 600, y: 0, data: {} }
const tasks = TG.buildGenerationTasksV2(gen, [styleNode(), gen], [edge('s1', 'g1')])
assert.strictEqual(tasks.length, 1)
assert.strictEqual(tasks[0].text, STYLE_TEXT)
assert.strictEqual(JSON.stringify(tasks[0].refs), JSON.stringify([{ type: 'url', value: 'https://cdn/style.webp' }]))
})
ok('经提示词节点(空):下游照样拿到原文与图(own || 上游 的老规则没被改动)', () => {
const gen = { id: 'g1', type: 'gen', x: 600, y: 0, data: {} }
const tasks = TG.buildGenerationTasksV2(gen, [styleNode(), promptNode(), gen], [edge('s1', 'p1'), edge('p1', 'g1')])
assert.strictEqual(tasks.length, 1)
assert.strictEqual(tasks[0].text, STYLE_TEXT)
assert.strictEqual(JSON.stringify(tasks[0].refs), JSON.stringify([{ type: 'url', value: 'https://cdn/style.webp' }]))
})
ok('节点自己写了字 ⇒ 下游用它写的(接管语义不变)', () => {
const gen = { id: 'g1', type: 'gen', x: 600, y: 0, data: {} }
const tasks = TG.buildGenerationTasksV2(gen, [styleNode(), promptNode({ text: '我的改写' }), gen], [edge('s1', 'p1'), edge('p1', 'g1')])
assert.strictEqual(tasks.length, 1)
assert.strictEqual(tasks[0].text, '我的改写')
})
console.log('\n[D] 连线数 = 任务数(用户报障:连了 3 条线只出 1 个任务)')
ok('样式节点直连 + 经提示词 + 经文本 ⇒ 3 个任务(都继承同一段文本也不合并)', () => {
const gen = { id: 'g1', type: 'gen', x: 900, y: 0, data: {} }
const nodes = [styleNode(), promptNode(), textoutNode(), gen]
const edges = [edge('s1', 'p1'), edge('s1', 't1'), edge('s1', 'g1'), edge('p1', 'g1'), edge('t1', 'g1')]
const tasks = TG.buildGenerationTasksV2(gen, nodes, edges)
assert.strictEqual(tasks.length, 3, '三条入边应各生成 1 个任务,实际 ' + tasks.length)
// 身份归到直接上游节点:生图节点上的「任务 id」必须互不相同,否则 taskActive / taskStats 会互相踩
assert.strictEqual(JSON.stringify(tasks.map((t) => t.id).sort()), JSON.stringify(['p1', 's1', 't1']))
// 文本来源仍是样式节点(继承语义不变)
assert.strictEqual(JSON.stringify(tasks.map((t) => t.sourceNodeId).sort()), JSON.stringify(['s1', 's1', 's1']))
assert.ok(tasks.every((t) => t.text === STYLE_TEXT))
})
ok('同一提示词既直连生图又经 relay 抵达 ⇒ 仍只算 1 个任务(防重复计费)', () => {
const gen = { id: 'g1', type: 'gen', x: 900, y: 0, data: {} }
const relay = { id: 'r1', type: 'relay', x: 600, y: 300, data: {} }
const nodes = [styleNode(), promptNode(), relay, gen]
const edges = [edge('s1', 'p1'), edge('p1', 'g1'), edge('p1', 'r1'), edge('r1', 'g1')]
const tasks = TG.buildGenerationTasksV2(gen, nodes, edges)
assert.strictEqual(tasks.length, 1, '同一来源经多条路径抵达只留一条,实际 ' + tasks.length)
})
console.log('\n' + passed + ' 项全部通过')