|
| 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