#[cfg(test)]
use crate::core::streaming::{handler::StreamingHandler, utils};
#[test]
fn test_streaming_handler_creation() {
let handler = StreamingHandler::new("gpt-4".to_string());
assert_eq!(handler.model, "gpt-4");
assert!(handler.is_first_chunk);
assert!(handler.accumulated_content.is_empty());
}
#[test]
fn test_extract_content_from_chunk() {
let handler = StreamingHandler::new("gpt-4".to_string());
let openai_chunk = r#"data: {"choices":[{"delta":{"content":"Hello"}}]}"#;
let content = handler.extract_content_from_chunk(openai_chunk).unwrap();
assert_eq!(content, "Hello");
let anthropic_chunk = r#"data: {"delta":{"text":"World"}}"#;
let content = handler.extract_content_from_chunk(anthropic_chunk).unwrap();
assert_eq!(content, "World");
let done_chunk = "data: [DONE]";
let content = handler.extract_content_from_chunk(done_chunk).unwrap();
assert!(content.is_empty());
}
#[test]
fn test_token_estimation() {
let handler = StreamingHandler::new("gpt-4".to_string());
let text = "Hello world"; let tokens = handler.estimate_token_count(text);
assert_eq!(tokens, 3);
let longer_text = "This is a longer text for testing"; let tokens = handler.estimate_token_count(longer_text);
assert_eq!(tokens, 9);
}
#[tokio::test]
async fn test_sse_utils() {
let line = "data: Hello World";
let data = utils::parse_sse_line(line);
assert_eq!(data, Some("Hello World".to_string()));
assert!(utils::is_done_line("data: [DONE]"));
assert!(utils::is_done_line("[DONE]"));
assert!(!utils::is_done_line("data: Hello"));
let _error_event = utils::create_error_event("Test error");
}