@@ -42,6 +42,14 @@ vitest.mock("@roo-code/telemetry", () => ({
4242 } ,
4343} ) )
4444
45+ const { mockGetModelEndpoints } = vitest . hoisted ( ( ) => ( {
46+ mockGetModelEndpoints : vitest . fn ( ) ,
47+ } ) )
48+
49+ vitest . mock ( "../fetchers/modelEndpointCache" , ( ) => ( {
50+ getModelEndpoints : mockGetModelEndpoints ,
51+ } ) )
52+
4553vitest . mock ( "../fetchers/modelCache" , ( ) => ( {
4654 getModels : vitest . fn ( ) . mockImplementation ( function ( ) {
4755 return Promise . resolve ( {
@@ -127,7 +135,13 @@ describe("OpenRouterHandler", () => {
127135 openRouterModelId : "anthropic/claude-sonnet-4" ,
128136 } )
129137
130- beforeEach ( ( ) => clearAllMocks ( ) )
138+ beforeEach ( ( ) => {
139+ clearAllMocks ( )
140+ // Endpoint records default to "not fetched" (same as the real guard for
141+ // missing options); tests that exercise the specific-provider branch
142+ // override this per test.
143+ mockGetModelEndpoints . mockResolvedValue ( { } )
144+ } )
131145
132146 it ( "initializes with correct options" , ( ) => {
133147 const handler = new OpenRouterHandler ( mockOptions )
@@ -265,13 +279,58 @@ describe("OpenRouterHandler", () => {
265279 // correct all of that before any request parameters are derived.
266280 expect ( result . id ) . toBe ( "moonshotai/kimi-k3" )
267281 expect ( result . maxTokens ) . toBe ( 32768 )
268- expect ( result . temperature ) . toBeUndefined ( )
282+ expect ( result . temperature ) . toBe ( 1 )
269283 expect ( result . reasoningEffort ) . toBe ( "high" )
270284 expect ( result . reasoning ) . toEqual ( { effort : "high" } )
271285 expect ( result . info . maxTokens ) . toBe ( 32768 )
272286 expect ( result . info . supportsReasoningEffort ) . toEqual ( [ "low" , "high" , "max" ] )
273287 expect ( result . info . reasoningEffort ) . toBe ( "high" )
274- expect ( result . info . supportsTemperature ) . toBe ( false )
288+ expect ( result . info . supportsTemperature ) . toBe ( true )
289+ expect ( result . info . defaultTemperature ) . toBe ( 1 )
290+ } )
291+
292+ it ( "applies the Moonshot K3 profile to a stale specific-provider endpoint record" , async ( ) => {
293+ // Endpoint records are cached separately from the parent model cache and
294+ // are selected before the profile is applied. A stale endpoint record
295+ // (fabricated max_tokens, boolean supportsReasoningEffort, no reasoning
296+ // default, no temperature default) must be corrected for the selected
297+ // endpoint.
298+ mockGetModelEndpoints . mockResolvedValue ( {
299+ moonshotai : {
300+ maxTokens : 209716 ,
301+ contextWindow : 1000000 ,
302+ supportsImages : true ,
303+ supportsPromptCache : true ,
304+ inputPrice : 0.6 ,
305+ outputPrice : 3 ,
306+ description : "Kimi K3 (Moonshot endpoint)" ,
307+ supportsReasoningEffort : true ,
308+ } ,
309+ } )
310+
311+ const handler = new OpenRouterHandler (
312+ makeApiHandlerOptions ( {
313+ openRouterApiKey : "test-key" ,
314+ openRouterModelId : "moonshotai/kimi-k3" ,
315+ openRouterSpecificProvider : "moonshotai" ,
316+ } ) ,
317+ )
318+
319+ const result = await handler . fetchModel ( )
320+
321+ // The selected endpoint record (not the parent model cache entry) must be
322+ // profiled: a regression that applied the profile before endpoint
323+ // selection would send the stale endpoint values.
324+ expect ( result . id ) . toBe ( "moonshotai/kimi-k3" )
325+ expect ( result . info . maxTokens ) . toBe ( 32768 )
326+ expect ( result . info . supportsReasoningEffort ) . toEqual ( [ "low" , "high" , "max" ] )
327+ expect ( result . info . reasoningEffort ) . toBe ( "high" )
328+ expect ( result . info . supportsTemperature ) . toBe ( true )
329+ expect ( result . info . defaultTemperature ) . toBe ( 1 )
330+ expect ( result . maxTokens ) . toBe ( 32768 )
331+ expect ( result . temperature ) . toBe ( 1 )
332+ expect ( result . reasoningEffort ) . toBe ( "high" )
333+ expect ( result . reasoning ) . toEqual ( { effort : "high" } )
275334 } )
276335 } )
277336
@@ -581,7 +640,7 @@ describe("OpenRouterHandler", () => {
581640 expect ( endChunks [ 0 ] . id ) . toBe ( "call_openrouter_test" )
582641 } )
583642
584- it ( "sends profiled max_tokens and reasoning effort, omitting temperature, for moonshotai/kimi-k3" , async ( ) => {
643+ it ( "sends profiled max_tokens, explicit temperature 1.0, and reasoning effort for moonshotai/kimi-k3" , async ( ) => {
585644 const handler = new OpenRouterHandler (
586645 makeApiHandlerOptions ( {
587646 openRouterApiKey : "test-key" ,
@@ -595,19 +654,28 @@ describe("OpenRouterHandler", () => {
595654 // The vitest-mocked OpenAI class is structurally incompatible with the narrow
596655 // chat stub; the double assertion routes through unknown (instead of any) to
597656 // keep this file's no-explicit-any budget flat.
598- ; ( OpenAI as unknown as { prototype : { chat : typeof chatStub } } ) . prototype . chat = chatStub
599-
600- const messages : Anthropic . Messages . MessageParam [ ] = [ { role : "user" , content : "test message" } ]
601- await collectStream ( handler . createMessage ( "test system prompt" , messages ) )
602-
603- const [ requestParams ] = mockCreate . mock . calls [ 0 ] as [ Record < string , unknown > ]
604- expect ( requestParams ) . toMatchObject ( {
605- model : "moonshotai/kimi-k3" ,
606- max_tokens : 32768 ,
607- reasoning : { effort : "high" } ,
608- } )
609- // K3 ignores temperature (fixed at 1.0 server-side); the request must omit it.
610- expect ( requestParams . temperature ) . toBeUndefined ( )
657+ const openAiPrototype = OpenAI as unknown as { prototype : { chat ?: unknown } }
658+ const originalChat = openAiPrototype . prototype . chat
659+ openAiPrototype . prototype . chat = chatStub
660+
661+ try {
662+ const messages : Anthropic . Messages . MessageParam [ ] = [ { role : "user" , content : "test message" } ]
663+ await collectStream ( handler . createMessage ( "test system prompt" , messages ) )
664+
665+ const [ requestParams ] = mockCreate . mock . calls [ 0 ] as [ Record < string , unknown > ]
666+ expect ( requestParams ) . toMatchObject ( {
667+ model : "moonshotai/kimi-k3" ,
668+ max_tokens : 32768 ,
669+ temperature : 1 ,
670+ reasoning : { effort : "high" } ,
671+ } )
672+ // K3 is fixed at temperature 1.0 upstream (issue #1316); the request
673+ // must carry it explicitly.
674+ } finally {
675+ // Restore the original prototype property so this stub cannot leak
676+ // into later tests; clearAllMocks does not undo prototype assignment.
677+ openAiPrototype . prototype . chat = originalChat
678+ }
611679 } )
612680 } )
613681
0 commit comments