Files
image-workbench-desktop/tests/canvas-v3-hit-test.js
T

27 lines
1.5 KiB
JavaScript

'use strict'
const assert = require('assert')
const fs = require('fs')
const path = require('path')
const vm = require('vm')
const sandbox = { window: {} }
vm.createContext(sandbox)
vm.runInContext(fs.readFileSync(path.join(__dirname, '..', 'src', 'canvas-v3-hit.js'), 'utf8'), sandbox)
const H = sandbox.window.IWBCanvasV3Hit
const nodes = [
{ id: 'a', type: 'ref', x: 0, y: 0, w: 200, h: 100 },
{ id: 'b', type: 'prompt', x: 300, y: 0, w: 200, h: 100 },
{ id: 'c', type: 'ref', x: 0, y: 0, w: 200, h: 100 },
]
const opts = { widthOf: (n) => n.w, heightOf: (n) => n.h, portRadiusWorld: 10 }
assert.strictEqual(H.hitRefNode(nodes, 50, 50, opts).node.id, 'c', '后渲染的 ref 优先命中')
assert.strictEqual(H.hitRefNode(nodes, 50, 50, opts).port, '', '卡内是 body')
assert.strictEqual(H.hitRefNode(nodes, -5, 50, opts).port, 'in', '左边缘是输入口')
assert.strictEqual(H.hitRefNode(nodes, 205, 50, opts).port, 'out', '右边缘是输出口')
assert.strictEqual(H.hitRefNode(nodes, 400, 50, opts), null, '非 ref 不参与 hitRefNode')
assert.strictEqual(H.hitRefNode(nodes, 500, 500, opts), null, '空白不命中')
const hitAny = H.hitNode(nodes, 400, 50, Object.assign({}, opts, { isType: (n) => n.type === 'prompt' || n.type === 'ref', wantPorts: false }))
assert.strictEqual(hitAny.node.id, 'b', 'hitNode 按 isType 命中 prompt')
assert.strictEqual(hitAny.port, '', 'prompt 只做 body 命中')
console.log('canvas-v3-hit types checks passed')
console.log('canvas-v3-hit checks passed')