import { test, expect, type Page } from '@playwright/test'; /** * Landing 门禁 ③:首屏演示剧本 + 无 JS 降级 + reduced-motion(移植自 * `novavis-authority/scripts/landing-demo-verify.py`) * * ## 这道门禁守的是「内容在、但看不见」 * * 2026-11-04 的 Blocking①(屏 2 白底白字)与 2026-11-07 的 BLOCKING-2(nojs 断言只读 * `textContent` + computed `display`,注入 `opacity:0;visibility:hidden` 后仍 PASS)是同一 * 失效模式。所以无 JS 断言必须是**四层真实可见性**,缺一不可: * * ① `checkVisibility({checkOpacity, checkVisibilityCSS})` —— 浏览器自己的口径 * ② 祖先链上 opacity 有效值 > 0.05(旧内核 checkVisibility 对 opacity 的处理不一致) * ③ 元素矩形有面积(排除被 layout 折叠) * ④ 文字非空 * * 另加两条**防恒真**的断言(本仓反复踩过「判据通过态是 0 或空集时,0 本身不是证据」): * - 反向:仅动画层的步骤徽标在无 JS 时必须**不可见**(`badge_visible === 0`); * - 金丝雀:徽标选择器必须真的命中元素(`badge_count > 0`),否则上一条恒真。 * * ## 采样时刻未改 * * `DUR = {s1:2.6, s2:3.4, s3:3.2, s4:3.8, hold:1.6}`,故 1.4 / 4.4 / 7.6 / 11.6 / 16.4 的 * 采样点落在各自的步内。**时刻不改**,因为改动时刻会让「脚本采样点」与「动画真实时间轴」 * 脱钩,那是判据自伤而不是适配(原脚本注释原文)。 */ const LANDING_URL = '/novavis'; /** 采样时刻(秒,相对页面 load 完成)。 */ const STEPS = [ { name: 'step1-create', at: 1.4 }, { name: 'step2-open', at: 4.4 }, { name: 'step3-import', at: 7.6 }, { name: 'step4-ready', at: 11.6 }, { name: 'loop-restart', at: 16.4 }, ] as const; /** 步号 → 该步应显示的徽标文案(与 index.html 的 `data-badge` 一一对应)。 */ const BADGE_OF_STEP: Record = { 'demo-step-1': '新建任务', 'demo-step-2': '打开任务', 'demo-step-3': '导入数据', 'demo-step-4': '就绪', }; async function heroClass(page: Page): Promise { return page.evaluate(() => document.getElementById('heroDemo')?.className ?? ''); } /** * 当前**真实可见**的徽标文案(三层可见性,与 nojs 判定同口径)。 */ async function visibleBadges(page: Page): Promise { return page.evaluate(() => { const els = [...document.querySelectorAll('.demo-chrome-badge-item')]; return els .filter((el) => { const r = el.getBoundingClientRect(); if (!el.checkVisibility({ checkOpacity: true, checkVisibilityCSS: true })) return false; if (r.width <= 0 || r.height <= 0) return false; let op = 1; let n: Element | null = el; while (n && n.nodeType === 1) { const o = parseFloat(getComputedStyle(n).opacity); if (!Number.isNaN(o)) op *= o; if (op <= 0.05) return false; n = n.parentElement; } return true; }) .map((el) => (el.textContent ?? '').trim()); }); } /** * 徽标正向断言:每个采样点上当前步必须**恰好显示 1 个**徽标,且文案与该步同义。 * * 为什么是「恰好 1 个」而不是「>= 1」:同时显示两个说明映射有重叠(正是 2026-11-07 首轮 * 「徽标错位一整步」的伴生症状),那会让观众同时看到两个互斥的进度。 */ function checkBadgeMatchesStep( classSamples: Record, badgeSamples: Record, ): string[] { const problems: string[] = []; for (const [name, cls] of Object.entries(classSamples)) { const step = Object.keys(BADGE_OF_STEP).find((st) => cls.includes(st)); if (step === undefined) continue; // loop-restart 采样点落在复位瞬间,不判 const want = BADGE_OF_STEP[step]; const got = badgeSamples[name] ?? []; if (got.length !== 1) { problems.push(`${name}: ${step} 应恰好显示 1 个徽标,实测 ${got.length} 个 ${JSON.stringify(got)}`); } else if (got[0] !== want) { problems.push(`${name}: ${step} 应显示「${want}」,实测「${got[0]}」`); } } return problems; } test.describe('landing ③ 首屏剧本与降级', () => { test('剧本四步推进 + 徽标一一对应 + 循环复位', async ({ page }) => { const pageErrors: string[] = []; page.on('pageerror', (e) => pageErrors.push(String(e))); await page.goto(LANDING_URL, { waitUntil: 'load' }); const start = Date.now(); const classSamples: Record = {}; const badgeSamples: Record = {}; for (const step of STEPS) { const delta = step.at * 1000 - (Date.now() - start); if (delta > 0) await page.waitForTimeout(delta); classSamples[step.name] = await heroClass(page); badgeSamples[step.name] = await visibleBadges(page); } expect(pageErrors, `landing 页有 JS 运行时错误:${pageErrors.join(' | ')}`).toEqual([]); // 循环复位:采样到第 1 步说明时间轴在循环 expect( classSamples['loop-restart'], `剧本未循环复位到 demo-step-1:实测 ${classSamples['loop-restart']}`, ).toContain('demo-step-1'); const problems = checkBadgeMatchesStep(classSamples, badgeSamples); expect(problems, problems.join('\n')).toEqual([]); }); test('无 JS 降级:内容在且看得见(四层真实可见性)', async ({ browser }) => { // 独立 context 关掉 JS:用 browser 而不是 page fixture(后者已带 JS) const context = await browser.newContext({ javaScriptEnabled: false, viewport: { width: 1440, height: 900 } }); const page = await context.newPage(); await page.goto(LANDING_URL, { waitUntil: 'load' }); await page.waitForTimeout(500); const nojs = await page.evaluate(() => { const rows = [...document.querySelectorAll('.nv-home-row')]; const visibleRows = rows.filter((el) => { const r = el.getBoundingClientRect(); // ② 祖先链有效 opacity let op = 1; let n: Element | null = el; while (n && n.nodeType === 1) { const o = parseFloat(getComputedStyle(n).opacity); if (!Number.isNaN(o)) op *= o; if (op <= 0.05) return false; n = n.parentElement; } // ① + ③ + ④ return ( el.checkVisibility({ checkOpacity: true, checkVisibilityCSS: true }) && r.width > 0 && r.height > 0 && (el.textContent ?? '').trim().length > 0 ); }).length; const badgeItems = [...document.querySelectorAll('.demo-chrome-badge-item')]; const visibleBadge = badgeItems.filter((el) => { const r = el.getBoundingClientRect(); return el.checkVisibility({ checkOpacity: true, checkVisibilityCSS: true }) && r.width > 0 && r.height > 0; }).length; const pager = document.querySelector('.nv-home-pager b'); const cols = document.querySelector('.nv-home-cols'); return { taskTotal: pager?.textContent ?? null, currentRow: document.querySelectorAll('.nv-home-row.is-current').length, colsVisible: cols ? cols.checkVisibility({ checkOpacity: true, checkVisibilityCSS: true }) : false, demoRowsSeen: rows.length, rowsVisible: visibleRows, badgeCount: badgeItems.length, badgeVisible: visibleBadge, // 状态栏的静态读数:无 JS 时也应直接可见 bodyText: (document.body.textContent ?? '').length, }; }); // 台账读数是**静态数据**(6 个任务),不是动画指标 —— 它旁边就列着 4 行真实任务, // 若写成 0 就与内容自相矛盾。故无 JS 时必须等于 6。 expect(nojs.taskTotal, `无 JS 时任务总数应为 6,实测 ${nojs.taskTotal}`).toBe('6'); expect(nojs.currentRow, '无 JS 时当前行(is-current 是静态类,不靠 JS 加)应恰有 1 行').toBe(1); expect(nojs.colsVisible, '无 JS 时列头不可见(视口锁定后列头只渲染一次,它是纯静态结构)').toBe(true); expect(nojs.demoRowsSeen, '无 JS 时一行任务都没渲染(判据分母为空)').toBeGreaterThan(0); expect( nojs.rowsVisible, `无 JS 时任务行不可见:${nojs.rowsVisible}/${nojs.demoRowsSeen}(内容在、但被 opacity/visibility 藏起来)`, ).toBe(nojs.demoRowsSeen); // 反向断言 + 金丝雀:仅动画层的徽标无 JS 时不得可见;选择器必须非空否则上一条恒真 expect(nojs.badgeCount, '徽标选择器零命中(反向断言会恒真)').toBeGreaterThan(0); expect(nojs.badgeVisible, `仅动画层的徽标在无 JS 时不得可见,实测 ${nojs.badgeVisible} 个`).toBe(0); await context.close(); }); test('prefers-reduced-motion:直达终态、不排时间轴', async ({ page }) => { await page.emulateMedia({ reducedMotion: 'reduce' }); await page.goto(LANDING_URL, { waitUntil: 'load' }); await page.waitForTimeout(1200); const hero = await heroClass(page); expect(hero, `reduced-motion 下应挂 demo-final,实测 className="${hero}"`).toContain('demo-final'); const readings = await page.evaluate(() => ({ taskTotal: document.querySelector('.nv-home-pager b')?.textContent ?? null, currentRow: document.querySelectorAll('.nv-home-row.is-current').length, })); expect(readings.taskTotal, 'reduced-motion 下任务总数应为 6').toBe('6'); expect(readings.currentRow, 'reduced-motion 下当前行应恰有 1 行').toBe(1); }); });