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