1use std::sync::Arc;
2
3use gpui::{BackgroundExecutor, TestAppContext};
4use notifications::NotificationEvent;
5use parking_lot::Mutex;
6use rpc::{Notification, proto};
7
8use crate::tests::TestServer;
9
10#[gpui::test]
11async fn test_notifications(
12 executor: BackgroundExecutor,
13 cx_a: &mut TestAppContext,
14 cx_b: &mut TestAppContext,
15) {
16 let mut server = TestServer::start(executor.clone()).await;
17 let client_a = server.create_client(cx_a, "user_a").await;
18 let client_b = server.create_client(cx_b, "user_b").await;
19
20 let notification_events_a = Arc::new(Mutex::new(Vec::new()));
21 let notification_events_b = Arc::new(Mutex::new(Vec::new()));
22 client_a.notification_store().update(cx_a, |_, cx| {
23 let events = notification_events_a.clone();
24 cx.subscribe(&cx.entity(), move |_, _, event, _| {
25 events.lock().push(event.clone());
26 })
27 .detach()
28 });
29 client_b.notification_store().update(cx_b, |_, cx| {
30 let events = notification_events_b.clone();
31 cx.subscribe(&cx.entity(), move |_, _, event, _| {
32 events.lock().push(event.clone());
33 })
34 .detach()
35 });
36
37 // Client A sends a contact request to client B.
38 client_a
39 .user_store()
40 .update(cx_a, |store, cx| store.request_contact(client_b.id(), cx))
41 .await
42 .unwrap();
43
44 // Client B receives a contact request notification and responds to the
45 // request, accepting it.
46 executor.run_until_parked();
47 client_b.notification_store().update(cx_b, |store, cx| {
48 assert_eq!(store.notification_count(), 1);
49 assert_eq!(store.unread_notification_count(), 1);
50
51 let entry = store.notification_at(0).unwrap();
52 assert_eq!(
53 entry.notification,
54 Notification::ContactRequest {
55 sender_id: client_a.id()
56 }
57 );
58 assert!(!entry.is_read);
59 assert_eq!(
60 ¬ification_events_b.lock()[0..],
61 &[
62 NotificationEvent::NewNotification {
63 entry: entry.clone(),
64 },
65 NotificationEvent::NotificationsUpdated {
66 old_range: 0..0,
67 new_count: 1
68 }
69 ]
70 );
71
72 store.respond_to_notification(entry.notification.clone(), true, cx);
73 });
74
75 // Client B sees the notification is now read, and that they responded.
76 executor.run_until_parked();
77 client_b.notification_store().read_with(cx_b, |store, _| {
78 assert_eq!(store.notification_count(), 1);
79 assert_eq!(store.unread_notification_count(), 0);
80
81 let entry = store.notification_at(0).unwrap();
82 assert!(entry.is_read);
83 assert_eq!(entry.response, Some(true));
84 assert_eq!(
85 ¬ification_events_b.lock()[2..],
86 &[
87 NotificationEvent::NotificationRead {
88 entry: entry.clone(),
89 },
90 NotificationEvent::NotificationsUpdated {
91 old_range: 0..1,
92 new_count: 1
93 }
94 ]
95 );
96 });
97
98 // Client A receives a notification that client B accepted their request.
99 client_a.notification_store().read_with(cx_a, |store, _| {
100 assert_eq!(store.notification_count(), 1);
101 assert_eq!(store.unread_notification_count(), 1);
102
103 let entry = store.notification_at(0).unwrap();
104 assert_eq!(
105 entry.notification,
106 Notification::ContactRequestAccepted {
107 responder_id: client_b.id()
108 }
109 );
110 assert!(!entry.is_read);
111 });
112
113 // Client A creates a channel and invites client B to be a member.
114 let channel_id = client_a
115 .channel_store()
116 .update(cx_a, |store, cx| {
117 store.create_channel("the-channel", None, cx)
118 })
119 .await
120 .unwrap();
121 client_a
122 .channel_store()
123 .update(cx_a, |store, cx| {
124 store.invite_member(channel_id, client_b.id(), proto::ChannelRole::Member, cx)
125 })
126 .await
127 .unwrap();
128
129 // Client B receives a channel invitation notification and responds to the
130 // invitation, accepting it.
131 executor.run_until_parked();
132 client_b.notification_store().update(cx_b, |store, cx| {
133 assert_eq!(store.notification_count(), 2);
134 assert_eq!(store.unread_notification_count(), 1);
135
136 let entry = store.notification_at(0).unwrap();
137 assert_eq!(
138 entry.notification,
139 Notification::ChannelInvitation {
140 channel_id: channel_id.0,
141 channel_name: "the-channel".to_string(),
142 inviter_id: client_a.id()
143 }
144 );
145 assert!(!entry.is_read);
146
147 store.respond_to_notification(entry.notification.clone(), true, cx);
148 });
149
150 // Client B sees the notification is now read, and that they responded.
151 executor.run_until_parked();
152 client_b.notification_store().read_with(cx_b, |store, _| {
153 assert_eq!(store.notification_count(), 2);
154 assert_eq!(store.unread_notification_count(), 0);
155
156 let entry = store.notification_at(0).unwrap();
157 assert!(entry.is_read);
158 assert_eq!(entry.response, Some(true));
159 });
160}