Skip to content

Commit 2f491d3

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 29dc58f commit 2f491d3

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
@@ -43,6 +43,7 @@ vi.mock("fs", () => ({
4343
vi.mock("../litellm")
4444
vi.mock("../openrouter")
4545
vi.mock("../requesty")
46+
vi.mock("../moonshot")
4647

4748
// Mock ContextProxy with a simple static instance
4849
vi.mock("../../../core/config/ContextProxy", () => ({
@@ -63,10 +64,12 @@ import { getModels, getModelsFromCache } from "../modelCache"
6364
import { getLiteLLMModels } from "../litellm"
6465
import { getOpenRouterModels } from "../openrouter"
6566
import { getRequestyModels } from "../requesty"
67+
import { getMoonshotModels } from "../moonshot"
6668

6769
const mockGetLiteLLMModels = getLiteLLMModels as Mock<typeof getLiteLLMModels>
6870
const mockGetOpenRouterModels = getOpenRouterModels as Mock<typeof getOpenRouterModels>
6971
const mockGetRequestyModels = getRequestyModels as Mock<typeof getRequestyModels>
72+
const mockGetMoonshotModels = getMoonshotModels as Mock<typeof getMoonshotModels>
7073

7174
const DUMMY_REQUESTY_KEY = "requesty-key-for-testing"
7275

@@ -143,6 +146,27 @@ describe("getModels with new GetModelsOptions", () => {
143146
).rejects.toThrow("LiteLLM connection failed")
144147
})
145148

149+
it("calls getMoonshotModels with correct parameters", async () => {
150+
const mockModels = {
151+
"kimi-k2-0905-preview": {
152+
maxTokens: 16384,
153+
contextWindow: 262144,
154+
supportsPromptCache: true,
155+
description: "Moonshot Kimi K2",
156+
},
157+
}
158+
mockGetMoonshotModels.mockResolvedValue(mockModels)
159+
160+
const result = await getModels({
161+
provider: "moonshot",
162+
apiKey: "test-key",
163+
baseUrl: "https://api.moonshot.ai/v1",
164+
})
165+
166+
expect(mockGetMoonshotModels).toHaveBeenCalledWith("https://api.moonshot.ai/v1", "test-key")
167+
expect(result).toEqual(mockModels)
168+
})
169+
146170
it("validates exhaustive provider checking with unknown provider", async () => {
147171
// This test ensures TypeScript catches unknown providers at compile time
148172
// 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)