import { test, expect } from './fixtures'; import { expectHydrated } from './hydrated'; /** * 站点外壳(header / 移动抽屉 / mega 面板)的**持久性**回归。 * * 三个缺陷同源于 2026-11-24 的对标审计,每一个都有「文档级验收看不见」的共同特征: * * 1. **页头不吸顶**(P0-1)。`
` 写的是 `position: fixed`,但 `PageTransition` 的 * `motion.div` 常驻内联 `will-change: transform, opacity` —— `will-change: transform` * 会让该元素成为 `position: fixed` 后代的**包含块**(CSS Containment / transform 语义), * 于是 header 相对「文档高度」的那个祖先定位,而不是相对视口。实测 `scrollY=1500` 时 * `header.top === -1500`,computed `position` 仍是 `fixed`。 * 这里断言的是**行为**(滚动了它还在视口顶部),不是 computed style —— 后者在缺陷态下 * 依然是 `fixed`,拿它当判据等于什么都没测。 * * 2. **移动抽屉透明白底**(P0-2)。`bg-bg-primary/98` 的 `/98` 不在 Tailwind 的 opacity * scale 里,编译器**静默不生成**该规则(同族 `/95` 正常生成)。抽屉文字因此直接压在 * 首页深色建筑照与朱砂 CTA 上。断言 `backgroundColor` 不能是 `rgba(0, 0, 0, 0)`: * 「class 写在源码里」和「CSS 真的存在」是两件事,只有前者能被 grep 验到。 * * 3. **能力 mega 面板超视口**(P1-2)。面板 946px 高、`overflow-y: visible`,1280×720 * 下把最后一个入口裁掉 289px 且无处可滚。断言面板底部不超过视口,或面板自身可滚。 * * 敏感性(改回即转红): * - 恢复 `page-transition.tsx` 的常驻 `willChange` ⇒ 用例 1 红; * - 把 `bg-bg-primary/95` 改回 `/98` ⇒ 用例 2 红; * - 去掉 mega 面板的 `max-h`/`overflow-y-auto` ⇒ 用例 3 在 1280×720 下红。 */ /** 页头在滚动后必须仍贴在视口顶部(容差 2px,吸收亚像素与高度过渡抖动)。 */ const HEADER_STICK_TOLERANCE = 2; for (const scrollY of [1500, 3000]) { test(`页头在滚动 ${scrollY}px 后仍吸附视口顶部`, async ({ page }) => { await page.goto('/', { waitUntil: 'domcontentloaded' }); const header = page.locator('header').first(); await expect(header).toBeVisible({ timeout: 15000 }); // ⚠ 必须等 hydration 再滚。PageTransition 在 mounted=false 时直接 `return <>{children}`, // 那层 motion.div **此时还不存在**(实测:`domcontentloaded` 就滚,读到「正确」的 top=0 —— // 那是在测一个还没有缺陷的 DOM,本用例在本缺陷下会假绿,首轮实测就是绿的)。 // // 判据用项目既定的 `expectHydrated`(React 写在宿主元素上的 `__reactProps$` 内部键): // 不往 DOM 塞属性(那会触发 React 对多余属性的 hydration warning,用例自己制造噪声), // 也不锚 `will-change`(缺陷修好后它已被删除,锚它 = 自证失效)。 await expectHydrated(header); // ⚠ 再等**入场动画走完**。这是一条独立于 hydration 的前置条件,实测必须有: // framer-motion 在 280ms 入场期间会给 PageTransition 的包裹层写 `transform: matrix(...)`, // 而 transform 与 will-change 一样会创建 fixed 的包含块。实测(scrollY → header.top): // 500 → -483(matrix 在)、1000 → -1000(matrix 在)、1500 → 0(transform: none)。 // 也就是说「动画进行中页头不吸顶」是**瞬时且固有**的,不是缺陷 —— 缺陷是 will-change 让它 // **永久**如此。用例必须测稳态,否则会把动画帧当成回归。 // // 门控分两步,两步都不能省: // // ① **等 `data-content-ready` 置位**(`(marketing)/template.tsx` 在水合后 800ms 写入, // 800ms > 最长入场 280ms,成立的唯一理由就是「入场动画已结束、控制权交还 framer」)。 // 这一条同时堵住两个坑: // · 直接 `expect.poll(transform === 'none')` 会在动画**尚未开始**那刻命中(framer 还没写 // matrix),然后放行 —— 那是假绿; // · 固定 `waitForTimeout(600)` 在**慢机器 / 移动 project 上不够**。实测 chromium-mobile // 首轮就死在这里:600ms 时仍读到 `matrix(0.999928, 0, 0, 0.999928, 0, 0.028695)`, // 而同一台机器等 3s 后 transform 链为空、`headerTop=0`(产品是好的,是本用例等短了)。 // `data-content-ready` 是项目既有判据,不是为本用例新造的锚点:它表达的正是 // 「动画该结束了」,比任何猜出来的毫秒数都可复算。 await expect .poll(() => page.evaluate(() => document.documentElement.getAttribute('data-content-ready')), { timeout: 15000, message: '入场动画交接标记 data-content-ready 未置位 —— 页面可能停在动画中,此时测的是动画帧不是稳态', }) .toBe('1'); // ② 交接之后再硬断言一次 transform 已归零:不轮询、不等待,只用它抓「交接了但仍在动」的真缺陷。 await expect( await page.evaluate(() => { let n: Element | null = document.querySelector('header'); while (n && n !== document.documentElement) { if (getComputedStyle(n).transform !== 'none') return getComputedStyle(n).transform; n = n.parentElement; } return null; }), '祖先链上仍有 transform,入场动画未结束(此时测量会读到动画帧,不是稳态)', ).toBeNull(); await page.evaluate((y) => { // scroll-behavior: smooth 会让 scrollTo 与随后的读数竞态 —— 先显式关掉。 document.documentElement.style.scrollBehavior = 'auto'; window.scrollTo(0, y); }, scrollY); await expect .poll(() => page.evaluate(() => Math.round(window.scrollY)), { timeout: 5000 }) .toBe(scrollY); // 缺陷态:top 恒等于 -scrollY 且不随轮询改善(fixed 被包含块劫持,随文档一起滚走)。 // 正确态:top 收敛到 0±容差。用 poll 吸收 header 高度 80→64 过渡期的亚像素抖动; // 缺陷态下它不会「恰好」收敛,只会超时并打印末次读数。 await expect .poll(() => page.evaluate(() => document.querySelector('header')!.getBoundingClientRect().top), { timeout: 5000, message: `页头在 scrollY=${scrollY} 时应贴在视口顶部(top≈0)。` + `若收敛到 -${scrollY},说明祖先有 transform / will-change 劫持了 fixed 的包含块`, }) .toBeGreaterThanOrEqual(-HEADER_STICK_TOLERANCE); }); } test('移动抽屉的面板背景不得是透明的', async ({ page }) => { await page.setViewportSize({ width: 390, height: 844 }); await page.goto('/', { waitUntil: 'domcontentloaded' }); const button = page.getByTestId('mobile-menu-button'); await expect(button).toBeVisible({ timeout: 15000 }); const drawer = page.getByTestId('mobile-navigation'); await expect(async () => { if (!(await drawer.isVisible())) await button.click(); await expect(drawer).toBeVisible({ timeout: 1000 }); }).toPass({ timeout: 15000 }); // 抽屉的滚动容器(面板本体)。取第一个带 overflow-y 的子节点,退化为抽屉自身。 const panel = drawer.locator('div[class*="overflow-y-auto"]').first(); const target = (await panel.count()) > 0 ? panel : drawer; const bg = await target.evaluate((el) => getComputedStyle(el).backgroundColor); expect( bg, `抽屉面板背景为 ${bg}。rgba(0, 0, 0, 0) 表示 class 存在但 CSS 未生成` + `(Tailwind opacity scale 之外的值如 /98 会静默失效,同族 /95 正常)`, ).not.toBe('rgba(0, 0, 0, 0)'); // 同时断言它真的是「不透明到能承载正文」:alpha 至少 0.9。 const alpha = await target.evaluate((el) => { const m = /rgba?\(([^)]+)\)/.exec(getComputedStyle(el).backgroundColor); if (!m || !m[1]) return null; const parts = m[1].split(/[,/]/).map((s) => parseFloat(s.trim())); return parts.length >= 4 ? parts[3] : 1; }); expect(alpha, `抽屉面板背景 alpha=${alpha},正文会与页面内容叠印`).toBeGreaterThanOrEqual(0.9); }); test('能力 mega 面板在 1280x720 下不被裁切,或自身可滚', async ({ page }) => { // 1280×720 是本仓实测最容易翻车的桌面尺寸(修复前面板 946px、overflow-y: visible)。 await page.setViewportSize({ width: 1280, height: 720 }); await page.goto('/', { waitUntil: 'domcontentloaded' }); const nav = page.locator('nav[aria-label="主导航"]'); await expect(nav).toBeVisible({ timeout: 15000 }); const trigger = nav.locator('button:has-text("能力")').first(); await expect(trigger).toBeVisible(); await expect(trigger).toHaveAttribute('aria-expanded', 'false'); // ⚠ **必须 hover 打开,不能 click**。本仓 mega 是 hover 驱动的(`onMouseEnter`), // `click()` 只切 `aria-expanded` 所依赖的另一条路径、面板根本不挂载 —— // 首轮实测:click 之后 `[class*="top-full"]` 在整页命中 **0** 个,用例死在 // 「locator 等不到元素」而不是死在裁切判据上(假红,且指向错误方向)。 // 独立验证读数(同机、同产物、hover 打开):面板 rect h=612、`max-h: 612px`、 // `overflow-y: auto`、`scrollHeight 935 > clientHeight 610` ⇒ 自身可滚,`/products` 入口在视口内。 // 与 `e2e/nav-dropdown.spec.ts` 的 openAxis 同一姿势(那里也是 hover + toPass 重试): // 入场动画期间的指针可能落在按钮尚未覆盖的帧上,mouseenter 不会补发,故要重试。 await expectHydrated(trigger); await expect(async () => { await page.mouse.move(0, 0); await trigger.hover(); await expect(trigger).toHaveAttribute('aria-expanded', 'true', { timeout: 1000 }); }).toPass({ timeout: 20000 }); // 面板 = 导航地标里 `top-full` 容器内的第一层 div(把选择器收进 nav,避免命中别处的 absolute)。 const panel = nav.locator('[class*="top-full"] > div').first(); await expect(panel).toBeVisible({ timeout: 5000 }); const { bottom, overflowY, scrollable } = await panel.evaluate((el) => { const cs = getComputedStyle(el); return { bottom: el.getBoundingClientRect().bottom, overflowY: cs.overflowY, scrollable: el.scrollHeight > el.clientHeight + 1, }; }); const vh = 720; const clipped = Math.round(bottom - vh); const fits = clipped <= 0; const usable = fits || scrollable || overflowY === 'auto' || overflowY === 'scroll'; expect( usable, `mega 面板底部 ${Math.round(bottom)}px,超出视口 ${clipped}px,` + `overflowY=${overflowY}、内部可滚=${scrollable} ⇒ 最后几个入口既不可见也点不到`, ).toBe(true); // 最后一组入口必须真的可达(scrollIntoView 后可见并可点)。 const lastLink = panel.getByRole('link', { name: /查看全部能力|产品矩阵总览/ }).last(); if ((await lastLink.count()) > 0) { await lastLink.scrollIntoViewIfNeeded(); await expect(lastLink).toBeVisible({ timeout: 3000 }); } });