// 核心链路纯函数行为测试(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 })()) console.log('\n workbench-logic: ' + pass + ' 通过 / ' + fail + ' 失败') if (fail) process.exit(1)