@@ -9,6 +9,7 @@ import type { WorkspaceItem } from "../src/provider-workspace/catalog";
99const globals = [ "document" , "window" , "navigator" , "localStorage" , "IS_REACT_ACT_ENVIRONMENT" ] as const ;
1010let previousGlobals : Record < ( typeof globals ) [ number ] , unknown > ;
1111let testWindow : Window ;
12+ const originalFetch = globalThis . fetch ;
1213
1314beforeEach ( ( ) => {
1415 previousGlobals = Object . fromEntries ( globals . map ( key => [ key , Reflect . get ( globalThis , key ) ] ) ) as typeof previousGlobals ;
@@ -24,6 +25,7 @@ beforeEach(() => {
2425} ) ;
2526
2627afterEach ( ( ) => {
28+ globalThis . fetch = originalFetch ;
2729 testWindow . close ( ) ;
2830 for ( const key of globals ) {
2931 Object . defineProperty ( globalThis , key , { configurable : true , value : previousGlobals [ key ] } ) ;
@@ -46,6 +48,8 @@ async function flush(): Promise<void> {
4648async function mountOverview (
4749 onUpdateProvider : ( name : string , patch : { note ?: string } ) => Promise < { ok : boolean ; error ?: string } > ,
4850 providerItem = item ,
51+ apiBase ?: string ,
52+ connectionIdentity ?: string ,
4953) : Promise < { root : Root ; container : HTMLElement } > {
5054 const container = document . createElement ( "div" ) ;
5155 document . body . append ( container ) ;
@@ -55,7 +59,12 @@ async function mountOverview(
5559 root = createRoot ( container ) ;
5660 root . render (
5761 < LanguageProvider >
58- < ProviderOverview item = { providerItem } onUpdateProvider = { onUpdateProvider } />
62+ < ProviderOverview
63+ item = { providerItem }
64+ apiBase = { apiBase }
65+ connectionIdentity = { connectionIdentity }
66+ onUpdateProvider = { onUpdateProvider }
67+ />
5968 </ LanguageProvider > ,
6069 ) ;
6170 } ) ;
@@ -141,3 +150,123 @@ test("notes save closes editor only after an acknowledged success", async () =>
141150
142151 await act ( async ( ) => { root . unmount ( ) ; } ) ;
143152} ) ;
153+
154+ test ( "static catalog connection test renders as not applicable instead of failed" , async ( ) => {
155+ let requests = 0 ;
156+ globalThis . fetch = ( async ( input : RequestInfo | URL , init ?: RequestInit ) => {
157+ requests += 1 ;
158+ expect ( String ( input ) ) . toBe ( "http://localhost/api/providers/test?name=google-antigravity" ) ;
159+ expect ( init ?. method ) . toBe ( "POST" ) ;
160+ return Response . json ( { applicable : false , reason : "static_catalog" , latencyMs : 0 } ) ;
161+ } ) as typeof fetch ;
162+ const antigravity = {
163+ name : "google-antigravity" ,
164+ adapter : "google" ,
165+ baseUrl : "https://daily-cloudcode-pa.googleapis.com" ,
166+ authMode : "oauth" ,
167+ liveModels : false ,
168+ } as WorkspaceItem ;
169+
170+ const { root, container } = await mountOverview ( async ( ) => ( { ok : true } ) , antigravity , "http://localhost" ) ;
171+ const button = [ ...container . querySelectorAll < HTMLButtonElement > ( "button" ) ]
172+ . find ( candidate => candidate . textContent ?. includes ( "Test connection" ) ) ;
173+ expect ( button ) . toBeTruthy ( ) ;
174+
175+ await act ( async ( ) => {
176+ button ! . click ( ) ;
177+ await flush ( ) ;
178+ } ) ;
179+
180+ const result = container . querySelector < HTMLElement > ( '[data-connection-test-state="not-applicable"]' ) ;
181+ expect ( requests ) . toBe ( 1 ) ;
182+ expect ( result ?. textContent ) . toContain ( "Not applicable" ) ;
183+ expect ( result ?. classList . contains ( "pws-status-warn" ) ) . toBe ( false ) ;
184+ expect ( container . textContent ) . not . toContain ( "Connection failed" ) ;
185+
186+ await act ( async ( ) => { root . unmount ( ) ; } ) ;
187+ } ) ;
188+
189+ test ( "same-provider config changes clear a rendered connection result" , async ( ) => {
190+ globalThis . fetch = ( async ( ) => Response . json ( {
191+ ok : true ,
192+ latencyMs : 4 ,
193+ message : "Connected" ,
194+ } ) ) as typeof fetch ;
195+ const update = async ( ) => ( { ok : true as const } ) ;
196+ const { root, container } = await mountOverview ( update , item , "http://localhost" , "key:key-a" ) ;
197+ const button = [ ...container . querySelectorAll < HTMLButtonElement > ( "button" ) ]
198+ . find ( candidate => candidate . textContent ?. includes ( "Test connection" ) ) ! ;
199+
200+ await act ( async ( ) => {
201+ button . click ( ) ;
202+ await flush ( ) ;
203+ } ) ;
204+ expect ( container . querySelector ( '[data-connection-test-state="ok"]' ) ) . toBeTruthy ( ) ;
205+
206+ await act ( async ( ) => {
207+ root . render (
208+ < LanguageProvider >
209+ < ProviderOverview
210+ item = { { ...item , disabled : true } }
211+ apiBase = "http://localhost"
212+ connectionIdentity = "key:key-a"
213+ onUpdateProvider = { update }
214+ />
215+ </ LanguageProvider > ,
216+ ) ;
217+ await flush ( ) ;
218+ } ) ;
219+
220+ expect ( container . querySelector ( "[data-connection-test-state]" ) ) . toBeNull ( ) ;
221+ expect ( container . textContent ) . not . toContain ( "Connected" ) ;
222+
223+ await act ( async ( ) => { root . unmount ( ) ; } ) ;
224+ } ) ;
225+
226+ test ( "same-provider auth identity changes abort and ignore an in-flight result" , async ( ) => {
227+ let resolveFetch ! : ( response : Response ) => void ;
228+ let signal : AbortSignal | null = null ;
229+ globalThis . fetch = ( async ( _input : RequestInfo | URL , init ?: RequestInit ) => {
230+ signal = init ?. signal ?? null ;
231+ return await new Promise < Response > ( resolve => {
232+ resolveFetch = resolve ;
233+ } ) ;
234+ } ) as typeof fetch ;
235+ const update = async ( ) => ( { ok : true as const } ) ;
236+ const { root, container } = await mountOverview ( update , item , "http://localhost" , "key:key-a" ) ;
237+ const button = [ ...container . querySelectorAll < HTMLButtonElement > ( "button" ) ]
238+ . find ( candidate => candidate . textContent ?. includes ( "Test connection" ) ) ! ;
239+
240+ await act ( async ( ) => {
241+ button . click ( ) ;
242+ await Promise . resolve ( ) ;
243+ } ) ;
244+ expect ( button . disabled ) . toBe ( true ) ;
245+
246+ await act ( async ( ) => {
247+ root . render (
248+ < LanguageProvider >
249+ < ProviderOverview
250+ item = { item }
251+ apiBase = "http://localhost"
252+ connectionIdentity = "key:key-b"
253+ onUpdateProvider = { update }
254+ />
255+ </ LanguageProvider > ,
256+ ) ;
257+ await flush ( ) ;
258+ } ) ;
259+
260+ expect ( signal ?. aborted ) . toBe ( true ) ;
261+ expect ( container . querySelector ( "[data-connection-test-state]" ) ) . toBeNull ( ) ;
262+ expect ( container . querySelector < HTMLButtonElement > ( "button" ) ?. disabled ) . toBe ( false ) ;
263+
264+ await act ( async ( ) => {
265+ resolveFetch ( Response . json ( { ok : true , latencyMs : 4 , message : "Old result" } ) ) ;
266+ await flush ( ) ;
267+ } ) ;
268+ expect ( container . querySelector ( "[data-connection-test-state]" ) ) . toBeNull ( ) ;
269+ expect ( container . textContent ) . not . toContain ( "Old result" ) ;
270+
271+ await act ( async ( ) => { root . unmount ( ) ; } ) ;
272+ } ) ;
0 commit comments