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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
|
"""Tests for tools module."""
import os
from dotenv import load_dotenv
import pytest
import json
from typing import List
from openai.types.chat import ChatCompletionMessageToolCall
load_dotenv()
# Import from the installed package or src directly
try:
# Try importing from the installed package first
from supermemory_openai_sdk import (
SupermemoryTools,
SupermemoryToolsConfig,
create_supermemory_tools,
get_memory_tool_definitions,
execute_memory_tool_calls,
create_search_memories_tool,
create_add_memory_tool,
)
except ImportError:
# Fallback to importing from src directory
import sys
import os
# Add src directory to path
sys.path.insert(0, os.path.join(os.path.dirname(os.path.dirname(__file__)), "src"))
from tools import (
SupermemoryTools,
SupermemoryToolsConfig,
create_supermemory_tools,
get_memory_tool_definitions,
execute_memory_tool_calls,
create_search_memories_tool,
create_add_memory_tool,
)
# These classes don't exist in the current codebase - commenting out for now
# SupermemoryOpenAI,
# SupermemoryInfiniteChatConfigWithProviderName,
@pytest.fixture
def test_api_key() -> str:
"""Get test Supermemory API key from environment."""
api_key = os.getenv("SUPERMEMORY_API_KEY")
if not api_key:
pytest.skip("SUPERMEMORY_API_KEY environment variable is required for tests")
return api_key
@pytest.fixture
def test_provider_api_key() -> str:
"""Get test provider API key from environment."""
api_key = os.getenv("PROVIDER_API_KEY")
if not api_key:
pytest.skip("PROVIDER_API_KEY environment variable is required for tests")
return api_key
@pytest.fixture
def test_base_url() -> str:
"""Get test base URL from environment."""
return os.getenv("SUPERMEMORY_BASE_URL", "")
@pytest.fixture
def test_model_name() -> str:
"""Get test model name from environment."""
return os.getenv("MODEL_NAME", "gpt-5-nano")
class TestToolInitialization:
"""Test tool initialization."""
def test_create_tools_with_default_configuration(self, test_api_key: str):
"""Test creating tools with default configuration."""
config: SupermemoryToolsConfig = {}
tools = SupermemoryTools(test_api_key, config)
assert tools is not None
assert tools.get_tool_definitions() is not None
assert (
len(tools.get_tool_definitions()) == 2
) # Currently has search_memories and add_memory
def test_create_tools_with_helper(self, test_api_key: str):
"""Test creating tools with createSupermemoryTools helper."""
tools = create_supermemory_tools(
test_api_key,
{
"project_id": "test-project",
},
)
assert tools is not None
assert tools.get_tool_definitions() is not None
def test_create_tools_with_custom_base_url(
self, test_api_key: str, test_base_url: str
):
"""Test creating tools with custom baseUrl."""
if not test_base_url:
pytest.skip("SUPERMEMORY_BASE_URL not provided")
config: SupermemoryToolsConfig = {
"base_url": test_base_url,
}
tools = SupermemoryTools(test_api_key, config)
assert tools is not None
assert (
len(tools.get_tool_definitions()) == 2
) # Currently has search_memories and add_memory
def test_create_tools_with_project_id(self, test_api_key: str):
"""Test creating tools with projectId configuration."""
config: SupermemoryToolsConfig = {
"project_id": "test-project-123",
}
tools = SupermemoryTools(test_api_key, config)
assert tools is not None
assert (
len(tools.get_tool_definitions()) == 2
) # Currently has search_memories and add_memory
def test_create_tools_with_custom_container_tags(self, test_api_key: str):
"""Test creating tools with custom container tags."""
config: SupermemoryToolsConfig = {
"container_tags": ["custom-tag-1", "custom-tag-2"],
}
tools = SupermemoryTools(test_api_key, config)
assert tools is not None
assert (
len(tools.get_tool_definitions()) == 2
) # Currently has search_memories and add_memory
class TestToolDefinitions:
"""Test tool definitions."""
def test_return_proper_openai_function_definitions(self):
"""Test returning proper OpenAI function definitions."""
definitions = get_memory_tool_definitions()
assert definitions is not None
assert len(definitions) == 2 # Currently has search_memories and add_memory
# Check searchMemories
search_tool = next(
(d for d in definitions if d["function"]["name"] == "search_memories"), None
)
assert search_tool is not None
assert search_tool["type"] == "function"
assert "information_to_get" in search_tool["function"]["parameters"]["required"]
# Check addMemory
add_tool = next(
(d for d in definitions if d["function"]["name"] == "add_memory"), None
)
assert add_tool is not None
assert add_tool["type"] == "function"
assert "memory" in add_tool["function"]["parameters"]["required"]
def test_consistent_tool_definitions_from_class_and_helper(self, test_api_key: str):
"""Test that tool definitions are consistent between class and helper."""
tools = SupermemoryTools(test_api_key)
class_definitions = tools.get_tool_definitions()
helper_definitions = get_memory_tool_definitions()
assert class_definitions == helper_definitions
class TestMemoryOperations:
"""Test memory operations."""
@pytest.mark.asyncio
async def test_search_memories(self, test_api_key: str, test_base_url: str):
"""Test searching memories."""
config: SupermemoryToolsConfig = {
"project_id": "test-search",
}
if test_base_url:
config["base_url"] = test_base_url
tools = SupermemoryTools(test_api_key, config)
result = await tools.search_memories(
information_to_get="test preferences",
limit=5,
)
assert result is not None
assert "success" in result
assert isinstance(result["success"], bool)
if result["success"]:
assert "results" in result
assert "count" in result
assert isinstance(result["count"], int)
else:
assert "error" in result
@pytest.mark.asyncio
async def test_add_memory(self, test_api_key: str, test_base_url: str):
"""Test adding memory."""
config: SupermemoryToolsConfig = {
"container_tags": ["test-add-memory"],
}
if test_base_url:
config["base_url"] = test_base_url
tools = SupermemoryTools(test_api_key, config)
result = await tools.add_memory(
memory="User prefers dark roast coffee in the morning - test memory"
)
assert result is not None
assert "success" in result
assert isinstance(result["success"], bool)
if result["success"]:
assert "memory" in result
else:
assert "error" in result
class TestIndividualToolCreators:
"""Test individual tool creators."""
def test_create_individual_search_tool(self, test_api_key: str):
"""Test creating individual search tool."""
search_tool = create_search_memories_tool(
test_api_key,
{
"project_id": "test-individual",
},
)
assert search_tool is not None
assert search_tool.definition is not None
assert callable(search_tool.execute)
assert search_tool.definition["function"]["name"] == "search_memories"
def test_create_individual_add_tool(self, test_api_key: str):
"""Test creating individual add tool."""
add_tool = create_add_memory_tool(
test_api_key,
{
"project_id": "test-individual",
},
)
assert add_tool is not None
assert add_tool.definition is not None
assert callable(add_tool.execute)
assert add_tool.definition["function"]["name"] == "add_memory"
class TestOpenAIIntegration:
"""Test OpenAI integration."""
def test_placeholder(self):
"""Placeholder test for OpenAI integration."""
# TODO: Implement proper OpenAI integration tests when
# SupermemoryOpenAI and SupermemoryInfiniteChatConfigWithProviderName classes are available
assert True
# TODO: Uncomment this test when SupermemoryOpenAI and
# SupermemoryInfiniteChatConfigWithProviderName classes are implemented
# @pytest.mark.asyncio
# async def test_work_with_supermemory_openai_for_function_calling(
# self,
# test_api_key: str,
# test_provider_api_key: str,
# test_model_name: str,
# test_base_url: str,
# ):
# """Test working with SupermemoryOpenAI for function calling."""
# client = SupermemoryOpenAI(
# test_api_key,
# SupermemoryInfiniteChatConfigWithProviderName(
# provider_name="openai",
# provider_api_key=test_provider_api_key,
# ),
# )
# tools_config: SupermemoryToolsConfig = {
# "project_id": "test-openai-integration",
# }
# if test_base_url:
# tools_config["base_url"] = test_base_url
# tools = SupermemoryTools(test_api_key, tools_config)
# response = await client.chat_completion(
# messages=[
# {
# "role": "system",
# "content": (
# "You are a helpful assistant with access to user memories. "
# "When the user asks you to remember something, use the add_memory tool."
# ),
# },
# {
# "role": "user",
# "content": "Please remember that I prefer tea over coffee",
# },
# ],
# model=test_model_name,
# tools=tools.get_tool_definitions(),
# )
# assert response is not None
# assert hasattr(response, "choices")
# choice = response.choices[0]
# assert choice.message is not None
# # If the model decided to use function calling, test the execution
# if hasattr(choice.message, "tool_calls") and choice.message.tool_calls:
# tool_results = await execute_memory_tool_calls(
# test_api_key,
# choice.message.tool_calls,
# tools_config,
# )
# assert tool_results is not None
# assert len(tool_results) == len(choice.message.tool_calls)
# for result in tool_results:
# assert result["role"] == "tool"
# assert "content" in result
# assert "tool_call_id" in result
@pytest.mark.asyncio
async def test_handle_multiple_tool_calls(
self, test_api_key: str, test_base_url: str
):
"""Test handling multiple tool calls."""
tools_config: SupermemoryToolsConfig = {
"container_tags": ["test-multi-tools"],
}
if test_base_url:
tools_config["base_url"] = test_base_url
# Simulate tool calls (normally these would come from OpenAI)
mock_tool_calls: List[ChatCompletionMessageToolCall] = [
ChatCompletionMessageToolCall(
id="call_1",
type="function",
function={
"name": "search_memories",
"arguments": json.dumps({"information_to_get": "preferences"}),
},
),
ChatCompletionMessageToolCall(
id="call_2",
type="function",
function={
"name": "add_memory",
"arguments": json.dumps(
{"memory": "Test memory for multiple calls"}
),
},
),
]
results = await execute_memory_tool_calls(
test_api_key, mock_tool_calls, tools_config
)
assert results is not None
assert len(results) == 2
assert results[0]["tool_call_id"] == "call_1"
assert results[1]["tool_call_id"] == "call_2"
for result in results:
assert result["role"] == "tool"
assert "content" in result
content = json.loads(result["content"])
assert "success" in content
|