| 1 | use chrono::{DateTime, TimeZone, Utc}; |
| 2 | use store::{ |
| 3 | AgentProposalMention, AgentProposalRecord, AgentReactionRecord, AttachmentImage, MessageRecord, |
| 4 | Store, |
| 5 | }; |
| 6 | |
| 7 | fn at(minute: u32) -> DateTime<Utc> { |
| 8 | Utc.with_ymd_and_hms(2026, 10, 6, 9, minute, 0).unwrap() |
| 9 | } |
| 10 | |
| 11 | fn message(conversation_id: &str, message_id: &str, minute: u32, html: &str) -> MessageRecord { |
| 12 | MessageRecord { |
| 13 | conversation_id: conversation_id.to_owned(), |
| 14 | message_id: message_id.to_owned(), |
| 15 | sender_id: Some("user-ada".to_owned()), |
| 16 | sender_name: Some("Ada Example".to_owned()), |
| 17 | created_at: at(minute), |
| 18 | body_html: html.to_owned(), |
| 19 | attachments_json: "[]".to_owned(), |
| 20 | reactions_json: "[]".to_owned(), |
| 21 | mentions_json: "[]".to_owned(), |
| 22 | links_json: "[]".to_owned(), |
| 23 | ..MessageRecord::default() |
| 24 | } |
| 25 | } |
| 26 | |
| 27 | fn proposal(id: &str, conversation_id: &str, minute: u32) -> AgentProposalRecord { |
| 28 | AgentProposalRecord { |
| 29 | id: id.to_owned(), |
| 30 | conversation_id: conversation_id.to_owned(), |
| 31 | reply_to_message_id: None, |
| 32 | text: format!("text {id}"), |
| 33 | mentions: Vec::new(), |
| 34 | images: Vec::new(), |
| 35 | agent: Some("claude".to_owned()), |
| 36 | session_id: None, |
| 37 | created_at: at(minute), |
| 38 | state: "pending".to_owned(), |
| 39 | resolved_at: None, |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | #[test] |
| 44 | fn search_in_filters_before_the_limit_applies() { |
| 45 | let store = Store::open_in_memory().unwrap(); |
| 46 | store |
| 47 | .upsert_messages(&[ |
| 48 | message("c1", "m1", 1, "<p>budget one</p>"), |
| 49 | message("c2", "m2", 2, "<p>budget two</p>"), |
| 50 | message("c2", "m3", 3, "<p>budget three</p>"), |
| 51 | ]) |
| 52 | .unwrap(); |
| 53 | let hits = store |
| 54 | .search_messages_in("budget", &["c1".to_owned()], 1) |
| 55 | .unwrap(); |
| 56 | assert_eq!(hits.len(), 1); |
| 57 | assert_eq!(hits[0].message_id, "m1"); |
| 58 | assert!( |
| 59 | store |
| 60 | .search_messages_in("budget", &[], 10) |
| 61 | .unwrap() |
| 62 | .is_empty() |
| 63 | ); |
| 64 | } |
| 65 | |
| 66 | #[test] |
| 67 | fn messages_after_pages_forward_oldest_first() { |
| 68 | let store = Store::open_in_memory().unwrap(); |
| 69 | store |
| 70 | .upsert_messages(&[ |
| 71 | message("c1", "m1", 1, "<p>a</p>"), |
| 72 | message("c1", "m2", 2, "<p>b</p>"), |
| 73 | message("c1", "m3", 3, "<p>c</p>"), |
| 74 | message("c2", "m4", 4, "<p>d</p>"), |
| 75 | ]) |
| 76 | .unwrap(); |
| 77 | let page = store.messages_after("c1", at(1), 1).unwrap(); |
| 78 | assert_eq!(page.len(), 1); |
| 79 | assert_eq!(page[0].message_id, "m2"); |
| 80 | assert_eq!(store.messages_after("c1", at(1), 10).unwrap().len(), 2); |
| 81 | } |
| 82 | |
| 83 | #[test] |
| 84 | fn proposals_persist_and_only_pending_ones_are_listed() { |
| 85 | let store = Store::open_in_memory().unwrap(); |
| 86 | store |
| 87 | .insert_agent_proposal(&proposal("p2", "c1", 2)) |
| 88 | .unwrap(); |
| 89 | store |
| 90 | .insert_agent_proposal(&proposal("p1", "c2", 1)) |
| 91 | .unwrap(); |
| 92 | let pending = store.pending_agent_proposals().unwrap(); |
| 93 | assert_eq!( |
| 94 | pending |
| 95 | .iter() |
| 96 | .map(|record| record.id.as_str()) |
| 97 | .collect::<Vec<_>>(), |
| 98 | ["p1", "p2"] |
| 99 | ); |
| 100 | assert_eq!(pending[0], proposal("p1", "c2", 1)); |
| 101 | |
| 102 | assert!( |
| 103 | store |
| 104 | .set_agent_proposal_state("p1", "sent", Some(at(5))) |
| 105 | .unwrap() |
| 106 | ); |
| 107 | assert!( |
| 108 | !store |
| 109 | .set_agent_proposal_state("nope", "sent", None) |
| 110 | .unwrap() |
| 111 | ); |
| 112 | let pending = store.pending_agent_proposals().unwrap(); |
| 113 | assert_eq!(pending.len(), 1); |
| 114 | assert_eq!(pending[0].id, "p2"); |
| 115 | } |
| 116 | |
| 117 | #[test] |
| 118 | fn proposal_mentions_and_images_survive_a_restart_and_images_go_when_resolved() { |
| 119 | let store = Store::open_in_memory().unwrap(); |
| 120 | let image = AttachmentImage { |
| 121 | name: "image-1.png".to_owned(), |
| 122 | format: "image/png".to_owned(), |
| 123 | bytes: vec![1, 2, 3], |
| 124 | width: None, |
| 125 | height: None, |
| 126 | }; |
| 127 | let rich = AgentProposalRecord { |
| 128 | mentions: vec![AgentProposalMention { |
| 129 | user_id: "u1".to_owned(), |
| 130 | name: "Ada Example".to_owned(), |
| 131 | }], |
| 132 | images: vec![image], |
| 133 | ..proposal("p1", "c1", 1) |
| 134 | }; |
| 135 | store.insert_agent_proposal(&rich).unwrap(); |
| 136 | assert_eq!(store.pending_agent_proposals().unwrap(), [rich.clone()]); |
| 137 | |
| 138 | store |
| 139 | .set_agent_proposal_state("p1", "sent", Some(at(2))) |
| 140 | .unwrap(); |
| 141 | store |
| 142 | .insert_agent_proposal(&AgentProposalRecord { |
| 143 | id: "p1".to_owned() + "-again", |
| 144 | ..rich |
| 145 | }) |
| 146 | .unwrap(); |
| 147 | let pending = store.pending_agent_proposals().unwrap(); |
| 148 | assert_eq!(pending.len(), 1); |
| 149 | assert_eq!(pending[0].images.len(), 1); |
| 150 | assert_eq!(pending[0].mentions.len(), 1); |
| 151 | } |
| 152 | |
| 153 | fn reaction(id: &str, message_id: &str, minute: u32) -> AgentReactionRecord { |
| 154 | AgentReactionRecord { |
| 155 | id: id.to_owned(), |
| 156 | conversation_id: "c1".to_owned(), |
| 157 | message_id: message_id.to_owned(), |
| 158 | emoji: "\u{1F44D}".to_owned(), |
| 159 | agent: None, |
| 160 | session_id: None, |
| 161 | created_at: at(minute), |
| 162 | state: "pending".to_owned(), |
| 163 | resolved_at: None, |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | #[test] |
| 168 | fn reaction_proposals_persist_until_resolved() { |
| 169 | let store = Store::open_in_memory().unwrap(); |
| 170 | store |
| 171 | .insert_agent_reaction(&reaction("r2", "m2", 2)) |
| 172 | .unwrap(); |
| 173 | store |
| 174 | .insert_agent_reaction(&reaction("r1", "m1", 1)) |
| 175 | .unwrap(); |
| 176 | let pending = store.pending_agent_reactions().unwrap(); |
| 177 | assert_eq!(pending[0], reaction("r1", "m1", 1)); |
| 178 | assert_eq!(pending.len(), 2); |
| 179 | assert!( |
| 180 | store |
| 181 | .set_agent_reaction_state("r1", "accepted", Some(at(3))) |
| 182 | .unwrap() |
| 183 | ); |
| 184 | let pending = store.pending_agent_reactions().unwrap(); |
| 185 | assert_eq!(pending.len(), 1); |
| 186 | assert_eq!(pending[0].id, "r2"); |
| 187 | } |
| 188 | |
| 189 | #[test] |
| 190 | fn reaction_state_lists_only_the_senders_undeleted_messages_newest_first() { |
| 191 | let store = Store::open_in_memory().unwrap(); |
| 192 | let own = |id: &str, minute: u32, reactions: &str| MessageRecord { |
| 193 | sender_id: Some("me".to_owned()), |
| 194 | reactions_json: reactions.to_owned(), |
| 195 | ..message("c1", id, minute, "<p>x</p>") |
| 196 | }; |
| 197 | let mut deleted = own("m3", 3, "[]"); |
| 198 | deleted.deleted = true; |
| 199 | store |
| 200 | .upsert_messages(&[ |
| 201 | own("m1", 1, "[1]"), |
| 202 | own("m2", 2, "[2]"), |
| 203 | deleted, |
| 204 | message("c1", "m4", 4, "<p>other</p>"), |
| 205 | ]) |
| 206 | .unwrap(); |
| 207 | let state = store.reaction_state_of_sender("c1", "me", 10).unwrap(); |
| 208 | assert_eq!( |
| 209 | state, |
| 210 | [ |
| 211 | ("m2".to_owned(), "[2]".to_owned()), |
| 212 | ("m1".to_owned(), "[1]".to_owned()) |
| 213 | ] |
| 214 | ); |
| 215 | assert_eq!( |
| 216 | store.reaction_state_of_sender("c1", "me", 1).unwrap().len(), |
| 217 | 1 |
| 218 | ); |
| 219 | } |
| 220 | |
| 221 | #[test] |
| 222 | fn conversation_people_reads_exact_mention_ids_and_survives_bad_json() { |
| 223 | let store = Store::open_in_memory().unwrap(); |
| 224 | let with_mentions = |id: &str, minute: u32, json: &str| MessageRecord { |
| 225 | sender_id: None, |
| 226 | sender_name: None, |
| 227 | mentions_json: json.to_owned(), |
| 228 | ..message("c1", id, minute, "<p>hi</p>") |
| 229 | }; |
| 230 | store |
| 231 | .upsert_messages(&[ |
| 232 | with_mentions("m1", 1, r#"[{"user_id":"user-bobby","name":"Bobby"}]"#), |
| 233 | with_mentions("m2", 2, "not json"), |
| 234 | with_mentions("m3", 3, r#"[{"user_id":"user-cy"}]"#), |
| 235 | ]) |
| 236 | .unwrap(); |
| 237 | assert_eq!( |
| 238 | store.conversation_people("c1", 100).unwrap(), |
| 239 | [("user-bobby".to_owned(), "Bobby".to_owned())] |
| 240 | ); |
| 241 | } |
| 242 | |
| 243 | #[test] |
| 244 | fn conversation_people_lists_senders_and_mentioned_names_once() { |
| 245 | let store = Store::open_in_memory().unwrap(); |
| 246 | let mentioning = MessageRecord { |
| 247 | mentions_json: r#"[{"user_id":"user-bob","name":"Bob Sample"},{"user_id":"user-ada","name":"Ada Example"}]"#.to_owned(), |
| 248 | ..message("c1", "m2", 2, "<p>hi</p>") |
| 249 | }; |
| 250 | store |
| 251 | .upsert_messages(&[message("c1", "m1", 1, "<p>hi</p>"), mentioning]) |
| 252 | .unwrap(); |
| 253 | let mut people = store.conversation_people("c1", 100).unwrap(); |
| 254 | people.sort(); |
| 255 | assert_eq!( |
| 256 | people, |
| 257 | [ |
| 258 | ("user-ada".to_owned(), "Ada Example".to_owned()), |
| 259 | ("user-bob".to_owned(), "Bob Sample".to_owned()) |
| 260 | ] |
| 261 | ); |
| 262 | assert!(store.conversation_people("c2", 100).unwrap().is_empty()); |
| 263 | } |
| 264 | |
| 265 | #[test] |
| 266 | fn conversation_people_only_scans_the_newest_messages() { |
| 267 | let store = Store::open_in_memory().unwrap(); |
| 268 | let sender = |id: &str, minute: u32, user: &str| MessageRecord { |
| 269 | sender_id: Some(user.to_owned()), |
| 270 | sender_name: Some(user.to_owned()), |
| 271 | ..message("c1", id, minute, "<p>x</p>") |
| 272 | }; |
| 273 | store |
| 274 | .upsert_messages(&[sender("m1", 1, "old"), sender("m2", 2, "new")]) |
| 275 | .unwrap(); |
| 276 | assert_eq!( |
| 277 | store.conversation_people("c1", 1).unwrap(), |
| 278 | [("new".to_owned(), "new".to_owned())] |
| 279 | ); |
| 280 | } |