test(e2e): 修 pollBoundingBox 自身的二次读取竞态,§14.2 登记逐条处置与探针否证

- 探针 docs/acceptance/2026-10-17-final/probe-logo.mjs 实测加载期无双字标可见窗口
  (390x844 与 1280x800 各 60 次采样,双可见 0;唯一零可见是 t≈43ms header 尚未进 DOM)
  ⇒ 产品不闪,红来自辅助函数:expect.poll 等到非空后再单独读一次 boundingBox,
  hydration 亚 60ms 的重挂让第二次读取返回 null,被误报成「元素在断言瞬间消失」。
- 改为在同一次读取里既判非空又产出值;超时才 throw,且 throw 文案带轮询次数。
  判据不降:仍要求非空 box + 原几何断言,没有 catch、没有静默通过。
- website-acceptance × 4 project 连跑两次各 56 passed。
- §14.2 表逐条登记:fail-open 修复 / 触摸目标与 region 命名 / 限流真因与双向用例 /
  锚点重锚 / 引擎差异取证法(我上一个假设被自己的读数否掉)/ 本条竞态。
This commit is contained in:
zhangxiang committed 2026-10-17 10:59:50 +08:00
1 parent 1e80167b7c
commit fadc35b980
3 files changed
+69 -7

No files matched your search

+20 -7
View File
@@ -14,16 +14,29 @@ interface BoxLike {
* 旧写法 `const box = await expect.poll(() => el.boundingBox()).not.toBeNull()` 的
* await 结果是 undefined(断言不产数),随后的 `if (box)` 恒假 ⇒ 几何断言全部死代码。
* 本函数把「等到非空」与「拿到值」分开,且拿不到值直接 throw(元素消失 ⇒ 用例失败)。
*
* 2026-10-17(§14)再修一次自身的竞态:上一版先 `expect.poll` 等到非空,
* **再单独调一次** `boundingBox()` 取值 —— 两次读取之间若赶上 header hydration 重挂
* (实测探针:加载期无「双字标可见」窗口,只有 t≈43ms 时 header 尚未进 DOM 的一瞬,
* 采样步长 60ms 抓不到的亚 60ms 卸载也足以让第二次读取返回 null),
* 就会把「值拿到了但那一瞬间元素换了节点」误报成「元素在断言瞬间消失」。
* ⇒ 改成在**同一次读取**里既判非空又产出值:读到非空立即返回,超时才 throw。
* 判据强度不降:仍然要求最终拿到一个非空 box 并做几何断言,也没有 catch/静默通过。
*/
async function pollBoundingBox(target: Locator, timeout = 5000): Promise<BoxLike> {
await expect
.poll(() => target.boundingBox(), { timeout, message: '等待 boundingBox 就绪' })
.not.toBeNull();
const box = await target.boundingBox();
if (!box) {
throw new Error('boundingBox 在轮询通过后仍为 null(元素在断言瞬间消失)');
const deadline = Date.now() + timeout;
let lastMissing = 0;
for (;;) {
const box = await target.boundingBox().catch(() => null);
if (box) return box;
lastMissing += 1;
if (Date.now() > deadline) {
throw new Error(
`boundingBox 在 ${timeout}ms 内始终为 null(轮询 ${lastMissing} 次全部拿不到几何 ⇒ 元素不存在或不可见)`,
);
}
await new Promise((r) => setTimeout(r, 50));
}
return box;
}
test.describe('网站全面测试验收', { tag: '@regression' }, () => {