codex-memory 3.0.15

A simple memory storage service with MCP interface for Claude Desktop
Documentation
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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
use crate::common::fixtures::{generate_unique_content, TestMemory};
use crate::common::TestDatabaseManager;
use anyhow::Result;
use codex_memory::{Storage, StorageInterface};
use serial_test::serial;
use std::sync::Arc;

#[tokio::test]
#[serial]
async fn test_store_with_full_fields() -> Result<()> {
    // Use TestDatabaseManager for proper isolation
    let mut manager = TestDatabaseManager::new()?;
    let pool = manager.setup_test_database().await?;
    let storage = Arc::new(Storage::new(pool));

    let test_data = TestMemory::full();

    // Store with all fields
    let id = storage
        .store(
            &test_data.content,
            test_data.context.clone(),
            test_data.summary.clone(),
            Some(test_data.tags.clone()),
        )
        .await?;

    // Retrieve and verify
    let retrieved = storage.get(id).await?.expect("Memory should exist");

    assert_eq!(retrieved.content, test_data.content);
    assert_eq!(retrieved.context, test_data.context);
    assert_eq!(retrieved.summary, test_data.summary);
    assert_eq!(retrieved.tags, test_data.tags);

    // Cleanup
    manager.cleanup().await?;
    Ok(())
}

#[tokio::test]
#[serial]
async fn test_store_minimal_content_only() -> Result<()> {
    let mut manager = TestDatabaseManager::new()?;
    let pool = manager.setup_test_database().await?;
    let storage = Arc::new(Storage::new(pool));

    let content = "Just plain content, nothing else";

    // Store with only content (no context, summary, or tags)
    let id = storage
        .store(
            content,
            "Test context".to_string(),
            "Test summary".to_string(),
            None,
        )
        .await?;

    // Retrieve and verify
    let retrieved = storage.get(id).await?.expect("Memory should exist");

    assert_eq!(retrieved.content, content);
    assert_eq!(retrieved.context, "Test context");
    assert_eq!(retrieved.summary, "Test summary");
    assert!(retrieved.tags.is_empty());

    // Cleanup
    manager.cleanup().await?;
    Ok(())
}

#[tokio::test]
#[serial]
async fn test_store_with_context_only() -> Result<()> {
    let mut manager = TestDatabaseManager::new()?;
    let pool = manager.setup_test_database().await?;
    let storage = Arc::new(Storage::new(pool));

    let test_data = TestMemory::with_context_only();

    let id = storage
        .store(
            &test_data.content,
            test_data.context.clone(),
            test_data.summary.clone(),
            Some(test_data.tags.clone()),
        )
        .await?;

    let retrieved = storage.get(id).await?.expect("Memory should exist");

    assert_eq!(retrieved.content, test_data.content);
    assert_eq!(retrieved.context, test_data.context);
    assert_eq!(retrieved.summary, test_data.summary);
    assert_eq!(retrieved.tags, test_data.tags);

    // Cleanup
    manager.cleanup().await?;
    Ok(())
}

#[tokio::test]
#[serial]
async fn test_store_with_summary_only() -> Result<()> {
    let mut manager = TestDatabaseManager::new()?;
    let pool = manager.setup_test_database().await?;
    let storage = Arc::new(Storage::new(pool));

    let test_data = TestMemory::with_summary_only();

    let id = storage
        .store(
            &test_data.content,
            test_data.context.clone(),
            test_data.summary.clone(),
            Some(test_data.tags.clone()),
        )
        .await?;

    let retrieved = storage.get(id).await?.expect("Memory should exist");

    assert_eq!(retrieved.content, test_data.content);
    assert_eq!(retrieved.context, test_data.context);
    assert_eq!(retrieved.summary, test_data.summary);
    assert_eq!(retrieved.tags, test_data.tags);

    // Cleanup
    manager.cleanup().await?;
    Ok(())
}

#[tokio::test]
#[serial]
async fn test_content_stored_without_modification() -> Result<()> {
    let mut manager = TestDatabaseManager::new()?;
    let pool = manager.setup_test_database().await?;
    let storage = Arc::new(Storage::new(pool));

    // Test various content types to ensure no modification
    let test_cases = vec![
        "Simple text",
        "Text with 'quotes' and \"double quotes\"",
        "Text with special chars: @#$%^&*()",
        "Multi-line\ntext\nwith\nbreaks",
        "Text with emoji 🎉 🚀 💾",
        "   Text with leading/trailing spaces   ",
        "Text\twith\ttabs",
    ];

    for content in test_cases {
        let id = storage
            .store(
                content,
                "Test context".to_string(),
                "Test summary".to_string(),
                None,
            )
            .await?;
        let retrieved = storage.get(id).await?.expect("Memory should exist");

        // Verify content is exactly the same
        assert_eq!(
            retrieved.content, content,
            "Content should be stored without modification"
        );
    }

    // Cleanup
    manager.cleanup().await?;
    Ok(())
}

#[tokio::test]
#[serial]
async fn test_deduplication_with_different_context_summary() -> Result<()> {
    let mut manager = TestDatabaseManager::new()?;
    let pool = manager.setup_test_database().await?;
    let storage = Arc::new(Storage::new(pool));

    let content = "Duplicate content test";

    // Store same content with different context/summary
    let id1 = storage
        .store(
            content,
            "Context 1".to_string(),
            "Summary 1".to_string(),
            None,
        )
        .await?;

    // Store again with different context/summary - should update
    let id2 = storage
        .store(
            content,
            "Context 2".to_string(),
            "Summary 2".to_string(),
            None,
        )
        .await?;

    // Should be the same ID (deduplication)
    assert_eq!(id1, id2);

    // Verify the context/summary were updated
    let retrieved = storage.get(id1).await?.expect("Memory should exist");
    assert_eq!(retrieved.context, "Context 2".to_string());
    assert_eq!(retrieved.summary, "Summary 2".to_string());

    // Cleanup
    manager.cleanup().await?;
    Ok(())
}

#[tokio::test]
#[serial]
async fn test_tag_operations() -> Result<()> {
    let mut manager = TestDatabaseManager::new()?;
    let pool = manager.setup_test_database().await?;
    let storage = Arc::new(Storage::new(pool));

    // Test empty tags
    let id1 = storage
        .store(
            "Content 1",
            "Test context".to_string(),
            "Test summary".to_string(),
            Some(vec![]),
        )
        .await?;
    let mem1 = storage.get(id1).await?.expect("Memory should exist");
    assert!(mem1.tags.is_empty());

    // Test single tag
    let id2 = storage
        .store(
            "Content 2",
            "Test context".to_string(),
            "Test summary".to_string(),
            Some(vec!["single".to_string()]),
        )
        .await?;
    let mem2 = storage.get(id2).await?.expect("Memory should exist");
    assert_eq!(mem2.tags, vec!["single"]);

    // Test multiple tags
    let tags = vec!["tag1".to_string(), "tag2".to_string(), "tag3".to_string()];
    let id3 = storage
        .store(
            "Content 3",
            "Test context".to_string(),
            "Test summary".to_string(),
            Some(tags.clone()),
        )
        .await?;
    let mem3 = storage.get(id3).await?.expect("Memory should exist");
    assert_eq!(mem3.tags, tags);

    // Test special character tags
    let special_tags = TestMemory::with_special_tags();
    let id4 = storage
        .store(
            &special_tags.content,
            special_tags.context.clone(),
            special_tags.summary.clone(),
            Some(special_tags.tags.clone()),
        )
        .await?;
    let mem4 = storage.get(id4).await?.expect("Memory should exist");
    assert_eq!(mem4.tags, special_tags.tags);

    // Cleanup
    manager.cleanup().await?;
    Ok(())
}

// Tier storage test removed - tier system no longer exists

#[tokio::test]
#[serial]
async fn test_delete_memory() -> Result<()> {
    let mut manager = TestDatabaseManager::new()?;
    let pool = manager.setup_test_database().await?;
    let storage = Arc::new(Storage::new(pool));

    let content = "Content to be deleted";
    let id = storage
        .store(
            content,
            "Test context".to_string(),
            "Test summary".to_string(),
            None,
        )
        .await?;

    // Verify it exists
    assert!(storage.get(id).await?.is_some());

    // Delete it
    let deleted = storage.delete(id).await?;
    assert!(deleted);

    // Verify it's gone
    assert!(storage.get(id).await?.is_none());

    // Try to delete again - should return false
    let deleted_again = storage.delete(id).await?;
    assert!(!deleted_again);

    // Cleanup
    manager.cleanup().await?;
    Ok(())
}

#[tokio::test]
#[serial]
async fn test_storage_stats() -> Result<()> {
    let mut manager = TestDatabaseManager::new()?;
    let pool = manager.setup_test_database().await?;
    let storage = Arc::new(Storage::new(pool));

    // Initial stats
    let stats = storage.stats().await?;
    assert_eq!(stats.total_memories, 0);

    // Add some memories
    for i in 0..5 {
        let content = format!("Memory #{} - {}", i, generate_unique_content());
        storage
            .store(
                &content,
                "Test context".to_string(),
                "Test summary".to_string(),
                None,
            )
            .await?;
    }

    // Check updated stats
    let stats = storage.stats().await?;
    assert_eq!(stats.total_memories, 5);
    assert!(stats.last_memory_created.is_some());

    // Cleanup
    manager.cleanup().await?;
    Ok(())
}

#[tokio::test]
#[serial]
async fn test_list_recent() -> Result<()> {
    let mut manager = TestDatabaseManager::new()?;
    let pool = manager.setup_test_database().await?;
    let storage = Arc::new(Storage::new(pool));

    // Store 10 memories
    for i in 0..10 {
        let content = format!("Memory #{} - {}", i, generate_unique_content());
        storage
            .store(
                &content,
                "Test context".to_string(),
                "Test summary".to_string(),
                None,
            )
            .await?;
        // Small delay to ensure different timestamps
        tokio::time::sleep(tokio::time::Duration::from_millis(10)).await;
    }

    // Get recent 5
    let recent = storage.list_recent(5).await?;
    assert_eq!(recent.len(), 5);

    // Verify they're in descending order (most recent first)
    for i in 0..recent.len() - 1 {
        assert!(recent[i].created_at >= recent[i + 1].created_at);
    }

    // Cleanup
    manager.cleanup().await?;
    Ok(())
}

/// Test that Storage implements the StorageInterface trait properly
#[tokio::test]
#[serial]
async fn test_storage_interface_trait() -> Result<()> {
    let mut manager = TestDatabaseManager::new()?;
    let pool = manager.setup_test_database().await?;
    let storage: Arc<dyn StorageInterface> = Arc::new(Storage::new(pool));

    let test_data = TestMemory::full();

    // Test store through trait
    let id = storage
        .store(
            &test_data.content,
            test_data.context.clone(),
            test_data.summary.clone(),
            Some(test_data.tags.clone()),
        )
        .await?;

    // Test get through trait
    let retrieved = storage.get(id).await?.expect("Memory should exist");
    assert_eq!(retrieved.content, test_data.content);

    // Test stats through trait
    let stats = storage.stats().await?;
    assert!(stats.total_memories > 0);

    // Test delete through trait
    let deleted = storage.delete(id).await?;
    assert!(deleted);

    // Verify deletion worked
    let not_found = storage.get(id).await?;
    assert!(not_found.is_none());

    // Cleanup
    manager.cleanup().await?;
    Ok(())
}

/// Test error handling improvements in Storage operations
#[tokio::test]
#[serial]
async fn test_storage_error_handling() -> Result<()> {
    let mut manager = TestDatabaseManager::new()?;
    let pool = manager.setup_test_database().await?;
    let storage = Arc::new(Storage::new(pool));

    // Test with invalid UUID
    let invalid_uuid = uuid::Uuid::parse_str("invalid-uuid");
    assert!(invalid_uuid.is_err());

    // Test retrieval of non-existent memory
    let fake_id = uuid::Uuid::new_v4();
    let result = storage.get(fake_id).await?;
    assert!(result.is_none());

    // Test deletion of non-existent memory
    let deleted = storage.delete(fake_id).await?;
    assert!(!deleted);

    // Cleanup
    manager.cleanup().await?;
    Ok(())
}