channel_store_tests.rs

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