ralph-coder 0.2.1

An agentic code generation CLI powered by multiple LLM backends
Documentation
/// Compaction logic tests — no network, tests token counting and threshold logic.
use ralph::compaction;
use ralph::providers::{Message, MessageContent, Role};

fn make_messages(n: usize, chars_each: usize) -> Vec<Message> {
    (0..n)
        .map(|i| Message {
            role: if i % 2 == 0 {
                Role::User
            } else {
                Role::Assistant
            },
            content: MessageContent::Text("x".repeat(chars_each)),
            tool_call_id: None,
            name: None,
            reasoning_content: None,
        })
        .collect()
}

#[test]
fn estimate_tokens_scales_with_content() {
    let small = make_messages(1, 4); // ~1 token
    let large = make_messages(1, 400); // ~100 tokens
    assert!(compaction::estimate_tokens(&large) > compaction::estimate_tokens(&small));
}

#[test]
fn should_compact_false_when_below_threshold() {
    // 10 messages × 100 chars = 1000 chars ≈ 250 tokens
    // compact_at_tokens = 200_000, overhead = 0 → should NOT compact
    let messages = make_messages(10, 100);
    assert!(!compaction::should_compact(&messages, 200_000, 0));
}

#[test]
fn should_compact_true_when_at_threshold() {
    // 10 messages × 400 chars each ≈ 1000 tokens — exceeds compact_at_tokens=80
    let messages = make_messages(10, 400);
    assert!(compaction::should_compact(&messages, 80, 0));
}

#[test]
fn should_compact_false_when_empty() {
    let messages = vec![];
    assert!(!compaction::should_compact(&messages, 200_000, 0));
}

#[test]
fn should_compact_zero_threshold_always_triggers() {
    // compact_at_tokens = 0 means always compact
    let messages = make_messages(1, 1);
    assert!(compaction::should_compact(&messages, 0, 0));
}