Files
image-workbench-desktop/tests/iwb-crash-log.spec.js
T

117 lines
6.5 KiB
JavaScript
Raw 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.
// v3.87.6:崩溃可观测性验收
// ① 未捕获错误 + 未处理 Promise 拒绝 → 写入 localStorage 环形记录,并在「设置 → 诊断」可见
// ② 内存/规模采样每 15 秒落盘一条(标签页被杀后重开仍能看到崩溃前的增长曲线)
// ③ 渲染期异常不再整页白屏:错误边界给出可恢复的兜底界面,「尝试恢复」后应用继续可用
// 运行:npm run build && IWB_BASE_URL=http://127.0.0.1:8787 npx playwright test tests/iwb-crash-log.spec.js
const { test, expect } = require('./_pw-coverage')
const W = require('./_helpers/wait')
const baseURL = process.env.IWB_BASE_URL || 'http://127.0.0.1:8787'
test.use({ baseURL, viewport: { width: 1440, height: 1000 } })
const CFG = { providers: [{ id: 'p-a', name: '服务商 A', baseUrl: 'https://a.example/v1', keys: [{ id: 'a-1', name: 'K1', value: 'sk-fake' }], models: [{ name: 'gpt-image-2', visible: true }] }], canvas: {} }
const WS = {
workspaces: [{ id: 'w1', name: 'W', nodes: [{ id: 'p1', type: 'prompt', x: 120, y: 140, data: { text: 'x' } }], edges: [], groups: [] }],
activeId: 'w1', rev: 1,
}
async function seed(page, opts) {
const o = opts || {}
await page.addInitScript((d) => {
localStorage.clear()
localStorage.setItem('iwb_web_cfg', JSON.stringify(d.cfg))
localStorage.setItem('iwb_web_workspaces', JSON.stringify(d.ws))
localStorage.setItem('iwb_ws_active', d.ws.activeId)
localStorage.setItem('iwb_ws_w1', JSON.stringify(d.ws.workspaces[0]))
if (d.e2e) window.__IWB_E2E__ = true
if (d.boom) window.__IWB_E2E_BOOM__ = true
}, { cfg: CFG, ws: WS, e2e: !!o.e2e, boom: !!o.boom })
await page.goto('/', { waitUntil: 'domcontentloaded' })
await page.waitForTimeout(700)
// 进入画布(设置入口在画布顶栏);boom 用例首屏就崩,跳过
if (!o.boom) {
await page.locator('.iwb-home-card:not(.iwb-home-new)').first().dblclick()
await expect(page.locator('.iwb-canvas:not(.iwb-canvas-hidden)')).toBeVisible()
}
}
async function openDiagTab(page) {
await page.locator('.iwb-top-nav-icon[title="服务与 Key 设置"]').click()
await expect(page.locator('.iwb-settings-modal')).toBeVisible()
await page.locator('.iwb-settings-modal .iwb-tab', { hasText: '诊断' }).click()
}
const readLog = (page, key) => page.evaluate((k) => { try { return JSON.parse(localStorage.getItem(k) || '[]') } catch (e) { return [] } }, key)
test('未捕获错误与未处理拒绝:写入环形记录,并在「设置 → 诊断」可见', async ({ page }) => {
await seed(page, { e2e: true })
// 真实抛出:异步未捕获错误 + 未处理 Promise 拒绝(都在用户操作链路上可能发生)
await page.evaluate(() => { setTimeout(() => { throw new Error('e2e-uncaught-error') }, 0) })
await page.evaluate(() => { Promise.reject(new Error('e2e-unhandled-rejection')) })
// 等两条记录都写进环形日志(轮询,替代固定 500ms)
await W.poll(page, async () => {
const es = await readLog(page, 'iwb_crash_log')
const ks = es.map((e) => e.kind)
return ks.indexOf('error') >= 0 && ks.indexOf('rejection') >= 0
}, { timeout: 8000 }).toBe(true)
const entries = await readLog(page, 'iwb_crash_log')
const kinds = entries.map((e) => e.kind)
expect(kinds).toContain('error')
expect(kinds).toContain('rejection')
expect(entries.some((e) => String(e.message || '').includes('e2e-uncaught-error'))).toBe(true)
expect(entries.some((e) => String(e.message || '').includes('e2e-unhandled-rejection'))).toBe(true)
// 每条都带环境信息:版本 + 堆占用(Chromium 下 performance.memory 可用)
const one = entries.find((e) => String(e.message || '').includes('e2e-uncaught-error'))
expect(String(one.version || '')).toMatch(/^v\d/)
expect(one.memory === null || typeof one.memory === 'object').toBe(true)
// 诊断页能看到这些记录
await openDiagTab(page)
const panel = page.locator('.iwb-settings-modal')
await expect(panel.getByText('崩溃 / 错误记录')).toBeVisible()
await expect(panel.getByText(/e2e-uncaught-error/).first()).toBeVisible()
await expect(panel.getByText(/e2e-unhandled-rejection/).first()).toBeVisible()
})
test('内存采样:每 15 秒落盘一条,含堆占用与图片字节估算', async ({ page }) => {
test.setTimeout(60000)
await seed(page, { e2e: true })
await expect.poll(() => page.evaluate(() => { try { return (JSON.parse(localStorage.getItem('iwb_diag_samples') || '[]') || []).length } catch (e) { return 0 } }), { timeout: 30000, intervals: [1000] }).toBeGreaterThan(0)
const samples = await readLog(page, 'iwb_diag_samples')
const last = samples[samples.length - 1]
expect(typeof last.nodes).toBe('number')
expect(typeof last.base64MB).toBe('number')
expect(typeof last.localKB).toBe('number')
expect(last.memory === null || typeof last.memory.usedMB === 'number').toBe(true)
// 诊断页展示采样
await openDiagTab(page)
await expect(page.locator('.iwb-settings-modal').getByText('内存 / 规模采样')).toBeVisible()
})
test('渲染期异常不再白屏:错误边界兜底 + 「尝试恢复」后继续可用', async ({ page }) => {
const errors = []
page.on('pageerror', (e) => errors.push(String((e && e.message) || e)))
// 首屏渲染即抛错(探针只在 e2e 标志下生效)
await seed(page, { e2e: true, boom: true })
const screen = page.locator('.iwb-crash-screen')
await expect(screen).toBeVisible()
await expect(screen).toContainText('界面出错了')
await expect(screen).toContainText('e2e boundary boom')
// 兜底界面提供三个动作
await expect(screen.getByRole('button', { name: '尝试恢复' })).toBeVisible()
await expect(screen.getByRole('button', { name: '重新加载页面' })).toBeVisible()
await expect(screen.getByRole('button', { name: '复制诊断信息' })).toBeVisible()
// 崩溃已入记录(kind=boundary,含 scope)
const entries = await readLog(page, 'iwb_crash_log')
const hit = entries.find((e) => e.kind === 'boundary')
expect(hit, '错误边界必须写入崩溃记录').toBeTruthy()
expect(String(hit.scope || '')).toContain('react:app')
// 清掉触发条件后「尝试恢复」→ 应用正常渲染(不再白屏)
await page.evaluate(() => { window.__IWB_E2E_BOOM__ = false })
await screen.getByRole('button', { name: '尝试恢复' }).click()
await expect(page.locator('.iwb-crash-screen')).toHaveCount(0)
await expect(page.locator('.iwb-home-card:not(.iwb-home-new)').first()).toBeVisible({ timeout: 10000 })
expect(errors.filter((m) => !/boundary boom/.test(m))).toEqual([])
})