xonecas.io
5875
Os

ollama-space-molt-player / files

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

10.3 KB
1import { afterEach, beforeEach, describe, expect, mock, test } from "bun:test";
2import type { Server } from "bun";
3import { GameClient } from "../src/game-client.ts";
4import { GameDatabase } from "../src/db.ts";
5import { unlink, exists } from "node:fs/promises";
6
7describe("GameClient agent loop", () => {
8	let ollamaServer: Server<unknown> | null = null;
9	let mcpServer: Server<unknown> | null = null;
10	let db: GameDatabase | null = null;
11	const testInstanceId = "test-agent-loop";
12	const dbPath = `data/${testInstanceId}.db`;
13
14	beforeEach(async () => {
15		if (await exists(dbPath)) {
16			await unlink(dbPath);
17		}
18	});
19
20	afterEach(async () => {
21		ollamaServer?.stop();
22		ollamaServer = null;
23		mcpServer?.stop();
24		mcpServer = null;
25		db?.close();
26		db = null;
27
28		if (await exists(dbPath)) {
29			await unlink(dbPath);
30		}
31	});
32
33	test("agent loop should continue after text-only LLM response", async () => {
34		const llmResponses: Array<{ content: string; tool_calls?: unknown[] }> = [];
35		let llmCallCount = 0;
36		const maxRounds = 3;
37
38		ollamaServer = Bun.serve({
39			port: 0,
40			fetch: async (req) => {
41				llmCallCount++;
42
43				if (llmCallCount === 1) {
44					const response = {
45						model: "test",
46						created_at: new Date().toISOString(),
47						message: {
48							role: "assistant",
49							content: "",
50							tool_calls: [
51								{
52									function: {
53										name: "register",
54										arguments: { username: "TestBot", empire: "solarian" },
55									},
56								},
57							],
58						},
59						done: true,
60					};
61					llmResponses.push(response.message);
62					return Response.json(response);
63				}
64
65				const response = {
66					model: "test",
67					created_at: new Date().toISOString(),
68					message: {
69						role: "assistant",
70						content: `Round ${llmCallCount} response - continuing play`,
71					},
72					done: true,
73				};
74				llmResponses.push(response.message);
75				return Response.json(response);
76			},
77		});
78
79		mcpServer = Bun.serve({
80			port: 0,
81			fetch: async (req) => {
82				const body = await req.json();
83
84				if (body.method === "initialize") {
85					return Response.json({
86						jsonrpc: "2.0",
87						id: body.id,
88						result: {
89							protocolVersion: "2024-11-05",
90							capabilities: { tools: {} },
91							serverInfo: { name: "test-mcp", version: "1.0.0" },
92						},
93					});
94				}
95
96				if (body.method === "tools/list") {
97					return Response.json({
98						jsonrpc: "2.0",
99						id: body.id,
100						result: {
101							tools: [
102								{
103									name: "register",
104									description: "Register a new player",
105									inputSchema: {
106										type: "object",
107										properties: {
108											username: { type: "string" },
109											empire: { type: "string" },
110										},
111										required: ["username", "empire"],
112									},
113								},
114							],
115						},
116					});
117				}
118
119				if (body.method === "tools/call") {
120					return Response.json({
121						jsonrpc: "2.0",
122						id: body.id,
123						result: {
124							content: [{ type: "text", text: JSON.stringify({ error: "username_taken" }) }],
125							isError: true,
126						},
127					});
128				}
129
130				return Response.json({ jsonrpc: "2.0", id: body.id, result: {} });
131			},
132		});
133
134		db = new GameDatabase(testInstanceId);
135		const client = new GameClient(db, {
136			instanceId: testInstanceId,
137			model: "qwen3-8b",
138			verbose: false,
139			tickRate: 20000,
140			ollamaTimeout: 5000,
141			serverUrl: `http://localhost:${mcpServer.port}`,
142		});
143
144		client.ollama.updateConfig({ baseUrl: `http://localhost:${ollamaServer.port}` });
145
146		const startPromise = client.start();
147
148		await new Promise((resolve) => setTimeout(resolve, 3500));
149
150		await client.stop();
151
152		expect(llmCallCount).toBeGreaterThanOrEqual(maxRounds);
153
154		const textOnlyResponses = llmResponses.filter(
155			(r) => !r.tool_calls || r.tool_calls.length === 0,
156		);
157		expect(textOnlyResponses.length).toBeGreaterThanOrEqual(2);
158	});
159
160	test("agent loop should log round start for visibility", async () => {
161		const logs: string[] = [];
162		const originalLog = console.log;
163		console.log = (...args: unknown[]) => {
164			logs.push(args.join(" "));
165			originalLog(...args);
166		};
167
168		let llmCallCount = 0;
169
170		ollamaServer = Bun.serve({
171			port: 0,
172			fetch: async () => {
173				llmCallCount++;
174				return Response.json({
175					model: "test",
176					created_at: new Date().toISOString(),
177					message: {
178						role: "assistant",
179						content: `Response ${llmCallCount}`,
180					},
181					done: true,
182				});
183			},
184		});
185
186		mcpServer = Bun.serve({
187			port: 0,
188			fetch: async (req) => {
189				const body = await req.json();
190				if (body.method === "initialize") {
191					return Response.json({
192						jsonrpc: "2.0",
193						id: body.id,
194						result: {
195							protocolVersion: "2024-11-05",
196							capabilities: { tools: {} },
197							serverInfo: { name: "test-mcp", version: "1.0.0" },
198						},
199					});
200				}
201				if (body.method === "tools/list") {
202					return Response.json({
203						jsonrpc: "2.0",
204						id: body.id,
205						result: { tools: [] },
206					});
207				}
208				return Response.json({ jsonrpc: "2.0", id: body.id, result: {} });
209			},
210		});
211
212		db = new GameDatabase(testInstanceId);
213		const client = new GameClient(db, {
214			instanceId: testInstanceId,
215			model: "qwen3-8b",
216			verbose: false,
217			tickRate: 20000,
218			ollamaTimeout: 5000,
219			serverUrl: `http://localhost:${mcpServer.port}`,
220		});
221
222		client.ollama.updateConfig({ baseUrl: `http://localhost:${ollamaServer.port}` });
223
224		const startPromise = client.start();
225		await new Promise((resolve) => setTimeout(resolve, 2500));
226		await client.stop();
227
228		console.log = originalLog;
229
230		expect(llmCallCount).toBeGreaterThanOrEqual(2);
231
232		expect(logs.some((l) => l.includes("Starting agent loop"))).toBe(true);
233	});
234
235	test("agent loop should handle LLM timeout gracefully", { timeout: 15000 }, async () => {
236		const errors: string[] = [];
237		const originalError = console.error;
238		console.error = (...args: unknown[]) => {
239			errors.push(args.join(" "));
240			originalError(...args);
241		};
242
243		let requestCount = 0;
244
245		ollamaServer = Bun.serve({
246			port: 0,
247			fetch: async () => {
248				requestCount++;
249
250				if (requestCount === 1) {
251					await new Promise((resolve) => setTimeout(resolve, 2000));
252				}
253				return Response.json({
254					model: "test",
255					created_at: new Date().toISOString(),
256					message: { role: "assistant", content: "response" },
257					done: true,
258				});
259			},
260		});
261
262		mcpServer = Bun.serve({
263			port: 0,
264			fetch: async (req) => {
265				const body = await req.json();
266				if (body.method === "initialize") {
267					return Response.json({
268						jsonrpc: "2.0",
269						id: body.id,
270						result: {
271							protocolVersion: "2024-11-05",
272							capabilities: { tools: {} },
273							serverInfo: { name: "test-mcp", version: "1.0.0" },
274						},
275					});
276				}
277				if (body.method === "tools/list") {
278					return Response.json({
279						jsonrpc: "2.0",
280						id: body.id,
281						result: { tools: [] },
282					});
283				}
284				return Response.json({ jsonrpc: "2.0", id: body.id, result: {} });
285			},
286		});
287
288		db = new GameDatabase(testInstanceId);
289		const client = new GameClient(db, {
290			instanceId: testInstanceId,
291			model: "qwen3-8b",
292			verbose: false,
293			tickRate: 20000,
294			ollamaTimeout: 500,
295			serverUrl: `http://localhost:${mcpServer.port}`,
296		});
297
298		client.ollama.updateConfig({ baseUrl: `http://localhost:${ollamaServer.port}` });
299
300		const startPromise = client.start();
301
302		await new Promise((resolve) => setTimeout(resolve, 7000));
303		await client.stop();
304
305		console.error = originalError;
306
307		expect(errors.some((e) => e.includes("Agent loop error") || e.includes("timed out"))).toBe(
308			true,
309		);
310
311		expect(requestCount).toBeGreaterThan(1);
312	});
313
314	test("agent loop should nudge LLM when it returns empty response", async () => {
315		const logs: string[] = [];
316		const originalLog = console.log;
317		console.log = (...args: unknown[]) => {
318			logs.push(args.join(" "));
319			originalLog(...args);
320		};
321
322		let llmCallCount = 0;
323		let lastRequestMessages: unknown[] = [];
324
325		ollamaServer = Bun.serve({
326			port: 0,
327			fetch: async (req) => {
328				llmCallCount++;
329				const body = await req.json();
330				lastRequestMessages = body.messages || [];
331
332				if (llmCallCount <= 2) {
333					return Response.json({
334						model: "test",
335						created_at: new Date().toISOString(),
336						message: {
337							role: "assistant",
338							content: "",
339						},
340						done: true,
341					});
342				}
343
344				return Response.json({
345					model: "test",
346					created_at: new Date().toISOString(),
347					message: {
348						role: "assistant",
349						content: "Now taking action!",
350						tool_calls: [
351							{
352								function: {
353									name: "get_status",
354									arguments: {},
355								},
356							},
357						],
358					},
359					done: true,
360				});
361			},
362		});
363
364		mcpServer = Bun.serve({
365			port: 0,
366			fetch: async (req) => {
367				const body = await req.json();
368				if (body.method === "initialize") {
369					return Response.json({
370						jsonrpc: "2.0",
371						id: body.id,
372						result: {
373							protocolVersion: "2024-11-05",
374							capabilities: { tools: {} },
375							serverInfo: { name: "test-mcp", version: "1.0.0" },
376						},
377					});
378				}
379				if (body.method === "tools/list") {
380					return Response.json({
381						jsonrpc: "2.0",
382						id: body.id,
383						result: {
384							tools: [
385								{
386									name: "get_status",
387									description: "Get player status",
388									inputSchema: { type: "object", properties: {}, required: [] },
389								},
390							],
391						},
392					});
393				}
394				if (body.method === "tools/call") {
395					return Response.json({
396						jsonrpc: "2.0",
397						id: body.id,
398						result: {
399							content: [{ type: "text", text: JSON.stringify({ status: "ok" }) }],
400						},
401					});
402				}
403				return Response.json({ jsonrpc: "2.0", id: body.id, result: {} });
404			},
405		});
406
407		db = new GameDatabase(testInstanceId);
408		const client = new GameClient(db, {
409			instanceId: testInstanceId,
410			model: "qwen3-8b",
411			verbose: false,
412			tickRate: 20000,
413			ollamaTimeout: 5000,
414			serverUrl: `http://localhost:${mcpServer.port}`,
415		});
416
417		client.ollama.updateConfig({ baseUrl: `http://localhost:${ollamaServer.port}` });
418
419		const startPromise = client.start();
420		await new Promise((resolve) => setTimeout(resolve, 2500));
421		await client.stop();
422
423		console.log = originalLog;
424
425		expect(llmCallCount).toBeGreaterThanOrEqual(3);
426
427		expect(logs.some((l) => l.includes("empty response") || l.includes("nudging"))).toBe(true);
428
429		const hasNudgeMessage = lastRequestMessages.some(
430			(msg: any) =>
431				msg.role === "user" &&
432				typeof msg.content === "string" &&
433				msg.content.includes("didn't take any action"),
434		);
435		expect(hasNudgeMessage).toBe(true);
436	});
437});