-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMemoryManagerAgentIntegrationTest.php
More file actions
197 lines (155 loc) · 6.41 KB
/
MemoryManagerAgentIntegrationTest.php
File metadata and controls
197 lines (155 loc) · 6.41 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
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
<?php
declare(strict_types=1);
namespace Tests\Integration\Agents;
use ClaudeAgents\Agents\MemoryManagerAgent;
use ClaudePhp\ClaudePhp;
use PHPUnit\Framework\TestCase;
/**
* Integration tests for MemoryManagerAgent
*
* These tests verify the agent works with real API calls.
* Set ANTHROPIC_API_KEY environment variable to run these tests.
*/
class MemoryManagerAgentIntegrationTest extends TestCase
{
private ?ClaudePhp $client = null;
private ?MemoryManagerAgent $agent = null;
protected function setUp(): void
{
$apiKey = getenv('ANTHROPIC_API_KEY');
if (! $apiKey) {
$this->markTestSkipped('ANTHROPIC_API_KEY not set. Skipping integration tests.');
}
$this->client = new ClaudePhp(apiKey: $apiKey);
$this->agent = new MemoryManagerAgent($this->client, [
'name' => 'integration_test_agent',
]);
}
public function test_stores_and_retrieves_memory(): void
{
$content = 'Integration test content: The quick brown fox jumps over the lazy dog';
$id = $this->agent->store($content);
$this->assertIsString($id);
$this->assertStringStartsWith('mem_', $id);
$retrieved = $this->agent->retrieve($id);
$this->assertSame($content, $retrieved);
}
public function test_search_with_llm_finds_relevant_memories(): void
{
// Store multiple memories
$this->agent->store('PHP is a server-side scripting language used for web development');
$this->agent->store('Python is a high-level programming language known for readability');
$this->agent->store('JavaScript is primarily used for client-side web programming');
$this->agent->store('The weather today is sunny and warm');
// Search for programming-related content
$results = $this->agent->search('programming languages', 3);
$this->assertIsArray($results);
$this->assertGreaterThan(0, count($results));
// Results should contain programming-related content
$allContent = implode(' ', array_column($results, 'content'));
$this->assertTrue(
stripos($allContent, 'programming') !== false ||
stripos($allContent, 'language') !== false ||
stripos($allContent, 'PHP') !== false
);
}
public function test_stores_memory_with_tags_and_finds_by_tag(): void
{
$id1 = $this->agent->store(
'Claude AI is an advanced language model',
['source' => 'documentation'],
['AI', 'Claude']
);
$id2 = $this->agent->store(
'Machine learning powers modern AI systems',
['source' => 'article'],
['AI', 'ML']
);
$aiMemories = $this->agent->findByTag('AI');
$this->assertCount(2, $aiMemories);
$claudeMemories = $this->agent->findByTag('Claude');
$this->assertCount(1, $claudeMemories);
$this->assertSame($id1, $claudeMemories[0]['id']);
}
public function test_run_method_with_various_commands(): void
{
// Test store command
$result = $this->agent->run('store: Important information about API integration');
$this->assertTrue($result->isSuccess());
$metadata = $result->getMetadata();
$memId = $metadata['id'];
// Test retrieve command
$result = $this->agent->run("retrieve: {$memId}");
$this->assertTrue($result->isSuccess());
$this->assertStringContainsString('API integration', $result->getAnswer());
// Test stats command
$result = $this->agent->run('stats');
$this->assertTrue($result->isSuccess());
$this->assertStringContainsString('total_memories', $result->getAnswer());
// Test forget command
$result = $this->agent->run("forget: {$memId}");
$this->assertTrue($result->isSuccess());
// Verify it's forgotten
$result = $this->agent->run("retrieve: {$memId}");
$this->assertSame('Not found', $result->getAnswer());
}
public function test_import_and_export_workflow(): void
{
// Store some memories
$this->agent->store('Memory 1', ['type' => 'test'], ['export-test']);
$this->agent->store('Memory 2', ['type' => 'test'], ['export-test']);
$this->agent->store('Memory 3', ['type' => 'test'], ['export-test']);
// Export
$exported = $this->agent->export();
$this->assertCount(3, $exported);
// Create new agent and import
$newAgent = new MemoryManagerAgent($this->client, ['name' => 'import_test']);
$count = $newAgent->import($exported);
$this->assertSame(3, $count);
$this->assertSame(3, $newAgent->getMemoryCount());
// Verify imported memories have tags
$tagged = $newAgent->findByTag('export-test');
$this->assertCount(3, $tagged);
}
public function test_memory_statistics_accuracy(): void
{
// Clear and start fresh
$this->agent->clear();
$this->agent->store('Short', ['key' => 'val'], ['tag1']);
$this->agent->store('Medium length text', [], ['tag1', 'tag2']);
$this->agent->store('Another piece of content');
$stats = $this->agent->getStats();
$this->assertSame(3, $stats['total_memories']);
$this->assertSame(2, $stats['unique_tags']);
$this->assertSame(1, $stats['memories_with_metadata']);
$this->assertSame(2, $stats['memories_with_tags']);
$this->assertGreaterThan(0, $stats['total_size_bytes']);
$this->assertGreaterThan(0, $stats['index_size']);
}
public function test_concurrent_operations(): void
{
$ids = [];
// Store multiple memories
for ($i = 0; $i < 5; $i++) {
$ids[] = $this->agent->store("Memory number {$i}", ['index' => $i], ["batch{$i}"]);
}
// Retrieve all
foreach ($ids as $i => $id) {
$content = $this->agent->retrieve($id);
$this->assertStringContainsString("Memory number {$i}", $content);
}
// Verify count
$this->assertSame(5, $this->agent->getMemoryCount());
// Forget some
$this->agent->forget($ids[0]);
$this->agent->forget($ids[2]);
$this->assertSame(3, $this->agent->getMemoryCount());
}
protected function tearDown(): void
{
// Clean up test data
if ($this->agent) {
$this->agent->clear();
}
}
}