Elffuss BraidHub

kikocis/continue public

87f75892af56 · 5.3 KiBRawPermalink
1import { afterEach, describe, expect, test, vi } from "vitest";
2import { runAdapterTest } from "./adapter-test-utils.js";
3
4// Mock the fetch package
5vi.mock("@continuedev/fetch", async () => {
6 const actual = await vi.importActual("@continuedev/fetch");
7 return {
8 ...actual,
9 fetchwithRequestOptions: vi.fn(),
10 };
11});
12
13describe("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: [{ role: "user", content: "hello" }],
54 system: undefined,
55 model: "claude-3-5-sonnet-20241022",
56 max_tokens: 32000,
57 stream: undefined,
58 },
59 },
60 mockResponse: {
61 id: "msg_test",
62 type: "message",
63 role: "assistant",
64 content: [
65 {
66 type: "text",
67 text: "Hello! How can I help you today?",
68 },
69 ],
70 model: "claude-3-5-sonnet-20241022",
71 stop_reason: "end_turn",
72 stop_sequence: null,
73 usage: {
74 input_tokens: 10,
75 output_tokens: 15,
76 },
77 },
78 });
79 });
80
81 test("chatCompletionStream should send a valid request", async () => {
82 await runAdapterTest({
83 config: {
84 provider: "anthropic",
85 apiKey: "test-api-key",
86 apiBase: "https://api.anthropic.com/v1/",
87 },
88 methodToTest: "chatCompletionStream",
89 params: [
90 {
91 model: "claude-3-5-sonnet-20241022",
92 messages: [{ role: "user", content: "hello" }],
93 stream: true,
94 },
95 new AbortController().signal,
96 ],
97 expectedRequest: {
98 url: "https://api.anthropic.com/v1/messages",
99 method: "POST",
100 headers: {
101 "Content-Type": "application/json",
102 Accept: "application/json",
103 "anthropic-version": "2023-06-01",
104 "x-api-key": "test-api-key",
105 },
106 body: {
107 messages: [{ role: "user", content: "hello" }],
108 system: undefined,
109 model: "claude-3-5-sonnet-20241022",
110 max_tokens: 32000,
111 stream: true,
112 },
113 },
114 mockStream: [
115 {
116 type: "content_block_delta",
117 delta: {
118 type: "text_delta",
119 text: "Hello",
120 },
121 },
122 {
123 type: "content_block_delta",
124 delta: {
125 type: "text_delta",
126 text: " world",
127 },
128 },
129 ],
130 });
131 });
132
133 test("chatCompletionStream with system message should convert correctly", async () => {
134 await runAdapterTest({
135 config: {
136 provider: "anthropic",
137 apiKey: "test-api-key",
138 apiBase: "https://api.anthropic.com/v1/",
139 },
140 methodToTest: "chatCompletionStream",
141 params: [
142 {
143 model: "claude-3-5-sonnet-20241022",
144 messages: [
145 { role: "system", content: "You are a helpful assistant." },
146 { role: "user", content: "hello" },
147 ],
148 stream: true,
149 },
150 new AbortController().signal,
151 ],
152 expectedRequest: {
153 url: "https://api.anthropic.com/v1/messages",
154 method: "POST",
155 headers: {
156 "Content-Type": "application/json",
157 Accept: "application/json",
158 "anthropic-version": "2023-06-01",
159 "x-api-key": "test-api-key",
160 },
161 body: {
162 messages: [{ role: "user", content: "hello" }],
163 system: [
164 {
165 type: "text",
166 text: "You are a helpful assistant.",
167 cache_control: { type: "ephemeral" },
168 },
169 ],
170 model: "claude-3-5-sonnet-20241022",
171 max_tokens: 32000,
172 stream: true,
173 },
174 },
175 mockStream: [
176 {
177 type: "content_block_delta",
178 delta: {
179 type: "text_delta",
180 text: "Hello",
181 },
182 },
183 ],
184 });
185 });
186
187 test("embed should throw not implemented error", async () => {
188 const { constructLlmApi } = await import("../index.js");
189 const api = constructLlmApi({
190 provider: "anthropic",
191 apiKey: "test-api-key",
192 });
193
194 await expect(
195 api!.embed({
196 model: "text-embedding-ada-002",
197 input: ["Hello", "World"],
198 }),
199 ).rejects.toThrow("Method not implemented.");
200 });
201});