Files

114 lines
6.6 KiB
JavaScript
Raw Permalink 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'
/* 相册(outKind:'images')任务图行为(2026-11-26)
*
* 守的是什么:
* · 一图一任务:相册 N 张 → N 个任务,每个只带自己那张图
* · 批量模式:按 ALBUM_BATCH_MAX(12) 分批 → ⌈N/12⌉ 个任务,每批多张参考图
* · 组合扇出:相册接在组合前面时必须变成 N 个任务(否则会被压成 1 个任务 + N 张图)
* · 零回归:没有相册时,ref/组合/中继的任务数逐字不变
*
* 用真实模块 + 最小注入实现(不切片 app.js):依赖全是纯函数,本文件里各写一份等价实现。
*/
const assert = require('assert')
const fs = require('fs')
const path = require('path')
const vm = require('vm')
const src = fs.readFileSync(path.join(__dirname, '..', 'src', 'generation-task-graph.js'), 'utf8')
const box = { console }
vm.runInNewContext(src, box)
const TG = box.IWBWTaskGraph
assert.ok(TG && typeof TG.create === 'function', '模块应导出 IWBWTaskGraph.create')
// ── 最小注入依赖(语义与 app.js 一致,但实现从简)──
const byPos = (a, b) => (Number(a.x) - Number(b.x)) || (Number(a.y) - Number(b.y)) || String(a.id).localeCompare(String(b.id))
const dedupeTaskRefs = (list) => { const seen = new Set(), out = []; for (const r of (list || [])) { if (!r || seen.has(r.id)) continue; seen.add(r.id); out.push(r) } return out }
const refArgsOf = (rn) => { const d = (rn && rn.data) || {}; if (d.localName) return [{ type: 'local', name: d.localName }]; if (d.data) return [{ type: 'data', value: d.data, name: d.name || 'ref.png' }]; if (typeof d.url === 'string') return d.url.trim() ? [{ type: 'url', value: d.url.trim() }] : []; if (d.url) return [{ type: 'data', value: d.url, name: d.name || 'ref.png' }]; return [] }
const isBatchSrc = (n) => !!n && (n.type === 'prompt' || n.type === 'textout' || n.type === 'combine' || n.type === 'batch' || (n.data && n.data.outKind === 'prompt'))
const batchJoinedText = (data, base) => String((data && data.combined) || base || '')
const batchSrcText = (n) => String((n && n.data && (n.data.combined || n.data.text)) || '')
const wDedupeTaskIds = (list) => { const seen = new Set(), out = []; for (const t of (list || [])) { if (!t || seen.has(t.id)) continue; seen.add(t.id); out.push(t) } return out }
const tg = TG.create({
sortByPosition: (arr) => arr.slice().sort(byPos),
compareCanvasImageOrder: byPos,
compareCanvasTextOrder: byPos,
dedupeTaskRefs,
isBatchSrc,
batchJoinedText,
wDedupeTaskIds,
refArgsOf,
batchSrcText,
standardSizeOf: () => '1024x1024',
})
const n = (id, type, data, extra) => Object.assign({ id, type, x: Number(id.replace(/\D/g, '')) || 0, y: 0, data: data || {} }, extra || {})
const e = (from, to) => ({ id: from + '>' + to, from, to })
const img = (i) => ({ name: 'img' + i + '.png', src: 'data:image/png;base64,AAA' + i })
const album = (id, count, extra) => n(id, 'plugin:album:album', Object.assign({ outKind: 'images', items: Array.from({ length: count }, (_, i) => img(i + 1)) }, extra || {}))
const gen = n('g9', 'gen', {})
const tasks = (nodes, edges) => tg.buildGenerationTasksV2(gen, nodes.concat([gen]), edges)
let pass = 0, fail = 0
const ok = (name, cond, extra) => { if (cond) { pass++; console.log(' PASS ' + name) } else { fail++; console.log(' FAIL ' + name + (extra ? ' → ' + extra : '')) } }
// ① 一图一任务:相册(3) → 组合 ← 提示词 → 生图 = 3 个任务,各 1 张图 + 同一段文本
{
const nodes = [album('a1', 3), n('p2', 'prompt', { text: '一段提示词' }), n('c3', 'combine', {}), gen]
const out = tasks(nodes, [e('a1', 'c3'), e('p2', 'c3'), e('c3', 'g9')])
ok('一图一任务 = 3 个任务', out.length === 3, '实际 ' + out.length)
ok('每个任务只带 1 张图', out.every((t) => t.refs.length === 1), JSON.stringify(out.map((t) => t.refs.length)))
ok('每个任务都带同一段提示词', out.every((t) => t.text === '一段提示词'), JSON.stringify(out.map((t) => t.text)))
ok('任务 id 互不相同', new Set(out.map((t) => t.id)).size === 3, JSON.stringify(out.map((t) => t.id)))
}
// ② 批量模式:22 张 → 2 个任务(12 + 10)
{
const nodes = [album('a1', 22, { albumMode: 'batch' }), n('c3', 'combine', {}), gen]
const out = tasks(nodes, [e('a1', 'c3'), e('c3', 'g9')])
ok('批量 = 2 个任务', out.length === 2, '实际 ' + out.length)
ok('按 12 张分批:12 + 10', JSON.stringify(out.map((t) => t.refs.length)) === '[12,10]', JSON.stringify(out.map((t) => t.refs.length)))
}
// ③ 组合前面再拼「其他参考图」:每个任务 = 该相册图 + 额外参考图
{
const nodes = [album('a1', 3), n('r4', 'ref', { url: 'https://example.test/extra.png' }), n('c3', 'combine', {}), gen]
const out = tasks(nodes, [e('a1', 'c3'), e('r4', 'c3'), e('c3', 'g9')])
ok('组合扇出后仍是 3 个任务', out.length === 3, '实际 ' + out.length)
ok('每个任务 = 相册图 + 额外参考图(2 张)', out.every((t) => t.refs.length === 2), JSON.stringify(out.map((t) => t.refs.length)))
}
// ④ 相册直连生图(不接组合):一图一任务
{
const nodes = [album('a1', 4), gen]
const out = tasks(nodes, [e('a1', 'g9')])
ok('直连仍是 4 个任务', out.length === 4, '实际 ' + out.length)
}
// ⑤ 零回归:普通 ref + 提示词 → 生图(1 个任务、1 张图)
{
const nodes = [n('p2', 'prompt', { text: 'hello' }), n('r4', 'ref', { url: 'https://example.test/x.png' }), gen]
const out = tasks(nodes, [e('p2', 'g9'), e('r4', 'g9')])
ok('普通画布仍是 1 个任务', out.length === 1, '实际 ' + out.length)
ok('普通画布 refs = 1', out[0].refs.length === 1, JSON.stringify(out[0].refs.length))
}
// ⑥ 零回归:组合(无多值来源)仍是 1 个任务、合并文本与图
{
const nodes = [n('p2', 'prompt', { text: 'a' }), n('t5', 'textout', { text: 'b' }), n('r4', 'ref', { url: 'https://example.test/x.png' }), n('c3', 'combine', {}), gen]
const out = tasks(nodes, [e('p2', 'c3'), e('t5', 'c3'), e('r4', 'c3'), e('c3', 'g9')])
ok('普通组合仍是 1 个任务', out.length === 1, '实际 ' + out.length)
ok('普通组合文本已合并', /a/.test(out[0].text) && /b/.test(out[0].text), out[0].text)
}
// ⑦ 停用(on:false)的相册项不参与
{
const items = [img(1), Object.assign(img(2), { on: false }), img(3)]
const nodes = [n('a1', 'plugin:album:album', { outKind: 'images', items }), gen]
const out = tasks(nodes, [e('a1', 'g9')])
ok('停用的相册项被跳过', out.length === 2, '实际 ' + out.length)
}
console.log('\n===== album 任务图:' + pass + ' 通过 / ' + fail + ' 失败 =====')
process.exit(fail ? 1 : 0)