Skip to content

Commit ce38534

Browse files
committed
test(app-shell): pin the warm-catalog loading state outside act, on the hook and on the ai:chat page
Claude-Session: https://claude.ai/code/session_016WV4cwGVgec21LwQTtRY85 Co-authored-by: Claude <noreply@anthropic.com>
1 parent 59f685f commit ce38534

2 files changed

Lines changed: 112 additions & 2 deletions

File tree

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

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -419,12 +419,31 @@ describe('objectstack#37833 — the ai:chat entry is hidden from a member who ca
419419
it('a direct link for a member with no AI seat: the page draws AiUnavailable, not its loading state', async () => {
420420
metadataApps = [policyApp()];
421421
const apiFetch = await warmTheCatalog(NO_SEAT);
422-
const unmount = mountOutsideAct('/apps/policy/component/ai/chat', apiFetch);
422+
// The page alone, as the component route mounts it: the console's other
423+
// renders in the same window would flush the loading render it must
424+
// never depend on.
425+
const host = document.createElement('div');
426+
document.body.appendChild(host);
427+
const root = createRoot(host);
428+
const actEnv = globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean };
429+
actEnv.IS_REACT_ACT_ENVIRONMENT = false;
430+
root.render(
431+
withProviders(
432+
apiFetch,
433+
<MemoryRouter initialEntries={['/apps/policy/component/ai/chat']}>
434+
<Routes>
435+
<Route path="/apps/:appName/component/ai/chat" element={<AppAskPage />} />
436+
</Routes>
437+
</MemoryRouter>,
438+
),
439+
);
423440
try {
424441
expect(await drawn('ai-unavailable')).toBe(true);
425442
expect(document.querySelector('[data-testid="app-ask-page-loading"]')).toBeNull();
426443
} finally {
427-
unmount();
444+
root.unmount();
445+
host.remove();
446+
actEnv.IS_REACT_ACT_ENVIRONMENT = true;
428447
}
429448
});
430449

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license.
2+
3+
/**
4+
* objectstack#37833 — `useAiSurfaceEnabled` leaves its loading state when the
5+
* agent catalog is empty and the session cache answers it.
6+
*
7+
* The console reads the catalog on every open, so a surface that mounts later
8+
* (the app root's landing, the `ai:chat` page on a direct link) is answered by
9+
* `useAgents`' module cache: `isLoading` goes true and false before React
10+
* renders once. Read through a latch that only a render seeing `isLoading`
11+
* could set, a member with no AI seat stayed "loading" for ever; it reads
12+
* `useAgents`' `settled` now.
13+
*
14+
* Real `useAgents` (this is why the file is not a `describe` in
15+
* `useAiSurface.test.ts`, which mocks it) over a stubbed `apiFetch`, mounted
16+
* with `createRoot` and React's act environment OFF, as a browser schedules:
17+
* under `act` each queued update is flushed in turn and the loading render is
18+
* seen, which hides the defect.
19+
*/
20+
import { describe, it, expect, beforeEach, afterEach } from 'vitest';
21+
import React from 'react';
22+
import { createRoot } from 'react-dom/client';
23+
import { SchemaRendererContext, type ApiFetch } from '@object-ui/react';
24+
import { __resetAgentsCatalog } from '../../../../plugin-chatbot/src/useAgents';
25+
import { useAiSurfaceEnabled } from '../useAiSurface';
26+
27+
const sleep = (ms: number) => new Promise<void>((resolve) => setTimeout(resolve, ms));
28+
const actEnv = globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean };
29+
let savedActEnv: boolean | undefined;
30+
31+
function catalogFetch(agents: unknown[]): ApiFetch {
32+
return (async () => {
33+
await sleep(20);
34+
return { ok: true, status: 200, json: async () => ({ success: true, data: { agents } }) } as unknown as Response;
35+
}) as ApiFetch;
36+
}
37+
38+
/** Mount `useAiSurfaceEnabled` outside `act`; every render's `enabled/isLoading` is recorded. */
39+
function mount(apiFetch: ApiFetch) {
40+
const renders: string[] = [];
41+
function Probe() {
42+
const surface = useAiSurfaceEnabled();
43+
renders.push(`${surface.enabled}/${surface.isLoading}`);
44+
return null;
45+
}
46+
const root = createRoot(document.createElement('div'));
47+
root.render(
48+
<SchemaRendererContext.Provider value={{ dataSource: null, apiFetch } as never}>
49+
<Probe />
50+
</SchemaRendererContext.Provider>,
51+
);
52+
return { renders, unmount: () => root.unmount() };
53+
}
54+
55+
beforeEach(() => {
56+
__resetAgentsCatalog();
57+
savedActEnv = actEnv.IS_REACT_ACT_ENVIRONMENT;
58+
actEnv.IS_REACT_ACT_ENVIRONMENT = false;
59+
});
60+
61+
afterEach(() => {
62+
actEnv.IS_REACT_ACT_ENVIRONMENT = savedActEnv;
63+
});
64+
65+
describe('useAiSurfaceEnabled on a catalog the session cache answers (objectstack#37833)', () => {
66+
it('an empty catalog: loading on the cold read, then disabled and not loading on the warm one', async () => {
67+
const apiFetch = catalogFetch([]);
68+
const cold = mount(apiFetch);
69+
await sleep(200);
70+
expect(cold.renders[0]).toBe('false/true');
71+
expect(cold.renders[cold.renders.length - 1]).toBe('false/false');
72+
cold.unmount();
73+
74+
const warm = mount(apiFetch);
75+
await sleep(200);
76+
expect(warm.renders[0]).toBe('false/true');
77+
expect(warm.renders[warm.renders.length - 1]).toBe('false/false');
78+
warm.unmount();
79+
});
80+
81+
it('CONTROL: a served catalog is enabled on the warm read', async () => {
82+
const apiFetch = catalogFetch([{ name: 'ask', label: 'Ask' }]);
83+
const cold = mount(apiFetch);
84+
await sleep(200);
85+
cold.unmount();
86+
const warm = mount(apiFetch);
87+
await sleep(200);
88+
expect(warm.renders[warm.renders.length - 1]).toBe('true/false');
89+
warm.unmount();
90+
});
91+
});

0 commit comments

Comments
 (0)