| 1 | import { afterEach, describe, expect, test, vi } from "vitest"; |
| 2 | import { runAdapterTest } from "./adapter-test-utils.js"; |
| 3 | |
| 4 | // Mock the fetch package |
| 5 | vi.mock("@continuedev/fetch", async () => { |
| 6 | const actual = await vi.importActual("@continuedev/fetch"); |
| 7 | return { |
| 8 | ...actual, |
| 9 | fetchwithRequestOptions: vi.fn(), |
| 10 | }; |
| 11 | }); |
| 12 | |
| 13 | describe("Anthropic Adapter Tests", () => { |
| 14 | afterEach(() => { |
| 15 | vi.clearAllMocks(); |
| 16 | vi.unstubAllGlobals(); |
| 17 | }); |
| 18 | |
| 19 | test("should construct API correctly", async () => { |
| 20 | const { constructLlmApi } = await import("../index.js"); |
| 21 | const api = constructLlmApi({ |
| 22 | provider: "anthropic", |
| 23 | apiKey: "test-api-key", |
| 24 | }); |
| 25 | expect(api).toBeDefined(); |
| 26 | }); |
| 27 | |
| 28 | test("chatCompletionNonStream should send a valid request", async () => { |
| 29 | await runAdapterTest({ |
| 30 | config: { |
| 31 | provider: "anthropic", |
| 32 | apiKey: "test-api-key", |
| 33 | apiBase: "https://api.anthropic.com/v1/", |
| 34 | }, |
| 35 | methodToTest: "chatCompletionNonStream", |
| 36 | params: [ |
| 37 | { |
| 38 | model: "claude-3-5-sonnet-20241022", |
| 39 | messages: [{ role: "user", content: "hello" }], |
| 40 | }, |
| 41 | new AbortController().signal, |
| 42 | ], |
| 43 | expectedRequest: { |
| 44 | url: "https://api.anthropic.com/v1/messages", |
| 45 | method: "POST", |
| 46 | headers: { |
| 47 | "Content-Type": "application/json", |
| 48 | Accept: "application/json", |
| 49 | "anthropic-version": "2023-06-01", |
| 50 | "x-api-key": "test-api-key", |
| 51 | }, |
| 52 | body: { |
| 53 | messages: [ |
| 54 | { |
| 55 | role: "user", |
| 56 | content: [ |
| 57 | { |
| 58 | type: "text", |
| 59 | text: "hello", |
| 60 | }, |
| 61 | ], |
| 62 | }, |
| 63 | ], |
| 64 | system: undefined, |
| 65 | model: "claude-3-5-sonnet-20241022", |
| 66 | max_tokens: 32000, |
| 67 | stream: undefined, |
| 68 | }, |
| 69 | }, |
| 70 | mockResponse: { |
| 71 | id: "msg_test", |
| 72 | type: "message", |
| 73 | role: "assistant", |
| 74 | content: [ |
| 75 | { |
| 76 | type: "text", |
| 77 | text: "Hello! How can I help you today?", |
| 78 | }, |
| 79 | ], |
| 80 | model: "claude-3-5-sonnet-20241022", |
| 81 | stop_reason: "end_turn", |
| 82 | stop_sequence: null, |
| 83 | usage: { |
| 84 | input_tokens: 10, |
| 85 | output_tokens: 15, |
| 86 | }, |
| 87 | }, |
| 88 | }); |
| 89 | }); |
| 90 | |
| 91 | test("chatCompletionStream should send a valid request", async () => { |
| 92 | await runAdapterTest({ |
| 93 | config: { |
| 94 | provider: "anthropic", |
| 95 | apiKey: "test-api-key", |
| 96 | apiBase: "https://api.anthropic.com/v1/", |
| 97 | }, |
| 98 | methodToTest: "chatCompletionStream", |
| 99 | params: [ |
| 100 | { |
| 101 | model: "claude-3-5-sonnet-20241022", |
| 102 | messages: [{ role: "user", content: "hello" }], |
| 103 | stream: true, |
| 104 | }, |
| 105 | new AbortController().signal, |
| 106 | ], |
| 107 | expectedRequest: { |
| 108 | url: "https://api.anthropic.com/v1/messages", |
| 109 | method: "POST", |
| 110 | headers: { |
| 111 | "Content-Type": "application/json", |
| 112 | Accept: "application/json", |
| 113 | "anthropic-version": "2023-06-01", |
| 114 | "x-api-key": "test-api-key", |
| 115 | }, |
| 116 | body: { |
| 117 | messages: [ |
| 118 | { |
| 119 | role: "user", |
| 120 | content: [ |
| 121 | { |
| 122 | type: "text", |
| 123 | text: "hello", |
| 124 | }, |
| 125 | ], |
| 126 | }, |
| 127 | ], |
| 128 | system: undefined, |
| 129 | model: "claude-3-5-sonnet-20241022", |
| 130 | max_tokens: 32000, |
| 131 | stream: true, |
| 132 | }, |
| 133 | }, |
| 134 | mockStream: [ |
| 135 | { |
| 136 | type: "content_block_delta", |
| 137 | delta: { |
| 138 | type: "text_delta", |
| 139 | text: "Hello", |
| 140 | }, |
| 141 | }, |
| 142 | { |
| 143 | type: "content_block_delta", |
| 144 | delta: { |
| 145 | type: "text_delta", |
| 146 | text: " world", |
| 147 | }, |
| 148 | }, |
| 149 | ], |
| 150 | }); |
| 151 | }); |
| 152 | |
| 153 | test("chatCompletionStream with system message should convert correctly", async () => { |
| 154 | await runAdapterTest({ |
| 155 | config: { |
| 156 | provider: "anthropic", |
| 157 | apiKey: "test-api-key", |
| 158 | apiBase: "https://api.anthropic.com/v1/", |
| 159 | }, |
| 160 | methodToTest: "chatCompletionStream", |
| 161 | params: [ |
| 162 | { |
| 163 | model: "claude-3-5-sonnet-20241022", |
| 164 | messages: [ |
| 165 | { role: "system", content: "You are a helpful assistant." }, |
| 166 | { role: "user", content: "hello" }, |
| 167 | ], |
| 168 | stream: true, |
| 169 | }, |
| 170 | new AbortController().signal, |
| 171 | ], |
| 172 | expectedRequest: { |
| 173 | url: "https://api.anthropic.com/v1/messages", |
| 174 | method: "POST", |
| 175 | headers: { |
| 176 | "Content-Type": "application/json", |
| 177 | Accept: "application/json", |
| 178 | "anthropic-version": "2023-06-01", |
| 179 | "x-api-key": "test-api-key", |
| 180 | }, |
| 181 | body: { |
| 182 | messages: [ |
| 183 | { |
| 184 | role: "user", |
| 185 | content: [ |
| 186 | { |
| 187 | type: "text", |
| 188 | text: "hello", |
| 189 | }, |
| 190 | ], |
| 191 | }, |
| 192 | ], |
| 193 | system: [ |
| 194 | { |
| 195 | type: "text", |
| 196 | text: "You are a helpful assistant.", |
| 197 | cache_control: { type: "ephemeral" }, |
| 198 | }, |
| 199 | ], |
| 200 | model: "claude-3-5-sonnet-20241022", |
| 201 | max_tokens: 32000, |
| 202 | stream: true, |
| 203 | }, |
| 204 | }, |
| 205 | mockStream: [ |
| 206 | { |
| 207 | type: "content_block_delta", |
| 208 | delta: { |
| 209 | type: "text_delta", |
| 210 | text: "Hello", |
| 211 | }, |
| 212 | }, |
| 213 | ], |
| 214 | }); |
| 215 | }); |
| 216 | |
| 217 | test("embed should throw not implemented error", async () => { |
| 218 | const { constructLlmApi } = await import("../index.js"); |
| 219 | const api = constructLlmApi({ |
| 220 | provider: "anthropic", |
| 221 | apiKey: "test-api-key", |
| 222 | }); |
| 223 | |
| 224 | await expect( |
| 225 | api!.embed({ |
| 226 | model: "text-embedding-ada-002", |
| 227 | input: ["Hello", "World"], |
| 228 | }), |
| 229 | ).rejects.toThrow("Method not implemented."); |
| 230 | }); |
| 231 | }); |