test(visual): 首页缺 data-cms-state 时判红,避免守卫退化成死判据

上一版写成「有标记就校验、没有就跳过」。可标记正是这条守卫存在的理由:
prop 没透传、组件换成旧版、属性改名,任何一种都会让守卫静默失效,
而用例照样绿——正是「备好但从未跑成」的那类自伤。

改为按路径判定:`/` 必须带标记且为 ok,否则按名字报错;其它页面跳过。
正控:25 个首页相关用例全绿(标记确实在 DOM 里且为 ok)。
负控:临时把属性去掉 ⇒ 立刻红,报「首页缺少 data-cms-state 标记」。
This commit was merged in pull request #60.
This commit is contained in:
zhangxiang committed 2026-10-21 16:57:47 +08:00
1 parent 2c974d1971
commit 03be1c9e36
1 file changed
+8 -1
+8 -1
View File
@@ -112,10 +112,17 @@ async function waitForRevealsSettled(page: Page) {
/**
* 首页把 CMS 读数状态写在 `data-cms-state` 上。退化时页面照常 200、只是整套落到常量版式,
* 像素比对只会给出一团无从归因的差异 —— 在这里先按名字判一次。
* 首页**必须有**这个标记:标记消失(改坏 prop / 改回旧组件)时不能静默放行,
* 否则这条守卫会退化成永不生效的死判据。其它页面不标,跳过。
*/
async function assertCmsStateOk(page: Page) {
const isHome = new URL(page.url()).pathname === '/';
if (!isHome) return;
const marker = page.locator('main [data-cms-state]').first();
if ((await marker.count()) === 0) return;
if ((await marker.count()) === 0) {
throw new Error('首页缺少 data-cms-state 标记:CMS 退化守卫已失效(见 lessons §5.51)');
}
const state = await marker.getAttribute('data-cms-state');
if (state !== 'ok') {
throw new Error(