Anthropic tests
This commit is contained in:
parent
440bc07420
commit
9b74ef03fd
3 changed files with 217 additions and 1 deletions
|
|
@ -127,6 +127,12 @@ export async function runAdapterTest(testCase: AdapterTestCase) {
|
|||
// Mock fetch globally before constructing the API
|
||||
vi.stubGlobal('fetch', mockFetch);
|
||||
|
||||
// For non-OpenAI adapters, mock the fetch package
|
||||
const fetchPackage = await import("@continuedev/fetch");
|
||||
if (fetchPackage.fetchwithRequestOptions) {
|
||||
vi.mocked(fetchPackage.fetchwithRequestOptions).mockImplementation(mockFetch as any);
|
||||
}
|
||||
|
||||
const api = constructLlmApi(config);
|
||||
if (!api) {
|
||||
throw new Error(`Failed to construct API for provider: ${config.provider}`);
|
||||
|
|
|
|||
201
packages/openai-adapters/src/test/anthropic-adapter.vitest.ts
Normal file
201
packages/openai-adapters/src/test/anthropic-adapter.vitest.ts
Normal file
|
|
@ -0,0 +1,201 @@
|
|||
import { describe, test, vi, expect, afterEach } from "vitest";
|
||||
import { runAdapterTest } from "./adapter-test-utils.js";
|
||||
|
||||
// Mock the fetch package
|
||||
vi.mock("@continuedev/fetch", async () => {
|
||||
const actual = await vi.importActual("@continuedev/fetch");
|
||||
return {
|
||||
...actual,
|
||||
fetchwithRequestOptions: vi.fn(),
|
||||
};
|
||||
});
|
||||
|
||||
describe("Anthropic Adapter Tests", () => {
|
||||
afterEach(() => {
|
||||
vi.clearAllMocks();
|
||||
vi.unstubAllGlobals();
|
||||
});
|
||||
|
||||
test("should construct API correctly", async () => {
|
||||
const { constructLlmApi } = await import("../index.js");
|
||||
const api = constructLlmApi({
|
||||
provider: "anthropic",
|
||||
apiKey: "test-api-key",
|
||||
});
|
||||
expect(api).toBeDefined();
|
||||
});
|
||||
|
||||
test("chatCompletionNonStream should send a valid request", async () => {
|
||||
await runAdapterTest({
|
||||
config: {
|
||||
provider: "anthropic",
|
||||
apiKey: "test-api-key",
|
||||
apiBase: "https://api.anthropic.com/v1/",
|
||||
},
|
||||
methodToTest: "chatCompletionNonStream",
|
||||
params: [
|
||||
{
|
||||
model: "claude-3-5-sonnet-20241022",
|
||||
messages: [{ role: "user", content: "hello" }],
|
||||
},
|
||||
new AbortController().signal,
|
||||
],
|
||||
expectedRequest: {
|
||||
url: "https://api.anthropic.com/v1/messages",
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
"Accept": "application/json",
|
||||
"anthropic-version": "2023-06-01",
|
||||
"x-api-key": "test-api-key",
|
||||
},
|
||||
body: {
|
||||
messages: [{ role: "user", content: "hello" }],
|
||||
system: undefined,
|
||||
model: "claude-3-5-sonnet-20241022",
|
||||
max_tokens: 4096,
|
||||
stream: undefined,
|
||||
},
|
||||
},
|
||||
mockResponse: {
|
||||
id: "msg_test",
|
||||
type: "message",
|
||||
role: "assistant",
|
||||
content: [
|
||||
{
|
||||
type: "text",
|
||||
text: "Hello! How can I help you today?",
|
||||
},
|
||||
],
|
||||
model: "claude-3-5-sonnet-20241022",
|
||||
stop_reason: "end_turn",
|
||||
stop_sequence: null,
|
||||
usage: {
|
||||
input_tokens: 10,
|
||||
output_tokens: 15,
|
||||
},
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
test("chatCompletionStream should send a valid request", async () => {
|
||||
await runAdapterTest({
|
||||
config: {
|
||||
provider: "anthropic",
|
||||
apiKey: "test-api-key",
|
||||
apiBase: "https://api.anthropic.com/v1/",
|
||||
},
|
||||
methodToTest: "chatCompletionStream",
|
||||
params: [
|
||||
{
|
||||
model: "claude-3-5-sonnet-20241022",
|
||||
messages: [{ role: "user", content: "hello" }],
|
||||
stream: true,
|
||||
},
|
||||
new AbortController().signal,
|
||||
],
|
||||
expectedRequest: {
|
||||
url: "https://api.anthropic.com/v1/messages",
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
"Accept": "application/json",
|
||||
"anthropic-version": "2023-06-01",
|
||||
"x-api-key": "test-api-key",
|
||||
},
|
||||
body: {
|
||||
messages: [{ role: "user", content: "hello" }],
|
||||
system: undefined,
|
||||
model: "claude-3-5-sonnet-20241022",
|
||||
max_tokens: 4096,
|
||||
stream: true,
|
||||
},
|
||||
},
|
||||
mockStream: [
|
||||
{
|
||||
type: "content_block_delta",
|
||||
delta: {
|
||||
type: "text_delta",
|
||||
text: "Hello",
|
||||
},
|
||||
},
|
||||
{
|
||||
type: "content_block_delta",
|
||||
delta: {
|
||||
type: "text_delta",
|
||||
text: " world",
|
||||
},
|
||||
},
|
||||
],
|
||||
});
|
||||
});
|
||||
|
||||
test("chatCompletionStream with system message should convert correctly", async () => {
|
||||
await runAdapterTest({
|
||||
config: {
|
||||
provider: "anthropic",
|
||||
apiKey: "test-api-key",
|
||||
apiBase: "https://api.anthropic.com/v1/",
|
||||
},
|
||||
methodToTest: "chatCompletionStream",
|
||||
params: [
|
||||
{
|
||||
model: "claude-3-5-sonnet-20241022",
|
||||
messages: [
|
||||
{ role: "system", content: "You are a helpful assistant." },
|
||||
{ role: "user", content: "hello" },
|
||||
],
|
||||
stream: true,
|
||||
},
|
||||
new AbortController().signal,
|
||||
],
|
||||
expectedRequest: {
|
||||
url: "https://api.anthropic.com/v1/messages",
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
"Accept": "application/json",
|
||||
"anthropic-version": "2023-06-01",
|
||||
"x-api-key": "test-api-key",
|
||||
},
|
||||
body: {
|
||||
messages: [{ role: "user", content: "hello" }],
|
||||
system: [
|
||||
{
|
||||
type: "text",
|
||||
text: "You are a helpful assistant.",
|
||||
cache_control: { type: "ephemeral" },
|
||||
},
|
||||
],
|
||||
model: "claude-3-5-sonnet-20241022",
|
||||
max_tokens: 4096,
|
||||
stream: true,
|
||||
},
|
||||
},
|
||||
mockStream: [
|
||||
{
|
||||
type: "content_block_delta",
|
||||
delta: {
|
||||
type: "text_delta",
|
||||
text: "Hello",
|
||||
},
|
||||
},
|
||||
],
|
||||
});
|
||||
});
|
||||
|
||||
test("embed should throw not implemented error", async () => {
|
||||
const { constructLlmApi } = await import("../index.js");
|
||||
const api = constructLlmApi({
|
||||
provider: "anthropic",
|
||||
apiKey: "test-api-key",
|
||||
});
|
||||
|
||||
await expect(
|
||||
api!.embed({
|
||||
model: "text-embedding-ada-002",
|
||||
input: ["Hello", "World"],
|
||||
})
|
||||
).rejects.toThrow("Method not implemented.");
|
||||
});
|
||||
});
|
||||
|
|
@ -1,6 +1,15 @@
|
|||
import { describe } from "vitest";
|
||||
import { describe, vi } from "vitest";
|
||||
import { createAdapterTests } from "./adapter-test-utils.js";
|
||||
|
||||
// Mock the fetch package (not needed for OpenAI but required by the shared test utils)
|
||||
vi.mock("@continuedev/fetch", async () => {
|
||||
const actual = await vi.importActual("@continuedev/fetch");
|
||||
return {
|
||||
...actual,
|
||||
fetchwithRequestOptions: vi.fn(),
|
||||
};
|
||||
});
|
||||
|
||||
describe("OpenAI Adapter Tests", () => {
|
||||
createAdapterTests({
|
||||
providerName: "openai",
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue