#[test]
fn test_format_unknown_json_event_control_event() {
let colors = Colors { enabled: false };
let json = r#"{"type":"message_start","message":{"id":"msg_123"}}"#;
let output = super::types::format_unknown_json_event(json, "Claude", colors, true);
assert!(output.is_empty());
}
#[test]
fn test_format_unknown_json_event_partial_event() {
let colors = Colors { enabled: false };
let json =
r#"{"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"Hello"}}"#;
let output = super::types::format_unknown_json_event(json, "Claude", colors, false);
assert!(!output.is_empty());
}
#[test]
fn test_format_unknown_json_event_partial_event_verbose() {
let colors = Colors { enabled: false };
let json =
r#"{"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"Hello"}}"#;
let output = super::types::format_unknown_json_event(json, "Claude", colors, true);
assert!(!output.is_empty());
assert!(output.contains("Partial event"));
}
#[test]
fn test_format_unknown_json_event_complete_event_verbose() {
let colors = Colors { enabled: false };
let json = r#"{"type":"message","content":"This is a complete message with substantial content that should be displayed as is."}"#;
let output = super::types::format_unknown_json_event(json, "Claude", colors, true);
assert!(!output.is_empty());
assert!(output.contains("Complete event"));
}
#[test]
fn test_format_unknown_json_event_complete_event_normal() {
let colors = Colors { enabled: false };
let json = r#"{"type":"status","status":"ok"}"#;
let output = super::types::format_unknown_json_event(json, "Claude", colors, false);
assert!(output.is_empty());
}
#[test]
fn test_format_unknown_json_event_with_explicit_delta_flag() {
let colors = Colors { enabled: false };
let json = r#"{"type":"message","delta":true,"content":"partial content"}"#;
let output = super::types::format_unknown_json_event(json, "Test", colors, true);
assert!(!output.is_empty());
assert!(output.contains("Partial event"));
}
#[test]
fn test_format_unknown_json_event_error_control() {
let colors = Colors { enabled: false };
let json = r#"{"type":"error","message":"Something went wrong"}"#;
let output = super::types::format_unknown_json_event(json, "Claude", colors, true);
assert!(output.is_empty());
}
#[test]
fn test_format_unknown_json_event_delta_shows_content_in_normal_mode() {
let colors = Colors { enabled: false };
let json = r#"{"type":"content_block_delta","delta":{"text":"Hello World"}}"#;
let output = super::types::format_unknown_json_event(json, "Test", colors, false);
assert!(!output.is_empty());
assert!(output.contains("Hello World"));
}
#[test]
fn test_format_unknown_json_event_partial_with_delta_field() {
let colors = Colors { enabled: false };
let json = r#"{"type":"chunk","delta":"some partial content"}"#;
let output = super::types::format_unknown_json_event(json, "Test", colors, false);
assert!(!output.is_empty());
assert!(output.contains("some partial content"));
}
#[test]
fn test_format_unknown_json_event_partial_with_text_field() {
let colors = Colors { enabled: false };
let json = r#"{"type":"partial","text":"streaming text here"}"#;
let output = super::types::format_unknown_json_event(json, "Test", colors, false);
assert!(!output.is_empty());
assert!(output.contains("streaming text here"));
}
#[test]
fn test_format_unknown_json_event_nested_delta_text() {
let colors = Colors { enabled: false };
let json = r#"{"type":"update","delta":{"text":"nested content"}}"#;
let output = super::types::format_unknown_json_event(json, "Test", colors, false);
assert!(!output.is_empty());
assert!(output.contains("nested content"));
}
#[test]
fn test_format_unknown_json_event_deeply_nested_content() {
let colors = Colors { enabled: false };
let json = r#"{"type":"delta","delta":{"text":"deep nested text"}}"#;
let output = super::types::format_unknown_json_event(json, "Test", colors, false);
assert!(!output.is_empty());
assert!(output.contains("deep nested text"));
}
#[test]
fn test_format_unknown_json_event_array_content() {
let colors = Colors { enabled: false };
let json = r#"{"type":"message","content":["item1","item2","item3"]}"#;
let _output = super::types::format_unknown_json_event(json, "Test", colors, false);
}
#[test]
fn test_format_unknown_json_event_empty_delta() {
let colors = Colors { enabled: false };
let json = r#"{"type":"content_block_delta","delta":{"text":""}}"#;
let output = super::types::format_unknown_json_event(json, "Test", colors, false);
assert!(output.is_empty() || output.trim().is_empty());
}
#[test]
fn test_format_unknown_json_event_whitespace_only_delta() {
let colors = Colors { enabled: false };
let json = r#"{"type":"content_block_delta","delta":{"text":" "}}"#;
let output = super::types::format_unknown_json_event(json, "Test", colors, false);
assert!(output.is_empty() || output.trim().is_empty());
}
#[test]
fn test_format_unknown_json_event_unicode_delta_content() {
let colors = Colors { enabled: false };
let json = r#"{"type":"delta","text":"Hello 世界 🌍"}"#;
let output = super::types::format_unknown_json_event(json, "Test", colors, false);
assert!(!output.is_empty());
assert!(output.contains("Hello 世界"));
}
#[test]
fn test_format_unknown_json_event_text_field_priority() {
let colors = Colors { enabled: false };
let json = r#"{"type":"content_delta","text":"first","content":"second"}"#;
let output = super::types::format_unknown_json_event(json, "Test", colors, false);
assert!(!output.is_empty());
assert!(output.contains("second"));
}
#[test]
fn test_format_unknown_json_event_null_content_field() {
let colors = Colors { enabled: false };
let json = r#"{"type":"message","content":null}"#;
let output = super::types::format_unknown_json_event(json, "Test", colors, false);
assert!(output.is_empty());
}
#[test]
fn test_format_unknown_json_event_numeric_content_field() {
let colors = Colors { enabled: false };
let json = r#"{"type":"metric","content":12345}"#;
let output = super::types::format_unknown_json_event(json, "Test", colors, true);
assert!(!output.is_empty());
}
#[test]
fn test_format_unknown_json_event_boolean_delta_flag() {
let colors = Colors { enabled: false };
let json = r#"{"type":"chunk","delta":true,"content":"test"}"#;
let output = super::types::format_unknown_json_event(json, "Test", colors, false);
assert!(!output.is_empty());
assert!(output.contains("test"));
}
#[test]
fn test_format_unknown_json_event_special_characters_in_content() {
let colors = Colors { enabled: false };
let json = r#"{"type":"delta","text":"Line1\nLine2\tTabbed\"Quoted"}"#;
let output = super::types::format_unknown_json_event(json, "Test", colors, false);
assert!(!output.is_empty());
}
#[test]
fn test_format_unknown_json_event_very_long_content() {
let colors = Colors { enabled: false };
let long_text = "a".repeat(10000);
let json = format!(r#"{{"type":"delta","text":"{long_text}"}}"#);
let output = super::types::format_unknown_json_event(&json, "Test", colors, false);
assert!(!output.is_empty());
}
#[test]
fn test_stream_classifies_short_content_as_partial() {
use super::stream_classifier::{StreamEventClassifier, StreamEventType};
let classifier = StreamEventClassifier::new();
let event = serde_json::json!({
"type": "chunk",
"content": "Hi"
});
let result = classifier.classify(&event);
assert_eq!(result.event_type, StreamEventType::Partial);
}
#[test]
fn test_stream_classifies_long_content_as_complete() {
use super::stream_classifier::{StreamEventClassifier, StreamEventType};
let classifier = StreamEventClassifier::new();
let long_text = "This is a substantial message that exceeds the default threshold and should be considered complete.";
let event = serde_json::json!({
"type": "message",
"content": long_text
});
let result = classifier.classify(&event);
assert_eq!(result.event_type, StreamEventType::Complete);
}
#[test]
fn test_stream_classifies_status_without_content_as_control() {
use super::stream_classifier::{StreamEventClassifier, StreamEventType};
let classifier = StreamEventClassifier::new();
let event = serde_json::json!({
"type": "status",
"status": "processing"
});
let result = classifier.classify(&event);
assert_eq!(result.event_type, StreamEventType::Control);
}
#[cfg(test)]
#[test]
fn test_stream_classifies_error_as_control() {
use super::stream_classifier::{StreamEventClassifier, StreamEventType};
let classifier = StreamEventClassifier::new();
let event = serde_json::json!({
"type": "error",
"message": "Something went wrong"
});
let result = classifier.classify(&event);
assert_eq!(result.event_type, StreamEventType::Control);
}
#[cfg(test)]
#[test]
fn test_claude_parser_tracks_partial_events_in_health_monitoring() {
use std::io::Cursor;
let test_printer = Rc::new(RefCell::new(TestPrinter::new()));
let printer: SharedPrinter = test_printer.clone();
let mut parser =
ClaudeParser::with_printer(Colors { enabled: false }, Verbosity::Normal, printer)
.with_terminal_mode(TerminalMode::Full);
let input = r#"{"type":"stream_event","event":{"type":"message_start"}}
{"type":"stream_event","event":{"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"Hello"}}}
{"type":"stream_event","event":{"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":" World"}}}
{"type":"stream_event","event":{"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"!"}}}
{"type":"stream_event","event":{"type":"message_stop"}}
{"type":"assistant","message":{"content":[{"type":"text","text":"Complete message"}]}}"#;
let reader = Cursor::new(input);
let result = parser.parse_stream(reader, &MemoryWorkspace::new_test());
assert!(result.is_ok());
let printer_ref = test_printer.borrow();
let output = printer_ref.get_output();
assert!(output.contains("Hello") || output.contains("World") || output.contains("Complete"));
}
#[test]
fn test_health_monitor_no_warning_with_high_partial_percentage() {
use super::health::HealthMonitor;
let monitor = HealthMonitor::new("test");
let colors = Colors { enabled: false };
for _ in 0..2049 {
monitor.record_partial_event();
}
for _ in 0..53 {
monitor.record_parsed();
}
let warning = monitor.check_and_warn(colors);
assert!(
warning.is_none(),
"Should not warn with high percentage of partial events"
);
}
#[test]
fn test_health_monitor_warning_only_for_parse_errors() {
use super::health::HealthMonitor;
let monitor = HealthMonitor::new("test");
let colors = Colors { enabled: false };
for _ in 0..1000 {
monitor.record_partial_event();
}
for _ in 0..500 {
monitor.record_control_event();
}
for _ in 0..50 {
monitor.record_parsed();
}
let warning = monitor.check_and_warn(colors);
assert!(
warning.is_none(),
"Should not warn with mix of partial, control, and parsed events"
);
let monitor = HealthMonitor::new("claude");
for _ in 0..60 {
monitor.record_parse_error();
}
for _ in 0..40 {
monitor.record_parsed();
}
let warning = monitor.check_and_warn(colors);
assert!(warning.is_some(), "Should warn with >50% parse errors");
assert!(warning.unwrap().contains("parse errors"));
}