forked from Lilly-Protocol/agentlily-runtime
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathopenai-compatible-provider.test.ts
More file actions
189 lines (166 loc) · 5.25 KB
/
Copy pathopenai-compatible-provider.test.ts
File metadata and controls
189 lines (166 loc) · 5.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
import { afterEach, describe, expect, it, vi } from "vitest";
import {
OpenAICompatibleModelProvider,
type ModelPrompt
} from "../src/index.js";
describe("OpenAICompatibleModelProvider", () => {
afterEach(() => {
vi.restoreAllMocks();
});
it("validates configuration and throws on missing apiKey", () => {
expect(
() => new OpenAICompatibleModelProvider({ apiKey: "" })
).toThrowError("OpenAI-compatible provider requires a non-empty apiKey.");
expect(
() =>
new OpenAICompatibleModelProvider({
apiKey: " "
})
).toThrowError("OpenAI-compatible provider requires a non-empty apiKey.");
});
it("validates configuration and throws on invalid baseUrl", () => {
expect(
() =>
new OpenAICompatibleModelProvider({
apiKey: "valid-key",
baseUrl: "not-a-valid-url"
})
).toThrowError(
'Invalid baseUrl provided to OpenAICompatibleModelProvider: "not-a-valid-url".'
);
});
it("initializes with default options", () => {
const provider = new OpenAICompatibleModelProvider({
apiKey: "sk-test-key"
});
expect(provider.name).toBe("openai-compatible");
expect(provider.getBaseUrl()).toBe("https://api.openai.com/v1");
expect(provider.getModel()).toBe("gpt-4o-mini");
});
it("generates model response with mocked HTTP success", async () => {
const mockResponsePayload = {
id: "chatcmpl-test",
model: "gpt-4o-mini",
choices: [
{
index: 0,
message: {
role: "assistant",
content: "Autonomous payment prepared successfully."
},
finish_reason: "stop"
}
],
usage: {
prompt_tokens: 15,
completion_tokens: 8,
total_tokens: 23
}
};
const fetchSpy = vi.spyOn(globalThis, "fetch").mockResolvedValueOnce(
new Response(JSON.stringify(mockResponsePayload), {
status: 200,
headers: { "Content-Type": "application/json" }
})
);
const provider = new OpenAICompatibleModelProvider({
apiKey: "sk-mock-key",
model: "gpt-4o-mini"
});
const prompt: ModelPrompt = {
instructions: "You are an autonomous Stellar agent.",
input: "Prepare 10 XLM payment to recipient."
};
const response = await provider.generate(prompt);
expect(fetchSpy).toHaveBeenCalledTimes(1);
expect(fetchSpy).toHaveBeenCalledWith(
"https://api.openai.com/v1/chat/completions",
expect.objectContaining({
method: "POST",
headers: expect.objectContaining({
"Content-Type": "application/json",
Authorization: "Bearer sk-mock-key"
}),
body: JSON.stringify({
model: "gpt-4o-mini",
messages: [
{ role: "system", content: prompt.instructions },
{ role: "user", content: prompt.input }
]
})
})
);
expect(response.outputText).toBe(
"Autonomous payment prepared successfully."
);
expect(response.metadata).toEqual({
model: "gpt-4o-mini",
finishReason: "stop",
usage: {
prompt_tokens: 15,
completion_tokens: 8,
total_tokens: 23
}
});
});
it("supports custom baseUrl and custom headers", async () => {
const fetchSpy = vi.spyOn(globalThis, "fetch").mockResolvedValueOnce(
new Response(
JSON.stringify({
model: "custom-llm",
choices: [{ message: { content: "Custom response" } }]
}),
{ status: 200, headers: { "Content-Type": "application/json" } }
)
);
const provider = new OpenAICompatibleModelProvider({
apiKey: "custom-token",
baseUrl: "https://custom-llm.example.com/v1/",
model: "custom-llm",
headers: { "X-Custom-Header": "custom-val" }
});
const response = await provider.generate({
instructions: "Instructions",
input: "Input"
});
expect(fetchSpy).toHaveBeenCalledWith(
"https://custom-llm.example.com/v1/chat/completions",
expect.objectContaining({
headers: expect.objectContaining({
"X-Custom-Header": "custom-val",
Authorization: "Bearer custom-token"
})
})
);
expect(response.outputText).toBe("Custom response");
});
it("handles non-2xx HTTP responses with descriptive error", async () => {
vi.spyOn(globalThis, "fetch").mockResolvedValueOnce(
new Response(JSON.stringify({ error: "Invalid API key" }), {
status: 401,
statusText: "Unauthorized"
})
);
const provider = new OpenAICompatibleModelProvider({
apiKey: "invalid-key"
});
await expect(
provider.generate({ instructions: "test", input: "test" })
).rejects.toThrowError(
'OpenAI-compatible provider returned HTTP 401: {"error":"Invalid API key"}'
);
});
it("handles network failure gracefully", async () => {
vi.spyOn(globalThis, "fetch").mockRejectedValueOnce(
new Error("ECONNREFUSED")
);
const provider = new OpenAICompatibleModelProvider({
apiKey: "valid-key"
});
await expect(
provider.generate({ instructions: "test", input: "test" })
).rejects.toThrowError(
"OpenAI-compatible provider request failed: ECONNREFUSED"
);
});
});