Skip to content

Commit 36b1355

Browse files
committed
test: fix ContextProxy mock path to ensure modelCache validation paths are fully exercised
1 parent d561cdc commit 36b1355

1 file changed

Lines changed: 37 additions & 18 deletions

File tree

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

Lines changed: 37 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ vi.mock("../moonshot")
4949
vi.mock("../zoo-gateway")
5050

5151
// Mock ContextProxy with a simple static instance
52-
vi.mock("../../../core/config/ContextProxy", () => ({
52+
vi.mock("../../../../core/config/ContextProxy", () => ({
5353
ContextProxy: {
5454
instance: {
5555
globalStorageUri: {
@@ -321,9 +321,9 @@ describe("getModelsFromCache disk fallback", () => {
321321

322322
const result = getModelsFromCache(providerIdentifiers.openrouter)
323323

324-
// In the test environment, ContextProxy.instance may not be fully initialized,
325-
// so getCacheDirectoryPathSync returns undefined and disk cache is not attempted
326-
expect(result).toBeUndefined()
324+
// With ContextProxy correctly mocked, getCacheDirectoryPathSync resolves
325+
// properly and disk cache loading + validation is fully exercised.
326+
expect(result).toEqual(diskModels)
327327
})
328328

329329
it("handles disk read errors gracefully", () => {
@@ -357,29 +357,48 @@ describe("getModelsFromCache disk fallback", () => {
357357
})
358358
})
359359

360-
describe("validateModelRecord schema validation", () => {
361-
// Mirrors the modelRecordSchema used by the private validateModelRecord helper.
362-
const modelRecordSchema = z.record(z.string(), modelInfoSchema)
360+
describe("validateModelRecord via getModelsFromCache", () => {
361+
let mockCache: Mocked<NodeCache>
363362

364-
it("accepts a valid ModelRecord", () => {
363+
beforeEach(() => {
364+
vi.clearAllMocks()
365+
const MockedNodeCache = vi.mocked(NodeCache)
366+
mockCache = vi.mocked(new MockedNodeCache())
367+
// Always miss memory cache so disk path is exercised
368+
mockCache.get.mockReturnValue(undefined)
369+
vi.mocked(fsSync.existsSync).mockReturnValue(true)
370+
})
371+
372+
it("returns validated data when disk cache contains a valid ModelRecord", () => {
365373
const validModels = {
366-
"cached-model": {
367-
maxTokens: 8192,
368-
contextWindow: 200000,
369-
supportsPromptCache: true,
374+
"test-model": {
375+
maxTokens: 4096,
376+
contextWindow: 128000,
377+
supportsPromptCache: false,
370378
},
371379
}
372-
const result = modelRecordSchema.safeParse(validModels)
373380

374-
expect(result.success).toBe(true)
375-
expect(result.data).toEqual(validModels)
381+
vi.mocked(fsSync.readFileSync).mockReturnValue(JSON.stringify(validModels))
382+
383+
const result = getModelsFromCache(providerIdentifiers.openrouter)
384+
expect(result).toEqual(validModels)
376385
})
377386

378-
it("rejects data that does not conform to ModelRecord", () => {
387+
it("returns undefined and logs error when disk cache contains invalid schema data", () => {
379388
const invalidData = [{ notAModelRecord: true }]
380-
const result = modelRecordSchema.safeParse(invalidData)
381389

382-
expect(result.success).toBe(false)
390+
vi.mocked(fsSync.readFileSync).mockReturnValue(JSON.stringify(invalidData))
391+
const consoleErrorSpy = vi.spyOn(console, "error").mockImplementation(function () {})
392+
393+
const result = getModelsFromCache(providerIdentifiers.openrouter)
394+
395+
expect(result).toBeUndefined()
396+
expect(consoleErrorSpy).toHaveBeenCalledWith(
397+
expect.stringContaining("[MODEL_CACHE] Invalid disk cache for"),
398+
expect.anything(),
399+
)
400+
401+
consoleErrorSpy.mockRestore()
383402
})
384403
})
385404

0 commit comments

Comments
 (0)