Files

122 lines
7.5 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'
// 输出节点「结果组」的归组与排序契约(行为断言,无源码文本钉点)。
//
// 回归背景(2026-11-04 用户实测):批量出图后,输出节点的图片顺序与生图节点任务卡顺序对不上。
// 旧实现用**来源节点坐标**反推组序:nodeFor() 只在「直连生图节点的上游」里找 promptId 前缀,
// 中继 / 连接 / 组合等中间节点介入时一律匹配不到 ⇒ 全部落到 1e9 ⇒ 顺序退化成「谁先出图谁在前」。
// 组内张序同理来自追加写的 result 数组(并发完成顺序 ≈ 随机),同任务第 2 张可能排在第 1 张前。
//
// 本文件锁住四条判据:
// ① 组序 = 任务卡顺序(含中继链路:任务 id 带 @relay 后缀);
// ② 组内按变体序号 vi 升序,不随完成先后抖动;
// ③ 任务列表里没有的旧结果排最后(按出图时间),不能插队打乱当前任务;
// ④ 纯函数:不改入参(result 数组顺序必须原样),异常入参不抛。
const assert = require('assert')
const fs = require('fs')
const path = require('path')
const vm = require('vm')
const root = path.resolve(__dirname, '..')
const sandbox = { React: {}, window: undefined }
vm.runInNewContext(fs.readFileSync(path.join(root, 'src', 'ui-node-bodies.js'), 'utf8'), sandbox)
const B = sandbox.IWBNodeBodies
assert.ok(B && typeof B.outGroupsOf === 'function', '应挂载 window.IWBNodeBodies.outGroupsOf')
const item = (tag) => ({ ok: true, url: 'https://cdn.test/' + tag + '.png', width: 1024, height: 1365 })
const entry = (promptId, vi, at, tag) => ({ promptId, vi, at, label: '任务', taskTitle: '', items: [item(tag || promptId + '_' + vi)] })
const gen = (id, x, y, result) => ({ id, x, y, data: { result } })
// vm.runInNewContext 里的数组属于另一个 realm(原型不同),比较前先在主 realm 重建
const ids = (groups) => Array.from(groups, (g) => String(g.promptId))
const urls = (groups) => Array.from(groups, (g) => Array.from(g.items, (r) => String(r.url).split('/').pop().replace('.png', '')))
// ── 1. 中继链路:任务 id 带 @relay 后缀,旧逻辑按坐标反推必然全部失配 ────────
// 生图节点直连的只有中继,任务前缀是上游文案节点的 id ⇒ nodeFor() 找不到。
{
const RELAY = 'r1'
const t = (i) => 'tx' + i + '@' + RELAY
// result 是「完成顺序」:任务3 → 任务1 → 任务2
const gi = gen('g1', 0, 0, [entry(t(2), 0, 300), entry(t(0), 0, 100), entry(t(1), 0, 200)])
const meta = [{ id: t(0) }, { id: t(1) }, { id: t(2) }]
const groups = B.outGroupsOf([gi], () => meta)
assert.deepStrictEqual(ids(groups), [t(0), t(1), t(2)], '① 组序必须跟随任务卡顺序,而不是出图完成顺序')
}
// ── 2. 组内按 vi 升序:第 2 张先回来也不能排到第 1 张前面 ──────────────────
{
const gi = gen('g1', 0, 0, [entry('t1', 1, 100), entry('t1', 0, 999), entry('t1', 2, 500)])
const groups = B.outGroupsOf([gi], () => [{ id: 't1' }])
assert.deepStrictEqual(urls(groups), [['t1_0', 't1_1', 't1_2']], '② 同任务多张按变体序号排,不按完成时间')
}
// ── 3. 任务列表里没有的旧结果排最后,且不打乱当前任务 ─────────────────────
{
const gi = gen('g1', 0, 0, [entry('old', 0, 999), entry('t1', 0, 100), entry('t2', 0, 200)])
const groups = B.outGroupsOf([gi], () => [{ id: 't1' }, { id: 't2' }])
assert.deepStrictEqual(ids(groups), ['t1', 't2', 'old'], '③ 旧结果排最后(known 任务在前,unknown 按 at)')
assert.deepStrictEqual(Array.from(groups, (g) => !!g.known), [true, true, false], '③ known 标记用于提示「另有 N 张来自已变更的任务」')
}
// ── 4. 多个生图节点按画布坐标(y → x),与接入顺序无关 ────────────────────
{
const gLow = gen('gLow', 0, 900, [entry('b', 0, 100)])
const gHigh = gen('gHigh', 0, 100, [entry('a', 0, 100)])
const groups = B.outGroupsOf([gLow, gHigh], () => [])
assert.deepStrictEqual(ids(groups), ['a', 'b'], '④ 生图节点按 y 上→下排序(不按 edges 接入顺序)')
}
{
const gRight = gen('gRight', 900, 0, [entry('b', 0, 100)])
const gLeft = gen('gLeft', 100, 0, [entry('a', 0, 100)])
const groups = B.outGroupsOf([gRight, gLeft], () => [])
assert.deepStrictEqual(ids(groups), ['a', 'b'], '④ 同 y 时按 x 左→右')
}
// ── 5. 同 (任务, 变体) 的历史残留条目只留 at 最新,不重复出图 ─────────────
{
const gi = gen('g1', 0, 0, [entry('t1', 0, 100, 'late_old'), entry('t1', 0, 900, 'late_new')])
const groups = B.outGroupsOf([gi], () => [{ id: 't1' }])
assert.deepStrictEqual(urls(groups), [['late_new']], '⑤ 同键条目取 at 最新,避免同一张图重复显示')
}
// ── 6. 纯函数:不改入参(result 数组与条目对象顺序原样) ─────────────────
{
const res = [entry('t2', 0, 200), entry('t1', 0, 100)]
const gi = gen('g1', 0, 0, res)
B.outGroupsOf([gi], () => [{ id: 't1' }, { id: 't2' }])
assert.deepStrictEqual(res.map((e) => e.promptId), ['t2', 't1'], '⑥ 不得就地排序入参 result')
}
// ── 7. 只收成功项;失败/空项不占格子但组仍在 ─────────────────────────────
{
const gi = gen('g1', 0, 0, [{ promptId: 't1', vi: 0, at: 1, items: [{ ok: false, error: 'x' }, { ok: true, url: 'https://cdn.test/ok.png' }, null] }])
const groups = B.outGroupsOf([gi], () => [{ id: 't1' }])
assert.strictEqual(groups.length, 1, '⑦ 失败条目仍保留组(任务卡需要组位)')
assert.deepStrictEqual(urls(groups), [['ok']], '⑦ 只显示 ok 且有 url 的项')
}
// ── 8. 异常入参不抛:非数组 / null / metaOf 抛错 / 缺 id ─────────────────
{
assert.strictEqual(B.outGroupsOf(null, null).length, 0, '⑧ null 入参 → 空数组')
assert.strictEqual(B.outGroupsOf([null, undefined], () => []).length, 0, '⑧ 空元素被过滤')
const gi = { id: 'g1', x: 0, y: 0, data: null }
assert.strictEqual(B.outGroupsOf([gi], () => { throw new Error('boom') }).length, 0, '⑧ metaOf 抛错时降级为空任务表')
const gi2 = { id: 'g1', x: 0, y: 0, data: { result: [{ vi: 0, at: 1, items: [{ ok: true, url: 'https://cdn.test/x.png' }] }] } }
const groups = B.outGroupsOf([gi2], () => [])
assert.strictEqual(groups.length, 1, '⑧ 缺 promptId 时仍成组(用占位 id)')
assert.ok(String(groups[0].promptId).indexOf('gen_g1_') === 0, '⑧ 占位 id 前缀可辨识')
}
// ── 9. 组标题:结果快照优先,其次任务当前标题(中继链路只有后者可用) ────
{
const gi = gen('g1', 0, 0, [{ promptId: 't1', vi: 0, at: 1, label: '把图片1的内容修改为图2的风格', items: [item('a')] }])
const withSnap = B.outGroupsOf([gi], () => [{ id: 't1', title: '风格2' }])
assert.strictEqual(withSnap[0].taskTitle, '', '⑨ 无快照时 taskTitle 为空')
assert.strictEqual(withSnap[0].taskTitleNow, '风格2', '⑨ 任务当前标题可作为回退(旧数据/中继链路)')
const gi2 = gen('g2', 0, 0, [{ promptId: 't1', vi: 0, at: 1, taskTitle: '快照标题', items: [item('a')] }])
assert.strictEqual(B.outGroupsOf([gi2], () => [{ id: 't1', title: '当前标题' }])[0].taskTitle, '快照标题', '⑨ 结果快照优先于当前标题')
}
console.log('out-groups-order: 全部通过')