channel_store_tests.rs

  1use crate::channel_chat::ChannelChatEvent;
  2
  3use super::*;
  4use client::{test::FakeServer, Client, UserStore};
  5use clock::FakeSystemClock;
  6use gpui::{AppContext, Context, Model, TestAppContext};
  7use rpc::proto::{self};
  8use settings::SettingsStore;
  9use util::http::FakeHttpClient;
 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                },
190                proto::ChannelMessage {
191                    id: 11,
192                    body: "b".into(),
193                    timestamp: 1001,
194                    sender_id: 6,
195                    mentions: vec![],
196                    nonce: Some(2.into()),
197                    reply_to_message_id: None,
198                },
199            ],
200            done: false,
201        },
202    );
203
204    cx.executor().start_waiting();
205
206    // Client requests all users for the received messages
207    let mut get_users = server.receive::<proto::GetUsers>().await.unwrap();
208    get_users.payload.user_ids.sort();
209    assert_eq!(get_users.payload.user_ids, vec![6]);
210    server.respond(
211        get_users.receipt(),
212        proto::UsersResponse {
213            users: vec![proto::User {
214                id: 6,
215                github_login: "maxbrunsfeld".into(),
216                avatar_url: "http://avatar.com/maxbrunsfeld".into(),
217            }],
218        },
219    );
220
221    let channel = channel.await.unwrap();
222    channel.update(cx, |channel, _| {
223        assert_eq!(
224            channel
225                .messages_in_range(0..2)
226                .map(|message| (message.sender.github_login.clone(), message.body.clone()))
227                .collect::<Vec<_>>(),
228            &[
229                ("nathansobo".into(), "a".into()),
230                ("maxbrunsfeld".into(), "b".into())
231            ]
232        );
233    });
234
235    // Receive a new message.
236    server.send(proto::ChannelMessageSent {
237        channel_id,
238        message: Some(proto::ChannelMessage {
239            id: 12,
240            body: "c".into(),
241            timestamp: 1002,
242            sender_id: 7,
243            mentions: vec![],
244            nonce: Some(3.into()),
245            reply_to_message_id: None,
246        }),
247    });
248
249    // Client requests user for message since they haven't seen them yet
250    let get_users = server.receive::<proto::GetUsers>().await.unwrap();
251    assert_eq!(get_users.payload.user_ids, vec![7]);
252    server.respond(
253        get_users.receipt(),
254        proto::UsersResponse {
255            users: vec![proto::User {
256                id: 7,
257                github_login: "as-cii".into(),
258                avatar_url: "http://avatar.com/as-cii".into(),
259            }],
260        },
261    );
262
263    assert_eq!(
264        channel.next_event(cx),
265        ChannelChatEvent::MessagesUpdated {
266            old_range: 2..2,
267            new_count: 1,
268        }
269    );
270    channel.update(cx, |channel, _| {
271        assert_eq!(
272            channel
273                .messages_in_range(2..3)
274                .map(|message| (message.sender.github_login.clone(), message.body.clone()))
275                .collect::<Vec<_>>(),
276            &[("as-cii".into(), "c".into())]
277        )
278    });
279
280    // Scroll up to view older messages.
281    channel.update(cx, |channel, cx| {
282        channel.load_more_messages(cx).unwrap().detach();
283    });
284    let get_messages = server.receive::<proto::GetChannelMessages>().await.unwrap();
285    assert_eq!(get_messages.payload.channel_id, 5);
286    assert_eq!(get_messages.payload.before_message_id, 10);
287    server.respond(
288        get_messages.receipt(),
289        proto::GetChannelMessagesResponse {
290            done: true,
291            messages: vec![
292                proto::ChannelMessage {
293                    id: 8,
294                    body: "y".into(),
295                    timestamp: 998,
296                    sender_id: 5,
297                    nonce: Some(4.into()),
298                    mentions: vec![],
299                    reply_to_message_id: None,
300                },
301                proto::ChannelMessage {
302                    id: 9,
303                    body: "z".into(),
304                    timestamp: 999,
305                    sender_id: 6,
306                    nonce: Some(5.into()),
307                    mentions: vec![],
308                    reply_to_message_id: None,
309                },
310            ],
311        },
312    );
313
314    assert_eq!(
315        channel.next_event(cx),
316        ChannelChatEvent::MessagesUpdated {
317            old_range: 0..0,
318            new_count: 2,
319        }
320    );
321    channel.update(cx, |channel, _| {
322        assert_eq!(
323            channel
324                .messages_in_range(0..2)
325                .map(|message| (message.sender.github_login.clone(), message.body.clone()))
326                .collect::<Vec<_>>(),
327            &[
328                ("nathansobo".into(), "y".into()),
329                ("maxbrunsfeld".into(), "z".into())
330            ]
331        );
332    });
333}
334
335fn init_test(cx: &mut AppContext) -> Model<ChannelStore> {
336    let settings_store = SettingsStore::test(cx);
337    cx.set_global(settings_store);
338    release_channel::init("0.0.0", cx);
339    client::init_settings(cx);
340
341    let clock = Arc::new(FakeSystemClock::default());
342    let http = FakeHttpClient::with_404_response();
343    let client = Client::new(clock, http.clone(), cx);
344    let user_store = cx.new_model(|cx| UserStore::new(client.clone(), cx));
345
346    client::init(&client, cx);
347    crate::init(&client, user_store, cx);
348
349    ChannelStore::global(cx)
350}
351
352fn update_channels(
353    channel_store: &Model<ChannelStore>,
354    message: proto::UpdateChannels,
355    cx: &mut AppContext,
356) {
357    let task = channel_store.update(cx, |store, cx| store.update_channels(message, cx));
358    assert!(task.is_none());
359}
360
361#[track_caller]
362fn assert_channels(
363    channel_store: &Model<ChannelStore>,
364    expected_channels: &[(usize, String)],
365    cx: &mut AppContext,
366) {
367    let actual = channel_store.update(cx, |store, _| {
368        store
369            .ordered_channels()
370            .map(|(depth, channel)| (depth, channel.name.to_string()))
371            .collect::<Vec<_>>()
372    });
373    assert_eq!(actual, expected_channels);
374}