导航由全宽贴顶条改为内缩白浮条:top 48 / 高 64,左右对齐 container-x 的 gutter。 值走 px 令牌(--header-inset-top / --header-height),避免根字号 16/17/18px 导致 rem 漂移。 契约变化:新增 data-header-state="top|scrolled" 与浮条测试锚点;外壳仍 top-0, chrome-persistence 既有的「滚动后 top===0」断言保持不变,另加浮条 rect(48/64)断言; 移动抽屉起点改 calc(inset + height)。 基线:ADR-0012 预测 180 张,实测 195 张(32 页 × 5 + 主题首页 10 + 页头 5 + 元素级 20), 逐项归因后以 --update-snapshots=changed 写入;独立只读复跑 225 passed / 0 failed / EXIT=0(9.0 分钟)。 门禁:type-check / check:grammar 21/21 / check:motion / check:a11y 全绿,lint 0 error / 88 warning, test:unit 159 套件 / 1883 例,导航三 spec 52 passed / 8 skipped(skip 均既有条件)。 OPEN-02a 关闭;OPEN-02b(滚动收方汉堡)维持不做。
292 lines
15 KiB
TypeScript
292 lines
15 KiB
TypeScript
import { test, expect } from './fixtures';
|
||
import { expectHydrated } from './hydrated';
|
||
|
||
/**
|
||
* 站点外壳(header / 移动抽屉 / mega 面板)的**持久性**回归。
|
||
*
|
||
* 三个缺陷同源于 2026-11-24 的对标审计,每一个都有「文档级验收看不见」的共同特征:
|
||
*
|
||
* 1. **页头不吸顶**(P0-1)。`<header>` 写的是 `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 });
|
||
}
|
||
});
|
||
|
||
/**
|
||
* 内缩白浮条(ADR-0012 §4 / `OPEN-02a`)。
|
||
*
|
||
* 为什么断言 `getComputedStyle` 而不只读 `getBoundingClientRect`:
|
||
* 本仓根字号随断点变 16/17/18px,若有人把 `--header-inset-top` 换回 `top-12`(3rem)、
|
||
* 把高度换回 `h-16`(4rem),类名照样在、type-check 照样过,而实测会变成 48/51/54 与 64/68/72。
|
||
* computed style 直接读解析后的 px,与视口/滚动无关。
|
||
*
|
||
* ⚠ 移动 project(`isMobile: true`)**不做** rect 断言:移动视口模拟下 `position: fixed`
|
||
* 的 `getBoundingClientRect().top` 会随视觉视口偏移漂移(本轮实测同一用例连跑得到 0 / 48 / 139
|
||
* 三个值),那是模拟层的量法噪声,不是产品缺陷。桌面 project 才断言「距顶 48 + 左右内缩」。
|
||
*/
|
||
test('页头是内缩浮条:padding-top 48px / 高 64px(px 令牌,不是 rem 漂移值)', async ({ page }, testInfo) => {
|
||
const isMobileProject = testInfo.project.name.includes('mobile');
|
||
|
||
await page.goto('/', { waitUntil: 'domcontentloaded' });
|
||
const wrap = page.locator('header > div').first();
|
||
const bar = page.getByTestId('header-bar');
|
||
await expect(bar).toBeVisible({ timeout: 15000 });
|
||
|
||
// ① 解析后的 px —— 这条同时钉住「token 被删/被换成 rem 档」两种腐化
|
||
await expect
|
||
.poll(() => wrap.evaluate((el) => getComputedStyle(el).paddingTop), { timeout: 15000 })
|
||
.toBe('48px');
|
||
await expect
|
||
.poll(() => bar.evaluate((el) => getComputedStyle(el).height), { timeout: 15000 })
|
||
.toBe('64px');
|
||
|
||
// ② 桌面档:距视口顶 48、自身高 64、左右内缩;滚动后不收起、不位移
|
||
if (!isMobileProject) {
|
||
await page.setViewportSize({ width: 1600, height: 900 });
|
||
|
||
// ⚠ 必须等入场动画交接(`data-content-ready=1`)后再量:`page-transition.tsx` 的
|
||
// `will-change: transform` 会劫持 `fixed` 的包含块,此时滚动 600 ⇒ 浮条 top 读到
|
||
// **48−600 = −552**(本用例首版实测就是这个值)。这与 §5.52 是同一个机理,
|
||
// 既有两条用例已经在用这个交接标记,这里复用而不是新造锚点。
|
||
await expect
|
||
.poll(() => page.evaluate(() => document.documentElement.getAttribute('data-content-ready')), { timeout: 15000 })
|
||
.toBe('1');
|
||
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,此时量 fixed 元素读到的是动画帧',
|
||
).toBeNull();
|
||
|
||
const readBar = () =>
|
||
bar.evaluate((el) => {
|
||
const r = el.getBoundingClientRect();
|
||
return {
|
||
top: Math.round(r.top),
|
||
height: Math.round(r.height),
|
||
left: Math.round(r.left),
|
||
right: Math.round(r.right),
|
||
};
|
||
});
|
||
|
||
await expect.poll(async () => (await readBar()).top, { timeout: 15000 }).toBe(48);
|
||
const top = await readBar();
|
||
expect(top.top, '浮条顶距必须是 48px').toBe(48);
|
||
expect(top.height, '浮条高度必须是 64px').toBe(64);
|
||
expect(top.left, '1600 视口下浮条必须内缩(左边界 > 0)').toBeGreaterThan(0);
|
||
expect(top.right, '1600 视口下浮条必须内缩(右边界 < 视口宽)').toBeLessThan(1600);
|
||
|
||
await page.evaluate(() => {
|
||
document.documentElement.style.scrollBehavior = 'auto';
|
||
window.scrollTo(0, 600);
|
||
});
|
||
await expect.poll(() => page.evaluate(() => Math.round(window.scrollY)), { timeout: 5000 }).toBe(600);
|
||
|
||
const scrolled = await readBar();
|
||
expect(scrolled.top, '滚动后浮条仍应在 48px').toBe(48);
|
||
expect(scrolled.height, '滚动后浮条高度不变(02b「收成方汉堡」已判定不做)').toBe(64);
|
||
await expect(page.locator('header'), '滚动后应切到 scrolled 状态').toHaveAttribute('data-header-state', 'scrolled');
|
||
|
||
// 外壳仍贴顶 —— chrome-persistence 既有断言的形态不变
|
||
await expect
|
||
.poll(() => page.evaluate(() => Math.round(document.querySelector('header')!.getBoundingClientRect().top)), { timeout: 5000 })
|
||
.toBe(0);
|
||
}
|
||
});
|