134 lines
7.8 KiB
JavaScript
134 lines
7.8 KiB
JavaScript
// 离开页面前的 flush 兜底契约:pagehide / visibilitychange(hidden) 必须绕开防抖窗口,把"待保存但尚未落盘"
|
||
// 的编辑同步写进 localStorage,刷新后仍在(I1:不丢数据)。
|
||
// 本用例是 flush 路径优化的回归护栏——原实现逐节点深拷贝 data + 二次全量 strip 被换成
|
||
// copyNodeForPersist + WorkspaceStore.writeStripped 后,对外语义必须一字不变。
|
||
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: 'K', value: 'sk' }], models: [{ name: 'gpt-image-2', visible: true }] }], canvas: { snap: true } }
|
||
const MARK = 'FLUSHMARK'
|
||
const mk = (id, name, text) => ({ id, name, nodes: [{ id: id + '-1', type: 'prompt', x: 100, y: 100, data: { text } }], edges: [], groups: [] })
|
||
|
||
// 播种:老格式索引(内嵌节点)→ 启动时由应用自行迁移成 ws-keys(与 iwb-storage-incremental 同一套播种方式)
|
||
async function seed(page, cfg, boards, activeId) {
|
||
await page.addInitScript((d) => {
|
||
// 本初始化脚本每次导航(含 reload)都会执行;哨兵保证存储只播种一次,否则 reload 会冲掉 flush 刚落盘的数据(用例假绿)。
|
||
if (sessionStorage.getItem('__iwb_seeded__') === '1') return
|
||
sessionStorage.setItem('__iwb_seeded__', '1')
|
||
localStorage.clear()
|
||
localStorage.setItem('iwb_web_cfg', JSON.stringify(d.cfg))
|
||
// 老格式索引(内嵌节点)→ 启动时由应用自行迁移成 ws-keys(与 iwb-storage-incremental 同一套播种方式)
|
||
localStorage.setItem('iwb_web_workspaces', JSON.stringify({ schemaVersion: 1, rev: 5, activeId: d.activeId, workspaces: d.boards }))
|
||
localStorage.setItem('iwb_ws_active', d.activeId)
|
||
}, { cfg, boards, activeId })
|
||
}
|
||
// 读取某块画板分板键里的节点 JSON(不存在 → null)
|
||
const boardKey = (page, id) => page.evaluate((bid) => localStorage.getItem('iwb_ws_' + bid), id)
|
||
// 索引里的保存版本号(flush 必须递增它,否则其它标签页轮询看不到这次兜底落盘)
|
||
const revOf = (page) => page.evaluate(() => { try { return (JSON.parse(localStorage.getItem('iwb_web_workspaces')) || {}).rev || 0 } catch (e) { return 0 } })
|
||
const textOf = (raw) => {
|
||
try { return JSON.stringify((JSON.parse(raw) || {}).nodes || []) } catch (e) { return '' }
|
||
}
|
||
// 双击提示词节点体 → 输入(局部化输入静止 ~350ms 才写回 store,防抖 1.5s/打字 2.6s 都还没到)
|
||
async function typeIntoFirstNode(page, text) {
|
||
await page.locator('.iwb-node-body').first().dblclick()
|
||
await page.waitForTimeout(200)
|
||
await page.locator('.iwb-node textarea').first().click()
|
||
await page.keyboard.type(text, { delay: 30 })
|
||
}
|
||
const openCard = async (page, name) => {
|
||
await page.locator('.iwb-home-card:not(.iwb-home-new)', name ? { hasText: name } : {}).first().dblclick()
|
||
await expect(page.locator('.iwb-canvas:not(.iwb-canvas-hidden)')).toBeVisible()
|
||
await expect(page.locator('.iwb-node').first()).toBeVisible()
|
||
}
|
||
|
||
test('pagehide 兜底:防抖窗口内的编辑立即落盘且刷新后仍在', async ({ page }) => {
|
||
const errors = []
|
||
page.on('pageerror', (e) => errors.push(String((e && e.message) || e)))
|
||
await seed(page, CFG, [mk('wa', '甲', '原始文本')], 'wa')
|
||
await page.goto('/', { waitUntil: 'domcontentloaded' })
|
||
await page.waitForTimeout(1200)
|
||
await openCard(page, null)
|
||
expect(await boardKey(page, 'wa'), '分板键应已由迁移写入').not.toBeNull()
|
||
|
||
await typeIntoFirstNode(page, MARK)
|
||
await page.waitForTimeout(600) // 输入已写回 store,但防抖(≥1.5s)尚未触发
|
||
const before = textOf(await boardKey(page, 'wa'))
|
||
expect(before, '派发前必须还没有落盘(否则本用例证明不了 flush 生效)').not.toContain(MARK)
|
||
const revBefore = await revOf(page)
|
||
|
||
await page.evaluate(() => window.dispatchEvent(new Event('pagehide')))
|
||
await page.waitForTimeout(300)
|
||
const after = textOf(await boardKey(page, 'wa'))
|
||
console.log('AFTER=' + after)
|
||
expect(after, 'pagehide 后编辑必须已同步落盘').toContain(MARK)
|
||
// v3.24.80:flush 原先写回旧 rev ⇒ 其它标签页轮询永远看不到这次落盘
|
||
await expect.poll(() => revOf(page), 'flush 必须把保存版本号递增一次').toBe(revBefore + 1)
|
||
|
||
await page.reload({ waitUntil: 'domcontentloaded' })
|
||
expect(textOf(await boardKey(page, 'wa')), '刷新后编辑仍在').toContain(MARK)
|
||
expect(errors).toEqual([])
|
||
})
|
||
|
||
test('visibilitychange(hidden) 兜底:防抖窗口内的编辑立即落盘且刷新后仍在', async ({ page }) => {
|
||
const errors = []
|
||
page.on('pageerror', (e) => errors.push(String((e && e.message) || e)))
|
||
await seed(page, CFG, [mk('wa', '甲', '原始文本')], 'wa')
|
||
await page.goto('/', { waitUntil: 'domcontentloaded' })
|
||
await page.waitForTimeout(1200)
|
||
await openCard(page, null)
|
||
await page.waitForTimeout(2600)
|
||
|
||
await typeIntoFirstNode(page, MARK)
|
||
await page.waitForTimeout(600)
|
||
const before = textOf(await boardKey(page, 'wa'))
|
||
expect(before, '派发前必须还没有落盘').not.toContain(MARK)
|
||
const revBefore = await revOf(page)
|
||
|
||
// 切后台:监听器挂在 window 上,必须派发到 window 才可达;visibilityState 需可写以模拟隐藏
|
||
await page.evaluate(() => {
|
||
Object.defineProperty(document, 'visibilityState', { value: 'hidden', configurable: true })
|
||
window.dispatchEvent(new Event('visibilitychange'))
|
||
})
|
||
await page.waitForTimeout(300)
|
||
const after = textOf(await boardKey(page, 'wa'))
|
||
console.log('AFTER=' + after)
|
||
expect(after, 'visibilitychange(hidden) 后编辑必须已同步落盘').toContain(MARK)
|
||
await expect.poll(() => revOf(page), 'visibilitychange 兜底同样必须递增 rev').toBe(revBefore + 1)
|
||
|
||
await page.reload({ waitUntil: 'domcontentloaded' })
|
||
expect(textOf(await boardKey(page, 'wa')), '刷新后编辑仍在').toContain(MARK)
|
||
expect(errors).toEqual([])
|
||
})
|
||
|
||
test('flush 合成所有打开 session:非激活标签的未保存编辑同样落盘', async ({ page }) => {
|
||
const errors = []
|
||
page.on('pageerror', (e) => errors.push(String((e && e.message) || e)))
|
||
await seed(page, CFG, [mk('wa', '甲', 'A 文本'), mk('wb', '乙', 'B 文本')], 'wa')
|
||
await page.goto('/', { waitUntil: 'domcontentloaded' })
|
||
await page.waitForTimeout(1200)
|
||
await openCard(page, '甲') // 打开 甲 → 标签 1
|
||
await page.waitForTimeout(2000)
|
||
await W.goHome(page) // 回画布库(甲 仍是打开但未激活的 session)
|
||
await openCard(page, '乙') // 打开 乙 → 标签 2,成为激活 session
|
||
await page.waitForTimeout(2600)
|
||
const wbId = await page.evaluate(() => localStorage.getItem('iwb_ws_active'))
|
||
expect(wbId, '激活画板应为 乙').toBe('wb')
|
||
|
||
await typeIntoFirstNode(page, MARK) // 在 乙 上编辑(乙 为激活 session)
|
||
// ⚠️ 必须 force:Playwright 的「稳定性」检查在标签栏上一直不通过(元素 rect 恒定、DOM 未重建、
|
||
// 真鼠标点击实测可切换),不加 force 会 30s 超时把整条用例拖死(2026-11-01 实测)。
|
||
await page.locator('.iwb-tab', { hasText: '甲' }).click({ force: true }) // 立刻切到 甲:乙 变成"未激活但打开、且有未保存改动"
|
||
await page.evaluate(() => window.dispatchEvent(new Event('pagehide')))
|
||
await page.waitForTimeout(300)
|
||
|
||
const wb = textOf(await boardKey(page, 'wb'))
|
||
console.log('WB=' + wb)
|
||
expect(wb, '未激活 session 的未保存编辑也必须被 flush 合成落盘').toContain(MARK)
|
||
|
||
await page.reload({ waitUntil: 'domcontentloaded' })
|
||
expect(textOf(await boardKey(page, 'wb')), '刷新后 乙 的编辑仍在').toContain(MARK)
|
||
expect(errors).toEqual([])
|
||
})
|