Skip to content

flaky(storybook): project-groups focus assertion races the post-close rAF focus restore; dialog query races one-frame menu-close handover #4884

Closed

Description

@orangeCatDeveloper

Summary

product-sidebar-session-list--project-groups intermittently fails the smoke:storybook step. Two independent boundaries in the play function can each fail under the smoke runner's four-page concurrency (scripts/storybook-visual-smoke.mjs calls runJobs(..., 4)):

  1. Focus assertion after closing the rename dialog — the story clicks 关闭 and immediately asserts expect(action).toHaveFocus(). The Storybook toHaveFocus matcher is a one-shot synchronous check (activeElement === element, no polling). The product closes the dialog via closeRename() (packages/ui/src/session-history-list.tsx:408) and restores focus through window.requestAnimationFrame(() => opener.focus()), which can only succeed after the modal unmounts. When the assertion runs before the next frame paints, activeElement is still <body> and the story fails — the product then restores focus correctly one frame later.
  2. Dialog query right after picking 重命名 — getByRole('dialog', { name: '重命名项目' }) runs while the menu-close → dialog-mount handover is still mid-flight (the product comment at session-history-list.tsx:362-369 documents this one-frame race). CI has failed with missing dialog role "重命名项目" as a separate assertion mode.

Observed failure modes on CI (multiple runs, including PRs whose diffs do not touch the sidebar): expected maka-agent 项目操作 button to hold focus, actual body; and missing dialog role "重命名项目".

Evidence

  • Historical failure: run 33992802279 / job 101377914164 — stack points at assets/session-list-panel.stories-BCpYL2Ip.js:1:10335, which maps exactly to the await expect(action).toHaveFocus() line after clicking 关闭 (source: packages/ui/stories/session-list-panel.stories.tsx:677).
  • The story, session-history-list.tsx, and the smoke runner are byte-identical between the failing merge (0467fbc4…) and current main (411512bd9…).
  • Reproduction on macOS arm64 Chromium, real runner, four-page concurrency, no artificial delays: 120 runs of the original assertions → 17 failures at the focus assertion + 17 dialog-not-ready failures; post-failure observation confirmed the product restored focus within ~300 ms every time (53/53), so this is a test-waiting defect, not a product bug. Converting the two assertion points to waitFor/findByRole → 240/240 pass; reverting to the original assertions reproduced the failure again (36 focus failures / 120).
  • The same story has kept failing CI after this report window, including a run where the failure mode was missing dialog role "重命名项目" (see PR fix(desktop): localize expected errors #4457 discussion), and a reviewer reproduced a focus failure on the unmodified base.

Proposed fix (test-only)

In packages/ui/stories/session-list-panel.stories.tsx, ProjectGroups play:

await userEvent.click(page.getByRole('menuitem', { name: '重命名' }));
await expect(await page.findByRole('dialog', { name: '重命名项目' })).toBeVisible();
await userEvent.click(page.getByRole('button', { name: '关闭' }));
await waitFor(() => expect(action).toHaveFocus());

waitFor is already imported by the file. No product change: closing the dialog and refocusing the opener on the next frame is intended behavior, and the post-failure probe showed the restore always completes.

Note: Desktop Nightly does not run smoke:storybook, so there is no green main baseline to compare against; the same flake is currently blocking unrelated PRs (e.g. #4457).

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions