217 lines
13 KiB
JavaScript
217 lines
13 KiB
JavaScript
// 资源中心面板 交互优化(2026-10-20,v3.23.0)
|
||
//
|
||
// 本用例守 5 项交互改进(**行为断言**,不看源码文本 —— 遵守源码钉点棘轮「只减不增」):
|
||
// ① 面板快捷键:Ctrl+F 聚焦搜索、↑↓ 选卡片、Esc 关闭
|
||
// (此前**全部失效** —— 守卫是 `if (!fullOpen) return`,而资源中心走 `props.resOpen`)
|
||
// ② 搜索是**全局**的(跨分组)且当前范围**可见**
|
||
// (此前先按分组过滤再按搜索词过滤 ⇒ 在「小红书」里搜「收藏」搜不到「收藏」分组的内容)
|
||
// ③ 分类胶囊行**单行横向滚动**(分组多了不再换行挤矮卡片区),「⚙」固定在右侧不随滚动
|
||
// ④ 卡片「插入到画布」按钮用 **+ 图标**(此前是 ⬇ 下载图标,与行为相反 ⇒ 误导)
|
||
// ⑤ 手动收起右侧编辑区后,**记住这个偏好**(点卡片不再自动展开)
|
||
//
|
||
// 全部用真实浏览器 + 真实交互驱动,不做源码文本比对。
|
||
|
||
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 WS = {
|
||
workspaces: [{ id: 'ws_ux', name: '资源中心', nodes: [], edges: [], groups: [] }],
|
||
activeId: 'ws_ux', rev: 1, schemaVersion: 1,
|
||
}
|
||
// 两个分组的提示词 + 一个未分组 ⇒ 够验证「跨分组搜索」
|
||
const PROMPTS = [
|
||
{ id: 'q1', name: '小红书封面', text: '生成小红书封面图', tags: ['封面'], group: '小红书' },
|
||
{ id: 'q2', name: '收藏的提示', text: '这是被收藏的内容', tags: [], group: '收藏' },
|
||
{ id: 'q3', name: '未分组的一条', text: '没有分组的', tags: [], group: '' },
|
||
]
|
||
const GROUPS = [{ id: 'g1', name: '小红书' }, { id: 'g2', name: '收藏' }]
|
||
|
||
async function openResourceCenter(page) {
|
||
await page.addInitScript((ws) => { localStorage.setItem('iwb_web_workspaces', JSON.stringify(ws)) }, WS)
|
||
// 提示词与分组分别存在 iwb_web_prompts / iwb_lib_groups_cache
|
||
// (后者是 prompt groups 的本地缓存键 —— 服务端未返回 groups 时用它兜底)
|
||
await page.addInitScript(([p, g]) => {
|
||
localStorage.setItem('iwb_web_prompts', JSON.stringify(p))
|
||
localStorage.setItem('iwb_lib_groups_cache', JSON.stringify(g))
|
||
}, [PROMPTS, GROUPS])
|
||
await page.goto('/', { waitUntil: 'domcontentloaded' })
|
||
await page.waitForSelector('.iwb-home-card:not(.iwb-home-new)', { timeout: 20000 })
|
||
await page.locator('.iwb-home-card:not(.iwb-home-new)').first().dblclick()
|
||
await page.waitForSelector('.iwb-canvas-topbar', { timeout: 20000 })
|
||
await page.locator('.iwb-top-nav-icon[title*="资源中心"]').click()
|
||
await page.waitForSelector('.iwb-res-tabs', { timeout: 10000 })
|
||
await W.poll(page, () => page.locator('.iwb-pcard').count()).toBeGreaterThan(0)
|
||
}
|
||
|
||
test('① 资源中心快捷键:Ctrl+F 聚焦搜索 / ↑↓ 选卡片 / Esc 关闭', async ({ page }) => {
|
||
await openResourceCenter(page)
|
||
|
||
// Ctrl+F ⇒ 焦点落在搜索框(此前**完全失效**,但搜索框 placeholder 明写了「Ctrl+F 聚焦」)
|
||
await page.keyboard.press('Control+f')
|
||
await W.poll(page, () => page.evaluate(() => {
|
||
const a = document.activeElement
|
||
return !!(a && a.placeholder && String(a.placeholder).includes('搜索'))
|
||
})).toBe(true)
|
||
const inSearch = await page.evaluate(() => {
|
||
const a = document.activeElement
|
||
return !!(a && a.placeholder && String(a.placeholder).includes('搜索'))
|
||
})
|
||
expect(inSearch, 'Ctrl+F 应聚焦搜索框').toBe(true)
|
||
|
||
// ↑↓ ⇒ 选中项变化(此前失效)
|
||
await page.locator('.iwb-fullmask input[placeholder*="搜索"]').fill('')
|
||
await page.waitForTimeout(200)
|
||
// 焦点移出搜索框(面板浮层挡住画布,点击画布会超时 ⇒ 改用 blur)
|
||
await page.evaluate(() => { const i = document.querySelector('.iwb-fullmask input[placeholder*="搜索"]'); if (i) i.blur() })
|
||
await page.waitForTimeout(150)
|
||
await page.keyboard.press('ArrowDown')
|
||
await W.poll(page, () => page.locator('.iwb-pcard-selected').count()).toBeGreaterThan(0)
|
||
const selected = await page.locator('.iwb-pcard-selected').count()
|
||
expect(selected, '↓ 应能选中卡片').toBeGreaterThan(0)
|
||
|
||
// Esc ⇒ 关闭资源中心
|
||
await page.keyboard.press('Escape')
|
||
expect(await page.locator('.iwb-fullmask').count(), 'Esc 应关闭资源中心').toBe(0)
|
||
|
||
// v3.24.30:开合状态刷新后保持(此前刷新必定关闭,用户反馈「打开了刷新又没了」)
|
||
expect(await page.evaluate(() => localStorage.getItem('iwb_drawer_open')), '关闭后面板应写 0').toBe('0')
|
||
await page.reload({ waitUntil: 'domcontentloaded' })
|
||
await page.waitForSelector('.iwb-canvas-topbar', { timeout: 20000 })
|
||
expect(await page.locator('.iwb-fullmask').count(), '关闭态刷新后不应自动打开').toBe(0)
|
||
|
||
// 重新打开 → 刷新 → 仍保持打开(进入画布时曾 setDrawerOpen(false),刷新恢复不能走那条路径)
|
||
await page.locator('.iwb-top-nav-icon[title*="资源中心"]').click()
|
||
await page.waitForSelector('.iwb-res-tabs', { timeout: 10000 })
|
||
expect(await page.evaluate(() => localStorage.getItem('iwb_drawer_open')), '打开后面板应写 1').toBe('1')
|
||
await page.reload({ waitUntil: 'domcontentloaded' })
|
||
await page.waitForSelector('.iwb-canvas-topbar', { timeout: 20000 })
|
||
expect(await page.locator('.iwb-fullmask').count(), '打开态刷新后应保持打开').toBeGreaterThan(0)
|
||
|
||
// v3.24.38:标签页也要记住(此前 drawerTab 是纯内存 state,刷新必回「提示词库」)
|
||
await page.locator('.iwb-res-tab', { hasText: '图库' }).click()
|
||
expect(await page.evaluate(() => localStorage.getItem('iwb_drawer_tab')), '切到图库应写 history').toBe('history')
|
||
await page.reload({ waitUntil: 'domcontentloaded' })
|
||
await page.waitForSelector('.iwb-canvas-topbar', { timeout: 20000 })
|
||
await W.poll(page, () => page.locator('.iwb-res-tab-active').first().innerText()).toContain('图库')
|
||
const activeTab = await page.locator('.iwb-res-tab-active').first().innerText()
|
||
expect(activeTab, '刷新后应停在图库标签').toContain('图库')
|
||
})
|
||
|
||
test('①b Esc 分层:输入框 / 临时层优先于面板(2026-10-20 回归)', async ({ page }) => {
|
||
await openResourceCenter(page)
|
||
|
||
// ① 正在输入框打字时,Esc 归输入框(取消编辑),**不能**关掉整个面板
|
||
// (此前面板自己在捕获阶段处理 Esc ⇒ 会抢在 app.js 的画布 Esc 之前关掉面板)
|
||
await page.locator('.iwb-fullmask input[placeholder*="搜索"]').click()
|
||
await page.keyboard.press('Escape')
|
||
expect(await page.locator('.iwb-fullmask').count(), '输入框里按 Esc 不应关闭面板').toBe(1)
|
||
|
||
// ② 存在临时层(真实右键菜单)时,Esc 先关临时层,面板保持打开
|
||
// (app.js 画布 Esc 的分层:右键菜单 → … → 资源中心)
|
||
// ⚠️ 两个要点:
|
||
// · 必须**真的右键**打开菜单 —— ctxMenu 是 React state,注入 DOM 元素不会改变它;
|
||
// · 必须右键在**面板之外**的画布区(面板浮层会吞掉面板内的右键事件)。
|
||
await page.evaluate(() => { const i = document.querySelector('.iwb-fullmask input[placeholder*="搜索"]'); if (i) i.blur() })
|
||
const maskBox = await page.locator('.iwb-fullmask').boundingBox()
|
||
const outsideX = Math.max(10, Math.round(maskBox.x / 2)) // 面板左侧的画布区
|
||
const outsideY = Math.round(maskBox.y + maskBox.height / 2)
|
||
await page.mouse.move(outsideX, outsideY)
|
||
await page.mouse.click(outsideX, outsideY, { button: 'right' })
|
||
expect(await page.locator('.iwb-ctxmenu').count(), '应打开右键菜单').toBeGreaterThan(0)
|
||
|
||
await page.keyboard.press('Escape')
|
||
expect(await page.locator('.iwb-fullmask').count(), '临时层存在时 Esc 不应关闭面板').toBe(1)
|
||
expect(await page.locator('.iwb-ctxmenu').count(), 'Esc 应先关掉右键菜单').toBe(0)
|
||
|
||
// ③ 临时层清掉后,Esc 才能关面板(此时由 app.js 的画布 Esc 负责)
|
||
await page.keyboard.press('Escape')
|
||
expect(await page.locator('.iwb-fullmask').count(), '无临时层时 Esc 应关闭面板').toBe(0)
|
||
})
|
||
|
||
test('② 搜索跨分组 + 当前范围可见', async ({ page }) => {
|
||
await openResourceCenter(page)
|
||
|
||
// 切到「小红书」分组 ⇒ 只应有 1 条
|
||
await page.locator('.iwb-fullpanel .iwb-chip').filter({ hasText: '小红书' }).first().click()
|
||
expect(await page.locator('.iwb-full-grid .iwb-pcard').count(), '小红书分组内应只有 1 条').toBe(1)
|
||
|
||
// 搜「收藏」⇒ 应能搜到**其他分组**里的那条(此前结果为 0)
|
||
await page.locator('.iwb-fullmask input[placeholder*="搜索"]').fill('收藏')
|
||
await W.poll(page, () => page.locator('.iwb-full-grid .iwb-pcard').count()).toBeGreaterThan(0)
|
||
const found = await page.locator('.iwb-full-grid .iwb-pcard').count()
|
||
expect(found, '搜索应跨分组(此前搜不到「收藏」分组的内容)').toBeGreaterThan(0)
|
||
|
||
// 范围必须**可见**:出现「在全部中搜索」提示,且 chips 高亮切到「全部」
|
||
expect(await page.locator('.iwb-lib-searchscope').count(), '应显示搜索范围提示').toBeGreaterThan(0)
|
||
const activeChip = await page.locator('.iwb-fullpanel .iwb-chip-active').first().textContent()
|
||
expect(activeChip, 'chips 高亮应切到「全部」(高亮与结果范围必须一致)').toContain('全部')
|
||
|
||
// 清空搜索 ⇒ 回到原来的分组(「小红书」高亮)
|
||
await page.locator('.iwb-lib-searchscope-back').click()
|
||
await W.poll(page, () => page.locator('.iwb-fullpanel .iwb-chip-active').first().textContent()).toContain('小红书')
|
||
const backChip = await page.locator('.iwb-fullpanel .iwb-chip-active').first().textContent()
|
||
expect(backChip, '清空搜索后应回到原分组').toContain('小红书')
|
||
})
|
||
|
||
test('③ 分类胶囊行单行滚动 + ⚙ 固定在右侧', async ({ page }) => {
|
||
await openResourceCenter(page)
|
||
const info = await page.evaluate(() => {
|
||
const sc = document.querySelector('.iwb-lib-chips-scroll')
|
||
if (!sc) return null
|
||
const chips = document.querySelector('.iwb-lib-chips')
|
||
const mgmt = Array.from(chips.querySelectorAll('.iwb-chip')).find((c) => c.title === '管理分组(重命名 / 排序 / 删除)')
|
||
const cs = getComputedStyle(sc)
|
||
return {
|
||
overflowX: cs.overflowX,
|
||
flexWrap: getComputedStyle(chips).flexWrap,
|
||
mgmtInsideScroll: mgmt ? sc.contains(mgmt) : null,
|
||
mgmtRight: mgmt ? mgmt.getBoundingClientRect().right : null,
|
||
scrollRight: sc.getBoundingClientRect().right,
|
||
}
|
||
})
|
||
expect(info, '应存在 .iwb-lib-chips-scroll 滚动容器').not.toBeNull()
|
||
expect(info.flexWrap, '胶囊行不应换行').toBe('nowrap')
|
||
expect(info.overflowX, '胶囊行应可横向滚动').toBe('auto')
|
||
expect(info.mgmtInsideScroll, '「⚙ 管理分组」应固定在滚动区之外(始终可点)').toBe(false)
|
||
})
|
||
|
||
test('④ 卡片「插入到画布」用 + 图标(不再是 ⬇ 下载)', async ({ page }) => {
|
||
await openResourceCenter(page)
|
||
const btn = page.locator('.iwb-full-grid .iwb-pcard .iwb-pbtns button').first()
|
||
const title = await btn.getAttribute('title')
|
||
expect(title, '插入按钮的 title 应保留「插入到画布」措辞').toContain('插入到画布')
|
||
// + 图标 = 两个 path(水平线 + 垂直线);⬇ 下载图标 = polyline + line + path
|
||
const shape = await btn.evaluate((b) => {
|
||
const svg = b.querySelector('svg')
|
||
return svg ? { tags: Array.from(svg.children).map((c) => c.tagName.toLowerCase()), d: svg.innerHTML.replace(/\s+/g, '') } : null
|
||
})
|
||
expect(shape, '插入按钮应有 svg 图标').not.toBeNull()
|
||
// + 图标的特征:两条 path(一横一竖),下载图标的特征:含 polyline(箭头)
|
||
expect(shape.tags, '插入按钮应使用 + 图标(两个 path),不应含下载图标的 polyline').toEqual(['path', 'path'])
|
||
expect(shape.d, '不应含下载图标的 polyline 箭头').not.toContain('polyline')
|
||
})
|
||
|
||
test('⑤ 手动收起编辑区后记住偏好', async ({ page }) => {
|
||
await openResourceCenter(page)
|
||
|
||
// 点卡片 ⇒ 编辑区展开
|
||
await page.locator('.iwb-full-grid .iwb-pcard').first().click()
|
||
expect(await page.locator('.iwb-full-edit').count(), '点卡片应展开编辑区').toBe(1)
|
||
|
||
// 手动收起 ⇒ 记偏好
|
||
await page.locator('.iwb-full-edit-head button').click()
|
||
expect(await page.locator('.iwb-full-collapsed').count(), '应折叠').toBe(1)
|
||
|
||
// 再点卡片 ⇒ **不再**自动展开(尊重用户偏好)
|
||
await page.locator('.iwb-full-grid .iwb-pcard').nth(1).click()
|
||
expect(await page.locator('.iwb-full-collapsed').count(), '手动收起后点卡片不应再自动展开').toBe(1)
|
||
expect(await page.locator('.iwb-full-edit').count(), '编辑区应保持收起').toBe(0)
|
||
|
||
// 偏好已持久化
|
||
const pref = await page.evaluate(() => localStorage.getItem('iwb_lib_edit_open'))
|
||
expect(pref, '收起偏好应写入 localStorage').toBe('0')
|
||
})
|