use super::*; fn message(id: &str, body: &str, created_at: &str) -> DeliveredMessage { DeliveredMessage { id: id.to_string(), body: body.to_string(), created_at: created_at.to_string(), } } #[test] fn a_turn_is_the_echo_of_the_same_words_whatever_the_line_endings() { assert!(same_words( "Plan the day\nthen the week", "Plan the day\r\nthen the week " )); assert!(same_words(" Second thing\n", "Second thing")); assert!(!same_words("Second thing, and more", "Second thing")); assert!(!same_words(" ", "Second thing")); } #[test] fn a_clipped_turn_matches_the_head_of_a_long_message() { let long = "word ".repeat(3000); let clipped: String = long.chars().take(TURN_TEXT_CAP).collect(); assert!(same_words(&clipped, &long)); // A short turn is never matched as a prefix. assert!(!same_words("word word", &long)); } #[test] fn control_characters_never_reach_the_terminal() { assert_eq!(typed_text("a\x1b[201~b\rc\td\r\n\n"), "a[201~b\nc\td"); } #[test] fn the_query_asks_for_every_typed_kind() { for kind in TYPED_KINDS { assert!( DELIVERED_MESSAGES_SQL.contains(&format!("'{kind}'")), "{kind}" ); } } /// A session's turns played through [`absorbing`] the way a writer does: a /// kept turn moves `after` and ends the window, an absorbed one moves `past`. /// Answers the user turns that were KEPT. fn kept_user_turns(turns: &[(&str, &str, &str)], messages: &[DeliveredMessage]) -> Vec { let mut after: Option = None; let mut past: Option = None; let mut kept = Vec::new(); for (role, text, ts) in turns { if *role == "user" { if let Some(message) = absorbing(text, after.as_deref(), past.as_ref(), messages) { past = Some(message.clone()); continue; } kept.push(format!("{text}@{ts}")); } after = Some((*ts).to_string()); past = None; } kept } /// RULE: a delivered message absorbs at most one turn — two identical "yes" messages absorb exactly two turns, and a third "yes" typed at the Mac is kept. #[test] fn two_yes_messages_absorb_two_turns_and_a_third_yes_is_kept() { let messages = [ message("m1", "yes", "2026-01-01T00:00:01.000Z"), message("m2", "yes", "2026-01-01T00:00:05.000Z"), ]; let turns = [ ("user", "yes", "2026-01-01T00:00:02.000Z"), ("assistant", "Done the first.", "2026-01-01T00:00:03.000Z"), ("user", "yes", "2026-01-01T00:00:06.000Z"), ("assistant", "Done the second.", "2026-01-01T00:00:07.000Z"), ("user", "yes", "2026-01-01T00:00:08.000Z"), ]; assert_eq!( kept_user_turns(&turns, &messages), vec!["yes@2026-01-01T00:00:08.000Z".to_string()], "the third yes was typed at the Mac; both messages were already spent" ); } /// RULE: two identical messages typed back to back absorb two turns in one window, and a third identical turn in that window is kept. #[test] fn two_messages_in_one_window_absorb_two_turns_not_one_twice() { let messages = [ message("m1", "yes", "2026-01-01T00:00:01.000Z"), message("m2", "yes", "2026-01-01T00:00:01.500Z"), ]; let turns = [ ("user", "yes", "2026-01-01T00:00:02Z"), ("user", "yes", "2026-01-01T00:00:03Z"), ("user", "yes", "2026-01-01T00:00:04Z"), ]; assert_eq!( kept_user_turns(&turns, &messages), vec!["yes@2026-01-01T00:00:04Z".to_string()] ); } /// RULE: a message sent before the session's last kept turn is spent — its words typed at the Mac later are kept. #[test] fn a_message_older_than_the_last_kept_turn_absorbs_nothing() { let messages = [message("m1", "yes", "2026-01-01T00:00:01.000Z")]; assert!(absorbing("yes", Some("2026-01-01T00:00:02Z"), None, &messages).is_none()); assert_eq!( absorbing("yes", Some("2026-01-01T00:00:00.500Z"), None, &messages).map(|m| m.id.as_str()), Some("m1") ); } #[test] fn a_turn_with_other_words_takes_no_message() { let messages = [message("m1", "yes", "2026-01-01T00:00:01.000Z")]; assert!(absorbing("no", None, None, &messages).is_none()); }