|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * objectstack#36969 — the full-page chat's addresses where the agent catalog |
| 5 | + * does not serve the build agent (in-app building closed: `build` persisted |
| 6 | + * inactive, `ask` still served), and where it does. |
| 7 | + * |
| 8 | + * - `/ai/build/ID` lands on the default surface WITH the conversation: |
| 9 | + * `/ai/ask/ID`, and that conversation is the one read, so an old build |
| 10 | + * thread still opens. It used to land on `/ai/ask` and drop the id. |
| 11 | + * - the cloud#1674 maker convergence (a maker's bare `/ai/ask` → `/ai/build`) |
| 12 | + * does not fire: there is no build to converge on. |
| 13 | + * - regression: where `build` is served, `/ai/build/ID` stays where it is and |
| 14 | + * a maker's bare `/ai/ask` still converges on `/ai/build`. |
| 15 | + * |
| 16 | + * Real subjects: `AiChatPage` under a real router, its route canonicalization |
| 17 | + * and `useChatConversation`. Stand-ins: the agent catalog, the authoring |
| 18 | + * capability, the signed-in user, the apps list, the chat transport (idle and |
| 19 | + * empty) and the side panels; `fetch` answers the conversation API from memory. |
| 20 | + */ |
| 21 | + |
| 22 | +import '@testing-library/jest-dom/vitest'; |
| 23 | +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; |
| 24 | +import { act, cleanup, render, screen, waitFor } from '@testing-library/react'; |
| 25 | +import { MemoryRouter, Route, Routes, useLocation } from 'react-router-dom'; |
| 26 | +import React from 'react'; |
| 27 | + |
| 28 | +const state = vi.hoisted(() => ({ |
| 29 | + agents: [] as Array<{ name: string; label?: string }>, |
| 30 | + canAuthor: false, |
| 31 | +})); |
| 32 | + |
| 33 | +vi.mock('@object-ui/plugin-chatbot', async (importOriginal) => { |
| 34 | + const actual = await importOriginal<Record<string, unknown>>(); |
| 35 | + return { |
| 36 | + ...actual, |
| 37 | + useAiModels: () => ({ models: [], defaultModelId: undefined }), |
| 38 | + useHitlInChat: () => ({ decide: vi.fn(), decisions: {} }), |
| 39 | + useObjectChat: () => ({ |
| 40 | + messages: [], |
| 41 | + isLoading: false, |
| 42 | + error: undefined, |
| 43 | + sendMessage: vi.fn(), |
| 44 | + stop: vi.fn(), |
| 45 | + reload: vi.fn(), |
| 46 | + clear: vi.fn(), |
| 47 | + setMessages: vi.fn(), |
| 48 | + }), |
| 49 | + }; |
| 50 | +}); |
| 51 | +vi.mock('@object-ui/plugin-chatbot/core', async (importOriginal) => ({ |
| 52 | + ...(await importOriginal<typeof import('@object-ui/plugin-chatbot/core')>()), |
| 53 | + useAgents: () => ({ agents: state.agents, isLoading: false, error: undefined, refetch: vi.fn() }), |
| 54 | +})); |
| 55 | +vi.mock('../../../hooks/useCanAuthorMetadata', () => ({ |
| 56 | + useCanAuthorMetadata: () => state.canAuthor, |
| 57 | +})); |
| 58 | +vi.mock('@object-ui/auth', async (importOriginal) => ({ |
| 59 | + ...(await importOriginal<Record<string, unknown>>()), |
| 60 | + useAuth: () => ({ user: { id: 'u1' } }), |
| 61 | +})); |
| 62 | +vi.mock('../../../providers/MetadataProvider', async (importOriginal) => ({ |
| 63 | + ...(await importOriginal<Record<string, unknown>>()), |
| 64 | + useMetadata: () => ({ apps: [] }), |
| 65 | +})); |
| 66 | +vi.mock('../../../providers/AdapterProvider', async (importOriginal) => ({ |
| 67 | + ...(await importOriginal<Record<string, unknown>>()), |
| 68 | + useAdapter: () => null, |
| 69 | +})); |
| 70 | +vi.mock('../../../layout/AiUsageIndicator', () => ({ AiUsageIndicator: () => null })); |
| 71 | +vi.mock('../ConversationsSidebar', () => ({ |
| 72 | + ConversationsSidebar: () => <div data-testid="sidebar" />, |
| 73 | +})); |
| 74 | + |
| 75 | +import { I18nProvider } from '@object-ui/i18n'; |
| 76 | +import { AiChatPage } from '../AiChatPage'; |
| 77 | + |
| 78 | +window.matchMedia = ((query: string) => ({ |
| 79 | + matches: false, |
| 80 | + media: query, |
| 81 | + onchange: null, |
| 82 | + addEventListener: () => {}, |
| 83 | + removeEventListener: () => {}, |
| 84 | + addListener: () => {}, |
| 85 | + removeListener: () => {}, |
| 86 | + dispatchEvent: () => false, |
| 87 | +})) as unknown as typeof window.matchMedia; |
| 88 | + |
| 89 | +const API = '/api/v1/ai'; |
| 90 | +/** Every address the router showed, in order. */ |
| 91 | +const visited: string[] = []; |
| 92 | +/** Every conversation id read by id. */ |
| 93 | +const readIds: string[] = []; |
| 94 | + |
| 95 | +function LocationProbe() { |
| 96 | + const location = useLocation(); |
| 97 | + React.useEffect(() => { |
| 98 | + visited.push(location.pathname); |
| 99 | + }, [location.pathname]); |
| 100 | + return <output data-testid="location">{location.pathname}</output>; |
| 101 | +} |
| 102 | + |
| 103 | +function visit(entry: string) { |
| 104 | + render( |
| 105 | + <I18nProvider config={{ defaultLanguage: 'en', detectBrowserLanguage: false }}> |
| 106 | + <MemoryRouter initialEntries={[entry]}> |
| 107 | + <Routes> |
| 108 | + <Route path="/ai/:agent/:conversationId" element={<AiChatPage apiBase={API} />} /> |
| 109 | + <Route path="/ai/:agent" element={<AiChatPage apiBase={API} />} /> |
| 110 | + </Routes> |
| 111 | + <LocationProbe /> |
| 112 | + </MemoryRouter> |
| 113 | + </I18nProvider>, |
| 114 | + ); |
| 115 | +} |
| 116 | + |
| 117 | +const ASK_ONLY = [{ name: 'ask', label: 'Ask' }]; |
| 118 | +const BOTH = [ |
| 119 | + { name: 'build', label: 'Build' }, |
| 120 | + { name: 'ask', label: 'Ask' }, |
| 121 | +]; |
| 122 | + |
| 123 | +beforeEach(() => { |
| 124 | + visited.length = 0; |
| 125 | + readIds.length = 0; |
| 126 | + state.agents = ASK_ONLY; |
| 127 | + state.canAuthor = false; |
| 128 | + window.localStorage.clear(); |
| 129 | + window.sessionStorage.clear(); |
| 130 | + let minted = 0; |
| 131 | + vi.stubGlobal( |
| 132 | + 'fetch', |
| 133 | + vi.fn(async (input: RequestInfo | URL, init?: RequestInit) => { |
| 134 | + const url = String(input); |
| 135 | + const method = init?.method ?? 'GET'; |
| 136 | + const json = (body: unknown) => |
| 137 | + new Response(JSON.stringify(body), { status: 200, headers: { 'Content-Type': 'application/json' } }); |
| 138 | + if (url === `${API}/conversations` && method === 'POST') { |
| 139 | + minted += 1; |
| 140 | + return json({ id: `conv-${minted}`, messages: [] }); |
| 141 | + } |
| 142 | + const byId = /\/conversations\/([^/?]+)$/.exec(url); |
| 143 | + if (byId && method === 'GET') { |
| 144 | + readIds.push(decodeURIComponent(byId[1])); |
| 145 | + return json({ id: byId[1], agentId: 'build', messages: [] }); |
| 146 | + } |
| 147 | + return json({ success: true, data: [] }); |
| 148 | + }), |
| 149 | + ); |
| 150 | +}); |
| 151 | + |
| 152 | +afterEach(() => { |
| 153 | + cleanup(); |
| 154 | + vi.unstubAllGlobals(); |
| 155 | +}); |
| 156 | + |
| 157 | +describe('in-app building closed: the catalog serves `ask`, not `build` (objectstack#36969)', () => { |
| 158 | + it('/ai/build/ID opens that conversation on the default surface: /ai/ask/ID', async () => { |
| 159 | + visit('/ai/build/c-42'); |
| 160 | + await waitFor(() => expect(screen.getByTestId('location')).toHaveTextContent(/^\/ai\/ask\/c-42$/)); |
| 161 | + await waitFor(() => expect(readIds).toContain('c-42')); |
| 162 | + expect(visited).not.toContain('/ai/ask'); |
| 163 | + }); |
| 164 | + |
| 165 | + it('a maker gets it too', async () => { |
| 166 | + state.canAuthor = true; |
| 167 | + visit('/ai/build/c-42'); |
| 168 | + await waitFor(() => expect(screen.getByTestId('location')).toHaveTextContent(/^\/ai\/ask\/c-42$/)); |
| 169 | + expect(visited.some((p) => p === '/ai/build' || p === '/ai/ask')).toBe(false); |
| 170 | + }); |
| 171 | + |
| 172 | + it('the maker convergence (cloud#1674) never sends a maker\'s bare /ai/ask to /ai/build', async () => { |
| 173 | + state.canAuthor = true; |
| 174 | + visit('/ai/ask'); |
| 175 | + // The same window in which the served-build leg below is redirected. |
| 176 | + await act(async () => { |
| 177 | + await new Promise((resolve) => setTimeout(resolve, 500)); |
| 178 | + }); |
| 179 | + expect(screen.getByTestId('location')).toHaveTextContent(/^\/ai\/ask/); |
| 180 | + expect(visited.some((p) => p.startsWith('/ai/build'))).toBe(false); |
| 181 | + }); |
| 182 | +}); |
| 183 | + |
| 184 | +describe('regression: where the catalog serves `build`, the addresses are unchanged (objectstack#36969)', () => { |
| 185 | + beforeEach(() => { |
| 186 | + state.agents = BOTH; |
| 187 | + }); |
| 188 | + |
| 189 | + it('/ai/build/ID stays on the build conversation', async () => { |
| 190 | + visit('/ai/build/c-42'); |
| 191 | + await waitFor(() => expect(readIds).toContain('c-42')); |
| 192 | + expect(screen.getByTestId('location')).toHaveTextContent(/^\/ai\/build\/c-42$/); |
| 193 | + expect(visited.every((p) => p === '/ai/build/c-42')).toBe(true); |
| 194 | + }); |
| 195 | + |
| 196 | + it('a maker\'s bare /ai/ask still converges on /ai/build (cloud#1674)', async () => { |
| 197 | + state.canAuthor = true; |
| 198 | + visit('/ai/ask'); |
| 199 | + await waitFor(() => expect(visited.some((p) => p.startsWith('/ai/build'))).toBe(true)); |
| 200 | + }); |
| 201 | +}); |
0 commit comments