| 1 | use chrono::{DateTime, TimeZone, Utc}; |
| 2 | use rusqlite::Connection; |
| 3 | use store::{ |
| 4 | AvatarRecord, ChannelLayoutRecord, ChannelMentions, ChannelNotificationLevel, |
| 5 | ChannelNotifications, ChannelRecord, ChannelTabRecord, ChatPreview, ChatRecord, FolderRecord, |
| 6 | MemberRecord, MessageImportance, MessageRecord, ReadHorizonRecord, RecentScope, Store, |
| 7 | SyncState, TeamLayoutRecord, TeamRecord, UNKNOWN_USER_NAME, |
| 8 | }; |
| 9 | |
| 10 | #[test] |
| 11 | fn a_system_event_keeps_the_previous_unread_state() { |
| 12 | let store = Store::open_in_memory().unwrap(); |
| 13 | for (previous, expected) in [(true, true), (false, false)] { |
| 14 | let id = format!("chat-{previous}"); |
| 15 | let mut seeded = chat(&id, "Planning", Some(at(5))); |
| 16 | seeded.unread = previous; |
| 17 | store.upsert_chats(&[seeded]).unwrap(); |
| 18 | |
| 19 | let mut event = chat(&id, "Planning", Some(at(12))); |
| 20 | event.unread = false; |
| 21 | event.last_event_system = true; |
| 22 | store.upsert_chats(&[event]).unwrap(); |
| 23 | assert_eq!(store.chat(&id).unwrap().unwrap().unread, expected); |
| 24 | } |
| 25 | } |
| 26 | |
| 27 | #[test] |
| 28 | fn a_call_event_keeps_the_previous_preview() { |
| 29 | let store = Store::open_in_memory().unwrap(); |
| 30 | let mut seeded = chat("chat-call", "Planning", Some(at(5))); |
| 31 | seeded.last_message_preview = Some("See you there".to_owned()); |
| 32 | seeded.last_message_sender_id = Some("user-ada".to_owned()); |
| 33 | seeded.last_message_sender_name = Some("Ada Example".to_owned()); |
| 34 | store.upsert_chats(&[seeded]).unwrap(); |
| 35 | |
| 36 | let mut call = chat("chat-call", "Planning", Some(at(12))); |
| 37 | call.last_event_system = true; |
| 38 | call.last_event_call = true; |
| 39 | store.upsert_chats(&[call]).unwrap(); |
| 40 | let stored = store.chat("chat-call").unwrap().unwrap(); |
| 41 | assert_eq!( |
| 42 | stored.last_message_preview.as_deref(), |
| 43 | Some("See you there") |
| 44 | ); |
| 45 | assert_eq!( |
| 46 | stored.last_message_sender_name.as_deref(), |
| 47 | Some("Ada Example") |
| 48 | ); |
| 49 | assert_eq!(stored.last_message_at, Some(at(12))); |
| 50 | |
| 51 | let mut recording = chat("chat-call", "Planning", Some(at(14))); |
| 52 | recording.last_event_system = true; |
| 53 | recording.last_message_preview = Some("Recording is ready".to_owned()); |
| 54 | store.upsert_chats(&[recording]).unwrap(); |
| 55 | let stored = store.chat("chat-call").unwrap().unwrap(); |
| 56 | assert_eq!( |
| 57 | stored.last_message_preview.as_deref(), |
| 58 | Some("Recording is ready") |
| 59 | ); |
| 60 | assert_eq!(stored.last_message_sender_name, None); |
| 61 | } |
| 62 | |
| 63 | fn at(minute: u32) -> DateTime<Utc> { |
| 64 | Utc.with_ymd_and_hms(2026, 10, 6, 9, minute, 0).unwrap() |
| 65 | } |
| 66 | |
| 67 | fn chat(id: &str, title: &str, last: Option<DateTime<Utc>>) -> ChatRecord { |
| 68 | ChatRecord { |
| 69 | id: id.to_owned(), |
| 70 | kind: "group".to_owned(), |
| 71 | title: title.to_owned(), |
| 72 | member_summary: "Ada Example, Bob Sample".to_owned(), |
| 73 | last_message_at: last, |
| 74 | last_read_at: Some(at(0)), |
| 75 | unread: true, |
| 76 | members: vec![ |
| 77 | MemberRecord { |
| 78 | user_id: Some("user-ada".to_owned()), |
| 79 | display_name: "Ada Example".to_owned(), |
| 80 | }, |
| 81 | MemberRecord { |
| 82 | user_id: None, |
| 83 | display_name: "Bob Sample".to_owned(), |
| 84 | }, |
| 85 | ], |
| 86 | ..ChatRecord::default() |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | fn message(conversation_id: &str, message_id: &str, minute: u32) -> MessageRecord { |
| 91 | MessageRecord { |
| 92 | conversation_id: conversation_id.to_owned(), |
| 93 | message_id: message_id.to_owned(), |
| 94 | reply_to_id: None, |
| 95 | sender_id: Some("user-ada".to_owned()), |
| 96 | sender_name: Some("Ada Example".to_owned()), |
| 97 | created_at: at(minute), |
| 98 | edited_at: None, |
| 99 | deleted: false, |
| 100 | body_html: format!("<p>message {message_id}</p>"), |
| 101 | attachments_json: "[]".to_owned(), |
| 102 | reactions_json: "[]".to_owned(), |
| 103 | mentions_json: "[]".to_owned(), |
| 104 | sender_application_id: None, |
| 105 | links_json: "[]".to_owned(), |
| 106 | subject: None, |
| 107 | call_event_json: None, |
| 108 | importance: MessageImportance::Normal, |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | #[test] |
| 113 | fn importance_round_trips_and_survives_an_upsert_without_one() { |
| 114 | let store = Store::open_in_memory().unwrap(); |
| 115 | let urgent = MessageRecord { |
| 116 | importance: MessageImportance::Urgent, |
| 117 | ..message("c1", "m1", 1) |
| 118 | }; |
| 119 | store |
| 120 | .upsert_messages(std::slice::from_ref(&urgent)) |
| 121 | .unwrap(); |
| 122 | let found = store.messages_by_id("c1", &["m1".to_owned()]).unwrap(); |
| 123 | assert_eq!(found["m1"].importance, MessageImportance::Urgent); |
| 124 | |
| 125 | store.upsert_messages(&[message("c1", "m1", 1)]).unwrap(); |
| 126 | let found = store.messages_by_id("c1", &["m1".to_owned()]).unwrap(); |
| 127 | assert_eq!(found["m1"].importance, MessageImportance::Urgent); |
| 128 | } |
| 129 | |
| 130 | #[test] |
| 131 | fn unread_marker_follows_the_last_message_from_others() { |
| 132 | let store = Store::open_in_memory().unwrap(); |
| 133 | let important = MessageRecord { |
| 134 | importance: MessageImportance::High, |
| 135 | ..message("c1", "m2", 2) |
| 136 | }; |
| 137 | store |
| 138 | .upsert_messages(&[message("c1", "m1", 1), important]) |
| 139 | .unwrap(); |
| 140 | let marker = |after: Option<u32>, me: &str| { |
| 141 | store |
| 142 | .unread_last_message_importance("c1", after.map(at), me) |
| 143 | .unwrap() |
| 144 | }; |
| 145 | assert_eq!(marker(None, "me"), MessageImportance::High); |
| 146 | assert_eq!(marker(Some(1), "me"), MessageImportance::High); |
| 147 | assert_eq!(marker(Some(2), "me"), MessageImportance::Normal); |
| 148 | assert_eq!(marker(None, "user-ada"), MessageImportance::Normal); |
| 149 | |
| 150 | store.upsert_messages(&[message("c1", "m3", 3)]).unwrap(); |
| 151 | assert_eq!(marker(None, "me"), MessageImportance::Normal); |
| 152 | } |
| 153 | |
| 154 | #[test] |
| 155 | fn subject_round_trips_and_survives_an_upsert_without_one() { |
| 156 | let store = Store::open_in_memory().unwrap(); |
| 157 | let post = MessageRecord { |
| 158 | subject: Some("Release plan".to_owned()), |
| 159 | ..message("c1", "m1", 1) |
| 160 | }; |
| 161 | store.upsert_messages(std::slice::from_ref(&post)).unwrap(); |
| 162 | let found = store.messages_by_id("c1", &["m1".to_owned()]).unwrap(); |
| 163 | assert_eq!(found["m1"], post); |
| 164 | |
| 165 | store.upsert_messages(&[message("c1", "m1", 1)]).unwrap(); |
| 166 | let found = store.messages_by_id("c1", &["m1".to_owned()]).unwrap(); |
| 167 | assert_eq!(found["m1"].subject.as_deref(), Some("Release plan")); |
| 168 | } |
| 169 | |
| 170 | #[test] |
| 171 | fn thread_messages_return_root_and_replies_oldest_first_beyond_any_page() { |
| 172 | let store = Store::open_in_memory().unwrap(); |
| 173 | let reply = |id: &str, minute: u32, root: &str| MessageRecord { |
| 174 | reply_to_id: Some(root.to_owned()), |
| 175 | ..message("c1", id, minute) |
| 176 | }; |
| 177 | store |
| 178 | .upsert_messages(&[ |
| 179 | reply("r2", 30, "root"), |
| 180 | message("c1", "root", 1), |
| 181 | message("c1", "other", 2), |
| 182 | reply("r1", 10, "root"), |
| 183 | reply("o1", 11, "other"), |
| 184 | ]) |
| 185 | .unwrap(); |
| 186 | let thread = store.thread_messages("c1", "root").unwrap(); |
| 187 | assert_eq!(ids(&thread), ["root", "r1", "r2"]); |
| 188 | } |
| 189 | |
| 190 | #[test] |
| 191 | fn sender_application_round_trips() { |
| 192 | let store = Store::open_in_memory().unwrap(); |
| 193 | let bot_message = MessageRecord { |
| 194 | sender_application_id: Some("bot-guid".to_owned()), |
| 195 | ..message("c1", "m1", 1) |
| 196 | }; |
| 197 | store |
| 198 | .upsert_messages(std::slice::from_ref(&bot_message)) |
| 199 | .unwrap(); |
| 200 | let found = store.messages_by_id("c1", &["m1".to_owned()]).unwrap(); |
| 201 | assert_eq!(found["m1"], bot_message); |
| 202 | } |
| 203 | |
| 204 | #[test] |
| 205 | fn an_upsert_without_links_keeps_the_stored_links() { |
| 206 | let store = Store::open_in_memory().unwrap(); |
| 207 | store.upsert_messages(&[message("c1", "m1", 1)]).unwrap(); |
| 208 | let links = r#"[{"url":"https://a.example"}]"#; |
| 209 | assert!(store.set_message_links("c1", "m1", links).unwrap()); |
| 210 | assert!(!store.set_message_links("c1", "m1", links).unwrap()); |
| 211 | |
| 212 | let edited = MessageRecord { |
| 213 | body_html: "<p>edited</p>".to_owned(), |
| 214 | ..message("c1", "m1", 1) |
| 215 | }; |
| 216 | store.upsert_messages(&[edited]).unwrap(); |
| 217 | let found = store.messages_by_id("c1", &["m1".to_owned()]).unwrap(); |
| 218 | assert_eq!(found["m1"].links_json, links); |
| 219 | assert_eq!(found["m1"].body_html, "<p>edited</p>"); |
| 220 | |
| 221 | let replaced = MessageRecord { |
| 222 | links_json: r#"[{"url":"https://b.example"}]"#.to_owned(), |
| 223 | ..message("c1", "m1", 1) |
| 224 | }; |
| 225 | store |
| 226 | .upsert_messages(std::slice::from_ref(&replaced)) |
| 227 | .unwrap(); |
| 228 | let found = store.messages_by_id("c1", &["m1".to_owned()]).unwrap(); |
| 229 | assert_eq!(found["m1"].links_json, replaced.links_json); |
| 230 | } |
| 231 | |
| 232 | #[test] |
| 233 | fn new_messages_start_without_links() { |
| 234 | let store = Store::open_in_memory().unwrap(); |
| 235 | store.upsert_messages(&[message("c1", "m1", 1)]).unwrap(); |
| 236 | let found = store.messages_by_id("c1", &["m1".to_owned()]).unwrap(); |
| 237 | assert_eq!(found["m1"].links_json, "[]"); |
| 238 | } |
| 239 | |
| 240 | fn ids(messages: &[MessageRecord]) -> Vec<&str> { |
| 241 | messages |
| 242 | .iter() |
| 243 | .map(|message| message.message_id.as_str()) |
| 244 | .collect() |
| 245 | } |
| 246 | |
| 247 | #[test] |
| 248 | fn chats_round_trip_with_members() { |
| 249 | let store = Store::open_in_memory().unwrap(); |
| 250 | let original = chat("chat-1", "Planning", Some(at(5))); |
| 251 | store.upsert_chats(std::slice::from_ref(&original)).unwrap(); |
| 252 | assert_eq!(store.chat("chat-1").unwrap(), Some(original)); |
| 253 | assert_eq!(store.chat("missing").unwrap(), None); |
| 254 | } |
| 255 | |
| 256 | #[test] |
| 257 | fn chat_upsert_replaces_fields_and_members() { |
| 258 | let store = Store::open_in_memory().unwrap(); |
| 259 | store |
| 260 | .upsert_chats(&[chat("chat-1", "Old", Some(at(1)))]) |
| 261 | .unwrap(); |
| 262 | let mut changed = chat("chat-1", "New", Some(at(9))); |
| 263 | changed.members.truncate(1); |
| 264 | changed.unread = false; |
| 265 | store.upsert_chats(&[changed.clone()]).unwrap(); |
| 266 | assert_eq!(store.recent_chats(10).unwrap(), vec![changed]); |
| 267 | } |
| 268 | |
| 269 | #[test] |
| 270 | fn chat_upsert_keeps_a_real_title_over_the_only_you_fallback() { |
| 271 | let store = Store::open_in_memory().unwrap(); |
| 272 | store |
| 273 | .upsert_chats(&[chat("chat-1", "Sample Bot", Some(at(1)))]) |
| 274 | .unwrap(); |
| 275 | store |
| 276 | .upsert_chats(&[chat("chat-1", "(only you)", Some(at(2)))]) |
| 277 | .unwrap(); |
| 278 | assert_eq!(store.chat("chat-1").unwrap().unwrap().title, "Sample Bot"); |
| 279 | store |
| 280 | .upsert_chats(&[chat("chat-2", "(only you)", Some(at(2)))]) |
| 281 | .unwrap(); |
| 282 | store |
| 283 | .upsert_chats(&[chat("chat-2", "Sample Bot", Some(at(3)))]) |
| 284 | .unwrap(); |
| 285 | assert_eq!(store.chat("chat-2").unwrap().unwrap().title, "Sample Bot"); |
| 286 | } |
| 287 | |
| 288 | #[test] |
| 289 | fn recent_chats_are_newest_first_with_unknown_last() { |
| 290 | let store = Store::open_in_memory().unwrap(); |
| 291 | store |
| 292 | .upsert_chats(&[ |
| 293 | chat("old", "Old", Some(at(1))), |
| 294 | chat("none", "None", None), |
| 295 | chat("new", "New", Some(at(30))), |
| 296 | chat("mid", "Mid", Some(at(10))), |
| 297 | ]) |
| 298 | .unwrap(); |
| 299 | let recent = store.recent_chats(3).unwrap(); |
| 300 | let order: Vec<&str> = recent.iter().map(|chat| chat.id.as_str()).collect(); |
| 301 | assert_eq!(order, ["new", "mid", "old"]); |
| 302 | assert_eq!(store.recent_chats(10).unwrap().last().unwrap().id, "none"); |
| 303 | } |
| 304 | |
| 305 | #[test] |
| 306 | fn messages_page_backwards_oldest_first() { |
| 307 | let store = Store::open_in_memory().unwrap(); |
| 308 | let all: Vec<MessageRecord> = (1..=10) |
| 309 | .map(|minute| message("chat-1", &format!("m{minute:02}"), minute)) |
| 310 | .collect(); |
| 311 | store.upsert_messages(&all).unwrap(); |
| 312 | store |
| 313 | .upsert_messages(&[message("chat-2", "other", 5)]) |
| 314 | .unwrap(); |
| 315 | |
| 316 | let newest = store.messages("chat-1", None, 4).unwrap(); |
| 317 | assert_eq!(ids(&newest), ["m07", "m08", "m09", "m10"]); |
| 318 | |
| 319 | let older = store |
| 320 | .messages("chat-1", Some(newest[0].created_at), 4) |
| 321 | .unwrap(); |
| 322 | assert_eq!(ids(&older), ["m03", "m04", "m05", "m06"]); |
| 323 | |
| 324 | let oldest = store |
| 325 | .messages("chat-1", Some(older[0].created_at), 4) |
| 326 | .unwrap(); |
| 327 | assert_eq!(ids(&oldest), ["m01", "m02"]); |
| 328 | assert!( |
| 329 | store |
| 330 | .messages("chat-1", Some(oldest[0].created_at), 4) |
| 331 | .unwrap() |
| 332 | .is_empty() |
| 333 | ); |
| 334 | assert_eq!(store.message_count("chat-1").unwrap(), 10); |
| 335 | } |
| 336 | |
| 337 | #[test] |
| 338 | fn equal_timestamps_keep_a_stable_order() { |
| 339 | let store = Store::open_in_memory().unwrap(); |
| 340 | store |
| 341 | .upsert_messages(&[ |
| 342 | message("c", "b", 1), |
| 343 | message("c", "a", 1), |
| 344 | message("c", "c", 1), |
| 345 | ]) |
| 346 | .unwrap(); |
| 347 | assert_eq!( |
| 348 | ids(&store.messages("c", None, 10).unwrap()), |
| 349 | ["a", "b", "c"] |
| 350 | ); |
| 351 | } |
| 352 | |
| 353 | #[test] |
| 354 | fn message_upsert_updates_in_place() { |
| 355 | let store = Store::open_in_memory().unwrap(); |
| 356 | let mut edited = message("c", "m1", 1); |
| 357 | store |
| 358 | .upsert_messages(std::slice::from_ref(&edited)) |
| 359 | .unwrap(); |
| 360 | edited.body_html = "<p>edited</p>".to_owned(); |
| 361 | edited.edited_at = Some(at(2)); |
| 362 | edited.reactions_json = r#"[{"type":"like"}]"#.to_owned(); |
| 363 | store |
| 364 | .upsert_messages(std::slice::from_ref(&edited)) |
| 365 | .unwrap(); |
| 366 | |
| 367 | let stored = store.messages("c", None, 10).unwrap(); |
| 368 | assert_eq!(stored, vec![edited.clone()]); |
| 369 | let by_id = store |
| 370 | .messages_by_id("c", &["m1".to_owned(), "nope".to_owned()]) |
| 371 | .unwrap(); |
| 372 | assert_eq!(by_id.len(), 1); |
| 373 | assert_eq!(by_id["m1"], edited); |
| 374 | } |
| 375 | |
| 376 | #[test] |
| 377 | fn deleted_flag_and_json_columns_round_trip() { |
| 378 | let store = Store::open_in_memory().unwrap(); |
| 379 | let mut record = message("c", "m1", 1); |
| 380 | record.deleted = true; |
| 381 | record.reply_to_id = Some("m0".to_owned()); |
| 382 | record.attachments_json = r#"[{"name":"a.pdf"}]"#.to_owned(); |
| 383 | record.mentions_json = r#"[{"id":"u1","name":"Ada Example"}]"#.to_owned(); |
| 384 | store |
| 385 | .upsert_messages(std::slice::from_ref(&record)) |
| 386 | .unwrap(); |
| 387 | assert_eq!(store.messages("c", None, 1).unwrap(), vec![record]); |
| 388 | } |
| 389 | |
| 390 | #[test] |
| 391 | fn thread_has_sender_checks_root_and_replies() { |
| 392 | let store = Store::open_in_memory().unwrap(); |
| 393 | let mut reply = message("c", "r1", 2); |
| 394 | reply.reply_to_id = Some("root".to_owned()); |
| 395 | reply.sender_id = Some("user-bob".to_owned()); |
| 396 | let mut other_reply = message("c", "r2", 3); |
| 397 | other_reply.reply_to_id = Some("other".to_owned()); |
| 398 | other_reply.sender_id = Some("user-carl".to_owned()); |
| 399 | store |
| 400 | .upsert_messages(&[message("c", "root", 1), reply, other_reply]) |
| 401 | .unwrap(); |
| 402 | assert!(store.thread_has_sender("c", "root", "user-ada").unwrap()); |
| 403 | assert!(store.thread_has_sender("c", "root", "user-bob").unwrap()); |
| 404 | assert!(!store.thread_has_sender("c", "root", "user-carl").unwrap()); |
| 405 | assert!(!store.thread_has_sender("d", "root", "user-ada").unwrap()); |
| 406 | } |
| 407 | |
| 408 | #[test] |
| 409 | fn sync_state_round_trip() { |
| 410 | let store = Store::open_in_memory().unwrap(); |
| 411 | assert_eq!(store.sync_state("c").unwrap(), None); |
| 412 | let state = SyncState { |
| 413 | newest_seen: Some(at(9)), |
| 414 | oldest_loaded: Some(at(1)), |
| 415 | has_more: false, |
| 416 | older_cursor: Some("https://graph.microsoft.com/v1.0/next".to_owned()), |
| 417 | delta_link: Some("https://graph.microsoft.com/v1.0/delta".to_owned()), |
| 418 | }; |
| 419 | store.set_sync_state("c", &state).unwrap(); |
| 420 | assert_eq!(store.sync_state("c").unwrap(), Some(state)); |
| 421 | let reset = SyncState { |
| 422 | has_more: true, |
| 423 | ..SyncState::default() |
| 424 | }; |
| 425 | store.set_sync_state("c", &reset).unwrap(); |
| 426 | assert_eq!(store.sync_state("c").unwrap(), Some(reset)); |
| 427 | } |
| 428 | |
| 429 | #[test] |
| 430 | fn sidebar_groups_channels_under_teams() { |
| 431 | let store = Store::open_in_memory().unwrap(); |
| 432 | store |
| 433 | .upsert_chats(&[chat("chat-1", "Planning", Some(at(5)))]) |
| 434 | .unwrap(); |
| 435 | store |
| 436 | .upsert_teams(&[ |
| 437 | TeamRecord { |
| 438 | id: "t2".to_owned(), |
| 439 | name: "beta".to_owned(), |
| 440 | }, |
| 441 | TeamRecord { |
| 442 | id: "t1".to_owned(), |
| 443 | name: "Alpha".to_owned(), |
| 444 | }, |
| 445 | ]) |
| 446 | .unwrap(); |
| 447 | let channel = |id: &str, team_id: &str, name: &str| ChannelRecord { |
| 448 | id: id.to_owned(), |
| 449 | team_id: team_id.to_owned(), |
| 450 | name: name.to_owned(), |
| 451 | membership_type: Some("standard".to_owned()), |
| 452 | last_message_at: None, |
| 453 | unread: false, |
| 454 | }; |
| 455 | store |
| 456 | .upsert_channels(&[ |
| 457 | channel("c2", "t1", "Random"), |
| 458 | channel("c1", "t1", "General"), |
| 459 | channel("c3", "t2", "General"), |
| 460 | ]) |
| 461 | .unwrap(); |
| 462 | |
| 463 | let sidebar = store.sidebar().unwrap(); |
| 464 | assert_eq!(sidebar.chats.len(), 1); |
| 465 | let team_names: Vec<&str> = sidebar |
| 466 | .teams |
| 467 | .iter() |
| 468 | .map(|entry| entry.team.name.as_str()) |
| 469 | .collect(); |
| 470 | assert_eq!(team_names, ["Alpha", "beta"]); |
| 471 | let alpha_channels: Vec<&str> = sidebar.teams[0] |
| 472 | .channels |
| 473 | .iter() |
| 474 | .map(|entry| entry.name.as_str()) |
| 475 | .collect(); |
| 476 | assert_eq!(alpha_channels, ["General", "Random"]); |
| 477 | assert_eq!(store.channel("c3").unwrap().unwrap().team_id, "t2"); |
| 478 | } |
| 479 | |
| 480 | #[test] |
| 481 | fn channel_upsert_keeps_known_last_message_time() { |
| 482 | let store = Store::open_in_memory().unwrap(); |
| 483 | store |
| 484 | .upsert_teams(&[TeamRecord { |
| 485 | id: "t1".to_owned(), |
| 486 | name: "A".to_owned(), |
| 487 | }]) |
| 488 | .unwrap(); |
| 489 | let mut channel = ChannelRecord { |
| 490 | id: "c1".to_owned(), |
| 491 | team_id: "t1".to_owned(), |
| 492 | name: "General".to_owned(), |
| 493 | membership_type: None, |
| 494 | last_message_at: Some(at(7)), |
| 495 | unread: false, |
| 496 | }; |
| 497 | store |
| 498 | .upsert_channels(std::slice::from_ref(&channel)) |
| 499 | .unwrap(); |
| 500 | channel.last_message_at = None; |
| 501 | store |
| 502 | .upsert_channels(std::slice::from_ref(&channel)) |
| 503 | .unwrap(); |
| 504 | assert_eq!( |
| 505 | store.channel("c1").unwrap().unwrap().last_message_at, |
| 506 | Some(at(7)) |
| 507 | ); |
| 508 | } |
| 509 | |
| 510 | #[test] |
| 511 | fn channel_read_state_survives_a_metadata_refresh() { |
| 512 | let store = Store::open_in_memory().unwrap(); |
| 513 | store |
| 514 | .upsert_teams(&[TeamRecord { |
| 515 | id: "t1".to_owned(), |
| 516 | name: "A".to_owned(), |
| 517 | }]) |
| 518 | .unwrap(); |
| 519 | let channel = ChannelRecord { |
| 520 | id: "c1".to_owned(), |
| 521 | team_id: "t1".to_owned(), |
| 522 | name: "General".to_owned(), |
| 523 | membership_type: None, |
| 524 | last_message_at: None, |
| 525 | unread: false, |
| 526 | }; |
| 527 | store |
| 528 | .upsert_channels(std::slice::from_ref(&channel)) |
| 529 | .unwrap(); |
| 530 | store |
| 531 | .apply_channel_read_states(&[ |
| 532 | ("c1".to_owned(), true, Some(at(9))), |
| 533 | ("unknown".to_owned(), true, None), |
| 534 | ]) |
| 535 | .unwrap(); |
| 536 | store |
| 537 | .upsert_channels(std::slice::from_ref(&channel)) |
| 538 | .unwrap(); |
| 539 | let stored = store.channel("c1").unwrap().unwrap(); |
| 540 | assert!(stored.unread); |
| 541 | assert_eq!(stored.last_message_at, Some(at(9))); |
| 542 | assert!(store.channel("unknown").unwrap().is_none()); |
| 543 | |
| 544 | store.mark_channel_read("c1").unwrap(); |
| 545 | assert!(!store.channel("c1").unwrap().unwrap().unread); |
| 546 | } |
| 547 | |
| 548 | #[test] |
| 549 | fn pruning_removes_departed_teams_and_channels() { |
| 550 | let store = Store::open_in_memory().unwrap(); |
| 551 | store |
| 552 | .upsert_teams(&[ |
| 553 | TeamRecord { |
| 554 | id: "t1".to_owned(), |
| 555 | name: "A".to_owned(), |
| 556 | }, |
| 557 | TeamRecord { |
| 558 | id: "t2".to_owned(), |
| 559 | name: "B".to_owned(), |
| 560 | }, |
| 561 | ]) |
| 562 | .unwrap(); |
| 563 | let channel = |id: &str| ChannelRecord { |
| 564 | id: id.to_owned(), |
| 565 | team_id: "t1".to_owned(), |
| 566 | name: id.to_owned(), |
| 567 | membership_type: None, |
| 568 | last_message_at: None, |
| 569 | unread: false, |
| 570 | }; |
| 571 | store |
| 572 | .upsert_channels(&[channel("c1"), channel("c2")]) |
| 573 | .unwrap(); |
| 574 | assert_eq!( |
| 575 | store |
| 576 | .remove_channels_except("t1", &["c1".to_owned()]) |
| 577 | .unwrap(), |
| 578 | 1 |
| 579 | ); |
| 580 | assert_eq!(store.remove_teams_except(&["t1".to_owned()]).unwrap(), 1); |
| 581 | let sidebar = store.sidebar().unwrap(); |
| 582 | assert_eq!(sidebar.teams.len(), 1); |
| 583 | assert_eq!(sidebar.teams[0].channels.len(), 1); |
| 584 | } |
| 585 | |
| 586 | #[test] |
| 587 | fn file_database_uses_wal_persists_and_migrates_once() { |
| 588 | let directory = tempfile::tempdir().unwrap(); |
| 589 | let path = directory.path().join("nested").join("cache.sqlite3"); |
| 590 | { |
| 591 | let store = Store::open(&path).unwrap(); |
| 592 | assert_eq!(store.schema_version().unwrap(), 24); |
| 593 | store.upsert_messages(&[message("c", "m1", 1)]).unwrap(); |
| 594 | } |
| 595 | let reopened = Store::open(&path).unwrap(); |
| 596 | assert_eq!(reopened.schema_version().unwrap(), 24); |
| 597 | assert_eq!(reopened.message_count("c").unwrap(), 1); |
| 598 | drop(reopened); |
| 599 | let mode: String = rusqlite_open(&path) |
| 600 | .pragma_query_value(None, "journal_mode", |row| row.get(0)) |
| 601 | .unwrap(); |
| 602 | assert_eq!(mode, "wal"); |
| 603 | } |
| 604 | |
| 605 | #[test] |
| 606 | fn newer_schema_is_refused() { |
| 607 | let directory = tempfile::tempdir().unwrap(); |
| 608 | let path = directory.path().join("cache.sqlite3"); |
| 609 | drop(Store::open(&path).unwrap()); |
| 610 | let connection = rusqlite_open(&path); |
| 611 | connection |
| 612 | .execute_batch("PRAGMA user_version = 99") |
| 613 | .unwrap(); |
| 614 | drop(connection); |
| 615 | let error = Store::open(&path).err().expect("must refuse"); |
| 616 | assert!(error.to_string().contains("99"), "{error}"); |
| 617 | } |
| 618 | |
| 619 | #[test] |
| 620 | fn old_schema_is_upgraded_in_place() { |
| 621 | let directory = tempfile::tempdir().unwrap(); |
| 622 | let path = directory.path().join("cache.sqlite3"); |
| 623 | drop(Store::open(&path).unwrap()); |
| 624 | let connection = rusqlite_open(&path); |
| 625 | connection.execute_batch("DROP TABLE messages; DROP TABLE sync_state; DROP TABLE chats; DROP TABLE chat_members; DROP TABLE channels; DROP TABLE teams; DROP TABLE meta; DROP TABLE avatars; DROP TABLE folder_items; DROP TABLE folders; DROP TABLE pinned_channels; DROP TABLE images; DROP TABLE search_keys; DROP TABLE message_search; DROP TABLE title_search; DROP TABLE team_layout; DROP TABLE channel_layout; DROP TABLE channel_tabs; DROP TABLE presence; DROP TABLE activity; DROP TABLE outbox; DROP TABLE drafts; DROP TABLE attachment_images; DROP TABLE agent_reaction_proposals; DROP TABLE agent_proposals; PRAGMA user_version = 0").unwrap(); |
| 626 | drop(connection); |
| 627 | let store = Store::open(&path).unwrap(); |
| 628 | assert_eq!(store.schema_version().unwrap(), 24); |
| 629 | store.upsert_messages(&[message("c", "m1", 1)]).unwrap(); |
| 630 | } |
| 631 | |
| 632 | fn rusqlite_open(path: &std::path::Path) -> Connection { |
| 633 | Connection::open(path).unwrap() |
| 634 | } |
| 635 | |
| 636 | #[test] |
| 637 | fn meta_values_and_times_round_trip() { |
| 638 | let store = Store::open_in_memory().unwrap(); |
| 639 | assert_eq!(store.meta("k").unwrap(), None); |
| 640 | store.set_meta("k", "one").unwrap(); |
| 641 | store.set_meta("k", "two").unwrap(); |
| 642 | assert_eq!(store.meta("k").unwrap().as_deref(), Some("two")); |
| 643 | assert_eq!(store.meta_time("t").unwrap(), None); |
| 644 | store.set_meta_time("t", at(7)).unwrap(); |
| 645 | assert_eq!(store.meta_time("t").unwrap(), Some(at(7))); |
| 646 | store.set_meta("bad", "not a number").unwrap(); |
| 647 | assert_eq!(store.meta_time("bad").unwrap(), None); |
| 648 | } |
| 649 | |
| 650 | #[test] |
| 651 | fn chats_can_be_pruned_and_marked_read() { |
| 652 | let store = Store::open_in_memory().unwrap(); |
| 653 | store |
| 654 | .upsert_chats(&[ |
| 655 | chat("a", "A", Some(at(1))), |
| 656 | chat("b", "B", Some(at(2))), |
| 657 | chat("c", "C", None), |
| 658 | ]) |
| 659 | .unwrap(); |
| 660 | let times = store |
| 661 | .chat_last_message_times(&["a".to_owned(), "c".to_owned(), "zzz".to_owned()]) |
| 662 | .unwrap(); |
| 663 | assert_eq!(times.len(), 2); |
| 664 | assert_eq!(times["a"], Some(at(1))); |
| 665 | assert_eq!(times["c"], None); |
| 666 | |
| 667 | assert_eq!(store.remove_chats(&["a".to_owned()]).unwrap(), 1); |
| 668 | assert_eq!(store.remove_chats_except(&["b".to_owned()]).unwrap(), 1); |
| 669 | assert_eq!(store.chat_count().unwrap(), 1); |
| 670 | |
| 671 | store.mark_chat_read("b", at(30)).unwrap(); |
| 672 | let read = store.chat("b").unwrap().unwrap(); |
| 673 | assert!(!read.unread); |
| 674 | assert_eq!(read.last_read_at, Some(at(30))); |
| 675 | } |
| 676 | |
| 677 | #[test] |
| 678 | fn migration_to_v2_keeps_existing_sync_state() { |
| 679 | let directory = tempfile::tempdir().unwrap(); |
| 680 | let path = directory.path().join("old.sqlite3"); |
| 681 | { |
| 682 | let connection = Connection::open(&path).unwrap(); |
| 683 | connection |
| 684 | .execute_batch(include_str!("../src/migrations/0001_initial.sql")) |
| 685 | .unwrap(); |
| 686 | connection.pragma_update(None, "user_version", 1).unwrap(); |
| 687 | connection |
| 688 | .execute("INSERT INTO sync_state (conversation_id, newest_seen, has_more) VALUES ('c', 5, 1)", []) |
| 689 | .unwrap(); |
| 690 | } |
| 691 | let store = Store::open(&path).unwrap(); |
| 692 | let state = store.sync_state("c").unwrap().unwrap(); |
| 693 | assert_eq!(state.delta_link, None); |
| 694 | assert_eq!(store.schema_version().unwrap(), 24); |
| 695 | } |
| 696 | |
| 697 | #[test] |
| 698 | fn migration_to_v13_clears_channel_delta_links_only() { |
| 699 | let directory = tempfile::tempdir().unwrap(); |
| 700 | let path = directory.path().join("old.sqlite3"); |
| 701 | { |
| 702 | let store = Store::open(&path).unwrap(); |
| 703 | drop(store); |
| 704 | let connection = Connection::open(&path).unwrap(); |
| 705 | connection |
| 706 | .execute_batch( |
| 707 | "INSERT INTO teams (id, name) VALUES ('t', 'T'); |
| 708 | INSERT INTO channels (id, team_id, name) VALUES ('ch', 't', 'C'); |
| 709 | INSERT INTO sync_state (conversation_id, delta_link) VALUES ('ch', 'link'), ('chat', 'link'); |
| 710 | ALTER TABLE messages DROP COLUMN subject; |
| 711 | ALTER TABLE messages DROP COLUMN call_event_json; |
| 712 | ALTER TABLE messages DROP COLUMN importance; |
| 713 | ALTER TABLE folders DROP COLUMN expanded; |
| 714 | DROP TABLE channel_tabs; |
| 715 | DROP TABLE agent_reaction_proposals; DROP TABLE agent_proposals; |
| 716 | ALTER TABLE channel_layout DROP COLUMN notification_level; |
| 717 | ALTER TABLE channel_layout DROP COLUMN include_replies; |
| 718 | PRAGMA user_version = 12", |
| 719 | ) |
| 720 | .unwrap(); |
| 721 | } |
| 722 | let store = Store::open(&path).unwrap(); |
| 723 | assert_eq!( |
| 724 | store |
| 725 | .sync_state("ch") |
| 726 | .unwrap() |
| 727 | .and_then(|state| state.delta_link), |
| 728 | None |
| 729 | ); |
| 730 | assert_eq!( |
| 731 | store |
| 732 | .sync_state("chat") |
| 733 | .unwrap() |
| 734 | .unwrap() |
| 735 | .delta_link |
| 736 | .as_deref(), |
| 737 | Some("link") |
| 738 | ); |
| 739 | } |
| 740 | |
| 741 | #[test] |
| 742 | fn preview_updates_only_for_newer_messages() { |
| 743 | let store = Store::open_in_memory().unwrap(); |
| 744 | store.upsert_chats(&[chat("a", "A", Some(at(5)))]).unwrap(); |
| 745 | let preview = |text| ChatPreview { |
| 746 | text: Some(text), |
| 747 | sender_id: Some("user-ada"), |
| 748 | sender_name: Some("Ada Example"), |
| 749 | deleted: false, |
| 750 | }; |
| 751 | assert!( |
| 752 | !store |
| 753 | .update_chat_preview("a", at(4), &preview("older")) |
| 754 | .unwrap() |
| 755 | ); |
| 756 | assert!( |
| 757 | store |
| 758 | .update_chat_preview("a", at(6), &preview("newer")) |
| 759 | .unwrap() |
| 760 | ); |
| 761 | let record = store.chat("a").unwrap().unwrap(); |
| 762 | assert_eq!(record.last_message_preview.as_deref(), Some("newer")); |
| 763 | assert_eq!( |
| 764 | record.last_message_sender_name.as_deref(), |
| 765 | Some("Ada Example") |
| 766 | ); |
| 767 | assert_eq!(record.last_message_at, Some(at(6))); |
| 768 | assert!(!record.last_message_deleted); |
| 769 | } |
| 770 | |
| 771 | #[test] |
| 772 | fn a_call_moves_the_chat_time_and_keeps_the_line() { |
| 773 | let store = Store::open_in_memory().unwrap(); |
| 774 | store.upsert_chats(&[chat("a", "A", Some(at(5)))]).unwrap(); |
| 775 | let before = store.chat("a").unwrap().unwrap().last_message_preview; |
| 776 | assert!(!store.touch_chat_time("a", at(4)).unwrap()); |
| 777 | assert!(store.touch_chat_time("a", at(7)).unwrap()); |
| 778 | let record = store.chat("a").unwrap().unwrap(); |
| 779 | assert_eq!(record.last_message_at, Some(at(7))); |
| 780 | assert_eq!(record.last_message_preview, before); |
| 781 | } |
| 782 | |
| 783 | #[test] |
| 784 | fn avatars_round_trip_and_report_stale_ids() { |
| 785 | let store = Store::open_in_memory().unwrap(); |
| 786 | store |
| 787 | .upsert_avatar(&AvatarRecord { |
| 788 | user_id: "fresh".into(), |
| 789 | bytes: Some(vec![1, 2, 3]), |
| 790 | content_type: "image/jpeg".into(), |
| 791 | fetched_at: at(50), |
| 792 | }) |
| 793 | .unwrap(); |
| 794 | store |
| 795 | .upsert_avatar(&AvatarRecord { |
| 796 | user_id: "missing".into(), |
| 797 | bytes: None, |
| 798 | content_type: String::new(), |
| 799 | fetched_at: at(1), |
| 800 | }) |
| 801 | .unwrap(); |
| 802 | assert_eq!( |
| 803 | store.avatar("fresh").unwrap().unwrap().bytes, |
| 804 | Some(vec![1, 2, 3]) |
| 805 | ); |
| 806 | assert_eq!(store.avatar("missing").unwrap().unwrap().bytes, None); |
| 807 | assert!(store.avatar("unknown").unwrap().is_none()); |
| 808 | let ids: Vec<String> = ["fresh", "missing", "unknown"].map(String::from).to_vec(); |
| 809 | assert_eq!( |
| 810 | store.avatar_ids_needing_fetch(&ids, at(10)).unwrap(), |
| 811 | ["missing", "unknown"] |
| 812 | ); |
| 813 | } |
| 814 | |
| 815 | #[test] |
| 816 | fn folders_and_pinned_channels_are_replaced_in_order() { |
| 817 | let store = Store::open_in_memory().unwrap(); |
| 818 | let folder = |id: &str, kind: &str, items: &[&str]| FolderRecord { |
| 819 | id: id.into(), |
| 820 | name: id.into(), |
| 821 | kind: kind.into(), |
| 822 | expanded: kind != "MutedChats", |
| 823 | conversation_ids: items.iter().map(|item| item.to_string()).collect(), |
| 824 | }; |
| 825 | let first = [ |
| 826 | folder("f1", "Favorites", &["b", "a"]), |
| 827 | folder("f2", "UserCreated", &[]), |
| 828 | folder("f4", "MutedChats", &[]), |
| 829 | ]; |
| 830 | store |
| 831 | .replace_folders(&first, &["c2".into(), "c1".into()]) |
| 832 | .unwrap(); |
| 833 | assert_eq!(store.folders().unwrap(), first); |
| 834 | assert_eq!(store.pinned_channel_ids().unwrap(), ["c2", "c1"]); |
| 835 | store.set_folder_expanded("f2", false).unwrap(); |
| 836 | assert!(!store.folders().unwrap()[1].expanded); |
| 837 | store |
| 838 | .replace_folders(&[folder("f3", "UserCreated", &["z"])], &[]) |
| 839 | .unwrap(); |
| 840 | assert_eq!(store.folders().unwrap().len(), 1); |
| 841 | assert!(store.pinned_channel_ids().unwrap().is_empty()); |
| 842 | } |
| 843 | |
| 844 | #[test] |
| 845 | fn counts_messages_after_a_time_from_others() { |
| 846 | let store = Store::open_in_memory().unwrap(); |
| 847 | let mut mine = message("c", "m3", 3); |
| 848 | mine.sender_id = Some("me".into()); |
| 849 | store |
| 850 | .upsert_messages(&[message("c", "m1", 1), message("c", "m2", 2), mine]) |
| 851 | .unwrap(); |
| 852 | assert_eq!( |
| 853 | store.count_messages_after("c", Some(at(1)), "me").unwrap(), |
| 854 | 1 |
| 855 | ); |
| 856 | assert_eq!(store.count_messages_after("c", None, "me").unwrap(), 2); |
| 857 | } |
| 858 | |
| 859 | #[test] |
| 860 | fn messages_between_is_inclusive_and_oldest_first() { |
| 861 | let store = Store::open_in_memory().unwrap(); |
| 862 | store |
| 863 | .upsert_messages(&[ |
| 864 | message("c", "m4", 4), |
| 865 | message("c", "m1", 1), |
| 866 | message("c", "m3", 3), |
| 867 | message("c", "m2", 2), |
| 868 | message("other", "o3", 3), |
| 869 | ]) |
| 870 | .unwrap(); |
| 871 | let ids: Vec<String> = store |
| 872 | .messages_between("c", at(2), at(3)) |
| 873 | .unwrap() |
| 874 | .into_iter() |
| 875 | .map(|record| record.message_id) |
| 876 | .collect(); |
| 877 | assert_eq!(ids, ["m2", "m3"]); |
| 878 | } |
| 879 | |
| 880 | fn html_message( |
| 881 | conversation_id: &str, |
| 882 | message_id: &str, |
| 883 | minute: u32, |
| 884 | sender: &str, |
| 885 | html: &str, |
| 886 | ) -> MessageRecord { |
| 887 | MessageRecord { |
| 888 | sender_name: Some(sender.to_owned()), |
| 889 | body_html: html.to_owned(), |
| 890 | ..message(conversation_id, message_id, minute) |
| 891 | } |
| 892 | } |
| 893 | |
| 894 | #[test] |
| 895 | fn fts5_is_available_and_matches_prefixes_with_highlights() { |
| 896 | let store = Store::open_in_memory().unwrap(); |
| 897 | store |
| 898 | .upsert_messages(&[ |
| 899 | html_message( |
| 900 | "c1", |
| 901 | "m1", |
| 902 | 1, |
| 903 | "Ada Example", |
| 904 | "<p>The <b>budget</b> planning is done</p>", |
| 905 | ), |
| 906 | html_message("c1", "m2", 2, "Bob Sample", "<p>Lunch at noon</p>"), |
| 907 | html_message("c2", "m3", 3, "Bob Sample", "<p>Budget approved</p>"), |
| 908 | ]) |
| 909 | .unwrap(); |
| 910 | let hits = store.search_messages("budg plan", None, 10).unwrap(); |
| 911 | assert_eq!(hits.len(), 1); |
| 912 | assert_eq!(hits[0].message_id, "m1"); |
| 913 | assert_eq!(hits[0].sender_name.as_deref(), Some("Ada Example")); |
| 914 | assert_eq!(hits[0].created_at, at(1)); |
| 915 | assert!( |
| 916 | hits[0].snippet.contains("\u{1}budget\u{2}"), |
| 917 | "{}", |
| 918 | hits[0].snippet |
| 919 | ); |
| 920 | let all = store.search_messages("budget", None, 10).unwrap(); |
| 921 | assert_eq!(ids_of(&all), ["m3", "m1"]); |
| 922 | let scoped = store.search_messages("budget", Some("c1"), 10).unwrap(); |
| 923 | assert_eq!(ids_of(&scoped), ["m1"]); |
| 924 | assert_eq!(store.search_messages("budget", None, 1).unwrap().len(), 1); |
| 925 | } |
| 926 | |
| 927 | fn ids_of(hits: &[store::SearchHit]) -> Vec<&str> { |
| 928 | hits.iter().map(|hit| hit.message_id.as_str()).collect() |
| 929 | } |
| 930 | |
| 931 | #[test] |
| 932 | fn search_filters_by_sender_and_ignores_unsearchable_input() { |
| 933 | let store = Store::open_in_memory().unwrap(); |
| 934 | store |
| 935 | .upsert_messages(&[ |
| 936 | html_message("c1", "m1", 1, "Ada Example", "<p>report</p>"), |
| 937 | html_message("c1", "m2", 2, "Bob Sample", "<p>report</p>"), |
| 938 | ]) |
| 939 | .unwrap(); |
| 940 | assert_eq!( |
| 941 | ids_of(&store.search_messages("from:bob report", None, 10).unwrap()), |
| 942 | ["m2"] |
| 943 | ); |
| 944 | assert!(store.search_messages(" ", None, 10).unwrap().is_empty()); |
| 945 | assert!(store.search_messages("\"-*", None, 10).unwrap().is_empty()); |
| 946 | assert!(store.search_messages("rep) OR (", None, 10).is_ok()); |
| 947 | } |
| 948 | |
| 949 | #[test] |
| 950 | fn search_follows_edits_and_deletes() { |
| 951 | let store = Store::open_in_memory().unwrap(); |
| 952 | store |
| 953 | .upsert_messages(&[html_message("c1", "m1", 1, "Ada", "<p>alpha</p>")]) |
| 954 | .unwrap(); |
| 955 | store |
| 956 | .upsert_messages(&[html_message("c1", "m1", 1, "Ada", "<p>beta</p>")]) |
| 957 | .unwrap(); |
| 958 | assert!(store.search_messages("alpha", None, 10).unwrap().is_empty()); |
| 959 | assert_eq!(store.search_messages("beta", None, 10).unwrap().len(), 1); |
| 960 | let mut deleted = html_message("c1", "m1", 1, "Ada", ""); |
| 961 | deleted.deleted = true; |
| 962 | store.upsert_messages(&[deleted]).unwrap(); |
| 963 | assert!(store.search_messages("beta", None, 10).unwrap().is_empty()); |
| 964 | } |
| 965 | |
| 966 | #[test] |
| 967 | fn title_search_follows_chat_renames() { |
| 968 | let store = Store::open_in_memory().unwrap(); |
| 969 | store |
| 970 | .upsert_chats(&[chat("c1", "Budget planning", None)]) |
| 971 | .unwrap(); |
| 972 | assert_eq!( |
| 973 | store.search_conversations("budg", 10).unwrap()[0].conversation_id, |
| 974 | "c1" |
| 975 | ); |
| 976 | store.upsert_chats(&[chat("c1", "Holiday", None)]).unwrap(); |
| 977 | assert!(store.search_conversations("budget", 10).unwrap().is_empty()); |
| 978 | assert_eq!(store.search_conversations("holi", 10).unwrap().len(), 1); |
| 979 | assert!( |
| 980 | store |
| 981 | .search_conversations("from:holi", 10) |
| 982 | .unwrap() |
| 983 | .is_empty() |
| 984 | ); |
| 985 | } |
| 986 | |
| 987 | #[test] |
| 988 | fn migration_indexes_rows_that_predate_search() { |
| 989 | let directory = tempfile::tempdir().unwrap(); |
| 990 | let path = directory.path().join("cache.sqlite3"); |
| 991 | { |
| 992 | let store = Store::open(&path).unwrap(); |
| 993 | store.upsert_chats(&[chat("c1", "Old chat", None)]).unwrap(); |
| 994 | store |
| 995 | .upsert_messages(&[html_message("c1", "m1", 1, "Ada", "<p>legacy text</p>")]) |
| 996 | .unwrap(); |
| 997 | } |
| 998 | { |
| 999 | let connection = Connection::open(&path).unwrap(); |
| 1000 | connection |
| 1001 | .execute_batch( |
| 1002 | "DROP TRIGGER chats_title_insert; DROP TRIGGER chats_title_update; DROP TRIGGER chats_title_delete; |
| 1003 | DROP TRIGGER channels_title_insert; DROP TRIGGER channels_title_update; DROP TRIGGER channels_title_delete; |
| 1004 | DROP TABLE images; DROP TABLE search_keys; DROP TABLE message_search; DROP TABLE title_search; |
| 1005 | DROP TABLE team_layout; DROP TABLE channel_layout; DROP TABLE channel_tabs; DROP TABLE presence; DROP TABLE activity; DROP TABLE outbox; DROP TABLE drafts; DROP TABLE attachment_images; DROP TABLE agent_reaction_proposals; DROP TABLE agent_proposals; |
| 1006 | DROP INDEX messages_by_sender; DROP INDEX chat_members_by_user; |
| 1007 | ALTER TABLE messages DROP COLUMN sender_application_id; |
| 1008 | ALTER TABLE messages DROP COLUMN links_json; |
| 1009 | ALTER TABLE messages DROP COLUMN subject; |
| 1010 | ALTER TABLE messages DROP COLUMN call_event_json; |
| 1011 | ALTER TABLE messages DROP COLUMN importance; |
| 1012 | ALTER TABLE chats DROP COLUMN muted; |
| 1013 | ALTER TABLE folders DROP COLUMN expanded; |
| 1014 | PRAGMA user_version = 3;", |
| 1015 | ) |
| 1016 | .unwrap(); |
| 1017 | } |
| 1018 | let store = Store::open(&path).unwrap(); |
| 1019 | assert_eq!(store.search_messages("legacy", None, 10).unwrap().len(), 1); |
| 1020 | assert_eq!(store.search_conversations("old", 10).unwrap().len(), 1); |
| 1021 | } |
| 1022 | |
| 1023 | fn image(key: &str, minute: u32, size: usize) -> store::ImageRecord { |
| 1024 | store::ImageRecord { |
| 1025 | key: key.to_owned(), |
| 1026 | bytes: vec![7; size], |
| 1027 | content_type: "image/png".to_owned(), |
| 1028 | fetched_at: at(minute), |
| 1029 | } |
| 1030 | } |
| 1031 | |
| 1032 | #[test] |
| 1033 | fn images_round_trip_and_evict_the_oldest_over_the_cap() { |
| 1034 | let store = Store::open_in_memory().unwrap(); |
| 1035 | store.put_image(&image("a", 1, 40), 100).unwrap(); |
| 1036 | store.put_image(&image("b", 2, 40), 100).unwrap(); |
| 1037 | assert_eq!(store.image("a").unwrap().unwrap().bytes.len(), 40); |
| 1038 | store.put_image(&image("c", 3, 40), 100).unwrap(); |
| 1039 | assert!(store.image("a").unwrap().is_none()); |
| 1040 | assert!(store.has_image("b").unwrap()); |
| 1041 | assert!(store.has_image("c").unwrap()); |
| 1042 | assert_eq!(store.image_cache_bytes().unwrap(), 80); |
| 1043 | store.put_image(&image("big", 0, 500), 100).unwrap(); |
| 1044 | assert!(store.has_image("big").unwrap()); |
| 1045 | assert_eq!(store.image_cache_bytes().unwrap(), 500); |
| 1046 | } |
| 1047 | |
| 1048 | #[test] |
| 1049 | fn first_message_after_from_others_skips_own_and_deleted() { |
| 1050 | let store = Store::open_in_memory().unwrap(); |
| 1051 | let mut own = message("c1", "own", 2); |
| 1052 | own.sender_id = Some("me".to_owned()); |
| 1053 | let mut gone = message("c1", "gone", 3); |
| 1054 | gone.deleted = true; |
| 1055 | store |
| 1056 | .upsert_messages(&[message("c1", "old", 1), own, gone, message("c1", "new", 4)]) |
| 1057 | .unwrap(); |
| 1058 | assert_eq!( |
| 1059 | store |
| 1060 | .first_message_after_from_others("c1", Some(at(1)), "me") |
| 1061 | .unwrap() |
| 1062 | .as_deref(), |
| 1063 | Some("new") |
| 1064 | ); |
| 1065 | assert_eq!( |
| 1066 | store |
| 1067 | .first_message_after_from_others("c1", None, "me") |
| 1068 | .unwrap() |
| 1069 | .as_deref(), |
| 1070 | Some("old") |
| 1071 | ); |
| 1072 | assert!( |
| 1073 | store |
| 1074 | .first_message_after_from_others("c1", Some(at(4)), "me") |
| 1075 | .unwrap() |
| 1076 | .is_none() |
| 1077 | ); |
| 1078 | } |
| 1079 | |
| 1080 | #[test] |
| 1081 | fn a_team_is_found_by_id() { |
| 1082 | let store = Store::open_in_memory().unwrap(); |
| 1083 | store |
| 1084 | .upsert_teams(&[TeamRecord { |
| 1085 | id: "t1".to_owned(), |
| 1086 | name: "Alpha".to_owned(), |
| 1087 | }]) |
| 1088 | .unwrap(); |
| 1089 | assert_eq!(store.team("t1").unwrap().unwrap().name, "Alpha"); |
| 1090 | assert!(store.team("nope").unwrap().is_none()); |
| 1091 | } |
| 1092 | |
| 1093 | #[test] |
| 1094 | fn sync_does_not_resurrect_a_locally_read_chat() { |
| 1095 | let store = Store::open_in_memory().unwrap(); |
| 1096 | store |
| 1097 | .upsert_chats(&[chat("chat-1", "Planning", Some(at(5)))]) |
| 1098 | .unwrap(); |
| 1099 | store.mark_chat_read("chat-1", at(10)).unwrap(); |
| 1100 | |
| 1101 | store |
| 1102 | .upsert_chats(&[chat("chat-1", "Planning", Some(at(5)))]) |
| 1103 | .unwrap(); |
| 1104 | let stale = store.chat("chat-1").unwrap().unwrap(); |
| 1105 | assert!(!stale.unread); |
| 1106 | assert_eq!(stale.last_read_at, Some(at(10))); |
| 1107 | |
| 1108 | store |
| 1109 | .upsert_chats(&[chat("chat-1", "Planning", Some(at(12)))]) |
| 1110 | .unwrap(); |
| 1111 | assert!(store.chat("chat-1").unwrap().unwrap().unread); |
| 1112 | } |
| 1113 | |
| 1114 | #[test] |
| 1115 | fn mark_chat_unread_sets_the_flag_and_horizon() { |
| 1116 | let store = Store::open_in_memory().unwrap(); |
| 1117 | store |
| 1118 | .upsert_chats(&[chat("chat-1", "Planning", Some(at(5)))]) |
| 1119 | .unwrap(); |
| 1120 | store.mark_chat_read("chat-1", at(10)).unwrap(); |
| 1121 | |
| 1122 | store.mark_chat_unread("chat-1", at(4)).unwrap(); |
| 1123 | let unread = store.chat("chat-1").unwrap().unwrap(); |
| 1124 | assert!(unread.unread); |
| 1125 | assert_eq!(unread.last_read_at, Some(at(4))); |
| 1126 | } |
| 1127 | |
| 1128 | #[test] |
| 1129 | fn muted_flag_survives_a_chat_refresh() { |
| 1130 | let store = Store::open_in_memory().unwrap(); |
| 1131 | store |
| 1132 | .upsert_chats(&[chat("a", "A", Some(at(1))), chat("b", "B", Some(at(2)))]) |
| 1133 | .unwrap(); |
| 1134 | assert!(!store.chat("a").unwrap().unwrap().muted); |
| 1135 | |
| 1136 | store.set_chat_muted("a", true).unwrap(); |
| 1137 | store |
| 1138 | .upsert_chats(&[chat("a", "A", Some(at(3))), chat("b", "B", Some(at(2)))]) |
| 1139 | .unwrap(); |
| 1140 | assert!(store.chat("a").unwrap().unwrap().muted); |
| 1141 | assert!(!store.chat("b").unwrap().unwrap().muted); |
| 1142 | |
| 1143 | store.set_chat_muted("a", false).unwrap(); |
| 1144 | assert!(!store.chat("a").unwrap().unwrap().muted); |
| 1145 | } |
| 1146 | |
| 1147 | #[test] |
| 1148 | fn muted_states_touch_only_the_listed_chats() { |
| 1149 | let store = Store::open_in_memory().unwrap(); |
| 1150 | store |
| 1151 | .upsert_chats(&[ |
| 1152 | chat("a", "A", Some(at(1))), |
| 1153 | chat("b", "B", Some(at(2))), |
| 1154 | chat("c", "C", Some(at(3))), |
| 1155 | ]) |
| 1156 | .unwrap(); |
| 1157 | store.set_chat_muted("a", true).unwrap(); |
| 1158 | store.set_chat_muted("c", true).unwrap(); |
| 1159 | |
| 1160 | store |
| 1161 | .apply_muted_states(&[ |
| 1162 | ("a".to_owned(), false), |
| 1163 | ("b".to_owned(), true), |
| 1164 | ("gone".to_owned(), true), |
| 1165 | ]) |
| 1166 | .unwrap(); |
| 1167 | assert!(!store.chat("a").unwrap().unwrap().muted); |
| 1168 | assert!(store.chat("b").unwrap().unwrap().muted); |
| 1169 | assert!(store.chat("c").unwrap().unwrap().muted); |
| 1170 | } |
| 1171 | |
| 1172 | #[test] |
| 1173 | fn presence_upsert_keeps_the_latest_availability() { |
| 1174 | let store = Store::open_in_memory().unwrap(); |
| 1175 | let first = [("ada".to_owned(), "Available".to_owned())]; |
| 1176 | store.upsert_presences(&first, at(1)).unwrap(); |
| 1177 | let second = [ |
| 1178 | ("ada".to_owned(), "Away".to_owned()), |
| 1179 | ("bob".to_owned(), "Busy".to_owned()), |
| 1180 | ]; |
| 1181 | store.upsert_presences(&second, at(2)).unwrap(); |
| 1182 | let mut presences = store.presences().unwrap(); |
| 1183 | presences.sort(); |
| 1184 | assert_eq!(presences, second.to_vec()); |
| 1185 | } |
| 1186 | |
| 1187 | fn activity(id: i64, minute: u32) -> store::ActivityRecord { |
| 1188 | store::ActivityRecord { |
| 1189 | id, |
| 1190 | conversation_id: "c1".to_owned(), |
| 1191 | kind: "messages".to_owned(), |
| 1192 | message_id: format!("m{id}"), |
| 1193 | actors_json: r#"[{"user_id":"user-ada","name":"Ada Example"}]"#.to_owned(), |
| 1194 | preview: "hello".to_owned(), |
| 1195 | glyphs: String::new(), |
| 1196 | count: 1, |
| 1197 | updated_at: at(minute), |
| 1198 | read: false, |
| 1199 | } |
| 1200 | } |
| 1201 | |
| 1202 | #[test] |
| 1203 | fn activity_upsert_inserts_then_updates_in_place() { |
| 1204 | let store = Store::open_in_memory().unwrap(); |
| 1205 | store |
| 1206 | .upsert_activity(&[activity(1, 5), activity(2, 9)]) |
| 1207 | .unwrap(); |
| 1208 | let mut changed = activity(1, 12); |
| 1209 | changed.count = 3; |
| 1210 | changed.read = true; |
| 1211 | store |
| 1212 | .upsert_activity(std::slice::from_ref(&changed)) |
| 1213 | .unwrap(); |
| 1214 | let records = store.activity().unwrap(); |
| 1215 | assert_eq!(records.len(), 2); |
| 1216 | assert_eq!(records[0], changed); |
| 1217 | assert_eq!(records[1].id, 2); |
| 1218 | } |
| 1219 | |
| 1220 | #[test] |
| 1221 | fn activity_prune_drops_only_older_rows() { |
| 1222 | let store = Store::open_in_memory().unwrap(); |
| 1223 | store |
| 1224 | .upsert_activity(&[activity(1, 5), activity(2, 9), activity(3, 20)]) |
| 1225 | .unwrap(); |
| 1226 | assert_eq!(store.prune_activity(at(9)).unwrap(), 1); |
| 1227 | let ids: Vec<i64> = store |
| 1228 | .activity() |
| 1229 | .unwrap() |
| 1230 | .iter() |
| 1231 | .map(|record| record.id) |
| 1232 | .collect(); |
| 1233 | assert_eq!(ids, vec![3, 2]); |
| 1234 | } |
| 1235 | |
| 1236 | #[test] |
| 1237 | fn display_names_prefer_members_then_latest_sender_name() { |
| 1238 | let store = Store::open_in_memory().unwrap(); |
| 1239 | let unnamed_member = ChatRecord { |
| 1240 | members: vec![MemberRecord { |
| 1241 | user_id: Some("user-sam".to_owned()), |
| 1242 | display_name: UNKNOWN_USER_NAME.to_owned(), |
| 1243 | }], |
| 1244 | ..chat("chat-2", "Review", Some(at(6))) |
| 1245 | }; |
| 1246 | store |
| 1247 | .upsert_chats(&[chat("chat-1", "Planning", Some(at(5))), unnamed_member]) |
| 1248 | .unwrap(); |
| 1249 | let sent_by = |message_id: &str, minute: u32, sender: &str, name: Option<&str>| MessageRecord { |
| 1250 | sender_id: Some(sender.to_owned()), |
| 1251 | sender_name: name.map(str::to_owned), |
| 1252 | ..message("channel", message_id, minute) |
| 1253 | }; |
| 1254 | store |
| 1255 | .upsert_messages(&[ |
| 1256 | sent_by("m1", 1, "user-ada", Some("Ada Sender")), |
| 1257 | sent_by("m2", 2, "user-sam", Some("Sam Old")), |
| 1258 | sent_by("m3", 3, "user-sam", Some("Sam New")), |
| 1259 | sent_by("m4", 4, "user-sam", None), |
| 1260 | sent_by("m5", 5, "user-blank", Some("")), |
| 1261 | ]) |
| 1262 | .unwrap(); |
| 1263 | let wanted: Vec<String> = ["user-ada", "user-sam", "user-blank", "user-none"] |
| 1264 | .map(str::to_owned) |
| 1265 | .to_vec(); |
| 1266 | let names = store.display_names(&wanted).unwrap(); |
| 1267 | assert_eq!(names.len(), 2); |
| 1268 | assert_eq!(names["user-ada"], "Ada Example"); |
| 1269 | assert_eq!(names["user-sam"], "Sam New"); |
| 1270 | assert!(store.display_names(&[]).unwrap().is_empty()); |
| 1271 | } |
| 1272 | |
| 1273 | #[test] |
| 1274 | fn channel_tabs_keep_their_saved_order_and_are_replaced_on_refresh() { |
| 1275 | let store = Store::open_in_memory().unwrap(); |
| 1276 | let tab = |tab_id: &str, open_url: Option<&str>| ChannelTabRecord { |
| 1277 | tab_id: tab_id.to_owned(), |
| 1278 | name: format!("Tab {tab_id}"), |
| 1279 | definition_id: "com.microsoft.teamspace.tab.web".to_owned(), |
| 1280 | open_url: open_url.map(str::to_owned), |
| 1281 | }; |
| 1282 | let layout = |tabs: Vec<ChannelTabRecord>| { |
| 1283 | vec![TeamLayoutRecord { |
| 1284 | team_id: "team".to_owned(), |
| 1285 | hidden: false, |
| 1286 | channels: vec![ChannelLayoutRecord { |
| 1287 | channel_id: "channel".to_owned(), |
| 1288 | general: false, |
| 1289 | hidden: false, |
| 1290 | tabs, |
| 1291 | notifications: ChannelNotifications::default(), |
| 1292 | }], |
| 1293 | }] |
| 1294 | }; |
| 1295 | store |
| 1296 | .replace_team_layout(&layout(vec![ |
| 1297 | tab("b", Some("https://b.example")), |
| 1298 | tab("a", None), |
| 1299 | ])) |
| 1300 | .unwrap(); |
| 1301 | let tabs = store.channel_tabs("channel").unwrap(); |
| 1302 | let ids: Vec<&str> = tabs.iter().map(|tab| tab.tab_id.as_str()).collect(); |
| 1303 | assert_eq!(ids, ["b", "a"]); |
| 1304 | assert_eq!(tabs[0].open_url.as_deref(), Some("https://b.example")); |
| 1305 | assert!(store.channel_tabs("other").unwrap().is_empty()); |
| 1306 | store.replace_team_layout(&layout(Vec::new())).unwrap(); |
| 1307 | assert!(store.channel_tabs("channel").unwrap().is_empty()); |
| 1308 | } |
| 1309 | |
| 1310 | #[test] |
| 1311 | fn channel_notifications_default_to_feed_and_survive_layout_and_local_writes() { |
| 1312 | let store = Store::open_in_memory().unwrap(); |
| 1313 | store |
| 1314 | .upsert_teams(&[TeamRecord { |
| 1315 | id: "t1".to_owned(), |
| 1316 | name: "Alpha".to_owned(), |
| 1317 | }]) |
| 1318 | .unwrap(); |
| 1319 | let channel = |id: &str, name: &str| ChannelRecord { |
| 1320 | id: id.to_owned(), |
| 1321 | team_id: "t1".to_owned(), |
| 1322 | name: name.to_owned(), |
| 1323 | membership_type: None, |
| 1324 | last_message_at: None, |
| 1325 | unread: false, |
| 1326 | }; |
| 1327 | store |
| 1328 | .upsert_channels(&[channel("c1", "General"), channel("c2", "Random")]) |
| 1329 | .unwrap(); |
| 1330 | let team = || store.sidebar().unwrap().teams.remove(0); |
| 1331 | assert_eq!( |
| 1332 | team().channel_notifications("c2"), |
| 1333 | ChannelNotifications::default() |
| 1334 | ); |
| 1335 | |
| 1336 | let banner = ChannelNotifications { |
| 1337 | level: ChannelNotificationLevel::BannerAndFeed, |
| 1338 | include_replies: true, |
| 1339 | }; |
| 1340 | store.set_channel_notifications("c2", banner).unwrap(); |
| 1341 | let sidebar_team = team(); |
| 1342 | assert_eq!(sidebar_team.channel_notifications("c2"), banner); |
| 1343 | assert_eq!(sidebar_team.channels[0].name, "General"); |
| 1344 | |
| 1345 | let off = ChannelNotifications { |
| 1346 | level: ChannelNotificationLevel::Off, |
| 1347 | include_replies: false, |
| 1348 | }; |
| 1349 | store |
| 1350 | .replace_team_layout(&[TeamLayoutRecord { |
| 1351 | team_id: "t1".to_owned(), |
| 1352 | hidden: false, |
| 1353 | channels: vec![ChannelLayoutRecord { |
| 1354 | channel_id: "c2".to_owned(), |
| 1355 | general: false, |
| 1356 | hidden: false, |
| 1357 | tabs: Vec::new(), |
| 1358 | notifications: off, |
| 1359 | }], |
| 1360 | }]) |
| 1361 | .unwrap(); |
| 1362 | assert_eq!(team().channel_notifications("c2"), off); |
| 1363 | } |
| 1364 | |
| 1365 | #[test] |
| 1366 | fn channel_mentions_are_replaced_whole_and_cleared_per_channel() { |
| 1367 | let store = Store::open_in_memory().unwrap(); |
| 1368 | store |
| 1369 | .upsert_teams(&[TeamRecord { |
| 1370 | id: "t1".to_owned(), |
| 1371 | name: "Alpha".to_owned(), |
| 1372 | }]) |
| 1373 | .unwrap(); |
| 1374 | let channel = |id: &str, name: &str| ChannelRecord { |
| 1375 | id: id.to_owned(), |
| 1376 | team_id: "t1".to_owned(), |
| 1377 | name: name.to_owned(), |
| 1378 | membership_type: None, |
| 1379 | last_message_at: None, |
| 1380 | unread: true, |
| 1381 | }; |
| 1382 | store |
| 1383 | .upsert_channels(&[channel("c1", "General"), channel("c2", "Random")]) |
| 1384 | .unwrap(); |
| 1385 | let counts = |personal, channel, team| ChannelMentions { |
| 1386 | personal, |
| 1387 | channel, |
| 1388 | team, |
| 1389 | }; |
| 1390 | store |
| 1391 | .replace_channel_mentions(&[ |
| 1392 | ("c1".to_owned(), counts(2, 0, 1)), |
| 1393 | ("c2".to_owned(), counts(0, 0, 0)), |
| 1394 | ]) |
| 1395 | .unwrap(); |
| 1396 | let team = || store.sidebar().unwrap().teams.remove(0); |
| 1397 | assert_eq!(team().channel_mentions("c1"), counts(2, 0, 1)); |
| 1398 | assert!(!team().mentions.contains_key("c2")); |
| 1399 | store |
| 1400 | .replace_channel_mentions(&[("c2".to_owned(), counts(0, 1, 0))]) |
| 1401 | .unwrap(); |
| 1402 | assert_eq!(team().channel_mentions("c1"), ChannelMentions::default()); |
| 1403 | assert_eq!(team().channel_mentions("c2"), counts(0, 1, 0)); |
| 1404 | store.clear_channel_mentions("c2").unwrap(); |
| 1405 | assert!(team().mentions.is_empty()); |
| 1406 | } |
| 1407 | |
| 1408 | #[test] |
| 1409 | fn the_team_thread_id_is_the_general_channel_id() { |
| 1410 | let store = Store::open_in_memory().unwrap(); |
| 1411 | store |
| 1412 | .upsert_teams(&["group-a", "group-b"].map(|id| TeamRecord { |
| 1413 | id: id.to_owned(), |
| 1414 | name: id.to_owned(), |
| 1415 | })) |
| 1416 | .unwrap(); |
| 1417 | let channel = |id: &str, team_id: &str, name: &str| ChannelRecord { |
| 1418 | id: id.to_owned(), |
| 1419 | team_id: team_id.to_owned(), |
| 1420 | name: name.to_owned(), |
| 1421 | membership_type: None, |
| 1422 | last_message_at: None, |
| 1423 | unread: false, |
| 1424 | }; |
| 1425 | store |
| 1426 | .upsert_channels(&[ |
| 1427 | channel("19:a-random", "group-a", "Random"), |
| 1428 | channel("19:a-general", "group-a", "General"), |
| 1429 | channel("19:b-renamed", "group-b", "Allgemein"), |
| 1430 | channel("19:b-other", "group-b", "Other"), |
| 1431 | ]) |
| 1432 | .unwrap(); |
| 1433 | store |
| 1434 | .replace_team_layout(&[TeamLayoutRecord { |
| 1435 | team_id: "group-b".to_owned(), |
| 1436 | hidden: false, |
| 1437 | channels: vec![ChannelLayoutRecord { |
| 1438 | channel_id: "19:b-renamed".to_owned(), |
| 1439 | general: true, |
| 1440 | hidden: false, |
| 1441 | tabs: Vec::new(), |
| 1442 | notifications: ChannelNotifications::default(), |
| 1443 | }], |
| 1444 | }]) |
| 1445 | .unwrap(); |
| 1446 | assert_eq!( |
| 1447 | store.team_thread_id("group-a").unwrap().as_deref(), |
| 1448 | Some("19:a-general") |
| 1449 | ); |
| 1450 | assert_eq!( |
| 1451 | store.team_thread_id("group-b").unwrap().as_deref(), |
| 1452 | Some("19:b-renamed") |
| 1453 | ); |
| 1454 | assert_eq!(store.team_thread_id("group-c").unwrap(), None); |
| 1455 | } |
| 1456 | |
| 1457 | #[test] |
| 1458 | fn recent_messages_run_newest_first_across_scopes_with_a_cursor() { |
| 1459 | let store = Store::open_in_memory().unwrap(); |
| 1460 | let mut deleted = message("chat-a", "a3", 8); |
| 1461 | deleted.deleted = true; |
| 1462 | store |
| 1463 | .upsert_messages(&[ |
| 1464 | message("chat-a", "a1", 1), |
| 1465 | message("chat-a", "a2", 6), |
| 1466 | deleted, |
| 1467 | message("chat-b", "b1", 5), |
| 1468 | message("chat-b", "b2", 7), |
| 1469 | message("chat-c", "c1", 9), |
| 1470 | ]) |
| 1471 | .unwrap(); |
| 1472 | let scope = |conversation_id: &str, minute: u32| RecentScope { |
| 1473 | conversation_id: conversation_id.to_owned(), |
| 1474 | after: at(minute), |
| 1475 | }; |
| 1476 | let scopes = [scope("chat-a", 0), scope("chat-b", 5)]; |
| 1477 | let ids = |messages: Vec<MessageRecord>| -> Vec<String> { |
| 1478 | messages |
| 1479 | .into_iter() |
| 1480 | .map(|message| message.message_id) |
| 1481 | .collect() |
| 1482 | }; |
| 1483 | assert_eq!( |
| 1484 | ids(store.recent_messages(&scopes, None, 10).unwrap()), |
| 1485 | ["b2", "a2", "a1"] |
| 1486 | ); |
| 1487 | assert_eq!( |
| 1488 | ids(store.recent_messages(&scopes, None, 2).unwrap()), |
| 1489 | ["b2", "a2"] |
| 1490 | ); |
| 1491 | assert_eq!( |
| 1492 | ids(store |
| 1493 | .recent_messages(&scopes, Some((at(6), "a2", "chat-a")), 10) |
| 1494 | .unwrap()), |
| 1495 | ["a1"] |
| 1496 | ); |
| 1497 | assert!(store.recent_messages(&[], None, 10).unwrap().is_empty()); |
| 1498 | } |
| 1499 | |
| 1500 | #[test] |
| 1501 | fn recent_messages_break_ties_by_conversation_across_the_cursor() { |
| 1502 | let store = Store::open_in_memory().unwrap(); |
| 1503 | store |
| 1504 | .upsert_messages(&[message("chat-a", "m1", 5), message("chat-b", "m1", 5)]) |
| 1505 | .unwrap(); |
| 1506 | let scopes: Vec<RecentScope> = ["chat-a", "chat-b"] |
| 1507 | .into_iter() |
| 1508 | .map(|conversation_id| RecentScope { |
| 1509 | conversation_id: conversation_id.to_owned(), |
| 1510 | after: at(0), |
| 1511 | }) |
| 1512 | .collect(); |
| 1513 | let first = store.recent_messages(&scopes, None, 1).unwrap(); |
| 1514 | assert_eq!(first[0].conversation_id, "chat-b"); |
| 1515 | let second = store |
| 1516 | .recent_messages(&scopes, Some((at(5), "m1", "chat-b")), 1) |
| 1517 | .unwrap(); |
| 1518 | assert_eq!(second[0].conversation_id, "chat-a"); |
| 1519 | assert!( |
| 1520 | store |
| 1521 | .recent_messages(&scopes, Some((at(5), "m1", "chat-a")), 1) |
| 1522 | .unwrap() |
| 1523 | .is_empty() |
| 1524 | ); |
| 1525 | } |
| 1526 | |
| 1527 | fn call_message(message_id: &str, minute: u32) -> MessageRecord { |
| 1528 | MessageRecord { |
| 1529 | sender_id: None, |
| 1530 | sender_name: None, |
| 1531 | body_html: String::new(), |
| 1532 | call_event_json: Some(r#"{"phase":"Ended"}"#.to_owned()), |
| 1533 | ..message("c", message_id, minute) |
| 1534 | } |
| 1535 | } |
| 1536 | |
| 1537 | #[test] |
| 1538 | fn call_event_round_trips_and_is_not_indexed_or_unread() { |
| 1539 | let store = Store::open_in_memory().unwrap(); |
| 1540 | store |
| 1541 | .upsert_messages(&[call_message("call", 1), message("c", "text", 2)]) |
| 1542 | .unwrap(); |
| 1543 | let stored = store.messages("c", None, 10).unwrap(); |
| 1544 | assert_eq!( |
| 1545 | stored[0].call_event_json.as_deref(), |
| 1546 | Some(r#"{"phase":"Ended"}"#) |
| 1547 | ); |
| 1548 | assert_eq!(stored[1].call_event_json, None); |
| 1549 | assert_eq!(store.count_messages_after("c", None, "me").unwrap(), 1); |
| 1550 | assert_eq!( |
| 1551 | store |
| 1552 | .first_message_after_from_others("c", None, "me") |
| 1553 | .unwrap() |
| 1554 | .as_deref(), |
| 1555 | Some("text") |
| 1556 | ); |
| 1557 | } |
| 1558 | |
| 1559 | #[test] |
| 1560 | fn migration_to_v20_adds_the_call_event_column() { |
| 1561 | let directory = tempfile::tempdir().unwrap(); |
| 1562 | let path = directory.path().join("old.sqlite3"); |
| 1563 | { |
| 1564 | let store = Store::open(&path).unwrap(); |
| 1565 | store.upsert_messages(&[message("c", "old", 1)]).unwrap(); |
| 1566 | store |
| 1567 | .upsert_chats(&[chat("c", "Chat", Some(at(1)))]) |
| 1568 | .unwrap(); |
| 1569 | store.set_sync_state("c", &SyncState::default()).unwrap(); |
| 1570 | store |
| 1571 | .set_sync_state("channel", &SyncState::default()) |
| 1572 | .unwrap(); |
| 1573 | drop(store); |
| 1574 | Connection::open(&path) |
| 1575 | .unwrap() |
| 1576 | .execute_batch( |
| 1577 | "ALTER TABLE messages DROP COLUMN call_event_json; ALTER TABLE messages DROP COLUMN importance; PRAGMA user_version = 19", |
| 1578 | ) |
| 1579 | .unwrap(); |
| 1580 | } |
| 1581 | let store = Store::open(&path).unwrap(); |
| 1582 | assert_eq!(store.schema_version().unwrap(), 24); |
| 1583 | let old = store.messages("c", None, 10).unwrap(); |
| 1584 | assert_eq!(old[0].call_event_json, None); |
| 1585 | assert!(store.sync_state("c").unwrap().is_none()); |
| 1586 | assert!(store.sync_state("channel").unwrap().is_some()); |
| 1587 | store.upsert_messages(&[call_message("call", 2)]).unwrap(); |
| 1588 | assert!( |
| 1589 | store.messages("c", None, 10).unwrap()[1] |
| 1590 | .call_event_json |
| 1591 | .is_some() |
| 1592 | ); |
| 1593 | } |
| 1594 | |
| 1595 | #[test] |
| 1596 | fn migration_to_v21_refetches_chats_and_channels_for_recordings() { |
| 1597 | let directory = tempfile::tempdir().unwrap(); |
| 1598 | let path = directory.path().join("old.sqlite3"); |
| 1599 | { |
| 1600 | let store = Store::open(&path).unwrap(); |
| 1601 | store |
| 1602 | .upsert_chats(&[chat("c", "Chat", Some(at(1)))]) |
| 1603 | .unwrap(); |
| 1604 | store |
| 1605 | .upsert_teams(&[TeamRecord { |
| 1606 | id: "team".to_owned(), |
| 1607 | name: "Team".to_owned(), |
| 1608 | }]) |
| 1609 | .unwrap(); |
| 1610 | store |
| 1611 | .upsert_channels(&[ChannelRecord { |
| 1612 | id: "channel".to_owned(), |
| 1613 | team_id: "team".to_owned(), |
| 1614 | name: "General".to_owned(), |
| 1615 | membership_type: None, |
| 1616 | last_message_at: None, |
| 1617 | unread: false, |
| 1618 | }]) |
| 1619 | .unwrap(); |
| 1620 | for conversation_id in ["c", "channel", "other"] { |
| 1621 | store |
| 1622 | .set_sync_state(conversation_id, &SyncState::default()) |
| 1623 | .unwrap(); |
| 1624 | } |
| 1625 | drop(store); |
| 1626 | Connection::open(&path) |
| 1627 | .unwrap() |
| 1628 | .execute_batch("ALTER TABLE messages DROP COLUMN importance; PRAGMA user_version = 20") |
| 1629 | .unwrap(); |
| 1630 | } |
| 1631 | let store = Store::open(&path).unwrap(); |
| 1632 | assert_eq!(store.schema_version().unwrap(), 24); |
| 1633 | assert!(store.sync_state("c").unwrap().is_none()); |
| 1634 | assert!(store.sync_state("channel").unwrap().is_none()); |
| 1635 | assert!(store.sync_state("other").unwrap().is_some()); |
| 1636 | } |
| 1637 | |
| 1638 | fn horizon(member_id: &str, read_minute: u32, at_minute: Option<u32>) -> ReadHorizonRecord { |
| 1639 | ReadHorizonRecord { |
| 1640 | member_id: member_id.to_owned(), |
| 1641 | read_until: at(read_minute), |
| 1642 | read_at: at_minute.map(at), |
| 1643 | } |
| 1644 | } |
| 1645 | |
| 1646 | #[test] |
| 1647 | fn migration_to_v22_adds_read_horizons_that_survive_a_reopen() { |
| 1648 | let directory = tempfile::tempdir().unwrap(); |
| 1649 | let path = directory.path().join("old.sqlite3"); |
| 1650 | { |
| 1651 | let store = Store::open(&path).unwrap(); |
| 1652 | store |
| 1653 | .upsert_chats(&[chat("c", "Chat", Some(at(1)))]) |
| 1654 | .unwrap(); |
| 1655 | drop(store); |
| 1656 | Connection::open(&path) |
| 1657 | .unwrap() |
| 1658 | .execute_batch("DROP TABLE read_horizons; ALTER TABLE messages DROP COLUMN importance; PRAGMA user_version = 21") |
| 1659 | .unwrap(); |
| 1660 | } |
| 1661 | let store = Store::open(&path).unwrap(); |
| 1662 | assert_eq!(store.schema_version().unwrap(), 24); |
| 1663 | assert!(store.read_horizons("c").unwrap().is_empty()); |
| 1664 | let horizons = [ |
| 1665 | horizon("8:orgid:ada", 3, Some(4)), |
| 1666 | horizon("8:orgid:bob", 2, None), |
| 1667 | ]; |
| 1668 | store.set_read_horizons("c", &horizons).unwrap(); |
| 1669 | drop(store); |
| 1670 | let reopened = Store::open(&path).unwrap(); |
| 1671 | assert_eq!(reopened.read_horizons("c").unwrap(), horizons); |
| 1672 | } |
| 1673 | |
| 1674 | #[test] |
| 1675 | fn setting_read_horizons_replaces_the_conversation_only() { |
| 1676 | let store = Store::open_in_memory().unwrap(); |
| 1677 | let first = [ |
| 1678 | horizon("8:orgid:ada", 3, None), |
| 1679 | horizon("8:orgid:bob", 2, None), |
| 1680 | ]; |
| 1681 | store.set_read_horizons("c", &first).unwrap(); |
| 1682 | store |
| 1683 | .set_read_horizons("other", &[horizon("8:orgid:cy", 1, None)]) |
| 1684 | .unwrap(); |
| 1685 | let second = [horizon("8:orgid:ada", 5, Some(6))]; |
| 1686 | store.set_read_horizons("c", &second).unwrap(); |
| 1687 | assert_eq!(store.read_horizons("c").unwrap(), second); |
| 1688 | assert_eq!(store.read_horizons("other").unwrap().len(), 1); |
| 1689 | } |
| 1690 | |
| 1691 | #[test] |
| 1692 | fn local_show_hide_and_channel_pin_writes_reach_the_sidebar() { |
| 1693 | let store = Store::open_in_memory().unwrap(); |
| 1694 | store |
| 1695 | .upsert_teams(&[TeamRecord { |
| 1696 | id: "t1".to_owned(), |
| 1697 | name: "Alpha".to_owned(), |
| 1698 | }]) |
| 1699 | .unwrap(); |
| 1700 | let channel = |id: &str, name: &str| ChannelRecord { |
| 1701 | id: id.to_owned(), |
| 1702 | team_id: "t1".to_owned(), |
| 1703 | name: name.to_owned(), |
| 1704 | membership_type: None, |
| 1705 | last_message_at: None, |
| 1706 | unread: false, |
| 1707 | }; |
| 1708 | store |
| 1709 | .upsert_channels(&[channel("c1", "General"), channel("c2", "Random")]) |
| 1710 | .unwrap(); |
| 1711 | let team = || store.sidebar().unwrap().teams.remove(0); |
| 1712 | |
| 1713 | store.set_channel_hidden("c2", true).unwrap(); |
| 1714 | store.set_team_hidden("t1", true).unwrap(); |
| 1715 | let hidden = team(); |
| 1716 | assert_eq!(hidden.hidden_channel_ids, vec!["c2".to_owned()]); |
| 1717 | assert!(hidden.hidden); |
| 1718 | assert_eq!(hidden.channels[0].name, "General"); |
| 1719 | |
| 1720 | store.set_channel_hidden("c2", false).unwrap(); |
| 1721 | store.set_team_hidden("t1", false).unwrap(); |
| 1722 | let shown = team(); |
| 1723 | assert!(shown.hidden_channel_ids.is_empty()); |
| 1724 | assert!(!shown.hidden); |
| 1725 | |
| 1726 | store |
| 1727 | .replace_pinned_channels(&["c2".to_owned(), "c1".to_owned()]) |
| 1728 | .unwrap(); |
| 1729 | assert_eq!(store.pinned_channel_ids().unwrap(), vec!["c2", "c1"]); |
| 1730 | } |