use ralph::compaction::{count_tokens, estimate_tokens, should_compact};
use ralph::providers::{Message, MessageContent, Role};
#[test]
fn count_tokens_empty_string() {
assert_eq!(count_tokens(""), 0);
}
#[test]
fn count_tokens_single_word() {
let t = count_tokens("hello");
assert!(t >= 1, "expected at least 1 token, got {}", t);
}
#[test]
fn count_tokens_longer_text() {
let short = count_tokens("hi");
let long =
count_tokens("The quick brown fox jumps over the lazy dog and then continues running.");
assert!(long > short, "longer text should produce more tokens");
}
#[test]
fn count_tokens_not_char_over_four() {
let repeated = "aaaaaaaaaaaaaaaa"; let tiktoken_count = count_tokens(repeated);
let char_estimate = (repeated.len() as u64) / 4;
assert!(
tiktoken_count <= char_estimate,
"tiktoken ({}) should be <= char/4 estimate ({}) for repeated chars",
tiktoken_count,
char_estimate
);
}
#[test]
fn estimate_tokens_sums_messages() {
let msgs = vec![
Message {
role: Role::User,
content: MessageContent::Text("hello world".to_string()),
tool_call_id: None,
name: None,
reasoning_content: None,
},
Message {
role: Role::Assistant,
content: MessageContent::Text("hi there friend".to_string()),
tool_call_id: None,
name: None,
reasoning_content: None,
},
];
let total = estimate_tokens(&msgs);
assert!(total > 0);
let single = estimate_tokens(&msgs[..1]);
assert!(total > single);
}
#[test]
fn should_compact_false_with_tiny_messages() {
let msgs = vec![Message {
role: Role::User,
content: MessageContent::Text("hi".to_string()),
tool_call_id: None,
name: None,
reasoning_content: None,
}];
assert!(!should_compact(&msgs, 200_000, 80));
}
#[test]
fn should_compact_true_when_over_threshold() {
let msgs: Vec<Message> = (0..20)
.map(|i| Message {
role: if i % 2 == 0 {
Role::User
} else {
Role::Assistant
},
content: MessageContent::Text("word ".repeat(50)),
tool_call_id: None,
name: None,
reasoning_content: None,
})
.collect();
assert!(should_compact(&msgs, 50, 10));
}