430 lines
29 KiB
JavaScript
430 lines
29 KiB
JavaScript
'use strict'
|
||
|
||
// tests/canvas-geometry.js 的单测(P2-9:Workbench 连线几何/合法性纯函数 → src/)
|
||
|
||
const assert = require('assert')
|
||
const fs = require('fs')
|
||
const path = require('path')
|
||
const vm = require('vm')
|
||
const root = path.resolve(__dirname, '..')
|
||
|
||
const sandbox = {}
|
||
vm.runInNewContext(fs.readFileSync(path.join(root, 'src', 'canvas-geometry.js'), 'utf8'), sandbox)
|
||
const G = sandbox.IWBCanvasGeometry
|
||
assert.ok(G && typeof G.connectReasonOf === 'function')
|
||
|
||
const isProducer = (n) => n.type === 'gen' || n.type === 'layer'
|
||
const reason = (srcType, dstType, hasEdge) => G.connectReasonOf({ type: srcType }, { type: dstType }, !!hasEdge, isProducer)
|
||
|
||
// 已连接:batch 多输出允许重复;普通节点拒绝
|
||
assert.strictEqual(reason('prompt', 'gen', true), '这两个节点已经连接')
|
||
assert.strictEqual(reason('batch', 'gen', true), '', 'batch 多输出允许同对多边')
|
||
|
||
// 入边白名单
|
||
assert.strictEqual(reason('gen', 'prompt'), '提示词节点只能接收参考图或中继节点')
|
||
assert.strictEqual(reason('ref', 'prompt'), '')
|
||
assert.strictEqual(reason('gen', 'llm'), '润色节点只能接收提示词')
|
||
assert.strictEqual(reason('prompt', 'llm'), '')
|
||
assert.strictEqual(reason('ref', 'textout'), '文本节点只能接收润色、组合或连接节点')
|
||
assert.strictEqual(reason('combine', 'textout'), '')
|
||
assert.strictEqual(reason('prompt', 'combine'), '')
|
||
assert.strictEqual(reason('ref', 'combine'), '')
|
||
assert.strictEqual(reason('ref', 'gen'), '')
|
||
assert.strictEqual(reason('gen', 'gen'), '生图节点不支持此来源')
|
||
assert.strictEqual(reason('gen', 'layer'), '分层节点不支持此来源')
|
||
assert.strictEqual(reason('prompt', 'layer'), '')
|
||
assert.strictEqual(reason('ref', 'batch'), '连接节点不支持此来源')
|
||
assert.strictEqual(reason('llm', 'batch'), '')
|
||
|
||
// out 只接收产图节点
|
||
assert.strictEqual(reason('gen', 'out'), '')
|
||
assert.strictEqual(reason('layer', 'out'), '')
|
||
assert.strictEqual(reason('prompt', 'out'), '输出节点只能接收配置 / 分层节点')
|
||
|
||
// relay 直通;ref 只能作来源
|
||
assert.strictEqual(reason('prompt', 'relay'), '')
|
||
assert.strictEqual(reason('gen', 'ref'), '图片节点只能作为来源,不接受连线')
|
||
|
||
// distToSeg
|
||
assert.strictEqual(G.distToSeg({ x: 5, y: 5 }, { x: 0, y: 0 }, { x: 10, y: 0 }), 5)
|
||
assert.strictEqual(G.distToSeg({ x: 5, y: 0 }, { x: 0, y: 0 }, { x: 10, y: 0 }), 0)
|
||
assert.strictEqual(G.distToSeg({ x: 3, y: 4 }, { x: 0, y: 0 }, { x: 0, y: 0 }), 5, '退化线段 = 到点的距离')
|
||
|
||
// ── P1 拆分批 3(2026-10-28):computeEdgePath / areNodeEqual 的行为契约 ─────────
|
||
// 这两个函数自 app.js 迁入本模块。它们守的是**性能相关的实现选择**(不是几何正确性本身):
|
||
// · computeEdgePath 靠 pathCache 复用路径,避免每帧重算全部连线
|
||
// · areNodeEqual 在拖拽/缩放窗口内放宽数组引用比较,避免每帧重建全部节点体
|
||
// 所以这里断言的是「缓存真的命中 / 放宽真的生效」,而不只是"源码里有这行字"。
|
||
{
|
||
const G2 = G.create({
|
||
nodeTypeH: (t) => ({ prompt: 216, ref: 252, gen: 460, out: 320, llm: 240, textout: 220, combine: 240, batch: 240, relay: 72, plugin: 200, layer: 520 }[t] || 220),
|
||
nodeWidth: (n) => n && (n.type === 'relay' ? 72 : (n.w || (n.type === 'gen' ? 480 : (n.type === 'layer' ? 520 : 264)))),
|
||
isBatchSrc: (n) => !!n && (n.type === 'prompt' || n.type === 'textout' || n.type === 'llm' || n.type === 'combine' || n.type === 'relay'),
|
||
batchPortTop: (idx, total, h) => (h / (total + 1)) * (idx + 1),
|
||
compareCanvasTextOrder: (a, b) => (Number(a.y) - Number(b.y)) || (Number(a.x) - Number(b.x)),
|
||
})
|
||
assert.ok(G2 && typeof G2.computeEdgePath === 'function', 'create() 应产出 computeEdgePath')
|
||
assert.ok(G2 && typeof G2.areNodeEqual === 'function', 'create() 应产出 areNodeEqual')
|
||
|
||
// 缺依赖必须显式抛错(不静默降级)
|
||
assert.throws(() => G.create({}), /缺少注入依赖: nodeTypeH/, '缺 nodeTypeH 应抛错')
|
||
assert.throws(() => G.create({ nodeTypeH: () => 220 }), /缺少注入依赖: nodeWidth/, '缺 nodeWidth 应抛错')
|
||
|
||
// ── computeEdgePath:路径格式 + 缓存命中 ──
|
||
const mkGraph = (nodes, edges) => {
|
||
const byId = new Map(nodes.map((n) => [n.id, n]))
|
||
const incomingByNode = new Map(), outgoingByNode = new Map()
|
||
for (const e of edges) {
|
||
if (!incomingByNode.has(e.to)) incomingByNode.set(e.to, [])
|
||
if (!outgoingByNode.has(e.from)) outgoingByNode.set(e.from, [])
|
||
incomingByNode.get(e.to).push(e); outgoingByNode.get(e.from).push(e)
|
||
}
|
||
return { nodes, edges, byId, incomingByNode, outgoingByNode }
|
||
}
|
||
const nodeMap = new Map([
|
||
['p1', { id: 'p1', type: 'prompt', x: 0, y: 0, w: 240, h: 216 }],
|
||
['g1', { id: 'g1', type: 'gen', x: 400, y: 0, w: 480, h: 460 }],
|
||
])
|
||
const edges = [{ id: 'e1', from: 'p1', to: 'g1' }]
|
||
const graph = mkGraph([nodeMap.get('p1'), nodeMap.get('g1')], edges)
|
||
|
||
const cache = new Map()
|
||
const d1 = G2.computeEdgePath(edges[0], graph, nodeMap, null, cache)
|
||
assert.ok(typeof d1 === 'string' && /^M .* C /.test(d1), 'computeEdgePath 应产出 SVG 三次贝塞尔路径(M … C …)')
|
||
assert.strictEqual(cache.size, 1, '首次计算应写入 pathCache')
|
||
|
||
// 缓存命中:同输入第二次必须**真的走 get 命中**(不只是"结果一样")。
|
||
// ⚠️ 这里用**计数代理缓存**:早期版本只断言 cache.size===1,无法区分"命中"与"重算后覆盖同一 key"
|
||
// —— 实测把 `if (hit) return hit` 删掉,size 仍是 1,断言照样通过(假阳性)。
|
||
const counting = {
|
||
_m: new Map(), gets: 0, sets: 0,
|
||
get(k) { this.gets++; return this._m.get(k) },
|
||
set(k, v) { this.sets++; this._m.set(k, v) },
|
||
get size() { return this._m.size },
|
||
clear() { this._m.clear() },
|
||
}
|
||
const c1 = G2.computeEdgePath(edges[0], graph, nodeMap, null, counting)
|
||
assert.strictEqual(counting.sets, 1, '首次计算应写入缓存一次')
|
||
const setsAfterFirst = counting.sets
|
||
const getsAfterFirst = counting.gets
|
||
const c2 = G2.computeEdgePath(edges[0], graph, nodeMap, null, counting)
|
||
assert.strictEqual(c2, c1, '同输入应返回相同路径')
|
||
assert.strictEqual(counting.sets, setsAfterFirst, '同输入**不应再写缓存**(命中快路径直接返回)')
|
||
assert.ok(counting.gets > getsAfterFirst, '同输入应至少查一次缓存(get 被调用)')
|
||
|
||
// 位置变化 ⇒ key 变化 ⇒ 重新计算(不能返回过期路径)
|
||
const nodeMap2 = new Map(nodeMap)
|
||
nodeMap2.set('g1', Object.assign({}, nodeMap.get('g1'), { x: 999 }))
|
||
const d3 = G2.computeEdgePath(edges[0], graph, nodeMap2, null, cache)
|
||
assert.notStrictEqual(d3, d1, '目标节点移动后必须重算路径,不得返回过期缓存')
|
||
assert.strictEqual(cache.size, 2, '位置变化应新增缓存条目')
|
||
|
||
// 端点缺失 ⇒ null(不抛错)
|
||
assert.strictEqual(G2.computeEdgePath({ id: 'ex', from: 'nope', to: 'nada' }, graph, nodeMap, null, new Map()), null,
|
||
'端点缺失时应返回 null 而不是抛错')
|
||
|
||
// ── areNodeEqual:拖拽/缩放窗口内放宽比较 ──
|
||
const base = {
|
||
node: {}, selected: false, draggingNode: null, measuredH: null, busy: {}, progress: {},
|
||
sourceBusy: {}, sourceProgress: {}, connTarget: null, sizing: false, resizing: false, zooming: false,
|
||
edges: [], nodes: [], batchSources: {}, thumbRev: 0, config: {}, lodLow: false,
|
||
}
|
||
assert.strictEqual(G2.areNodeEqual(base, base), true, '同一对象应判定相等(跳过渲染)')
|
||
assert.strictEqual(G2.areNodeEqual(base, Object.assign({}, base, { selected: true })), false,
|
||
'selected 变化必须重渲染')
|
||
|
||
// 关键性能契约:拖拽中即便 edges/nodes 数组引用变了,只要 node 对象没变就跳过渲染
|
||
const dragging = Object.assign({}, base, { dragging: true })
|
||
const dragging2 = Object.assign({}, base, { dragging: true, edges: [{}], nodes: [{}] })
|
||
assert.strictEqual(G2.areNodeEqual(dragging, dragging2), true,
|
||
'拖拽窗口内应放宽 edges/nodes 引用比较(否则大画布每帧重建全部节点体)')
|
||
|
||
// 非拖拽/缩放时,引用变化必须重渲染
|
||
assert.strictEqual(G2.areNodeEqual(base, Object.assign({}, base, { edges: [{}] })), false,
|
||
'非拖拽窗口内 edges 引用变化必须重渲染')
|
||
|
||
// thumbRev 变化必须重渲染(否则缩略图换不上)
|
||
assert.strictEqual(G2.areNodeEqual(base, Object.assign({}, base, { thumbRev: 1 })), false,
|
||
'thumbRev 变化必须重渲染(小图派生完成后节点体要换缩略图)')
|
||
}
|
||
|
||
// ── 屏幕坐标 → 世界坐标(插件拖拽插入:松手点即建节点位置)───────────────────
|
||
{
|
||
const t1 = G.parseWorldTransform('translate(120px, -40px) scale(0.5)')
|
||
assert.strictEqual(t1.x, 120, 'translate x 解析')
|
||
assert.strictEqual(t1.y, -40, 'translate y 解析(可为负)')
|
||
assert.strictEqual(t1.scale, 0.5, 'scale 解析')
|
||
const t0 = G.parseWorldTransform('')
|
||
assert.strictEqual(t0.x + t0.y, 0, '空 transform 回落零平移')
|
||
assert.strictEqual(t0.scale, 1, '空 transform 回落缩放 1')
|
||
assert.strictEqual(G.parseWorldTransform('translate(10px, 10px) scale(0)').scale, 1, 'scale=0 必须回落 1(防除零)')
|
||
// 画布容器左上角在 (300, 0)(左侧停靠了面板),视口平移 (120,-40)、缩放 0.5
|
||
const rect = { left: 300, top: 0 }
|
||
assert.strictEqual(G.clientToWorld(600, 200, rect, t1).x, (600 - 300 - 120) / 0.5, 'x:先扣画布左偏移,再减平移,最后除缩放')
|
||
assert.strictEqual(G.clientToWorld(600, 200, rect, t1).y, (200 - 0 + 40) / 0.5, 'y:同上(平移为负 ⇒ 加上 40)')
|
||
assert.ok(G.clientToWorld(100, 200, rect, t1).x < 0, '松手点落在画布容器左侧 ⇒ 世界 x 为负(由调用方判「不在画布上」)')
|
||
assert.strictEqual(G.clientToWorld(300, 0, rect, { x: 0, y: 0, scale: 2 }).x, 0, '缩放 2 倍时容器左上角仍是世界原点')
|
||
assert.strictEqual(G.clientToWorld(10, 10, null, null).x, 10, '缺参时按单位变换处理(不抛异常)')
|
||
}
|
||
|
||
// ── 拖放落点吸附(松手点压住节点 ⇒ 就近找空位,不硬叠)────────────────────────
|
||
{
|
||
const obstacles = [{ x: 500, y: 500, w: 300, h: 200 }]
|
||
// 空地:原样返回,snapped=false(拖到空处必须精确落在松手点)
|
||
const free = G.snapToFreeSpot({ x: 100, y: 100, w: 300, h: 120 }, obstacles)
|
||
assert.deepStrictEqual({ x: free.x, y: free.y }, { x: 100, y: 100 }, '落点无重叠 ⇒ 坐标不动')
|
||
assert.strictEqual(free.snapped, false, '未吸附时应如实标记 snapped=false')
|
||
// 压在节点上:必须挪开,且与障碍不重叠(含 20px 间隙)
|
||
const hit = G.snapToFreeSpot({ x: 520, y: 520, w: 300, h: 120 }, obstacles)
|
||
assert.strictEqual(hit.snapped, true, '压住节点 ⇒ 必须吸附')
|
||
const overlaps = hit.x < 500 + 300 + 20 && hit.x + 300 + 20 > 500 && hit.y < 500 + 200 + 20 && hit.y + 120 + 20 > 500
|
||
assert.strictEqual(overlaps, false, '吸附后不得与已有节点重叠(含 20px 间隙):' + JSON.stringify(hit))
|
||
assert.ok(Math.hypot(hit.x - 520, hit.y - 520) <= 2 * (Math.max(300, 120) + 20),
|
||
'应就近吸附(不超过一个环的步长),不能跳到很远:' + JSON.stringify(hit))
|
||
// bounds 优先:近处候选在视口外时,宁可稍远也要落在视口内
|
||
const bounded = G.snapToFreeSpot({ x: 520, y: 520, w: 300, h: 120 }, obstacles,
|
||
{ gap: 20, bounds: { minX: 0, minY: 0, maxX: 900, maxY: 800 } })
|
||
assert.ok(bounded.x >= 0 && bounded.y >= 0 && bounded.x <= 900 && bounded.y <= 800,
|
||
'bounds 内结果必须落在视口范围内:' + JSON.stringify(bounded))
|
||
// 幂等:对吸附结果再吸附一次不得再挪(否则拖两次会漂)
|
||
const again = G.snapToFreeSpot({ x: bounded.x, y: bounded.y, w: 300, h: 120 }, obstacles,
|
||
{ gap: 20, bounds: { minX: 0, minY: 0, maxX: 900, maxY: 800 } })
|
||
assert.deepStrictEqual({ x: again.x, y: again.y, s: again.snapped }, { x: bounded.x, y: bounded.y, s: false },
|
||
'已吸附过的位置再吸附应原地不动(幂等)')
|
||
// 参数缺失/非法 ⇒ 原样返回,不抛错(插件可能传半截坐标)
|
||
const bad = G.snapToFreeSpot({ x: NaN, y: 10, w: 3, h: 3 }, obstacles)
|
||
assert.ok(Number.isNaN(bad.x) && bad.y === 10 && bad.snapped === false, '非法坐标应原样返回')
|
||
assert.strictEqual(G.snapToFreeSpot(null, null).snapped, false, '空入参不抛异常')
|
||
assert.strictEqual(G.snapToFreeSpot({ x: 520, y: 520, w: 300, h: 120 }, null).snapped, false,
|
||
'无障碍物时永不吸附')
|
||
}
|
||
|
||
// ── 复制落位(右侧优先、零重叠)──────────────────────────────────────────────
|
||
// 守的是一条用户可见契约:复制出来的节点**不得压住原节点**(旧实现固定 56px 对角偏移 +
|
||
// 重叠检测排除源节点 ⇒ 480 宽的节点被盖住 88%,复制体又在上层,原节点只剩细边可点)。
|
||
{
|
||
assert.ok(typeof G.placeBeside === 'function', 'placeBeside 应导出')
|
||
const box = { x: 400, y: 100, w: 480, h: 460 }
|
||
const self = { x: 400, y: 100, w: 480, h: 460 }
|
||
const over = (a, b, gap) => a.x < b.x + b.w + gap && a.x + a.w + gap > b.x && a.y < b.y + b.h + gap && a.y + a.h + gap > b.y
|
||
|
||
// ① 右侧空地 ⇒ 贴右侧,间距 20(用户选的默认)
|
||
const right = G.placeBeside(box, [self], { gap: 20 })
|
||
assert.deepStrictEqual({ x: right.x, y: right.y }, { x: 900, y: 100 }, '右侧空地应贴右侧(x = 原 x + 宽 + 20,y 对齐)')
|
||
assert.strictEqual(over({ ...right, w: 480, h: 460 }, self, 20), false, '右侧落点不得与原节点重叠(含 20px 间隙)')
|
||
|
||
// ② 右侧被占 ⇒ 落到下方(不是继续往右推,也不是叠上去)
|
||
const blocker = { x: 900, y: 100, w: 480, h: 460 }
|
||
const below = G.placeBeside(box, [self, blocker], { gap: 20 })
|
||
assert.deepStrictEqual({ x: below.x, y: below.y }, { x: 400, y: 580 }, '右侧被占应落到正下方(y = 原 y + 高 + 20)')
|
||
assert.strictEqual(over({ ...below, w: 480, h: 460 }, self, 20), false, '下方落点不得与原节点重叠')
|
||
|
||
// ③ 观感:不重叠 ≠ 离得远。落点必须紧贴(一格之内),否则用户又找不到复制体
|
||
assert.ok(Math.abs(below.y - (self.y + self.h)) <= 20, '下方落点应紧贴原节点(间隙就是 gap)')
|
||
|
||
// ④ 八方向全占 ⇒ 环形兜底,且仍然零重叠(不能退化成"随便叠上去")
|
||
const ring = [
|
||
self,
|
||
{ x: 900, y: 100, w: 480, h: 460 }, { x: 400, y: 580, w: 480, h: 460 },
|
||
{ x: -100, y: 100, w: 480, h: 460 }, { x: 400, y: -380, w: 480, h: 460 },
|
||
{ x: 900, y: 580, w: 480, h: 460 }, { x: -100, y: 580, w: 480, h: 460 },
|
||
{ x: 900, y: -380, w: 480, h: 460 }, { x: -100, y: -380, w: 480, h: 460 },
|
||
]
|
||
const far = G.placeBeside(box, ring, { gap: 20 })
|
||
const farBox = { x: far.x, y: far.y, w: 480, h: 460 }
|
||
assert.strictEqual(ring.some((o) => over(farBox, o, 20)), false, '兜底落点也不得与任何节点重叠:' + JSON.stringify(far))
|
||
assert.strictEqual(over(farBox, self, 20), false, '兜底落点尤其不得压住原节点')
|
||
|
||
// ⑤ bounds:右侧虽空但出了视口 ⇒ 宁可放下方(复制体必须落在看得见的地方)
|
||
const bounded = G.placeBeside(box, [self], { gap: 20, bounds: { minX: 0, minY: 0, maxX: 850, maxY: 2000 } })
|
||
assert.deepStrictEqual({ x: bounded.x, y: bounded.y }, { x: 400, y: 580 }, '右侧出视口时应改放下方(界内优先)')
|
||
|
||
// ⑥ 组复制:锚点是整组包围盒 ⇒ 组内相对位置不变,整体让开(不按成员逐个错位)
|
||
const groupBox = { x: 100, y: 100, w: 1200, h: 460 }
|
||
const groupSpot = G.placeBeside(groupBox, [{ x: 100, y: 100, w: 1200, h: 460 }], { gap: 20 })
|
||
assert.deepStrictEqual({ x: groupSpot.x, y: groupSpot.y }, { x: 1320, y: 100 }, '组复制应按包围盒整体贴右侧,而不是按单个成员算')
|
||
assert.strictEqual(groupSpot.x - groupBox.x, 1220, '组内每个成员共用同一个 dx ⇒ 相对位置不变')
|
||
|
||
// ⑦ 非法/缺失入参不抛错(插件与旧存档都可能传半截几何)
|
||
assert.strictEqual(G.placeBeside(null, null).placed, false, '空入参不抛异常')
|
||
assert.strictEqual(G.placeBeside({ x: NaN, y: 10, w: 100, h: 100 }).placed, false, '非法坐标应回落 placed=false')
|
||
assert.strictEqual(G.placeBeside(box, null, { gap: 20 }).placed, true, '无障碍物时也应给出落点')
|
||
}
|
||
|
||
// ── 测量高度表裁剪(换画板)──────────────────────────────────────────────────
|
||
// 守的是一条隐蔽但影响面很大的契约:换画板时**不能整表清空**。NodeCard 的高度测量 effect
|
||
// 依赖数组为空(只在挂载时跑一次),同 id 节点不重新挂载、ResizeObserver 也不因高度未变而回调
|
||
// ⇒ 清空即永久为空,端口/连线/适应视图/复制落点全部退化成默认高度(实测提示词 216 vs 实际 89)。
|
||
{
|
||
assert.ok(typeof G.pruneMeasured === 'function', 'pruneMeasured 应导出')
|
||
const mh = { a: 89, b: 216, gone: 300 }
|
||
const ret = G.pruneMeasured(mh, [{ id: 'a' }, { id: 'b' }, { id: 'c' }])
|
||
assert.strictEqual(ret, mh, '必须原地返回同一对象(session.measuredH 与 refs.measuredH 共享引用)')
|
||
assert.deepStrictEqual(Object.keys(mh).sort(), ['a', 'b'], '只删不属于当前画板的 id')
|
||
assert.strictEqual(mh.a, 89, '保留项的值不变')
|
||
assert.deepStrictEqual(G.pruneMeasured({ x: 1 }, null), { x: 1 }, '节点表缺失时不误删(首屏装载)')
|
||
assert.deepStrictEqual(G.pruneMeasured({}, [{ id: 'a' }]), {}, '空表不抛错')
|
||
assert.strictEqual(G.pruneMeasured(null, [{ id: 'a' }]), null, '空入参不抛错')
|
||
}
|
||
|
||
// ── 粘贴落点:指针 = 内容左上角(v3.24.74)──────────────────────────────────
|
||
// 守的是「落点不再依赖高度估算」这件事。旧语义把 seed 当**中心**,而高度取 nodeTypeH
|
||
// 的估算值(prompt 216 / 空节点实测 ~90)⇒ 视觉中心恒定偏上 63px。
|
||
// 判据直接绑几何关系:中心 - 一半宽高 === 原始指针点(与 h 取多少无关)。
|
||
// ⚠️ 用字段级 strictEqual 而不是 deepStrictEqual:对象来自 vm 沙箱 realm,原型不同,深比较会假红。
|
||
const near = (name, got, wantX, wantY) => {
|
||
assert.strictEqual(got.x, wantX, name + ' x')
|
||
assert.strictEqual(got.y, wantY, name + ' y')
|
||
}
|
||
{
|
||
assert.ok(typeof G.seedFromTopLeft === 'function', 'seedFromTopLeft 应导出')
|
||
const tl = { x: 1000, y: 800 }
|
||
near('中心 = 左上角 + 半宽半高', G.seedFromTopLeft(tl, 264, 216), 1132, 908)
|
||
near('高度不同只影响 y,不引入 offset', G.seedFromTopLeft({ x: 1000, y: 800 }, 264, 89), 1132, 844.5)
|
||
// 反算:中心 - 半宽半高 必须回到指针本身(这就是"消除偏差"的形式化说法)
|
||
for (const h of [89, 216, 400]) {
|
||
const m = G.seedFromTopLeft(tl, 264, h)
|
||
assert.strictEqual(m.x - 132, 1000, 'x 不随高度变')
|
||
assert.strictEqual(m.y - h / 2, 800, 'y 反算回指针(h=' + h + ')')
|
||
}
|
||
const bad = G.seedFromTopLeft({ x: NaN, y: 1 }, 264, 216)
|
||
assert.ok(Number.isNaN(bad.x) && bad.y === 1, '非有限坐标原样返回(交给调用方兜底)')
|
||
assert.strictEqual(G.seedFromTopLeft(undefined, 264, 216), undefined, '缺 seed 原样返回')
|
||
near('缺 w/h 当作 0,不产出 NaN', G.seedFromTopLeft({ x: 10, y: 20 }), 10, 20)
|
||
}
|
||
|
||
// ── 视口夹取(v3.24.74)──────────────────────────────────────────────────────
|
||
// 落点跟鼠标之后,在窗口右下角粘贴会把节点推出视口。这条守「可见优先」:
|
||
// 盒子里至少左上角必须在界内;盒子比视口还大时贴 minX/minY(否则整块看不见)。
|
||
{
|
||
assert.ok(typeof G.clampIntoBounds === 'function', 'clampIntoBounds 应导出')
|
||
const bounds = { minX: -200, minY: -100, maxX: 800, maxY: 600 }
|
||
near('界内不动', G.clampIntoBounds({ x: 100, y: 100, w: 264, h: 216 }, bounds), 100, 100)
|
||
near('右下溢出 → 贴右/下边(maxX - w)', G.clampIntoBounds({ x: 790, y: 590, w: 264, h: 216 }, bounds), 536, 384)
|
||
near('左上溢出 → 贴 minX/minY', G.clampIntoBounds({ x: -900, y: -900, w: 264, h: 216 }, bounds), -200, -100)
|
||
near('盒子比视口大 → 贴左上,宁可右/下溢出', G.clampIntoBounds({ x: 0, y: 0, w: 2000, h: 2000 }, bounds), -200, -100)
|
||
near('缺 bounds 不动', G.clampIntoBounds({ x: 100, y: 100, w: 264, h: 216 }, {}), 100, 100)
|
||
near('结果取整(与 findSpot 的 Math.round 同口径)', G.clampIntoBounds({ x: 1.6, y: 2.4, w: 10, h: 10 }, bounds), 2, 2)
|
||
const badBox = G.clampIntoBounds({ x: NaN, y: 0, w: 10, h: 10 }, bounds)
|
||
assert.ok(Number.isNaN(badBox.x) && badBox.y === 0, '非有限坐标不解构、不抛错')
|
||
const empty = G.clampIntoBounds(null, bounds)
|
||
assert.strictEqual(empty.x, undefined, '空入参不抛错')
|
||
}
|
||
|
||
// ── LOD 判据(v3.24.77):按节点**屏幕宽度**,不是全局 scale ────────────────────
|
||
// 守的是「大画布 fit 视图不渲染看不见的细节」这条性能契约(实测切画布 488 → 198ms)。
|
||
// 判据错的两个方向都必须拦住:
|
||
// · 太激进(阈值过大)→ 正常编辑档位(scale 0.5)就把节点变成灰块 —— 实测用 96px 撞到过,当场回调;
|
||
// · 太保守(阈值过小 / 退回全局 scale<0.05)→ 800 节点 fit(scale 0.2)不生效,切画布回到 488ms。
|
||
{
|
||
assert.ok(typeof G.lodLowOf === 'function', 'lodLowOf 应导出')
|
||
assert.strictEqual(G.LOD_NODE_PX, 64, '阈值 64px:264 宽节点约 scale 0.24 才进 LOD')
|
||
// 与 app.js 的 nodeWidth 同口径(这里模拟,避免测试依赖宿主)
|
||
const W = (n) => (n.type === 'relay' ? 72 : (n.w || (n.type === 'gen' ? 480 : (n.type === 'layer' ? 520 : 264))))
|
||
const prompt = { type: 'prompt' }
|
||
const narrow = { type: 'prompt', w: 160 } // 用户手动调窄的提示词节点(实测里就是它把 96 阈值打穿的)
|
||
|
||
// 必须进 LOD:大画布 fit 视图(264 × 0.2 = 52.8px)
|
||
assert.strictEqual(G.lodLowOf({ scale: 0.2 }, prompt, W), true, '800 节点 fit 视图必须进 LOD')
|
||
// 必须完整渲染:正常编辑档位
|
||
assert.strictEqual(G.lodLowOf({ scale: 1 }, prompt, W), false, 'scale 1 绝不 LOD')
|
||
assert.strictEqual(G.lodLowOf({ scale: 0.5 }, prompt, W), false, 'scale 0.5 默认宽 132px 不该变灰块')
|
||
assert.strictEqual(G.lodLowOf({ scale: 0.5 }, narrow, W), false, 'scale 0.5 窄节点 80px 仍在阈值上(96px 阈值会在这里误伤)')
|
||
// 边界两侧(64 / 264 = 0.2424…)
|
||
assert.strictEqual(G.lodLowOf({ scale: 0.242 }, prompt, W), true, '63.9px 低于阈值 → LOD')
|
||
assert.strictEqual(G.lodLowOf({ scale: 0.243 }, prompt, W), false, '64.2px 高于阈值 → 完整渲染')
|
||
// 宽节点按自己的宽度判(480 × 0.13 = 62.4 → LOD;520 × 0.12 = 62.4 → LOD)
|
||
assert.strictEqual(G.lodLowOf({ scale: 0.13 }, { type: 'gen' }, W), true, 'gen 节点按 480 判,不看别的节点')
|
||
assert.strictEqual(G.lodLowOf({ scale: 0.12 }, { type: 'layer' }, W), true)
|
||
// 中继节点豁免:节点体就一个圆环,是连线锚点,变空白会让画布"看起来断线"
|
||
assert.strictEqual(G.lodLowOf({ scale: 0.02 }, { type: 'relay' }, W), false, 'relay 永不 LOD')
|
||
// 异常入参一律「宁可完整渲染」(判错方向不能是把内容藏掉)
|
||
assert.strictEqual(G.lodLowOf(null, prompt, W), false)
|
||
assert.strictEqual(G.lodLowOf({ scale: NaN }, prompt, W), false)
|
||
assert.strictEqual(G.lodLowOf({ scale: 0.2 }, null, W), false)
|
||
assert.strictEqual(G.lodLowOf({ scale: 0.2 }, prompt, () => 0), false, '宽度取不到 → 不 LOD')
|
||
assert.strictEqual(G.lodLowOf({ scale: 0.2 }, prompt), false, '没传 nodeWidth → 不 LOD')
|
||
}
|
||
|
||
// ── LOD 两档(v3.24.78):'' 完整 / 'lite' 缩略图或首行 / 'plain' 纯占位 ────────────
|
||
// 用户实测反馈「图片简化后无法判断节点内容」⇒ 只有缩到 15% 以下才允许退成灰块;
|
||
// 15% 以上的 LOD 必须画内容(lite)。两个方向都守:
|
||
// · 太早变灰块(lite 直接返回 plain,或 LOD_PLAIN_SCALE 调到 0.2 以上)→ 打红;
|
||
// · 该省的没省(lodModeOf 恒返回 '')→ 打红。
|
||
{
|
||
assert.ok(typeof G.lodModeOf === 'function', 'lodModeOf 应导出')
|
||
assert.strictEqual(G.LOD_PLAIN_SCALE, 0.15, '纯占位起点 = 全局缩放 15%(用户明确要求)')
|
||
const W = (n) => (n.type === 'relay' ? 72 : (n.w || (n.type === 'gen' ? 480 : (n.type === 'layer' ? 520 : 264))))
|
||
const prompt = { type: 'prompt' }
|
||
// 800 节点 fit(scale 0.2、默认宽 264 → 52.8px):≥15% ⇒ lite(有内容),不是灰块
|
||
assert.strictEqual(G.lodModeOf({ scale: 0.2 }, prompt, W), 'lite', '0.2 档必须是 lite:用户要能判断节点内容')
|
||
// 低于 15% ⇒ plain
|
||
assert.strictEqual(G.lodModeOf({ scale: 0.14 }, prompt, W), 'plain', '14% 该退成占位')
|
||
assert.strictEqual(G.lodModeOf({ scale: 0.149 }, prompt, W), 'plain', '14.9% 仍在占位档')
|
||
// 15% 边界是开区间:正好 0.15 不算「以下」
|
||
assert.strictEqual(G.lodModeOf({ scale: 0.15 }, prompt, W), 'lite', '0.15 本身不该被算作「以下」')
|
||
// 正常编辑档位完整渲染
|
||
assert.strictEqual(G.lodModeOf({ scale: 1 }, prompt, W), '', 'scale 1 完整渲染')
|
||
assert.strictEqual(G.lodModeOf({ scale: 0.5 }, prompt, W), '', 'scale 0.5 完整渲染')
|
||
// plain 只看全局缩放:足够宽的节点在 10% 下也算「缩小到看不清」,照样占位
|
||
assert.strictEqual(G.lodModeOf({ scale: 0.1 }, { type: 'prompt', w: 1200 }, W), 'plain', 'plain 不受节点宽度影响')
|
||
// 中继节点两档都豁免
|
||
assert.strictEqual(G.lodModeOf({ scale: 0.02 }, { type: 'relay' }, W), '', 'relay 永不降级')
|
||
// 异常入参:宁可完整渲染
|
||
assert.strictEqual(G.lodModeOf(null, prompt, W), '')
|
||
assert.strictEqual(G.lodModeOf({ scale: NaN }, prompt, W), '')
|
||
assert.strictEqual(G.lodModeOf({ scale: 0.2 }, null, W), '')
|
||
}
|
||
|
||
// ── 底栏「−/+」缩放(v3.24.83)────────────────────────────────────────────
|
||
// 断言四件事:① 一档 15%;② 锚点是画布中心(中心那点内容不动);③ 上下限夹住;
|
||
// ④ 顶到边界后视图**完全不动**(否则按一下画面会跳)。
|
||
{
|
||
const CW = 1000, CH = 800
|
||
const cx = CW / 2, cy = CH / 2
|
||
// 中心锚定检查:世界坐标 (wx,wy) 的点,缩放前后屏幕位置应一致
|
||
const screenOf = (v, wx, wy) => ({ x: v.x + wx * v.scale, y: v.y + wy * v.scale })
|
||
const v0 = { x: -300, y: -200, scale: 1 }
|
||
// 当前落在屏幕中心的那一点(世界坐标),缩放后必须还落在屏幕中心
|
||
const wx = (cx - v0.x) / v0.scale, wy = (cy - v0.y) / v0.scale
|
||
const c0 = screenOf(v0, wx, wy)
|
||
const vIn = G.zoomStep(v0, 1, CW, CH)
|
||
assert.strictEqual(Math.round(vIn.scale * 100), 115, '放大一档 = ×1.15')
|
||
const c1 = screenOf(vIn, wx, wy)
|
||
assert.ok(Math.abs(c1.x - c0.x) < 0.001 && Math.abs(c1.y - c0.y) < 0.001,
|
||
'放大后画布中心那一点必须留在原地(锚点 = 中心,不是左上角)')
|
||
const vOut = G.zoomStep(vIn, -1, CW, CH)
|
||
assert.strictEqual(Math.round(vOut.scale * 100), 100, '缩小一档回得去(1.15 与 1/1.15 互逆)')
|
||
// 上下限
|
||
assert.strictEqual(G.zoomStep({ x: 0, y: 0, scale: 3 }, 1, CW, CH).scale, G.ZOOM_MAX, '上限 3')
|
||
assert.strictEqual(G.zoomStep({ x: 0, y: 0, scale: 0.01 }, -1, CW, CH).scale, G.ZOOM_MIN, '下限 0.01')
|
||
// 顶到上限:位置也不许动(k===1 早退)
|
||
assert.strictEqual(JSON.stringify(G.zoomStep({ x: 12, y: 34, scale: 3 }, 1, CW, CH)), '{"x":12,"y":34,"scale":3}')
|
||
// 不会越过上限:从 2.9 再放大一次直接落到 3,不会变成 3.335
|
||
assert.strictEqual(G.zoomStep({ x: 0, y: 0, scale: 2.9 }, 1, CW, CH).scale, G.ZOOM_MAX, '不得越过上限')
|
||
// 异常入参:缺 viewport / 缺尺寸时给安全默认,不产生 NaN
|
||
const nan = G.zoomStep(null, 1, 0, 0)
|
||
assert.ok(Number.isFinite(nan.scale) && Number.isFinite(nan.x) && Number.isFinite(nan.y), '入参缺失不得产出 NaN')
|
||
}
|
||
|
||
// ── 2026-11-30:边线 SVG 放在屏幕坐标系(不能按世界尺寸分配合成位图)──
|
||
{
|
||
const size = G.edgeLayerSize({ w: 1000, h: 800 })
|
||
assert.strictEqual(size.w, 1000)
|
||
assert.strictEqual(size.h, 800)
|
||
const props = G.edgeLayerProps({ w: 1000, h: 800 })
|
||
assert.strictEqual(props.width, 1000)
|
||
assert.strictEqual(props.height, 800)
|
||
assert.strictEqual(props.viewBox, '0 0 1000 800')
|
||
assert.strictEqual(G.edgeGroupTransform({ x: -100, y: -50, scale: 0.5 }), 'translate(-100,-50) scale(0.5)')
|
||
const rects = G.cullRectsOf({ w: 800, h: 600 }, { x: 0, y: 0, scale: 1 })
|
||
const far = { id: 'far', type: 'ref', x: 5000, y: 5000, w: 200, h: 200 }
|
||
assert.strictEqual(G.nodeVisibleIn(far, rects, { widthOf: (n) => n.w, heightOf: (n) => n.h }), false)
|
||
assert.strictEqual(G.nodeVisibleIn(far, rects, { pan: true, widthOf: (n) => n.w, heightOf: (n) => n.h }), true, '平移期间必须全量渲染')
|
||
const fake = { attrs: {}, setAttribute(k, v) { this.attrs[k] = v } }
|
||
G.applyEdgeLayerDom({ querySelector: (s) => (s === '.iwb-edge-world' ? fake : null) }, { w: 1200, h: 900 }, 120, 80, 2)
|
||
assert.strictEqual(fake.attrs.transform, 'translate(120,80) scale(2)')
|
||
}
|
||
|
||
console.log('canvas geometry checks passed(含 computeEdgePath 缓存快路径、areNodeEqual 拖拽放宽、拖放落点吸附、复制落位零重叠、粘贴左上角落点与视口夹取、LOD 屏幕宽度判据、边线视口层的行为验证)')
|