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