aboutsummaryrefslogtreecommitdiff
path: root/packages/tools/test/test-memory-tool.ts
blob: c0395a7914e530286791fa44253f330457354246 (plain) (blame)
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
190
191
192
193
194
195
196
197
198
#!/usr/bin/env bun
/**
 * Manual test script for Claude Memory Tool
 * Run with: bun run src/test-memory-tool.ts
 */

import { createClaudeMemoryTool, type MemoryCommand } from "./claude-memory"
import "dotenv/config"

async function testMemoryTool() {
	console.log("๐Ÿงช Testing Claude Memory Tool Operations")
	console.log("=".repeat(50))

	if (!process.env.SUPERMEMORY_API_KEY) {
		console.error("โŒ SUPERMEMORY_API_KEY environment variable is required")
		process.exit(1)
	}

	const memoryTool = createClaudeMemoryTool(process.env.SUPERMEMORY_API_KEY, {
		projectId: "memory-tool-test",
		memoryContainerTag: "claude_memory_test",
		baseUrl: process.env.SUPERMEMORY_BASE_URL,
	})

	const testCases: Array<{
		name: string
		command: MemoryCommand
		expectSuccess: boolean
	}> = [
		{
			name: "Check empty memory directory",
			command: { command: "view", path: "/memories/" },
			expectSuccess: true,
		},
		{
			name: "Create a project notes file",
			command: {
				command: "create",
				path: "/memories/project-notes.md",
				file_text:
					"# Project Notes\\n\\n## Meeting Notes\\n- Discussed requirements\\n- Set timeline\\n- Assigned tasks\\n\\n## Technical Stack\\n- Frontend: React\\n- Backend: Node.js\\n- Database: PostgreSQL",
			},
			expectSuccess: true,
		},
		{
			name: "Create a todo list",
			command: {
				command: "create",
				path: "/memories/todo.txt",
				file_text:
					"TODO List:\\n1. Set up development environment\\n2. Create database schema\\n3. Build authentication system\\n4. Implement user dashboard\\n5. Write documentation",
			},
			expectSuccess: true,
		},
		{
			name: "List directory contents (should show 2 files)",
			command: { command: "view", path: "/memories/" },
			expectSuccess: true,
		},
		{
			name: "Read project notes with line numbers",
			command: { command: "view", path: "/memories/project-notes.md" },
			expectSuccess: true,
		},
		{
			name: "Read specific lines from todo list",
			command: {
				command: "view",
				path: "/memories/todo.txt",
				view_range: [1, 3],
			},
			expectSuccess: true,
		},
		{
			name: "Replace text in project notes",
			command: {
				command: "str_replace",
				path: "/memories/project-notes.md",
				old_str: "Node.js",
				new_str: "Express.js",
			},
			expectSuccess: true,
		},
		{
			name: "Insert new item in todo list at line 3",
			command: {
				command: "insert",
				path: "/memories/todo.txt",
				insert_line: 3,
				insert_text: "2.5. Design database relationships",
			},
			expectSuccess: true,
		},
		{
			name: "Read updated todo list",
			command: { command: "view", path: "/memories/todo.txt" },
			expectSuccess: true,
		},
		{
			name: "Create a personal notes file",
			command: {
				command: "create",
				path: "/memories/personal/preferences.md",
				file_text:
					"# My Preferences\\n\\n- Prefers React over Vue\\n- Uses TypeScript for type safety\\n- Likes clean, readable code\\n- Prefers functional programming style",
			},
			expectSuccess: true,
		},
		{
			name: "List root directory (should show files and personal/ subdirectory)",
			command: { command: "view", path: "/memories/" },
			expectSuccess: true,
		},
		{
			name: "Rename project notes",
			command: {
				command: "rename",
				path: "/memories/project-notes.md",
				new_path: "/memories/project-meeting-notes.md",
			},
			expectSuccess: true,
		},
		{
			name: "List directory after rename",
			command: { command: "view", path: "/memories/" },
			expectSuccess: true,
		},
		{
			name: "Test invalid path (should fail)",
			command: { command: "view", path: "/etc/passwd" },
			expectSuccess: false,
		},
		{
			name: "Test file not found (should fail)",
			command: { command: "view", path: "/memories/nonexistent.txt" },
			expectSuccess: false,
		},
	]

	let passed = 0
	let failed = 0

	for (let i = 0; i < testCases.length; i++) {
		const testCase = testCases[i]
		console.log(`\\n๐Ÿ”„ Test ${i + 1}/${testCases.length}: ${testCase.name}`)

		try {
			const result = await memoryTool.handleCommand(testCase.command)

			if (result.success === testCase.expectSuccess) {
				console.log("โœ… PASS")
				if (result.content && result.content.length < 500) {
					console.log("๐Ÿ“„ Result:")
					console.log(result.content)
				} else if (result.content) {
					console.log(
						`๐Ÿ“„ Result: ${result.content.substring(0, 100)}... (truncated)`,
					)
				}
				passed++
			} else {
				console.log("โŒ FAIL")
				console.log(
					`Expected success: ${testCase.expectSuccess}, got: ${result.success}`,
				)
				if (result.error) {
					console.log(`Error: ${result.error}`)
				}
				failed++
			}
		} catch (error) {
			console.log("๐Ÿ’ฅ ERROR")
			console.log(`Exception: ${error}`)
			failed++
		}

		// Add a small delay to avoid rate limiting
		await new Promise((resolve) => setTimeout(resolve, 500))
	}

	console.log("\\n๐Ÿ“Š Test Results:")
	console.log(`โœ… Passed: ${passed}`)
	console.log(`โŒ Failed: ${failed}`)
	console.log(
		`๐Ÿ“ˆ Success Rate: ${((passed / (passed + failed)) * 100).toFixed(1)}%`,
	)

	if (failed === 0) {
		console.log(
			"\\n๐ŸŽ‰ All tests passed! Claude Memory Tool is working perfectly!",
		)
	} else {
		console.log(`\\nโš ๏ธ  ${failed} test(s) failed. Check the errors above.`)
	}
}

// Run the tests
testMemoryTool().catch(console.error)