import { test, expect, type Page } from '@playwright/test'; /** * Landing 门禁 ①:布局几何与焦点管理(移植自 `novavis-authority/scripts/verify-landing-layout.py`) * * ## 判据来源与迁移口径 * * 迁移前的脚本是 python + playwright,只在 authority 仓手动跑、从不在任何门禁链里 * (本仓反复记录过那个失效模式:**门禁不在链里就等于没有**)。2026-12-03 landing 迁入本仓, * 四道门禁一并移植为 Playwright spec,由 `npm run test:landing` 承载并进 `test:all`。 * * **判据一个不放宽**,逐条对应原脚本: * * | 原检查项 | 本文件 | * |---|---| * | 1 tabs:各视口下 `.showcase-tabs` 每个 tab 中心不被 `#showcasePause` 覆盖,且无横向溢出 | `showcase tab 行与暂停按钮几何` | * | 2a focus:焦点进入卡片后轮播让位 | `焦点进入卡片后轮播让位` | * | 2b focus:持焦 tab 不被摘掉 tabindex | 同上 | * | 2c focus:失焦后轮播恢复(证明 2a 不是「轮播整体坏了」的假通过) | 同上 | * | 3 roving:方向键后整组只留一只可 Tab 到达,且该只即 active、焦点跟随 | `键盘 roving tabindex 单点不变量` | * | 4 cta:nav 转化入口与信息链接有可视分层 | `nav 转化入口与信息链接分层` | * * ## 两个必须知道的操作前提(原脚本注释里踩过) * * 1. **悬停会冻结轮播**(既有设计)。focus 项全程把鼠标移到 (0,0),否则测的是 hover 冻结 * 而不是 focusin 暂停。 * 2. **视口是被测对象**:每条用例自己 `setViewportSize`,不依赖 project 的 viewport。 */ const LANDING_URL = '/novavis'; /** 与迁移前一致的五个视口(桌面 / 笔记本 / 平板竖屏 / 手机 480 / 手机 375)。 */ const VIEWPORTS = [ { name: '桌面 1440', width: 1440, height: 900 }, { name: '笔记本 1024', width: 1024, height: 768 }, { name: '平板竖屏 768', width: 768, height: 1024 }, { name: '手机 480', width: 480, height: 900 }, { name: '手机 375', width: 375, height: 812 }, ] as const; /** 轮播周期 5500ms + 余量:等待窗口内若未暂停,active tab 必定已切换。 */ const CYCLE_MS = 5500; const WAIT_MS = CYCLE_MS + 2000; /** * tab 行的几何读数(与迁移前 `TAB_INFO` 逐字同义)。 * * 注意这里必须是**真函数**而不是字符串:Playwright 的 JS 版 `page.evaluate(string)` 把入参 * 当「表达式」求值,`"() => {...}"` 求值结果是一个函数对象、序列化成 undefined * (python 版把字符串当函数体,所以原脚本能用字符串;两个绑定语义不同)。 */ function readTabInfo() { const pause = document.querySelector('#showcasePause'); const pr = pause && getComputedStyle(pause).display !== 'none' ? pause.getBoundingClientRect() : null; const covers = (r: DOMRect): boolean => { if (!pr) return false; const cx = r.left + r.width / 2; const cy = r.top + r.height / 2; return pr.left <= cx && cx <= pr.right && pr.top <= cy && cy <= pr.bottom; }; return [...document.querySelectorAll('.showcase-tab')].map((t) => { const r = t.getBoundingClientRect(); const cx = r.left + r.width / 2; const cy = r.top + r.height / 2; return { id: t.id, covered: covers(r), centerInViewport: cx >= 0 && cy >= 0 && cx <= window.innerWidth && cy <= window.innerHeight, }; }); } interface TabInfo { id: string; covered: boolean; centerInViewport: boolean; } async function gotoLanding(page: Page): Promise { await page.goto(LANDING_URL, { waitUntil: 'load' }); // CSS 与 main.js 都是同源静态资源,等 1.2s 让 scroll-spy / 轮播完成首轮初始化 // (迁移前脚本同样 sleep 1.2s,理由相同:过早采样会读到轮播尚未挂上的中间态)。 await page.waitForTimeout(1200); } async function activeTabId(page: Page): Promise { return page.evaluate(() => document.querySelector('.showcase-tab.active')?.id ?? null); } async function focusedTabState(page: Page): Promise<{ id: string; tabindex: string | null } | null> { return page.evaluate(() => { const el = document.activeElement; if (!el || !el.classList || !el.classList.contains('showcase-tab')) return null; return { id: (el as HTMLElement).id, tabindex: el.getAttribute('tabindex') }; }); } test.describe('landing ① 布局几何与焦点', () => { test.beforeEach(async ({ page }) => { await page.setViewportSize({ width: 1440, height: 900 }); await gotoLanding(page); }); test('showcase tab 行与暂停按钮几何(五视口)', async ({ page }) => { const failures: string[] = []; for (const vp of VIEWPORTS) { await page.setViewportSize({ width: vp.width, height: vp.height }); await page.evaluate(() => window.scrollTo(0, 0)); await page.mouse.move(0, 0); await page.locator('.showcase-tabs').scrollIntoViewIfNeeded(); await page.waitForTimeout(300); const tabs = (await page.evaluate(readTabInfo)) as TabInfo[]; expect(tabs.length, `${vp.name}: .showcase-tab 一个都没选中(判据的分母为空)`).toBeGreaterThan(0); const covered = tabs.filter((t) => t.covered).map((t) => t.id); const offscreen = tabs.filter((t) => !t.centerInViewport).map((t) => t.id); const overflow = await page.evaluate( () => document.documentElement.scrollWidth - document.documentElement.clientWidth, ); if (covered.length > 0) failures.push(`${vp.name}: tab 被 #showcasePause 覆盖 ${covered.join(',')}`); if (offscreen.length > 0) failures.push(`${vp.name}: tab 中心不在视口内 ${offscreen.join(',')}`); if (overflow > 1) failures.push(`${vp.name}: 页面横向溢出 ${overflow}px`); } expect(failures, failures.join('\n')).toEqual([]); }); test('焦点进入卡片后轮播让位、持焦不被摘 tabindex、失焦后恢复', async ({ page }) => { await page.mouse.move(0, 0); await page.locator('.showcase-tabs').scrollIntoViewIfNeeded(); await page.waitForTimeout(600); await page.mouse.move(0, 0); const before = await activeTabId(page); expect(before, 'showcase 轮播没有 active tab(用例前提不成立)').not.toBeNull(); await page.evaluate(() => { const t = document.querySelector('.showcase-tab.active'); t?.focus(); }); const state = await focusedTabState(page); expect(state, '未能把焦点放进 tablist(用例前提不成立)').not.toBeNull(); // 2a:持焦期间轮播必须让位 await page.waitForTimeout(WAIT_MS); const after = await activeTabId(page); expect(after, `焦点在卡片内时轮播未让位:active ${before} → ${after}`).toBe(before); // 2b:持焦 tab 不得被摘成 tabindex="-1"(摘掉即被移出 tab 序列,键盘焦点无处可回) const held = await focusedTabState(page); if (held) { expect(held.tabindex, `持焦 tab ${held.id} 的 tabindex 被摘成 -1`).not.toBe('-1'); } // 2c:失焦后必须恢复轮播 —— 否则上一条「不变」可能只是轮播整体失效的假通过 await page.evaluate(() => (document.activeElement as HTMLElement | null)?.blur()); await page.evaluate(() => window.scrollTo(0, 0)); await page.mouse.move(0, 0); await page.locator('#nav').scrollIntoViewIfNeeded(); await page.locator('.showcase-tabs').scrollIntoViewIfNeeded(); await expect .poll(async () => await activeTabId(page), { timeout: WAIT_MS + 1000, message: `失焦后轮播未恢复:active 恒为 ${before}(前一条「持焦不变」可能是轮播整体失效的假通过)`, }) .not.toBe(before); }); test('键盘 roving tabindex 单点不变量', async ({ page }) => { await page.mouse.move(0, 0); await page.locator('.showcase-tabs').scrollIntoViewIfNeeded(); await page.waitForTimeout(400); await page.evaluate(() => { const t = document.querySelector('.showcase-tab.active'); t?.focus(); }); expect(await focusedTabState(page), '未能把焦点放进 tablist(roving 不变量前提不成立)').not.toBeNull(); await page.keyboard.press('ArrowRight'); await page.waitForTimeout(300); const state = await page.evaluate(() => { const tabs = [...document.querySelectorAll('.showcase-tab')]; const isTab = (el: Element | null): el is HTMLElement => !!el && el.classList.contains('showcase-tab'); return { zero: tabs.filter((t) => t.getAttribute('tabindex') === '0').map((t) => t.id), active: tabs.filter((t) => t.classList.contains('active')).map((t) => t.id), focused: isTab(document.activeElement) ? document.activeElement.id : null, }; }); // 单点不变量:整组只能有一只 tab 可被 Tab 键到达 expect( state.zero.length, `方向键切换后整组有 ${state.zero.length} 只 tab 可被 Tab 键到达(${state.zero.join(',')}),违反 roving tabindex 单点(旧 tab 仍留在序列里)`, ).toBe(1); expect(state.active.length, '方向键切换后无 active tab').toBeGreaterThan(0); expect(state.zero[0], `可被 Tab 键到达的 tab(${state.zero[0]})不是 active tab(${state.active[0]})`).toBe( state.active[0], ); expect(state.focused, `方向键切换后焦点未跟随 active tab(持焦 ${state.focused})`).toBe(state.active[0]); }); test('nav 转化入口与信息链接分层', async ({ page }) => { await page.setViewportSize({ width: 1440, height: 900 }); await page.evaluate(() => window.scrollTo(0, 0)); const styles = await page.evaluate(() => { const links = [...document.querySelectorAll('.nav-links a')]; const info = links.find((a) => a.getAttribute('href') === '#features'); const cta = links.find((a) => a.getAttribute('href') === '#contact'); const pick = (el: HTMLElement | undefined) => { if (!el) return null; const s = getComputedStyle(el); return { cls: el.className, border: `${s.borderTopWidth} ${s.borderTopStyle} ${s.borderTopColor}`, bg: s.backgroundColor, color: s.color, }; }; return { info: pick(info), cta: pick(cta) }; }); expect(styles.info, 'nav 信息链接(#features)不存在').not.toBeNull(); expect(styles.cta, 'nav 转化入口(#contact)不存在').not.toBeNull(); expect( styles.cta, 'nav 转化入口与信息链接样式完全相同(无分层)', ).not.toEqual(styles.info); }); });