-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathagent_test.ts
More file actions
116 lines (102 loc) Β· 2.97 KB
/
agent_test.ts
File metadata and controls
116 lines (102 loc) Β· 2.97 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
import { assertEquals } from "@std/assert";
import { runAgent } from "./agent.ts";
import type { Anthropic } from "npm:@anthropic-ai/sdk";
class MockAi {
messages = {
create(params: { messages: Array<{ content: string }> }) {
const lastMessage = params.messages[params.messages.length - 1];
const userInput = lastMessage.content;
if (typeof userInput === "string" && userInput.includes("list")) {
return {
content: [{
type: "tool_use",
id: "tool_123",
name: "list_notes",
input: {},
}],
};
}
if (typeof userInput === "string" && userInput.includes("create")) {
return {
content: [{
type: "tool_use",
id: "tool_456",
name: "create_note",
input: { title: "New Note", content: "# New Content" },
}],
};
}
return {
content: [{
type: "text",
text: "I can help you with HackMD notes.",
}],
};
},
};
}
Deno.test("runAgent should handle tool execution", async () => {
const mockAI = new MockAi() as unknown as Anthropic;
const testTools = [
{
name: "list_notes",
description: "List notes",
input_schema: { type: "object" as const, properties: {} },
call() {
return Promise.resolve("- Test Note 1\n- Test Note 2");
},
},
];
// mock prompt
let promptCallCount = 0;
const originalPrompt = globalThis.prompt;
globalThis.prompt = (_message?: string) => {
promptCallCount++;
if (promptCallCount === 1) {
return "list my notes";
}
return null;
};
// mock console.log
const logs: string[] = [];
const originalLog = console.log;
console.log = (...args: unknown[]) => {
logs.push(args.join(" "));
};
try {
await runAgent(mockAI, testTools);
assertEquals(promptCallCount, 2); // one input + one exit
const toolUsageLogs = logs.filter((log) => log.includes("π§ Using:"));
assertEquals(toolUsageLogs.length, 1);
assertEquals(toolUsageLogs[0], "π§ Using: list_notes...");
} finally {
globalThis.prompt = originalPrompt;
console.log = originalLog;
}
});
Deno.test("runAgent should handle text responses", async () => {
const mockAI = new MockAi() as unknown as Anthropic;
let promptCallCount = 0;
const originalPrompt = globalThis.prompt;
globalThis.prompt = (_message?: string) => {
promptCallCount++;
if (promptCallCount === 1) {
return "hello";
}
return null;
};
const logs: string[] = [];
const originalLog = console.log;
console.log = (...args: unknown[]) => {
logs.push(args.join(" "));
};
try {
await runAgent(mockAI, []);
const textLogs = logs.filter((log) => log.includes("π€:"));
assertEquals(textLogs.length, 1);
assertEquals(textLogs[0], "π€: I can help you with HackMD notes.");
} finally {
globalThis.prompt = originalPrompt;
console.log = originalLog;
}
});