Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 101 additions & 0 deletions src/api/providers/__tests__/lm-studio-timeout.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
// npx vitest run api/providers/__tests__/lm-studio-timeout.spec.ts

import { LmStudioHandler } from "../lm-studio"
import { ApiHandlerOptions } from "../../../shared/api"
import * as vscode from "vscode"

// Mock vscode
vitest.mock("vscode", () => ({
workspace: {
getConfiguration: vitest.fn().mockReturnValue({
get: vitest.fn(),
}),
},
}))

// Mock OpenAI
const mockOpenAIConstructor = vitest.fn()
vitest.mock("openai", () => {
return {
__esModule: true,
default: vitest.fn().mockImplementation((config) => {
mockOpenAIConstructor(config)
return {
chat: {
completions: {
create: vitest.fn(),
},
},
}
}),
}
})

describe("LmStudioHandler timeout configuration", () => {
let mockGetConfig: any

beforeEach(() => {
vitest.clearAllMocks()
mockGetConfig = vitest.fn()
;(vscode.workspace.getConfiguration as any).mockReturnValue({
get: mockGetConfig,
})
})

it("should use default timeout of 600 seconds when no configuration is set", () => {
mockGetConfig.mockReturnValue(600)

const options: ApiHandlerOptions = {
apiModelId: "llama2",
lmStudioModelId: "llama2",
lmStudioBaseUrl: "http://localhost:1234",
}

new LmStudioHandler(options)

expect(vscode.workspace.getConfiguration).toHaveBeenCalledWith("roo-cline")
expect(mockGetConfig).toHaveBeenCalledWith("apiRequestTimeout", 600)
expect(mockOpenAIConstructor).toHaveBeenCalledWith(
expect.objectContaining({
baseURL: "http://localhost:1234/v1",
apiKey: "noop",
timeout: 600000, // 600 seconds in milliseconds
}),
)
})

it("should use custom timeout when configuration is set", () => {
mockGetConfig.mockReturnValue(1200) // 20 minutes

const options: ApiHandlerOptions = {
apiModelId: "llama2",
lmStudioModelId: "llama2",
lmStudioBaseUrl: "http://localhost:1234",
}

new LmStudioHandler(options)

expect(mockOpenAIConstructor).toHaveBeenCalledWith(
expect.objectContaining({
timeout: 1200000, // 1200 seconds in milliseconds
}),
)
})

it("should handle zero timeout (no timeout)", () => {
mockGetConfig.mockReturnValue(0)

const options: ApiHandlerOptions = {
apiModelId: "llama2",
lmStudioModelId: "llama2",
}

new LmStudioHandler(options)

expect(mockOpenAIConstructor).toHaveBeenCalledWith(
expect.objectContaining({
timeout: 0, // No timeout
}),
)
})
})
118 changes: 118 additions & 0 deletions src/api/providers/__tests__/ollama-timeout.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
// npx vitest run api/providers/__tests__/ollama-timeout.spec.ts

import { OllamaHandler } from "../ollama"
import { ApiHandlerOptions } from "../../../shared/api"
import * as vscode from "vscode"

// Mock vscode
vitest.mock("vscode", () => ({
workspace: {
getConfiguration: vitest.fn().mockReturnValue({
get: vitest.fn(),
}),
},
}))

// Mock OpenAI
const mockOpenAIConstructor = vitest.fn()
vitest.mock("openai", () => {
return {
__esModule: true,
default: vitest.fn().mockImplementation((config) => {
mockOpenAIConstructor(config)
return {
chat: {
completions: {
create: vitest.fn(),
},
},
}
}),
}
})

describe("OllamaHandler timeout configuration", () => {
let mockGetConfig: any

beforeEach(() => {
vitest.clearAllMocks()
mockGetConfig = vitest.fn()
;(vscode.workspace.getConfiguration as any).mockReturnValue({
get: mockGetConfig,
})
})

it("should use default timeout of 600 seconds when no configuration is set", () => {
mockGetConfig.mockReturnValue(600)

const options: ApiHandlerOptions = {
apiModelId: "llama2",
ollamaModelId: "llama2",
ollamaBaseUrl: "http://localhost:11434",
}

new OllamaHandler(options)

expect(vscode.workspace.getConfiguration).toHaveBeenCalledWith("roo-cline")
expect(mockGetConfig).toHaveBeenCalledWith("apiRequestTimeout", 600)
expect(mockOpenAIConstructor).toHaveBeenCalledWith(
expect.objectContaining({
baseURL: "http://localhost:11434/v1",
apiKey: "ollama",
timeout: 600000, // 600 seconds in milliseconds
}),
)
})

it("should use custom timeout when configuration is set", () => {
mockGetConfig.mockReturnValue(3600) // 1 hour

const options: ApiHandlerOptions = {
apiModelId: "llama2",
ollamaModelId: "llama2",
}

new OllamaHandler(options)

expect(mockOpenAIConstructor).toHaveBeenCalledWith(
expect.objectContaining({
timeout: 3600000, // 3600 seconds in milliseconds
}),
)
})

it("should handle zero timeout (no timeout)", () => {
mockGetConfig.mockReturnValue(0)

const options: ApiHandlerOptions = {
apiModelId: "llama2",
ollamaModelId: "llama2",
ollamaBaseUrl: "http://localhost:11434",
}

new OllamaHandler(options)

expect(mockOpenAIConstructor).toHaveBeenCalledWith(
expect.objectContaining({
timeout: 0, // No timeout
}),
)
})

it("should use default base URL when not provided", () => {
mockGetConfig.mockReturnValue(600)

const options: ApiHandlerOptions = {
apiModelId: "llama2",
ollamaModelId: "llama2",
}

new OllamaHandler(options)

expect(mockOpenAIConstructor).toHaveBeenCalledWith(
expect.objectContaining({
baseURL: "http://localhost:11434/v1",
}),
)
})
})
154 changes: 154 additions & 0 deletions src/api/providers/__tests__/openai-timeout.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
// npx vitest run api/providers/__tests__/openai-timeout.spec.ts

import { OpenAiHandler } from "../openai"
import { ApiHandlerOptions } from "../../../shared/api"
import * as vscode from "vscode"

// Mock vscode
vitest.mock("vscode", () => ({
workspace: {
getConfiguration: vitest.fn().mockReturnValue({
get: vitest.fn(),
}),
},
}))

// Mock OpenAI and AzureOpenAI
const mockOpenAIConstructor = vitest.fn()
const mockAzureOpenAIConstructor = vitest.fn()

vitest.mock("openai", () => {
return {
__esModule: true,
default: vitest.fn().mockImplementation((config) => {
mockOpenAIConstructor(config)
return {
chat: {
completions: {
create: vitest.fn(),
},
},
}
}),
AzureOpenAI: vitest.fn().mockImplementation((config) => {
mockAzureOpenAIConstructor(config)
return {
chat: {
completions: {
create: vitest.fn(),
},
},
}
}),
}
})

describe("OpenAiHandler timeout configuration", () => {
let mockGetConfig: any

beforeEach(() => {
vitest.clearAllMocks()
mockGetConfig = vitest.fn()
;(vscode.workspace.getConfiguration as any).mockReturnValue({
get: mockGetConfig,
})
})

it("should use default timeout for standard OpenAI", () => {
mockGetConfig.mockReturnValue(600)

const options: ApiHandlerOptions = {
apiModelId: "gpt-4",
openAiModelId: "gpt-4",
openAiApiKey: "test-key",
}

new OpenAiHandler(options)

expect(vscode.workspace.getConfiguration).toHaveBeenCalledWith("roo-cline")
expect(mockGetConfig).toHaveBeenCalledWith("apiRequestTimeout", 600)
expect(mockOpenAIConstructor).toHaveBeenCalledWith(
expect.objectContaining({
baseURL: "https://api.openai.com/v1",
apiKey: "test-key",
timeout: 600000, // 600 seconds in milliseconds
}),
)
})

it("should use custom timeout for OpenAI-compatible providers", () => {
mockGetConfig.mockReturnValue(1800) // 30 minutes

const options: ApiHandlerOptions = {
apiModelId: "custom-model",
openAiModelId: "custom-model",
openAiBaseUrl: "http://localhost:8080/v1",
openAiApiKey: "test-key",
}

new OpenAiHandler(options)

expect(mockOpenAIConstructor).toHaveBeenCalledWith(
expect.objectContaining({
baseURL: "http://localhost:8080/v1",
timeout: 1800000, // 1800 seconds in milliseconds
}),
)
})

it("should use timeout for Azure OpenAI", () => {
mockGetConfig.mockReturnValue(900) // 15 minutes

const options: ApiHandlerOptions = {
apiModelId: "gpt-4",
openAiModelId: "gpt-4",
openAiBaseUrl: "https://myinstance.openai.azure.com",
openAiApiKey: "test-key",
openAiUseAzure: true,
}

new OpenAiHandler(options)

expect(mockAzureOpenAIConstructor).toHaveBeenCalledWith(
expect.objectContaining({
timeout: 900000, // 900 seconds in milliseconds
}),
)
})

it("should use timeout for Azure AI Inference", () => {
mockGetConfig.mockReturnValue(1200) // 20 minutes

const options: ApiHandlerOptions = {
apiModelId: "deepseek",
openAiModelId: "deepseek",
openAiBaseUrl: "https://myinstance.services.ai.azure.com",
openAiApiKey: "test-key",
}

new OpenAiHandler(options)

expect(mockOpenAIConstructor).toHaveBeenCalledWith(
expect.objectContaining({
timeout: 1200000, // 1200 seconds in milliseconds
}),
)
})

it("should handle zero timeout (no timeout)", () => {
mockGetConfig.mockReturnValue(0)

const options: ApiHandlerOptions = {
apiModelId: "gpt-4",
openAiModelId: "gpt-4",
}

new OpenAiHandler(options)

expect(mockOpenAIConstructor).toHaveBeenCalledWith(
expect.objectContaining({
timeout: 0, // No timeout
}),
)
})
})
1 change: 1 addition & 0 deletions src/api/providers/__tests__/openai.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ describe("OpenAiHandler", () => {
"X-Title": "Roo Code",
"User-Agent": `RooCode/${Package.version}`,
},
timeout: expect.any(Number),
})
})
})
Expand Down
Loading