#[cfg(test)]
#[test]
fn test_consecutive_duplicate_detection_drops_resend_glitch() {
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_lines = [
r#"{"type":"stream_event","event":{"type":"message_start"}}"#.to_string(),
r#"{"type":"stream_event","event":{"type":"content_block_start","index":0,"content_block":{"type":"text","text":""}}}"#.to_string(),
r#"{"type":"stream_event","event":{"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"Repeated delta"}}}"#.to_string(),
r#"{"type":"stream_event","event":{"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"Repeated delta"}}}"#.to_string(),
r#"{"type":"stream_event","event":{"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"Repeated delta"}}}"#.to_string(),
r#"{"type":"stream_event","event":{"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"Repeated delta"}}}"#.to_string(),
r#"{"type":"stream_event","event":{"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"Repeated delta"}}}"#.to_string(),
r#"{"type":"stream_event","event":{"type":"message_stop"}}"#.to_string(),
];
let input = input_lines.join("\n");
let reader = Cursor::new(input);
parser
.parse_stream(reader, &MemoryWorkspace::new_test())
.unwrap();
let printer_ref = test_printer.borrow();
let output = printer_ref.get_output();
let delta_count = output.matches("Repeated delta").count();
assert_eq!(
delta_count, 1,
"Consecutive duplicate detection behavior: only first occurrence appears in output. Found {delta_count} occurrences. Output: {output:?}"
);
}
#[cfg(test)]
#[test]
fn test_consecutive_duplicate_counter_resets_on_different_delta() {
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_lines = [
r#"{"type":"stream_event","event":{"type":"message_start"}}"#.to_string(),
r#"{"type":"stream_event","event":{"type":"content_block_start","index":0,"content_block":{"type":"text","text":""}}}"#.to_string(),
r#"{"type":"stream_event","event":{"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"First"}}}"#.to_string(),
r#"{"type":"stream_event","event":{"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"First"}}}"#.to_string(),
r#"{"type":"stream_event","event":{"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"Second"}}}"#.to_string(),
r#"{"type":"stream_event","event":{"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"First"}}}"#.to_string(),
r#"{"type":"stream_event","event":{"type":"message_stop"}}"#.to_string(),
];
let input = input_lines.join("\n");
let reader = Cursor::new(input);
parser
.parse_stream(reader, &MemoryWorkspace::new_test())
.unwrap();
let printer_ref = test_printer.borrow();
let output = printer_ref.get_output();
let first_count = output.matches("First").count();
let second_count = output.matches("Second").count();
assert_eq!(
first_count, 2,
"Found {first_count} occurrences of 'First'. Output: {output:?}"
);
assert_eq!(
second_count, 1,
"Found {second_count} occurrences of 'Second'. Output: {output:?}"
);
}
#[cfg(test)]
#[test]
fn test_consecutive_duplicate_allows_legitimate_repetition() {
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_lines = [
r#"{"type":"stream_event","event":{"type":"message_start"}}"#.to_string(),
r#"{"type":"stream_event","event":{"type":"content_block_start","index":0,"content_block":{"type":"text","text":""}}}"#.to_string(),
r#"{"type":"stream_event","event":{"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"Hello"}}}"#.to_string(),
r#"{"type":"stream_event","event":{"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":" World"}}}"#.to_string(),
r#"{"type":"stream_event","event":{"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"!"}}}"#.to_string(),
r#"{"type":"stream_event","event":{"type":"message_stop"}}"#.to_string(),
];
let input = input_lines.join("\n");
let reader = Cursor::new(input);
parser
.parse_stream(reader, &MemoryWorkspace::new_test())
.unwrap();
let printer_ref = test_printer.borrow();
let output = printer_ref.get_output();
assert!(
output.contains("Hello World!"),
"Legitimate streaming content should not be affected by consecutive duplicate detection. Output: {output:?}"
);
}