Skip to content

Commit 7037016

Browse files
committed
fix(zoo-gateway): resolve CI type errors and raise patch coverage
Narrow auth-scoped fetch options for exhaustive switching, type zoo-gateway test mocks as ModelRecord, and add session-cache tests for flush, errors, and kimi-code.
1 parent c8ea394 commit 7037016

2 files changed

Lines changed: 99 additions & 12 deletions

File tree

‎src/api/providers/fetchers/__tests__/modelCache.spec.ts‎

Lines changed: 80 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ vi.mock("../kenari")
4848
vi.mock("../nanogpt")
4949
vi.mock("../moonshot")
5050
vi.mock("../zoo-gateway")
51+
vi.mock("../kimi-code")
5152

5253
// Mock ContextProxy with a simple static instance
5354
vi.mock("../../../core/config/ContextProxy", () => ({
@@ -62,6 +63,7 @@ vi.mock("../../../core/config/ContextProxy", () => ({
6263

6364
// Then imports
6465
import type { Mock, Mocked } from "vitest"
66+
import type { ModelRecord } from "@roo-code/types"
6567
import { providerIdentifiers } from "@roo-code/types"
6668
import * as fsSync from "fs"
6769
import NodeCache from "node-cache"
@@ -74,6 +76,7 @@ import { getKenariModels } from "../kenari"
7476
import { getNanoGptModels } from "../nanogpt"
7577
import { getMoonshotModels } from "../moonshot"
7678
import { getZooGatewayModels } from "../zoo-gateway"
79+
import { getKimiCodeModels } from "../kimi-code"
7780

7881
const mockGetLiteLLMModels = getLiteLLMModels as Mock<typeof getLiteLLMModels>
7982
const mockGetOpenRouterModels = getOpenRouterModels as Mock<typeof getOpenRouterModels>
@@ -82,8 +85,9 @@ const mockGetKenariModels = getKenariModels as Mock<typeof getKenariModels>
8285
const mockGetNanoGptModels = getNanoGptModels as Mock<typeof getNanoGptModels>
8386
const mockGetMoonshotModels = getMoonshotModels as Mock<typeof getMoonshotModels>
8487
const 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
})

‎src/api/providers/fetchers/modelCache.ts‎

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,10 @@ const AUTH_SCOPED_PROVIDERS: ReadonlySet<RouterName> = new Set([
108108
providerIdentifiers.kimiCode,
109109
])
110110

111+
type AuthScopedProvider = typeof providerIdentifiers.zooGateway | typeof providerIdentifiers.kimiCode
112+
113+
type AuthScopedGetModelsOptions = Extract<GetModelsOptions, { provider: AuthScopedProvider }>
114+
111115
const AUTH_SESSION_TTL_MS = 5 * 60 * 1000
112116

113117
type AuthSessionCacheEntry = {
@@ -120,10 +124,18 @@ type AuthSessionCacheEntry = {
120124
// and keyed by the same compound identity as getCacheKey (provider + baseUrl + token hash).
121125
const authSessionCache = new Map<string, AuthSessionCacheEntry>()
122126

123-
function isAuthScopedProvider(provider: RouterName): boolean {
127+
function isAuthScopedProvider(provider: RouterName): provider is AuthScopedProvider {
124128
return AUTH_SCOPED_PROVIDERS.has(provider)
125129
}
126130

131+
function assertAuthScopedGetModelsOptions(options: GetModelsOptions): AuthScopedGetModelsOptions {
132+
if (!isAuthScopedProvider(options.provider)) {
133+
throw new Error(`Expected auth-scoped provider, got ${options.provider}`)
134+
}
135+
// Runtime guard above; TS cannot narrow the GetModelsOptions discriminated union here.
136+
return options as AuthScopedGetModelsOptions
137+
}
138+
127139
function isAuthSessionFresh(entry: AuthSessionCacheEntry): boolean {
128140
return Date.now() - entry.fetchedAt < AUTH_SESSION_TTL_MS
129141
}
@@ -162,12 +174,10 @@ type AuthScopedFetchResult = {
162174
}
163175

164176
async function fetchAuthScopedModelsFromProvider(
165-
options: GetModelsOptions,
177+
options: AuthScopedGetModelsOptions,
166178
ifNoneMatch?: string,
167179
): Promise<AuthScopedFetchResult> {
168-
const { provider } = options
169-
170-
switch (provider) {
180+
switch (options.provider) {
171181
case providerIdentifiers.zooGateway: {
172182
const result = await getZooGatewayModels({
173183
zooSessionToken: options.apiKey,
@@ -181,10 +191,6 @@ async function fetchAuthScopedModelsFromProvider(
181191
}
182192
case providerIdentifiers.kimiCode:
183193
return { models: await getKimiCodeModels(options.apiKey) }
184-
default: {
185-
const exhaustiveCheck: never = provider
186-
throw new Error(`Unknown auth-scoped provider: ${exhaustiveCheck}`)
187-
}
188194
}
189195
}
190196

@@ -201,7 +207,10 @@ async function resolveAuthScopedModels(
201207
}
202208

203209
try {
204-
const fetched = await fetchAuthScopedModelsFromProvider(options, existing?.etag)
210+
const fetched = await fetchAuthScopedModelsFromProvider(
211+
assertAuthScopedGetModelsOptions(options),
212+
existing?.etag,
213+
)
205214

206215
if (fetched.notModified) {
207216
if (existing && Object.keys(existing.models).length > 0) {

0 commit comments

Comments
 (0)