@@ -48,6 +48,7 @@ vi.mock("../kenari")
4848vi . mock ( "../nanogpt" )
4949vi . mock ( "../moonshot" )
5050vi . mock ( "../zoo-gateway" )
51+ vi . mock ( "../kimi-code" )
5152
5253// Mock ContextProxy with a simple static instance
5354vi . mock ( "../../../core/config/ContextProxy" , ( ) => ( {
@@ -62,6 +63,7 @@ vi.mock("../../../core/config/ContextProxy", () => ({
6263
6364// Then imports
6465import type { Mock , Mocked } from "vitest"
66+ import type { ModelRecord } from "@roo-code/types"
6567import { providerIdentifiers } from "@roo-code/types"
6668import * as fsSync from "fs"
6769import NodeCache from "node-cache"
@@ -74,6 +76,7 @@ import { getKenariModels } from "../kenari"
7476import { getNanoGptModels } from "../nanogpt"
7577import { getMoonshotModels } from "../moonshot"
7678import { getZooGatewayModels } from "../zoo-gateway"
79+ import { getKimiCodeModels } from "../kimi-code"
7780
7881const mockGetLiteLLMModels = getLiteLLMModels as Mock < typeof getLiteLLMModels >
7982const mockGetOpenRouterModels = getOpenRouterModels as Mock < typeof getOpenRouterModels >
@@ -82,8 +85,9 @@ const mockGetKenariModels = getKenariModels as Mock<typeof getKenariModels>
8285const mockGetNanoGptModels = getNanoGptModels as Mock < typeof getNanoGptModels >
8386const mockGetMoonshotModels = getMoonshotModels as Mock < typeof getMoonshotModels >
8487const mockGetZooGatewayModels = getZooGatewayModels as Mock < typeof getZooGatewayModels >
88+ const mockGetKimiCodeModels = getKimiCodeModels as Mock < typeof getKimiCodeModels >
8589
86- function zooGatewayOk ( models : Record < string , unknown > ) {
90+ function zooGatewayOk ( models : ModelRecord ) {
8791 return { kind : "ok" as const , models }
8892}
8993
@@ -1212,11 +1216,13 @@ describe("auth session cache", () => {
12121216
12131217 let freshGetModels : ModelCacheModule [ "getModels" ]
12141218 let freshRefreshModels : ModelCacheModule [ "refreshModels" ]
1219+ let freshFlushModels : ModelCacheModule [ "flushModels" ]
12151220 let freshClearAuthSessionModelsForProvider : ModelCacheModule [ "clearAuthSessionModelsForProvider" ]
12161221 let freshMockGetZooGatewayModels : Mock < typeof getZooGatewayModels >
1222+ let freshMockGetKimiCodeModels : Mock < typeof getKimiCodeModels >
12171223 let mockSet : Mocked < NodeCache > [ "set" ]
12181224
1219- const zooModels = {
1225+ const zooModels : ModelRecord = {
12201226 "anthropic/claude-sonnet-4" : {
12211227 maxTokens : 64000 ,
12221228 contextWindow : 200000 ,
@@ -1230,15 +1236,18 @@ describe("auth session cache", () => {
12301236
12311237 const modelCacheModule : ModelCacheModule = await import ( "../modelCache" )
12321238 const zooGatewayModule = await import ( "../zoo-gateway" )
1239+ const kimiCodeModule = await import ( "../kimi-code" )
12331240 const MockedNodeCache = vi . mocked ( NodeCache )
12341241 const mockCache = vi . mocked ( new MockedNodeCache ( ) )
12351242 mockCache . get . mockReturnValue ( undefined )
12361243 mockSet = mockCache . set
12371244
12381245 freshGetModels = modelCacheModule . getModels
12391246 freshRefreshModels = modelCacheModule . refreshModels
1247+ freshFlushModels = modelCacheModule . flushModels
12401248 freshClearAuthSessionModelsForProvider = modelCacheModule . clearAuthSessionModelsForProvider
12411249 freshMockGetZooGatewayModels = zooGatewayModule . getZooGatewayModels as Mock < typeof getZooGatewayModels >
1250+ freshMockGetKimiCodeModels = kimiCodeModule . getKimiCodeModels as Mock < typeof getKimiCodeModels >
12421251 } )
12431252
12441253 it ( "reuses in-memory session cache within TTL without refetching" , async ( ) => {
@@ -1305,4 +1314,73 @@ describe("auth session cache", () => {
13051314 vi . useRealTimers ( )
13061315 }
13071316 } )
1317+
1318+ it ( "flushModels clears session cache and can force a refetch" , async ( ) => {
1319+ freshMockGetZooGatewayModels . mockResolvedValue ( zooGatewayOk ( zooModels ) )
1320+ const options = { provider : providerIdentifiers . zooGateway , apiKey : "session-token" }
1321+
1322+ await freshGetModels ( options )
1323+ expect ( freshMockGetZooGatewayModels ) . toHaveBeenCalledTimes ( 1 )
1324+
1325+ await freshFlushModels ( options , true )
1326+
1327+ expect ( freshMockGetZooGatewayModels ) . toHaveBeenCalledTimes ( 2 )
1328+ } )
1329+
1330+ it ( "returns the prior session catalog when a fetch throws" , async ( ) => {
1331+ vi . useFakeTimers ( )
1332+ try {
1333+ freshMockGetZooGatewayModels
1334+ . mockResolvedValueOnce ( zooGatewayOk ( zooModels ) )
1335+ . mockRejectedValueOnce ( new Error ( "network down" ) )
1336+ const options = { provider : providerIdentifiers . zooGateway , apiKey : "session-token" }
1337+
1338+ await freshGetModels ( options )
1339+ vi . advanceTimersByTime ( 5 * 60 * 1000 + 1 )
1340+ const second = await freshGetModels ( options )
1341+
1342+ expect ( second ) . toEqual ( zooModels )
1343+ } finally {
1344+ vi . useRealTimers ( )
1345+ }
1346+ } )
1347+
1348+ it ( "refreshModels returns an empty catalog when refresh throws and no session cache exists" , async ( ) => {
1349+ freshMockGetZooGatewayModels . mockRejectedValue ( new Error ( "network down" ) )
1350+ const options = { provider : providerIdentifiers . zooGateway , apiKey : "session-token" }
1351+
1352+ const refreshed = await freshRefreshModels ( options )
1353+
1354+ expect ( refreshed ) . toEqual ( { } )
1355+ } )
1356+
1357+ it ( "refreshModels keeps the prior session catalog when refresh throws" , async ( ) => {
1358+ freshMockGetZooGatewayModels
1359+ . mockResolvedValueOnce ( zooGatewayOk ( zooModels ) )
1360+ . mockRejectedValueOnce ( new Error ( "network down" ) )
1361+ const options = { provider : providerIdentifiers . zooGateway , apiKey : "session-token" }
1362+
1363+ await freshGetModels ( options )
1364+ const refreshed = await freshRefreshModels ( options )
1365+
1366+ expect ( refreshed ) . toEqual ( zooModels )
1367+ } )
1368+
1369+ it ( "caches kimi-code catalogs in the session store" , async ( ) => {
1370+ const kimiModels : ModelRecord = {
1371+ "kimi-for-coding" : {
1372+ maxTokens : 8192 ,
1373+ contextWindow : 128000 ,
1374+ supportsPromptCache : false ,
1375+ } ,
1376+ }
1377+ freshMockGetKimiCodeModels . mockResolvedValue ( kimiModels )
1378+ const options = { provider : providerIdentifiers . kimiCode , apiKey : "kimi-session" }
1379+
1380+ await freshGetModels ( options )
1381+ await freshGetModels ( options )
1382+
1383+ expect ( freshMockGetKimiCodeModels ) . toHaveBeenCalledTimes ( 1 )
1384+ expect ( mockSet ) . not . toHaveBeenCalled ( )
1385+ } )
13081386} )
0 commit comments