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 process_candidate_rejects_inline_image_output() {
let processor = StreamingProcessor::new();
let candidate = StreamingCandidate {
content: Content {
role: "model".to_string(),
parts: vec![
Part::Text {
text: "partial text".to_string(),
thought_signature: None,
},
Part::InlineData {
inline_data: super::super::super::models::InlineData {
mime_type: "image/png".to_string(),
data: "image-payload-sentinel".to_string(),
},
},
],
},
finish_reason: None,
index: Some(0),
};
let mut chunks = Vec::new();
let mut on_chunk = |chunk: &str| {
chunks.push(chunk.to_string());
Ok(())
};
let error = processor
.process_candidate(&candidate, &mut on_chunk)
.expect_err("image output must not be silently discarded");
assert!(matches!(&error, StreamingError::ContentError { message } if message.contains("cannot surface yet")));
assert!(!error.to_string().contains("image-payload-sentinel"));
assert!(chunks.is_empty(), "unsupported image output should not expose partial text from the same candidate");
}
#[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 };
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;
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());
}