vtcode-llm 0.186.0

LLM provider abstraction, client implementations, and streaming for VT Code
Documentation
use super::*;

#[test]
fn test_streaming_processor_creation() {
    let processor = StreamingProcessor::new();
    assert_eq!(processor.metrics().total_requests, 0);
}

#[test]
fn test_streaming_processor_with_config() {
    use std::time::Duration;

    let config = StreamingConfig {
        chunk_timeout: Duration::from_secs(10),
        first_chunk_timeout: Duration::from_secs(30),
        total_timeout: Duration::from_secs(120),
        buffer_size: 512,
    };

    let processor = StreamingProcessor::with_config(config);
    assert_eq!(processor.metrics().total_requests, 0);
}

#[test]
fn test_streaming_config_default() {
    let config = StreamingConfig::default();
    assert_eq!(config.buffer_size, 1024);
}

#[test]
fn test_handles_back_to_back_data_lines_without_blank_lines() {
    let mut processor = StreamingProcessor::new();
    let mut accumulated = StreamingResponse { candidates: Vec::new(), usage_metadata: None };
    let mut received_chunks: Vec<String> = Vec::new();
    let mut buffer = String::from(
        "data: {\"candidates\":[{\"index\":0,\"content\":{\"role\":\"model\",\"parts\":[{\"text\":\"Hello\"}]}}]}\n",
    );
    buffer.push_str(
        "data: {\"candidates\":[{\"index\":0,\"content\":{\"role\":\"model\",\"parts\":[{\"text\":\" world\"}]}}]}\n",
    );

    {
        let mut on_chunk = |chunk: &str| {
            received_chunks.push(chunk.to_owned());
            Ok(())
        };
        let has_valid = processor
            .process_buffer(&mut buffer, &mut accumulated, &mut on_chunk)
            .expect("processing should succeed");
        assert!(has_valid);
    }

    assert_eq!(received_chunks, vec!["Hello", " world"]);
    assert_eq!(accumulated.candidates.len(), 1);
    let combined = match &accumulated.candidates[0].content.parts[0] {
        Part::Text { text, .. } => text.clone(),
        _ => String::new(),
    };
    assert_eq!(combined, "Hello world");
}

#[test]
fn process_buffer_fails_closed_on_boundary_less_tail() {
    use crate::providers::shared::MAX_SSE_TAIL_BYTES;

    let mut processor = StreamingProcessor::new();
    let mut accumulated = StreamingResponse { candidates: Vec::new(), usage_metadata: None };
    // No newline anywhere: the whole 16 MiB + 1 stays retained.
    let mut buffer = "x".repeat(MAX_SSE_TAIL_BYTES + 1);
    let mut on_chunk = |_: &str| Ok(());

    let error = processor
        .process_buffer(&mut buffer, &mut accumulated, &mut on_chunk)
        .expect_err("boundary-less tail past the cap must trip");
    assert!(
        matches!(error, StreamingError::StreamingError { partial_content: None, .. }),
        "overflow must not clone the tail into the error: {error:?}"
    );
    assert!(error.to_string().contains("without an event boundary"), "{error}");
}

#[test]
fn process_buffer_accepts_complete_event_past_the_cap() {
    use crate::providers::shared::MAX_SSE_TAIL_BYTES;

    // Asymmetric control: the same oversized payload with a trailing
    // newline parses and drains, so nothing is retained and the bound
    // must not trip — the check measures the tail, not the chunk.
    let mut processor = StreamingProcessor::new();
    let mut accumulated = StreamingResponse { candidates: Vec::new(), usage_metadata: None };
    let mut buffer = "x".repeat(MAX_SSE_TAIL_BYTES + 1);
    buffer.push('\n');
    let mut on_chunk = |_: &str| Ok(());
    processor
        .process_buffer(&mut buffer, &mut accumulated, &mut on_chunk)
        .expect("drained event must pass");
    assert!(buffer.is_empty());
}