Files
image-workbench-desktop/tests/iwb-private-gate.spec.js
T

118 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.4 私有功能开关(分层):默认隐藏入口,用链接暗号 `/#iwb-tools=<暗号>` 开启
// ① 全新 profile(无开关):右键「添加节点」与设置面板里都没有任何分层痕迹
// ② 带暗号访问链接:写入开关、地址栏 fragment 被抹掉,入口出现;`#iwb-tools=off` 可关闭
// ③ 隐藏的是「入口」不是「功能」:画布里已存在的分层节点仍照常渲染与可运行(否则自己的画布会坏)
// 运行:npm run build && IWB_BASE_URL=http://127.0.0.1:8787 npx playwright test tests/iwb-private-gate.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: 1440, height: 1000 } })
const TOKEN = 'malongan' // 与 app.js 里的 PRIVATE_TOOLS_TOKEN 一致(私有功能暗号)
const CFG = {
providers: [{ id: 'p-a', name: '服务商 A', baseUrl: 'https://a.example/v1', keys: [{ id: 'a-1', name: 'A key' }], models: [] }],
canvas: {},
}
const wsWith = (nodes) => ({ workspaces: [{ id: 'w1', name: 'W', nodes, edges: [], groups: [] }], activeId: 'w1', rev: 1 })
const WS_PLAIN = wsWith([{ id: 'p1', type: 'prompt', x: 120, y: 140, data: { text: 'x' } }])
const WS_LAYER = wsWith([{ id: 'L1', type: 'layer', x: 120, y: 140, w: 520, data: { model: 'doubao-seedream-5-0-pro-260628', provider: 'p-a', keyGroup: 'a-1', sizeParam: '1K', layers: [{ id: 'x', name: '背景装饰层', bbox: null }], result: null } }])
async function seed(page, ws, token) {
await page.addInitScript((d) => {
// 幂等:只在本标签页首次加载时种数据。否则后续 reload 会再清一次 localStorage,
// 把「链接暗号」写下的开关抹掉(测试假失败)。
try { if (sessionStorage.getItem('__iwb_gate_seeded__') === '1') return; sessionStorage.setItem('__iwb_gate_seeded__', '1') } catch (e) { /* ignore */ }
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.token) localStorage.setItem('iwb_tools', d.token)
}, { cfg: CFG, ws, token })
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()
}
// 刷新后应用可能停在首页卡片视图:先确保回到画布再操作
async function ensureCanvas(page) {
if (await page.locator('.iwb-canvas:not(.iwb-canvas-hidden)').count()) return
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(200)
}
async function openCtxMenu(page) {
await ensureCanvas(page)
await page.locator('.iwb-canvas').click({ button: 'right', position: { x: 900, y: 620 } })
await expect(page.locator('.iwb-ctxmenu')).toBeVisible()
}
const layerCtxItem = (page) => page.locator('.iwb-ctxmenu .iwb-ctx-item', { hasText: '分层' })
test('默认隐藏:无开关时右键菜单与设置面板都没有分层入口', async ({ page }) => {
const errors = []
page.on('pageerror', (e) => errors.push(String((e && e.message) || e)))
await seed(page, WS_PLAIN, null)
await openCtxMenu(page)
await expect(layerCtxItem(page)).toHaveCount(0)
await page.keyboard.press('Escape')
await page.locator('.iwb-top-nav-icon[title="服务与 Key 设置"]').click()
await expect(page.locator('.iwb-settings-modal')).toBeVisible()
await expect(page.locator('.iwb-settings-modal').getByText(/分层模型(/)).toHaveCount(0)
expect(await page.evaluate(() => localStorage.getItem('iwb_tools'))).toBe(null)
expect(errors).toEqual([])
})
test('链接暗号:开启后入口出现且地址栏不留痕,off 可关闭', async ({ page }) => {
const errors = []
page.on('pageerror', (e) => errors.push(String((e && e.message) || e)))
await seed(page, WS_PLAIN, null)
// ① 带暗号访问(同源 hash 导航 → hashchange 立即生效,无需刷新)
await page.goto('/#iwb-tools=' + TOKEN, { waitUntil: 'domcontentloaded' })
expect(await page.evaluate(() => localStorage.getItem('iwb_tools'))).toBe(TOKEN)
expect(page.url(), '地址栏 fragment 必须被抹掉').not.toContain('iwb-tools')
await openCtxMenu(page)
await expect(layerCtxItem(page)).toHaveCount(1)
await page.keyboard.press('Escape')
await page.locator('.iwb-top-nav-icon[title="服务与 Key 设置"]').click()
await expect(page.locator('.iwb-settings-modal').getByText(/分层模型(/)).toBeVisible()
await page.locator('.iwb-modal-head button[title="关闭"]').click()
// ② off 关闭:入口重新消失
await page.goto('/#iwb-tools=off', { waitUntil: 'domcontentloaded' })
expect(await page.evaluate(() => localStorage.getItem('iwb_tools'))).toBe(null)
await openCtxMenu(page)
await expect(layerCtxItem(page)).toHaveCount(0)
expect(errors).toEqual([])
})
test('隐藏的是入口不是功能:已存在的分层节点仍照常渲染与可运行', async ({ page }) => {
const errors = []
page.on('pageerror', (e) => errors.push(String((e && e.message) || e)))
await seed(page, WS_LAYER, null) // 没有开关
const node = page.locator('[data-node-id="L1"]')
await expect(node.locator('.iwb-layer-body')).toBeVisible()
await expect(node.locator('.iwb-layer-run')).toBeVisible()
await expect(node.locator('.iwb-layer-prefix')).toHaveText('将图片进行精确图层分离:')
// 但入口仍然是隐藏的
await openCtxMenu(page)
await expect(layerCtxItem(page)).toHaveCount(0)
expect(errors).toEqual([])
})
test('文本节点(textout):入口已恢复(曾短暂按私有工具隐藏)', async ({ page }) => {
// 2026-10-30 用户先要求屏蔽、后要求还原 ⇒ 这里守住"还原"的结果:底栏 + 右键菜单 + 拖线菜单都有
await seed(page, WS_PLAIN, null)
await expect(page.locator('.iwb-toolbar-bottom .iwb-btn[data-tip="文本"]'), '底栏应有文本按钮').toHaveCount(1)
await openCtxMenu(page)
await expect(page.locator('.iwb-ctxmenu .iwb-ctx-item', { hasText: '文本' }), '右键菜单应有文本').toHaveCount(1)
await page.keyboard.press('Escape')
// 私有类型仍默认隐藏(对照:分层/润色不因此被放出)
await openCtxMenu(page)
await expect(page.locator('.iwb-ctxmenu .iwb-ctx-item', { hasText: '分层' })).toHaveCount(0)
})