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