1use crate::channel_chat::ChannelChatEvent;
2
3use super::*;
4use client::{test::FakeServer, Client, UserStore};
5use clock::FakeSystemClock;
6use gpui::{AppContext, Context, Model, SemanticVersion, TestAppContext};
7use http_client::FakeHttpClient;
8use rpc::proto::{self};
9use settings::SettingsStore;
10
11#[gpui::test]
12fn test_update_channels(cx: &mut AppContext) {
13 let channel_store = init_test(cx);
14
15 update_channels(
16 &channel_store,
17 proto::UpdateChannels {
18 channels: vec![
19 proto::Channel {
20 id: 1,
21 name: "b".to_string(),
22 visibility: proto::ChannelVisibility::Members as i32,
23 parent_path: Vec::new(),
24 },
25 proto::Channel {
26 id: 2,
27 name: "a".to_string(),
28 visibility: proto::ChannelVisibility::Members as i32,
29 parent_path: Vec::new(),
30 },
31 ],
32 ..Default::default()
33 },
34 cx,
35 );
36 assert_channels(
37 &channel_store,
38 &[
39 //
40 (0, "a".to_string()),
41 (0, "b".to_string()),
42 ],
43 cx,
44 );
45
46 update_channels(
47 &channel_store,
48 proto::UpdateChannels {
49 channels: vec![
50 proto::Channel {
51 id: 3,
52 name: "x".to_string(),
53 visibility: proto::ChannelVisibility::Members as i32,
54 parent_path: vec![1],
55 },
56 proto::Channel {
57 id: 4,
58 name: "y".to_string(),
59 visibility: proto::ChannelVisibility::Members as i32,
60 parent_path: vec![2],
61 },
62 ],
63 ..Default::default()
64 },
65 cx,
66 );
67 assert_channels(
68 &channel_store,
69 &[
70 (0, "a".to_string()),
71 (1, "y".to_string()),
72 (0, "b".to_string()),
73 (1, "x".to_string()),
74 ],
75 cx,
76 );
77}
78
79#[gpui::test]
80fn test_dangling_channel_paths(cx: &mut AppContext) {
81 let channel_store = init_test(cx);
82
83 update_channels(
84 &channel_store,
85 proto::UpdateChannels {
86 channels: vec![
87 proto::Channel {
88 id: 0,
89 name: "a".to_string(),
90 visibility: proto::ChannelVisibility::Members as i32,
91 parent_path: vec![],
92 },
93 proto::Channel {
94 id: 1,
95 name: "b".to_string(),
96 visibility: proto::ChannelVisibility::Members as i32,
97 parent_path: vec![0],
98 },
99 proto::Channel {
100 id: 2,
101 name: "c".to_string(),
102 visibility: proto::ChannelVisibility::Members as i32,
103 parent_path: vec![0, 1],
104 },
105 ],
106 ..Default::default()
107 },
108 cx,
109 );
110 // Sanity check
111 assert_channels(
112 &channel_store,
113 &[
114 //
115 (0, "a".to_string()),
116 (1, "b".to_string()),
117 (2, "c".to_string()),
118 ],
119 cx,
120 );
121
122 update_channels(
123 &channel_store,
124 proto::UpdateChannels {
125 delete_channels: vec![1, 2],
126 ..Default::default()
127 },
128 cx,
129 );
130
131 // Make sure that the 1/2/3 path is gone
132 assert_channels(&channel_store, &[(0, "a".to_string())], cx);
133}
134
135#[gpui::test]
136async fn test_channel_messages(cx: &mut TestAppContext) {
137 let user_id = 5;
138 let channel_id = 5;
139 let channel_store = cx.update(init_test);
140 let client = channel_store.update(cx, |s, _| s.client());
141 let server = FakeServer::for_client(user_id, &client, cx).await;
142
143 // Get the available channels.
144 server.send(proto::UpdateChannels {
145 channels: vec![proto::Channel {
146 id: channel_id,
147 name: "the-channel".to_string(),
148 visibility: proto::ChannelVisibility::Members as i32,
149 parent_path: vec![],
150 }],
151 ..Default::default()
152 });
153 cx.executor().run_until_parked();
154 cx.update(|cx| {
155 assert_channels(&channel_store, &[(0, "the-channel".to_string())], cx);
156 });
157
158 let get_users = server.receive::<proto::GetUsers>().await.unwrap();
159 assert_eq!(get_users.payload.user_ids, vec![5]);
160 server.respond(
161 get_users.receipt(),
162 proto::UsersResponse {
163 users: vec![proto::User {
164 id: 5,
165 github_login: "nathansobo".into(),
166 avatar_url: "http://avatar.com/nathansobo".into(),
167 }],
168 },
169 );
170
171 // Join a channel and populate its existing messages.
172 let channel = channel_store.update(cx, |store, cx| {
173 let channel_id = store.ordered_channels().next().unwrap().1.id;
174 store.open_channel_chat(channel_id, cx)
175 });
176 let join_channel = server.receive::<proto::JoinChannelChat>().await.unwrap();
177 server.respond(
178 join_channel.receipt(),
179 proto::JoinChannelChatResponse {
180 messages: vec![
181 proto::ChannelMessage {
182 id: 10,
183 body: "a".into(),
184 timestamp: 1000,
185 sender_id: 5,
186 mentions: vec![],
187 nonce: Some(1.into()),
188 reply_to_message_id: None,
189 edited_at: None,
190 },
191 proto::ChannelMessage {
192 id: 11,
193 body: "b".into(),
194 timestamp: 1001,
195 sender_id: 6,
196 mentions: vec![],
197 nonce: Some(2.into()),
198 reply_to_message_id: None,
199 edited_at: None,
200 },
201 ],
202 done: false,
203 },
204 );
205
206 cx.executor().start_waiting();
207
208 // Client requests all users for the received messages
209 let mut get_users = server.receive::<proto::GetUsers>().await.unwrap();
210 get_users.payload.user_ids.sort();
211 assert_eq!(get_users.payload.user_ids, vec![6]);
212 server.respond(
213 get_users.receipt(),
214 proto::UsersResponse {
215 users: vec![proto::User {
216 id: 6,
217 github_login: "maxbrunsfeld".into(),
218 avatar_url: "http://avatar.com/maxbrunsfeld".into(),
219 }],
220 },
221 );
222
223 let channel = channel.await.unwrap();
224 channel.update(cx, |channel, _| {
225 assert_eq!(
226 channel
227 .messages_in_range(0..2)
228 .map(|message| (message.sender.github_login.clone(), message.body.clone()))
229 .collect::<Vec<_>>(),
230 &[
231 ("nathansobo".into(), "a".into()),
232 ("maxbrunsfeld".into(), "b".into())
233 ]
234 );
235 });
236
237 // Receive a new message.
238 server.send(proto::ChannelMessageSent {
239 channel_id,
240 message: Some(proto::ChannelMessage {
241 id: 12,
242 body: "c".into(),
243 timestamp: 1002,
244 sender_id: 7,
245 mentions: vec![],
246 nonce: Some(3.into()),
247 reply_to_message_id: None,
248 edited_at: None,
249 }),
250 });
251
252 // Client requests user for message since they haven't seen them yet
253 let get_users = server.receive::<proto::GetUsers>().await.unwrap();
254 assert_eq!(get_users.payload.user_ids, vec![7]);
255 server.respond(
256 get_users.receipt(),
257 proto::UsersResponse {
258 users: vec![proto::User {
259 id: 7,
260 github_login: "as-cii".into(),
261 avatar_url: "http://avatar.com/as-cii".into(),
262 }],
263 },
264 );
265
266 assert_eq!(
267 channel.next_event(cx).await,
268 ChannelChatEvent::MessagesUpdated {
269 old_range: 2..2,
270 new_count: 1,
271 }
272 );
273 channel.update(cx, |channel, _| {
274 assert_eq!(
275 channel
276 .messages_in_range(2..3)
277 .map(|message| (message.sender.github_login.clone(), message.body.clone()))
278 .collect::<Vec<_>>(),
279 &[("as-cii".into(), "c".into())]
280 )
281 });
282
283 // Scroll up to view older messages.
284 channel.update(cx, |channel, cx| {
285 channel.load_more_messages(cx).unwrap().detach();
286 });
287 let get_messages = server.receive::<proto::GetChannelMessages>().await.unwrap();
288 assert_eq!(get_messages.payload.channel_id, 5);
289 assert_eq!(get_messages.payload.before_message_id, 10);
290 server.respond(
291 get_messages.receipt(),
292 proto::GetChannelMessagesResponse {
293 done: true,
294 messages: vec![
295 proto::ChannelMessage {
296 id: 8,
297 body: "y".into(),
298 timestamp: 998,
299 sender_id: 5,
300 nonce: Some(4.into()),
301 mentions: vec![],
302 reply_to_message_id: None,
303 edited_at: None,
304 },
305 proto::ChannelMessage {
306 id: 9,
307 body: "z".into(),
308 timestamp: 999,
309 sender_id: 6,
310 nonce: Some(5.into()),
311 mentions: vec![],
312 reply_to_message_id: None,
313 edited_at: None,
314 },
315 ],
316 },
317 );
318
319 assert_eq!(
320 channel.next_event(cx).await,
321 ChannelChatEvent::MessagesUpdated {
322 old_range: 0..0,
323 new_count: 2,
324 }
325 );
326 channel.update(cx, |channel, _| {
327 assert_eq!(
328 channel
329 .messages_in_range(0..2)
330 .map(|message| (message.sender.github_login.clone(), message.body.clone()))
331 .collect::<Vec<_>>(),
332 &[
333 ("nathansobo".into(), "y".into()),
334 ("maxbrunsfeld".into(), "z".into())
335 ]
336 );
337 });
338}
339
340fn init_test(cx: &mut AppContext) -> Model<ChannelStore> {
341 let settings_store = SettingsStore::test(cx);
342 cx.set_global(settings_store);
343 release_channel::init(SemanticVersion::default(), cx);
344 client::init_settings(cx);
345
346 let clock = Arc::new(FakeSystemClock::new());
347 let http = FakeHttpClient::with_404_response();
348 let client = Client::new(clock, http.clone(), cx);
349 let user_store = cx.new_model(|cx| UserStore::new(client.clone(), cx));
350
351 client::init(&client, cx);
352 crate::init(&client, user_store, cx);
353
354 ChannelStore::global(cx)
355}
356
357fn update_channels(
358 channel_store: &Model<ChannelStore>,
359 message: proto::UpdateChannels,
360 cx: &mut AppContext,
361) {
362 let task = channel_store.update(cx, |store, cx| store.update_channels(message, cx));
363 assert!(task.is_none());
364}
365
366#[track_caller]
367fn assert_channels(
368 channel_store: &Model<ChannelStore>,
369 expected_channels: &[(usize, String)],
370 cx: &mut AppContext,
371) {
372 let actual = channel_store.update(cx, |store, _| {
373 store
374 .ordered_channels()
375 .map(|(depth, channel)| (depth, channel.name.to_string()))
376 .collect::<Vec<_>>()
377 });
378 assert_eq!(actual, expected_channels);
379}