Skip to content

Commit 5d1d81c

Browse files
committed
fix(moonshot): address PR review feedback — add tests and fix inline import type
- Replace inline import() type with top-level OpenAI.Chat.Completions.X in moonshot.ts - Add useSelectedModel moonshot tests (default model, router override, unknown fallback, getValidatedModelId) - Add modelCache moonshot dispatch test for fetchModelsFromProvider switch - Create Moonshot.spec.tsx with state machine tests (idle/loading/success/error states, race condition guard)
1 parent 8197efd commit 5d1d81c

4 files changed

Lines changed: 522 additions & 2 deletions

File tree

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ vi.mock("../litellm")
4444
vi.mock("../openrouter")
4545
vi.mock("../requesty")
4646
vi.mock("../kenari")
47+
vi.mock("../moonshot")
4748

4849
// Mock ContextProxy with a simple static instance
4950
vi.mock("../../../core/config/ContextProxy", () => ({
@@ -65,11 +66,13 @@ import { getLiteLLMModels } from "../litellm"
6566
import { getOpenRouterModels } from "../openrouter"
6667
import { getRequestyModels } from "../requesty"
6768
import { getKenariModels } from "../kenari"
69+
import { getMoonshotModels } from "../moonshot"
6870

6971
const mockGetLiteLLMModels = getLiteLLMModels as Mock<typeof getLiteLLMModels>
7072
const mockGetOpenRouterModels = getOpenRouterModels as Mock<typeof getOpenRouterModels>
7173
const mockGetRequestyModels = getRequestyModels as Mock<typeof getRequestyModels>
7274
const mockGetKenariModels = getKenariModels as Mock<typeof getKenariModels>
75+
const mockGetMoonshotModels = getMoonshotModels as Mock<typeof getMoonshotModels>
7376

7477
const DUMMY_REQUESTY_KEY = "requesty-key-for-testing"
7578

@@ -163,6 +166,27 @@ describe("getModels with new GetModelsOptions", () => {
163166
).rejects.toThrow("LiteLLM connection failed")
164167
})
165168

169+
it("calls getMoonshotModels with correct parameters", async () => {
170+
const mockModels = {
171+
"kimi-k2-0905-preview": {
172+
maxTokens: 16384,
173+
contextWindow: 262144,
174+
supportsPromptCache: true,
175+
description: "Moonshot Kimi K2",
176+
},
177+
}
178+
mockGetMoonshotModels.mockResolvedValue(mockModels)
179+
180+
const result = await getModels({
181+
provider: "moonshot",
182+
apiKey: "test-key",
183+
baseUrl: "https://api.moonshot.ai/v1",
184+
})
185+
186+
expect(mockGetMoonshotModels).toHaveBeenCalledWith("https://api.moonshot.ai/v1", "test-key")
187+
expect(result).toEqual(mockModels)
188+
})
189+
166190
it("validates exhaustive provider checking with unknown provider", async () => {
167191
// This test ensures TypeScript catches unknown providers at compile time
168192
// In practice, the discriminated union should prevent this at compile time

src/api/providers/moonshot.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import OpenAI from "openai"
2+
13
import { moonshotModels, moonshotDefaultModelId, type ModelInfo } from "@roo-code/types"
24

35
import type { ApiHandlerOptions } from "../../shared/api"
@@ -76,8 +78,8 @@ export class MoonshotHandler extends OpenAiHandler {
7678
*/
7779
protected override addMaxTokensIfNeeded(
7880
requestOptions:
79-
| import("openai/resources/chat/completions").ChatCompletionCreateParamsStreaming
80-
| import("openai/resources/chat/completions").ChatCompletionCreateParamsNonStreaming,
81+
| OpenAI.Chat.Completions.ChatCompletionCreateParamsStreaming
82+
| OpenAI.Chat.Completions.ChatCompletionCreateParamsNonStreaming,
8183
modelInfo: ModelInfo,
8284
): void {
8385
// Moonshot always requires max_tokens (not max_completion_tokens)

0 commit comments

Comments
 (0)