| 1 | use chrono::{DateTime, TimeZone, Utc}; |
| 2 | use rusqlite::Connection; |
| 3 | use store::{ |
| 4 | AvatarRecord, ChannelRecord, ChatPreview, ChatRecord, FolderRecord, MemberRecord, |
| 5 | MessageRecord, Store, SyncState, TeamRecord, |
| 6 | }; |
| 7 | |
| 8 | #[test] |
| 9 | fn a_system_event_keeps_the_previous_unread_state() { |
| 10 | let store = Store::open_in_memory().unwrap(); |
| 11 | for (previous, expected) in [(true, true), (false, false)] { |
| 12 | let id = format!("chat-{previous}"); |
| 13 | let mut seeded = chat(&id, "Planning", Some(at(5))); |
| 14 | seeded.unread = previous; |
| 15 | store.upsert_chats(&[seeded]).unwrap(); |
| 16 | |
| 17 | let mut event = chat(&id, "Planning", Some(at(12))); |
| 18 | event.unread = false; |
| 19 | event.last_event_system = true; |
| 20 | store.upsert_chats(&[event]).unwrap(); |
| 21 | assert_eq!(store.chat(&id).unwrap().unwrap().unread, expected); |
| 22 | } |
| 23 | } |
| 24 | |
| 25 | fn at(minute: u32) -> DateTime<Utc> { |
| 26 | Utc.with_ymd_and_hms(2026, 10, 6, 9, minute, 0).unwrap() |
| 27 | } |
| 28 | |
| 29 | fn chat(id: &str, title: &str, last: Option<DateTime<Utc>>) -> ChatRecord { |
| 30 | ChatRecord { |
| 31 | id: id.to_owned(), |
| 32 | kind: "group".to_owned(), |
| 33 | title: title.to_owned(), |
| 34 | member_summary: "Ada Example, Bob Sample".to_owned(), |
| 35 | last_message_at: last, |
| 36 | last_read_at: Some(at(0)), |
| 37 | unread: true, |
| 38 | members: vec![ |
| 39 | MemberRecord { |
| 40 | user_id: Some("user-ada".to_owned()), |
| 41 | display_name: "Ada Example".to_owned(), |
| 42 | }, |
| 43 | MemberRecord { |
| 44 | user_id: None, |
| 45 | display_name: "Bob Sample".to_owned(), |
| 46 | }, |
| 47 | ], |
| 48 | ..ChatRecord::default() |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | fn message(conversation_id: &str, message_id: &str, minute: u32) -> MessageRecord { |
| 53 | MessageRecord { |
| 54 | conversation_id: conversation_id.to_owned(), |
| 55 | message_id: message_id.to_owned(), |
| 56 | reply_to_id: None, |
| 57 | sender_id: Some("user-ada".to_owned()), |
| 58 | sender_name: Some("Ada Example".to_owned()), |
| 59 | created_at: at(minute), |
| 60 | edited_at: None, |
| 61 | deleted: false, |
| 62 | body_html: format!("<p>message {message_id}</p>"), |
| 63 | attachments_json: "[]".to_owned(), |
| 64 | reactions_json: "[]".to_owned(), |
| 65 | mentions_json: "[]".to_owned(), |
| 66 | sender_application_id: None, |
| 67 | links_json: "[]".to_owned(), |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | #[test] |
| 72 | fn sender_application_round_trips() { |
| 73 | let store = Store::open_in_memory().unwrap(); |
| 74 | let bot_message = MessageRecord { |
| 75 | sender_application_id: Some("bot-guid".to_owned()), |
| 76 | ..message("c1", "m1", 1) |
| 77 | }; |
| 78 | store |
| 79 | .upsert_messages(std::slice::from_ref(&bot_message)) |
| 80 | .unwrap(); |
| 81 | let found = store.messages_by_id("c1", &["m1".to_owned()]).unwrap(); |
| 82 | assert_eq!(found["m1"], bot_message); |
| 83 | } |
| 84 | |
| 85 | #[test] |
| 86 | fn an_upsert_without_links_keeps_the_stored_links() { |
| 87 | let store = Store::open_in_memory().unwrap(); |
| 88 | store.upsert_messages(&[message("c1", "m1", 1)]).unwrap(); |
| 89 | let links = r#"[{"url":"https://a.example"}]"#; |
| 90 | assert!(store.set_message_links("c1", "m1", links).unwrap()); |
| 91 | assert!(!store.set_message_links("c1", "m1", links).unwrap()); |
| 92 | |
| 93 | let edited = MessageRecord { |
| 94 | body_html: "<p>edited</p>".to_owned(), |
| 95 | ..message("c1", "m1", 1) |
| 96 | }; |
| 97 | store.upsert_messages(&[edited]).unwrap(); |
| 98 | let found = store.messages_by_id("c1", &["m1".to_owned()]).unwrap(); |
| 99 | assert_eq!(found["m1"].links_json, links); |
| 100 | assert_eq!(found["m1"].body_html, "<p>edited</p>"); |
| 101 | |
| 102 | let replaced = MessageRecord { |
| 103 | links_json: r#"[{"url":"https://b.example"}]"#.to_owned(), |
| 104 | ..message("c1", "m1", 1) |
| 105 | }; |
| 106 | store.upsert_messages(std::slice::from_ref(&replaced)).unwrap(); |
| 107 | let found = store.messages_by_id("c1", &["m1".to_owned()]).unwrap(); |
| 108 | assert_eq!(found["m1"].links_json, replaced.links_json); |
| 109 | } |
| 110 | |
| 111 | #[test] |
| 112 | fn new_messages_start_without_links() { |
| 113 | let store = Store::open_in_memory().unwrap(); |
| 114 | store.upsert_messages(&[message("c1", "m1", 1)]).unwrap(); |
| 115 | let found = store.messages_by_id("c1", &["m1".to_owned()]).unwrap(); |
| 116 | assert_eq!(found["m1"].links_json, "[]"); |
| 117 | } |
| 118 | |
| 119 | fn ids(messages: &[MessageRecord]) -> Vec<&str> { |
| 120 | messages |
| 121 | .iter() |
| 122 | .map(|message| message.message_id.as_str()) |
| 123 | .collect() |
| 124 | } |
| 125 | |
| 126 | #[test] |
| 127 | fn chats_round_trip_with_members() { |
| 128 | let store = Store::open_in_memory().unwrap(); |
| 129 | let original = chat("chat-1", "Planning", Some(at(5))); |
| 130 | store.upsert_chats(std::slice::from_ref(&original)).unwrap(); |
| 131 | assert_eq!(store.chat("chat-1").unwrap(), Some(original)); |
| 132 | assert_eq!(store.chat("missing").unwrap(), None); |
| 133 | } |
| 134 | |
| 135 | #[test] |
| 136 | fn chat_upsert_replaces_fields_and_members() { |
| 137 | let store = Store::open_in_memory().unwrap(); |
| 138 | store |
| 139 | .upsert_chats(&[chat("chat-1", "Old", Some(at(1)))]) |
| 140 | .unwrap(); |
| 141 | let mut changed = chat("chat-1", "New", Some(at(9))); |
| 142 | changed.members.truncate(1); |
| 143 | changed.unread = false; |
| 144 | store.upsert_chats(&[changed.clone()]).unwrap(); |
| 145 | assert_eq!(store.recent_chats(10).unwrap(), vec![changed]); |
| 146 | } |
| 147 | |
| 148 | #[test] |
| 149 | fn chat_upsert_keeps_a_real_title_over_the_only_you_fallback() { |
| 150 | let store = Store::open_in_memory().unwrap(); |
| 151 | store |
| 152 | .upsert_chats(&[chat("chat-1", "Sample Bot", Some(at(1)))]) |
| 153 | .unwrap(); |
| 154 | store |
| 155 | .upsert_chats(&[chat("chat-1", "(only you)", Some(at(2)))]) |
| 156 | .unwrap(); |
| 157 | assert_eq!(store.chat("chat-1").unwrap().unwrap().title, "Sample Bot"); |
| 158 | store |
| 159 | .upsert_chats(&[chat("chat-2", "(only you)", Some(at(2)))]) |
| 160 | .unwrap(); |
| 161 | store |
| 162 | .upsert_chats(&[chat("chat-2", "Sample Bot", Some(at(3)))]) |
| 163 | .unwrap(); |
| 164 | assert_eq!(store.chat("chat-2").unwrap().unwrap().title, "Sample Bot"); |
| 165 | } |
| 166 | |
| 167 | #[test] |
| 168 | fn recent_chats_are_newest_first_with_unknown_last() { |
| 169 | let store = Store::open_in_memory().unwrap(); |
| 170 | store |
| 171 | .upsert_chats(&[ |
| 172 | chat("old", "Old", Some(at(1))), |
| 173 | chat("none", "None", None), |
| 174 | chat("new", "New", Some(at(30))), |
| 175 | chat("mid", "Mid", Some(at(10))), |
| 176 | ]) |
| 177 | .unwrap(); |
| 178 | let recent = store.recent_chats(3).unwrap(); |
| 179 | let order: Vec<&str> = recent.iter().map(|chat| chat.id.as_str()).collect(); |
| 180 | assert_eq!(order, ["new", "mid", "old"]); |
| 181 | assert_eq!(store.recent_chats(10).unwrap().last().unwrap().id, "none"); |
| 182 | } |
| 183 | |
| 184 | #[test] |
| 185 | fn messages_page_backwards_oldest_first() { |
| 186 | let store = Store::open_in_memory().unwrap(); |
| 187 | let all: Vec<MessageRecord> = (1..=10) |
| 188 | .map(|minute| message("chat-1", &format!("m{minute:02}"), minute)) |
| 189 | .collect(); |
| 190 | store.upsert_messages(&all).unwrap(); |
| 191 | store |
| 192 | .upsert_messages(&[message("chat-2", "other", 5)]) |
| 193 | .unwrap(); |
| 194 | |
| 195 | let newest = store.messages("chat-1", None, 4).unwrap(); |
| 196 | assert_eq!(ids(&newest), ["m07", "m08", "m09", "m10"]); |
| 197 | |
| 198 | let older = store |
| 199 | .messages("chat-1", Some(newest[0].created_at), 4) |
| 200 | .unwrap(); |
| 201 | assert_eq!(ids(&older), ["m03", "m04", "m05", "m06"]); |
| 202 | |
| 203 | let oldest = store |
| 204 | .messages("chat-1", Some(older[0].created_at), 4) |
| 205 | .unwrap(); |
| 206 | assert_eq!(ids(&oldest), ["m01", "m02"]); |
| 207 | assert!( |
| 208 | store |
| 209 | .messages("chat-1", Some(oldest[0].created_at), 4) |
| 210 | .unwrap() |
| 211 | .is_empty() |
| 212 | ); |
| 213 | assert_eq!(store.message_count("chat-1").unwrap(), 10); |
| 214 | } |
| 215 | |
| 216 | #[test] |
| 217 | fn equal_timestamps_keep_a_stable_order() { |
| 218 | let store = Store::open_in_memory().unwrap(); |
| 219 | store |
| 220 | .upsert_messages(&[ |
| 221 | message("c", "b", 1), |
| 222 | message("c", "a", 1), |
| 223 | message("c", "c", 1), |
| 224 | ]) |
| 225 | .unwrap(); |
| 226 | assert_eq!( |
| 227 | ids(&store.messages("c", None, 10).unwrap()), |
| 228 | ["a", "b", "c"] |
| 229 | ); |
| 230 | } |
| 231 | |
| 232 | #[test] |
| 233 | fn message_upsert_updates_in_place() { |
| 234 | let store = Store::open_in_memory().unwrap(); |
| 235 | let mut edited = message("c", "m1", 1); |
| 236 | store |
| 237 | .upsert_messages(std::slice::from_ref(&edited)) |
| 238 | .unwrap(); |
| 239 | edited.body_html = "<p>edited</p>".to_owned(); |
| 240 | edited.edited_at = Some(at(2)); |
| 241 | edited.reactions_json = r#"[{"type":"like"}]"#.to_owned(); |
| 242 | store |
| 243 | .upsert_messages(std::slice::from_ref(&edited)) |
| 244 | .unwrap(); |
| 245 | |
| 246 | let stored = store.messages("c", None, 10).unwrap(); |
| 247 | assert_eq!(stored, vec![edited.clone()]); |
| 248 | let by_id = store |
| 249 | .messages_by_id("c", &["m1".to_owned(), "nope".to_owned()]) |
| 250 | .unwrap(); |
| 251 | assert_eq!(by_id.len(), 1); |
| 252 | assert_eq!(by_id["m1"], edited); |
| 253 | } |
| 254 | |
| 255 | #[test] |
| 256 | fn deleted_flag_and_json_columns_round_trip() { |
| 257 | let store = Store::open_in_memory().unwrap(); |
| 258 | let mut record = message("c", "m1", 1); |
| 259 | record.deleted = true; |
| 260 | record.reply_to_id = Some("m0".to_owned()); |
| 261 | record.attachments_json = r#"[{"name":"a.pdf"}]"#.to_owned(); |
| 262 | record.mentions_json = r#"[{"id":"u1","name":"Ada Example"}]"#.to_owned(); |
| 263 | store |
| 264 | .upsert_messages(std::slice::from_ref(&record)) |
| 265 | .unwrap(); |
| 266 | assert_eq!(store.messages("c", None, 1).unwrap(), vec![record]); |
| 267 | } |
| 268 | |
| 269 | #[test] |
| 270 | fn thread_has_sender_checks_root_and_replies() { |
| 271 | let store = Store::open_in_memory().unwrap(); |
| 272 | let mut reply = message("c", "r1", 2); |
| 273 | reply.reply_to_id = Some("root".to_owned()); |
| 274 | reply.sender_id = Some("user-bob".to_owned()); |
| 275 | let mut other_reply = message("c", "r2", 3); |
| 276 | other_reply.reply_to_id = Some("other".to_owned()); |
| 277 | other_reply.sender_id = Some("user-carl".to_owned()); |
| 278 | store |
| 279 | .upsert_messages(&[message("c", "root", 1), reply, other_reply]) |
| 280 | .unwrap(); |
| 281 | assert!(store.thread_has_sender("c", "root", "user-ada").unwrap()); |
| 282 | assert!(store.thread_has_sender("c", "root", "user-bob").unwrap()); |
| 283 | assert!(!store.thread_has_sender("c", "root", "user-carl").unwrap()); |
| 284 | assert!(!store.thread_has_sender("d", "root", "user-ada").unwrap()); |
| 285 | } |
| 286 | |
| 287 | #[test] |
| 288 | fn sync_state_round_trip() { |
| 289 | let store = Store::open_in_memory().unwrap(); |
| 290 | assert_eq!(store.sync_state("c").unwrap(), None); |
| 291 | let state = SyncState { |
| 292 | newest_seen: Some(at(9)), |
| 293 | oldest_loaded: Some(at(1)), |
| 294 | has_more: false, |
| 295 | older_cursor: Some("https://graph.microsoft.com/v1.0/next".to_owned()), |
| 296 | delta_link: Some("https://graph.microsoft.com/v1.0/delta".to_owned()), |
| 297 | }; |
| 298 | store.set_sync_state("c", &state).unwrap(); |
| 299 | assert_eq!(store.sync_state("c").unwrap(), Some(state)); |
| 300 | let reset = SyncState { |
| 301 | has_more: true, |
| 302 | ..SyncState::default() |
| 303 | }; |
| 304 | store.set_sync_state("c", &reset).unwrap(); |
| 305 | assert_eq!(store.sync_state("c").unwrap(), Some(reset)); |
| 306 | } |
| 307 | |
| 308 | #[test] |
| 309 | fn sidebar_groups_channels_under_teams() { |
| 310 | let store = Store::open_in_memory().unwrap(); |
| 311 | store |
| 312 | .upsert_chats(&[chat("chat-1", "Planning", Some(at(5)))]) |
| 313 | .unwrap(); |
| 314 | store |
| 315 | .upsert_teams(&[ |
| 316 | TeamRecord { |
| 317 | id: "t2".to_owned(), |
| 318 | name: "beta".to_owned(), |
| 319 | }, |
| 320 | TeamRecord { |
| 321 | id: "t1".to_owned(), |
| 322 | name: "Alpha".to_owned(), |
| 323 | }, |
| 324 | ]) |
| 325 | .unwrap(); |
| 326 | let channel = |id: &str, team_id: &str, name: &str| ChannelRecord { |
| 327 | id: id.to_owned(), |
| 328 | team_id: team_id.to_owned(), |
| 329 | name: name.to_owned(), |
| 330 | membership_type: Some("standard".to_owned()), |
| 331 | last_message_at: None, |
| 332 | unread: false, |
| 333 | }; |
| 334 | store |
| 335 | .upsert_channels(&[ |
| 336 | channel("c2", "t1", "Random"), |
| 337 | channel("c1", "t1", "General"), |
| 338 | channel("c3", "t2", "General"), |
| 339 | ]) |
| 340 | .unwrap(); |
| 341 | |
| 342 | let sidebar = store.sidebar().unwrap(); |
| 343 | assert_eq!(sidebar.chats.len(), 1); |
| 344 | let team_names: Vec<&str> = sidebar |
| 345 | .teams |
| 346 | .iter() |
| 347 | .map(|entry| entry.team.name.as_str()) |
| 348 | .collect(); |
| 349 | assert_eq!(team_names, ["Alpha", "beta"]); |
| 350 | let alpha_channels: Vec<&str> = sidebar.teams[0] |
| 351 | .channels |
| 352 | .iter() |
| 353 | .map(|entry| entry.name.as_str()) |
| 354 | .collect(); |
| 355 | assert_eq!(alpha_channels, ["General", "Random"]); |
| 356 | assert_eq!(store.channel("c3").unwrap().unwrap().team_id, "t2"); |
| 357 | } |
| 358 | |
| 359 | #[test] |
| 360 | fn channel_upsert_keeps_known_last_message_time() { |
| 361 | let store = Store::open_in_memory().unwrap(); |
| 362 | store |
| 363 | .upsert_teams(&[TeamRecord { |
| 364 | id: "t1".to_owned(), |
| 365 | name: "A".to_owned(), |
| 366 | }]) |
| 367 | .unwrap(); |
| 368 | let mut channel = ChannelRecord { |
| 369 | id: "c1".to_owned(), |
| 370 | team_id: "t1".to_owned(), |
| 371 | name: "General".to_owned(), |
| 372 | membership_type: None, |
| 373 | last_message_at: Some(at(7)), |
| 374 | unread: false, |
| 375 | }; |
| 376 | store |
| 377 | .upsert_channels(std::slice::from_ref(&channel)) |
| 378 | .unwrap(); |
| 379 | channel.last_message_at = None; |
| 380 | store |
| 381 | .upsert_channels(std::slice::from_ref(&channel)) |
| 382 | .unwrap(); |
| 383 | assert_eq!( |
| 384 | store.channel("c1").unwrap().unwrap().last_message_at, |
| 385 | Some(at(7)) |
| 386 | ); |
| 387 | } |
| 388 | |
| 389 | #[test] |
| 390 | fn pruning_removes_departed_teams_and_channels() { |
| 391 | let store = Store::open_in_memory().unwrap(); |
| 392 | store |
| 393 | .upsert_teams(&[ |
| 394 | TeamRecord { |
| 395 | id: "t1".to_owned(), |
| 396 | name: "A".to_owned(), |
| 397 | }, |
| 398 | TeamRecord { |
| 399 | id: "t2".to_owned(), |
| 400 | name: "B".to_owned(), |
| 401 | }, |
| 402 | ]) |
| 403 | .unwrap(); |
| 404 | let channel = |id: &str| ChannelRecord { |
| 405 | id: id.to_owned(), |
| 406 | team_id: "t1".to_owned(), |
| 407 | name: id.to_owned(), |
| 408 | membership_type: None, |
| 409 | last_message_at: None, |
| 410 | unread: false, |
| 411 | }; |
| 412 | store |
| 413 | .upsert_channels(&[channel("c1"), channel("c2")]) |
| 414 | .unwrap(); |
| 415 | assert_eq!( |
| 416 | store |
| 417 | .remove_channels_except("t1", &["c1".to_owned()]) |
| 418 | .unwrap(), |
| 419 | 1 |
| 420 | ); |
| 421 | assert_eq!(store.remove_teams_except(&["t1".to_owned()]).unwrap(), 1); |
| 422 | let sidebar = store.sidebar().unwrap(); |
| 423 | assert_eq!(sidebar.teams.len(), 1); |
| 424 | assert_eq!(sidebar.teams[0].channels.len(), 1); |
| 425 | } |
| 426 | |
| 427 | #[test] |
| 428 | fn file_database_uses_wal_persists_and_migrates_once() { |
| 429 | let directory = tempfile::tempdir().unwrap(); |
| 430 | let path = directory.path().join("nested").join("cache.sqlite3"); |
| 431 | { |
| 432 | let store = Store::open(&path).unwrap(); |
| 433 | assert_eq!(store.schema_version().unwrap(), 12); |
| 434 | store.upsert_messages(&[message("c", "m1", 1)]).unwrap(); |
| 435 | } |
| 436 | let reopened = Store::open(&path).unwrap(); |
| 437 | assert_eq!(reopened.schema_version().unwrap(), 12); |
| 438 | assert_eq!(reopened.message_count("c").unwrap(), 1); |
| 439 | drop(reopened); |
| 440 | let mode: String = rusqlite_open(&path) |
| 441 | .pragma_query_value(None, "journal_mode", |row| row.get(0)) |
| 442 | .unwrap(); |
| 443 | assert_eq!(mode, "wal"); |
| 444 | } |
| 445 | |
| 446 | #[test] |
| 447 | fn newer_schema_is_refused() { |
| 448 | let directory = tempfile::tempdir().unwrap(); |
| 449 | let path = directory.path().join("cache.sqlite3"); |
| 450 | drop(Store::open(&path).unwrap()); |
| 451 | let connection = rusqlite_open(&path); |
| 452 | connection |
| 453 | .execute_batch("PRAGMA user_version = 99") |
| 454 | .unwrap(); |
| 455 | drop(connection); |
| 456 | let error = Store::open(&path).err().expect("must refuse"); |
| 457 | assert!(error.to_string().contains("99"), "{error}"); |
| 458 | } |
| 459 | |
| 460 | #[test] |
| 461 | fn old_schema_is_upgraded_in_place() { |
| 462 | let directory = tempfile::tempdir().unwrap(); |
| 463 | let path = directory.path().join("cache.sqlite3"); |
| 464 | drop(Store::open(&path).unwrap()); |
| 465 | let connection = rusqlite_open(&path); |
| 466 | 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 presence; DROP TABLE activity; DROP TABLE outbox; DROP TABLE drafts; DROP TABLE attachment_images; PRAGMA user_version = 0").unwrap(); |
| 467 | drop(connection); |
| 468 | let store = Store::open(&path).unwrap(); |
| 469 | assert_eq!(store.schema_version().unwrap(), 12); |
| 470 | store.upsert_messages(&[message("c", "m1", 1)]).unwrap(); |
| 471 | } |
| 472 | |
| 473 | fn rusqlite_open(path: &std::path::Path) -> Connection { |
| 474 | Connection::open(path).unwrap() |
| 475 | } |
| 476 | |
| 477 | #[test] |
| 478 | fn meta_values_and_times_round_trip() { |
| 479 | let store = Store::open_in_memory().unwrap(); |
| 480 | assert_eq!(store.meta("k").unwrap(), None); |
| 481 | store.set_meta("k", "one").unwrap(); |
| 482 | store.set_meta("k", "two").unwrap(); |
| 483 | assert_eq!(store.meta("k").unwrap().as_deref(), Some("two")); |
| 484 | assert_eq!(store.meta_time("t").unwrap(), None); |
| 485 | store.set_meta_time("t", at(7)).unwrap(); |
| 486 | assert_eq!(store.meta_time("t").unwrap(), Some(at(7))); |
| 487 | store.set_meta("bad", "not a number").unwrap(); |
| 488 | assert_eq!(store.meta_time("bad").unwrap(), None); |
| 489 | } |
| 490 | |
| 491 | #[test] |
| 492 | fn chats_can_be_pruned_and_marked_read() { |
| 493 | let store = Store::open_in_memory().unwrap(); |
| 494 | store |
| 495 | .upsert_chats(&[ |
| 496 | chat("a", "A", Some(at(1))), |
| 497 | chat("b", "B", Some(at(2))), |
| 498 | chat("c", "C", None), |
| 499 | ]) |
| 500 | .unwrap(); |
| 501 | let times = store |
| 502 | .chat_last_message_times(&["a".to_owned(), "c".to_owned(), "zzz".to_owned()]) |
| 503 | .unwrap(); |
| 504 | assert_eq!(times.len(), 2); |
| 505 | assert_eq!(times["a"], Some(at(1))); |
| 506 | assert_eq!(times["c"], None); |
| 507 | |
| 508 | assert_eq!(store.remove_chats(&["a".to_owned()]).unwrap(), 1); |
| 509 | assert_eq!(store.remove_chats_except(&["b".to_owned()]).unwrap(), 1); |
| 510 | assert_eq!(store.chat_count().unwrap(), 1); |
| 511 | |
| 512 | store.mark_chat_read("b", at(30)).unwrap(); |
| 513 | let read = store.chat("b").unwrap().unwrap(); |
| 514 | assert!(!read.unread); |
| 515 | assert_eq!(read.last_read_at, Some(at(30))); |
| 516 | } |
| 517 | |
| 518 | #[test] |
| 519 | fn migration_to_v2_keeps_existing_sync_state() { |
| 520 | let directory = tempfile::tempdir().unwrap(); |
| 521 | let path = directory.path().join("old.sqlite3"); |
| 522 | { |
| 523 | let connection = Connection::open(&path).unwrap(); |
| 524 | connection |
| 525 | .execute_batch(include_str!("../src/migrations/0001_initial.sql")) |
| 526 | .unwrap(); |
| 527 | connection.pragma_update(None, "user_version", 1).unwrap(); |
| 528 | connection |
| 529 | .execute("INSERT INTO sync_state (conversation_id, newest_seen, has_more) VALUES ('c', 5, 1)", []) |
| 530 | .unwrap(); |
| 531 | } |
| 532 | let store = Store::open(&path).unwrap(); |
| 533 | let state = store.sync_state("c").unwrap().unwrap(); |
| 534 | assert_eq!(state.delta_link, None); |
| 535 | assert_eq!(store.schema_version().unwrap(), 12); |
| 536 | } |
| 537 | |
| 538 | #[test] |
| 539 | fn preview_updates_only_for_newer_messages() { |
| 540 | let store = Store::open_in_memory().unwrap(); |
| 541 | store.upsert_chats(&[chat("a", "A", Some(at(5)))]).unwrap(); |
| 542 | let preview = |text| ChatPreview { |
| 543 | text: Some(text), |
| 544 | sender_id: Some("user-ada"), |
| 545 | sender_name: Some("Ada Example"), |
| 546 | deleted: false, |
| 547 | }; |
| 548 | assert!( |
| 549 | !store |
| 550 | .update_chat_preview("a", at(4), &preview("older")) |
| 551 | .unwrap() |
| 552 | ); |
| 553 | assert!( |
| 554 | store |
| 555 | .update_chat_preview("a", at(6), &preview("newer")) |
| 556 | .unwrap() |
| 557 | ); |
| 558 | let record = store.chat("a").unwrap().unwrap(); |
| 559 | assert_eq!(record.last_message_preview.as_deref(), Some("newer")); |
| 560 | assert_eq!( |
| 561 | record.last_message_sender_name.as_deref(), |
| 562 | Some("Ada Example") |
| 563 | ); |
| 564 | assert_eq!(record.last_message_at, Some(at(6))); |
| 565 | assert!(!record.last_message_deleted); |
| 566 | } |
| 567 | |
| 568 | #[test] |
| 569 | fn avatars_round_trip_and_report_stale_ids() { |
| 570 | let store = Store::open_in_memory().unwrap(); |
| 571 | store |
| 572 | .upsert_avatar(&AvatarRecord { |
| 573 | user_id: "fresh".into(), |
| 574 | bytes: Some(vec![1, 2, 3]), |
| 575 | content_type: "image/jpeg".into(), |
| 576 | fetched_at: at(50), |
| 577 | }) |
| 578 | .unwrap(); |
| 579 | store |
| 580 | .upsert_avatar(&AvatarRecord { |
| 581 | user_id: "missing".into(), |
| 582 | bytes: None, |
| 583 | content_type: String::new(), |
| 584 | fetched_at: at(1), |
| 585 | }) |
| 586 | .unwrap(); |
| 587 | assert_eq!( |
| 588 | store.avatar("fresh").unwrap().unwrap().bytes, |
| 589 | Some(vec![1, 2, 3]) |
| 590 | ); |
| 591 | assert_eq!(store.avatar("missing").unwrap().unwrap().bytes, None); |
| 592 | assert!(store.avatar("unknown").unwrap().is_none()); |
| 593 | let ids: Vec<String> = ["fresh", "missing", "unknown"].map(String::from).to_vec(); |
| 594 | assert_eq!( |
| 595 | store.avatar_ids_needing_fetch(&ids, at(10)).unwrap(), |
| 596 | ["missing", "unknown"] |
| 597 | ); |
| 598 | } |
| 599 | |
| 600 | #[test] |
| 601 | fn folders_and_pinned_channels_are_replaced_in_order() { |
| 602 | let store = Store::open_in_memory().unwrap(); |
| 603 | let folder = |id: &str, kind: &str, items: &[&str]| FolderRecord { |
| 604 | id: id.into(), |
| 605 | name: id.into(), |
| 606 | kind: kind.into(), |
| 607 | conversation_ids: items.iter().map(|item| item.to_string()).collect(), |
| 608 | }; |
| 609 | let first = [ |
| 610 | folder("f1", "Favorites", &["b", "a"]), |
| 611 | folder("f2", "UserCreated", &[]), |
| 612 | ]; |
| 613 | store |
| 614 | .replace_folders(&first, &["c2".into(), "c1".into()]) |
| 615 | .unwrap(); |
| 616 | assert_eq!(store.folders().unwrap(), first); |
| 617 | assert_eq!(store.pinned_channel_ids().unwrap(), ["c2", "c1"]); |
| 618 | store |
| 619 | .replace_folders(&[folder("f3", "UserCreated", &["z"])], &[]) |
| 620 | .unwrap(); |
| 621 | assert_eq!(store.folders().unwrap().len(), 1); |
| 622 | assert!(store.pinned_channel_ids().unwrap().is_empty()); |
| 623 | } |
| 624 | |
| 625 | #[test] |
| 626 | fn counts_messages_after_a_time_from_others() { |
| 627 | let store = Store::open_in_memory().unwrap(); |
| 628 | let mut mine = message("c", "m3", 3); |
| 629 | mine.sender_id = Some("me".into()); |
| 630 | store |
| 631 | .upsert_messages(&[message("c", "m1", 1), message("c", "m2", 2), mine]) |
| 632 | .unwrap(); |
| 633 | assert_eq!( |
| 634 | store.count_messages_after("c", Some(at(1)), "me").unwrap(), |
| 635 | 1 |
| 636 | ); |
| 637 | assert_eq!(store.count_messages_after("c", None, "me").unwrap(), 2); |
| 638 | } |
| 639 | |
| 640 | fn html_message( |
| 641 | conversation_id: &str, |
| 642 | message_id: &str, |
| 643 | minute: u32, |
| 644 | sender: &str, |
| 645 | html: &str, |
| 646 | ) -> MessageRecord { |
| 647 | MessageRecord { |
| 648 | sender_name: Some(sender.to_owned()), |
| 649 | body_html: html.to_owned(), |
| 650 | ..message(conversation_id, message_id, minute) |
| 651 | } |
| 652 | } |
| 653 | |
| 654 | #[test] |
| 655 | fn fts5_is_available_and_matches_prefixes_with_highlights() { |
| 656 | let store = Store::open_in_memory().unwrap(); |
| 657 | store |
| 658 | .upsert_messages(&[ |
| 659 | html_message( |
| 660 | "c1", |
| 661 | "m1", |
| 662 | 1, |
| 663 | "Ada Example", |
| 664 | "<p>The <b>budget</b> planning is done</p>", |
| 665 | ), |
| 666 | html_message("c1", "m2", 2, "Bob Sample", "<p>Lunch at noon</p>"), |
| 667 | html_message("c2", "m3", 3, "Bob Sample", "<p>Budget approved</p>"), |
| 668 | ]) |
| 669 | .unwrap(); |
| 670 | let hits = store.search_messages("budg plan", None, 10).unwrap(); |
| 671 | assert_eq!(hits.len(), 1); |
| 672 | assert_eq!(hits[0].message_id, "m1"); |
| 673 | assert_eq!(hits[0].sender_name.as_deref(), Some("Ada Example")); |
| 674 | assert_eq!(hits[0].created_at, at(1)); |
| 675 | assert!( |
| 676 | hits[0].snippet.contains("\u{1}budget\u{2}"), |
| 677 | "{}", |
| 678 | hits[0].snippet |
| 679 | ); |
| 680 | let all = store.search_messages("budget", None, 10).unwrap(); |
| 681 | assert_eq!(ids_of(&all), ["m3", "m1"]); |
| 682 | let scoped = store.search_messages("budget", Some("c1"), 10).unwrap(); |
| 683 | assert_eq!(ids_of(&scoped), ["m1"]); |
| 684 | assert_eq!(store.search_messages("budget", None, 1).unwrap().len(), 1); |
| 685 | } |
| 686 | |
| 687 | fn ids_of(hits: &[store::SearchHit]) -> Vec<&str> { |
| 688 | hits.iter().map(|hit| hit.message_id.as_str()).collect() |
| 689 | } |
| 690 | |
| 691 | #[test] |
| 692 | fn search_filters_by_sender_and_ignores_unsearchable_input() { |
| 693 | let store = Store::open_in_memory().unwrap(); |
| 694 | store |
| 695 | .upsert_messages(&[ |
| 696 | html_message("c1", "m1", 1, "Ada Example", "<p>report</p>"), |
| 697 | html_message("c1", "m2", 2, "Bob Sample", "<p>report</p>"), |
| 698 | ]) |
| 699 | .unwrap(); |
| 700 | assert_eq!( |
| 701 | ids_of(&store.search_messages("from:bob report", None, 10).unwrap()), |
| 702 | ["m2"] |
| 703 | ); |
| 704 | assert!(store.search_messages(" ", None, 10).unwrap().is_empty()); |
| 705 | assert!(store.search_messages("\"-*", None, 10).unwrap().is_empty()); |
| 706 | assert!(store.search_messages("rep) OR (", None, 10).is_ok()); |
| 707 | } |
| 708 | |
| 709 | #[test] |
| 710 | fn search_follows_edits_and_deletes() { |
| 711 | let store = Store::open_in_memory().unwrap(); |
| 712 | store |
| 713 | .upsert_messages(&[html_message("c1", "m1", 1, "Ada", "<p>alpha</p>")]) |
| 714 | .unwrap(); |
| 715 | store |
| 716 | .upsert_messages(&[html_message("c1", "m1", 1, "Ada", "<p>beta</p>")]) |
| 717 | .unwrap(); |
| 718 | assert!(store.search_messages("alpha", None, 10).unwrap().is_empty()); |
| 719 | assert_eq!(store.search_messages("beta", None, 10).unwrap().len(), 1); |
| 720 | let mut deleted = html_message("c1", "m1", 1, "Ada", ""); |
| 721 | deleted.deleted = true; |
| 722 | store.upsert_messages(&[deleted]).unwrap(); |
| 723 | assert!(store.search_messages("beta", None, 10).unwrap().is_empty()); |
| 724 | } |
| 725 | |
| 726 | #[test] |
| 727 | fn title_search_follows_chat_renames() { |
| 728 | let store = Store::open_in_memory().unwrap(); |
| 729 | store |
| 730 | .upsert_chats(&[chat("c1", "Budget planning", None)]) |
| 731 | .unwrap(); |
| 732 | assert_eq!( |
| 733 | store.search_conversations("budg", 10).unwrap()[0].conversation_id, |
| 734 | "c1" |
| 735 | ); |
| 736 | store.upsert_chats(&[chat("c1", "Holiday", None)]).unwrap(); |
| 737 | assert!(store.search_conversations("budget", 10).unwrap().is_empty()); |
| 738 | assert_eq!(store.search_conversations("holi", 10).unwrap().len(), 1); |
| 739 | assert!( |
| 740 | store |
| 741 | .search_conversations("from:holi", 10) |
| 742 | .unwrap() |
| 743 | .is_empty() |
| 744 | ); |
| 745 | } |
| 746 | |
| 747 | #[test] |
| 748 | fn migration_indexes_rows_that_predate_search() { |
| 749 | let directory = tempfile::tempdir().unwrap(); |
| 750 | let path = directory.path().join("cache.sqlite3"); |
| 751 | { |
| 752 | let store = Store::open(&path).unwrap(); |
| 753 | store.upsert_chats(&[chat("c1", "Old chat", None)]).unwrap(); |
| 754 | store |
| 755 | .upsert_messages(&[html_message("c1", "m1", 1, "Ada", "<p>legacy text</p>")]) |
| 756 | .unwrap(); |
| 757 | } |
| 758 | { |
| 759 | let connection = Connection::open(&path).unwrap(); |
| 760 | connection |
| 761 | .execute_batch( |
| 762 | "DROP TRIGGER chats_title_insert; DROP TRIGGER chats_title_update; DROP TRIGGER chats_title_delete; |
| 763 | DROP TRIGGER channels_title_insert; DROP TRIGGER channels_title_update; DROP TRIGGER channels_title_delete; |
| 764 | DROP TABLE images; DROP TABLE search_keys; DROP TABLE message_search; DROP TABLE title_search; |
| 765 | DROP TABLE team_layout; DROP TABLE channel_layout; DROP TABLE presence; DROP TABLE activity; DROP TABLE outbox; DROP TABLE drafts; DROP TABLE attachment_images; |
| 766 | DROP INDEX messages_by_sender; DROP INDEX chat_members_by_user; |
| 767 | ALTER TABLE messages DROP COLUMN sender_application_id; |
| 768 | ALTER TABLE messages DROP COLUMN links_json; |
| 769 | ALTER TABLE chats DROP COLUMN muted; |
| 770 | PRAGMA user_version = 3;", |
| 771 | ) |
| 772 | .unwrap(); |
| 773 | } |
| 774 | let store = Store::open(&path).unwrap(); |
| 775 | assert_eq!(store.search_messages("legacy", None, 10).unwrap().len(), 1); |
| 776 | assert_eq!(store.search_conversations("old", 10).unwrap().len(), 1); |
| 777 | } |
| 778 | |
| 779 | fn image(key: &str, minute: u32, size: usize) -> store::ImageRecord { |
| 780 | store::ImageRecord { |
| 781 | key: key.to_owned(), |
| 782 | bytes: vec![7; size], |
| 783 | content_type: "image/png".to_owned(), |
| 784 | fetched_at: at(minute), |
| 785 | } |
| 786 | } |
| 787 | |
| 788 | #[test] |
| 789 | fn images_round_trip_and_evict_the_oldest_over_the_cap() { |
| 790 | let store = Store::open_in_memory().unwrap(); |
| 791 | store.put_image(&image("a", 1, 40), 100).unwrap(); |
| 792 | store.put_image(&image("b", 2, 40), 100).unwrap(); |
| 793 | assert_eq!(store.image("a").unwrap().unwrap().bytes.len(), 40); |
| 794 | store.put_image(&image("c", 3, 40), 100).unwrap(); |
| 795 | assert!(store.image("a").unwrap().is_none()); |
| 796 | assert!(store.has_image("b").unwrap()); |
| 797 | assert!(store.has_image("c").unwrap()); |
| 798 | assert_eq!(store.image_cache_bytes().unwrap(), 80); |
| 799 | store.put_image(&image("big", 0, 500), 100).unwrap(); |
| 800 | assert!(store.has_image("big").unwrap()); |
| 801 | assert_eq!(store.image_cache_bytes().unwrap(), 500); |
| 802 | } |
| 803 | |
| 804 | #[test] |
| 805 | fn first_message_after_from_others_skips_own_and_deleted() { |
| 806 | let store = Store::open_in_memory().unwrap(); |
| 807 | let mut own = message("c1", "own", 2); |
| 808 | own.sender_id = Some("me".to_owned()); |
| 809 | let mut gone = message("c1", "gone", 3); |
| 810 | gone.deleted = true; |
| 811 | store |
| 812 | .upsert_messages(&[message("c1", "old", 1), own, gone, message("c1", "new", 4)]) |
| 813 | .unwrap(); |
| 814 | assert_eq!( |
| 815 | store |
| 816 | .first_message_after_from_others("c1", Some(at(1)), "me") |
| 817 | .unwrap() |
| 818 | .as_deref(), |
| 819 | Some("new") |
| 820 | ); |
| 821 | assert_eq!( |
| 822 | store |
| 823 | .first_message_after_from_others("c1", None, "me") |
| 824 | .unwrap() |
| 825 | .as_deref(), |
| 826 | Some("old") |
| 827 | ); |
| 828 | assert!( |
| 829 | store |
| 830 | .first_message_after_from_others("c1", Some(at(4)), "me") |
| 831 | .unwrap() |
| 832 | .is_none() |
| 833 | ); |
| 834 | } |
| 835 | |
| 836 | #[test] |
| 837 | fn a_team_is_found_by_id() { |
| 838 | let store = Store::open_in_memory().unwrap(); |
| 839 | store |
| 840 | .upsert_teams(&[TeamRecord { |
| 841 | id: "t1".to_owned(), |
| 842 | name: "Alpha".to_owned(), |
| 843 | }]) |
| 844 | .unwrap(); |
| 845 | assert_eq!(store.team("t1").unwrap().unwrap().name, "Alpha"); |
| 846 | assert!(store.team("nope").unwrap().is_none()); |
| 847 | } |
| 848 | |
| 849 | #[test] |
| 850 | fn sync_does_not_resurrect_a_locally_read_chat() { |
| 851 | let store = Store::open_in_memory().unwrap(); |
| 852 | store |
| 853 | .upsert_chats(&[chat("chat-1", "Planning", Some(at(5)))]) |
| 854 | .unwrap(); |
| 855 | store.mark_chat_read("chat-1", at(10)).unwrap(); |
| 856 | |
| 857 | store |
| 858 | .upsert_chats(&[chat("chat-1", "Planning", Some(at(5)))]) |
| 859 | .unwrap(); |
| 860 | let stale = store.chat("chat-1").unwrap().unwrap(); |
| 861 | assert!(!stale.unread); |
| 862 | assert_eq!(stale.last_read_at, Some(at(10))); |
| 863 | |
| 864 | store |
| 865 | .upsert_chats(&[chat("chat-1", "Planning", Some(at(12)))]) |
| 866 | .unwrap(); |
| 867 | assert!(store.chat("chat-1").unwrap().unwrap().unread); |
| 868 | } |
| 869 | |
| 870 | #[test] |
| 871 | fn mark_chat_unread_sets_the_flag_and_horizon() { |
| 872 | let store = Store::open_in_memory().unwrap(); |
| 873 | store |
| 874 | .upsert_chats(&[chat("chat-1", "Planning", Some(at(5)))]) |
| 875 | .unwrap(); |
| 876 | store.mark_chat_read("chat-1", at(10)).unwrap(); |
| 877 | |
| 878 | store.mark_chat_unread("chat-1", at(4)).unwrap(); |
| 879 | let unread = store.chat("chat-1").unwrap().unwrap(); |
| 880 | assert!(unread.unread); |
| 881 | assert_eq!(unread.last_read_at, Some(at(4))); |
| 882 | } |
| 883 | |
| 884 | #[test] |
| 885 | fn muted_flag_survives_a_chat_refresh() { |
| 886 | let store = Store::open_in_memory().unwrap(); |
| 887 | store |
| 888 | .upsert_chats(&[chat("a", "A", Some(at(1))), chat("b", "B", Some(at(2)))]) |
| 889 | .unwrap(); |
| 890 | assert!(!store.chat("a").unwrap().unwrap().muted); |
| 891 | |
| 892 | store.set_chat_muted("a", true).unwrap(); |
| 893 | store |
| 894 | .upsert_chats(&[chat("a", "A", Some(at(3))), chat("b", "B", Some(at(2)))]) |
| 895 | .unwrap(); |
| 896 | assert!(store.chat("a").unwrap().unwrap().muted); |
| 897 | assert!(!store.chat("b").unwrap().unwrap().muted); |
| 898 | |
| 899 | store.set_chat_muted("a", false).unwrap(); |
| 900 | assert!(!store.chat("a").unwrap().unwrap().muted); |
| 901 | } |
| 902 | |
| 903 | #[test] |
| 904 | fn muted_states_touch_only_the_listed_chats() { |
| 905 | let store = Store::open_in_memory().unwrap(); |
| 906 | store |
| 907 | .upsert_chats(&[ |
| 908 | chat("a", "A", Some(at(1))), |
| 909 | chat("b", "B", Some(at(2))), |
| 910 | chat("c", "C", Some(at(3))), |
| 911 | ]) |
| 912 | .unwrap(); |
| 913 | store.set_chat_muted("a", true).unwrap(); |
| 914 | store.set_chat_muted("c", true).unwrap(); |
| 915 | |
| 916 | store |
| 917 | .apply_muted_states(&[ |
| 918 | ("a".to_owned(), false), |
| 919 | ("b".to_owned(), true), |
| 920 | ("gone".to_owned(), true), |
| 921 | ]) |
| 922 | .unwrap(); |
| 923 | assert!(!store.chat("a").unwrap().unwrap().muted); |
| 924 | assert!(store.chat("b").unwrap().unwrap().muted); |
| 925 | assert!(store.chat("c").unwrap().unwrap().muted); |
| 926 | } |
| 927 | |
| 928 | #[test] |
| 929 | fn presence_upsert_keeps_the_latest_availability() { |
| 930 | let store = Store::open_in_memory().unwrap(); |
| 931 | let first = [("ada".to_owned(), "Available".to_owned())]; |
| 932 | store.upsert_presences(&first, at(1)).unwrap(); |
| 933 | let second = [ |
| 934 | ("ada".to_owned(), "Away".to_owned()), |
| 935 | ("bob".to_owned(), "Busy".to_owned()), |
| 936 | ]; |
| 937 | store.upsert_presences(&second, at(2)).unwrap(); |
| 938 | let mut presences = store.presences().unwrap(); |
| 939 | presences.sort(); |
| 940 | assert_eq!(presences, second.to_vec()); |
| 941 | } |
| 942 | |
| 943 | fn activity(id: i64, minute: u32) -> store::ActivityRecord { |
| 944 | store::ActivityRecord { |
| 945 | id, |
| 946 | conversation_id: "c1".to_owned(), |
| 947 | kind: "messages".to_owned(), |
| 948 | message_id: format!("m{id}"), |
| 949 | actors_json: r#"[{"user_id":"user-ada","name":"Ada Example"}]"#.to_owned(), |
| 950 | preview: "hello".to_owned(), |
| 951 | glyphs: String::new(), |
| 952 | count: 1, |
| 953 | updated_at: at(minute), |
| 954 | read: false, |
| 955 | } |
| 956 | } |
| 957 | |
| 958 | #[test] |
| 959 | fn activity_upsert_inserts_then_updates_in_place() { |
| 960 | let store = Store::open_in_memory().unwrap(); |
| 961 | store |
| 962 | .upsert_activity(&[activity(1, 5), activity(2, 9)]) |
| 963 | .unwrap(); |
| 964 | let mut changed = activity(1, 12); |
| 965 | changed.count = 3; |
| 966 | changed.read = true; |
| 967 | store |
| 968 | .upsert_activity(std::slice::from_ref(&changed)) |
| 969 | .unwrap(); |
| 970 | let records = store.activity().unwrap(); |
| 971 | assert_eq!(records.len(), 2); |
| 972 | assert_eq!(records[0], changed); |
| 973 | assert_eq!(records[1].id, 2); |
| 974 | } |
| 975 | |
| 976 | #[test] |
| 977 | fn activity_prune_drops_only_older_rows() { |
| 978 | let store = Store::open_in_memory().unwrap(); |
| 979 | store |
| 980 | .upsert_activity(&[activity(1, 5), activity(2, 9), activity(3, 20)]) |
| 981 | .unwrap(); |
| 982 | assert_eq!(store.prune_activity(at(9)).unwrap(), 1); |
| 983 | let ids: Vec<i64> = store |
| 984 | .activity() |
| 985 | .unwrap() |
| 986 | .iter() |
| 987 | .map(|record| record.id) |
| 988 | .collect(); |
| 989 | assert_eq!(ids, vec![3, 2]); |
| 990 | } |
| 991 | |
| 992 | #[test] |
| 993 | fn display_names_prefer_members_then_latest_sender_name() { |
| 994 | let store = Store::open_in_memory().unwrap(); |
| 995 | store |
| 996 | .upsert_chats(&[chat("chat-1", "Planning", Some(at(5)))]) |
| 997 | .unwrap(); |
| 998 | let sent_by = |message_id: &str, minute: u32, sender: &str, name: Option<&str>| MessageRecord { |
| 999 | sender_id: Some(sender.to_owned()), |
| 1000 | sender_name: name.map(str::to_owned), |
| 1001 | ..message("channel", message_id, minute) |
| 1002 | }; |
| 1003 | store |
| 1004 | .upsert_messages(&[ |
| 1005 | sent_by("m1", 1, "user-ada", Some("Ada Sender")), |
| 1006 | sent_by("m2", 2, "user-sam", Some("Sam Old")), |
| 1007 | sent_by("m3", 3, "user-sam", Some("Sam New")), |
| 1008 | sent_by("m4", 4, "user-sam", None), |
| 1009 | sent_by("m5", 5, "user-blank", Some("")), |
| 1010 | ]) |
| 1011 | .unwrap(); |
| 1012 | let wanted: Vec<String> = ["user-ada", "user-sam", "user-blank", "user-none"] |
| 1013 | .map(str::to_owned) |
| 1014 | .to_vec(); |
| 1015 | let names = store.display_names(&wanted).unwrap(); |
| 1016 | assert_eq!(names.len(), 2); |
| 1017 | assert_eq!(names["user-ada"], "Ada Example"); |
| 1018 | assert_eq!(names["user-sam"], "Sam New"); |
| 1019 | assert!(store.display_names(&[]).unwrap().is_empty()); |
| 1020 | } |