channel_buffer_tests.rs

  1use crate::{
  2    rpc::{CLEANUP_TIMEOUT, RECONNECT_TIMEOUT},
  3    tests::{TestServer, test_server::open_channel_notes},
  4};
  5use call::ActiveCall;
  6use channel::ACKNOWLEDGE_DEBOUNCE_INTERVAL;
  7use client::{Collaborator, ParticipantIndex, UserId};
  8use collab_ui::channel_view::ChannelView;
  9use collections::HashMap;
 10use editor::{Anchor, Editor, ToOffset};
 11use futures::future;
 12use gpui::{BackgroundExecutor, Context, Entity, TestAppContext, Window};
 13use rpc::{RECEIVE_TIMEOUT, proto::PeerId};
 14use serde_json::json;
 15use std::ops::Range;
 16use workspace::CollaboratorId;
 17
 18#[gpui::test]
 19async fn test_core_channel_buffers(
 20    executor: BackgroundExecutor,
 21    cx_a: &mut TestAppContext,
 22    cx_b: &mut TestAppContext,
 23) {
 24    let mut server = TestServer::start(executor.clone()).await;
 25    let client_a = server.create_client(cx_a, "user_a").await;
 26    let client_b = server.create_client(cx_b, "user_b").await;
 27
 28    let channel_id = server
 29        .make_channel("zed", None, (&client_a, cx_a), &mut [(&client_b, cx_b)])
 30        .await;
 31
 32    // Client A joins the channel buffer
 33    let channel_buffer_a = client_a
 34        .channel_store()
 35        .update(cx_a, |store, cx| store.open_channel_buffer(channel_id, cx))
 36        .await
 37        .unwrap();
 38
 39    // Client A edits the buffer
 40    let buffer_a = channel_buffer_a.read_with(cx_a, |buffer, _| buffer.buffer());
 41    buffer_a.update(cx_a, |buffer, cx| {
 42        buffer.edit([(0..0, "hello world")], None, cx)
 43    });
 44    buffer_a.update(cx_a, |buffer, cx| {
 45        buffer.edit([(5..5, ", cruel")], None, cx)
 46    });
 47    buffer_a.update(cx_a, |buffer, cx| {
 48        buffer.edit([(0..5, "goodbye")], None, cx)
 49    });
 50    buffer_a.update(cx_a, |buffer, cx| buffer.undo(cx));
 51    assert_eq!(buffer_text(&buffer_a, cx_a), "hello, cruel world");
 52    executor.run_until_parked();
 53
 54    // Client B joins the channel buffer
 55    let channel_buffer_b = client_b
 56        .channel_store()
 57        .update(cx_b, |store, cx| store.open_channel_buffer(channel_id, cx))
 58        .await
 59        .unwrap();
 60    channel_buffer_b.read_with(cx_b, |buffer, _| {
 61        assert_collaborators(
 62            buffer.collaborators(),
 63            &[client_a.user_id(), client_b.user_id()],
 64        );
 65    });
 66
 67    // Client B sees the correct text, and then edits it
 68    let buffer_b = channel_buffer_b.read_with(cx_b, |buffer, _| buffer.buffer());
 69    assert_eq!(
 70        buffer_b.read_with(cx_b, |buffer, _| buffer.remote_id()),
 71        buffer_a.read_with(cx_a, |buffer, _| buffer.remote_id())
 72    );
 73    assert_eq!(buffer_text(&buffer_b, cx_b), "hello, cruel world");
 74    buffer_b.update(cx_b, |buffer, cx| {
 75        buffer.edit([(7..12, "beautiful")], None, cx)
 76    });
 77
 78    // Both A and B see the new edit
 79    executor.run_until_parked();
 80    assert_eq!(buffer_text(&buffer_a, cx_a), "hello, beautiful world");
 81    assert_eq!(buffer_text(&buffer_b, cx_b), "hello, beautiful world");
 82
 83    // Client A closes the channel buffer.
 84    cx_a.update(|_| drop(channel_buffer_a));
 85    executor.run_until_parked();
 86
 87    // Client B sees that client A is gone from the channel buffer.
 88    channel_buffer_b.read_with(cx_b, |buffer, _| {
 89        assert_collaborators(buffer.collaborators(), &[client_b.user_id()]);
 90    });
 91
 92    // Client A rejoins the channel buffer
 93    let _channel_buffer_a = client_a
 94        .channel_store()
 95        .update(cx_a, |store, cx| store.open_channel_buffer(channel_id, cx))
 96        .await
 97        .unwrap();
 98    executor.run_until_parked();
 99
100    // Sanity test, make sure we saw A rejoining
101    channel_buffer_b.read_with(cx_b, |buffer, _| {
102        assert_collaborators(
103            buffer.collaborators(),
104            &[client_a.user_id(), client_b.user_id()],
105        );
106    });
107
108    // Client A loses connection.
109    server.forbid_connections();
110    server.disconnect_client(client_a.peer_id().unwrap());
111    executor.advance_clock(RECEIVE_TIMEOUT + RECONNECT_TIMEOUT);
112
113    // Client B observes A disconnect
114    channel_buffer_b.read_with(cx_b, |buffer, _| {
115        assert_collaborators(buffer.collaborators(), &[client_b.user_id()]);
116    });
117
118    // TODO:
119    // - Test synchronizing offline updates, what happens to A's channel buffer when A disconnects
120    // - Test interaction with channel deletion while buffer is open
121}
122
123#[gpui::test]
124async fn test_channel_notes_participant_indices(
125    executor: BackgroundExecutor,
126    cx_a: &mut TestAppContext,
127    cx_b: &mut TestAppContext,
128    cx_c: &mut TestAppContext,
129) {
130    let mut server = TestServer::start(executor.clone()).await;
131    let client_a = server.create_client(cx_a, "user_a").await;
132    let client_b = server.create_client(cx_b, "user_b").await;
133    let client_c = server.create_client(cx_c, "user_c").await;
134
135    let active_call_a = cx_a.read(ActiveCall::global);
136    let active_call_b = cx_b.read(ActiveCall::global);
137
138    cx_a.update(editor::init);
139    cx_b.update(editor::init);
140    cx_c.update(editor::init);
141
142    let channel_id = server
143        .make_channel(
144            "the-channel",
145            None,
146            (&client_a, cx_a),
147            &mut [(&client_b, cx_b), (&client_c, cx_c)],
148        )
149        .await;
150
151    client_a
152        .fs()
153        .insert_tree("/root", json!({"file.txt": "123"}))
154        .await;
155    let (project_a, worktree_id_a) = client_a.build_local_project("/root", cx_a).await;
156    let project_b = client_b.build_empty_local_project(cx_b);
157    let project_c = client_c.build_empty_local_project(cx_c);
158
159    let (workspace_a, mut cx_a) = client_a.build_workspace(&project_a, cx_a);
160    let (workspace_b, mut cx_b) = client_b.build_workspace(&project_b, cx_b);
161    let (workspace_c, cx_c) = client_c.build_workspace(&project_c, cx_c);
162
163    // Clients A, B, and C open the channel notes
164    let channel_view_a = cx_a
165        .update(|window, cx| ChannelView::open(channel_id, None, workspace_a.clone(), window, cx))
166        .await
167        .unwrap();
168    let channel_view_b = cx_b
169        .update(|window, cx| ChannelView::open(channel_id, None, workspace_b.clone(), window, cx))
170        .await
171        .unwrap();
172    let channel_view_c = cx_c
173        .update(|window, cx| ChannelView::open(channel_id, None, workspace_c.clone(), window, cx))
174        .await
175        .unwrap();
176
177    // Clients A, B, and C all insert and select some text
178    channel_view_a.update_in(cx_a, |notes, window, cx| {
179        notes.editor.update(cx, |editor, cx| {
180            editor.insert("a", window, cx);
181            editor.change_selections(Default::default(), window, cx, |selections| {
182                selections.select_ranges(vec![0..1]);
183            });
184        });
185    });
186    executor.run_until_parked();
187    channel_view_b.update_in(cx_b, |notes, window, cx| {
188        notes.editor.update(cx, |editor, cx| {
189            editor.move_down(&Default::default(), window, cx);
190            editor.insert("b", window, cx);
191            editor.change_selections(Default::default(), window, cx, |selections| {
192                selections.select_ranges(vec![1..2]);
193            });
194        });
195    });
196    executor.run_until_parked();
197    channel_view_c.update_in(cx_c, |notes, window, cx| {
198        notes.editor.update(cx, |editor, cx| {
199            editor.move_down(&Default::default(), window, cx);
200            editor.insert("c", window, cx);
201            editor.change_selections(Default::default(), window, cx, |selections| {
202                selections.select_ranges(vec![2..3]);
203            });
204        });
205    });
206
207    // Client A sees clients B and C without assigned colors, because they aren't
208    // in a call together.
209    executor.run_until_parked();
210    channel_view_a.update_in(cx_a, |notes, window, cx| {
211        notes.editor.update(cx, |editor, cx| {
212            assert_remote_selections(editor, &[(None, 1..2), (None, 2..3)], window, cx);
213        });
214    });
215
216    // Clients A and B join the same call.
217    for (call, cx) in [(&active_call_a, &mut cx_a), (&active_call_b, &mut cx_b)] {
218        call.update(*cx, |call, cx| call.join_channel(channel_id, cx))
219            .await
220            .unwrap();
221    }
222
223    // Clients A and B see each other with two different assigned colors. Client C
224    // still doesn't have a color.
225    executor.run_until_parked();
226    channel_view_a.update_in(cx_a, |notes, window, cx| {
227        notes.editor.update(cx, |editor, cx| {
228            assert_remote_selections(
229                editor,
230                &[(Some(ParticipantIndex(1)), 1..2), (None, 2..3)],
231                window,
232                cx,
233            );
234        });
235    });
236    channel_view_b.update_in(cx_b, |notes, window, cx| {
237        notes.editor.update(cx, |editor, cx| {
238            assert_remote_selections(
239                editor,
240                &[(Some(ParticipantIndex(0)), 0..1), (None, 2..3)],
241                window,
242                cx,
243            );
244        });
245    });
246
247    // Client A shares a project, and client B joins.
248    let project_id = active_call_a
249        .update(cx_a, |call, cx| call.share_project(project_a.clone(), cx))
250        .await
251        .unwrap();
252    let project_b = client_b.join_remote_project(project_id, cx_b).await;
253    let (workspace_b, cx_b) = client_b.build_workspace(&project_b, cx_b);
254
255    // Clients A and B open the same file.
256    let editor_a = workspace_a
257        .update_in(cx_a, |workspace, window, cx| {
258            workspace.open_path((worktree_id_a, "file.txt"), None, true, window, cx)
259        })
260        .await
261        .unwrap()
262        .downcast::<Editor>()
263        .unwrap();
264    let editor_b = workspace_b
265        .update_in(cx_b, |workspace, window, cx| {
266            workspace.open_path((worktree_id_a, "file.txt"), None, true, window, cx)
267        })
268        .await
269        .unwrap()
270        .downcast::<Editor>()
271        .unwrap();
272
273    editor_a.update_in(cx_a, |editor, window, cx| {
274        editor.change_selections(Default::default(), window, cx, |selections| {
275            selections.select_ranges(vec![0..1]);
276        });
277    });
278    editor_b.update_in(cx_b, |editor, window, cx| {
279        editor.change_selections(Default::default(), window, cx, |selections| {
280            selections.select_ranges(vec![2..3]);
281        });
282    });
283    executor.run_until_parked();
284
285    // Clients A and B see each other with the same colors as in the channel notes.
286    editor_a.update_in(cx_a, |editor, window, cx| {
287        assert_remote_selections(editor, &[(Some(ParticipantIndex(1)), 2..3)], window, cx);
288    });
289    editor_b.update_in(cx_b, |editor, window, cx| {
290        assert_remote_selections(editor, &[(Some(ParticipantIndex(0)), 0..1)], window, cx);
291    });
292}
293
294#[track_caller]
295fn assert_remote_selections(
296    editor: &mut Editor,
297    expected_selections: &[(Option<ParticipantIndex>, Range<usize>)],
298    window: &mut Window,
299    cx: &mut Context<Editor>,
300) {
301    let snapshot = editor.snapshot(window, cx);
302    let hub = editor.collaboration_hub().unwrap();
303    let collaborators = hub.collaborators(cx);
304    let range = Anchor::min()..Anchor::max();
305    let remote_selections = snapshot
306        .remote_selections_in_range(&range, hub, cx)
307        .map(|s| {
308            let CollaboratorId::PeerId(peer_id) = s.collaborator_id else {
309                panic!("unexpected collaborator id");
310            };
311            let start = s.selection.start.to_offset(&snapshot.buffer_snapshot);
312            let end = s.selection.end.to_offset(&snapshot.buffer_snapshot);
313            let user_id = collaborators.get(&peer_id).unwrap().user_id;
314            let participant_index = hub.user_participant_indices(cx).get(&user_id).copied();
315            (participant_index, start..end)
316        })
317        .collect::<Vec<_>>();
318    assert_eq!(
319        remote_selections, expected_selections,
320        "incorrect remote selections"
321    );
322}
323
324#[gpui::test]
325async fn test_multiple_handles_to_channel_buffer(
326    deterministic: BackgroundExecutor,
327    cx_a: &mut TestAppContext,
328) {
329    let mut server = TestServer::start(deterministic.clone()).await;
330    let client_a = server.create_client(cx_a, "user_a").await;
331
332    let channel_id = server
333        .make_channel("the-channel", None, (&client_a, cx_a), &mut [])
334        .await;
335
336    let channel_buffer_1 = client_a
337        .channel_store()
338        .update(cx_a, |store, cx| store.open_channel_buffer(channel_id, cx));
339    let channel_buffer_2 = client_a
340        .channel_store()
341        .update(cx_a, |store, cx| store.open_channel_buffer(channel_id, cx));
342    let channel_buffer_3 = client_a
343        .channel_store()
344        .update(cx_a, |store, cx| store.open_channel_buffer(channel_id, cx));
345
346    // All concurrent tasks for opening a channel buffer return the same model handle.
347    let (channel_buffer, channel_buffer_2, channel_buffer_3) =
348        future::try_join3(channel_buffer_1, channel_buffer_2, channel_buffer_3)
349            .await
350            .unwrap();
351    let channel_buffer_entity_id = channel_buffer.entity_id();
352    assert_eq!(channel_buffer, channel_buffer_2);
353    assert_eq!(channel_buffer, channel_buffer_3);
354
355    channel_buffer.update(cx_a, |buffer, cx| {
356        buffer.buffer().update(cx, |buffer, cx| {
357            buffer.edit([(0..0, "hello")], None, cx);
358        })
359    });
360    deterministic.run_until_parked();
361
362    cx_a.update(|_| {
363        drop(channel_buffer);
364        drop(channel_buffer_2);
365        drop(channel_buffer_3);
366    });
367    deterministic.run_until_parked();
368
369    // The channel buffer can be reopened after dropping it.
370    let channel_buffer = client_a
371        .channel_store()
372        .update(cx_a, |store, cx| store.open_channel_buffer(channel_id, cx))
373        .await
374        .unwrap();
375    assert_ne!(channel_buffer.entity_id(), channel_buffer_entity_id);
376    channel_buffer.update(cx_a, |buffer, cx| {
377        buffer.buffer().update(cx, |buffer, _| {
378            assert_eq!(buffer.text(), "hello");
379        })
380    });
381}
382
383#[gpui::test]
384async fn test_channel_buffer_disconnect(
385    deterministic: BackgroundExecutor,
386    cx_a: &mut TestAppContext,
387    cx_b: &mut TestAppContext,
388) {
389    let mut server = TestServer::start(deterministic.clone()).await;
390    let client_a = server.create_client(cx_a, "user_a").await;
391    let client_b = server.create_client(cx_b, "user_b").await;
392
393    let channel_id = server
394        .make_channel(
395            "the-channel",
396            None,
397            (&client_a, cx_a),
398            &mut [(&client_b, cx_b)],
399        )
400        .await;
401
402    let channel_buffer_a = client_a
403        .channel_store()
404        .update(cx_a, |store, cx| store.open_channel_buffer(channel_id, cx))
405        .await
406        .unwrap();
407
408    let channel_buffer_b = client_b
409        .channel_store()
410        .update(cx_b, |store, cx| store.open_channel_buffer(channel_id, cx))
411        .await
412        .unwrap();
413
414    server.forbid_connections();
415    server.disconnect_client(client_a.peer_id().unwrap());
416    deterministic.advance_clock(RECEIVE_TIMEOUT + RECONNECT_TIMEOUT);
417
418    channel_buffer_a.update(cx_a, |buffer, cx| {
419        assert_eq!(buffer.channel(cx).unwrap().name, "the-channel");
420        assert!(!buffer.is_connected());
421    });
422
423    deterministic.run_until_parked();
424
425    server.allow_connections();
426    deterministic.advance_clock(RECEIVE_TIMEOUT + RECONNECT_TIMEOUT);
427
428    deterministic.run_until_parked();
429
430    client_a
431        .channel_store()
432        .update(cx_a, |channel_store, _| {
433            channel_store.remove_channel(channel_id)
434        })
435        .await
436        .unwrap();
437    deterministic.run_until_parked();
438
439    // Channel buffer observed the deletion
440    channel_buffer_b.update(cx_b, |buffer, cx| {
441        assert!(buffer.channel(cx).is_none());
442        assert!(!buffer.is_connected());
443    });
444}
445
446#[gpui::test]
447async fn test_rejoin_channel_buffer(
448    deterministic: BackgroundExecutor,
449    cx_a: &mut TestAppContext,
450    cx_b: &mut TestAppContext,
451) {
452    let mut server = TestServer::start(deterministic.clone()).await;
453    let client_a = server.create_client(cx_a, "user_a").await;
454    let client_b = server.create_client(cx_b, "user_b").await;
455
456    let channel_id = server
457        .make_channel(
458            "the-channel",
459            None,
460            (&client_a, cx_a),
461            &mut [(&client_b, cx_b)],
462        )
463        .await;
464
465    let channel_buffer_a = client_a
466        .channel_store()
467        .update(cx_a, |store, cx| store.open_channel_buffer(channel_id, cx))
468        .await
469        .unwrap();
470    let channel_buffer_b = client_b
471        .channel_store()
472        .update(cx_b, |store, cx| store.open_channel_buffer(channel_id, cx))
473        .await
474        .unwrap();
475
476    channel_buffer_a.update(cx_a, |buffer, cx| {
477        buffer.buffer().update(cx, |buffer, cx| {
478            buffer.edit([(0..0, "1")], None, cx);
479        })
480    });
481    deterministic.run_until_parked();
482
483    // Client A disconnects.
484    server.forbid_connections();
485    server.disconnect_client(client_a.peer_id().unwrap());
486
487    // Both clients make an edit.
488    channel_buffer_a.update(cx_a, |buffer, cx| {
489        buffer.buffer().update(cx, |buffer, cx| {
490            buffer.edit([(1..1, "2")], None, cx);
491        })
492    });
493    channel_buffer_b.update(cx_b, |buffer, cx| {
494        buffer.buffer().update(cx, |buffer, cx| {
495            buffer.edit([(0..0, "0")], None, cx);
496        })
497    });
498
499    // Both clients see their own edit.
500    deterministic.run_until_parked();
501    channel_buffer_a.read_with(cx_a, |buffer, cx| {
502        assert_eq!(buffer.buffer().read(cx).text(), "12");
503    });
504    channel_buffer_b.read_with(cx_b, |buffer, cx| {
505        assert_eq!(buffer.buffer().read(cx).text(), "01");
506    });
507
508    // Client A reconnects. Both clients see each other's edits, and see
509    // the same collaborators.
510    server.allow_connections();
511    deterministic.advance_clock(RECEIVE_TIMEOUT);
512    channel_buffer_a.read_with(cx_a, |buffer, cx| {
513        assert_eq!(buffer.buffer().read(cx).text(), "012");
514    });
515    channel_buffer_b.read_with(cx_b, |buffer, cx| {
516        assert_eq!(buffer.buffer().read(cx).text(), "012");
517    });
518
519    channel_buffer_a.read_with(cx_a, |buffer_a, _| {
520        channel_buffer_b.read_with(cx_b, |buffer_b, _| {
521            assert_eq!(buffer_a.collaborators(), buffer_b.collaborators());
522        });
523    });
524}
525
526#[gpui::test]
527async fn test_channel_buffers_and_server_restarts(
528    deterministic: BackgroundExecutor,
529    cx_a: &mut TestAppContext,
530    cx_b: &mut TestAppContext,
531    cx_c: &mut TestAppContext,
532) {
533    let mut server = TestServer::start(deterministic.clone()).await;
534    let client_a = server.create_client(cx_a, "user_a").await;
535    let client_b = server.create_client(cx_b, "user_b").await;
536    let client_c = server.create_client(cx_c, "user_c").await;
537
538    let channel_id = server
539        .make_channel(
540            "the-channel",
541            None,
542            (&client_a, cx_a),
543            &mut [(&client_b, cx_b), (&client_c, cx_c)],
544        )
545        .await;
546
547    let channel_buffer_a = client_a
548        .channel_store()
549        .update(cx_a, |store, cx| store.open_channel_buffer(channel_id, cx))
550        .await
551        .unwrap();
552    let channel_buffer_b = client_b
553        .channel_store()
554        .update(cx_b, |store, cx| store.open_channel_buffer(channel_id, cx))
555        .await
556        .unwrap();
557    let _channel_buffer_c = client_c
558        .channel_store()
559        .update(cx_c, |store, cx| store.open_channel_buffer(channel_id, cx))
560        .await
561        .unwrap();
562
563    channel_buffer_a.update(cx_a, |buffer, cx| {
564        buffer.buffer().update(cx, |buffer, cx| {
565            buffer.edit([(0..0, "1")], None, cx);
566        })
567    });
568    deterministic.run_until_parked();
569
570    // Client C can't reconnect.
571    client_c.override_establish_connection(|_, cx| cx.spawn(async |_| future::pending().await));
572
573    // Server stops.
574    server.reset().await;
575    deterministic.advance_clock(RECEIVE_TIMEOUT);
576
577    // While the server is down, both clients make an edit.
578    channel_buffer_a.update(cx_a, |buffer, cx| {
579        buffer.buffer().update(cx, |buffer, cx| {
580            buffer.edit([(1..1, "2")], None, cx);
581        })
582    });
583    channel_buffer_b.update(cx_b, |buffer, cx| {
584        buffer.buffer().update(cx, |buffer, cx| {
585            buffer.edit([(0..0, "0")], None, cx);
586        })
587    });
588
589    // Server restarts.
590    server.start().await.unwrap();
591    deterministic.advance_clock(CLEANUP_TIMEOUT);
592
593    // Clients reconnects. Clients A and B see each other's edits, and see
594    // that client C has disconnected.
595    channel_buffer_a.read_with(cx_a, |buffer, cx| {
596        assert_eq!(buffer.buffer().read(cx).text(), "012");
597    });
598    channel_buffer_b.read_with(cx_b, |buffer, cx| {
599        assert_eq!(buffer.buffer().read(cx).text(), "012");
600    });
601
602    channel_buffer_a.read_with(cx_a, |buffer_a, _| {
603        channel_buffer_b.read_with(cx_b, |buffer_b, _| {
604            assert_collaborators(
605                buffer_a.collaborators(),
606                &[client_a.user_id(), client_b.user_id()],
607            );
608            assert_eq!(buffer_a.collaborators(), buffer_b.collaborators());
609        });
610    });
611}
612
613#[gpui::test]
614async fn test_channel_buffer_changes(
615    deterministic: BackgroundExecutor,
616    cx_a: &mut TestAppContext,
617    cx_b: &mut TestAppContext,
618) {
619    let (server, client_a, client_b, channel_id) = TestServer::start2(cx_a, cx_b).await;
620    let (_, cx_a) = client_a.build_test_workspace(cx_a).await;
621    let (workspace_b, cx_b) = client_b.build_test_workspace(cx_b).await;
622    let channel_store_b = client_b.channel_store().clone();
623
624    // Editing the channel notes should set them to dirty
625    open_channel_notes(channel_id, cx_a).await.unwrap();
626    cx_a.simulate_keystrokes("1");
627    channel_store_b.read_with(cx_b, |channel_store, _| {
628        assert!(channel_store.has_channel_buffer_changed(channel_id))
629    });
630
631    // Opening the buffer should clear the changed flag.
632    open_channel_notes(channel_id, cx_b).await.unwrap();
633    channel_store_b.read_with(cx_b, |channel_store, _| {
634        assert!(!channel_store.has_channel_buffer_changed(channel_id))
635    });
636
637    // Editing the channel while the buffer is open should not show that the buffer has changed.
638    cx_a.simulate_keystrokes("2");
639    channel_store_b.read_with(cx_b, |channel_store, _| {
640        assert!(!channel_store.has_channel_buffer_changed(channel_id))
641    });
642
643    // Test that the server is tracking things correctly, and we retain our 'not changed'
644    // state across a disconnect
645    deterministic.advance_clock(ACKNOWLEDGE_DEBOUNCE_INTERVAL);
646    server
647        .simulate_long_connection_interruption(client_b.peer_id().unwrap(), deterministic.clone());
648    channel_store_b.read_with(cx_b, |channel_store, _| {
649        assert!(!channel_store.has_channel_buffer_changed(channel_id))
650    });
651
652    // Closing the buffer should re-enable change tracking
653    cx_b.update(|window, cx| {
654        workspace_b.update(cx, |workspace, cx| {
655            workspace.close_all_items_and_panes(&Default::default(), window, cx)
656        });
657    });
658    deterministic.run_until_parked();
659
660    cx_a.simulate_keystrokes("3");
661    channel_store_b.read_with(cx_b, |channel_store, _| {
662        assert!(channel_store.has_channel_buffer_changed(channel_id))
663    });
664}
665
666#[gpui::test]
667async fn test_channel_buffer_changes_persist(
668    cx_a: &mut TestAppContext,
669    cx_b: &mut TestAppContext,
670    cx_b2: &mut TestAppContext,
671) {
672    let (mut server, client_a, client_b, channel_id) = TestServer::start2(cx_a, cx_b).await;
673    let (_, cx_a) = client_a.build_test_workspace(cx_a).await;
674    let (_, cx_b) = client_b.build_test_workspace(cx_b).await;
675
676    // a) edits the notes
677    open_channel_notes(channel_id, cx_a).await.unwrap();
678    cx_a.simulate_keystrokes("1");
679    // b) opens them to observe the current version
680    open_channel_notes(channel_id, cx_b).await.unwrap();
681
682    // On boot the client should get the correct state.
683    let client_b2 = server.create_client(cx_b2, "user_b").await;
684    let channel_store_b2 = client_b2.channel_store().clone();
685    channel_store_b2.read_with(cx_b2, |channel_store, _| {
686        assert!(!channel_store.has_channel_buffer_changed(channel_id))
687    });
688}
689
690#[track_caller]
691fn assert_collaborators(collaborators: &HashMap<PeerId, Collaborator>, ids: &[Option<UserId>]) {
692    let mut user_ids = collaborators
693        .values()
694        .map(|collaborator| collaborator.user_id)
695        .collect::<Vec<_>>();
696    user_ids.sort();
697    assert_eq!(
698        user_ids,
699        ids.iter().map(|id| id.unwrap()).collect::<Vec<_>>()
700    );
701}
702
703fn buffer_text(channel_buffer: &Entity<language::Buffer>, cx: &mut TestAppContext) -> String {
704    channel_buffer.read_with(cx, |buffer, _| buffer.text())
705}