|
| 1 | +import { describe, it, expect, vi, beforeEach } from "vitest" |
| 2 | +import OpenAI from "openai" |
| 3 | +import { Anthropic } from "@anthropic-ai/sdk" |
| 4 | + |
| 5 | +import { type SambaNovaModelId, sambaNovaModels } from "@roo-code/types" |
| 6 | + |
| 7 | +import { SambaNovaHandler } from "../sambanova" |
| 8 | + |
| 9 | +// Mock OpenAI |
| 10 | +vi.mock("openai", () => { |
| 11 | + const mockCreate = vi.fn() |
| 12 | + return { |
| 13 | + default: vi.fn(() => ({ |
| 14 | + chat: { |
| 15 | + completions: { |
| 16 | + create: mockCreate, |
| 17 | + }, |
| 18 | + }, |
| 19 | + })), |
| 20 | + } |
| 21 | +}) |
| 22 | + |
| 23 | +describe("SambaNovaHandler", () => { |
| 24 | + let handler: SambaNovaHandler |
| 25 | + let mockCreate: any |
| 26 | + |
| 27 | + beforeEach(() => { |
| 28 | + vi.clearAllMocks() |
| 29 | + mockCreate = (OpenAI as unknown as any)().chat.completions.create |
| 30 | + handler = new SambaNovaHandler({ sambaNovaApiKey: "test-sambanova-api-key" }) |
| 31 | + }) |
| 32 | + |
| 33 | + it("should use the correct SambaNova base URL", () => { |
| 34 | + new SambaNovaHandler({ sambaNovaApiKey: "test-sambanova-api-key" }) |
| 35 | + expect(OpenAI).toHaveBeenCalledWith(expect.objectContaining({ baseURL: "https://api.sambanova.ai/v1" })) |
| 36 | + }) |
| 37 | + |
| 38 | + it("should use the provided API key", () => { |
| 39 | + const sambaNovaApiKey = "test-sambanova-api-key" |
| 40 | + new SambaNovaHandler({ sambaNovaApiKey }) |
| 41 | + expect(OpenAI).toHaveBeenCalledWith(expect.objectContaining({ apiKey: sambaNovaApiKey })) |
| 42 | + }) |
| 43 | + |
| 44 | + it("should throw an error if API key is not provided", () => { |
| 45 | + expect(() => new SambaNovaHandler({} as any)).toThrow("API key is required") |
| 46 | + }) |
| 47 | + |
| 48 | + it("should use the specified model when provided", () => { |
| 49 | + const testModelId: SambaNovaModelId = "Meta-Llama-3.3-70B-Instruct" |
| 50 | + const handlerWithModel = new SambaNovaHandler({ |
| 51 | + apiModelId: testModelId, |
| 52 | + sambaNovaApiKey: "test-sambanova-api-key", |
| 53 | + }) |
| 54 | + const model = handlerWithModel.getModel() |
| 55 | + expect(model.id).toBe(testModelId) |
| 56 | + expect(model.info).toEqual(sambaNovaModels[testModelId]) |
| 57 | + }) |
| 58 | + |
| 59 | + it("should use the default model when no model is specified", () => { |
| 60 | + const model = handler.getModel() |
| 61 | + expect(model.id).toBe("Meta-Llama-3.3-70B-Instruct") |
| 62 | + expect(model.info).toEqual(sambaNovaModels["Meta-Llama-3.3-70B-Instruct"]) |
| 63 | + }) |
| 64 | + |
| 65 | + describe("createMessage", () => { |
| 66 | + it("should create a streaming chat completion with correct parameters", async () => { |
| 67 | + const systemPrompt = "You are a helpful assistant" |
| 68 | + const messages: Anthropic.Messages.MessageParam[] = [ |
| 69 | + { |
| 70 | + role: "user", |
| 71 | + content: "Hello", |
| 72 | + }, |
| 73 | + ] |
| 74 | + |
| 75 | + mockCreate.mockImplementation(() => { |
| 76 | + const chunks = [ |
| 77 | + { |
| 78 | + choices: [{ delta: { content: "Hi there!" } }], |
| 79 | + }, |
| 80 | + { |
| 81 | + choices: [{ delta: {} }], |
| 82 | + usage: { prompt_tokens: 10, completion_tokens: 5 }, |
| 83 | + }, |
| 84 | + ] |
| 85 | + |
| 86 | + return { |
| 87 | + [Symbol.asyncIterator]: async function* () { |
| 88 | + for (const chunk of chunks) { |
| 89 | + yield chunk |
| 90 | + } |
| 91 | + }, |
| 92 | + } |
| 93 | + }) |
| 94 | + |
| 95 | + const stream = handler.createMessage(systemPrompt, messages) |
| 96 | + const results = [] |
| 97 | + for await (const chunk of stream) { |
| 98 | + results.push(chunk) |
| 99 | + } |
| 100 | + |
| 101 | + expect(mockCreate).toHaveBeenCalledWith( |
| 102 | + expect.objectContaining({ |
| 103 | + model: "Meta-Llama-3.3-70B-Instruct", |
| 104 | + max_tokens: 8192, |
| 105 | + temperature: 0.7, |
| 106 | + messages: [ |
| 107 | + { role: "system", content: systemPrompt }, |
| 108 | + { role: "user", content: "Hello" }, |
| 109 | + ], |
| 110 | + stream: true, |
| 111 | + stream_options: { include_usage: true }, |
| 112 | + }), |
| 113 | + ) |
| 114 | + |
| 115 | + expect(results).toEqual([ |
| 116 | + { type: "text", text: "Hi there!" }, |
| 117 | + { type: "usage", inputTokens: 10, outputTokens: 5 }, |
| 118 | + ]) |
| 119 | + }) |
| 120 | + }) |
| 121 | + |
| 122 | + describe("completePrompt", () => { |
| 123 | + it("should complete a prompt successfully", async () => { |
| 124 | + const prompt = "Test prompt" |
| 125 | + const expectedResponse = "Test response" |
| 126 | + |
| 127 | + mockCreate.mockResolvedValue({ |
| 128 | + choices: [{ message: { content: expectedResponse } }], |
| 129 | + }) |
| 130 | + |
| 131 | + const result = await handler.completePrompt(prompt) |
| 132 | + |
| 133 | + expect(mockCreate).toHaveBeenCalledWith({ |
| 134 | + model: "Meta-Llama-3.3-70B-Instruct", |
| 135 | + messages: [{ role: "user", content: prompt }], |
| 136 | + }) |
| 137 | + expect(result).toBe(expectedResponse) |
| 138 | + }) |
| 139 | + |
| 140 | + it("should handle errors properly", async () => { |
| 141 | + const prompt = "Test prompt" |
| 142 | + const errorMessage = "API Error" |
| 143 | + |
| 144 | + mockCreate.mockRejectedValue(new Error(errorMessage)) |
| 145 | + |
| 146 | + await expect(handler.completePrompt(prompt)).rejects.toThrow(`SambaNova completion error: ${errorMessage}`) |
| 147 | + }) |
| 148 | + }) |
| 149 | + |
| 150 | + describe("model selection", () => { |
| 151 | + it.each(Object.keys(sambaNovaModels) as SambaNovaModelId[])("should correctly handle model %s", (modelId) => { |
| 152 | + const modelInfo = sambaNovaModels[modelId] |
| 153 | + const handlerWithModel = new SambaNovaHandler({ |
| 154 | + apiModelId: modelId, |
| 155 | + sambaNovaApiKey: "test-sambanova-api-key", |
| 156 | + }) |
| 157 | + |
| 158 | + const model = handlerWithModel.getModel() |
| 159 | + expect(model.id).toBe(modelId) |
| 160 | + expect(model.info).toEqual(modelInfo) |
| 161 | + }) |
| 162 | + }) |
| 163 | +}) |
0 commit comments