Skip to content

Commit 6b11d13

Browse files
test(console): record the route probes in a layout effect so a drawn page is already recorded (#38000)
<!-- claude-projects-attribution: {"github_login":"os-tesla"} --> _Requested by **Tesla** · [project thread](https://claude.ai/code/project/chan_01AXsQkLFKRdQCqQ3RTr6Jao?thread=cmsg_01AXsQkLFKRdQCqQ3RTr6Jao7TkF6tARYTW3zLaaPzFK5G)_ Fixes #37996 Clause-②: no Metadata-protocol: none ## 维护者速读 - 用户无感:只改两份控制台路由测试,产品代码一行未动。 - 为什么做:这两份测试在慢的 CI 机器上偶发变红,挡住了与它们无关的 PR(如问 AI 的那张)。 - 出错会怎样:只影响测试,可随时撤回;改坏的话这两份测试会稳定变红,不会伤到用户。 - 要你做什么:无。 ## What changes Both files record the router's history through a private `LocationProbe` that pushed in a passive `useEffect`. Each case waits for the routed DOM (`findByTestId`, or the outside-`act` `drawn()` poll) and then reads the history array. React may flush a passive effect after the DOM is visible, so on a slow runner the read came one entry short: CI run 46013899997, `Test UI (2/16)`, `expected [ '/apps/policy' ] to deeply equal [ '/apps/policy', …(1) ]` at the outside-`act` "a member with no AI seat: the home is the next entry, not a hold that never ends" case. Both probes now record in `useLayoutEffect`, which runs in the same commit that writes the DOM. Once a routed page is drawn, its location is already recorded. No assertion, wait or timeout changes; the diff is the effect hook and a comment on each probe. The race was in the probe, not only in the two cases the card names, so the fix covers every case in both files that reads `seen` / `pathnames` (31 of 46). Files: `ui/apps/console/src/components/StudioRoute.metadataArea-28561.test.tsx`, `ui/packages/app-shell/src/console/__tests__/AppContent.aiChatEntryGate-37833.test.tsx`. Test-only: neither file ships in a package's `files[]` (`@object-ui/app-shell` is private and ships `dist`; `@object-ui/console` ships `dist` and its plugin entry), so this PR carries `skip-changeset`. ## Tests <details><summary>Reproduction on origin/main dc45b1e (before the fix)</summary> - Baseline: both files, 46/46 passed. - Each probe's push delayed 30 ms with `setTimeout` (a late passive-effect flush): 31 failed / 15 passed of 46, including both cases the card names, with CI's failure shape (`expected [ '/apps/policy' ] to deeply equal [ '/apps/policy', …(1) ]`, and for the Studio case an array one entry short of `[ '/studio/~all', … ]`). Mutation reverted, `git diff HEAD` empty. </details> <details><summary>After the fix, head 556804f</summary> - `pnpm --dir ui exec vitest run apps/console/src/components/StudioRoute.metadataArea-28561.test.tsx packages/app-shell/src/console/__tests__/AppContent.aiChatEntryGate-37833.test.tsx`: 46/46 passed, eight runs in a row (8/8). </details> <details><summary>Ablations (teeth kept), each through scripts/ablation-replace.mjs, restored to the HEAD blob with git diff HEAD empty</summary> - S1, two hops: `StudioAllLegAddress` first sends any `~all` URL to `/studio/~all/hop`, then to its target. Studio file: 5 failed / 28 passed. The three "lands on Setup's environment directory, in one hop, never on a pillar" rows fail at line 188 (`expect(seen).toEqual([url, '/apps/setup/component/security/access-directory'])`), the two "without Setup's entry capability" rows at line 200. - S2, lands on a pillar: `StudioAllLegAddress` always sends a Setup holder to the area's root. The three Setup-directory rows fail at line 185 (`findByTestId('setup-directory')`; the builder is drawn instead). - G1, changed next entry: `resolveLandingRoute` skips the first entry the member is shown. Gate file: 6 failed / 7 passed. The card's case ("a member with no AI seat: the home is the next entry, not a hold that never ends") fails at line 418 (`expect(pathnames).toEqual(['/apps/policy', '/apps/policy/crm_case'])`); the others at lines 300, 313, 324, 357 and 461. </details> ## Acceptance notes - Same probe shape elsewhere, named per the card and not edited here (each is a private function in its own file, none shares a helper with these two). A census of `useLocation` probes that push in a passive `useEffect` found: `ui/packages/app-shell/src/views/ObjectView.listUrlState-11860.test.tsx`, `ui/packages/app-shell/src/views/ObjectView.ufViewSwitch-11992.test.tsx`, `ui/packages/app-shell/src/views/ObjectView.listPageInUrl-30850.test.tsx`, `ui/packages/app-shell/src/layout/__tests__/usePhoneNavDrawerHistory-29131.test.tsx`, `ui/packages/app-shell/src/console/ai/__tests__/AiChatPage.buildClosedRoutes-36969.test.tsx`, `ui/apps/console/src/pages/system/ApprovalsInboxPage.lateBulkReload-37548.test.tsx`, `ui/apps/console/src/pages/system/ApprovalsInboxPage.lateBulkToast-37328.test.tsx`. The census matched a push within a short `useEffect` body, so a probe spelled across a longer body may be missing from it. Whether each one races depends on whether it reads the array right after a DOM wait; not measured here. carrier: seat repo:objectstack#3. ## Verification Run the two files with the command under Tests; expect 46 passed. To see the race the assertions are exposed to, delay either probe's push with `setTimeout(…, 30)`, which stands in for a late passive-effect flush: the history assertions go red. Co-authored-by: Claude <noreply@anthropic.com>
1 parent 34f7078 commit 6b11d13

2 files changed

Lines changed: 14 additions & 3 deletions

File tree

‎ui/apps/console/src/components/StudioRoute.metadataArea-28561.test.tsx‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,10 +99,16 @@ import { studioRoutes } from './StudioRoute';
9999
/** Every location the router showed, in order: one redirect is two entries. */
100100
let seen: string[] = [];
101101

102+
/**
103+
* Records in a LAYOUT effect, which runs in the same commit that writes the
104+
* DOM: once a routed target is visible, its location is already in `seen`.
105+
* A passive `useEffect` is flushed later, so on a slow runner a case that
106+
* waited for the DOM read `seen` one entry short.
107+
*/
102108
function LocationProbe() {
103109
const { pathname, search } = useLocation();
104110
const here = `${pathname}${search}`;
105-
React.useEffect(() => {
111+
React.useLayoutEffect(() => {
106112
seen.push(here);
107113
}, [here]);
108114
return <div data-testid="pathname">{here}</div>;

‎ui/packages/app-shell/src/console/__tests__/AppContent.aiChatEntryGate-37833.test.tsx‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,11 +141,16 @@ const AGENTS_URL = '/api/v1/ai/agents';
141141
const SERVED = { success: true, data: { agents: [{ name: 'ask', label: 'Ask' }] } };
142142
const NO_SEAT = { success: true, data: { agents: [] } };
143143

144-
/** Every pathname the router held, in order. */
144+
/**
145+
* Every pathname the router held, in order. Recorded in a LAYOUT effect, which
146+
* runs in the same commit that writes the DOM, so a routed page that is drawn
147+
* is already recorded: the cases mounted outside `act` poll the DOM, and a
148+
* passive `useEffect` may not have flushed when they read this array.
149+
*/
145150
let pathnames: string[] = [];
146151
function LocationProbe() {
147152
const location = useLocation();
148-
React.useEffect(() => {
153+
React.useLayoutEffect(() => {
149154
if (pathnames[pathnames.length - 1] !== location.pathname) pathnames.push(location.pathname);
150155
}, [location.pathname]);
151156
return <div data-testid="pathname">{location.pathname}</div>;

0 commit comments

Comments
 (0)