import { expect, test, type Page } from '@playwright/test' // 站内通知铃铛面板验收:聚合视图(场景 × 自然日 × 事件主体用户)、展开明细、 // 关键字/年月日区间搜索、按组分页、红点按组数。 // 依赖 scripts/mock-admin-server.mjs 的通知夹具:14 行折成 10 组、2 组未读、3 行未读。 // 夹具的 id 与 createdAt 同为插入时单调(与真库一致),场景显式写在夹具里而不是从 id 推。 // 夹具会被"标记已读"改写,而 mock 服务在 reuseExistingServer 下跨轮存活 ⇒ 每条用例前重置, // 否则第二遍跑会带着上一遍的已读态,绝对计数断言全漂。 test.beforeEach(async ({ request }) => { await request.post('/api/admin/notifications/__reset') }) async function clearDateFilter(page: Page) { const picker = page.locator('.bell-panel .bell-date-picker') await picker.hover() await picker.locator('.el-range__close-icon').click() await expect(page.locator('.bell-page-total')).toHaveText('共 10 组 · 明细 14 条') } async function openBell(page: Page) { await page.goto('/admin-vue/') await expect(page.locator('.admin-topbar h1')).toHaveText('用户管理') await page.locator('.admin-notification-bell .bell-trigger').click() await expect(page.locator('.bell-panel')).toBeVisible() // 面板默认按「当天」过滤,夹具是 09-10~09-13:先清空日期才能拿到全量 await clearDateFilter(page) await expect(page.locator('.bell-group').first()).toBeVisible() } /** * 打开日历并等入场动画落定。 * * 两件事必须先做对,否则读数全是假的: * ① 弹层节点是常驻的(Element 用 v-show),未打开时它也在 DOM 里、rect 全 0, * 所以 `toBeAttached()` 不能当「已打开」的判据; * ② 打开瞬间 el-zoom-in-top 把弹层 scaleY 压到 0,格子坐标整体贴到面板顶部, * 此时做命中测试会把「动画中」读成「被面板遮挡」。 */ async function openCalendar(page: Page) { await page.locator('.bell-panel .bell-date-picker').click() const popper = page.locator('.bell-date-popper') await expect(popper).toBeVisible() await page.waitForFunction(() => { const pop = document.querySelector('.bell-date-popper') if (!pop) return false const style = getComputedStyle(pop) return style.transform === 'none' && style.opacity === '1' && pop.getBoundingClientRect().height > 0 }) return popper } test('test_notification_panel_groups_and_pagination', async ({ page }) => { await openBell(page) const panel = page.locator('.bell-panel') const nextPage = panel.getByRole('button', { name: '下一页' }) // 一页 6 组:09-13 的 4 组 + 09-12 的 2 组 ⇒ 日期头 2 个(同一天可以多组) await expect(page.locator('.bell-group')).toHaveCount(6) await expect(page.locator('.bell-day-head')).toHaveCount(2) await expect(page.locator('.bell-day-head').first()).toHaveText(/^(今天|昨天|\d{4}年\d{1,2}月\d{1,2}日)$/) await expect(page.locator('.bell-page-info')).toHaveText('1 / 2') await expect(page.locator('.bell-page-total')).toHaveText('共 10 组 · 明细 14 条') // 聚合行必须把"折叠前几条、其中几条没读"写在行上 await expect(page.locator('.bell-group-count').first()).toHaveText('1 条 · 1 未读') const page2Request = page.waitForRequest( (request) => request.url().includes('/api/admin/notifications/groups?') && request.url().includes('page=2'), ) await nextPage.click() await page2Request await expect(page.locator('.bell-group')).toHaveCount(4) await expect(page.locator('.bell-page-info')).toHaveText('2 / 2') await expect(nextPage).toBeDisabled() }) test('test_notification_panel_search_by_keyword', async ({ page }) => { await openBell(page) const keywordRequest = page.waitForRequest((request) => decodeURIComponent(request.url()).includes('keyword=余额不足'), ) await page.locator('.bell-search-input').fill('余额不足') await keywordRequest // 关键字命中标题/内容:夹具里 5 条含「余额不足」,且分属 5 个不同的组 await expect(page.locator('.bell-group')).toHaveCount(5) await expect(page.locator('.bell-page-total')).toHaveText('共 5 组 · 明细 5 条') await expect(page.locator('.bell-search-reset')).toBeVisible() await page.locator('.bell-search-reset').click() // 重置回到面板的默认视图:清空关键字、日期恢复「当天」(当天不是清空日期) await expect(page.locator('.bell-search-input')).toHaveValue('') await expect(page.locator('.bell-search-reset')).toBeHidden() await expect(page.locator('.bell-empty')).toHaveText('暂无通知') }) /** 红点按**组数**亮:3 条未读落在 2 个组里,徽标就是 2 而不是 3。 */ test('test_notification_panel_badge_counts_groups_not_rows', async ({ page }) => { await page.goto('/admin-vue/') await expect(page.locator('.admin-notification-bell .bell-badge')).toHaveText('2') }) test('test_notification_panel_expand_group_loads_that_group_only', async ({ page }) => { await openBell(page) const groups = page.locator('.bell-group') await expect(groups).toHaveCount(6) const firstDetailRequest = page.waitForRequest((request) => { const url = new URL(request.url()) return ( url.pathname.endsWith('/api/admin/notifications') && url.searchParams.get('scene') === 'secret_balance' && url.searchParams.get('day') === '2026-09-13' && url.searchParams.get('subjectUserId') === '1196' ) }) await groups.first().click() await firstDetailRequest await expect(page.locator('.bell-item--detail')).toHaveCount(1) await expect(page.locator('.bell-group--open')).toHaveCount(1) // 换一组展开:上一组的明细必须收掉,不能两组明细叠在一起 const secondDetailRequest = page.waitForRequest((request) => { const url = new URL(request.url()) return ( url.pathname.endsWith('/api/admin/notifications') && url.searchParams.get('scene') === 'task_failed' && url.searchParams.get('subjectUserId') === '1196' ) }) await groups.nth(1).click() await secondDetailRequest await expect(page.locator('.bell-item--detail')).toHaveCount(2) await expect(page.locator('.bell-group--open')).toHaveCount(1) // 再点一次收起 await groups.nth(1).click() await expect(page.locator('.bell-item--detail')).toHaveCount(0) }) test('test_notification_panel_date_popper_not_covered_by_panel', async ({ page }) => { await openBell(page) await openCalendar(page) // 日历与通知面板同为挂在 body 上的浮层:逐格做命中测试,最上层必须还是日历自己。 // 通知面板 z-index 3200 高于 Element 默认弹层段(自 2000 递增)时,日历整块被压在 // 面板底下,用户看到的就是「日期点不动」——这里必须判红而不是靠肉眼发现。 const covered = await page.evaluate(() => { const pop = document.querySelector('.bell-date-popper') as HTMLElement const panel = document.querySelector('.bell-panel') as HTMLElement const blocked: string[] = [] for (const cell of Array.from(pop.querySelectorAll('td.available'))) { const rect = cell.getBoundingClientRect() if (rect.bottom > window.innerHeight || rect.right > window.innerWidth) continue const hit = document.elementFromPoint(rect.left + rect.width / 2, rect.top + rect.height / 2) if (!hit || !pop.contains(hit)) blocked.push(String(hit?.className ?? hit?.tagName ?? 'null')) } return { blocked, popperZ: getComputedStyle(pop).zIndex, panelZ: getComputedStyle(panel).zIndex } }) expect(covered.blocked, `日历被通知面板遮挡(popper=${covered.popperZ},panel=${covered.panelZ})`).toEqual([]) }) /** 把左栏翻到 2026 年 9 月,返回按日取格的点击器(夹具:09-10 四条、09-12 四条、09-13 六条)。 */ async function openRangeOnSeptember(page: Page) { const popper = await openCalendar(page) await popper.locator('.el-picker-panel__icon-btn.arrow-left').click() const leftPanel = popper.locator('.el-date-range-picker__content').first() const day = (text: string) => leftPanel.locator('td.available .el-date-table-cell__text', { hasText: new RegExp(`^${text}$`) }) await expect(day('10')).toBeVisible() return day } test('test_notification_panel_search_by_day_range', async ({ page }) => { await openBell(page) const day = await openRangeOnSeptember(page) // 09-10 四条折成 3 组(两个用户 × 两个场景组合) const rangeRequest = page.waitForRequest( (request) => request.url().includes('startDate=2026-09-10') && request.url().includes('endDate=2026-09-12'), ) await day('10').click() await day('12').click() await rangeRequest await expect(page.locator('.bell-page-total')).toHaveText('共 6 组 · 明细 8 条') await expect(page.locator('.bell-day-head')).toHaveCount(2) await expect(page.locator('.bell-empty')).toHaveCount(0) // 选完区间后面板必须还开着:日历挂在 body 上,不能被「点击外部关闭」当成面板外 await expect(page.locator('.bell-panel')).toBeVisible() }) test('test_notification_panel_day_range_picked_backwards', async ({ page }) => { await openBell(page) const day = await openRangeOnSeptember(page) // 先点 13 再点 10:控件自己保证起 ≤ 止,落到 09-10 ~ 09-13(全量 14 条 / 10 组) const swappedRequest = page.waitForRequest( (request) => request.url().includes('startDate=2026-09-10') && request.url().includes('endDate=2026-09-13'), ) await day('13').click() await day('10').click() await swappedRequest await expect(page.locator('.bell-page-total')).toHaveText('共 10 组 · 明细 14 条') }) test('test_notification_panel_not_covered_by_page_content', async ({ page }) => { await openBell(page) // 回归:面板挂在顶栏内时曾被页面 el-select 压住(提 z-index 无效),现改为 Teleport 到 body。 // 这里在面板内取多点做命中测试,最上层元素必须属于面板自身。 const covered = await page.evaluate(() => { const panel = document.querySelector('.bell-panel') as HTMLElement const rect = panel.getBoundingClientRect() const points: Array<[number, number]> = [ [rect.left + 30, rect.top + 130], [rect.left + rect.width / 2, rect.top + rect.height / 2], [rect.right - 30, rect.bottom - 60], ] return points .map(([x, y]) => document.elementsFromPoint(x, y)[0]) .filter((el) => !!el && el !== panel && !panel.contains(el)) .map((el) => `${el!.tagName}.${String((el as HTMLElement).className).slice(0, 40)}`) }) expect(covered, '面板被页面元素遮挡').toEqual([]) }) test('test_notification_panel_reference_screenshot', async ({ page }) => { await openBell(page) const tops = await page .locator('.bell-group') .evaluateAll((els) => els.map((el) => el.getBoundingClientRect().top)) for (let i = 1; i < tops.length; i += 1) { expect(tops[i], `第 ${i + 1} 组应排在第 ${i} 组下方`).toBeGreaterThan(tops[i - 1]) } await page.locator('.bell-panel').screenshot({ path: 'test-results/notification-panel-admin-groups.jpg', animations: 'disabled', }) await page.locator('.bell-group').first().click() await expect(page.locator('.bell-item--detail')).toHaveCount(1) await page.locator('.bell-panel').screenshot({ path: 'test-results/notification-panel-admin-expanded.jpg', animations: 'disabled', }) }) /** * 已读只能进明细逐条点(口径见设计):组内未读清零,红点才掉一格。 */ test('test_notification_panel_mark_read_from_detail_drops_group_last', async ({ page }) => { await openBell(page) const group = page.locator('.bell-group').nth(1) await expect(group.locator('.bell-group-count')).toHaveText('2 条 · 2 未读') await group.click() await expect(page.locator('.bell-item--detail')).toHaveCount(2) // 每次点的都必须是「还红着」的那条:已读行点了不做事(onItemClick 直接 return), // 拿 .first() 连点两次会第二次点到刚读过的那条,看着像"没扣数"。 const unread = page.locator('.bell-item--detail.bell-item--unread') await unread.first().click() await expect(unread).toHaveCount(1) await expect(group.locator('.bell-group-count')).toHaveText('2 条 · 1 未读') // 还剩一条未读 ⇒ 这一组仍计为一个未读组,红点不动 await expect(page.locator('.admin-notification-bell .bell-badge')).toHaveText('2') await unread.first().click() await expect(unread).toHaveCount(0) await expect(group.locator('.bell-group-count')).toHaveText('2 条') await expect(page.locator('.admin-notification-bell .bell-badge')).toHaveText('1') })