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

23 lines
1.2 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 不参与 Canvas V3 命中')
assert.strictEqual(H.hitRefNode(nodes, 500, 500, opts), null, '空白不命中')
console.log('canvas-v3-hit checks passed')