xonecas.io
5875
Os

ollama-space-molt-player / files

ollama-space-molt-player / packages / client / tests / ollama.test.ts

5.9 KB
1import { afterEach, describe, expect, test } from "bun:test";
2import type { Server } from "bun";
3import { OllamaClient, type ChatMessage } from "../src/ollama.ts";
4
5describe("OllamaClient", () => {
6	let client: OllamaClient;
7	let mockServer: Server<unknown> | null = null;
8
9	afterEach(() => {
10		mockServer?.stop();
11		mockServer = null;
12	});
13
14	test("should create client with config", () => {
15		client = new OllamaClient({
16			baseUrl: "http://localhost:11434",
17			model: "qwen3:8b",
18			options: { temperature: 0.7 },
19			timeout: 8000,
20		});
21		const config = client.getConfig();
22
23		expect(config.baseUrl).toBe("http://localhost:11434");
24		expect(config.model).toBe("qwen3:8b");
25		expect(config.options).toEqual({ temperature: 0.7 });
26		expect(config.timeout).toBe(8000);
27	});
28
29	test("should update config", () => {
30		client = new OllamaClient({
31			baseUrl: "http://localhost:11434",
32			model: "old-model",
33			options: { temperature: 1.2 },
34			timeout: 8000,
35		});
36		client.updateConfig({ model: "new-model", options: { temperature: 0.8 } });
37
38		const config = client.getConfig();
39		expect(config.model).toBe("new-model");
40		expect(config.options).toEqual({ temperature: 0.8 });
41	});
42
43	test("should make successful chat request", async () => {
44		mockServer = Bun.serve({
45			port: 0,
46			fetch: async (req) => {
47				const body = await req.json();
48				expect(body.messages).toBeDefined();
49				return Response.json({
50					model: body.model,
51					created_at: new Date().toISOString(),
52					message: {
53						role: "assistant",
54						content: "test response",
55					},
56					done: true,
57				});
58			},
59		});
60
61		client = new OllamaClient({
62			baseUrl: `http://localhost:${mockServer.port}`,
63			model: "test-model",
64			options: {},
65			timeout: 8000,
66		});
67
68		const messages: ChatMessage[] = [{ role: "user", content: "test prompt" }];
69		const result = await client.chat(messages);
70		expect(result.content).toBe("test response");
71		expect(result.toolCalls).toEqual([]);
72		expect(result.thinking).toBeUndefined();
73	});
74
75	test("should handle tool calls in response", async () => {
76		mockServer = Bun.serve({
77			port: 0,
78			fetch: async () => {
79				return Response.json({
80					model: "test",
81					created_at: new Date().toISOString(),
82					message: {
83						role: "assistant",
84						content: "",
85						tool_calls: [
86							{
87								function: {
88									name: "get_status",
89									arguments: {},
90								},
91							},
92						],
93					},
94					done: true,
95				});
96			},
97		});
98
99		client = new OllamaClient({
100			baseUrl: `http://localhost:${mockServer.port}`,
101			model: "test-model",
102			options: {},
103			timeout: 8000,
104		});
105
106		const messages: ChatMessage[] = [{ role: "user", content: "check status" }];
107		const result = await client.chat(messages);
108
109		expect(result.toolCalls.length).toBe(1);
110		expect(result.toolCalls[0].function.name).toBe("get_status");
111	});
112
113	test("should pass tools to request", async () => {
114		let capturedRequest: { tools?: unknown[] } | undefined;
115
116		mockServer = Bun.serve({
117			port: 0,
118			fetch: async (req) => {
119				capturedRequest = await req.json();
120				return Response.json({
121					model: "test",
122					created_at: new Date().toISOString(),
123					message: { role: "assistant", content: "ok" },
124					done: true,
125				});
126			},
127		});
128
129		client = new OllamaClient({
130			baseUrl: `http://localhost:${mockServer.port}`,
131			model: "test-model",
132			options: {},
133			timeout: 8000,
134		});
135
136		const tools = [
137			{
138				type: "function" as const,
139				function: {
140					name: "test_tool",
141					description: "A test tool",
142					parameters: { type: "object", properties: {}, required: [] },
143				},
144			},
145		];
146
147		await client.chat([{ role: "user", content: "test" }], tools);
148
149		expect(capturedRequest?.tools).toBeDefined();
150		expect(capturedRequest?.tools?.length).toBe(1);
151	});
152
153	test("should handle timeout", async () => {
154		mockServer = Bun.serve({
155			port: 0,
156			fetch: async () => {
157				await new Promise((resolve) => setTimeout(resolve, 200));
158				return Response.json({ message: { content: "late" } });
159			},
160		});
161
162		client = new OllamaClient({
163			baseUrl: `http://localhost:${mockServer.port}`,
164			model: "test-model",
165			options: {},
166			timeout: 100,
167		});
168
169		await expect(client.chat([{ role: "user", content: "test" }])).rejects.toThrow(/timed out/);
170	});
171
172	test("should handle HTTP errors", async () => {
173		mockServer = Bun.serve({
174			port: 0,
175			fetch: () => {
176				return new Response("Server error", { status: 500 });
177			},
178		});
179
180		client = new OllamaClient({
181			baseUrl: `http://localhost:${mockServer.port}`,
182			model: "test-model",
183			options: {},
184			timeout: 8000,
185		});
186
187		await expect(client.chat([{ role: "user", content: "test" }])).rejects.toThrow(/status 500/);
188	});
189
190	test("should extract thinking from response content", async () => {
191		mockServer = Bun.serve({
192			port: 0,
193			fetch: async () => {
194				return Response.json({
195					model: "test",
196					created_at: new Date().toISOString(),
197					message: {
198						role: "assistant",
199						content: "<think>reasoning here</think>\nfinal answer",
200					},
201					done: true,
202				});
203			},
204		});
205
206		client = new OllamaClient({
207			baseUrl: `http://localhost:${mockServer.port}`,
208			model: "test-model",
209			options: {},
210			timeout: 8000,
211		});
212
213		const result = await client.chat([{ role: "user", content: "test" }]);
214		expect(result.content).toBe("final answer");
215		expect(result.thinking).toBe("reasoning here");
216	});
217
218	test("should handle empty content", async () => {
219		mockServer = Bun.serve({
220			port: 0,
221			fetch: async () => {
222				return Response.json({
223					model: "test",
224					created_at: new Date().toISOString(),
225					message: {
226						role: "assistant",
227						content: "",
228						tool_calls: [{ function: { name: "mine", arguments: {} } }],
229					},
230					done: true,
231				});
232			},
233		});
234
235		client = new OllamaClient({
236			baseUrl: `http://localhost:${mockServer.port}`,
237			model: "test-model",
238			options: {},
239			timeout: 8000,
240		});
241
242		const result = await client.chat([{ role: "user", content: "test" }]);
243		expect(result.content).toBe("");
244		expect(result.toolCalls.length).toBe(1);
245	});
246});