|
| 1 | +// npx vitest run api/providers/__tests__/base-openai-compatible-provider-timeout.spec.ts |
| 2 | + |
| 3 | +import type { ModelInfo } from "@roo-code/types" |
| 4 | + |
| 5 | +import { BaseOpenAiCompatibleProvider } from "../base-openai-compatible-provider" |
| 6 | + |
| 7 | +// Mock the timeout config utility |
| 8 | +vitest.mock("../utils/timeout-config", () => ({ |
| 9 | + getApiRequestTimeout: vitest.fn(), |
| 10 | +})) |
| 11 | + |
| 12 | +import { getApiRequestTimeout } from "../utils/timeout-config" |
| 13 | + |
| 14 | +// Mock OpenAI and capture constructor calls |
| 15 | +const mockOpenAIConstructor = vitest.fn() |
| 16 | + |
| 17 | +vitest.mock("openai", () => { |
| 18 | + return { |
| 19 | + __esModule: true, |
| 20 | + default: vitest.fn().mockImplementation((config) => { |
| 21 | + mockOpenAIConstructor(config) |
| 22 | + return { |
| 23 | + chat: { |
| 24 | + completions: { |
| 25 | + create: vitest.fn(), |
| 26 | + }, |
| 27 | + }, |
| 28 | + } |
| 29 | + }), |
| 30 | + } |
| 31 | +}) |
| 32 | + |
| 33 | +// Create a concrete test implementation of the abstract base class |
| 34 | +class TestOpenAiCompatibleProvider extends BaseOpenAiCompatibleProvider<"test-model"> { |
| 35 | + constructor(apiKey: string) { |
| 36 | + const testModels: Record<"test-model", ModelInfo> = { |
| 37 | + "test-model": { |
| 38 | + maxTokens: 4096, |
| 39 | + contextWindow: 128000, |
| 40 | + supportsImages: false, |
| 41 | + supportsPromptCache: false, |
| 42 | + inputPrice: 0.5, |
| 43 | + outputPrice: 1.5, |
| 44 | + }, |
| 45 | + } |
| 46 | + |
| 47 | + super({ |
| 48 | + providerName: "TestProvider", |
| 49 | + baseURL: "https://test.example.com/v1", |
| 50 | + defaultProviderModelId: "test-model", |
| 51 | + providerModels: testModels, |
| 52 | + apiKey, |
| 53 | + }) |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +describe("BaseOpenAiCompatibleProvider Timeout Configuration", () => { |
| 58 | + beforeEach(() => { |
| 59 | + vitest.clearAllMocks() |
| 60 | + }) |
| 61 | + |
| 62 | + it("should call getApiRequestTimeout when creating the provider", () => { |
| 63 | + ;(getApiRequestTimeout as any).mockReturnValue(600000) |
| 64 | + |
| 65 | + new TestOpenAiCompatibleProvider("test-api-key") |
| 66 | + |
| 67 | + expect(getApiRequestTimeout).toHaveBeenCalled() |
| 68 | + }) |
| 69 | + |
| 70 | + it("should pass the default timeout to the OpenAI client constructor", () => { |
| 71 | + ;(getApiRequestTimeout as any).mockReturnValue(600000) // 600 seconds in ms |
| 72 | + |
| 73 | + new TestOpenAiCompatibleProvider("test-api-key") |
| 74 | + |
| 75 | + expect(mockOpenAIConstructor).toHaveBeenCalledWith( |
| 76 | + expect.objectContaining({ |
| 77 | + baseURL: "https://test.example.com/v1", |
| 78 | + apiKey: "test-api-key", |
| 79 | + timeout: 600000, |
| 80 | + }), |
| 81 | + ) |
| 82 | + }) |
| 83 | + |
| 84 | + it("should use custom timeout value from getApiRequestTimeout", () => { |
| 85 | + ;(getApiRequestTimeout as any).mockReturnValue(1800000) // 30 minutes in ms |
| 86 | + |
| 87 | + new TestOpenAiCompatibleProvider("test-api-key") |
| 88 | + |
| 89 | + expect(mockOpenAIConstructor).toHaveBeenCalledWith( |
| 90 | + expect.objectContaining({ |
| 91 | + timeout: 1800000, |
| 92 | + }), |
| 93 | + ) |
| 94 | + }) |
| 95 | + |
| 96 | + it("should handle zero timeout (no timeout)", () => { |
| 97 | + ;(getApiRequestTimeout as any).mockReturnValue(0) |
| 98 | + |
| 99 | + new TestOpenAiCompatibleProvider("test-api-key") |
| 100 | + |
| 101 | + expect(mockOpenAIConstructor).toHaveBeenCalledWith( |
| 102 | + expect.objectContaining({ |
| 103 | + timeout: 0, |
| 104 | + }), |
| 105 | + ) |
| 106 | + }) |
| 107 | + |
| 108 | + it("should pass DEFAULT_HEADERS to the OpenAI client constructor", () => { |
| 109 | + ;(getApiRequestTimeout as any).mockReturnValue(600000) |
| 110 | + |
| 111 | + new TestOpenAiCompatibleProvider("test-api-key") |
| 112 | + |
| 113 | + expect(mockOpenAIConstructor).toHaveBeenCalledWith( |
| 114 | + expect.objectContaining({ |
| 115 | + defaultHeaders: expect.any(Object), |
| 116 | + }), |
| 117 | + ) |
| 118 | + }) |
| 119 | +}) |
0 commit comments