use std::time::Duration;
use synaptic_cache::{InMemoryCache, LlmCache};
use synaptic_core::{ChatResponse, Message};
fn make_response(text: &str) -> ChatResponse {
ChatResponse {
message: Message::ai(text),
usage: None,
}
}
#[tokio::test]
async fn cache_stores_and_retrieves() {
let cache = InMemoryCache::new();
let response = make_response("hello");
cache.put("key1", &response).await.unwrap();
let result = cache.get("key1").await.unwrap();
assert!(result.is_some());
assert_eq!(result.unwrap().message.content(), "hello");
}
#[tokio::test]
async fn cache_returns_none_for_miss() {
let cache = InMemoryCache::new();
let result = cache.get("nonexistent").await.unwrap();
assert!(result.is_none());
}
#[tokio::test]
async fn cache_ttl_expires() {
let cache = InMemoryCache::with_ttl(Duration::from_millis(50));
let response = make_response("ephemeral");
cache.put("key1", &response).await.unwrap();
assert!(cache.get("key1").await.unwrap().is_some());
tokio::time::sleep(Duration::from_millis(100)).await;
assert!(cache.get("key1").await.unwrap().is_none());
}
#[tokio::test]
async fn cache_no_ttl_persists() {
let cache = InMemoryCache::new();
let response = make_response("persistent");
cache.put("key1", &response).await.unwrap();
tokio::time::sleep(Duration::from_millis(50)).await;
let result = cache.get("key1").await.unwrap();
assert!(result.is_some());
assert_eq!(result.unwrap().message.content(), "persistent");
}
#[tokio::test]
async fn cache_clear_removes_all_entries() {
let cache = InMemoryCache::new();
cache.put("key1", &make_response("a")).await.unwrap();
cache.put("key2", &make_response("b")).await.unwrap();
assert!(cache.get("key1").await.unwrap().is_some());
assert!(cache.get("key2").await.unwrap().is_some());
cache.clear().await.unwrap();
assert!(cache.get("key1").await.unwrap().is_none());
assert!(cache.get("key2").await.unwrap().is_none());
}