// v3.87.0 分层节点(layer):真实浏览器验收 // ① 节点渲染:固定指令前缀 / 分层清单行 / 1K·¥0.15 与 2K·¥0.30 档位 / 预计费用 / 进出一对端口 // ② 运行链路:拦截网络返回真实的 4 张图层结构(含 z_index=0 无元数据的底层图)→ 结果区按 z 从底到顶列出 // ③ 请求体契约:layer_decomposition=true + size=1K/2K + output_format=png + 不发 quality + image 为字符串 // ④ 与既有链路打通:图层进入「输出」节点(isImageProducer),且全程 pageerror = 0 // 运行:npm run build && IWB_BASE_URL=http://127.0.0.1:8787 npx playwright test tests/iwb-layer.spec.js const { test, expect } = require('./_pw-coverage') const baseURL = process.env.IWB_BASE_URL || 'http://127.0.0.1:8787' test.use({ baseURL, viewport: { width: 1600, height: 1100 } }) const IMG = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8z8DwHwAFAAH/q842iQAAAABJRU5ErkJggg==' const PIXEL = Buffer.from(IMG.split(',')[1], 'base64') const WS = { workspaces: [{ id: 'ws1', name: 'LAYER', nodes: [ { id: 'r1', type: 'ref', x: 60, y: 160, data: { url: '', data: IMG, name: 'poster.png', localName: null } }, { id: 'L1', type: 'layer', x: 420, y: 120, w: 520, data: { model: 'doubao-seedream-5-0-pro-260628', provider: 'p-a', keyGroup: 'a-1', sizeParam: '1K', layers: [{ id: 'L1a', name: '背景装饰层', bbox: null }, { id: 'L1b', name: '主体视觉层', bbox: null }, { id: 'L1c', name: '文字标题层', bbox: null }], result: null, }, }, { id: 'o1', type: 'out', x: 1120, y: 160, w: 420, data: {} }, ], edges: [ { id: 'e1', from: 'r1', to: 'L1' }, { id: 'e2', from: 'L1', to: 'o1' }, ], groups: [], }], activeId: 'ws1', rev: 1, } const CFG = { providers: [{ id: 'p-a', name: '服务商 A', baseUrl: 'https://a.example/v1', keys: [{ id: 'a-1', name: 'K1', value: 'sk-fake-for-e2e' }], models: [{ name: 'doubao-seedream-5-0-pro-260628', visible: true }] }], outputsRetention: { enabled: true, maxFiles: 500, maxDays: 15 }, canvas: { snap: true }, } // 与真实响应同构:z=0 底层合成图**没有** name/description/bounding_box;其余三层带元数据 const LAYER_RESP = { created: 1, model: 'doubao-seedream-5-0-pro-260628', usage: { input_images: 1, generated_images: 4, output_tokens: 15525, total_tokens: 15525 }, data: [ { z_index: 0, size: '1152x864', url: 'https://cdn.example/full.png', output_format: 'png' }, { z_index: 1, size: '1060x771', url: 'https://cdn.example/bg.png', output_format: 'png', name: '背景装饰层', description: '背景装饰元素', bounding_box: { absolute: [75, 76, 1135, 847], normalized: [65, 87, 984, 979] } }, { z_index: 2, size: '734x640', url: 'https://cdn.example/subject.png', output_format: 'png', name: '主体视觉层', description: '主体卡片', bounding_box: { absolute: [316, 220, 836, 674], normalized: [274, 255, 725, 779] } }, { z_index: 3, size: '767x813', url: 'https://cdn.example/text.png', output_format: 'png', name: '文字标题层', description: '所有促销文字', bounding_box: { absolute: [61, 62, 618, 651], normalized: [53, 72, 536, 753] } }, ], } async function seed(page) { 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_' + d.ws.activeId, JSON.stringify(d.ws.workspaces[0])) localStorage.setItem('iwb_web_outputs', JSON.stringify([])) }, { cfg: CFG, ws: WS }) 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() } test('分层节点:渲染 / 运行 / 请求契约 / 输出节点打通', async ({ page }) => { const errors = [] page.on('pageerror', (e) => errors.push(String(e && e.message || e))) const bodies = [] await page.route('**/images/generations', async (route) => { try { bodies.push(JSON.parse(route.request().postData() || '{}')) } catch (e) { bodies.push({ parseError: String(e) }) } await route.fulfill({ status: 200, contentType: 'application/json', body: JSON.stringify(LAYER_RESP) }) }) await page.route('https://cdn.example/**', (route) => route.fulfill({ status: 200, contentType: 'image/png', headers: { 'access-control-allow-origin': '*' }, body: PIXEL })) await seed(page) const node = page.locator('.iwb-node', { has: page.locator('.iwb-layer-body') }) await expect(node).toHaveCount(1) // ① 节点渲染 await expect(node.locator('.iwb-layer-prefix')).toHaveText('将图片进行精确图层分离:') await expect(node.locator('.iwb-layer-row')).toHaveCount(3) await expect(node.locator('.iwb-layer-name').first()).toHaveValue('背景装饰层') await expect(node.locator('.iwb-chip-line')).toHaveCount(2) await expect(node.locator('.iwb-chip-line-on')).toHaveText(/1K · ¥0\.15\/层/) await expect(node.locator('.iwb-layer-cost')).toContainText('预计 4 张') await expect(node.locator('.iwb-layer-cost')).toContainText('¥0.60') // 提示词预览 = 固定前缀 + 3 个层名 await expect(node.locator('.iwb-layer-prompt')).toHaveText('将图片进行精确图层分离:背景装饰层,主体视觉层,文字标题层。') // 进/出端口各一个(hasIn/hasOut 放行 layer) await expect(node.locator('.iwb-port-in')).toHaveCount(1) await expect(node.locator('.iwb-port-out')).toHaveCount(1) // ② 运行(1K) await node.locator('.iwb-layer-run').click() await expect(node.locator('.iwb-layer-item')).toHaveCount(4, { timeout: 15000 }) // 结果按 z 从底到顶:第一个是补了默认名的底层合成图 await expect(node.locator('.iwb-layer-itemname').first()).toHaveText('底层合成图') await expect(node.locator('.iwb-layer-itemname').nth(1)).toHaveText('背景装饰层') await expect(node.locator('.iwb-layer-itemsub').nth(2)).toContainText('z=2') await expect(node.locator('.iwb-layer-itemsub').nth(2)).toContainText('734x640') // ③ 请求契约 expect(bodies.length).toBe(1) const b1 = bodies[0] expect(b1.layer_decomposition).toBe(true) expect(b1.size).toBe('1K') expect(b1.output_format).toBe('png') expect(b1.response_format).toBe('url') expect(b1.watermark).toBe(false) expect(b1.n).toBe(1) expect('quality' in b1).toBe(false) expect(typeof b1.image).toBe('string') // 字符串形态,不是数组 expect(b1.image.startsWith('data:image/')).toBe(true) expect(b1.prompt).toBe('将图片进行精确图层分离:背景装饰层,主体视觉层,文字标题层。') expect(b1.model).toBe('doubao-seedream-5-0-pro-260628') // ④ 图层进入「输出」节点(isImageProducer 的那一行) const outNode = page.locator('.iwb-node', { has: page.locator('.iwb-out-cell') }) await expect(outNode.locator('.iwb-out-cell')).toHaveCount(4, { timeout: 10000 }) // ⑤ 切 2K 档:费用翻倍,请求 size 跟随 await node.locator('.iwb-chip-line').nth(1).click() await expect(node.locator('.iwb-chip-line-on')).toHaveText(/2K · ¥0\.30\/层/) await expect(node.locator('.iwb-layer-cost')).toContainText('¥1.20') await node.locator('.iwb-layer-run').click() await expect(node.locator('.iwb-layer-item')).toHaveCount(4, { timeout: 15000 }) expect(bodies.length).toBe(2) expect(bodies[1].size).toBe('2K') // ⑥ 清单增删:+ 加一层 → 5 张(含底层)→ ¥0.75 await node.getByRole('button', { name: '+ 加一层' }).click() await expect(node.locator('.iwb-layer-row')).toHaveCount(4) // 空行不计费也不进提示词:费用仍是 4 张 await expect(node.locator('.iwb-layer-cost')).toContainText('预计 4 张') // 填上名字才计费:5 张 × ¥0.30 = ¥1.50 await node.locator('.iwb-layer-name').nth(3).fill('光影层') await node.locator('.iwb-layer-name').nth(3).blur() await expect(node.locator('.iwb-layer-cost')).toContainText('预计 5 张') await expect(node.locator('.iwb-layer-cost')).toContainText('¥1.50') // 删除该行 → 回到 3 行 / ¥1.20(2K) await node.locator('.iwb-layer-del').nth(3).click() await expect(node.locator('.iwb-layer-row')).toHaveCount(3) await expect(node.locator('.iwb-layer-cost')).toContainText('¥1.20') // ⑦ 清单改名即时反映到提示词预览(blur 提交,不打断中文输入法) await node.locator('.iwb-layer-name').nth(2).fill('标题文字层') await node.locator('.iwb-layer-name').nth(2).blur() await expect(node.locator('.iwb-layer-prompt')).toHaveText('将图片进行精确图层分离:背景装饰层,主体视觉层,标题文字层。') expect(errors).toEqual([]) }) test('分层节点:失败不产生结果且不自动重提(防重复计费)', async ({ page }) => { const errors = [] page.on('pageerror', (e) => errors.push(String(e && e.message || e))) let calls = 0 await page.route('**/images/generations', async (route) => { calls++ await route.fulfill({ status: 403, contentType: 'application/json', body: JSON.stringify({ error: { message: '图片被判定为敏感内容 OutputImageSensitiveContentDetected', type: 'BadRequest' } }) }) }) await seed(page) const node = page.locator('.iwb-node', { has: page.locator('.iwb-layer-body') }) await node.locator('.iwb-layer-run').click() await expect(page.locator('.iwb-status-item', { hasText: '敏感内容' })).toHaveCount(1, { timeout: 15000 }) expect(calls).toBe(1) // 明确失败只在用户点击时提交一次 await expect(node.locator('.iwb-layer-item')).toHaveCount(0) expect(errors).toEqual([]) })