use super::{ChatStore, TurnRole};
#[test]
fn record_and_history_round_trips() {
let store = ChatStore::default();
let count = store.record_exchange("conv-1", "what's up?", "all quiet");
assert_eq!(count, 2);
let history = store.history("conv-1");
assert_eq!(history.len(), 2);
assert_eq!(history[0].role, TurnRole::User);
assert_eq!(history[0].content, "what's up?");
assert_eq!(history[1].role, TurnRole::Assistant);
assert_eq!(history[1].content, "all quiet");
}
#[test]
fn history_is_bounded() {
let store = ChatStore::new(4, super::DEFAULT_MAX_KEYS);
for i in 0..5 {
store.record_exchange("conv-1", &format!("q{i}"), &format!("a{i}"));
}
let history = store.history("conv-1");
assert_eq!(history.len(), 4, "capped at max_turns");
assert_eq!(history[0].content, "q3");
assert_eq!(history[3].content, "a4");
}
#[test]
fn distinct_keys_are_isolated() {
let store = ChatStore::default();
store.record_exchange("conv-a", "hi a", "reply a");
store.record_exchange("conv-b", "hi b", "reply b");
assert_eq!(store.history("conv-a").len(), 2);
assert_eq!(store.history("conv-b")[0].content, "hi b");
assert!(store.history("conv-missing").is_empty());
}
#[test]
fn key_set_is_bounded_lru_eviction() {
let store = ChatStore::new(super::DEFAULT_MAX_TURNS, 2);
store.record_exchange("conv-1", "hi 1", "reply 1");
store.record_exchange("conv-2", "hi 2", "reply 2");
store.record_exchange("conv-3", "hi 3", "reply 3");
assert!(
store.history("conv-1").is_empty(),
"least-recently-used key must be evicted"
);
assert_eq!(store.history("conv-2").len(), 2, "conv-2 survives eviction");
assert_eq!(
store.history("conv-3").len(),
2,
"newly-inserted key survives"
);
}
#[test]
fn reading_a_key_promotes_it_and_protects_from_eviction() {
let store = ChatStore::new(super::DEFAULT_MAX_TURNS, 2);
store.record_exchange("conv-1", "hi 1", "reply 1");
store.record_exchange("conv-2", "hi 2", "reply 2");
let _ = store.history("conv-1");
store.record_exchange("conv-3", "hi 3", "reply 3");
assert_eq!(
store.history("conv-1").len(),
2,
"recently-read key survives"
);
assert!(
store.history("conv-2").is_empty(),
"un-touched key is evicted instead"
);
assert_eq!(store.history("conv-3").len(), 2);
}
#[test]
fn evicted_key_reads_as_empty_and_can_restart() {
let store = ChatStore::new(super::DEFAULT_MAX_TURNS, 1);
store.record_exchange("conv-1", "hi 1", "reply 1");
store.record_exchange("conv-2", "hi 2", "reply 2");
assert!(
store.history("conv-1").is_empty(),
"evicted by the 1-key cap"
);
let count = store.record_exchange("conv-1", "hi again", "reply again");
assert_eq!(count, 2);
assert_eq!(store.history("conv-1")[0].content, "hi again");
}