Files

168 lines
7.9 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// iwb-dev-probe: 切画布标签 —— 缩放档位与「有没有视口裁剪」(只读测量),不入常规门禁。
//
// 已完成归因:
// v1 落盘(localStorage/JSON)1.1ms ⇒ 排除
// v5 把 scrollHeight 换常量:427→332ms(−22%)⇒ 强制布局只占 22%
// v6 切 800 节点画布 Trace:react-dom 渲染 271ms/次、react 核心 173ms/次、
// app.js 自身逻辑仅 3.9ms/全程 ⇒ 成本在「重建 800 个节点卡片」
// v7 LOD 全开(临时把阈值改 1.01):488→184ms(−62%),Layout 489→34ms
//
// v8 回答两件事:
// ① 常规规模(100 节点)切标签到底多贵 —— 之前所有数字都是 800 节点的极端值;
// ② 把画布放大到 scale≈1 再切回来,DOM 里还是 800 个节点吗?
// 如果是 ⇒ 没有视口裁剪,放大后渲染的全部是屏幕外的节点(这才是真正该修的地方)。
//
// 用法:
// IWB_PROBE_NODES=800 IWB_PROBE_TABS=2 IWB_PROBE_SCALE=1 IWB_E2E_SPECS=tests/_probe-tab-switch.spec.js npm run test:e2e
const { test, expect } = require('./_pw-coverage')
test.use({ baseURL: process.env.IWB_BASE_URL || 'http://127.0.0.1:8787', viewport: { width: 1440, height: 1000 } })
const num = (k, d) => { const v = Number(process.env[k]); return Number.isFinite(v) && v > 0 ? v : d }
const NODES = num('IWB_PROBE_NODES', 800)
const TABS = num('IWB_PROBE_TABS', 2)
const ROUNDS = num('IWB_PROBE_ROUNDS', 3)
const TARGET_SCALE = num('IWB_PROBE_SCALE', 0) // >0 则先缩放到该档再测切换
const COLS = 40
test.setTimeout(300000)
const CFG = { providers: [{ id: 'p-a', name: 'A', baseUrl: 'https://a.example/v1', keys: [{ id: 'a-1', name: 'K', value: 'sk' }], models: [{ name: 'gpt-image-2.5', visible: true }] }], canvas: { snap: true } }
// IWB_PROBE_IMAGES=1:给参考图节点塞一张真图(1×1 data URL)。
// 动机(v3.24.78):lite 档会给图片节点画缩略图,空数据的探针测不出 <img> 的真实成本。
const WITH_IMAGES = process.env.IWB_PROBE_IMAGES === '1'
const DOT = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8z8BQDwAEhQGAhKmMIQAAAABJRU5ErkJggg=='
function mkBoard(id, name, nNodes) {
const nodes = []
for (let i = 0; i < nNodes; i++) {
const m = i % 13
const type = m === 0 ? 'gen' : (m <= 3 ? 'prompt' : 'ref')
const node = { id: id + '_n' + i, type, x: 60 + (i % COLS) * 200, y: 60 + Math.floor(i / COLS) * 150, w: 160, data: {} }
if (type === 'prompt') node.data = { text: 'p' + i }
if (type === 'gen') node.data = { model: 'gpt-image-2.5', provider: 'p-a', ratio: '1:1', res: '2K', quality: 'auto' }
if (type === 'ref' && WITH_IMAGES) node.data = { data: DOT, name: 'dot.png' }
nodes.push(node)
}
return { id, name, nodes, edges: [], groups: [], category: '', starred: false, deleted: false }
}
const rd = (v) => Math.round(v * 10) / 10
const med = (arr) => { if (!arr.length) return null; const s = [...arr].sort((a, b) => a - b); return s[Math.floor(s.length / 2)] }
test('探针:切标签 × 缩放档位 × 节点数量', async ({ page }) => {
const errors = []
page.on('pageerror', (e) => errors.push(String((e && e.message) || e)))
const board0 = mkBoard('w1', 'MAIN', NODES)
const tabs = [board0]
for (let t = 1; t < TABS; t++) tabs.push(mkBoard('w' + (t + 1), 'T' + t, 200))
await page.addInitScript((d) => {
localStorage.clear()
localStorage.setItem('iwb_web_cfg', JSON.stringify(d.cfg))
localStorage.setItem('iwb_web_workspaces', JSON.stringify({ schemaVersion: 1, rev: 1, activeId: d.tabs[0].id, workspaces: d.tabs }))
localStorage.setItem('iwb_ws_active', d.tabs[0].id)
for (const b of d.tabs) localStorage.setItem('iwb_ws_' + b.id, JSON.stringify(b))
localStorage.setItem('iwb_tabs_open', JSON.stringify(d.tabs.map((b) => b.id)))
window.__IWB_E2E__ = true
}, { cfg: CFG, tabs: tabs.map((b) => ({ id: b.id, name: b.name, nodes: b.nodes, edges: b.edges, groups: b.groups })) })
await page.goto('/', { waitUntil: 'domcontentloaded' })
await page.waitForTimeout(800)
await page.locator('.iwb-home-card:not(.iwb-home-new)').first().dblclick()
await page.waitForSelector('.iwb-canvas:not(.iwb-canvas-hidden)', { timeout: 30000 })
await page.waitForTimeout(2500)
const tabInfo = await page.evaluate(() => Array.from(document.querySelectorAll('.iwb-tab'))
.map((el, idx) => ({ idx, text: (el.textContent || '').trim().slice(0, 20) }))
.filter((t) => t.text !== '+'))
const big = tabInfo.find((t) => t.text === 'MAIN').idx
const small = tabInfo.find((t) => t.text === 'T1').idx
const readScale = () => page.evaluate(() => {
const w = document.querySelector('.iwb-world')
const m = w && (getComputedStyle(w).transform || '').match(/matrix\(([^,]+),/)
return m ? Number(m[1]) : null
})
// 视口统计:DOM 里挂了多少节点 / 其中多少个中心落在视口内
const viewportStats = () => page.evaluate(() => {
const nodes = Array.from(document.querySelectorAll('.iwb-node'))
let inVp = 0
for (const el of nodes) {
const r = el.getBoundingClientRect()
const cx = r.left + r.width / 2, cy = r.top + r.height / 2
if (cx > 0 && cy > 0 && cx < innerWidth && cy < innerHeight) inVp++
}
return { dom: nodes.length, inViewport: inVp, textareas: document.querySelectorAll('.iwb-node textarea').length }
})
const clickTab = (idx) => page.evaluate(async (i) => {
const list = Array.from(document.querySelectorAll('.iwb-tab')).filter((e) => (e.textContent || '').trim() !== '+')
const el = list[i]
if (!el) return null
const t0 = performance.now()
el.click()
await new Promise((res) => requestAnimationFrame(() => requestAnimationFrame(res)))
return Math.round(performance.now() - t0)
}, idx)
let scaleApplied = null
if (TARGET_SCALE > 0) {
// 先把当前画布(MAIN)缩放调到目标档,session 会记住它,切回来时沿用
// ⚠️ 锚点必须落在**节点身上**:宽扁布局 fit 后节点只占屏幕上方一小条,
// 固定用视口中心 (700,500) 滚轮会把视野推到空白区 ⇒ 测出 dom=0(2026-11-12 撞到过)。
const anchor = await page.evaluate(() => {
const n = document.querySelector('.iwb-node')
if (!n) return { x: 700, y: 500 }
const r = n.getBoundingClientRect()
return {
x: Math.max(1, Math.min(innerWidth - 1, Math.round(r.left + r.width / 2))),
y: Math.max(1, Math.min(innerHeight - 1, Math.round(r.top + r.height / 2))),
}
})
await page.mouse.move(anchor.x, anchor.y)
for (let i = 0; i < 200; i++) {
const s = await readScale()
if (s == null) break
if (Math.abs(s - TARGET_SCALE) <= TARGET_SCALE * 0.06) break
await page.mouse.wheel(0, s < TARGET_SCALE ? -240 : 240)
await page.waitForTimeout(20)
}
scaleApplied = await readScale()
await page.waitForTimeout(600)
}
const walls = []
const stats = []
for (let r = 0; r < ROUNDS; r++) {
await clickTab(small)
await page.waitForTimeout(400)
const w = await clickTab(big)
await page.waitForTimeout(200)
const st = await viewportStats()
const sc = await readScale()
walls.push(w)
stats.push({ wall: w, scale: rd(sc || 0), ...st })
await page.waitForTimeout(400)
}
const summary = {
scenario: { NODES, TABS, TARGET_SCALE, scaleApplied: rd(scaleApplied || 0), tabs: tabInfo.map((t) => t.text) },
walls,
medianWall: med(walls),
stats,
note: 'inViewport << dom ⇒ 没有视口裁剪,屏幕外节点照样全量渲染',
errors,
}
console.log('TABSWITCH_PROBE=' + JSON.stringify(summary, null, 1))
// LOD 视觉留证:把当前画布截一张,人工判断「灰块占位」能不能认出这里有内容
const shot = '/tmp/iwb-lod-' + NODES + '-scale' + (TARGET_SCALE > 0 ? TARGET_SCALE : 'fit') + '.png'
await page.screenshot({ path: shot })
console.log('LOD_SHOT=' + shot)
expect(true).toBe(true)
})