1- import axios , { AxiosError } from "axios"
1+ import axios , { AxiosError , type GenericAbortSignal } from "axios"
22
33import { lMStudioDefaultModelInfo , providerIdentifiers } from "@roo-code/types"
44
5- import { forceFullModelDetailsLoad , getLMStudioModels , getLMStudioModelAlias , hasLoadedFullDetails } from "../lmstudio"
5+ import {
6+ forceFullModelDetailsLoad ,
7+ getLMStudioModels ,
8+ getLMStudioModelAlias ,
9+ hasLoadedFullDetails ,
10+ LM_STUDIO_MODEL_LOAD_TIMEOUT_MS ,
11+ } from "../lmstudio"
612import type { ApiHandler } from "../../../index"
713
814import nativeFixture from "./fixtures/lmstudio-model-details.json"
915
1016const mockFlushModels = vi . hoisted ( ( ) => vi . fn ( ) )
11- vi . mock ( "../modelCache" , ( ) => ( { flushModels : mockFlushModels } ) )
17+ vi . mock ( "../modelCache" , ( ) => ( { flushModels : mockFlushModels , MODEL_CATALOG_FETCH_TIMEOUT_MS : 15_000 } ) )
1218
1319const get = vi . spyOn ( axios , "get" )
1420const post = vi . spyOn ( axios , "post" )
@@ -49,10 +55,56 @@ beforeEach(async () => {
4955} )
5056
5157describe ( "LM Studio HTTP discovery" , ( ) => {
58+ it . each ( [ "Unexpected endpoint or method" , "Endpoint not found" , "Unrecognized request" , "" ] ) (
59+ "falls back for an explicit unsupported-route body: %s" ,
60+ async ( error ) => {
61+ get . mockResolvedValueOnce ( { data : { error } } )
62+ . mockResolvedValueOnce ( { data : { error } } )
63+ . mockResolvedValueOnce ( { data : { data : [ { id : "legacy" } ] } } )
64+ expect ( ( await getLMStudioModels ( baseUrl ) ) . legacy . supportsImages ) . toBe ( false )
65+ expect ( get ) . toHaveBeenCalledTimes ( 3 )
66+ } ,
67+ )
68+
69+ it . each ( [ undefined , 42 , { message : "unsupported" } ] ) (
70+ "does not fall back for a malformed non-string error: %s" ,
71+ async ( error ) => {
72+ get . mockResolvedValueOnce ( { data : { error } } )
73+ expect ( await getLMStudioModels ( baseUrl ) ) . toEqual ( { } )
74+ expect ( get ) . toHaveBeenCalledOnce ( )
75+ } ,
76+ )
77+
78+ it ( "prefers a valid native catalog over an accompanying error string" , async ( ) => {
79+ get . mockResolvedValueOnce ( { data : { ...nativeResponse ( ) . data , error : "ignored" } } )
80+ expect ( Object . keys ( await getLMStudioModels ( baseUrl ) ) ) . toEqual ( [ modelKey ] )
81+ expect ( get ) . toHaveBeenCalledOnce ( )
82+ } )
83+
84+ it ( "expires loaded details and aliases after a failed refresh" , async ( ) => {
85+ get . mockResolvedValueOnce ( nativeResponse ( ) ) . mockRejectedValueOnce ( new Error ( "server offline" ) )
86+ await getLMStudioModels ( baseUrl )
87+ expect ( hasLoadedFullDetails ( modelKey , baseUrl ) ) . toBe ( true )
88+ expect ( getLMStudioModelAlias ( "smollm2-135m-instruct" , baseUrl ) ) . toBeDefined ( )
89+ await getLMStudioModels ( baseUrl )
90+ expect ( hasLoadedFullDetails ( modelKey , baseUrl ) ) . toBe ( false )
91+ expect ( getLMStudioModelAlias ( "smollm2-135m-instruct" , baseUrl ) ) . toBeUndefined ( )
92+ } )
93+
94+ it . each ( [ "__proto__" , "constructor" ] ) ( "preserves catalog keys and aliases named %s" , async ( id ) => {
95+ get . mockResolvedValueOnce ( nativeResponse ( [ { id, config : { context_length : 2048 } } ] ) )
96+ await getLMStudioModels ( baseUrl )
97+ expect ( getLMStudioModelAlias ( id , baseUrl ) ?. contextWindow ) . toBe ( 2048 )
98+ get . mockResolvedValueOnce ( { data : { models : [ { ...nativeModel ( [ ] ) , key : id } ] } } )
99+ const models = await getLMStudioModels ( baseUrl )
100+ expect ( Object . keys ( models ) ) . toEqual ( [ id ] )
101+ expect ( models [ id ] . supportsImages ) . toBe ( false )
102+ } )
103+
52104 it ( "returns only catalog keys while retaining saved instance aliases separately" , async ( ) => {
53105 get . mockResolvedValueOnce ( { data : nativeFixture } )
54106 const models = await getLMStudioModels ( baseUrl )
55- expect ( get ) . toHaveBeenCalledExactlyOnceWith ( `${ baseUrl } /api/v1/models` , { signal : undefined } )
107+ expect ( get ) . toHaveBeenCalledExactlyOnceWith ( `${ baseUrl } /api/v1/models` , { signal : expect . any ( AbortSignal ) } )
56108 expect ( models [ modelKey ] ) . toMatchObject ( { contextWindow : 1024 , maxTokens : 1024 , supportsImages : false } )
57109 expect ( Object . keys ( models ) ) . toEqual ( [ modelKey ] )
58110 expect ( getLMStudioModelAlias ( "smollm2-135m-instruct" , baseUrl ) ) . toEqual ( models [ modelKey ] )
@@ -121,7 +173,7 @@ describe("LM Studio HTTP discovery", () => {
121173 . mockRejectedValueOnce ( httpError ( 405 ) )
122174 . mockResolvedValueOnce ( { data : { data : [ { id : "legacy" } ] } } )
123175 expect ( await getLMStudioModels ( baseUrl ) ) . toEqual ( {
124- legacy : { ...lMStudioDefaultModelInfo , description : "legacy" } ,
176+ legacy : { ...lMStudioDefaultModelInfo , description : "legacy" , supportsImages : false } ,
125177 } )
126178 expect ( get . mock . calls . map ( ( [ url ] ) => url ) ) . toEqual ( [
127179 `${ baseUrl } /api/v1/models` ,
@@ -181,7 +233,7 @@ describe("LM Studio HTTP discovery", () => {
181233 it . each ( [ "" , `${ baseUrl } /` ] ) ( "normalizes the base URL %s" , async ( url ) => {
182234 get . mockResolvedValueOnce ( nativeResponse ( ) )
183235 await getLMStudioModels ( url )
184- expect ( get ) . toHaveBeenCalledWith ( `${ baseUrl } /api/v1/models` , { signal : undefined } )
236+ expect ( get ) . toHaveBeenCalledWith ( `${ baseUrl } /api/v1/models` , { signal : expect . any ( AbortSignal ) } )
185237 } )
186238
187239 it ( "does not request an invalid URL" , async ( ) => {
@@ -190,12 +242,17 @@ describe("LM Studio HTTP discovery", () => {
190242 } )
191243
192244 it ( "passes the signal through every fallback request" , async ( ) => {
193- const { signal } = new AbortController ( )
245+ const controller = new AbortController ( )
246+ const { signal } = controller
194247 get . mockRejectedValueOnce ( httpError ( 404 ) )
195248 . mockRejectedValueOnce ( httpError ( 405 ) )
196249 . mockResolvedValueOnce ( { data : { data : [ ] } } )
197250 await getLMStudioModels ( baseUrl , { signal } )
198- for ( const [ , config ] of get . mock . calls ) expect ( config ?. signal ) . toBe ( signal )
251+ const mergedSignal = get . mock . calls [ 0 ] [ 1 ] ?. signal
252+ expect ( mergedSignal ) . toBeInstanceOf ( AbortSignal )
253+ for ( const [ , config ] of get . mock . calls ) expect ( config ?. signal ) . toBe ( mergedSignal )
254+ controller . abort ( )
255+ expect ( mergedSignal ?. aborted ) . toBe ( true )
199256 } )
200257
201258 it ( "rejects on abort without falling back" , async ( ) => {
@@ -218,6 +275,78 @@ describe("LM Studio HTTP discovery", () => {
218275} )
219276
220277describe ( "LM Studio explicit loading" , ( ) => {
278+ it ( "allows a successful model load to run beyond the catalog deadline" , async ( ) => {
279+ vi . useFakeTimers ( )
280+ const timeout = vi . spyOn ( AbortSignal , "timeout" ) . mockImplementation ( ( ms ) => {
281+ const controller = new AbortController ( )
282+ setTimeout ( ( ) => controller . abort ( ) , ms )
283+ return controller . signal
284+ } )
285+ try {
286+ get . mockResolvedValueOnce ( nativeResponse ( [ ] ) )
287+ let finishLoad : ( ) => void = ( ) => {
288+ throw new Error ( "load not started" )
289+ }
290+ let loadSignal : GenericAbortSignal | undefined
291+ post . mockImplementationOnce (
292+ ( _url , _body , config ) =>
293+ new Promise ( ( resolve , reject ) => {
294+ loadSignal = config ?. signal
295+ loadSignal ?. addEventListener ?.( "abort" , ( ) => reject ( new Error ( "load timed out" ) ) )
296+ finishLoad = ( ) => resolve ( { data : { } } )
297+ } ) ,
298+ )
299+ const pending = forceFullModelDetailsLoad ( baseUrl , modelKey , handler )
300+ await vi . advanceTimersByTimeAsync ( 30_000 )
301+ expect ( post ) . toHaveBeenCalledOnce ( )
302+ expect ( loadSignal ?. aborted ) . toBe ( false )
303+ expect ( mockFlushModels ) . not . toHaveBeenCalled ( )
304+ finishLoad ( )
305+ await pending
306+ expect ( mockFlushModels ) . toHaveBeenCalledOnce ( )
307+ } finally {
308+ timeout . mockRestore ( )
309+ vi . useRealTimers ( )
310+ }
311+ } )
312+
313+ it . each ( [ "discovery" , "load" ] ) (
314+ "bounds a hung %s request and releases the pending slot for retry" ,
315+ async ( phase ) => {
316+ const deadline = new AbortController ( )
317+ const timeout = vi . spyOn ( AbortSignal , "timeout" ) . mockReturnValue ( deadline . signal )
318+ try {
319+ get . mockResolvedValue ( nativeResponse ( [ ] ) )
320+ post . mockResolvedValue ( { data : { } } )
321+ let requestSignal : AbortSignal | undefined
322+ const hang = ( _url : string , config ?: { signal ?: GenericAbortSignal } ) =>
323+ new Promise < never > ( ( _resolve , reject ) => {
324+ requestSignal = config ?. signal instanceof AbortSignal ? config . signal : undefined
325+ requestSignal ?. addEventListener (
326+ "abort" ,
327+ ( ) => reject ( new AxiosError ( "timeout" , "ERR_CANCELED" ) ) ,
328+ { once : true } ,
329+ )
330+ } )
331+ if ( phase === "discovery" ) get . mockImplementationOnce ( hang )
332+ else post . mockImplementationOnce ( ( url , _body , config ) => hang ( url , config ) )
333+ const pending = forceFullModelDetailsLoad ( baseUrl , modelKey , handler )
334+ await vi . waitFor ( ( ) => expect ( requestSignal ) . toBe ( deadline . signal ) )
335+ expect ( timeout ) . toHaveBeenLastCalledWith (
336+ phase === "discovery" ? 15_000 : LM_STUDIO_MODEL_LOAD_TIMEOUT_MS ,
337+ )
338+ deadline . abort ( )
339+ await pending
340+ expect ( mockFlushModels ) . not . toHaveBeenCalled ( )
341+ timeout . mockRestore ( )
342+ await forceFullModelDetailsLoad ( baseUrl , modelKey , handler )
343+ expect ( mockFlushModels ) . toHaveBeenCalledOnce ( )
344+ } finally {
345+ timeout . mockRestore ( )
346+ }
347+ } ,
348+ )
349+
221350 it . each ( [ modelKey , "custom" ] ) ( "reuses an existing instance for %s and refreshes metadata" , async ( savedId ) => {
222351 get . mockResolvedValueOnce ( nativeResponse ( [ { id : "custom" , config : { context_length : 2048 } } ] ) )
223352 await forceFullModelDetailsLoad ( baseUrl , savedId , handler )
@@ -231,12 +360,27 @@ describe("LM Studio explicit loading", () => {
231360
232361 it ( "loads by exact catalog key, then refreshes" , async ( ) => {
233362 get . mockResolvedValueOnce ( nativeResponse ( [ ] ) )
234- post . mockResolvedValueOnce ( { data : { instance_id : modelKey } } )
235- mockFlushModels . mockImplementationOnce ( ( ) => {
236- expect ( post ) . toHaveBeenCalledExactlyOnceWith ( `${ baseUrl } /api/v1/models/load` , { model : modelKey } )
237- } )
238- await forceFullModelDetailsLoad ( baseUrl , modelKey , handler )
363+ let finishLoad : ( ) => void = ( ) => {
364+ throw new Error ( "load not started" )
365+ }
366+ post . mockImplementationOnce (
367+ ( ) =>
368+ new Promise ( ( resolve ) => {
369+ finishLoad = ( ) => resolve ( { data : { instance_id : modelKey } } )
370+ } ) ,
371+ )
372+ const pending = forceFullModelDetailsLoad ( baseUrl , modelKey , handler )
373+ await vi . waitFor ( ( ) => expect ( post ) . toHaveBeenCalledOnce ( ) )
374+ expect ( mockFlushModels ) . not . toHaveBeenCalled ( )
375+ finishLoad ( )
376+ await pending
239377 expect ( mockFlushModels ) . toHaveBeenCalledTimes ( 1 )
378+ expect ( post ) . toHaveBeenCalledExactlyOnceWith (
379+ `${ baseUrl } /api/v1/models/load` ,
380+ { model : modelKey } ,
381+ { signal : expect . any ( AbortSignal ) } ,
382+ )
383+ expect ( post . mock . invocationCallOrder [ 0 ] ) . toBeLessThan ( mockFlushModels . mock . invocationCallOrder [ 0 ] )
240384 } )
241385
242386 it ( "shares one load and refresh across concurrent callers, including normalized URLs" , async ( ) => {
@@ -298,17 +442,23 @@ describe("LM Studio explicit loading", () => {
298442 forceFullModelDetailsLoad ( baseUrl , "another-model" , handler ) ,
299443 ] )
300444 expect ( post ) . toHaveBeenCalledTimes ( 2 )
301- expect ( post ) . toHaveBeenCalledWith ( `${ baseUrl } /api/v1/models/load` , { model : "another-model" } )
445+ expect ( post ) . toHaveBeenCalledWith (
446+ `${ baseUrl } /api/v1/models/load` ,
447+ { model : "another-model" } ,
448+ { signal : expect . any ( AbortSignal ) } ,
449+ )
302450 } )
303451
304452 it ( "records loaded details from the refreshed catalog rather than an optimistic load flag" , async ( ) => {
305453 get . mockResolvedValueOnce ( nativeResponse ( [ ] ) ) . mockResolvedValueOnce ( nativeResponse ( ) )
306454 post . mockResolvedValue ( { data : { } } )
455+ let loadedBeforeRefresh : boolean | undefined
307456 mockFlushModels . mockImplementationOnce ( async ( ) => {
308- expect ( hasLoadedFullDetails ( modelKey , baseUrl ) ) . toBe ( false )
457+ loadedBeforeRefresh = hasLoadedFullDetails ( modelKey , baseUrl )
309458 await getLMStudioModels ( baseUrl )
310459 } )
311460 await forceFullModelDetailsLoad ( baseUrl , modelKey , handler )
461+ expect ( loadedBeforeRefresh ) . toBe ( false )
312462 expect ( hasLoadedFullDetails ( modelKey , baseUrl ) ) . toBe ( true )
313463 } )
314464
0 commit comments