Files
image-workbench-desktop/tests/workbench-logic-test.js
T
qiqi 0e98270c87 fix(gallery): 输出面板老图缩略图 —— 结果条目自带图库映射(v3.26.258)
症状:换到新电脑(图库索引/缩略图缓存不同步)后,输出面板老结果大多空白,图库里的图其实还在。

- 归档成功时把「url → 图库行名 / out_ 键」回写进画布 data.result(wAddOutputs 两条路径都覆盖)
- wOutThumbEnsure 没有图库行时用结果自带的 name/idb 兜底(assets → images/gallery 现生成 320,再退 idb)
- 加载后一次性自愈:结果条目缺 idb 时按 url 查落地账本 iwb_ingest_idx 的 ref.__blob 补上;幂等、只在有变化时 commit
- 逻辑落 src/runtime-workbench-logic.js(markResultRefs / healResultRefs),app.js 只做装配
- 单测:workbench-logic ⑥ 段 8 条(42/42);gallery-sync / ui-workbench-hooks / out-groups-order / generation-results-store 全绿

棘轮:app.js 9534→9542(+8)、Workbench 2714→2716(+2)。原因:回写点在 wAddOutputs 与组件侧 bridge/heal effect,必须读 Workbench 内 refs/commitCanvas,抽不到 src;逻辑本体已落 src。
2026-12-05 21:26:28 +08:00

166 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.
// 核心链路纯函数行为测试(src/runtime-workbench-logic.js)—— 2026-11-10 新增
//
// 为什么加:该模块 1927 行,是全仓最大单体;但其中 5 个**纯逻辑**函数此前没有任何直接测试
// (测试里只出现函数名之外的间接覆盖)。纯函数最适合先钉住语义:
// · 没有 DOM、没有计时器 ⇒ 永不 flaky
// · 是 runNode / 画布交互 / 图库分列 的决策依据 ⇒ 改错会静默出错
//
// 覆盖:
// ① keyAnomalyOf —— 顶栏「请设置 Key」的判定(错判=用户以为能用)
// ② taskCountOf —— 生图节点任务总张数(错判=并发数/进度条算错)
// ③ computeHistColumns—— 图库瀑布流分列(错判=列高失衡/丢条目)
// ④ selAnchorOf —— 多选浮动工具栏定位(错判=工具条飞走)
// ⑤ joinGroupOf —— 「并入分组」选哪个组(错判=并错组)
'use strict'
const fs = require('fs')
const path = require('path')
const src = fs.readFileSync(path.join(__dirname, '..', 'src', 'runtime-workbench-logic.js'), 'utf8')
const box = {}
new Function('window', 'globalThis', src)(box, box)
const L = box.IWBWorkbenchLogic
let pass = 0
let fail = 0
const ok = (name, cond, extra) => {
if (cond) { pass++; console.log(' ✅ ' + name) } else { fail++; console.log(' ❌ ' + name + (extra ? ' → ' + JSON.stringify(extra) : '')) }
}
// ── ① keyAnomalyOf ────────────────────────────────────────────────────────────
ok('①-1 未配置任何服务 → 「未配置服务」', L.keyAnomalyOf({ providers: [] }) === '未配置服务')
ok('①-2 有服务但都没填 Key → 「请设置 Key」', L.keyAnomalyOf({ providers: [{ keys: [{ filled: false }] }] }) === '请设置 Key')
ok('①-3 任一 Key 已填 → null(正常,不占顶栏)', L.keyAnomalyOf({ providers: [{ keys: [{ filled: false }, { filled: true }] }] }) === null)
ok('①-4 服务存在但 keys 缺失 → 视为未填', L.keyAnomalyOf({ providers: [{ name: 'x' }] }) === '请设置 Key')
ok('①-5 空/异常入参不抛错', L.keyAnomalyOf(null) === '未配置服务' && L.keyAnomalyOf(undefined) === '未配置服务')
// ── ② taskCountOf ─────────────────────────────────────────────────────────────
// 注入假的 buildGenerationTasks:只按传入任务表返回
const mkCtx = (tasks) => ({ refs: { nodes: [], edges: [] }, buildGenerationTasks: () => tasks })
const gen = (data) => ({ id: 'g1', type: 'gen', data: data || {} })
ok('②-1 非 gen 节点 → 0', L.taskCountOf(mkCtx([]), { id: 'p', type: 'prompt', data: {} }) === 0)
ok('②-2 enabled=false 的节点 → 0(不参与运行)', L.taskCountOf(mkCtx([{ id: 't1', text: 'x' }]), gen({ enabled: false })) === 0)
ok('②-3 无文本的任务不计(缺上游文案)', L.taskCountOf(mkCtx([{ id: 't1', text: '' }]), gen({})) === 0)
ok('②-4 单任务默认 1 张', L.taskCountOf(mkCtx([{ id: 't1', text: 'x' }]), gen({})) === 1)
ok('②-5 taskVariants 优先于 variants', L.taskCountOf(mkCtx([{ id: 't1', text: 'x' }]), gen({ taskVariants: { t1: 3 }, variants: 9 })) === 3)
ok('②-6 未勾选的任务被跳过', L.taskCountOf(mkCtx([{ id: 't1', text: 'x' }, { id: 't2', text: 'y' }]), gen({ taskActive: { t2: false } })) === 1)
ok('②-7 变体数上限 10(防误填超大值)', L.taskCountOf(mkCtx([{ id: 't1', text: 'x' }]), gen({ variants: 999 })) === 10)
ok('②-8 变体数下限 1(0 / 负数按 1)', L.taskCountOf(mkCtx([{ id: 't1', text: 'x' }]), gen({ variants: 0 })) === 1 &&
L.taskCountOf(mkCtx([{ id: 't1', text: 'x' }]), gen({ variants: -5 })) === 1)
ok('②-9 多任务求和', L.taskCountOf(mkCtx([{ id: 't1', text: 'x' }, { id: 't2', text: 'y' }]), gen({ variants: 2 })) === 4)
// ── ③ computeHistColumns ──────────────────────────────────────────────────────
const col = (list, w, limit, tile) => L.computeHistColumns({ histFilteredFallback: list, histGridW: w, histLimit: limit || 60, histTile: tile })
const c0 = col([], 600)
ok('③-1 空列表 → 空桶且 colW=0', c0.buckets.length === 0 && c0.colW === 0)
const files = [
{ name: 'a', w: 100, h: 100, mtime: 1 },
{ name: 'b', w: 100, h: 100, mtime: 3 },
{ name: 'c', w: 100, h: 100, mtime: 2 },
]
const c1 = col(files, 600)
const all = c1.buckets.flat()
ok('③-2 不丢条目(桶内总数 == 输入数)', all.length === 3, all.length)
ok('③-3 按 mtime 倒序进桶(最新的先)', all[0].name === 'b', all.map((f) => f.name))
// v3.25.38 契约变更:列数 = floor((面板宽+gap)/(图幅+gap)),图幅固定 ⇒ 图片尺寸稳定,
// 只靠增减列数适应面板宽度(不再夹在 2~4 列:窄面板单列才能完整显示、宽面板可以更多列)
ok('③-4 列数随宽度单调增加', col(files, 200).buckets.length === 1 && col(files, 700).buckets.length === 5 && col(files, 2000).buckets.length > col(files, 700).buckets.length)
ok('③-4b 图幅可调:同一宽度下图幅越大列数越少', col(files, 700, 60, 220).buckets.length === 3, col(files, 700, 60, 220).buckets.length)
ok('③-5 colW 计算:(w - gap*(n-1))/n,gap=8', Math.abs(col(files, 600).colW - (600 - 8 * 3) / 4) < 1e-9)
ok('③-6 histGridW 为 0 时按 300 兜底', col(files, 0).buckets.length === 2)
// 极窄宽度:只保留 1 列(宁可少列,也不要把图片压成几十像素的小图)
ok('③-9 极窄宽度只有 1 列且列宽不塌缩', col(files, 100).buckets.length === 1 && col(files, 100).colW === 100, col(files, 100).buckets.length)
ok('③-7 histLimit 生效(截断到上限)', col([...files, ...files, ...files], 600, 4).buckets.flat().length === 4)
ok('③-8 缺 w/h 的文件按 1:1 处理,不产生 NaN', (() => {
const r = L.computeHistColumns({ histFilteredFallback: [{ name: 'x', mtime: 1 }], histGridW: 600, histLimit: 10 })
return r.buckets.flat().length === 1 && Number.isFinite(r.colW)
})())
// ── ④ selAnchorOf ─────────────────────────────────────────────────────────────
const anchorCtx = {
refs: { nodes: [], nodeGroupMap: new Map() },
nodeWidth: () => 200,
hOf: () => 100,
floatbarLift: (hasTitle, inGroup, single) => (single ? 10 : 0) + (inGroup ? 5 : 0),
hasNodeTitle: () => true,
}
const nodes = [
{ id: 'n1', type: 'prompt', x: 0, y: 0, data: { title: 'A' } },
{ id: 'n2', type: 'prompt', x: 400, y: 200, data: { title: 'B' } },
]
ok('④-1 空选择 → null', L.selAnchorOf(anchorCtx, [], nodes) === null)
ok('④-2 选中不存在的 id → null', L.selAnchorOf(anchorCtx, ['nope'], nodes) === null)
const a1 = L.selAnchorOf(anchorCtx, ['n1'], nodes)
ok('④-3 单选:cx = x + 宽/2(不是包围盒中心)', a1.cx === 100 && a1.count === 1, a1)
ok('④-4 单选:lift 含单选框抬升(10)', a1.lift === 10, a1.lift)
const a2 = L.selAnchorOf(anchorCtx, ['n1', 'n2'], nodes)
ok('④-5 多选:包围盒 = 0,0 → 600,300', a2.minX === 0 && a2.minY === 0 && a2.maxX === 600 && a2.maxY === 300, a2)
ok('④-6 多选:cx = 包围盒中心 300', a2.cx === 300, a2.cx)
ok('④-7 多选:lift 不含单选框抬升', a2.lift === 0, a2.lift)
const groupedCtx = { ...anchorCtx, refs: { nodes: [], nodeGroupMap: new Map([['n1', 'g1']]) } }
ok('④-8 组内节点 lift 额外 +5', L.selAnchorOf(groupedCtx, ['n1'], nodes).lift === 15)
// ── ⑤ joinGroupOf ─────────────────────────────────────────────────────────────
// 假 CanvasOps:connectedGroupIds 按 edges 找相连组;addMemberToGroup 真实写入
box.IWBCanvasOps = {
connectedGroupIds: (edges, groups, nodeId) => (edges || []).filter((e) => e.to === nodeId || e.from === nodeId)
.map((e) => (groups || []).find((g) => (g.memberIds || []).includes(e.to === nodeId ? e.from : e.to)))
.filter(Boolean).map((g) => g.id),
addMemberToGroup: (groups, gid, nodeId) => groups.map((g) => (g.id === gid && !(g.memberIds || []).includes(nodeId)
? { ...g, memberIds: (g.memberIds || []).concat(nodeId) } : g)),
}
const groups = [
{ id: 'g-small', name: '小组', memberIds: ['x1'] },
{ id: 'g-big', name: '大组', memberIds: ['y1', 'y2', 'y3'] },
]
const joinCtx = {
refs: { groups, edges: [{ from: 'x1', to: 'n1' }, { from: 'y1', to: 'n1' }] },
pushHistory: () => {},
setNotice: () => {},
commitCanvas: (a, b, nextGroups) => { joinCtx._groups = nextGroups },
}
const joined = L.joinGroupOf(joinCtx, 'n1')
ok('⑤-1 相连多个组时并入**成员最多**的那个', joined === true &&
joinCtx._groups.find((g) => g.id === 'g-big').memberIds.includes('n1'), joinCtx._groups)
ok('⑤-2 不相连任何组 → false 且不改画布', (() => {
const c = { refs: { groups, edges: [] }, commitCanvas: () => { throw new Error('不应提交') } }
return L.joinGroupOf(c, 'n9') === false
})())
// ── ⑥ markResultRefs / healResultRefs(v3.26.258)─────────────────────────────
// 跨机器图库索引丢失时,画布结果条目要能自带「行名 → out_ 键」或落地账本里的字节引用。
const mkNodes = () => ([
{ id: 'g1', type: 'gen', data: { result: [
{ promptId: 't1', vi: 0, items: [{ url: 'https://a/1.png', ok: true }, { url: 'https://a/2.png', ok: true }] },
{ promptId: 't2', vi: 0, items: [{ url: 'https://a/3.png', ok: true }] },
] } },
{ id: 'p1', type: 'prompt', data: { text: 'x' } },
])
const mctx = (nodes) => { const c = { refs: { nodes }, _commits: 0 }; c.commitCanvas = () => { c._commits++ }; return c }
const m1 = mctx(mkNodes())
const n1 = L.markResultRefs(m1.refs, m1.commitCanvas, [{ url: 'https://a/2.png', name: 'gen_2.png' }])
ok('⑥-1 只回写命中的 url', n1 === 1 && m1.refs.nodes[0].data.result[0].items[1].name === 'gen_2.png' &&
m1.refs.nodes[0].data.result[0].items[1].idb === 'out_gen_2.png' &&
m1.refs.nodes[0].data.result[0].items[0].name === undefined, m1.refs.nodes[0].data.result[0].items)
ok('⑥-2 有变更才 commit', m1._commits === 1)
ok('⑥-3 重复回写同一行名 → 0 变更、不再 commit', L.markResultRefs(m1.refs, m1.commitCanvas,
[{ url: 'https://a/2.png', name: 'gen_2.png' }]) === 0 && m1._commits === 1)
ok('⑥-4 空入参/坏入参不抛错', L.markResultRefs(null, null, null) === 0 && L.markResultRefs({ nodes: [] }, null, [{ url: 'u' }]) === 0)
const h1 = mctx(mkNodes())
const store1 = { iwb_ingest_idx: [{ id: 'i1', url: 'https://a/1.png', ref: { __blob: 'imgblob_x_1' } }, { id: 'i2', url: 'https://a/9.png', ref: { __blob: 'imgblob_y_9' } }] }
const hn1 = L.healResultRefs(h1.refs, h1.commitCanvas, { get: (k, fb) => (k in store1 ? store1[k] : fb), set: () => {} })
ok('⑥-5 按落地账本补 idb(只补缺的、只补命中的)', hn1 === 1 &&
h1.refs.nodes[0].data.result[0].items[0].idb === 'imgblob_x_1' &&
h1.refs.nodes[0].data.result[0].items[1].idb === undefined)
ok('⑥-6 自愈后不再重复补(幂等)', L.healResultRefs(h1.refs, h1.commitCanvas, { get: (k, fb) => (k in store1 ? store1[k] : fb) }) === 0)
ok('⑥-7 账本为空 → 0,不 commit', (() => {
const c = mctx(mkNodes())
return L.healResultRefs(c.refs, c.commitCanvas, { get: () => [] }) === 0 && c._commits === 0
})())
ok('⑥-8 get 抛错时安全返回 0', L.healResultRefs(mctx(mkNodes()).refs, null, { get: () => { throw new Error('boom') } }) === 0)
console.log('\n workbench-logic: ' + pass + ' 通过 / ' + fail + ' 失败')
if (fail) process.exit(1)