#[test]
fn test_is_likely_snapshot_detects_snapshot() {
let mut session = StreamingSession::new();
session.on_message_start();
session.on_content_block_start(0);
let initial = "This is a long message that exceeds threshold";
session.on_text_delta(0, initial);
let snapshot = format!("{initial} plus new content");
let is_snapshot = session.is_likely_snapshot(&snapshot, "0");
assert!(
is_snapshot,
"Should detect snapshot-as-delta with strong overlap"
);
}
#[test]
fn test_is_likely_snapshot_returns_false_for_genuine_delta() {
let mut session = StreamingSession::new();
session.on_message_start();
session.on_content_block_start(0);
session.on_text_delta(0, "Hello");
let is_snapshot = session.is_likely_snapshot(" World", "0");
assert!(
!is_snapshot,
"Genuine delta should not be flagged as snapshot"
);
}
#[test]
fn test_is_likely_snapshot_returns_false_when_no_previous_content() {
let mut session = StreamingSession::new();
session.on_message_start();
session.on_content_block_start(0);
let is_snapshot = session.is_likely_snapshot("Hello", "0");
assert!(
!is_snapshot,
"First delta should not be flagged as snapshot"
);
}
#[test]
fn test_extract_delta_from_snapshot() {
let mut session = StreamingSession::new();
session.on_message_start();
session.on_content_block_start(0);
let initial = "This is a long message that exceeds threshold";
session.on_text_delta(0, initial);
let snapshot = format!("{initial} plus new content");
let delta = session.get_delta_from_snapshot(&snapshot, "0").unwrap();
assert_eq!(delta, " plus new content");
}
#[test]
fn test_extract_delta_from_snapshot_empty_delta() {
let mut session = StreamingSession::new();
session.on_message_start();
session.on_content_block_start(0);
session.on_text_delta(0, "Hello");
let delta = session.get_delta_from_snapshot("Hello", "0").unwrap();
assert_eq!(delta, "");
}
#[test]
fn test_extract_delta_from_snapshot_returns_error_on_non_snapshot() {
let mut session = StreamingSession::new();
session.on_message_start();
session.on_content_block_start(0);
session.on_text_delta(0, "Hello");
let result = session.get_delta_from_snapshot("World", "0");
assert_eq!(
result,
Err(SnapshotDeltaError::NonSnapshot {
key: "0".to_string(),
text: "World".to_string(),
})
);
}
#[test]
fn test_snapshot_detection_with_string_keys() {
let mut session = StreamingSession::new();
session.on_message_start();
let initial = "This is a long message that exceeds threshold";
session.on_text_delta_key("main", initial);
let snapshot = format!("{initial} plus new content");
let is_snapshot = session.is_likely_snapshot(&snapshot, "main");
assert!(
is_snapshot,
"Should detect snapshot with string keys when thresholds are met"
);
let delta = session.get_delta_from_snapshot(&snapshot, "main").unwrap();
assert_eq!(delta, " plus new content");
}
#[test]
fn test_snapshot_extraction_exact_match() {
let mut session = StreamingSession::new();
session.on_message_start();
let initial = "This is a long message that exceeds threshold";
session.on_text_delta(0, initial);
let exact_match = format!("{initial} World");
let delta1 = session.get_delta_from_snapshot(&exact_match, "0").unwrap();
assert_eq!(delta1, " World");
}
#[test]
fn test_snapshot_in_token_stream() {
let mut session = StreamingSession::new();
session.on_message_start();
let initial = "This is a long message that exceeds threshold";
session.on_text_delta(0, initial);
session.on_text_delta(0, " with more content");
let accumulated = session
.get_accumulated(ContentType::Text, "0")
.unwrap()
.to_string();
let snapshot = format!("{accumulated}! This is additional content");
assert!(
session.is_likely_snapshot(&snapshot, "0"),
"Should detect snapshot in token stream with strong overlap"
);
let delta = session.get_delta_from_snapshot(&snapshot, "0").unwrap();
assert!(delta.contains("! This is additional content"));
session.on_text_delta(0, delta);
let expected = format!("{accumulated}! This is additional content");
assert_eq!(
session.get_accumulated(ContentType::Text, "0"),
Some(expected.as_str())
);
}