| 1 | use std::time::{Duration, Instant}; |
| 2 | |
| 3 | use super::rules::{ChatKind, Incoming, Preview}; |
| 4 | |
| 5 | pub const MAX_VISIBLE: usize = 3; |
| 6 | pub const MESSAGE_DURATION: Duration = Duration::from_secs(6); |
| 7 | pub const MENTION_DURATION: Duration = Duration::from_secs(10); |
| 8 | pub const MAX_TOTAL: Duration = Duration::from_secs(20); |
| 9 | pub const RESUME_REMAINING: Duration = Duration::from_secs(2); |
| 10 | pub const SENT_DURATION: Duration = Duration::from_millis(1500); |
| 11 | pub const FADE_IN: Duration = Duration::from_millis(150); |
| 12 | pub const FAST_FADE: Duration = Duration::from_millis(150); |
| 13 | pub const SLOW_FADE: Duration = Duration::from_secs(4); |
| 14 | |
| 15 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 16 | pub enum Curve { |
| 17 | Linear, |
| 18 | EaseInCirc, |
| 19 | } |
| 20 | |
| 21 | #[derive(Debug, Clone, Copy, PartialEq)] |
| 22 | pub struct Fade { |
| 23 | from: f32, |
| 24 | to: f32, |
| 25 | start: Instant, |
| 26 | duration: Duration, |
| 27 | curve: Curve, |
| 28 | } |
| 29 | |
| 30 | impl Fade { |
| 31 | pub fn new(from: f32, to: f32, start: Instant, duration: Duration, curve: Curve) -> Self { |
| 32 | Fade { |
| 33 | from, |
| 34 | to, |
| 35 | start, |
| 36 | duration, |
| 37 | curve, |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | #[cfg(test)] |
| 42 | pub fn shown(now: Instant) -> Self { |
| 43 | Fade::new(1., 1., now, Duration::ZERO, Curve::Linear) |
| 44 | } |
| 45 | |
| 46 | pub fn value(&self, now: Instant) -> f32 { |
| 47 | if self.duration.is_zero() { |
| 48 | return self.to; |
| 49 | } |
| 50 | let progress = (now.saturating_duration_since(self.start).as_secs_f32() |
| 51 | / self.duration.as_secs_f32()) |
| 52 | .clamp(0., 1.); |
| 53 | let eased = match self.curve { |
| 54 | Curve::Linear => progress, |
| 55 | Curve::EaseInCirc => 1. - (1. - progress * progress).sqrt(), |
| 56 | }; |
| 57 | self.from + (self.to - self.from) * eased |
| 58 | } |
| 59 | |
| 60 | pub fn is_leaving(&self) -> bool { |
| 61 | self.to == 0. |
| 62 | } |
| 63 | |
| 64 | fn is_gone(&self, now: Instant) -> bool { |
| 65 | self.is_leaving() && now.saturating_duration_since(self.start) >= self.duration |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | #[derive(Debug, Clone, Copy)] |
| 70 | pub struct ToastTimer { |
| 71 | first_shown: Instant, |
| 72 | started: Instant, |
| 73 | duration: Duration, |
| 74 | paused: Option<Instant>, |
| 75 | paused_total: Duration, |
| 76 | } |
| 77 | |
| 78 | impl ToastTimer { |
| 79 | pub fn new(now: Instant, mention: bool) -> Self { |
| 80 | ToastTimer { |
| 81 | first_shown: now, |
| 82 | started: now, |
| 83 | duration: base_duration(mention), |
| 84 | paused: None, |
| 85 | paused_total: Duration::ZERO, |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | pub fn restart(&mut self, now: Instant, mention: bool) { |
| 90 | self.started = now; |
| 91 | self.duration = base_duration(mention); |
| 92 | if self.paused.is_some() { |
| 93 | self.paused = Some(now); |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | pub fn pause(&mut self, now: Instant) { |
| 98 | self.paused.get_or_insert(now); |
| 99 | } |
| 100 | |
| 101 | pub fn resume(&mut self, now: Instant) { |
| 102 | if let Some(paused_at) = self.paused.take() { |
| 103 | self.paused_total += now.saturating_duration_since(paused_at); |
| 104 | self.started = now; |
| 105 | self.duration = RESUME_REMAINING; |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | fn elapsed(&self, now: Instant) -> Duration { |
| 110 | self.paused |
| 111 | .unwrap_or(now) |
| 112 | .saturating_duration_since(self.started) |
| 113 | } |
| 114 | |
| 115 | pub fn expired(&self, now: Instant) -> bool { |
| 116 | if self.paused.is_some() { |
| 117 | return false; |
| 118 | } |
| 119 | self.elapsed(now) >= self.duration |
| 120 | || now |
| 121 | .saturating_duration_since(self.first_shown) |
| 122 | .saturating_sub(self.paused_total) |
| 123 | >= MAX_TOTAL |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | fn base_duration(mention: bool) -> Duration { |
| 128 | if mention { |
| 129 | MENTION_DURATION |
| 130 | } else { |
| 131 | MESSAGE_DURATION |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 136 | pub enum ReplyState { |
| 137 | Closed, |
| 138 | Open, |
| 139 | Sent, |
| 140 | Failed, |
| 141 | } |
| 142 | |
| 143 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 144 | pub enum AgentToastKind { |
| 145 | Run { |
| 146 | run_id: u64, |
| 147 | session_id: String, |
| 148 | failed: bool, |
| 149 | }, |
| 150 | Proposal { |
| 151 | proposal_id: String, |
| 152 | }, |
| 153 | } |
| 154 | |
| 155 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 156 | pub struct AgentToast { |
| 157 | pub kind: AgentToastKind, |
| 158 | pub header: String, |
| 159 | pub title: String, |
| 160 | pub body: String, |
| 161 | } |
| 162 | |
| 163 | impl AgentToast { |
| 164 | pub fn failed(&self) -> bool { |
| 165 | matches!(self.kind, AgentToastKind::Run { failed: true, .. }) |
| 166 | } |
| 167 | |
| 168 | pub fn proposal_id(&self) -> Option<&str> { |
| 169 | match &self.kind { |
| 170 | AgentToastKind::Proposal { proposal_id } => Some(proposal_id), |
| 171 | AgentToastKind::Run { .. } => None, |
| 172 | } |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | #[derive(Debug, Clone)] |
| 177 | pub struct ToastModel { |
| 178 | pub id: u64, |
| 179 | pub conversation_id: String, |
| 180 | pub message_id: String, |
| 181 | pub kind: ChatKind, |
| 182 | pub chat_title: String, |
| 183 | pub sender_id: Option<String>, |
| 184 | pub sender_name: String, |
| 185 | pub preview: Preview, |
| 186 | pub mentions_me: bool, |
| 187 | pub count: u32, |
| 188 | pub timer: ToastTimer, |
| 189 | pub reply: ReplyState, |
| 190 | pub reply_text: String, |
| 191 | pub hovered: bool, |
| 192 | pub time: String, |
| 193 | pub queued: bool, |
| 194 | pub dismissed: bool, |
| 195 | pub fade: Fade, |
| 196 | pub agent: Option<AgentToast>, |
| 197 | pub status_change: bool, |
| 198 | pub urgent: bool, |
| 199 | /// Set for a message in an org that is not open; clicking switches to that org. |
| 200 | pub org: Option<app_base::orgs::Org>, |
| 201 | } |
| 202 | |
| 203 | impl ToastModel { |
| 204 | pub fn holds_open(&self) -> bool { |
| 205 | self.reply != ReplyState::Closed |
| 206 | } |
| 207 | |
| 208 | pub fn opacity(&self, now: Instant) -> f32 { |
| 209 | self.fade.value(now) |
| 210 | } |
| 211 | |
| 212 | pub fn is_leaving(&self) -> bool { |
| 213 | self.fade.is_leaving() |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 218 | pub struct PushOutcome { |
| 219 | pub id: u64, |
| 220 | pub bundled: bool, |
| 221 | pub became_mention: bool, |
| 222 | } |
| 223 | |
| 224 | impl PushOutcome { |
| 225 | pub fn plays_sound(self, throttle_allows: bool) -> bool { |
| 226 | self.became_mention || (!self.bundled && throttle_allows) |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | #[derive(Default)] |
| 231 | pub struct ToastStack { |
| 232 | toasts: Vec<ToastModel>, |
| 233 | next_id: u64, |
| 234 | } |
| 235 | |
| 236 | impl ToastStack { |
| 237 | pub fn push(&mut self, incoming: &Incoming, now: Instant) -> PushOutcome { |
| 238 | if let Some(existing) = self.toasts.iter_mut().find(|toast| { |
| 239 | toast.agent.is_none() |
| 240 | && !toast.status_change |
| 241 | && toast.org.is_none() |
| 242 | && !toast.dismissed |
| 243 | && !toast.is_leaving() |
| 244 | && toast.conversation_id == incoming.conversation_id |
| 245 | }) { |
| 246 | let became_mention = incoming.mentions_me && !existing.mentions_me; |
| 247 | existing.count += 1; |
| 248 | existing.mentions_me |= incoming.mentions_me; |
| 249 | existing.message_id = incoming.message_id.clone(); |
| 250 | existing.sender_id = incoming.sender_id.clone(); |
| 251 | existing.sender_name = incoming.sender_name.clone(); |
| 252 | existing.preview = incoming.preview.clone(); |
| 253 | existing.urgent = incoming.is_urgent(); |
| 254 | let mention = existing.mentions_me; |
| 255 | existing.timer.restart(now, mention); |
| 256 | return PushOutcome { |
| 257 | id: existing.id, |
| 258 | bundled: true, |
| 259 | became_mention, |
| 260 | }; |
| 261 | } |
| 262 | self.next_id += 1; |
| 263 | let queued = self.toasts.len() >= MAX_VISIBLE; |
| 264 | self.toasts.push(ToastModel { |
| 265 | id: self.next_id, |
| 266 | conversation_id: incoming.conversation_id.clone(), |
| 267 | message_id: incoming.message_id.clone(), |
| 268 | kind: incoming.kind.clone(), |
| 269 | chat_title: incoming.chat_title.clone(), |
| 270 | sender_id: incoming.sender_id.clone(), |
| 271 | sender_name: incoming.sender_name.clone(), |
| 272 | preview: incoming.preview.clone(), |
| 273 | mentions_me: incoming.mentions_me, |
| 274 | count: 1, |
| 275 | timer: ToastTimer::new(now, incoming.mentions_me), |
| 276 | reply: ReplyState::Closed, |
| 277 | reply_text: String::new(), |
| 278 | hovered: false, |
| 279 | time: String::new(), |
| 280 | queued, |
| 281 | dismissed: false, |
| 282 | fade: Fade::new(0., 1., now, FADE_IN, Curve::Linear), |
| 283 | agent: None, |
| 284 | status_change: false, |
| 285 | urgent: incoming.is_urgent(), |
| 286 | org: None, |
| 287 | }); |
| 288 | PushOutcome { |
| 289 | id: self.next_id, |
| 290 | bundled: false, |
| 291 | became_mention: false, |
| 292 | } |
| 293 | } |
| 294 | |
| 295 | pub fn push_agent( |
| 296 | &mut self, |
| 297 | agent: AgentToast, |
| 298 | conversation_id: &str, |
| 299 | message_id: &str, |
| 300 | now: Instant, |
| 301 | ) -> u64 { |
| 302 | self.next_id += 1; |
| 303 | let queued = self.toasts.len() >= MAX_VISIBLE; |
| 304 | self.toasts.push(ToastModel { |
| 305 | id: self.next_id, |
| 306 | conversation_id: conversation_id.to_owned(), |
| 307 | message_id: message_id.to_owned(), |
| 308 | kind: ChatKind::Direct, |
| 309 | chat_title: String::new(), |
| 310 | sender_id: None, |
| 311 | sender_name: String::new(), |
| 312 | preview: Preview::Text(agent.body.clone()), |
| 313 | mentions_me: false, |
| 314 | count: 1, |
| 315 | timer: ToastTimer::new(now, true), |
| 316 | reply: ReplyState::Closed, |
| 317 | reply_text: String::new(), |
| 318 | hovered: false, |
| 319 | time: String::new(), |
| 320 | queued, |
| 321 | dismissed: false, |
| 322 | fade: Fade::new(0., 1., now, FADE_IN, Curve::Linear), |
| 323 | agent: Some(agent), |
| 324 | status_change: false, |
| 325 | urgent: false, |
| 326 | org: None, |
| 327 | }); |
| 328 | self.next_id |
| 329 | } |
| 330 | |
| 331 | pub fn push_status_change( |
| 332 | &mut self, |
| 333 | user_id: &str, |
| 334 | name: &str, |
| 335 | text: &str, |
| 336 | conversation_id: &str, |
| 337 | now: Instant, |
| 338 | ) -> u64 { |
| 339 | self.next_id += 1; |
| 340 | let queued = self.toasts.len() >= MAX_VISIBLE; |
| 341 | self.toasts.push(ToastModel { |
| 342 | id: self.next_id, |
| 343 | conversation_id: conversation_id.to_owned(), |
| 344 | message_id: String::new(), |
| 345 | kind: ChatKind::Direct, |
| 346 | chat_title: name.to_owned(), |
| 347 | sender_id: Some(user_id.to_owned()), |
| 348 | sender_name: name.to_owned(), |
| 349 | preview: Preview::Text(text.to_owned()), |
| 350 | mentions_me: false, |
| 351 | count: 1, |
| 352 | timer: ToastTimer::new(now, false), |
| 353 | reply: ReplyState::Closed, |
| 354 | reply_text: String::new(), |
| 355 | hovered: false, |
| 356 | time: String::new(), |
| 357 | queued, |
| 358 | dismissed: false, |
| 359 | fade: Fade::new(0., 1., now, FADE_IN, Curve::Linear), |
| 360 | agent: None, |
| 361 | status_change: true, |
| 362 | urgent: false, |
| 363 | org: None, |
| 364 | }); |
| 365 | self.next_id |
| 366 | } |
| 367 | |
| 368 | /// A chat message from an org that is not open. No quick reply: the open org's engine cannot send there. |
| 369 | pub fn push_org_message( |
| 370 | &mut self, |
| 371 | message: &crate::org_watch::OrgMessage, |
| 372 | now: Instant, |
| 373 | ) -> u64 { |
| 374 | self.next_id += 1; |
| 375 | let queued = self.toasts.len() >= MAX_VISIBLE; |
| 376 | self.toasts.push(ToastModel { |
| 377 | id: self.next_id, |
| 378 | conversation_id: message.conversation_id.clone(), |
| 379 | message_id: message.message_id.clone(), |
| 380 | kind: if message.direct { |
| 381 | ChatKind::Direct |
| 382 | } else { |
| 383 | ChatKind::Group { member_count: 0 } |
| 384 | }, |
| 385 | chat_title: message.chat_title.clone(), |
| 386 | sender_id: None, |
| 387 | sender_name: message.sender_name.clone(), |
| 388 | preview: Preview::Text(message.preview.clone()), |
| 389 | mentions_me: false, |
| 390 | count: 1, |
| 391 | timer: ToastTimer::new(now, false), |
| 392 | reply: ReplyState::Closed, |
| 393 | reply_text: String::new(), |
| 394 | hovered: false, |
| 395 | time: String::new(), |
| 396 | queued, |
| 397 | dismissed: false, |
| 398 | fade: Fade::new(0., 1., now, FADE_IN, Curve::Linear), |
| 399 | agent: None, |
| 400 | status_change: false, |
| 401 | urgent: false, |
| 402 | org: Some(message.org.clone()), |
| 403 | }); |
| 404 | self.next_id |
| 405 | } |
| 406 | |
| 407 | pub fn get(&self, id: u64) -> Option<&ToastModel> { |
| 408 | self.toasts.iter().find(|toast| toast.id == id) |
| 409 | } |
| 410 | |
| 411 | pub fn get_mut(&mut self, id: u64) -> Option<&mut ToastModel> { |
| 412 | self.toasts.iter_mut().find(|toast| toast.id == id) |
| 413 | } |
| 414 | |
| 415 | pub fn remove(&mut self, id: u64) -> Option<ToastModel> { |
| 416 | let index = self.toasts.iter().position(|toast| toast.id == id)?; |
| 417 | Some(self.toasts.remove(index)) |
| 418 | } |
| 419 | |
| 420 | pub fn remove_conversation(&mut self, conversation_id: &str) -> Vec<u64> { |
| 421 | let matches = |
| 422 | |toast: &ToastModel| toast.agent.is_none() && toast.conversation_id == conversation_id; |
| 423 | let ids: Vec<u64> = self |
| 424 | .toasts |
| 425 | .iter() |
| 426 | .filter(|toast| matches(toast)) |
| 427 | .map(|toast| toast.id) |
| 428 | .collect(); |
| 429 | self.toasts.retain(|toast| !matches(toast)); |
| 430 | ids |
| 431 | } |
| 432 | |
| 433 | pub fn visible(&self) -> &[ToastModel] { |
| 434 | let shown = self |
| 435 | .toasts |
| 436 | .iter() |
| 437 | .position(|toast| toast.queued) |
| 438 | .unwrap_or(self.toasts.len()); |
| 439 | &self.toasts[..shown] |
| 440 | } |
| 441 | |
| 442 | pub fn queued_count(&self) -> usize { |
| 443 | self.toasts.len() - self.visible().len() |
| 444 | } |
| 445 | |
| 446 | pub fn promote(&mut self, now: Instant, fade_in: Duration) { |
| 447 | for toast in self.toasts.iter_mut().take(MAX_VISIBLE) { |
| 448 | if toast.queued { |
| 449 | toast.queued = false; |
| 450 | toast.timer = ToastTimer::new(now, toast.mentions_me); |
| 451 | toast.fade = Fade::new(0., 1., now, fade_in, Curve::Linear); |
| 452 | } |
| 453 | } |
| 454 | } |
| 455 | |
| 456 | pub fn fade_out(&mut self, id: u64, now: Instant, duration: Duration) { |
| 457 | if let Some(toast) = self.get_mut(id) |
| 458 | && !toast.is_leaving() |
| 459 | { |
| 460 | toast.fade = Fade::new(toast.opacity(now), 0., now, duration, Curve::EaseInCirc); |
| 461 | } |
| 462 | } |
| 463 | |
| 464 | pub fn dismiss(&mut self, id: u64, now: Instant, duration: Duration) { |
| 465 | let Some(index) = self.toasts.iter().position(|toast| toast.id == id) else { |
| 466 | return; |
| 467 | }; |
| 468 | if self.toasts[index].queued { |
| 469 | self.toasts.remove(index); |
| 470 | return; |
| 471 | } |
| 472 | let toast = &mut self.toasts[index]; |
| 473 | if !toast.dismissed { |
| 474 | toast.dismissed = true; |
| 475 | toast.hovered = false; |
| 476 | toast.fade = Fade::new(toast.opacity(now), 0., now, duration, Curve::Linear); |
| 477 | } |
| 478 | } |
| 479 | |
| 480 | pub fn restore(&mut self, now: Instant, duration: Duration) { |
| 481 | for toast in self |
| 482 | .toasts |
| 483 | .iter_mut() |
| 484 | .filter(|toast| toast.is_leaving() && !toast.dismissed) |
| 485 | { |
| 486 | toast.fade = Fade::new(toast.opacity(now), 1., now, duration, Curve::Linear); |
| 487 | } |
| 488 | } |
| 489 | |
| 490 | pub fn any_hovered(&self) -> bool { |
| 491 | self.toasts.iter().any(|toast| toast.hovered) |
| 492 | } |
| 493 | |
| 494 | pub fn ids(&self) -> Vec<u64> { |
| 495 | self.toasts.iter().map(|toast| toast.id).collect() |
| 496 | } |
| 497 | |
| 498 | pub fn gone(&self, now: Instant) -> Vec<u64> { |
| 499 | self.toasts |
| 500 | .iter() |
| 501 | .filter(|toast| toast.fade.is_gone(now)) |
| 502 | .map(|toast| toast.id) |
| 503 | .collect() |
| 504 | } |
| 505 | |
| 506 | pub fn is_empty(&self) -> bool { |
| 507 | self.toasts.is_empty() |
| 508 | } |
| 509 | |
| 510 | pub fn expired(&self, now: Instant) -> Vec<u64> { |
| 511 | self.toasts |
| 512 | .iter() |
| 513 | .filter(|toast| { |
| 514 | !toast.queued |
| 515 | && !toast.is_leaving() |
| 516 | && !toast.holds_open() |
| 517 | && toast.timer.expired(now) |
| 518 | }) |
| 519 | .map(|toast| toast.id) |
| 520 | .collect() |
| 521 | } |
| 522 | } |
| 523 | |
| 524 | #[cfg(test)] |
| 525 | mod tests { |
| 526 | use super::*; |
| 527 | |
| 528 | fn incoming(conversation: &str, mention: bool) -> Incoming { |
| 529 | Incoming { |
| 530 | conversation_id: conversation.into(), |
| 531 | message_id: "m".into(), |
| 532 | kind: ChatKind::Direct, |
| 533 | chat_title: "Chat".into(), |
| 534 | sender_id: None, |
| 535 | sender_name: "Mara".into(), |
| 536 | preview: Preview::Text("Hello".into()), |
| 537 | mentions_me: mention, |
| 538 | muted: false, |
| 539 | signals: Default::default(), |
| 540 | chat: Default::default(), |
| 541 | created_at: chrono::Utc::now(), |
| 542 | importance: Default::default(), |
| 543 | } |
| 544 | } |
| 545 | |
| 546 | #[test] |
| 547 | fn same_chat_bundles_into_one_toast_with_counter() { |
| 548 | let mut stack = ToastStack::default(); |
| 549 | let now = Instant::now(); |
| 550 | let first = stack.push(&incoming("a", false), now); |
| 551 | let second = stack.push(&incoming("a", false), now + Duration::from_secs(2)); |
| 552 | assert!(!first.bundled); |
| 553 | assert!(second.bundled); |
| 554 | assert_eq!(first.id, second.id); |
| 555 | assert_eq!(stack.get(first.id).unwrap().count, 2); |
| 556 | assert_eq!(stack.visible().len(), 1); |
| 557 | } |
| 558 | |
| 559 | #[test] |
| 560 | fn a_message_never_bundles_into_a_dismissed_toast() { |
| 561 | let mut stack = ToastStack::default(); |
| 562 | let now = Instant::now(); |
| 563 | let first = stack.push(&incoming("a", false), now); |
| 564 | stack.dismiss(first.id, now, Duration::from_millis(150)); |
| 565 | let second = stack.push(&incoming("a", false), now); |
| 566 | assert!(!second.bundled); |
| 567 | assert_ne!(first.id, second.id); |
| 568 | } |
| 569 | |
| 570 | #[test] |
| 571 | fn bundling_restarts_timer() { |
| 572 | let mut stack = ToastStack::default(); |
| 573 | let now = Instant::now(); |
| 574 | let id = stack.push(&incoming("a", false), now).id; |
| 575 | stack.push(&incoming("a", false), now + Duration::from_secs(5)); |
| 576 | assert!(stack.expired(now + Duration::from_secs(7)).is_empty()); |
| 577 | assert_eq!(stack.expired(now + Duration::from_secs(11)), vec![id]); |
| 578 | } |
| 579 | |
| 580 | #[test] |
| 581 | fn mention_in_burst_upgrades_and_plays_sound() { |
| 582 | let mut stack = ToastStack::default(); |
| 583 | let now = Instant::now(); |
| 584 | stack.push(&incoming("a", false), now); |
| 585 | let outcome = stack.push(&incoming("a", true), now); |
| 586 | assert!(outcome.became_mention); |
| 587 | assert!(outcome.plays_sound(false)); |
| 588 | assert!(stack.get(outcome.id).unwrap().mentions_me); |
| 589 | let again = stack.push(&incoming("a", true), now); |
| 590 | assert!(!again.plays_sound(true)); |
| 591 | } |
| 592 | |
| 593 | #[test] |
| 594 | fn only_first_of_burst_plays_sound() { |
| 595 | let mut stack = ToastStack::default(); |
| 596 | let now = Instant::now(); |
| 597 | assert!(stack.push(&incoming("a", false), now).plays_sound(true)); |
| 598 | assert!(!stack.push(&incoming("a", false), now).plays_sound(true)); |
| 599 | assert!(!stack.push(&incoming("b", false), now).plays_sound(false)); |
| 600 | } |
| 601 | |
| 602 | fn visible_chats(stack: &ToastStack) -> Vec<&str> { |
| 603 | stack |
| 604 | .visible() |
| 605 | .iter() |
| 606 | .map(|toast| toast.conversation_id.as_str()) |
| 607 | .collect() |
| 608 | } |
| 609 | |
| 610 | #[test] |
| 611 | fn shows_first_three_and_queues_the_rest() { |
| 612 | let mut stack = ToastStack::default(); |
| 613 | let now = Instant::now(); |
| 614 | for chat in ["a", "b", "c", "d", "e"] { |
| 615 | stack.push(&incoming(chat, false), now); |
| 616 | } |
| 617 | assert_eq!(visible_chats(&stack), ["a", "b", "c"]); |
| 618 | assert_eq!(stack.queued_count(), 2); |
| 619 | } |
| 620 | |
| 621 | #[test] |
| 622 | fn queued_toast_starts_its_timer_when_promoted() { |
| 623 | let mut stack = ToastStack::default(); |
| 624 | let start = Instant::now(); |
| 625 | let ids: Vec<u64> = ["a", "b", "c", "d"] |
| 626 | .into_iter() |
| 627 | .map(|chat| stack.push(&incoming(chat, false), start).id) |
| 628 | .collect(); |
| 629 | let later = start + Duration::from_secs(30); |
| 630 | assert!(!stack.expired(later).contains(&ids[3])); |
| 631 | stack.remove(ids[0]); |
| 632 | stack.promote(later, FADE_IN); |
| 633 | assert_eq!(visible_chats(&stack), ["b", "c", "d"]); |
| 634 | assert!( |
| 635 | !stack |
| 636 | .expired(later + Duration::from_secs(5)) |
| 637 | .contains(&ids[3]) |
| 638 | ); |
| 639 | assert!( |
| 640 | stack |
| 641 | .expired(later + Duration::from_secs(6)) |
| 642 | .contains(&ids[3]) |
| 643 | ); |
| 644 | } |
| 645 | |
| 646 | #[test] |
| 647 | fn slow_fade_stays_visible_then_drops_and_is_gone() { |
| 648 | let mut stack = ToastStack::default(); |
| 649 | let now = Instant::now(); |
| 650 | let id = stack.push(&incoming("a", false), now).id; |
| 651 | let hide_at = now + Duration::from_secs(6); |
| 652 | stack.fade_out(id, hide_at, SLOW_FADE); |
| 653 | let toast = stack.get(id).unwrap(); |
| 654 | assert!(toast.opacity(hide_at + Duration::from_secs(2)) > 0.8); |
| 655 | assert!(stack.gone(hide_at + Duration::from_secs(3)).is_empty()); |
| 656 | assert_eq!(stack.gone(hide_at + SLOW_FADE), vec![id]); |
| 657 | } |
| 658 | |
| 659 | #[test] |
| 660 | fn restore_brings_a_fading_toast_back() { |
| 661 | let mut stack = ToastStack::default(); |
| 662 | let now = Instant::now(); |
| 663 | let id = stack.push(&incoming("a", false), now).id; |
| 664 | stack.fade_out(id, now, SLOW_FADE); |
| 665 | stack.restore(now + Duration::from_secs(3), FAST_FADE); |
| 666 | let toast = stack.get(id).unwrap(); |
| 667 | assert!(!toast.is_leaving()); |
| 668 | assert_eq!(toast.opacity(now + Duration::from_secs(4)), 1.); |
| 669 | assert!(stack.gone(now + Duration::from_secs(10)).is_empty()); |
| 670 | } |
| 671 | |
| 672 | #[test] |
| 673 | fn hiding_a_queued_toast_drops_it_at_once() { |
| 674 | let mut stack = ToastStack::default(); |
| 675 | let now = Instant::now(); |
| 676 | let ids: Vec<u64> = ["a", "b", "c", "d"] |
| 677 | .into_iter() |
| 678 | .map(|chat| stack.push(&incoming(chat, false), now).id) |
| 679 | .collect(); |
| 680 | stack.dismiss(ids[3], now, FAST_FADE); |
| 681 | assert!(stack.get(ids[3]).is_none()); |
| 682 | assert_eq!(stack.queued_count(), 0); |
| 683 | } |
| 684 | |
| 685 | #[test] |
| 686 | fn dismiss_cuts_a_slow_fade_short_and_hover_does_not_undo_it() { |
| 687 | let mut stack = ToastStack::default(); |
| 688 | let now = Instant::now(); |
| 689 | let id = stack.push(&incoming("a", false), now).id; |
| 690 | stack.fade_out(id, now, SLOW_FADE); |
| 691 | stack.dismiss(id, now + Duration::from_secs(1), FAST_FADE); |
| 692 | stack.restore(now + Duration::from_millis(1050), FAST_FADE); |
| 693 | assert_eq!(stack.gone(now + Duration::from_millis(1150)), vec![id]); |
| 694 | } |
| 695 | |
| 696 | #[test] |
| 697 | fn mention_lasts_ten_seconds() { |
| 698 | let now = Instant::now(); |
| 699 | let timer = ToastTimer::new(now, true); |
| 700 | assert!(!timer.expired(now + Duration::from_secs(9))); |
| 701 | assert!(timer.expired(now + Duration::from_secs(10))); |
| 702 | } |
| 703 | |
| 704 | #[test] |
| 705 | fn hard_cap_beats_restarts() { |
| 706 | let mut stack = ToastStack::default(); |
| 707 | let start = Instant::now(); |
| 708 | let id = stack.push(&incoming("a", false), start).id; |
| 709 | for second in [5, 10, 15, 19] { |
| 710 | stack.push(&incoming("a", false), start + Duration::from_secs(second)); |
| 711 | } |
| 712 | assert!(stack.expired(start + Duration::from_secs(19)).is_empty()); |
| 713 | assert_eq!(stack.expired(start + Duration::from_secs(20)), vec![id]); |
| 714 | } |
| 715 | |
| 716 | #[test] |
| 717 | fn pause_stops_expiry_and_resume_leaves_two_seconds() { |
| 718 | let now = Instant::now(); |
| 719 | let mut timer = ToastTimer::new(now, false); |
| 720 | timer.pause(now + Duration::from_secs(1)); |
| 721 | assert!(!timer.expired(now + Duration::from_secs(60))); |
| 722 | let resumed = now + Duration::from_secs(60); |
| 723 | timer.resume(resumed); |
| 724 | assert!(!timer.expired(resumed + Duration::from_millis(1900))); |
| 725 | assert!(timer.expired(resumed + Duration::from_secs(2))); |
| 726 | } |
| 727 | |
| 728 | #[test] |
| 729 | fn open_reply_field_holds_toast() { |
| 730 | let mut stack = ToastStack::default(); |
| 731 | let now = Instant::now(); |
| 732 | let id = stack.push(&incoming("a", false), now).id; |
| 733 | stack.get_mut(id).unwrap().reply = ReplyState::Open; |
| 734 | assert!(stack.expired(now + Duration::from_secs(60)).is_empty()); |
| 735 | } |
| 736 | |
| 737 | #[test] |
| 738 | fn remove_conversation_returns_ids() { |
| 739 | let mut stack = ToastStack::default(); |
| 740 | let now = Instant::now(); |
| 741 | let id = stack.push(&incoming("a", false), now).id; |
| 742 | stack.push(&incoming("b", false), now); |
| 743 | assert_eq!(stack.remove_conversation("a"), vec![id]); |
| 744 | assert_eq!(stack.visible().len(), 1); |
| 745 | } |
| 746 | |
| 747 | fn agent_toast() -> AgentToast { |
| 748 | AgentToast { |
| 749 | kind: AgentToastKind::Run { |
| 750 | run_id: 7, |
| 751 | session_id: "s".into(), |
| 752 | failed: false, |
| 753 | }, |
| 754 | header: "Answer this - Claude - 1:02".into(), |
| 755 | title: "Claude finished: Answer this for Mara".into(), |
| 756 | body: "Done".into(), |
| 757 | } |
| 758 | } |
| 759 | |
| 760 | #[test] |
| 761 | fn agent_toasts_never_bundle_with_messages() { |
| 762 | let mut stack = ToastStack::default(); |
| 763 | let now = Instant::now(); |
| 764 | let agent_id = stack.push_agent(agent_toast(), "a", "m", now); |
| 765 | let outcome = stack.push(&incoming("a", false), now); |
| 766 | assert!(!outcome.bundled); |
| 767 | assert_ne!(outcome.id, agent_id); |
| 768 | assert_eq!(stack.visible().len(), 2); |
| 769 | } |
| 770 | |
| 771 | #[test] |
| 772 | fn opening_the_chat_keeps_the_agent_toast() { |
| 773 | let mut stack = ToastStack::default(); |
| 774 | let now = Instant::now(); |
| 775 | let agent_id = stack.push_agent(agent_toast(), "a", "m", now); |
| 776 | let message_id = stack.push(&incoming("a", false), now).id; |
| 777 | assert_eq!(stack.remove_conversation("a"), vec![message_id]); |
| 778 | assert!(stack.get(agent_id).is_some()); |
| 779 | } |
| 780 | } |