根因(4+1 条同一族):右键菜单是「主菜单 + 子菜单」两层,两者都带 .iwb-ctxmenu, 裸选择器在 strict mode 下命中 2 个元素 ⇒ toBeVisible 直接失败。 - private-gate:openCtxMenu / layerCtxItem 改用 .iwb-ctxmenu:not(.iwb-ctx-sub) - connect-menu ⑤:同样收窄(与 canvas-ops 里既有写法一致) 另:设置面板默认页已是「通用」,分层模型区块在「生图服务」页 ⇒ 补一次切页; 区块标题由「分层模型(…)」简化为「分层模型」,断言改用 .iwb-gset-group-h。 验证:private-gate 4/4 + connect-menu 9/9 全绿(此前 4 + 1 红)。 基线:已知红 25 → 20 条。
296 lines
15 KiB
JavaScript
296 lines
15 KiB
JavaScript
// 拖线建节点菜单(2026-10-30)
|
||
//
|
||
// 契约:
|
||
// ① 从输出端口拖到**画布空白处**松手 ⇒ 弹「引用该节点生成」菜单,菜单项由连接合法性表实时算出
|
||
// ② 点某一项 ⇒ 在落点建该类型节点 + **自动连边** + 选中新节点 + 一步撤销可整体回退
|
||
// ③ 拖到**合法节点**上 ⇒ 照旧直接连边,不弹菜单
|
||
// ④ Esc 关闭菜单,不残留预览线
|
||
// ⑤ 分层(layer)/润色(llm) 默认隐藏:底栏、右键菜单、拖线菜单都不出现
|
||
const { test, expect } = require('./_pw-coverage')
|
||
|
||
const baseURL = process.env.IWB_BASE_URL || 'http://127.0.0.1:8787'
|
||
test.use({ baseURL, viewport: { width: 1440, height: 1000 } })
|
||
|
||
const BOARD = {
|
||
id: 'ws_cm', name: 'CONNECT', edges: [], groups: [],
|
||
nodes: [{ id: 'p1', type: 'prompt', x: 200, y: 300, data: { text: 'hello' } }],
|
||
}
|
||
const WS = { workspaces: [BOARD], activeId: 'ws_cm', rev: 1, schemaVersion: 1 }
|
||
|
||
// ③ 专用:两个未连接的节点(位置固定,便于把线精确拖到输入端口)
|
||
const BOARD3 = {
|
||
id: 'ws_cm3', name: 'CONNECT3', edges: [], groups: [],
|
||
nodes: [
|
||
{ id: 'p1', type: 'prompt', x: 200, y: 300, data: { text: 'hello' } },
|
||
{ id: 'g1', type: 'combine', x: 760, y: 300, data: {} },
|
||
],
|
||
}
|
||
|
||
// 播种:**不用 addInitScript**(coverage 版复用浏览器上下文,init 脚本会跨用例累积互相覆盖)。
|
||
// 改为「先加载 → 写入 localStorage → reload」,保证每个用例拿到干净且确定的画布。
|
||
async function seedWith(page, ws) {
|
||
await page.goto('/', { waitUntil: 'domcontentloaded' })
|
||
await page.evaluate((w) => {
|
||
localStorage.clear()
|
||
localStorage.setItem('iwb_web_workspaces', JSON.stringify(w))
|
||
const b = (w.workspaces || [])[0] || {}
|
||
localStorage.setItem('iwb_ws_active', w.activeId)
|
||
localStorage.setItem('iwb_ws_' + w.activeId, JSON.stringify(b))
|
||
}, ws)
|
||
await page.reload({ waitUntil: 'domcontentloaded' })
|
||
await page.waitForSelector('.iwb-home-card:not(.iwb-home-new)', { timeout: 20000 })
|
||
await page.locator('.iwb-home-card:not(.iwb-home-new)').first().dblclick()
|
||
await page.waitForSelector('.iwb-canvas-topbar', { timeout: 20000 })
|
||
await page.waitForTimeout(600)
|
||
}
|
||
|
||
async function seed(page) {
|
||
await seedWith(page, WS)
|
||
}
|
||
|
||
// 从某个节点的输出端口拖到屏幕坐标 (x, y)
|
||
async function dragFromPort(page, nodeId, x, y) {
|
||
const port = page.locator(`[data-node-id="${nodeId}"] .iwb-port-out`).first()
|
||
await expect(port).toBeVisible({ timeout: 15000 })
|
||
const b = await port.boundingBox()
|
||
await page.mouse.move(b.x + b.width / 2, b.y + b.height / 2)
|
||
await page.mouse.down()
|
||
await page.mouse.move(x, y, { steps: 10 })
|
||
await page.mouse.up()
|
||
await page.waitForTimeout(260)
|
||
}
|
||
|
||
async function nodeCount(page) {
|
||
return page.locator('.iwb-node').count()
|
||
}
|
||
|
||
// 画布图(节点/边)存在每个画布独立的 key `iwb_ws_<id>`;`iwb_web_workspaces` 只存列表与 nodeCount。
|
||
async function readGraph(page) {
|
||
return page.evaluate(() => {
|
||
const act = localStorage.getItem('iwb_ws_active') || ''
|
||
const raw = localStorage.getItem('iwb_ws_' + act)
|
||
if (!raw) return { nodes: [], edges: [] }
|
||
try {
|
||
const g = JSON.parse(raw)
|
||
return { nodes: g.nodes || [], edges: g.edges || [] }
|
||
} catch (e) {
|
||
return { nodes: [], edges: [] }
|
||
}
|
||
})
|
||
}
|
||
|
||
async function edgeCount(page) {
|
||
const g = await readGraph(page)
|
||
return g.edges.length
|
||
}
|
||
|
||
test('① 拖到空白处 ⇒ 弹出「引用该节点生成」,且只列出可连类型', async ({ page }) => {
|
||
await seed(page)
|
||
const canvas = await page.locator('.iwb-canvas').boundingBox()
|
||
await dragFromPort(page, 'p1', canvas.x + canvas.width * 0.75, canvas.y + canvas.height * 0.75)
|
||
|
||
const menu = page.locator('.iwb-connect-menu')
|
||
await expect(menu, '空白处松手应弹出建节点菜单').toBeVisible({ timeout: 8000 })
|
||
await expect(menu.locator('.iwb-ctx-title')).toHaveText('引用该节点生成')
|
||
|
||
// 菜单项 = 合法性表算出来的,必须是"提示词节点的合法下游"
|
||
const types = await menu.locator('.iwb-ctx-item[data-type]').evaluateAll((els) => els.map((e) => e.getAttribute('data-type')))
|
||
expect(types.length, '菜单不应为空').toBeGreaterThan(0)
|
||
for (const t of ['gen', 'combine', 'batch', 'relay']) {
|
||
expect(types, `「${t}」应可连`).toContain(t)
|
||
}
|
||
expect(types, '文本节点只接润色/组合/连接 ⇒ 提示词不能直连文本').not.toContain('textout')
|
||
expect(types, '插件节点需变体数据,不出现在拖线菜单').not.toContain('plugin')
|
||
// 图片节点只能作来源、提示词节点只能接参考图/中继 ⇒ 都不该出现
|
||
expect(types, '图片节点不接受连线,不应出现在菜单里').not.toContain('ref')
|
||
expect(types, '提示词节点不能接提示词,不应出现在菜单里').not.toContain('prompt')
|
||
// 分层/润色默认隐藏(私有开关关闭)
|
||
expect(types, '分层默认隐藏').not.toContain('layer')
|
||
expect(types, '润色默认隐藏').not.toContain('llm')
|
||
})
|
||
|
||
test('② 点「生图」⇒ 落点建节点 + 自动连边 + 选中 + 一步撤销', async ({ page }) => {
|
||
await seed(page)
|
||
const before = await nodeCount(page)
|
||
const canvas = await page.locator('.iwb-canvas').boundingBox()
|
||
const dropX = canvas.x + canvas.width * 0.7
|
||
const dropY = canvas.y + canvas.height * 0.7
|
||
await dragFromPort(page, 'p1', dropX, dropY)
|
||
|
||
const menu = page.locator('.iwb-connect-menu')
|
||
await expect(menu).toBeVisible({ timeout: 8000 })
|
||
await menu.locator('.iwb-ctx-item[data-type="gen"]').click()
|
||
await expect(menu).toHaveCount(0)
|
||
|
||
// 新节点出现且被选中
|
||
await expect(page.locator('.iwb-node')).toHaveCount(before + 1)
|
||
await expect(page.locator('.iwb-node-selected').first()).toBeVisible({ timeout: 5000 }).catch(() => null)
|
||
|
||
// 自动连边:工作台里应有 1 条 p1 → 新节点 的边
|
||
await expect.poll(async () => edgeCount(page), { timeout: 8000 }).toBe(1)
|
||
const edge = (await readGraph(page)).edges[0] || null
|
||
expect(edge.from, '边应从源节点出发').toBe('p1')
|
||
expect(edge.to, '边应连到新节点').toBeTruthy()
|
||
|
||
// 一步撤销:节点与边一起回退
|
||
await page.keyboard.press('Control+z')
|
||
await expect.poll(async () => nodeCount(page), { timeout: 8000 }).toBe(before)
|
||
await expect.poll(async () => edgeCount(page), { timeout: 8000 }).toBe(0)
|
||
})
|
||
|
||
test('③ 拖到节点上 ⇒ 不弹菜单(不干扰原有连线手势)', async ({ page }) => {
|
||
// 关键回归点:新菜单只能在"空白处"出现,落在节点上时必须保持原有连线行为。
|
||
// ("拖到节点仍能连边"由 jsdom 连线用例覆盖:tests/iwb-v32-web-test.js 的 conn40 系列)
|
||
await seed(page)
|
||
await page.locator('.iwb-toolbar-bottom .iwb-btn[data-tip="组合"]').click()
|
||
await page.waitForTimeout(500)
|
||
|
||
const newId = await expect.poll(async () => {
|
||
const g = await readGraph(page)
|
||
const n = (g.nodes || []).find((x) => x.type === 'combine')
|
||
return n ? n.id : ''
|
||
}, { timeout: 8000 }).not.toBe('').then(() => readGraph(page)).then((g) => (g.nodes || []).find((x) => x.type === 'combine').id)
|
||
const target = page.locator(`[data-node-id="${newId}"]`).first()
|
||
await expect(target).toHaveCount(1, { timeout: 8000 })
|
||
|
||
const box = await target.boundingBox()
|
||
const before = await nodeCount(page)
|
||
await dragFromPort(page, 'p1', box.x + box.width / 2, box.y + box.height / 2)
|
||
await expect(page.locator('.iwb-connect-menu'), '落在节点上不应弹菜单').toHaveCount(0)
|
||
expect(await nodeCount(page), '不应额外新建节点').toBe(before)
|
||
})
|
||
|
||
test('④ Esc 关闭菜单且不残留预览线', async ({ page }) => {
|
||
await seed(page)
|
||
const canvas = await page.locator('.iwb-canvas').boundingBox()
|
||
await dragFromPort(page, 'p1', canvas.x + canvas.width * 0.7, canvas.y + canvas.height * 0.7)
|
||
await expect(page.locator('.iwb-connect-menu')).toBeVisible({ timeout: 8000 })
|
||
await page.keyboard.press('Escape')
|
||
await expect(page.locator('.iwb-connect-menu')).toHaveCount(0)
|
||
// 不应误建节点/边
|
||
expect(await nodeCount(page)).toBe(1)
|
||
expect(await edgeCount(page)).toBe(0)
|
||
})
|
||
|
||
test('⑤ 分层/润色默认隐藏:底栏与右键菜单都没有新建入口', async ({ page }) => {
|
||
await seed(page)
|
||
// 底栏
|
||
await expect(page.locator('.iwb-toolbar-bottom .iwb-btn[data-tip="润色"]')).toHaveCount(0)
|
||
await expect(page.locator('.iwb-toolbar-bottom .iwb-btn[data-tip="分层"]')).toHaveCount(0)
|
||
// 右键菜单
|
||
const canvas = await page.locator('.iwb-canvas').boundingBox()
|
||
await page.mouse.click(canvas.x + canvas.width * 0.5, canvas.y + canvas.height * 0.5, { button: 'right' })
|
||
// 2026-11-27:主菜单与子菜单都带 .iwb-ctxmenu ⇒ 收窄到主菜单(否则 strict mode 命中 2 个)
|
||
const MAIN_CTX = '.iwb-ctxmenu:not(.iwb-ctx-sub)'
|
||
await expect(page.locator(MAIN_CTX)).toBeVisible({ timeout: 8000 })
|
||
const items = await page.locator(MAIN_CTX + ' .iwb-ctx-item').evaluateAll((els) => els.map((e) => (e.textContent || '').trim()))
|
||
expect(items.some((t) => t.includes('润色')), '右键菜单默认不应有「润色」').toBe(false)
|
||
expect(items.some((t) => t.includes('分层')), '右键菜单默认不应有「分层」').toBe(false)
|
||
// 但常规节点仍在
|
||
expect(items.some((t) => t.includes('配置')), '常规节点入口必须保留(gen 节点已改名「配置」)').toBe(true)
|
||
// 2026-10-30:文本节点入口已恢复(曾短暂改为私有工具)⇒ 拖线菜单应重新出现
|
||
expect(items.some((t) => t.includes('文本')), '拖线菜单应恢复「文本」').toBe(true)
|
||
})
|
||
|
||
// ── 反向拖拽(2026-10-30 追加):从**输入端口**往左拖 = 添加上游 ──────────────
|
||
const BOARD_REV = {
|
||
id: 'ws_cm', name: 'REVERSE', edges: [], groups: [],
|
||
nodes: [
|
||
{ id: 'p1', type: 'prompt', x: 200, y: 300, data: { text: 'hello' } },
|
||
{ id: 'g1', type: 'gen', x: 700, y: 300, data: {} },
|
||
],
|
||
}
|
||
|
||
async function dragFromInPort(page, nodeId, x, y) {
|
||
const port = page.locator(`[data-node-id="${nodeId}"] .iwb-port-in`).first()
|
||
await expect(port).toBeVisible({ timeout: 15000 })
|
||
const b = await port.boundingBox()
|
||
await page.mouse.move(b.x + b.width / 2, b.y + b.height / 2)
|
||
await page.mouse.down()
|
||
await page.mouse.move(x, y, { steps: 10 })
|
||
await page.mouse.up()
|
||
await page.waitForTimeout(260)
|
||
}
|
||
|
||
test('⑥ 从输入端口拖到空白 ⇒ 弹「为它添加来源」,只列合法来源', async ({ page }) => {
|
||
await seedWith(page, Object.assign({}, BOARD_REV, { workspaces: undefined }) && { workspaces: [BOARD_REV], activeId: 'ws_cm', rev: 1, schemaVersion: 1 })
|
||
const canvas = await page.locator('.iwb-canvas').boundingBox()
|
||
await dragFromInPort(page, 'g1', canvas.x + canvas.width * 0.25, canvas.y + canvas.height * 0.75)
|
||
|
||
const menu = page.locator('.iwb-connect-menu')
|
||
await expect(menu, '从左端口拖到空白处应弹出菜单').toBeVisible({ timeout: 8000 })
|
||
await expect(menu.locator('.iwb-ctx-title')).toHaveText('为它添加来源')
|
||
|
||
const types = await menu.locator('.iwb-ctx-item[data-type]').evaluateAll((els) => els.map((e) => e.getAttribute('data-type')))
|
||
for (const t of ['prompt', 'ref', 'combine']) expect(types, `「${t}」应可作为生图的来源`).toContain(t)
|
||
expect(types, '生图不能作为生图的来源').not.toContain('gen')
|
||
expect(types, '输出节点不能作为来源').not.toContain('out')
|
||
expect(types, '分层默认隐藏').not.toContain('layer')
|
||
expect(types, '润色默认隐藏').not.toContain('llm')
|
||
})
|
||
|
||
test('⑦ 反向点选「提示词」⇒ 落点建节点 + 反向连边(新节点 → 我)', async ({ page }) => {
|
||
await seedWith(page, Object.assign({}, BOARD_REV, { workspaces: undefined }) && { workspaces: [BOARD_REV], activeId: 'ws_cm', rev: 1, schemaVersion: 1 })
|
||
const before = await nodeCount(page)
|
||
const canvas = await page.locator('.iwb-canvas').boundingBox()
|
||
await dragFromInPort(page, 'g1', canvas.x + canvas.width * 0.25, canvas.y + canvas.height * 0.75)
|
||
|
||
const menu = page.locator('.iwb-connect-menu')
|
||
await expect(menu).toBeVisible({ timeout: 8000 })
|
||
await menu.locator('.iwb-ctx-item[data-type="prompt"]').click()
|
||
await expect(menu).toHaveCount(0)
|
||
await expect(page.locator('.iwb-node')).toHaveCount(before + 1)
|
||
|
||
await expect.poll(async () => edgeCount(page), { timeout: 8000 }).toBe(1)
|
||
const edge = (await readGraph(page)).edges[0] || {}
|
||
expect(edge.to, '边应连到目标节点 g1').toBe('g1')
|
||
expect(edge.from, '边应从新节点出发').not.toBe('g1')
|
||
})
|
||
|
||
test('⑧ 从输入端口拖到已有节点 ⇒ 直接连边,不弹菜单', async ({ page }) => {
|
||
await seedWith(page, Object.assign({}, BOARD_REV, { workspaces: undefined }) && { workspaces: [BOARD_REV], activeId: 'ws_cm', rev: 1, schemaVersion: 1 })
|
||
const p1 = page.locator('[data-node-id="p1"]').first()
|
||
await expect(p1).toHaveCount(1, { timeout: 8000 })
|
||
const box = await p1.boundingBox()
|
||
await dragFromInPort(page, 'g1', box.x + box.width / 2, box.y + box.height / 2)
|
||
await expect(page.locator('.iwb-connect-menu'), '落在已有节点上不应弹菜单').toHaveCount(0)
|
||
await expect.poll(async () => edgeCount(page), { timeout: 8000 }).toBe(1)
|
||
const edge = (await readGraph(page)).edges[0] || {}
|
||
expect(edge.from, '反向连边方向必须是 p1 → g1').toBe('p1')
|
||
expect(edge.to).toBe('g1')
|
||
})
|
||
|
||
test('⑨ 拖线新建的节点:连线端点立即贴合输入端口(无需移动节点)', async ({ page }) => {
|
||
// 回归点(2026-10-30 用户实测):新建节点的真实高度(如 207px)与默认 NODE_H.gen(460)不同。
|
||
// EdgePathMemo 的自定义比较器原来不比较端点高度,而 measuredH 是**同一对象原地修改** ⇒
|
||
// memo 判定"没变"→ 连线不重算,端点停在按 460 算的位置;移动节点/改选中才修正。
|
||
await seed(page)
|
||
const canvas = await page.locator('.iwb-canvas').boundingBox()
|
||
await dragFromPort(page, 'p1', canvas.x + canvas.width * 0.55, canvas.y + canvas.height * 0.35)
|
||
const menu = page.locator('.iwb-connect-menu')
|
||
await expect(menu).toBeVisible({ timeout: 8000 })
|
||
await menu.locator('.iwb-ctx-item[data-type="gen"]').click()
|
||
await page.waitForTimeout(400)
|
||
|
||
const g = await page.evaluate(() => {
|
||
const world = document.querySelector('.iwb-world')
|
||
const m = /matrix\(([^)]+)\)/.exec(getComputedStyle(world).transform)
|
||
const v = m ? m[1].split(',').map(Number) : [1, 0, 0, 1, 0, 0]
|
||
const sc = v[0], tx = v[4], ty = v[5]
|
||
const edge = document.querySelector('.iwb-edge:not(.iwb-edge-temp)')
|
||
const mm = (edge ? edge.getAttribute('d') : '').match(/(-?[\d.]+)\s+(-?[\d.]+)\s*$/)
|
||
const node = document.querySelector('.iwb-node:not([data-node-id="p1"])')
|
||
const port = node ? node.querySelector('.iwb-port-in') : null
|
||
if (!mm || !port) return null
|
||
const pr = port.getBoundingClientRect()
|
||
return {
|
||
nodeH: Math.round(node.getBoundingClientRect().height),
|
||
dx: Math.round(Number(mm[1]) * sc + tx - (pr.x + pr.width / 2)),
|
||
dy: Math.round(Number(mm[2]) * sc + ty - (pr.y + pr.height / 2)),
|
||
}
|
||
})
|
||
expect(g, '应能取到连线端点与输入端口').toBeTruthy()
|
||
expect(Math.abs(g.dx), `端点 x 必须贴合端口(实测差 ${g.dx}px)`).toBeLessThanOrEqual(3)
|
||
expect(Math.abs(g.dy), `端点 y 必须贴合端口(实测差 ${g.dy}px,节点高 ${g.nodeH}px)`).toBeLessThanOrEqual(3)
|
||
})
|