|
| 1 | +// npx vitest run src/api/providers/__tests__/venice.spec.ts |
| 2 | + |
| 3 | +import OpenAI from "openai" |
| 4 | +import { Anthropic } from "@anthropic-ai/sdk" |
| 5 | + |
| 6 | +import { type VeniceModelId, veniceDefaultModelId, veniceModels } from "@roo-code/types" |
| 7 | + |
| 8 | +import { VeniceHandler } from "../venice" |
| 9 | + |
| 10 | +vitest.mock("openai", () => { |
| 11 | + const createMock = vitest.fn() |
| 12 | + return { |
| 13 | + default: vitest.fn(() => ({ chat: { completions: { create: createMock } } })), |
| 14 | + } |
| 15 | +}) |
| 16 | + |
| 17 | +describe("VeniceHandler", () => { |
| 18 | + let handler: VeniceHandler |
| 19 | + let mockCreate: any |
| 20 | + |
| 21 | + beforeEach(() => { |
| 22 | + vitest.clearAllMocks() |
| 23 | + mockCreate = (OpenAI as unknown as any)().chat.completions.create |
| 24 | + handler = new VeniceHandler({ veniceApiKey: "test-venice-api-key" }) |
| 25 | + }) |
| 26 | + |
| 27 | + it("should use the correct Venice base URL", () => { |
| 28 | + new VeniceHandler({ veniceApiKey: "test-venice-api-key" }) |
| 29 | + expect(OpenAI).toHaveBeenCalledWith(expect.objectContaining({ baseURL: "https://api.venice.ai/api/v1" })) |
| 30 | + }) |
| 31 | + |
| 32 | + it("should use the provided API key", () => { |
| 33 | + const veniceApiKey = "test-venice-api-key" |
| 34 | + new VeniceHandler({ veniceApiKey }) |
| 35 | + expect(OpenAI).toHaveBeenCalledWith(expect.objectContaining({ apiKey: veniceApiKey })) |
| 36 | + }) |
| 37 | + |
| 38 | + it("should return default model when no model is specified", () => { |
| 39 | + const model = handler.getModel() |
| 40 | + expect(model.id).toBe(veniceDefaultModelId) |
| 41 | + expect(model.info).toEqual(veniceModels[veniceDefaultModelId]) |
| 42 | + }) |
| 43 | + |
| 44 | + it("should return specified model when valid model is provided", () => { |
| 45 | + const testModelId: VeniceModelId = "deepseek-r1-671b" |
| 46 | + const handlerWithModel = new VeniceHandler({ |
| 47 | + apiModelId: testModelId, |
| 48 | + veniceApiKey: "test-venice-api-key", |
| 49 | + }) |
| 50 | + const model = handlerWithModel.getModel() |
| 51 | + expect(model.id).toBe(testModelId) |
| 52 | + expect(model.info).toEqual(veniceModels[testModelId]) |
| 53 | + }) |
| 54 | + |
| 55 | + it("completePrompt method should return text from Venice API", async () => { |
| 56 | + const expectedResponse = "This is a test response from Venice" |
| 57 | + mockCreate.mockResolvedValueOnce({ choices: [{ message: { content: expectedResponse } }] }) |
| 58 | + const result = await handler.completePrompt("test prompt") |
| 59 | + expect(result).toBe(expectedResponse) |
| 60 | + }) |
| 61 | + |
| 62 | + it("should handle errors in completePrompt", async () => { |
| 63 | + const errorMessage = "Venice API error" |
| 64 | + mockCreate.mockRejectedValueOnce(new Error(errorMessage)) |
| 65 | + await expect(handler.completePrompt("test prompt")).rejects.toThrow(`Venice completion error: ${errorMessage}`) |
| 66 | + }) |
| 67 | + |
| 68 | + it("createMessage should yield text content from stream", async () => { |
| 69 | + const testContent = "This is test content from Venice stream" |
| 70 | + |
| 71 | + mockCreate.mockImplementationOnce(() => { |
| 72 | + return { |
| 73 | + [Symbol.asyncIterator]: () => ({ |
| 74 | + next: vitest |
| 75 | + .fn() |
| 76 | + .mockResolvedValueOnce({ |
| 77 | + done: false, |
| 78 | + value: { choices: [{ delta: { content: testContent } }] }, |
| 79 | + }) |
| 80 | + .mockResolvedValueOnce({ done: true }), |
| 81 | + }), |
| 82 | + } |
| 83 | + }) |
| 84 | + |
| 85 | + const stream = handler.createMessage("system prompt", []) |
| 86 | + const firstChunk = await stream.next() |
| 87 | + |
| 88 | + expect(firstChunk.done).toBe(false) |
| 89 | + expect(firstChunk.value).toEqual({ type: "text", text: testContent }) |
| 90 | + }) |
| 91 | + |
| 92 | + it("createMessage should yield usage data from stream", async () => { |
| 93 | + mockCreate.mockImplementationOnce(() => { |
| 94 | + return { |
| 95 | + [Symbol.asyncIterator]: () => ({ |
| 96 | + next: vitest |
| 97 | + .fn() |
| 98 | + .mockResolvedValueOnce({ |
| 99 | + done: false, |
| 100 | + value: { |
| 101 | + choices: [{ delta: { content: "" } }], |
| 102 | + usage: { prompt_tokens: 10, completion_tokens: 5 }, |
| 103 | + }, |
| 104 | + }) |
| 105 | + .mockResolvedValueOnce({ done: true }), |
| 106 | + }), |
| 107 | + } |
| 108 | + }) |
| 109 | + |
| 110 | + const stream = handler.createMessage("system prompt", []) |
| 111 | + const firstChunk = await stream.next() |
| 112 | + |
| 113 | + expect(firstChunk.done).toBe(false) |
| 114 | + expect(firstChunk.value).toEqual( |
| 115 | + expect.objectContaining({ |
| 116 | + type: "usage", |
| 117 | + inputTokens: 10, |
| 118 | + outputTokens: 5, |
| 119 | + }), |
| 120 | + ) |
| 121 | + }) |
| 122 | + |
| 123 | + it("should pass the correct parameters to OpenAI API", async () => { |
| 124 | + mockCreate.mockImplementationOnce(() => { |
| 125 | + return { |
| 126 | + [Symbol.asyncIterator]: () => ({ |
| 127 | + next: vitest.fn().mockResolvedValueOnce({ done: true }), |
| 128 | + }), |
| 129 | + } |
| 130 | + }) |
| 131 | + |
| 132 | + const systemPrompt = "You are a helpful assistant" |
| 133 | + const messages: Anthropic.Messages.MessageParam[] = [{ role: "user", content: "Hello" }] |
| 134 | + |
| 135 | + const stream = handler.createMessage(systemPrompt, messages) |
| 136 | + |
| 137 | + // Consume the stream |
| 138 | + const results = [] |
| 139 | + for await (const chunk of stream) { |
| 140 | + results.push(chunk) |
| 141 | + } |
| 142 | + |
| 143 | + const callArgs = mockCreate.mock.calls[0][0] |
| 144 | + expect(callArgs.model).toBe(veniceDefaultModelId) |
| 145 | + expect(callArgs.stream).toBe(true) |
| 146 | + }) |
| 147 | +}) |
0 commit comments