import { test, expect, type Page } from './fixtures'; /** * 页脚「联系我们」两行的图标必须与首行文字的水平中线重合(lessons §5.53)。 * * 旧写法是 `flex items-start` + 图标固定 `mt-0.5`(0.125rem)。本项目把 Tailwind 的 * `leading-relaxed` 重映射成 `--line-height-relaxed: 1.85`(不是默认 1.625),根字号又有 * 16 / 17 / 18 三档(globals.css:542-558)⇒ 手拍的固定偏移在每档都比文字中线高 3px 上下, * 且随视口变化。像素门限(maxDiffPixels 200)对 3px 的图标位移只给出「刚好越线」的读数, * 指望它判红会时红时绿,所以这里按几何单独判一次。 * * 判据取**盒子**,不取 `Range.getClientRects()`:后者的高度是字体 ascent+descent(不是行盒), * top 还被整数量化,与行盒中心存在 0.45px 级的字体相关偏置 —— 拿它当参照会既可能假红也可能假绿。 */ const ROWS = '[data-testid="card-contact"] li'; const TOLERANCE = 0.5; // px;修复前偏差 2.0–3.3px,修复后应 ≈0 type RowMeasurement = { text: string; iconHeight: number; lineHeight: number; /** 图标盒中心 − 首行文字中线(正数=图标偏低) */ delta: number; tag: string; }; async function measureContactRows(page: Page): Promise { return page.evaluate((sel) => { return Array.from(document.querySelectorAll(sel)).map((li) => { const svg = li.querySelector('svg'); const textEl = li.querySelector('span:last-child, a'); if (!svg || !textEl) { return { text: '', iconHeight: 0, lineHeight: 0, delta: Number.NaN, tag: '' }; } const icon = svg.getBoundingClientRect(); const box = textEl.getBoundingClientRect(); const lineHeight = parseFloat(getComputedStyle(textEl).lineHeight); // 地址是普通块级文本:内容盒顶 + 行高/2 = 首行行盒中心(换行后首行依然成立)。 // 邮箱是 inline-flex + items-center 的链接:文字在盒内垂直居中 ⇒ 取盒中心。 const lineCenter = textEl.tagName === 'A' ? box.top + box.height / 2 : box.top + lineHeight / 2; return { text: (textEl.textContent ?? '').trim(), iconHeight: icon.height, lineHeight, delta: icon.top + icon.height / 2 - lineCenter, tag: textEl.tagName, }; }); }, ROWS); } const VIEWPORTS = [ { name: 'desktop-1440(根字号 18)', width: 1440, height: 900, rootFontSize: 18 }, { name: 'tablet-834(根字号 17)', width: 834, height: 1194, rootFontSize: 17 }, { name: 'mobile-390(根字号 16)', width: 390, height: 844, rootFontSize: 16 }, ] as const; test.describe('页脚联系方式:图标与首行文字同中线', { tag: '@regression' }, () => { for (const vp of VIEWPORTS) { test(vp.name, async ({ page }) => { await page.setViewportSize({ width: vp.width, height: vp.height }); await page.goto('/', { waitUntil: 'domcontentloaded' }); await page.evaluate(() => document.fonts.ready.then(() => undefined)); // 判据本身的分母:先证明这次测到的是「三档根字号」里的这一档, // 否则断言的是一张没走到目标断点的页面。 const rootFontSize = await page.evaluate( () => parseFloat(getComputedStyle(document.documentElement).fontSize), ); expect(rootFontSize, `视口 ${vp.width}px 的根字号应为 ${vp.rootFontSize}px`).toBe( vp.rootFontSize, ); const rows = await measureContactRows(page); // 防空转:行数必须正好两行(地址 + 邮箱),缺一行下面的逐行断言就会在空集上放行。 expect(rows.length, 'card-contact 必须正好两行(地址 + 邮箱)').toBe(2); // 逐行收集而不是逐行 expect:红的时候要一次看到两行、三档的读数,否则排查只能一档一档试。 const problems: string[] = []; for (const [index, row] of rows.entries()) { const label = `第 ${index + 1} 行(${row.tag || '缺 svg/文本'})`; if (row.text.length === 0) problems.push(`${label} 文本为空 ⇒ 行盒塌陷(site-config 默认 address 为空串)`); if (row.iconHeight <= 0) problems.push(`${label} 没有可量的 svg`); if (row.lineHeight <= 0) problems.push(`${label} 没有解析出 px 行高`); if (Math.abs(row.delta) > TOLERANCE) { problems.push( `${label} 图标中心与首行文字中线偏差 ${row.delta.toFixed(3)}px(容差 ${TOLERANCE}px;` + `文本=${row.text.slice(0, 12)},图标高=${row.iconHeight},行高=${row.lineHeight})`, ); } } expect(problems, `根字号 ${rootFontSize}px · 视口 ${vp.width}px\n${problems.join('\n')}`).toEqual([]); }); } });