Files
image-workbench-desktop/tests/iwb-duplicate-ws.spec.js
T

76 lines
4.8 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.
// 复制画布(创建副本)端到端契约(用户实测缺陷驱动):
// ① 本次访问新画的连线必须进入副本(回画布库 → 点「创建副本」→ 打开副本,连线仍在)
// ② 连续复制同一画布,副本名唯一:「X 副本」「X 副本 2」(不得出现两个同名副本)
// ③ 全程 pageerror = 0
// 运行:npm run build && IWB_E2E_SPECS=tests/iwb-duplicate-ws.spec.js npm run test:e2e
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.5', visible: true }] }], canvas: { snap: true } }
const board = { id: 'w1', name: '源画布', nodes: [
{ id: 'r1', type: 'ref', x: 120, y: 140, w: 420, data: {} },
{ id: 'g1', type: 'gen', x: 680, y: 160, w: 420, data: { model: 'gpt-image-2.5', provider: 'p-a', keyGroup: 'a-1', ratio: '1:1', res: '1K', variants: 1 } },
], edges: [], groups: [] }
const cardBy = (page, name) => page.locator('.iwb-home-card:not(.iwb-home-new)').filter({ has: page.locator('.iwb-home-card-title strong:text-is("' + name + '")') })
test('创建副本保留本次新画连线 + 连续复制副本名唯一', async ({ page }) => {
const errors = []
page.on('pageerror', (e) => errors.push(String((e && e.message) || e)))
await page.addInitScript((d) => {
localStorage.clear()
localStorage.setItem('iwb_web_cfg', JSON.stringify(d.cfg))
localStorage.setItem('iwb_web_workspaces', JSON.stringify({ schemaVersion: 1, workspaces: [d.board], activeId: 'w1', rev: 1 }))
localStorage.setItem('iwb_ws_active', 'w1')
localStorage.setItem('iwb_ws_w1', JSON.stringify(d.board))
window.__IWB_E2E__ = true
}, { cfg: CFG, board })
await page.goto('/', { waitUntil: 'domcontentloaded' })
await page.waitForTimeout(700)
await page.locator('.iwb-home-card:not(.iwb-home-new)').first().dblclick()
await expect(page.locator('.iwb-canvas:not(.iwb-canvas-hidden)')).toBeVisible()
await page.waitForTimeout(400)
// 本次访问新画一条连线(真实拖拽:出端口按下 → 悬停目标节点 → 释放;connTarget 由 pointermove 的 rAF 提交)
const outPort = page.locator('.iwb-node[data-node-id="r1"] .iwb-port-out')
const genNode = page.locator('.iwb-node[data-node-id="g1"]')
await outPort.hover()
await page.mouse.down()
await genNode.hover()
await page.waitForTimeout(150)
await page.mouse.up()
await expect(page.locator('.iwb-edges g[data-edge-id]'), '画布上应出现新画的连线').toHaveCount(1)
// 等防抖落盘(1500ms 防抖)→ 轮询直到该连线真的持久化(比固定等待精确)
await W.poll(page, () => page.evaluate(() => { const rec = JSON.parse(localStorage.getItem('iwb_ws_w1') || 'null'); return rec ? (rec.edges || []).length : 0 }), { timeout: 10000 }).toBe(1)
const persisted = await page.evaluate(() => { const rec = JSON.parse(localStorage.getItem('iwb_ws_w1') || 'null'); return rec ? (rec.edges || []).length : -1 })
expect(persisted, '新画的连线应已持久化到 iwb_ws_w1').toBe(1)
// 回画布库:常驻标签栏最左「首页」图标标签(.iwb-tab-home)
await page.locator('.iwb-tab-home').click()
await expect(cardBy(page, '源画布')).toHaveCount(1)
await expect(cardBy(page, '源画布')).toContainText('1 条连线')
// 创建副本 ×1 → 连线必须跟着走(用户实测缺陷:副本丢连线)
await cardBy(page, '源画布').locator('button[title="创建副本"]').click()
await expect(page.locator('.iwb-home-card:not(.iwb-home-new)')).toHaveCount(2)
await expect(cardBy(page, '源画布 副本'), '副本卡片应显示 1 条连线').toContainText('1 条连线')
// 再复制一次 → 副本名必须唯一(钉住命名去重)
await cardBy(page, '源画布').locator('button[title="创建副本"]').click()
await expect(page.locator('.iwb-home-card:not(.iwb-home-new)')).toHaveCount(3)
const titles = await page.locator('.iwb-home-card:not(.iwb-home-new) .iwb-home-card-title strong').allTextContents()
expect(titles.filter((t) => t === '源画布 副本'), '不得出现同名副本').toHaveLength(1)
expect(titles, '第二份副本应命名为「源画布 副本 2」').toContain('源画布 副本 2')
expect(new Set(titles).size, '三个画布名应互不相同').toBe(3)
// 打开第一份副本:连线仍在
await cardBy(page, '源画布 副本').dblclick()
await expect(page.locator('.iwb-canvas:not(.iwb-canvas-hidden)')).toBeVisible()
await expect(page.locator('.iwb-edges g[data-edge-id]'), '副本里必须保留源画布的连线').toHaveCount(1)
expect(errors).toEqual([])
})