Skip to main content

cel_memory/
conformance.rs

1//! Shared behavioral checks for [`MemoryProvider`] implementations.
2//!
3//! Backend crates should call these helpers from integration tests so every
4//! persistence layer honors the same contract. See
5//! [`cel-memory-sqlite/tests/swap.rs`](https://github.com/dimpagk92/cel-memory-sqlite/blob/main/tests/swap.rs)
6//! for a downstream example.
7
8use std::sync::Arc;
9
10use crate::{
11    ChunkKind, ChunkSource, MemoryChunk, MemoryProvider, MemoryStats, NewMemoryChunk, Result,
12};
13
14/// Minimal contract every backend must satisfy: write a chunk, read it back,
15/// and report coherent stats.
16pub async fn assert_write_get_stats(
17    memory: Arc<dyn MemoryProvider>,
18    content: &str,
19) -> Result<(MemoryChunk, MemoryStats)> {
20    let chunk = memory
21        .write(NewMemoryChunk {
22            kind: ChunkKind::Chat,
23            source: ChunkSource::Embedded,
24            session_id: None,
25            project_root: None,
26            caller_id: "conformance".into(),
27            content: content.into(),
28            metadata: serde_json::Value::Null,
29            importance: None,
30            shareable: false,
31            pinned: false,
32        })
33        .await?;
34
35    let fetched = memory
36        .get(&chunk.id)
37        .await?
38        .ok_or_else(|| crate::MemoryError::NotFound(chunk.id.clone()))?;
39    assert_eq!(fetched.content, chunk.content);
40
41    let stats = memory.stats().await?;
42    assert!(stats.total_chunks >= 1);
43
44    Ok((chunk, stats))
45}