|
| 1 | +import { describe, it, expect, vi, beforeEach } from "vitest" |
| 2 | +import { webviewMessageHandler } from "../webviewMessageHandler" |
| 3 | +import type { ClineProvider } from "../ClineProvider" |
| 4 | + |
| 5 | +// Mock vscode (minimal) |
| 6 | +vi.mock("vscode", () => ({ |
| 7 | + window: { |
| 8 | + showErrorMessage: vi.fn(), |
| 9 | + showWarningMessage: vi.fn(), |
| 10 | + showInformationMessage: vi.fn(), |
| 11 | + }, |
| 12 | + workspace: { |
| 13 | + workspaceFolders: undefined, |
| 14 | + getConfiguration: vi.fn(() => ({ |
| 15 | + get: vi.fn(), |
| 16 | + update: vi.fn(), |
| 17 | + })), |
| 18 | + }, |
| 19 | + env: { |
| 20 | + clipboard: { writeText: vi.fn() }, |
| 21 | + openExternal: vi.fn(), |
| 22 | + }, |
| 23 | + commands: { |
| 24 | + executeCommand: vi.fn(), |
| 25 | + }, |
| 26 | + Uri: { |
| 27 | + parse: vi.fn((s: string) => ({ toString: () => s })), |
| 28 | + file: vi.fn((p: string) => ({ fsPath: p })), |
| 29 | + }, |
| 30 | + ConfigurationTarget: { |
| 31 | + Global: 1, |
| 32 | + Workspace: 2, |
| 33 | + WorkspaceFolder: 3, |
| 34 | + }, |
| 35 | +})) |
| 36 | + |
| 37 | +// Mock modelCache getModels/flushModels used by the handler |
| 38 | +const getModelsMock = vi.fn() |
| 39 | +vi.mock("../../../api/providers/fetchers/modelCache", () => ({ |
| 40 | + getModels: (...args: any[]) => getModelsMock(...args), |
| 41 | + flushModels: vi.fn(), |
| 42 | +})) |
| 43 | + |
| 44 | +describe("webviewMessageHandler - requestRouterModels provider filter", () => { |
| 45 | + let mockProvider: ClineProvider & { |
| 46 | + postMessageToWebview: ReturnType<typeof vi.fn> |
| 47 | + getState: ReturnType<typeof vi.fn> |
| 48 | + contextProxy: any |
| 49 | + log: ReturnType<typeof vi.fn> |
| 50 | + } |
| 51 | + |
| 52 | + beforeEach(() => { |
| 53 | + vi.clearAllMocks() |
| 54 | + |
| 55 | + mockProvider = { |
| 56 | + // Only methods used by this code path |
| 57 | + postMessageToWebview: vi.fn(), |
| 58 | + getState: vi.fn().mockResolvedValue({ apiConfiguration: {} }), |
| 59 | + contextProxy: { |
| 60 | + getValue: vi.fn(), |
| 61 | + setValue: vi.fn(), |
| 62 | + globalStorageUri: { fsPath: "/mock/storage" }, |
| 63 | + }, |
| 64 | + log: vi.fn(), |
| 65 | + } as any |
| 66 | + |
| 67 | + // Default mock: return distinct model maps per provider so we can verify keys |
| 68 | + getModelsMock.mockImplementation(async (options: any) => { |
| 69 | + switch (options?.provider) { |
| 70 | + case "roo": |
| 71 | + return { "roo/sonnet": { contextWindow: 8192, supportsPromptCache: false } } |
| 72 | + case "openrouter": |
| 73 | + return { "openrouter/qwen2.5": { contextWindow: 32768, supportsPromptCache: false } } |
| 74 | + case "requesty": |
| 75 | + return { "requesty/model": { contextWindow: 8192, supportsPromptCache: false } } |
| 76 | + case "deepinfra": |
| 77 | + return { "deepinfra/model": { contextWindow: 8192, supportsPromptCache: false } } |
| 78 | + case "glama": |
| 79 | + return { "glama/model": { contextWindow: 8192, supportsPromptCache: false } } |
| 80 | + case "unbound": |
| 81 | + return { "unbound/model": { contextWindow: 8192, supportsPromptCache: false } } |
| 82 | + case "vercel-ai-gateway": |
| 83 | + return { "vercel/model": { contextWindow: 8192, supportsPromptCache: false } } |
| 84 | + case "io-intelligence": |
| 85 | + return { "io/model": { contextWindow: 8192, supportsPromptCache: false } } |
| 86 | + case "litellm": |
| 87 | + return { "litellm/model": { contextWindow: 8192, supportsPromptCache: false } } |
| 88 | + default: |
| 89 | + return {} |
| 90 | + } |
| 91 | + }) |
| 92 | + }) |
| 93 | + |
| 94 | + it("fetches only requested provider when values.provider is present ('roo')", async () => { |
| 95 | + await webviewMessageHandler( |
| 96 | + mockProvider as any, |
| 97 | + { |
| 98 | + type: "requestRouterModels", |
| 99 | + values: { provider: "roo" }, |
| 100 | + } as any, |
| 101 | + ) |
| 102 | + |
| 103 | + // Should post a single routerModels message |
| 104 | + expect(mockProvider.postMessageToWebview).toHaveBeenCalledWith( |
| 105 | + expect.objectContaining({ type: "routerModels", routerModels: expect.any(Object) }), |
| 106 | + ) |
| 107 | + |
| 108 | + const call = (mockProvider.postMessageToWebview as any).mock.calls.find( |
| 109 | + (c: any[]) => c[0]?.type === "routerModels", |
| 110 | + ) |
| 111 | + expect(call).toBeTruthy() |
| 112 | + const payload = call[0] |
| 113 | + const routerModels = payload.routerModels as Record<string, Record<string, any>> |
| 114 | + |
| 115 | + // Only "roo" key should be present |
| 116 | + const keys = Object.keys(routerModels) |
| 117 | + expect(keys).toEqual(["roo"]) |
| 118 | + expect(Object.keys(routerModels.roo || {})).toContain("roo/sonnet") |
| 119 | + |
| 120 | + // getModels should have been called exactly once for roo |
| 121 | + const providersCalled = getModelsMock.mock.calls.map((c: any[]) => c[0]?.provider) |
| 122 | + expect(providersCalled).toEqual(["roo"]) |
| 123 | + }) |
| 124 | + |
| 125 | + it("defaults to aggregate fetching when no provider filter is sent", async () => { |
| 126 | + await webviewMessageHandler( |
| 127 | + mockProvider as any, |
| 128 | + { |
| 129 | + type: "requestRouterModels", |
| 130 | + } as any, |
| 131 | + ) |
| 132 | + |
| 133 | + const call = (mockProvider.postMessageToWebview as any).mock.calls.find( |
| 134 | + (c: any[]) => c[0]?.type === "routerModels", |
| 135 | + ) |
| 136 | + expect(call).toBeTruthy() |
| 137 | + const routerModels = call[0].routerModels as Record<string, Record<string, any>> |
| 138 | + |
| 139 | + // Aggregate handler initializes many known routers - ensure a few expected keys exist |
| 140 | + expect(routerModels).toHaveProperty("openrouter") |
| 141 | + expect(routerModels).toHaveProperty("roo") |
| 142 | + expect(routerModels).toHaveProperty("requesty") |
| 143 | + }) |
| 144 | + |
| 145 | + it("supports filtering another single provider ('openrouter')", async () => { |
| 146 | + await webviewMessageHandler( |
| 147 | + mockProvider as any, |
| 148 | + { |
| 149 | + type: "requestRouterModels", |
| 150 | + values: { provider: "openrouter" }, |
| 151 | + } as any, |
| 152 | + ) |
| 153 | + |
| 154 | + const call = (mockProvider.postMessageToWebview as any).mock.calls.find( |
| 155 | + (c: any[]) => c[0]?.type === "routerModels", |
| 156 | + ) |
| 157 | + expect(call).toBeTruthy() |
| 158 | + const routerModels = call[0].routerModels as Record<string, Record<string, any>> |
| 159 | + const keys = Object.keys(routerModels) |
| 160 | + |
| 161 | + expect(keys).toEqual(["openrouter"]) |
| 162 | + expect(Object.keys(routerModels.openrouter || {})).toContain("openrouter/qwen2.5") |
| 163 | + |
| 164 | + const providersCalled = getModelsMock.mock.calls.map((c: any[]) => c[0]?.provider) |
| 165 | + expect(providersCalled).toEqual(["openrouter"]) |
| 166 | + }) |
| 167 | +}) |
0 commit comments