1use crate::{
2 RoomParticipants, TestClient, TestServer, channel_id, following_tests::join_channel,
3 room_participants,
4};
5use anyhow::{Result, anyhow};
6use buffer_diff::{DiffHunkSecondaryStatus, DiffHunkStatus, assert_hunks};
7use call::{ActiveCall, Room, room};
8use client::{RECEIVE_TIMEOUT, User};
9use collab::rpc::{CLEANUP_TIMEOUT, RECONNECT_TIMEOUT};
10use collections::{BTreeMap, HashMap, HashSet};
11use fs::{FakeFs, Fs as _, RemoveOptions};
12use futures::{StreamExt as _, channel::mpsc};
13use git::{
14 repository::repo_path,
15 status::{FileStatus, StatusCode, TrackedStatus, UnmergedStatus, UnmergedStatusCode},
16};
17use gpui::{
18 App, BackgroundExecutor, Entity, Modifiers, MouseButton, MouseDownEvent, TestAppContext,
19 UpdateGlobal, px, size,
20};
21use language::{
22 Diagnostic, DiagnosticEntry, DiagnosticSourceKind, FakeLspAdapter, Language, LanguageConfig,
23 LanguageMatcher, LineEnding, OffsetRangeExt, Point, Rope,
24 language_settings::{Formatter, FormatterList},
25 rust_lang, tree_sitter_rust, tree_sitter_typescript,
26};
27use lsp::{DEFAULT_LSP_REQUEST_TIMEOUT, LanguageServerId, OneOf};
28use parking_lot::Mutex;
29use pretty_assertions::assert_eq;
30use project::{
31 DiagnosticSummary, HoverBlockKind, Project, ProjectPath,
32 lsp_store::{FormatTrigger, LspFormatTarget, SymbolLocation},
33 search::{SearchQuery, SearchResult},
34};
35use rand::prelude::*;
36use serde_json::json;
37use settings::{LanguageServerFormatterSpecifier, PrettierSettingsContent, SettingsStore};
38use std::{
39 cell::{Cell, RefCell},
40 env, future, mem,
41 path::{Path, PathBuf},
42 rc::Rc,
43 sync::{
44 Arc,
45 atomic::{AtomicBool, Ordering::SeqCst},
46 },
47 time::Duration,
48};
49use unindent::Unindent as _;
50use util::{path, rel_path::rel_path, uri};
51use workspace::{Pane, ParticipantLocation};
52
53#[ctor::ctor]
54fn init_logger() {
55 zlog::init_test();
56}
57
58#[gpui::test(iterations = 10)]
59async fn test_database_failure_during_client_reconnection(
60 executor: BackgroundExecutor,
61 cx: &mut TestAppContext,
62) {
63 let mut server = TestServer::start(executor.clone()).await;
64 let client = server.create_client(cx, "user_a").await;
65
66 // Keep disconnecting the client until a database failure prevents it from
67 // reconnecting.
68 server.test_db.set_query_failure_probability(0.3);
69 loop {
70 server.disconnect_client(client.peer_id().unwrap());
71 executor.advance_clock(RECEIVE_TIMEOUT + RECONNECT_TIMEOUT);
72 if !client.status().borrow().is_connected() {
73 break;
74 }
75 }
76
77 // Make the database healthy again and ensure the client can finally connect.
78 server.test_db.set_query_failure_probability(0.);
79 executor.advance_clock(RECEIVE_TIMEOUT + RECONNECT_TIMEOUT);
80 assert!(
81 matches!(*client.status().borrow(), client::Status::Connected { .. }),
82 "status was {:?}",
83 *client.status().borrow()
84 );
85}
86
87#[gpui::test(iterations = 10)]
88async fn test_basic_calls(
89 executor: BackgroundExecutor,
90 cx_a: &mut TestAppContext,
91 cx_b: &mut TestAppContext,
92 cx_b2: &mut TestAppContext,
93 cx_c: &mut TestAppContext,
94) {
95 let mut server = TestServer::start(executor.clone()).await;
96
97 let client_a = server.create_client(cx_a, "user_a").await;
98 let client_b = server.create_client(cx_b, "user_b").await;
99 let client_c = server.create_client(cx_c, "user_c").await;
100 server
101 .make_contacts(&mut [(&client_a, cx_a), (&client_b, cx_b), (&client_c, cx_c)])
102 .await;
103
104 let active_call_a = cx_a.read(ActiveCall::global);
105 let active_call_b = cx_b.read(ActiveCall::global);
106 let active_call_c = cx_c.read(ActiveCall::global);
107
108 // Call user B from client A.
109 active_call_a
110 .update(cx_a, |call, cx| {
111 call.invite(client_b.user_id().unwrap(), None, cx)
112 })
113 .await
114 .unwrap();
115 let room_a = active_call_a.read_with(cx_a, |call, _| call.room().unwrap().clone());
116 executor.run_until_parked();
117 assert_eq!(
118 room_participants(&room_a, cx_a),
119 RoomParticipants {
120 remote: Default::default(),
121 pending: vec!["user_b".to_string()]
122 }
123 );
124
125 // User B receives the call.
126
127 let mut incoming_call_b = active_call_b.read_with(cx_b, |call, _| call.incoming());
128 let call_b = incoming_call_b.next().await.unwrap().unwrap();
129 assert_eq!(call_b.calling_user.github_login, "user_a");
130
131 // User B connects via another client and also receives a ring on the newly-connected client.
132 let _client_b2 = server.create_client(cx_b2, "user_b").await;
133 let active_call_b2 = cx_b2.read(ActiveCall::global);
134
135 let mut incoming_call_b2 = active_call_b2.read_with(cx_b2, |call, _| call.incoming());
136 executor.run_until_parked();
137 let call_b2 = incoming_call_b2.next().await.unwrap().unwrap();
138 assert_eq!(call_b2.calling_user.github_login, "user_a");
139
140 // User B joins the room using the first client.
141 active_call_b
142 .update(cx_b, |call, cx| call.accept_incoming(cx))
143 .await
144 .unwrap();
145
146 let room_b = active_call_b.read_with(cx_b, |call, _| call.room().unwrap().clone());
147 assert!(incoming_call_b.next().await.unwrap().is_none());
148
149 executor.run_until_parked();
150 assert_eq!(
151 room_participants(&room_a, cx_a),
152 RoomParticipants {
153 remote: vec!["user_b".to_string()],
154 pending: Default::default()
155 }
156 );
157 assert_eq!(
158 room_participants(&room_b, cx_b),
159 RoomParticipants {
160 remote: vec!["user_a".to_string()],
161 pending: Default::default()
162 }
163 );
164
165 // Call user C from client B.
166
167 let mut incoming_call_c = active_call_c.read_with(cx_c, |call, _| call.incoming());
168 active_call_b
169 .update(cx_b, |call, cx| {
170 call.invite(client_c.user_id().unwrap(), None, cx)
171 })
172 .await
173 .unwrap();
174
175 executor.run_until_parked();
176 assert_eq!(
177 room_participants(&room_a, cx_a),
178 RoomParticipants {
179 remote: vec!["user_b".to_string()],
180 pending: vec!["user_c".to_string()]
181 }
182 );
183 assert_eq!(
184 room_participants(&room_b, cx_b),
185 RoomParticipants {
186 remote: vec!["user_a".to_string()],
187 pending: vec!["user_c".to_string()]
188 }
189 );
190
191 // User C receives the call, but declines it.
192 let call_c = incoming_call_c.next().await.unwrap().unwrap();
193 assert_eq!(call_c.calling_user.github_login, "user_b");
194 active_call_c.update(cx_c, |call, cx| call.decline_incoming(cx).unwrap());
195 assert!(incoming_call_c.next().await.unwrap().is_none());
196
197 executor.run_until_parked();
198 assert_eq!(
199 room_participants(&room_a, cx_a),
200 RoomParticipants {
201 remote: vec!["user_b".to_string()],
202 pending: Default::default()
203 }
204 );
205 assert_eq!(
206 room_participants(&room_b, cx_b),
207 RoomParticipants {
208 remote: vec!["user_a".to_string()],
209 pending: Default::default()
210 }
211 );
212
213 // Call user C again from user A.
214 active_call_a
215 .update(cx_a, |call, cx| {
216 call.invite(client_c.user_id().unwrap(), None, cx)
217 })
218 .await
219 .unwrap();
220
221 executor.run_until_parked();
222 assert_eq!(
223 room_participants(&room_a, cx_a),
224 RoomParticipants {
225 remote: vec!["user_b".to_string()],
226 pending: vec!["user_c".to_string()]
227 }
228 );
229 assert_eq!(
230 room_participants(&room_b, cx_b),
231 RoomParticipants {
232 remote: vec!["user_a".to_string()],
233 pending: vec!["user_c".to_string()]
234 }
235 );
236
237 // User C accepts the call.
238 let call_c = incoming_call_c.next().await.unwrap().unwrap();
239 assert_eq!(call_c.calling_user.github_login, "user_a");
240 active_call_c
241 .update(cx_c, |call, cx| call.accept_incoming(cx))
242 .await
243 .unwrap();
244 assert!(incoming_call_c.next().await.unwrap().is_none());
245
246 let room_c = active_call_c.read_with(cx_c, |call, _| call.room().unwrap().clone());
247
248 executor.run_until_parked();
249 assert_eq!(
250 room_participants(&room_a, cx_a),
251 RoomParticipants {
252 remote: vec!["user_b".to_string(), "user_c".to_string()],
253 pending: Default::default()
254 }
255 );
256 assert_eq!(
257 room_participants(&room_b, cx_b),
258 RoomParticipants {
259 remote: vec!["user_a".to_string(), "user_c".to_string()],
260 pending: Default::default()
261 }
262 );
263 assert_eq!(
264 room_participants(&room_c, cx_c),
265 RoomParticipants {
266 remote: vec!["user_a".to_string(), "user_b".to_string()],
267 pending: Default::default()
268 }
269 );
270
271 // User A shares their screen
272 let display = gpui::TestScreenCaptureSource::new();
273 let events_b = active_call_events(cx_b);
274 let events_c = active_call_events(cx_c);
275 cx_a.set_screen_capture_sources(vec![display]);
276 let screen_a = cx_a
277 .update(|cx| cx.screen_capture_sources())
278 .await
279 .unwrap()
280 .unwrap()
281 .into_iter()
282 .next()
283 .unwrap();
284 active_call_a
285 .update(cx_a, |call, cx| {
286 call.room()
287 .unwrap()
288 .update(cx, |room, cx| room.share_screen(screen_a, cx))
289 })
290 .await
291 .unwrap();
292
293 executor.run_until_parked();
294
295 // User B observes the remote screen sharing track.
296 assert_eq!(events_b.borrow().len(), 1);
297 let event_b = events_b.borrow().first().unwrap().clone();
298 if let call::room::Event::RemoteVideoTracksChanged { participant_id } = event_b {
299 assert_eq!(participant_id, client_a.peer_id().unwrap());
300
301 room_b.read_with(cx_b, |room, _| {
302 assert_eq!(
303 room.remote_participants()[&client_a.user_id().unwrap()]
304 .video_tracks
305 .len(),
306 1
307 );
308 });
309 } else {
310 panic!("unexpected event")
311 }
312
313 // User C observes the remote screen sharing track.
314 assert_eq!(events_c.borrow().len(), 1);
315 let event_c = events_c.borrow().first().unwrap().clone();
316 if let call::room::Event::RemoteVideoTracksChanged { participant_id } = event_c {
317 assert_eq!(participant_id, client_a.peer_id().unwrap());
318
319 room_c.read_with(cx_c, |room, _| {
320 assert_eq!(
321 room.remote_participants()[&client_a.user_id().unwrap()]
322 .video_tracks
323 .len(),
324 1
325 );
326 });
327 } else {
328 panic!("unexpected event")
329 }
330
331 // User A leaves the room.
332 active_call_a
333 .update(cx_a, |call, cx| {
334 let hang_up = call.hang_up(cx);
335 assert!(call.room().is_none());
336 hang_up
337 })
338 .await
339 .unwrap();
340 executor.run_until_parked();
341 assert_eq!(
342 room_participants(&room_a, cx_a),
343 RoomParticipants {
344 remote: Default::default(),
345 pending: Default::default()
346 }
347 );
348 assert_eq!(
349 room_participants(&room_b, cx_b),
350 RoomParticipants {
351 remote: vec!["user_c".to_string()],
352 pending: Default::default()
353 }
354 );
355 assert_eq!(
356 room_participants(&room_c, cx_c),
357 RoomParticipants {
358 remote: vec!["user_b".to_string()],
359 pending: Default::default()
360 }
361 );
362
363 // User B gets disconnected from the LiveKit server, which causes them
364 // to automatically leave the room. User C leaves the room as well because
365 // nobody else is in there.
366 server
367 .test_livekit_server
368 .disconnect_client(client_b.user_id().unwrap().to_string())
369 .await;
370 executor.run_until_parked();
371
372 active_call_b.read_with(cx_b, |call, _| assert!(call.room().is_none()));
373
374 active_call_c.read_with(cx_c, |call, _| assert!(call.room().is_none()));
375 assert_eq!(
376 room_participants(&room_a, cx_a),
377 RoomParticipants {
378 remote: Default::default(),
379 pending: Default::default()
380 }
381 );
382 assert_eq!(
383 room_participants(&room_b, cx_b),
384 RoomParticipants {
385 remote: Default::default(),
386 pending: Default::default()
387 }
388 );
389 assert_eq!(
390 room_participants(&room_c, cx_c),
391 RoomParticipants {
392 remote: Default::default(),
393 pending: Default::default()
394 }
395 );
396}
397
398#[gpui::test(iterations = 10)]
399async fn test_calling_multiple_users_simultaneously(
400 executor: BackgroundExecutor,
401 cx_a: &mut TestAppContext,
402 cx_b: &mut TestAppContext,
403 cx_c: &mut TestAppContext,
404 cx_d: &mut TestAppContext,
405) {
406 let mut server = TestServer::start(executor.clone()).await;
407
408 let client_a = server.create_client(cx_a, "user_a").await;
409 let client_b = server.create_client(cx_b, "user_b").await;
410 let client_c = server.create_client(cx_c, "user_c").await;
411 let client_d = server.create_client(cx_d, "user_d").await;
412 server
413 .make_contacts(&mut [
414 (&client_a, cx_a),
415 (&client_b, cx_b),
416 (&client_c, cx_c),
417 (&client_d, cx_d),
418 ])
419 .await;
420
421 let active_call_a = cx_a.read(ActiveCall::global);
422 let active_call_b = cx_b.read(ActiveCall::global);
423 let active_call_c = cx_c.read(ActiveCall::global);
424 let active_call_d = cx_d.read(ActiveCall::global);
425
426 // Simultaneously call user B and user C from client A.
427 let b_invite = active_call_a.update(cx_a, |call, cx| {
428 call.invite(client_b.user_id().unwrap(), None, cx)
429 });
430 let c_invite = active_call_a.update(cx_a, |call, cx| {
431 call.invite(client_c.user_id().unwrap(), None, cx)
432 });
433 b_invite.await.unwrap();
434 c_invite.await.unwrap();
435
436 let room_a = active_call_a.read_with(cx_a, |call, _| call.room().unwrap().clone());
437 executor.run_until_parked();
438 assert_eq!(
439 room_participants(&room_a, cx_a),
440 RoomParticipants {
441 remote: Default::default(),
442 pending: vec!["user_b".to_string(), "user_c".to_string()]
443 }
444 );
445
446 // Call client D from client A.
447 active_call_a
448 .update(cx_a, |call, cx| {
449 call.invite(client_d.user_id().unwrap(), None, cx)
450 })
451 .await
452 .unwrap();
453 executor.run_until_parked();
454 assert_eq!(
455 room_participants(&room_a, cx_a),
456 RoomParticipants {
457 remote: Default::default(),
458 pending: vec![
459 "user_b".to_string(),
460 "user_c".to_string(),
461 "user_d".to_string()
462 ]
463 }
464 );
465
466 // Accept the call on all clients simultaneously.
467 let accept_b = active_call_b.update(cx_b, |call, cx| call.accept_incoming(cx));
468 let accept_c = active_call_c.update(cx_c, |call, cx| call.accept_incoming(cx));
469 let accept_d = active_call_d.update(cx_d, |call, cx| call.accept_incoming(cx));
470 accept_b.await.unwrap();
471 accept_c.await.unwrap();
472 accept_d.await.unwrap();
473
474 executor.run_until_parked();
475
476 let room_b = active_call_b.read_with(cx_b, |call, _| call.room().unwrap().clone());
477
478 let room_c = active_call_c.read_with(cx_c, |call, _| call.room().unwrap().clone());
479
480 let room_d = active_call_d.read_with(cx_d, |call, _| call.room().unwrap().clone());
481 assert_eq!(
482 room_participants(&room_a, cx_a),
483 RoomParticipants {
484 remote: vec![
485 "user_b".to_string(),
486 "user_c".to_string(),
487 "user_d".to_string(),
488 ],
489 pending: Default::default()
490 }
491 );
492 assert_eq!(
493 room_participants(&room_b, cx_b),
494 RoomParticipants {
495 remote: vec![
496 "user_a".to_string(),
497 "user_c".to_string(),
498 "user_d".to_string(),
499 ],
500 pending: Default::default()
501 }
502 );
503 assert_eq!(
504 room_participants(&room_c, cx_c),
505 RoomParticipants {
506 remote: vec![
507 "user_a".to_string(),
508 "user_b".to_string(),
509 "user_d".to_string(),
510 ],
511 pending: Default::default()
512 }
513 );
514 assert_eq!(
515 room_participants(&room_d, cx_d),
516 RoomParticipants {
517 remote: vec![
518 "user_a".to_string(),
519 "user_b".to_string(),
520 "user_c".to_string(),
521 ],
522 pending: Default::default()
523 }
524 );
525}
526
527#[gpui::test(iterations = 10)]
528async fn test_joining_channels_and_calling_multiple_users_simultaneously(
529 executor: BackgroundExecutor,
530 cx_a: &mut TestAppContext,
531 cx_b: &mut TestAppContext,
532 cx_c: &mut TestAppContext,
533) {
534 let mut server = TestServer::start(executor.clone()).await;
535
536 let client_a = server.create_client(cx_a, "user_a").await;
537 let client_b = server.create_client(cx_b, "user_b").await;
538 let client_c = server.create_client(cx_c, "user_c").await;
539 server
540 .make_contacts(&mut [(&client_a, cx_a), (&client_b, cx_b), (&client_c, cx_c)])
541 .await;
542
543 let channel_1 = server
544 .make_channel(
545 "channel1",
546 None,
547 (&client_a, cx_a),
548 &mut [(&client_b, cx_b), (&client_c, cx_c)],
549 )
550 .await;
551
552 let channel_2 = server
553 .make_channel(
554 "channel2",
555 None,
556 (&client_a, cx_a),
557 &mut [(&client_b, cx_b), (&client_c, cx_c)],
558 )
559 .await;
560
561 let active_call_a = cx_a.read(ActiveCall::global);
562
563 // Simultaneously join channel 1 and then channel 2
564 active_call_a
565 .update(cx_a, |call, cx| call.join_channel(channel_1, cx))
566 .detach();
567 let join_channel_2 = active_call_a.update(cx_a, |call, cx| call.join_channel(channel_2, cx));
568
569 join_channel_2.await.unwrap();
570
571 let room_a = active_call_a.read_with(cx_a, |call, _| call.room().unwrap().clone());
572 executor.run_until_parked();
573
574 assert_eq!(channel_id(&room_a, cx_a), Some(channel_2));
575
576 // Leave the room
577 active_call_a
578 .update(cx_a, |call, cx| call.hang_up(cx))
579 .await
580 .unwrap();
581
582 // Initiating invites and then joining a channel should fail gracefully
583 let b_invite = active_call_a.update(cx_a, |call, cx| {
584 call.invite(client_b.user_id().unwrap(), None, cx)
585 });
586 let c_invite = active_call_a.update(cx_a, |call, cx| {
587 call.invite(client_c.user_id().unwrap(), None, cx)
588 });
589
590 let join_channel = active_call_a.update(cx_a, |call, cx| call.join_channel(channel_1, cx));
591
592 b_invite.await.unwrap();
593 c_invite.await.unwrap();
594 join_channel.await.unwrap();
595
596 let room_a = active_call_a.read_with(cx_a, |call, _| call.room().unwrap().clone());
597 executor.run_until_parked();
598
599 assert_eq!(
600 room_participants(&room_a, cx_a),
601 RoomParticipants {
602 remote: Default::default(),
603 pending: vec!["user_b".to_string(), "user_c".to_string()]
604 }
605 );
606
607 assert_eq!(channel_id(&room_a, cx_a), None);
608
609 // Leave the room
610 active_call_a
611 .update(cx_a, |call, cx| call.hang_up(cx))
612 .await
613 .unwrap();
614
615 // Simultaneously join channel 1 and call user B and user C from client A.
616 let join_channel = active_call_a.update(cx_a, |call, cx| call.join_channel(channel_1, cx));
617
618 let b_invite = active_call_a.update(cx_a, |call, cx| {
619 call.invite(client_b.user_id().unwrap(), None, cx)
620 });
621 let c_invite = active_call_a.update(cx_a, |call, cx| {
622 call.invite(client_c.user_id().unwrap(), None, cx)
623 });
624
625 join_channel.await.unwrap();
626 b_invite.await.unwrap();
627 c_invite.await.unwrap();
628
629 active_call_a.read_with(cx_a, |call, _| call.room().unwrap().clone());
630 executor.run_until_parked();
631}
632
633#[gpui::test(iterations = 10)]
634async fn test_room_uniqueness(
635 executor: BackgroundExecutor,
636 cx_a: &mut TestAppContext,
637 cx_a2: &mut TestAppContext,
638 cx_b: &mut TestAppContext,
639 cx_b2: &mut TestAppContext,
640 cx_c: &mut TestAppContext,
641) {
642 let mut server = TestServer::start(executor.clone()).await;
643 let client_a = server.create_client(cx_a, "user_a").await;
644 let _client_a2 = server.create_client(cx_a2, "user_a").await;
645 let client_b = server.create_client(cx_b, "user_b").await;
646 let _client_b2 = server.create_client(cx_b2, "user_b").await;
647 let client_c = server.create_client(cx_c, "user_c").await;
648 server
649 .make_contacts(&mut [(&client_a, cx_a), (&client_b, cx_b), (&client_c, cx_c)])
650 .await;
651
652 let active_call_a = cx_a.read(ActiveCall::global);
653 let active_call_a2 = cx_a2.read(ActiveCall::global);
654 let active_call_b = cx_b.read(ActiveCall::global);
655 let active_call_b2 = cx_b2.read(ActiveCall::global);
656 let active_call_c = cx_c.read(ActiveCall::global);
657
658 // Call user B from client A.
659 active_call_a
660 .update(cx_a, |call, cx| {
661 call.invite(client_b.user_id().unwrap(), None, cx)
662 })
663 .await
664 .unwrap();
665
666 // Ensure a new room can't be created given user A just created one.
667 active_call_a2
668 .update(cx_a2, |call, cx| {
669 call.invite(client_c.user_id().unwrap(), None, cx)
670 })
671 .await
672 .unwrap_err();
673
674 active_call_a2.read_with(cx_a2, |call, _| assert!(call.room().is_none()));
675
676 // User B receives the call from user A.
677
678 let mut incoming_call_b = active_call_b.read_with(cx_b, |call, _| call.incoming());
679 let call_b1 = incoming_call_b.next().await.unwrap().unwrap();
680 assert_eq!(call_b1.calling_user.github_login, "user_a");
681
682 // Ensure calling users A and B from client C fails.
683 active_call_c
684 .update(cx_c, |call, cx| {
685 call.invite(client_a.user_id().unwrap(), None, cx)
686 })
687 .await
688 .unwrap_err();
689 active_call_c
690 .update(cx_c, |call, cx| {
691 call.invite(client_b.user_id().unwrap(), None, cx)
692 })
693 .await
694 .unwrap_err();
695
696 // Ensure User B can't create a room while they still have an incoming call.
697 active_call_b2
698 .update(cx_b2, |call, cx| {
699 call.invite(client_c.user_id().unwrap(), None, cx)
700 })
701 .await
702 .unwrap_err();
703
704 active_call_b2.read_with(cx_b2, |call, _| assert!(call.room().is_none()));
705
706 // User B joins the room and calling them after they've joined still fails.
707 active_call_b
708 .update(cx_b, |call, cx| call.accept_incoming(cx))
709 .await
710 .unwrap();
711 active_call_c
712 .update(cx_c, |call, cx| {
713 call.invite(client_b.user_id().unwrap(), None, cx)
714 })
715 .await
716 .unwrap_err();
717
718 // Ensure User B can't create a room while they belong to another room.
719 active_call_b2
720 .update(cx_b2, |call, cx| {
721 call.invite(client_c.user_id().unwrap(), None, cx)
722 })
723 .await
724 .unwrap_err();
725
726 active_call_b2.read_with(cx_b2, |call, _| assert!(call.room().is_none()));
727
728 // Client C can successfully call client B after client B leaves the room.
729 active_call_b
730 .update(cx_b, |call, cx| call.hang_up(cx))
731 .await
732 .unwrap();
733 executor.run_until_parked();
734 active_call_c
735 .update(cx_c, |call, cx| {
736 call.invite(client_b.user_id().unwrap(), None, cx)
737 })
738 .await
739 .unwrap();
740 executor.run_until_parked();
741 let call_b2 = incoming_call_b.next().await.unwrap().unwrap();
742 assert_eq!(call_b2.calling_user.github_login, "user_c");
743}
744
745#[gpui::test(iterations = 10)]
746async fn test_client_disconnecting_from_room(
747 executor: BackgroundExecutor,
748 cx_a: &mut TestAppContext,
749 cx_b: &mut TestAppContext,
750) {
751 let mut server = TestServer::start(executor.clone()).await;
752 let client_a = server.create_client(cx_a, "user_a").await;
753 let client_b = server.create_client(cx_b, "user_b").await;
754 server
755 .make_contacts(&mut [(&client_a, cx_a), (&client_b, cx_b)])
756 .await;
757
758 let active_call_a = cx_a.read(ActiveCall::global);
759 let active_call_b = cx_b.read(ActiveCall::global);
760
761 // Call user B from client A.
762 active_call_a
763 .update(cx_a, |call, cx| {
764 call.invite(client_b.user_id().unwrap(), None, cx)
765 })
766 .await
767 .unwrap();
768
769 let room_a = active_call_a.read_with(cx_a, |call, _| call.room().unwrap().clone());
770
771 // User B receives the call and joins the room.
772
773 let mut incoming_call_b = active_call_b.read_with(cx_b, |call, _| call.incoming());
774 incoming_call_b.next().await.unwrap().unwrap();
775 active_call_b
776 .update(cx_b, |call, cx| call.accept_incoming(cx))
777 .await
778 .unwrap();
779
780 let room_b = active_call_b.read_with(cx_b, |call, _| call.room().unwrap().clone());
781 executor.run_until_parked();
782 assert_eq!(
783 room_participants(&room_a, cx_a),
784 RoomParticipants {
785 remote: vec!["user_b".to_string()],
786 pending: Default::default()
787 }
788 );
789 assert_eq!(
790 room_participants(&room_b, cx_b),
791 RoomParticipants {
792 remote: vec!["user_a".to_string()],
793 pending: Default::default()
794 }
795 );
796
797 // User A automatically reconnects to the room upon disconnection.
798 server.disconnect_client(client_a.peer_id().unwrap());
799 executor.advance_clock(RECEIVE_TIMEOUT);
800 executor.run_until_parked();
801 assert_eq!(
802 room_participants(&room_a, cx_a),
803 RoomParticipants {
804 remote: vec!["user_b".to_string()],
805 pending: Default::default()
806 }
807 );
808 assert_eq!(
809 room_participants(&room_b, cx_b),
810 RoomParticipants {
811 remote: vec!["user_a".to_string()],
812 pending: Default::default()
813 }
814 );
815
816 // When user A disconnects, both client A and B clear their room on the active call.
817 server.forbid_connections();
818 server.disconnect_client(client_a.peer_id().unwrap());
819 executor.advance_clock(RECEIVE_TIMEOUT + RECONNECT_TIMEOUT);
820
821 active_call_a.read_with(cx_a, |call, _| assert!(call.room().is_none()));
822
823 active_call_b.read_with(cx_b, |call, _| assert!(call.room().is_none()));
824 assert_eq!(
825 room_participants(&room_a, cx_a),
826 RoomParticipants {
827 remote: Default::default(),
828 pending: Default::default()
829 }
830 );
831 assert_eq!(
832 room_participants(&room_b, cx_b),
833 RoomParticipants {
834 remote: Default::default(),
835 pending: Default::default()
836 }
837 );
838
839 // Allow user A to reconnect to the server.
840 server.allow_connections();
841 executor.advance_clock(RECONNECT_TIMEOUT);
842
843 // Call user B again from client A.
844 active_call_a
845 .update(cx_a, |call, cx| {
846 call.invite(client_b.user_id().unwrap(), None, cx)
847 })
848 .await
849 .unwrap();
850
851 let room_a = active_call_a.read_with(cx_a, |call, _| call.room().unwrap().clone());
852
853 // User B receives the call and joins the room.
854
855 let mut incoming_call_b = active_call_b.read_with(cx_b, |call, _| call.incoming());
856 incoming_call_b.next().await.unwrap().unwrap();
857 active_call_b
858 .update(cx_b, |call, cx| call.accept_incoming(cx))
859 .await
860 .unwrap();
861
862 let room_b = active_call_b.read_with(cx_b, |call, _| call.room().unwrap().clone());
863 executor.run_until_parked();
864 assert_eq!(
865 room_participants(&room_a, cx_a),
866 RoomParticipants {
867 remote: vec!["user_b".to_string()],
868 pending: Default::default()
869 }
870 );
871 assert_eq!(
872 room_participants(&room_b, cx_b),
873 RoomParticipants {
874 remote: vec!["user_a".to_string()],
875 pending: Default::default()
876 }
877 );
878
879 // User B gets disconnected from the LiveKit server, which causes it
880 // to automatically leave the room.
881 server
882 .test_livekit_server
883 .disconnect_client(client_b.user_id().unwrap().to_string())
884 .await;
885 executor.run_until_parked();
886 active_call_a.update(cx_a, |call, _| assert!(call.room().is_none()));
887 active_call_b.update(cx_b, |call, _| assert!(call.room().is_none()));
888 assert_eq!(
889 room_participants(&room_a, cx_a),
890 RoomParticipants {
891 remote: Default::default(),
892 pending: Default::default()
893 }
894 );
895 assert_eq!(
896 room_participants(&room_b, cx_b),
897 RoomParticipants {
898 remote: Default::default(),
899 pending: Default::default()
900 }
901 );
902}
903
904#[gpui::test(iterations = 10)]
905async fn test_server_restarts(
906 executor: BackgroundExecutor,
907 cx_a: &mut TestAppContext,
908 cx_b: &mut TestAppContext,
909 cx_c: &mut TestAppContext,
910 cx_d: &mut TestAppContext,
911) {
912 let mut server = TestServer::start(executor.clone()).await;
913 let client_a = server.create_client(cx_a, "user_a").await;
914 client_a
915 .fs()
916 .insert_tree("/a", json!({ "a.txt": "a-contents" }))
917 .await;
918
919 // Invite client B to collaborate on a project
920 let (project_a, _) = client_a.build_local_project("/a", cx_a).await;
921
922 let client_b = server.create_client(cx_b, "user_b").await;
923 let client_c = server.create_client(cx_c, "user_c").await;
924 let client_d = server.create_client(cx_d, "user_d").await;
925 server
926 .make_contacts(&mut [
927 (&client_a, cx_a),
928 (&client_b, cx_b),
929 (&client_c, cx_c),
930 (&client_d, cx_d),
931 ])
932 .await;
933
934 let active_call_a = cx_a.read(ActiveCall::global);
935 let active_call_b = cx_b.read(ActiveCall::global);
936 let active_call_c = cx_c.read(ActiveCall::global);
937 let active_call_d = cx_d.read(ActiveCall::global);
938
939 // User A calls users B, C, and D.
940 active_call_a
941 .update(cx_a, |call, cx| {
942 call.invite(client_b.user_id().unwrap(), Some(project_a.clone()), cx)
943 })
944 .await
945 .unwrap();
946 active_call_a
947 .update(cx_a, |call, cx| {
948 call.invite(client_c.user_id().unwrap(), Some(project_a.clone()), cx)
949 })
950 .await
951 .unwrap();
952 active_call_a
953 .update(cx_a, |call, cx| {
954 call.invite(client_d.user_id().unwrap(), Some(project_a.clone()), cx)
955 })
956 .await
957 .unwrap();
958
959 let room_a = active_call_a.read_with(cx_a, |call, _| call.room().unwrap().clone());
960
961 // User B receives the call and joins the room.
962
963 let mut incoming_call_b = active_call_b.read_with(cx_b, |call, _| call.incoming());
964 assert!(incoming_call_b.next().await.unwrap().is_some());
965 active_call_b
966 .update(cx_b, |call, cx| call.accept_incoming(cx))
967 .await
968 .unwrap();
969
970 let room_b = active_call_b.read_with(cx_b, |call, _| call.room().unwrap().clone());
971
972 // User C receives the call and joins the room.
973
974 let mut incoming_call_c = active_call_c.read_with(cx_c, |call, _| call.incoming());
975 assert!(incoming_call_c.next().await.unwrap().is_some());
976 active_call_c
977 .update(cx_c, |call, cx| call.accept_incoming(cx))
978 .await
979 .unwrap();
980
981 let room_c = active_call_c.read_with(cx_c, |call, _| call.room().unwrap().clone());
982
983 // User D receives the call but doesn't join the room yet.
984
985 let mut incoming_call_d = active_call_d.read_with(cx_d, |call, _| call.incoming());
986 assert!(incoming_call_d.next().await.unwrap().is_some());
987
988 executor.run_until_parked();
989 assert_eq!(
990 room_participants(&room_a, cx_a),
991 RoomParticipants {
992 remote: vec!["user_b".to_string(), "user_c".to_string()],
993 pending: vec!["user_d".to_string()]
994 }
995 );
996 assert_eq!(
997 room_participants(&room_b, cx_b),
998 RoomParticipants {
999 remote: vec!["user_a".to_string(), "user_c".to_string()],
1000 pending: vec!["user_d".to_string()]
1001 }
1002 );
1003 assert_eq!(
1004 room_participants(&room_c, cx_c),
1005 RoomParticipants {
1006 remote: vec!["user_a".to_string(), "user_b".to_string()],
1007 pending: vec!["user_d".to_string()]
1008 }
1009 );
1010
1011 // The server is torn down.
1012 server.reset().await;
1013
1014 // Users A and B reconnect to the call. User C has troubles reconnecting, so it leaves the room.
1015 client_c.override_establish_connection(|_, cx| cx.spawn(async |_| future::pending().await));
1016 executor.advance_clock(RECONNECT_TIMEOUT);
1017 assert_eq!(
1018 room_participants(&room_a, cx_a),
1019 RoomParticipants {
1020 remote: vec!["user_b".to_string(), "user_c".to_string()],
1021 pending: vec!["user_d".to_string()]
1022 }
1023 );
1024 assert_eq!(
1025 room_participants(&room_b, cx_b),
1026 RoomParticipants {
1027 remote: vec!["user_a".to_string(), "user_c".to_string()],
1028 pending: vec!["user_d".to_string()]
1029 }
1030 );
1031 assert_eq!(
1032 room_participants(&room_c, cx_c),
1033 RoomParticipants {
1034 remote: vec![],
1035 pending: vec![]
1036 }
1037 );
1038
1039 // User D is notified again of the incoming call and accepts it.
1040 assert!(incoming_call_d.next().await.unwrap().is_some());
1041 active_call_d
1042 .update(cx_d, |call, cx| call.accept_incoming(cx))
1043 .await
1044 .unwrap();
1045 executor.run_until_parked();
1046
1047 let room_d = active_call_d.read_with(cx_d, |call, _| call.room().unwrap().clone());
1048 assert_eq!(
1049 room_participants(&room_a, cx_a),
1050 RoomParticipants {
1051 remote: vec![
1052 "user_b".to_string(),
1053 "user_c".to_string(),
1054 "user_d".to_string(),
1055 ],
1056 pending: vec![]
1057 }
1058 );
1059 assert_eq!(
1060 room_participants(&room_b, cx_b),
1061 RoomParticipants {
1062 remote: vec![
1063 "user_a".to_string(),
1064 "user_c".to_string(),
1065 "user_d".to_string(),
1066 ],
1067 pending: vec![]
1068 }
1069 );
1070 assert_eq!(
1071 room_participants(&room_c, cx_c),
1072 RoomParticipants {
1073 remote: vec![],
1074 pending: vec![]
1075 }
1076 );
1077 assert_eq!(
1078 room_participants(&room_d, cx_d),
1079 RoomParticipants {
1080 remote: vec![
1081 "user_a".to_string(),
1082 "user_b".to_string(),
1083 "user_c".to_string(),
1084 ],
1085 pending: vec![]
1086 }
1087 );
1088
1089 // The server finishes restarting, cleaning up stale connections.
1090 server.start().await.unwrap();
1091 executor.advance_clock(CLEANUP_TIMEOUT);
1092 assert_eq!(
1093 room_participants(&room_a, cx_a),
1094 RoomParticipants {
1095 remote: vec!["user_b".to_string(), "user_d".to_string()],
1096 pending: vec![]
1097 }
1098 );
1099 assert_eq!(
1100 room_participants(&room_b, cx_b),
1101 RoomParticipants {
1102 remote: vec!["user_a".to_string(), "user_d".to_string()],
1103 pending: vec![]
1104 }
1105 );
1106 assert_eq!(
1107 room_participants(&room_c, cx_c),
1108 RoomParticipants {
1109 remote: vec![],
1110 pending: vec![]
1111 }
1112 );
1113 assert_eq!(
1114 room_participants(&room_d, cx_d),
1115 RoomParticipants {
1116 remote: vec!["user_a".to_string(), "user_b".to_string()],
1117 pending: vec![]
1118 }
1119 );
1120
1121 // User D hangs up.
1122 active_call_d
1123 .update(cx_d, |call, cx| call.hang_up(cx))
1124 .await
1125 .unwrap();
1126 executor.run_until_parked();
1127 assert_eq!(
1128 room_participants(&room_a, cx_a),
1129 RoomParticipants {
1130 remote: vec!["user_b".to_string()],
1131 pending: vec![]
1132 }
1133 );
1134 assert_eq!(
1135 room_participants(&room_b, cx_b),
1136 RoomParticipants {
1137 remote: vec!["user_a".to_string()],
1138 pending: vec![]
1139 }
1140 );
1141 assert_eq!(
1142 room_participants(&room_c, cx_c),
1143 RoomParticipants {
1144 remote: vec![],
1145 pending: vec![]
1146 }
1147 );
1148 assert_eq!(
1149 room_participants(&room_d, cx_d),
1150 RoomParticipants {
1151 remote: vec![],
1152 pending: vec![]
1153 }
1154 );
1155
1156 // User B calls user D again.
1157 active_call_b
1158 .update(cx_b, |call, cx| {
1159 call.invite(client_d.user_id().unwrap(), None, cx)
1160 })
1161 .await
1162 .unwrap();
1163
1164 // User D receives the call but doesn't join the room yet.
1165
1166 let mut incoming_call_d = active_call_d.read_with(cx_d, |call, _| call.incoming());
1167 assert!(incoming_call_d.next().await.unwrap().is_some());
1168 executor.run_until_parked();
1169 assert_eq!(
1170 room_participants(&room_a, cx_a),
1171 RoomParticipants {
1172 remote: vec!["user_b".to_string()],
1173 pending: vec!["user_d".to_string()]
1174 }
1175 );
1176 assert_eq!(
1177 room_participants(&room_b, cx_b),
1178 RoomParticipants {
1179 remote: vec!["user_a".to_string()],
1180 pending: vec!["user_d".to_string()]
1181 }
1182 );
1183
1184 // The server is torn down.
1185 server.reset().await;
1186
1187 // Users A and B have troubles reconnecting, so they leave the room.
1188 client_a.override_establish_connection(|_, cx| cx.spawn(async |_| future::pending().await));
1189 client_b.override_establish_connection(|_, cx| cx.spawn(async |_| future::pending().await));
1190 client_c.override_establish_connection(|_, cx| cx.spawn(async |_| future::pending().await));
1191 executor.advance_clock(RECONNECT_TIMEOUT);
1192 assert_eq!(
1193 room_participants(&room_a, cx_a),
1194 RoomParticipants {
1195 remote: vec![],
1196 pending: vec![]
1197 }
1198 );
1199 assert_eq!(
1200 room_participants(&room_b, cx_b),
1201 RoomParticipants {
1202 remote: vec![],
1203 pending: vec![]
1204 }
1205 );
1206
1207 // User D is notified again of the incoming call but doesn't accept it.
1208 assert!(incoming_call_d.next().await.unwrap().is_some());
1209
1210 // The server finishes restarting, cleaning up stale connections and canceling the
1211 // call to user D because the room has become empty.
1212 server.start().await.unwrap();
1213 executor.advance_clock(CLEANUP_TIMEOUT);
1214 assert!(incoming_call_d.next().await.unwrap().is_none());
1215}
1216
1217#[gpui::test(iterations = 10)]
1218async fn test_calls_on_multiple_connections(
1219 executor: BackgroundExecutor,
1220 cx_a: &mut TestAppContext,
1221 cx_b1: &mut TestAppContext,
1222 cx_b2: &mut TestAppContext,
1223) {
1224 let mut server = TestServer::start(executor.clone()).await;
1225 let client_a = server.create_client(cx_a, "user_a").await;
1226 let client_b1 = server.create_client(cx_b1, "user_b").await;
1227 let client_b2 = server.create_client(cx_b2, "user_b").await;
1228 server
1229 .make_contacts(&mut [(&client_a, cx_a), (&client_b1, cx_b1)])
1230 .await;
1231
1232 let active_call_a = cx_a.read(ActiveCall::global);
1233 let active_call_b1 = cx_b1.read(ActiveCall::global);
1234 let active_call_b2 = cx_b2.read(ActiveCall::global);
1235
1236 let mut incoming_call_b1 = active_call_b1.read_with(cx_b1, |call, _| call.incoming());
1237
1238 let mut incoming_call_b2 = active_call_b2.read_with(cx_b2, |call, _| call.incoming());
1239 assert!(incoming_call_b1.next().await.unwrap().is_none());
1240 assert!(incoming_call_b2.next().await.unwrap().is_none());
1241
1242 // Call user B from client A, ensuring both clients for user B ring.
1243 active_call_a
1244 .update(cx_a, |call, cx| {
1245 call.invite(client_b1.user_id().unwrap(), None, cx)
1246 })
1247 .await
1248 .unwrap();
1249 executor.run_until_parked();
1250 assert!(incoming_call_b1.next().await.unwrap().is_some());
1251 assert!(incoming_call_b2.next().await.unwrap().is_some());
1252
1253 // User B declines the call on one of the two connections, causing both connections
1254 // to stop ringing.
1255 active_call_b2.update(cx_b2, |call, cx| call.decline_incoming(cx).unwrap());
1256 executor.run_until_parked();
1257 assert!(incoming_call_b1.next().await.unwrap().is_none());
1258 assert!(incoming_call_b2.next().await.unwrap().is_none());
1259
1260 // Call user B again from client A.
1261 active_call_a
1262 .update(cx_a, |call, cx| {
1263 call.invite(client_b1.user_id().unwrap(), None, cx)
1264 })
1265 .await
1266 .unwrap();
1267 executor.run_until_parked();
1268 assert!(incoming_call_b1.next().await.unwrap().is_some());
1269 assert!(incoming_call_b2.next().await.unwrap().is_some());
1270
1271 // User B accepts the call on one of the two connections, causing both connections
1272 // to stop ringing.
1273 active_call_b2
1274 .update(cx_b2, |call, cx| call.accept_incoming(cx))
1275 .await
1276 .unwrap();
1277 executor.run_until_parked();
1278 assert!(incoming_call_b1.next().await.unwrap().is_none());
1279 assert!(incoming_call_b2.next().await.unwrap().is_none());
1280
1281 // User B disconnects the client that is not on the call. Everything should be fine.
1282 client_b1.disconnect(&cx_b1.to_async());
1283 executor.advance_clock(RECEIVE_TIMEOUT);
1284 client_b1
1285 .connect(false, &cx_b1.to_async())
1286 .await
1287 .into_response()
1288 .unwrap();
1289
1290 // User B hangs up, and user A calls them again.
1291 active_call_b2
1292 .update(cx_b2, |call, cx| call.hang_up(cx))
1293 .await
1294 .unwrap();
1295 executor.run_until_parked();
1296 active_call_a
1297 .update(cx_a, |call, cx| {
1298 call.invite(client_b1.user_id().unwrap(), None, cx)
1299 })
1300 .await
1301 .unwrap();
1302 executor.run_until_parked();
1303 assert!(incoming_call_b1.next().await.unwrap().is_some());
1304 assert!(incoming_call_b2.next().await.unwrap().is_some());
1305
1306 // User A cancels the call, causing both connections to stop ringing.
1307 active_call_a
1308 .update(cx_a, |call, cx| {
1309 call.cancel_invite(client_b1.user_id().unwrap(), cx)
1310 })
1311 .await
1312 .unwrap();
1313 executor.run_until_parked();
1314 assert!(incoming_call_b1.next().await.unwrap().is_none());
1315 assert!(incoming_call_b2.next().await.unwrap().is_none());
1316
1317 // User A calls user B again.
1318 active_call_a
1319 .update(cx_a, |call, cx| {
1320 call.invite(client_b1.user_id().unwrap(), None, cx)
1321 })
1322 .await
1323 .unwrap();
1324 executor.run_until_parked();
1325 assert!(incoming_call_b1.next().await.unwrap().is_some());
1326 assert!(incoming_call_b2.next().await.unwrap().is_some());
1327
1328 // User A hangs up, causing both connections to stop ringing.
1329 active_call_a
1330 .update(cx_a, |call, cx| call.hang_up(cx))
1331 .await
1332 .unwrap();
1333 executor.run_until_parked();
1334 assert!(incoming_call_b1.next().await.unwrap().is_none());
1335 assert!(incoming_call_b2.next().await.unwrap().is_none());
1336
1337 // User A calls user B again.
1338 active_call_a
1339 .update(cx_a, |call, cx| {
1340 call.invite(client_b1.user_id().unwrap(), None, cx)
1341 })
1342 .await
1343 .unwrap();
1344 executor.run_until_parked();
1345 assert!(incoming_call_b1.next().await.unwrap().is_some());
1346 assert!(incoming_call_b2.next().await.unwrap().is_some());
1347
1348 // User A disconnects, causing both connections to stop ringing.
1349 server.forbid_connections();
1350 server.disconnect_client(client_a.peer_id().unwrap());
1351 executor.advance_clock(RECEIVE_TIMEOUT + RECONNECT_TIMEOUT);
1352 assert!(incoming_call_b1.next().await.unwrap().is_none());
1353 assert!(incoming_call_b2.next().await.unwrap().is_none());
1354
1355 // User A reconnects automatically, then calls user B again.
1356 server.allow_connections();
1357 executor.advance_clock(RECONNECT_TIMEOUT);
1358 active_call_a
1359 .update(cx_a, |call, cx| {
1360 call.invite(client_b1.user_id().unwrap(), None, cx)
1361 })
1362 .await
1363 .unwrap();
1364 executor.run_until_parked();
1365 assert!(incoming_call_b1.next().await.unwrap().is_some());
1366 assert!(incoming_call_b2.next().await.unwrap().is_some());
1367
1368 // User B disconnects all clients, causing user A to no longer see a pending call for them.
1369 server.forbid_connections();
1370 server.disconnect_client(client_b1.peer_id().unwrap());
1371 server.disconnect_client(client_b2.peer_id().unwrap());
1372 executor.advance_clock(RECEIVE_TIMEOUT + RECONNECT_TIMEOUT);
1373
1374 active_call_a.read_with(cx_a, |call, _| assert!(call.room().is_none()));
1375}
1376
1377#[gpui::test(iterations = 10)]
1378async fn test_unshare_project(
1379 executor: BackgroundExecutor,
1380 cx_a: &mut TestAppContext,
1381 cx_b: &mut TestAppContext,
1382 cx_c: &mut TestAppContext,
1383) {
1384 let mut server = TestServer::start(executor.clone()).await;
1385 let client_a = server.create_client(cx_a, "user_a").await;
1386 let client_b = server.create_client(cx_b, "user_b").await;
1387 let client_c = server.create_client(cx_c, "user_c").await;
1388 server
1389 .create_room(&mut [(&client_a, cx_a), (&client_b, cx_b), (&client_c, cx_c)])
1390 .await;
1391
1392 let active_call_a = cx_a.read(ActiveCall::global);
1393 let active_call_b = cx_b.read(ActiveCall::global);
1394
1395 client_a
1396 .fs()
1397 .insert_tree(
1398 "/a",
1399 json!({
1400 "a.txt": "a-contents",
1401 "b.txt": "b-contents",
1402 }),
1403 )
1404 .await;
1405
1406 let (project_a, worktree_id) = client_a.build_local_project("/a", cx_a).await;
1407 let project_id = active_call_a
1408 .update(cx_a, |call, cx| call.share_project(project_a.clone(), cx))
1409 .await
1410 .unwrap();
1411
1412 let worktree_a = project_a.read_with(cx_a, |project, cx| project.worktrees(cx).next().unwrap());
1413 let project_b = client_b.join_remote_project(project_id, cx_b).await;
1414 executor.run_until_parked();
1415
1416 assert!(worktree_a.read_with(cx_a, |tree, _| tree.has_update_observer()));
1417
1418 project_b
1419 .update(cx_b, |p, cx| {
1420 p.open_buffer((worktree_id, rel_path("a.txt")), cx)
1421 })
1422 .await
1423 .unwrap();
1424
1425 // When client B leaves the room, the project becomes read-only.
1426 active_call_b
1427 .update(cx_b, |call, cx| call.hang_up(cx))
1428 .await
1429 .unwrap();
1430 executor.run_until_parked();
1431
1432 assert!(project_b.read_with(cx_b, |project, cx| project.is_disconnected(cx)));
1433
1434 // Client C opens the project.
1435 let project_c = client_c.join_remote_project(project_id, cx_c).await;
1436
1437 // When client A unshares the project, client C's project becomes read-only.
1438 project_a
1439 .update(cx_a, |project, cx| project.unshare(cx))
1440 .unwrap();
1441 executor.run_until_parked();
1442
1443 assert!(worktree_a.read_with(cx_a, |tree, _| !tree.has_update_observer()));
1444
1445 assert!(project_c.read_with(cx_c, |project, cx| project.is_disconnected(cx)));
1446
1447 // Client C can open the project again after client A re-shares.
1448 let project_id = active_call_a
1449 .update(cx_a, |call, cx| call.share_project(project_a.clone(), cx))
1450 .await
1451 .unwrap();
1452 let project_c2 = client_c.join_remote_project(project_id, cx_c).await;
1453 executor.run_until_parked();
1454
1455 assert!(worktree_a.read_with(cx_a, |tree, _| tree.has_update_observer()));
1456 project_c2
1457 .update(cx_c, |p, cx| {
1458 p.open_buffer((worktree_id, rel_path("a.txt")), cx)
1459 })
1460 .await
1461 .unwrap();
1462
1463 // When client A (the host) leaves the room, the project gets unshared and guests are notified.
1464 active_call_a
1465 .update(cx_a, |call, cx| call.hang_up(cx))
1466 .await
1467 .unwrap();
1468 executor.run_until_parked();
1469
1470 project_a.read_with(cx_a, |project, _| assert!(!project.is_shared()));
1471
1472 project_c2.read_with(cx_c, |project, cx| {
1473 assert!(project.is_disconnected(cx));
1474 assert!(project.collaborators().is_empty());
1475 });
1476}
1477
1478#[gpui::test(iterations = 10)]
1479async fn test_project_reconnect(
1480 executor: BackgroundExecutor,
1481 cx_a: &mut TestAppContext,
1482 cx_b: &mut TestAppContext,
1483) {
1484 let mut server = TestServer::start(executor.clone()).await;
1485 let client_a = server.create_client(cx_a, "user_a").await;
1486 let client_b = server.create_client(cx_b, "user_b").await;
1487 server
1488 .create_room(&mut [(&client_a, cx_a), (&client_b, cx_b)])
1489 .await;
1490
1491 cx_b.update(editor::init);
1492
1493 client_a
1494 .fs()
1495 .insert_tree(
1496 path!("/root-1"),
1497 json!({
1498 "dir1": {
1499 "a.txt": "a",
1500 "b.txt": "b",
1501 "subdir1": {
1502 "c.txt": "c",
1503 "d.txt": "d",
1504 "e.txt": "e",
1505 }
1506 },
1507 "dir2": {
1508 "v.txt": "v",
1509 },
1510 "dir3": {
1511 "w.txt": "w",
1512 "x.txt": "x",
1513 "y.txt": "y",
1514 },
1515 "dir4": {
1516 "z.txt": "z",
1517 },
1518 }),
1519 )
1520 .await;
1521 client_a
1522 .fs()
1523 .insert_tree(
1524 path!("/root-2"),
1525 json!({
1526 "2.txt": "2",
1527 }),
1528 )
1529 .await;
1530 client_a
1531 .fs()
1532 .insert_tree(
1533 path!("/root-3"),
1534 json!({
1535 "3.txt": "3",
1536 }),
1537 )
1538 .await;
1539
1540 let active_call_a = cx_a.read(ActiveCall::global);
1541 let (project_a1, _) = client_a
1542 .build_local_project(path!("/root-1/dir1"), cx_a)
1543 .await;
1544 let (project_a2, _) = client_a.build_local_project(path!("/root-2"), cx_a).await;
1545 let (project_a3, _) = client_a.build_local_project(path!("/root-3"), cx_a).await;
1546 let worktree_a1 =
1547 project_a1.read_with(cx_a, |project, cx| project.worktrees(cx).next().unwrap());
1548 let project1_id = active_call_a
1549 .update(cx_a, |call, cx| call.share_project(project_a1.clone(), cx))
1550 .await
1551 .unwrap();
1552 let project2_id = active_call_a
1553 .update(cx_a, |call, cx| call.share_project(project_a2.clone(), cx))
1554 .await
1555 .unwrap();
1556 let project3_id = active_call_a
1557 .update(cx_a, |call, cx| call.share_project(project_a3.clone(), cx))
1558 .await
1559 .unwrap();
1560
1561 let project_b1 = client_b.join_remote_project(project1_id, cx_b).await;
1562 let project_b2 = client_b.join_remote_project(project2_id, cx_b).await;
1563 let project_b3 = client_b.join_remote_project(project3_id, cx_b).await;
1564 executor.run_until_parked();
1565
1566 let worktree1_id = worktree_a1.read_with(cx_a, |worktree, _| {
1567 assert!(worktree.has_update_observer());
1568 worktree.id()
1569 });
1570 let (worktree_a2, _) = project_a1
1571 .update(cx_a, |p, cx| {
1572 p.find_or_create_worktree(path!("/root-1/dir2"), true, cx)
1573 })
1574 .await
1575 .unwrap();
1576 executor.run_until_parked();
1577
1578 let worktree2_id = worktree_a2.read_with(cx_a, |tree, _| {
1579 assert!(tree.has_update_observer());
1580 tree.id()
1581 });
1582 executor.run_until_parked();
1583
1584 project_b1.read_with(cx_b, |project, cx| {
1585 assert!(project.worktree_for_id(worktree2_id, cx).is_some())
1586 });
1587
1588 let buffer_a1 = project_a1
1589 .update(cx_a, |p, cx| {
1590 p.open_buffer((worktree1_id, rel_path("a.txt")), cx)
1591 })
1592 .await
1593 .unwrap();
1594 let buffer_b1 = project_b1
1595 .update(cx_b, |p, cx| {
1596 p.open_buffer((worktree1_id, rel_path("a.txt")), cx)
1597 })
1598 .await
1599 .unwrap();
1600
1601 // Drop client A's connection.
1602 server.forbid_connections();
1603 server.disconnect_client(client_a.peer_id().unwrap());
1604 executor.advance_clock(RECEIVE_TIMEOUT);
1605
1606 project_a1.read_with(cx_a, |project, _| {
1607 assert!(project.is_shared());
1608 assert_eq!(project.collaborators().len(), 1);
1609 });
1610
1611 project_b1.read_with(cx_b, |project, cx| {
1612 assert!(!project.is_disconnected(cx));
1613 assert_eq!(project.collaborators().len(), 1);
1614 });
1615
1616 worktree_a1.read_with(cx_a, |tree, _| assert!(tree.has_update_observer()));
1617
1618 // While client A is disconnected, add and remove files from client A's project.
1619 client_a
1620 .fs()
1621 .insert_tree(
1622 path!("/root-1/dir1/subdir2"),
1623 json!({
1624 "f.txt": "f-contents",
1625 "g.txt": "g-contents",
1626 "h.txt": "h-contents",
1627 "i.txt": "i-contents",
1628 }),
1629 )
1630 .await;
1631 client_a
1632 .fs()
1633 .remove_dir(
1634 path!("/root-1/dir1/subdir1").as_ref(),
1635 RemoveOptions {
1636 recursive: true,
1637 ..Default::default()
1638 },
1639 )
1640 .await
1641 .unwrap();
1642
1643 // While client A is disconnected, add and remove worktrees from client A's project.
1644 project_a1.update(cx_a, |project, cx| {
1645 project.remove_worktree(worktree2_id, cx)
1646 });
1647 let (worktree_a3, _) = project_a1
1648 .update(cx_a, |p, cx| {
1649 p.find_or_create_worktree(path!("/root-1/dir3"), true, cx)
1650 })
1651 .await
1652 .unwrap();
1653 worktree_a3
1654 .read_with(cx_a, |tree, _| tree.as_local().unwrap().scan_complete())
1655 .await;
1656
1657 let worktree3_id = worktree_a3.read_with(cx_a, |tree, _| {
1658 assert!(!tree.has_update_observer());
1659 tree.id()
1660 });
1661 executor.run_until_parked();
1662
1663 // While client A is disconnected, close project 2
1664 cx_a.update(|_| drop(project_a2));
1665
1666 // While client A is disconnected, mutate a buffer on both the host and the guest.
1667 buffer_a1.update(cx_a, |buf, cx| buf.edit([(0..0, "W")], None, cx));
1668 buffer_b1.update(cx_b, |buf, cx| buf.edit([(1..1, "Z")], None, cx));
1669 executor.run_until_parked();
1670
1671 // Client A reconnects. Their project is re-shared, and client B re-joins it.
1672 server.allow_connections();
1673 client_a
1674 .connect(false, &cx_a.to_async())
1675 .await
1676 .into_response()
1677 .unwrap();
1678 executor.run_until_parked();
1679
1680 project_a1.read_with(cx_a, |project, cx| {
1681 assert!(project.is_shared());
1682 assert!(worktree_a1.read(cx).has_update_observer());
1683 assert_eq!(
1684 worktree_a1.read(cx).snapshot().paths().collect::<Vec<_>>(),
1685 vec![
1686 rel_path("a.txt"),
1687 rel_path("b.txt"),
1688 rel_path("subdir2"),
1689 rel_path("subdir2/f.txt"),
1690 rel_path("subdir2/g.txt"),
1691 rel_path("subdir2/h.txt"),
1692 rel_path("subdir2/i.txt")
1693 ]
1694 );
1695 assert!(worktree_a3.read(cx).has_update_observer());
1696 assert_eq!(
1697 worktree_a3.read(cx).snapshot().paths().collect::<Vec<_>>(),
1698 vec![rel_path("w.txt"), rel_path("x.txt"), rel_path("y.txt")]
1699 );
1700 });
1701
1702 project_b1.read_with(cx_b, |project, cx| {
1703 assert!(!project.is_disconnected(cx));
1704 assert_eq!(
1705 project
1706 .worktree_for_id(worktree1_id, cx)
1707 .unwrap()
1708 .read(cx)
1709 .snapshot()
1710 .paths()
1711 .collect::<Vec<_>>(),
1712 vec![
1713 rel_path("a.txt"),
1714 rel_path("b.txt"),
1715 rel_path("subdir2"),
1716 rel_path("subdir2/f.txt"),
1717 rel_path("subdir2/g.txt"),
1718 rel_path("subdir2/h.txt"),
1719 rel_path("subdir2/i.txt")
1720 ]
1721 );
1722 assert!(project.worktree_for_id(worktree2_id, cx).is_none());
1723 assert_eq!(
1724 project
1725 .worktree_for_id(worktree3_id, cx)
1726 .unwrap()
1727 .read(cx)
1728 .snapshot()
1729 .paths()
1730 .collect::<Vec<_>>(),
1731 vec![rel_path("w.txt"), rel_path("x.txt"), rel_path("y.txt")]
1732 );
1733 });
1734
1735 project_b2.read_with(cx_b, |project, cx| assert!(project.is_disconnected(cx)));
1736
1737 project_b3.read_with(cx_b, |project, cx| assert!(!project.is_disconnected(cx)));
1738
1739 buffer_a1.read_with(cx_a, |buffer, _| assert_eq!(buffer.text(), "WaZ"));
1740
1741 buffer_b1.read_with(cx_b, |buffer, _| assert_eq!(buffer.text(), "WaZ"));
1742
1743 // Drop client B's connection.
1744 server.forbid_connections();
1745 server.disconnect_client(client_b.peer_id().unwrap());
1746 executor.advance_clock(RECEIVE_TIMEOUT);
1747
1748 // While client B is disconnected, add and remove files from client A's project
1749 client_a
1750 .fs()
1751 .insert_file(path!("/root-1/dir1/subdir2/j.txt"), "j-contents".into())
1752 .await;
1753 client_a
1754 .fs()
1755 .remove_file(
1756 path!("/root-1/dir1/subdir2/i.txt").as_ref(),
1757 Default::default(),
1758 )
1759 .await
1760 .unwrap();
1761
1762 // While client B is disconnected, add and remove worktrees from client A's project.
1763 let (worktree_a4, _) = project_a1
1764 .update(cx_a, |p, cx| {
1765 p.find_or_create_worktree(path!("/root-1/dir4"), true, cx)
1766 })
1767 .await
1768 .unwrap();
1769 executor.run_until_parked();
1770
1771 let worktree4_id = worktree_a4.read_with(cx_a, |tree, _| {
1772 assert!(tree.has_update_observer());
1773 tree.id()
1774 });
1775 project_a1.update(cx_a, |project, cx| {
1776 project.remove_worktree(worktree3_id, cx)
1777 });
1778 executor.run_until_parked();
1779
1780 // While client B is disconnected, mutate a buffer on both the host and the guest.
1781 buffer_a1.update(cx_a, |buf, cx| buf.edit([(1..1, "X")], None, cx));
1782 buffer_b1.update(cx_b, |buf, cx| buf.edit([(2..2, "Y")], None, cx));
1783 executor.run_until_parked();
1784
1785 // While disconnected, close project 3
1786 cx_a.update(|_| drop(project_a3));
1787 executor.run_until_parked();
1788
1789 // Client B reconnects. They re-join the room and the remaining shared project.
1790 server.allow_connections();
1791 client_b
1792 .connect(false, &cx_b.to_async())
1793 .await
1794 .into_response()
1795 .unwrap();
1796 executor.run_until_parked();
1797
1798 project_b1.read_with(cx_b, |project, cx| {
1799 assert!(!project.is_disconnected(cx));
1800 assert_eq!(
1801 project
1802 .worktree_for_id(worktree1_id, cx)
1803 .unwrap()
1804 .read(cx)
1805 .snapshot()
1806 .paths()
1807 .collect::<Vec<_>>(),
1808 vec![
1809 rel_path("a.txt"),
1810 rel_path("b.txt"),
1811 rel_path("subdir2"),
1812 rel_path("subdir2/f.txt"),
1813 rel_path("subdir2/g.txt"),
1814 rel_path("subdir2/h.txt"),
1815 rel_path("subdir2/j.txt")
1816 ]
1817 );
1818 assert!(project.worktree_for_id(worktree2_id, cx).is_none());
1819 assert_eq!(
1820 project
1821 .worktree_for_id(worktree4_id, cx)
1822 .unwrap()
1823 .read(cx)
1824 .snapshot()
1825 .paths()
1826 .map(|p| p.as_unix_str())
1827 .collect::<Vec<_>>(),
1828 vec!["z.txt"]
1829 );
1830 });
1831
1832 project_b3.read_with(cx_b, |project, cx| assert!(project.is_disconnected(cx)));
1833
1834 buffer_a1.read_with(cx_a, |buffer, _| assert_eq!(buffer.text(), "WXaYZ"));
1835
1836 buffer_b1.read_with(cx_b, |buffer, _| assert_eq!(buffer.text(), "WXaYZ"));
1837}
1838
1839#[gpui::test(iterations = 10)]
1840async fn test_active_call_events(
1841 executor: BackgroundExecutor,
1842 cx_a: &mut TestAppContext,
1843 cx_b: &mut TestAppContext,
1844) {
1845 let mut server = TestServer::start(executor.clone()).await;
1846 let client_a = server.create_client(cx_a, "user_a").await;
1847 let client_b = server.create_client(cx_b, "user_b").await;
1848 client_a.fs().insert_tree("/a", json!({})).await;
1849 client_b.fs().insert_tree("/b", json!({})).await;
1850
1851 let (project_a, _) = client_a.build_local_project("/a", cx_a).await;
1852 let (project_b, _) = client_b.build_local_project("/b", cx_b).await;
1853
1854 server
1855 .create_room(&mut [(&client_a, cx_a), (&client_b, cx_b)])
1856 .await;
1857 executor.run_until_parked();
1858
1859 let active_call_a = cx_a.read(ActiveCall::global);
1860 let active_call_b = cx_b.read(ActiveCall::global);
1861
1862 let events_a = active_call_events(cx_a);
1863 let events_b = active_call_events(cx_b);
1864
1865 let project_a_id = active_call_a
1866 .update(cx_a, |call, cx| call.share_project(project_a.clone(), cx))
1867 .await
1868 .unwrap();
1869 executor.run_until_parked();
1870 assert_eq!(mem::take(&mut *events_a.borrow_mut()), vec![]);
1871 assert_eq!(
1872 mem::take(&mut *events_b.borrow_mut()),
1873 vec![room::Event::RemoteProjectShared {
1874 owner: Arc::new(User {
1875 id: client_a.user_id().unwrap(),
1876 github_login: "user_a".into(),
1877 avatar_uri: "avatar_a".into(),
1878 name: None,
1879 }),
1880 project_id: project_a_id,
1881 worktree_root_names: vec!["a".to_string()],
1882 }]
1883 );
1884
1885 let project_b_id = active_call_b
1886 .update(cx_b, |call, cx| call.share_project(project_b.clone(), cx))
1887 .await
1888 .unwrap();
1889 executor.run_until_parked();
1890 assert_eq!(
1891 mem::take(&mut *events_a.borrow_mut()),
1892 vec![room::Event::RemoteProjectShared {
1893 owner: Arc::new(User {
1894 id: client_b.user_id().unwrap(),
1895 github_login: "user_b".into(),
1896 avatar_uri: "avatar_b".into(),
1897 name: None,
1898 }),
1899 project_id: project_b_id,
1900 worktree_root_names: vec!["b".to_string()]
1901 }]
1902 );
1903 assert_eq!(mem::take(&mut *events_b.borrow_mut()), vec![]);
1904
1905 // Sharing a project twice is idempotent.
1906 let project_b_id_2 = active_call_b
1907 .update(cx_b, |call, cx| call.share_project(project_b.clone(), cx))
1908 .await
1909 .unwrap();
1910 assert_eq!(project_b_id_2, project_b_id);
1911 executor.run_until_parked();
1912 assert_eq!(mem::take(&mut *events_a.borrow_mut()), vec![]);
1913 assert_eq!(mem::take(&mut *events_b.borrow_mut()), vec![]);
1914
1915 // Unsharing a project should dispatch the RemoteProjectUnshared event.
1916 active_call_a
1917 .update(cx_a, |call, cx| call.hang_up(cx))
1918 .await
1919 .unwrap();
1920 executor.run_until_parked();
1921
1922 assert_eq!(
1923 mem::take(&mut *events_a.borrow_mut()),
1924 vec![room::Event::RoomLeft { channel_id: None }]
1925 );
1926 assert_eq!(
1927 mem::take(&mut *events_b.borrow_mut()),
1928 vec![room::Event::RemoteProjectUnshared {
1929 project_id: project_a_id,
1930 }]
1931 );
1932}
1933
1934fn active_call_events(cx: &mut TestAppContext) -> Rc<RefCell<Vec<room::Event>>> {
1935 let events = Rc::new(RefCell::new(Vec::new()));
1936 let active_call = cx.read(ActiveCall::global);
1937 cx.update({
1938 let events = events.clone();
1939 |cx| {
1940 cx.subscribe(&active_call, move |_, event, _| {
1941 events.borrow_mut().push(event.clone())
1942 })
1943 .detach()
1944 }
1945 });
1946 events
1947}
1948
1949#[gpui::test]
1950async fn test_mute_deafen(
1951 executor: BackgroundExecutor,
1952 cx_a: &mut TestAppContext,
1953 cx_b: &mut TestAppContext,
1954 cx_c: &mut TestAppContext,
1955) {
1956 let mut server = TestServer::start(executor.clone()).await;
1957 let client_a = server.create_client(cx_a, "user_a").await;
1958 let client_b = server.create_client(cx_b, "user_b").await;
1959 let client_c = server.create_client(cx_c, "user_c").await;
1960
1961 server
1962 .make_contacts(&mut [(&client_a, cx_a), (&client_b, cx_b), (&client_c, cx_c)])
1963 .await;
1964
1965 let active_call_a = cx_a.read(ActiveCall::global);
1966 let active_call_b = cx_b.read(ActiveCall::global);
1967 let active_call_c = cx_c.read(ActiveCall::global);
1968
1969 // User A calls user B, B answers.
1970 active_call_a
1971 .update(cx_a, |call, cx| {
1972 call.invite(client_b.user_id().unwrap(), None, cx)
1973 })
1974 .await
1975 .unwrap();
1976 executor.run_until_parked();
1977 active_call_b
1978 .update(cx_b, |call, cx| call.accept_incoming(cx))
1979 .await
1980 .unwrap();
1981 executor.run_until_parked();
1982
1983 let room_a = active_call_a.read_with(cx_a, |call, _| call.room().unwrap().clone());
1984 let room_b = active_call_b.read_with(cx_b, |call, _| call.room().unwrap().clone());
1985
1986 room_a.read_with(cx_a, |room, _| assert!(!room.is_muted()));
1987 room_b.read_with(cx_b, |room, _| assert!(!room.is_muted()));
1988
1989 // Users A and B are both unmuted.
1990 assert_eq!(
1991 participant_audio_state(&room_a, cx_a),
1992 &[ParticipantAudioState {
1993 user_id: client_b.user_id().unwrap(),
1994 is_muted: false,
1995 audio_tracks_playing: vec![true],
1996 }]
1997 );
1998 assert_eq!(
1999 participant_audio_state(&room_b, cx_b),
2000 &[ParticipantAudioState {
2001 user_id: client_a.user_id().unwrap(),
2002 is_muted: false,
2003 audio_tracks_playing: vec![true],
2004 }]
2005 );
2006
2007 // User A mutes
2008 room_a.update(cx_a, |room, cx| room.toggle_mute(cx));
2009 executor.run_until_parked();
2010
2011 // User A hears user B, but B doesn't hear A.
2012 room_a.read_with(cx_a, |room, _| assert!(room.is_muted()));
2013 room_b.read_with(cx_b, |room, _| assert!(!room.is_muted()));
2014 assert_eq!(
2015 participant_audio_state(&room_a, cx_a),
2016 &[ParticipantAudioState {
2017 user_id: client_b.user_id().unwrap(),
2018 is_muted: false,
2019 audio_tracks_playing: vec![true],
2020 }]
2021 );
2022 assert_eq!(
2023 participant_audio_state(&room_b, cx_b),
2024 &[ParticipantAudioState {
2025 user_id: client_a.user_id().unwrap(),
2026 is_muted: true,
2027 audio_tracks_playing: vec![true],
2028 }]
2029 );
2030
2031 // User A deafens
2032 room_a.update(cx_a, |room, cx| room.toggle_deafen(cx));
2033 executor.run_until_parked();
2034
2035 // User A does not hear user B.
2036 room_a.read_with(cx_a, |room, _| assert!(room.is_muted()));
2037 room_b.read_with(cx_b, |room, _| assert!(!room.is_muted()));
2038 assert_eq!(
2039 participant_audio_state(&room_a, cx_a),
2040 &[ParticipantAudioState {
2041 user_id: client_b.user_id().unwrap(),
2042 is_muted: false,
2043 audio_tracks_playing: vec![false],
2044 }]
2045 );
2046 assert_eq!(
2047 participant_audio_state(&room_b, cx_b),
2048 &[ParticipantAudioState {
2049 user_id: client_a.user_id().unwrap(),
2050 is_muted: true,
2051 audio_tracks_playing: vec![true],
2052 }]
2053 );
2054
2055 // User B calls user C, C joins.
2056 active_call_b
2057 .update(cx_b, |call, cx| {
2058 call.invite(client_c.user_id().unwrap(), None, cx)
2059 })
2060 .await
2061 .unwrap();
2062 executor.run_until_parked();
2063 active_call_c
2064 .update(cx_c, |call, cx| call.accept_incoming(cx))
2065 .await
2066 .unwrap();
2067 executor.run_until_parked();
2068
2069 // User A does not hear users B or C.
2070 assert_eq!(
2071 participant_audio_state(&room_a, cx_a),
2072 &[
2073 ParticipantAudioState {
2074 user_id: client_b.user_id().unwrap(),
2075 is_muted: false,
2076 audio_tracks_playing: vec![false],
2077 },
2078 ParticipantAudioState {
2079 user_id: client_c.user_id().unwrap(),
2080 is_muted: false,
2081 audio_tracks_playing: vec![false],
2082 }
2083 ]
2084 );
2085 assert_eq!(
2086 participant_audio_state(&room_b, cx_b),
2087 &[
2088 ParticipantAudioState {
2089 user_id: client_a.user_id().unwrap(),
2090 is_muted: true,
2091 audio_tracks_playing: vec![true],
2092 },
2093 ParticipantAudioState {
2094 user_id: client_c.user_id().unwrap(),
2095 is_muted: false,
2096 audio_tracks_playing: vec![true],
2097 }
2098 ]
2099 );
2100
2101 #[derive(PartialEq, Eq, Debug)]
2102 struct ParticipantAudioState {
2103 user_id: u64,
2104 is_muted: bool,
2105 audio_tracks_playing: Vec<bool>,
2106 }
2107
2108 fn participant_audio_state(
2109 room: &Entity<Room>,
2110 cx: &TestAppContext,
2111 ) -> Vec<ParticipantAudioState> {
2112 room.read_with(cx, |room, _| {
2113 room.remote_participants()
2114 .iter()
2115 .map(|(user_id, participant)| ParticipantAudioState {
2116 user_id: *user_id,
2117 is_muted: participant.muted,
2118 audio_tracks_playing: participant
2119 .audio_tracks
2120 .values()
2121 .map(|(track, _)| track.enabled())
2122 .collect(),
2123 })
2124 .collect::<Vec<_>>()
2125 })
2126 }
2127}
2128
2129#[gpui::test(iterations = 10)]
2130async fn test_room_location(
2131 executor: BackgroundExecutor,
2132 cx_a: &mut TestAppContext,
2133 cx_b: &mut TestAppContext,
2134) {
2135 let mut server = TestServer::start(executor.clone()).await;
2136 let client_a = server.create_client(cx_a, "user_a").await;
2137 let client_b = server.create_client(cx_b, "user_b").await;
2138 client_a.fs().insert_tree("/a", json!({})).await;
2139 client_b.fs().insert_tree("/b", json!({})).await;
2140
2141 let active_call_a = cx_a.read(ActiveCall::global);
2142 let active_call_b = cx_b.read(ActiveCall::global);
2143
2144 let a_notified = Rc::new(Cell::new(false));
2145 cx_a.update({
2146 let notified = a_notified.clone();
2147 |cx| {
2148 cx.observe(&active_call_a, move |_, _| notified.set(true))
2149 .detach()
2150 }
2151 });
2152
2153 let b_notified = Rc::new(Cell::new(false));
2154 cx_b.update({
2155 let b_notified = b_notified.clone();
2156 |cx| {
2157 cx.observe(&active_call_b, move |_, _| b_notified.set(true))
2158 .detach()
2159 }
2160 });
2161
2162 let (project_a, _) = client_a.build_local_project("/a", cx_a).await;
2163 active_call_a
2164 .update(cx_a, |call, cx| call.set_location(Some(&project_a), cx))
2165 .await
2166 .unwrap();
2167 let (project_b, _) = client_b.build_local_project("/b", cx_b).await;
2168
2169 server
2170 .create_room(&mut [(&client_a, cx_a), (&client_b, cx_b)])
2171 .await;
2172
2173 let room_a = active_call_a.read_with(cx_a, |call, _| call.room().unwrap().clone());
2174
2175 let room_b = active_call_b.read_with(cx_b, |call, _| call.room().unwrap().clone());
2176 executor.run_until_parked();
2177 assert!(a_notified.take());
2178 assert_eq!(
2179 participant_locations(&room_a, cx_a),
2180 vec![("user_b".to_string(), ParticipantLocation::External)]
2181 );
2182 assert!(b_notified.take());
2183 assert_eq!(
2184 participant_locations(&room_b, cx_b),
2185 vec![("user_a".to_string(), ParticipantLocation::UnsharedProject)]
2186 );
2187
2188 let project_a_id = active_call_a
2189 .update(cx_a, |call, cx| call.share_project(project_a.clone(), cx))
2190 .await
2191 .unwrap();
2192 executor.run_until_parked();
2193 assert!(a_notified.take());
2194 assert_eq!(
2195 participant_locations(&room_a, cx_a),
2196 vec![("user_b".to_string(), ParticipantLocation::External)]
2197 );
2198 assert!(b_notified.take());
2199 assert_eq!(
2200 participant_locations(&room_b, cx_b),
2201 vec![(
2202 "user_a".to_string(),
2203 ParticipantLocation::SharedProject {
2204 project_id: project_a_id
2205 }
2206 )]
2207 );
2208
2209 let project_b_id = active_call_b
2210 .update(cx_b, |call, cx| call.share_project(project_b.clone(), cx))
2211 .await
2212 .unwrap();
2213 executor.run_until_parked();
2214 assert!(a_notified.take());
2215 assert_eq!(
2216 participant_locations(&room_a, cx_a),
2217 vec![("user_b".to_string(), ParticipantLocation::External)]
2218 );
2219 assert!(b_notified.take());
2220 assert_eq!(
2221 participant_locations(&room_b, cx_b),
2222 vec![(
2223 "user_a".to_string(),
2224 ParticipantLocation::SharedProject {
2225 project_id: project_a_id
2226 }
2227 )]
2228 );
2229
2230 active_call_b
2231 .update(cx_b, |call, cx| call.set_location(Some(&project_b), cx))
2232 .await
2233 .unwrap();
2234 executor.run_until_parked();
2235 assert!(a_notified.take());
2236 assert_eq!(
2237 participant_locations(&room_a, cx_a),
2238 vec![(
2239 "user_b".to_string(),
2240 ParticipantLocation::SharedProject {
2241 project_id: project_b_id
2242 }
2243 )]
2244 );
2245 assert!(b_notified.take());
2246 assert_eq!(
2247 participant_locations(&room_b, cx_b),
2248 vec![(
2249 "user_a".to_string(),
2250 ParticipantLocation::SharedProject {
2251 project_id: project_a_id
2252 }
2253 )]
2254 );
2255
2256 active_call_b
2257 .update(cx_b, |call, cx| call.set_location(None, cx))
2258 .await
2259 .unwrap();
2260 executor.run_until_parked();
2261 assert!(a_notified.take());
2262 assert_eq!(
2263 participant_locations(&room_a, cx_a),
2264 vec![("user_b".to_string(), ParticipantLocation::External)]
2265 );
2266 assert!(b_notified.take());
2267 assert_eq!(
2268 participant_locations(&room_b, cx_b),
2269 vec![(
2270 "user_a".to_string(),
2271 ParticipantLocation::SharedProject {
2272 project_id: project_a_id
2273 }
2274 )]
2275 );
2276
2277 fn participant_locations(
2278 room: &Entity<Room>,
2279 cx: &TestAppContext,
2280 ) -> Vec<(String, ParticipantLocation)> {
2281 room.read_with(cx, |room, _| {
2282 room.remote_participants()
2283 .values()
2284 .map(|participant| {
2285 (
2286 participant.user.github_login.to_string(),
2287 participant.location,
2288 )
2289 })
2290 .collect()
2291 })
2292 }
2293}
2294
2295#[gpui::test(iterations = 10)]
2296async fn test_propagate_saves_and_fs_changes(
2297 executor: BackgroundExecutor,
2298 cx_a: &mut TestAppContext,
2299 cx_b: &mut TestAppContext,
2300 cx_c: &mut TestAppContext,
2301) {
2302 let mut server = TestServer::start(executor.clone()).await;
2303 let client_a = server.create_client(cx_a, "user_a").await;
2304 let client_b = server.create_client(cx_b, "user_b").await;
2305 let client_c = server.create_client(cx_c, "user_c").await;
2306
2307 server
2308 .create_room(&mut [(&client_a, cx_a), (&client_b, cx_b), (&client_c, cx_c)])
2309 .await;
2310 let active_call_a = cx_a.read(ActiveCall::global);
2311
2312 let rust = Arc::new(Language::new(
2313 LanguageConfig {
2314 name: "Rust".into(),
2315 matcher: LanguageMatcher {
2316 path_suffixes: vec!["rs".to_string()],
2317 ..Default::default()
2318 },
2319 ..Default::default()
2320 },
2321 Some(tree_sitter_rust::LANGUAGE.into()),
2322 ));
2323 let javascript = Arc::new(Language::new(
2324 LanguageConfig {
2325 name: "JavaScript".into(),
2326 matcher: LanguageMatcher {
2327 path_suffixes: vec!["js".to_string()],
2328 ..Default::default()
2329 },
2330 ..Default::default()
2331 },
2332 Some(tree_sitter_rust::LANGUAGE.into()),
2333 ));
2334 for client in [&client_a, &client_b, &client_c] {
2335 client.language_registry().add(rust.clone());
2336 client.language_registry().add(javascript.clone());
2337 }
2338
2339 client_a
2340 .fs()
2341 .insert_tree(
2342 path!("/a"),
2343 json!({
2344 "file1.rs": "",
2345 "file2": ""
2346 }),
2347 )
2348 .await;
2349 let (project_a, worktree_id) = client_a.build_local_project(path!("/a"), cx_a).await;
2350
2351 let worktree_a = project_a.read_with(cx_a, |p, cx| p.worktrees(cx).next().unwrap());
2352 let project_id = active_call_a
2353 .update(cx_a, |call, cx| call.share_project(project_a.clone(), cx))
2354 .await
2355 .unwrap();
2356
2357 // Join that worktree as clients B and C.
2358 let project_b = client_b.join_remote_project(project_id, cx_b).await;
2359 let project_c = client_c.join_remote_project(project_id, cx_c).await;
2360
2361 let worktree_b = project_b.read_with(cx_b, |p, cx| p.worktrees(cx).next().unwrap());
2362
2363 let worktree_c = project_c.read_with(cx_c, |p, cx| p.worktrees(cx).next().unwrap());
2364
2365 // Open and edit a buffer as both guests B and C.
2366 let buffer_b = project_b
2367 .update(cx_b, |p, cx| {
2368 p.open_buffer((worktree_id, rel_path("file1.rs")), cx)
2369 })
2370 .await
2371 .unwrap();
2372 let buffer_c = project_c
2373 .update(cx_c, |p, cx| {
2374 p.open_buffer((worktree_id, rel_path("file1.rs")), cx)
2375 })
2376 .await
2377 .unwrap();
2378
2379 buffer_b.read_with(cx_b, |buffer, _| {
2380 assert_eq!(buffer.language().unwrap().name(), "Rust");
2381 });
2382
2383 buffer_c.read_with(cx_c, |buffer, _| {
2384 assert_eq!(buffer.language().unwrap().name(), "Rust");
2385 });
2386 buffer_b.update(cx_b, |buf, cx| buf.edit([(0..0, "i-am-b, ")], None, cx));
2387 buffer_c.update(cx_c, |buf, cx| buf.edit([(0..0, "i-am-c, ")], None, cx));
2388
2389 // Open and edit that buffer as the host.
2390 let buffer_a = project_a
2391 .update(cx_a, |p, cx| {
2392 p.open_buffer((worktree_id, rel_path("file1.rs")), cx)
2393 })
2394 .await
2395 .unwrap();
2396
2397 executor.run_until_parked();
2398
2399 buffer_a.read_with(cx_a, |buf, _| assert_eq!(buf.text(), "i-am-c, i-am-b, "));
2400 buffer_a.update(cx_a, |buf, cx| {
2401 buf.edit([(buf.len()..buf.len(), "i-am-a")], None, cx)
2402 });
2403
2404 executor.run_until_parked();
2405
2406 buffer_a.read_with(cx_a, |buf, _| {
2407 assert_eq!(buf.text(), "i-am-c, i-am-b, i-am-a");
2408 });
2409
2410 buffer_b.read_with(cx_b, |buf, _| {
2411 assert_eq!(buf.text(), "i-am-c, i-am-b, i-am-a");
2412 });
2413
2414 buffer_c.read_with(cx_c, |buf, _| {
2415 assert_eq!(buf.text(), "i-am-c, i-am-b, i-am-a");
2416 });
2417
2418 // Edit the buffer as the host and concurrently save as guest B.
2419 let save_b = project_b.update(cx_b, |project, cx| {
2420 project.save_buffer(buffer_b.clone(), cx)
2421 });
2422 buffer_a.update(cx_a, |buf, cx| buf.edit([(0..0, "hi-a, ")], None, cx));
2423 save_b.await.unwrap();
2424 assert_eq!(
2425 client_a.fs().load("/a/file1.rs".as_ref()).await.unwrap(),
2426 "hi-a, i-am-c, i-am-b, i-am-a"
2427 );
2428
2429 executor.run_until_parked();
2430
2431 buffer_a.read_with(cx_a, |buf, _| assert!(!buf.is_dirty()));
2432
2433 buffer_b.read_with(cx_b, |buf, _| assert!(!buf.is_dirty()));
2434
2435 buffer_c.read_with(cx_c, |buf, _| assert!(!buf.is_dirty()));
2436
2437 // Make changes on host's file system, see those changes on guest worktrees.
2438 client_a
2439 .fs()
2440 .rename(
2441 path!("/a/file1.rs").as_ref(),
2442 path!("/a/file1.js").as_ref(),
2443 Default::default(),
2444 )
2445 .await
2446 .unwrap();
2447 client_a
2448 .fs()
2449 .rename(
2450 path!("/a/file2").as_ref(),
2451 path!("/a/file3").as_ref(),
2452 Default::default(),
2453 )
2454 .await
2455 .unwrap();
2456 client_a
2457 .fs()
2458 .insert_file(path!("/a/file4"), "4".into())
2459 .await;
2460 executor.run_until_parked();
2461
2462 worktree_a.read_with(cx_a, |tree, _| {
2463 assert_eq!(
2464 tree.paths().collect::<Vec<_>>(),
2465 [rel_path("file1.js"), rel_path("file3"), rel_path("file4")]
2466 )
2467 });
2468
2469 worktree_b.read_with(cx_b, |tree, _| {
2470 assert_eq!(
2471 tree.paths().collect::<Vec<_>>(),
2472 [rel_path("file1.js"), rel_path("file3"), rel_path("file4")]
2473 )
2474 });
2475
2476 worktree_c.read_with(cx_c, |tree, _| {
2477 assert_eq!(
2478 tree.paths().collect::<Vec<_>>(),
2479 [rel_path("file1.js"), rel_path("file3"), rel_path("file4")]
2480 )
2481 });
2482
2483 // Ensure buffer files are updated as well.
2484
2485 buffer_a.read_with(cx_a, |buffer, _| {
2486 assert_eq!(buffer.file().unwrap().path().as_ref(), rel_path("file1.js"));
2487 assert_eq!(buffer.language().unwrap().name(), "JavaScript");
2488 });
2489
2490 buffer_b.read_with(cx_b, |buffer, _| {
2491 assert_eq!(buffer.file().unwrap().path().as_ref(), rel_path("file1.js"));
2492 assert_eq!(buffer.language().unwrap().name(), "JavaScript");
2493 });
2494
2495 buffer_c.read_with(cx_c, |buffer, _| {
2496 assert_eq!(buffer.file().unwrap().path().as_ref(), rel_path("file1.js"));
2497 assert_eq!(buffer.language().unwrap().name(), "JavaScript");
2498 });
2499
2500 let new_buffer_a = project_a
2501 .update(cx_a, |p, cx| p.create_buffer(None, false, cx))
2502 .await
2503 .unwrap();
2504
2505 let new_buffer_id = new_buffer_a.read_with(cx_a, |buffer, _| buffer.remote_id());
2506 let new_buffer_b = project_b
2507 .update(cx_b, |p, cx| p.open_buffer_by_id(new_buffer_id, cx))
2508 .await
2509 .unwrap();
2510
2511 new_buffer_b.read_with(cx_b, |buffer, _| {
2512 assert!(buffer.file().is_none());
2513 });
2514
2515 new_buffer_a.update(cx_a, |buffer, cx| {
2516 buffer.edit([(0..0, "ok")], None, cx);
2517 });
2518 project_a
2519 .update(cx_a, |project, cx| {
2520 let path = ProjectPath {
2521 path: rel_path("file3.rs").into(),
2522 worktree_id: worktree_a.read(cx).id(),
2523 };
2524
2525 project.save_buffer_as(new_buffer_a.clone(), path, cx)
2526 })
2527 .await
2528 .unwrap();
2529
2530 executor.run_until_parked();
2531
2532 new_buffer_b.read_with(cx_b, |buffer_b, _| {
2533 assert_eq!(
2534 buffer_b.file().unwrap().path().as_ref(),
2535 rel_path("file3.rs")
2536 );
2537
2538 new_buffer_a.read_with(cx_a, |buffer_a, _| {
2539 assert_eq!(buffer_b.saved_mtime(), buffer_a.saved_mtime());
2540 assert_eq!(buffer_b.saved_version(), buffer_a.saved_version());
2541 });
2542 });
2543}
2544
2545#[gpui::test(iterations = 10)]
2546async fn test_git_diff_base_change(
2547 executor: BackgroundExecutor,
2548 cx_a: &mut TestAppContext,
2549 cx_b: &mut TestAppContext,
2550) {
2551 let mut server = TestServer::start(executor.clone()).await;
2552 let client_a = server.create_client(cx_a, "user_a").await;
2553 let client_b = server.create_client(cx_b, "user_b").await;
2554 server
2555 .create_room(&mut [(&client_a, cx_a), (&client_b, cx_b)])
2556 .await;
2557 let active_call_a = cx_a.read(ActiveCall::global);
2558
2559 client_a
2560 .fs()
2561 .insert_tree(
2562 "/dir",
2563 json!({
2564 ".git": {},
2565 "sub": {
2566 ".git": {},
2567 "b.txt": "
2568 one
2569 two
2570 three
2571 ".unindent(),
2572 },
2573 "a.txt": "
2574 one
2575 two
2576 three
2577 ".unindent(),
2578 }),
2579 )
2580 .await;
2581
2582 let (project_local, worktree_id) = client_a.build_local_project("/dir", cx_a).await;
2583 let project_id = active_call_a
2584 .update(cx_a, |call, cx| {
2585 call.share_project(project_local.clone(), cx)
2586 })
2587 .await
2588 .unwrap();
2589
2590 let project_remote = client_b.join_remote_project(project_id, cx_b).await;
2591
2592 let staged_text = "
2593 one
2594 three
2595 "
2596 .unindent();
2597
2598 let committed_text = "
2599 one
2600 TWO
2601 three
2602 "
2603 .unindent();
2604
2605 let new_committed_text = "
2606 one
2607 TWO_HUNDRED
2608 three
2609 "
2610 .unindent();
2611
2612 let new_staged_text = "
2613 one
2614 two
2615 "
2616 .unindent();
2617
2618 client_a
2619 .fs()
2620 .set_index_for_repo(Path::new("/dir/.git"), &[("a.txt", staged_text.clone())]);
2621 client_a.fs().set_head_for_repo(
2622 Path::new("/dir/.git"),
2623 &[("a.txt", committed_text.clone())],
2624 "deadbeef",
2625 );
2626
2627 // Create the buffer
2628 let buffer_local_a = project_local
2629 .update(cx_a, |p, cx| {
2630 p.open_buffer((worktree_id, rel_path("a.txt")), cx)
2631 })
2632 .await
2633 .unwrap();
2634 let local_unstaged_diff_a = project_local
2635 .update(cx_a, |p, cx| {
2636 p.open_unstaged_diff(buffer_local_a.clone(), cx)
2637 })
2638 .await
2639 .unwrap();
2640
2641 // Wait for it to catch up to the new diff
2642 executor.run_until_parked();
2643 local_unstaged_diff_a.read_with(cx_a, |diff, cx| {
2644 let buffer = buffer_local_a.read(cx);
2645 assert_eq!(
2646 diff.base_text_string(cx).as_deref(),
2647 Some(staged_text.as_str())
2648 );
2649 assert_hunks(
2650 diff.snapshot(cx).hunks_in_row_range(0..4, buffer),
2651 buffer,
2652 &diff.base_text_string(cx).unwrap(),
2653 &[(1..2, "", "two\n", DiffHunkStatus::added_none())],
2654 );
2655 });
2656
2657 // Create remote buffer
2658 let remote_buffer_a = project_remote
2659 .update(cx_b, |p, cx| {
2660 p.open_buffer((worktree_id, rel_path("a.txt")), cx)
2661 })
2662 .await
2663 .unwrap();
2664 let remote_unstaged_diff_a = project_remote
2665 .update(cx_b, |p, cx| {
2666 p.open_unstaged_diff(remote_buffer_a.clone(), cx)
2667 })
2668 .await
2669 .unwrap();
2670
2671 // Wait remote buffer to catch up to the new diff
2672 executor.run_until_parked();
2673 remote_unstaged_diff_a.read_with(cx_b, |diff, cx| {
2674 let buffer = remote_buffer_a.read(cx);
2675 assert_eq!(
2676 diff.base_text_string(cx).as_deref(),
2677 Some(staged_text.as_str())
2678 );
2679 assert_hunks(
2680 diff.snapshot(cx).hunks_in_row_range(0..4, buffer),
2681 buffer,
2682 &diff.base_text_string(cx).unwrap(),
2683 &[(1..2, "", "two\n", DiffHunkStatus::added_none())],
2684 );
2685 });
2686
2687 // Open uncommitted changes on the guest, without opening them on the host first
2688 let remote_uncommitted_diff_a = project_remote
2689 .update(cx_b, |p, cx| {
2690 p.open_uncommitted_diff(remote_buffer_a.clone(), cx)
2691 })
2692 .await
2693 .unwrap();
2694 executor.run_until_parked();
2695 remote_uncommitted_diff_a.read_with(cx_b, |diff, cx| {
2696 let buffer = remote_buffer_a.read(cx);
2697 assert_eq!(
2698 diff.base_text_string(cx).as_deref(),
2699 Some(committed_text.as_str())
2700 );
2701 assert_hunks(
2702 diff.snapshot(cx).hunks_in_row_range(0..4, buffer),
2703 buffer,
2704 &diff.base_text_string(cx).unwrap(),
2705 &[(
2706 1..2,
2707 "TWO\n",
2708 "two\n",
2709 DiffHunkStatus::modified(DiffHunkSecondaryStatus::HasSecondaryHunk),
2710 )],
2711 );
2712 });
2713
2714 // Update the index text of the open buffer
2715 client_a.fs().set_index_for_repo(
2716 Path::new("/dir/.git"),
2717 &[("a.txt", new_staged_text.clone())],
2718 );
2719 client_a.fs().set_head_for_repo(
2720 Path::new("/dir/.git"),
2721 &[("a.txt", new_committed_text.clone())],
2722 "deadbeef",
2723 );
2724
2725 // Wait for buffer_local_a to receive it
2726 executor.run_until_parked();
2727 local_unstaged_diff_a.read_with(cx_a, |diff, cx| {
2728 let buffer = buffer_local_a.read(cx);
2729 assert_eq!(
2730 diff.base_text_string(cx).as_deref(),
2731 Some(new_staged_text.as_str())
2732 );
2733 assert_hunks(
2734 diff.snapshot(cx).hunks_in_row_range(0..4, buffer),
2735 buffer,
2736 &diff.base_text_string(cx).unwrap(),
2737 &[(2..3, "", "three\n", DiffHunkStatus::added_none())],
2738 );
2739 });
2740
2741 // Guest receives index text update
2742 remote_unstaged_diff_a.read_with(cx_b, |diff, cx| {
2743 let buffer = remote_buffer_a.read(cx);
2744 assert_eq!(
2745 diff.base_text_string(cx).as_deref(),
2746 Some(new_staged_text.as_str())
2747 );
2748 assert_hunks(
2749 diff.snapshot(cx).hunks_in_row_range(0..4, buffer),
2750 buffer,
2751 &diff.base_text_string(cx).unwrap(),
2752 &[(2..3, "", "three\n", DiffHunkStatus::added_none())],
2753 );
2754 });
2755
2756 remote_uncommitted_diff_a.read_with(cx_b, |diff, cx| {
2757 let buffer = remote_buffer_a.read(cx);
2758 assert_eq!(
2759 diff.base_text_string(cx).as_deref(),
2760 Some(new_committed_text.as_str())
2761 );
2762 assert_hunks(
2763 diff.snapshot(cx).hunks_in_row_range(0..4, buffer),
2764 buffer,
2765 &diff.base_text_string(cx).unwrap(),
2766 &[(
2767 1..2,
2768 "TWO_HUNDRED\n",
2769 "two\n",
2770 DiffHunkStatus::modified(DiffHunkSecondaryStatus::OverlapsWithSecondaryHunk),
2771 )],
2772 );
2773 });
2774
2775 // Nested git dir
2776 let staged_text = "
2777 one
2778 three
2779 "
2780 .unindent();
2781
2782 let new_staged_text = "
2783 one
2784 two
2785 "
2786 .unindent();
2787
2788 client_a.fs().set_index_for_repo(
2789 Path::new("/dir/sub/.git"),
2790 &[("b.txt", staged_text.clone())],
2791 );
2792
2793 // Create the buffer
2794 let buffer_local_b = project_local
2795 .update(cx_a, |p, cx| {
2796 p.open_buffer((worktree_id, rel_path("sub/b.txt")), cx)
2797 })
2798 .await
2799 .unwrap();
2800 let local_unstaged_diff_b = project_local
2801 .update(cx_a, |p, cx| {
2802 p.open_unstaged_diff(buffer_local_b.clone(), cx)
2803 })
2804 .await
2805 .unwrap();
2806
2807 // Wait for it to catch up to the new diff
2808 executor.run_until_parked();
2809 local_unstaged_diff_b.read_with(cx_a, |diff, cx| {
2810 let buffer = buffer_local_b.read(cx);
2811 assert_eq!(
2812 diff.base_text_string(cx).as_deref(),
2813 Some(staged_text.as_str())
2814 );
2815 assert_hunks(
2816 diff.snapshot(cx).hunks_in_row_range(0..4, buffer),
2817 buffer,
2818 &diff.base_text_string(cx).unwrap(),
2819 &[(1..2, "", "two\n", DiffHunkStatus::added_none())],
2820 );
2821 });
2822
2823 // Create remote buffer
2824 let remote_buffer_b = project_remote
2825 .update(cx_b, |p, cx| {
2826 p.open_buffer((worktree_id, rel_path("sub/b.txt")), cx)
2827 })
2828 .await
2829 .unwrap();
2830 let remote_unstaged_diff_b = project_remote
2831 .update(cx_b, |p, cx| {
2832 p.open_unstaged_diff(remote_buffer_b.clone(), cx)
2833 })
2834 .await
2835 .unwrap();
2836
2837 executor.run_until_parked();
2838 remote_unstaged_diff_b.read_with(cx_b, |diff, cx| {
2839 let buffer = remote_buffer_b.read(cx);
2840 assert_eq!(
2841 diff.base_text_string(cx).as_deref(),
2842 Some(staged_text.as_str())
2843 );
2844 assert_hunks(
2845 diff.snapshot(cx).hunks_in_row_range(0..4, buffer),
2846 buffer,
2847 &staged_text,
2848 &[(1..2, "", "two\n", DiffHunkStatus::added_none())],
2849 );
2850 });
2851
2852 // Updatet the staged text
2853 client_a.fs().set_index_for_repo(
2854 Path::new("/dir/sub/.git"),
2855 &[("b.txt", new_staged_text.clone())],
2856 );
2857
2858 // Wait for buffer_local_b to receive it
2859 executor.run_until_parked();
2860 local_unstaged_diff_b.read_with(cx_a, |diff, cx| {
2861 let buffer = buffer_local_b.read(cx);
2862 assert_eq!(
2863 diff.base_text_string(cx).as_deref(),
2864 Some(new_staged_text.as_str())
2865 );
2866 assert_hunks(
2867 diff.snapshot(cx).hunks_in_row_range(0..4, buffer),
2868 buffer,
2869 &new_staged_text,
2870 &[(2..3, "", "three\n", DiffHunkStatus::added_none())],
2871 );
2872 });
2873
2874 remote_unstaged_diff_b.read_with(cx_b, |diff, cx| {
2875 let buffer = remote_buffer_b.read(cx);
2876 assert_eq!(
2877 diff.base_text_string(cx).as_deref(),
2878 Some(new_staged_text.as_str())
2879 );
2880 assert_hunks(
2881 diff.snapshot(cx).hunks_in_row_range(0..4, buffer),
2882 buffer,
2883 &new_staged_text,
2884 &[(2..3, "", "three\n", DiffHunkStatus::added_none())],
2885 );
2886 });
2887}
2888
2889#[gpui::test(iterations = 10)]
2890async fn test_git_branch_name(
2891 executor: BackgroundExecutor,
2892 cx_a: &mut TestAppContext,
2893 cx_b: &mut TestAppContext,
2894 cx_c: &mut TestAppContext,
2895) {
2896 let mut server = TestServer::start(executor.clone()).await;
2897 let client_a = server.create_client(cx_a, "user_a").await;
2898 let client_b = server.create_client(cx_b, "user_b").await;
2899 let client_c = server.create_client(cx_c, "user_c").await;
2900 server
2901 .create_room(&mut [(&client_a, cx_a), (&client_b, cx_b), (&client_c, cx_c)])
2902 .await;
2903 let active_call_a = cx_a.read(ActiveCall::global);
2904
2905 client_a
2906 .fs()
2907 .insert_tree(
2908 "/dir",
2909 json!({
2910 ".git": {},
2911 }),
2912 )
2913 .await;
2914
2915 let (project_local, _worktree_id) = client_a.build_local_project("/dir", cx_a).await;
2916 let project_id = active_call_a
2917 .update(cx_a, |call, cx| {
2918 call.share_project(project_local.clone(), cx)
2919 })
2920 .await
2921 .unwrap();
2922
2923 let project_remote = client_b.join_remote_project(project_id, cx_b).await;
2924 client_a
2925 .fs()
2926 .set_branch_name(Path::new("/dir/.git"), Some("branch-1"));
2927
2928 // Wait for it to catch up to the new branch
2929 executor.run_until_parked();
2930
2931 #[track_caller]
2932 fn assert_branch(branch_name: Option<impl Into<String>>, project: &Project, cx: &App) {
2933 let branch_name = branch_name.map(Into::into);
2934 let repositories = project.repositories(cx).values().collect::<Vec<_>>();
2935 assert_eq!(repositories.len(), 1);
2936 let repository = repositories[0].clone();
2937 assert_eq!(
2938 repository
2939 .read(cx)
2940 .branch
2941 .as_ref()
2942 .map(|branch| branch.name().to_owned()),
2943 branch_name
2944 )
2945 }
2946
2947 // Smoke test branch reading
2948
2949 project_local.read_with(cx_a, |project, cx| {
2950 assert_branch(Some("branch-1"), project, cx)
2951 });
2952
2953 project_remote.read_with(cx_b, |project, cx| {
2954 assert_branch(Some("branch-1"), project, cx)
2955 });
2956
2957 client_a
2958 .fs()
2959 .set_branch_name(Path::new("/dir/.git"), Some("branch-2"));
2960
2961 // Wait for buffer_local_a to receive it
2962 executor.run_until_parked();
2963
2964 // Smoke test branch reading
2965
2966 project_local.read_with(cx_a, |project, cx| {
2967 assert_branch(Some("branch-2"), project, cx)
2968 });
2969
2970 project_remote.read_with(cx_b, |project, cx| {
2971 assert_branch(Some("branch-2"), project, cx)
2972 });
2973
2974 let project_remote_c = client_c.join_remote_project(project_id, cx_c).await;
2975 executor.run_until_parked();
2976
2977 project_remote_c.read_with(cx_c, |project, cx| {
2978 assert_branch(Some("branch-2"), project, cx)
2979 });
2980}
2981
2982#[gpui::test]
2983async fn test_git_status_sync(
2984 executor: BackgroundExecutor,
2985 cx_a: &mut TestAppContext,
2986 cx_b: &mut TestAppContext,
2987 cx_c: &mut TestAppContext,
2988) {
2989 let mut server = TestServer::start(executor.clone()).await;
2990 let client_a = server.create_client(cx_a, "user_a").await;
2991 let client_b = server.create_client(cx_b, "user_b").await;
2992 let client_c = server.create_client(cx_c, "user_c").await;
2993 server
2994 .create_room(&mut [(&client_a, cx_a), (&client_b, cx_b), (&client_c, cx_c)])
2995 .await;
2996 let active_call_a = cx_a.read(ActiveCall::global);
2997
2998 client_a
2999 .fs()
3000 .insert_tree(
3001 path!("/dir"),
3002 json!({
3003 ".git": {},
3004 "a.txt": "a",
3005 "b.txt": "b",
3006 "c.txt": "c",
3007 }),
3008 )
3009 .await;
3010
3011 // Initially, a.txt is uncommitted, but present in the index,
3012 // and b.txt is unmerged.
3013 client_a.fs().set_head_for_repo(
3014 path!("/dir/.git").as_ref(),
3015 &[("b.txt", "B".into()), ("c.txt", "c".into())],
3016 "deadbeef",
3017 );
3018 client_a.fs().set_index_for_repo(
3019 path!("/dir/.git").as_ref(),
3020 &[
3021 ("a.txt", "".into()),
3022 ("b.txt", "B".into()),
3023 ("c.txt", "c".into()),
3024 ],
3025 );
3026 client_a.fs().set_unmerged_paths_for_repo(
3027 path!("/dir/.git").as_ref(),
3028 &[(
3029 repo_path("b.txt"),
3030 UnmergedStatus {
3031 first_head: UnmergedStatusCode::Updated,
3032 second_head: UnmergedStatusCode::Deleted,
3033 },
3034 )],
3035 );
3036
3037 const A_STATUS_START: FileStatus = FileStatus::Tracked(TrackedStatus {
3038 index_status: StatusCode::Added,
3039 worktree_status: StatusCode::Modified,
3040 });
3041 const B_STATUS_START: FileStatus = FileStatus::Unmerged(UnmergedStatus {
3042 first_head: UnmergedStatusCode::Updated,
3043 second_head: UnmergedStatusCode::Deleted,
3044 });
3045
3046 let (project_local, _worktree_id) = client_a.build_local_project(path!("/dir"), cx_a).await;
3047 let project_id = active_call_a
3048 .update(cx_a, |call, cx| {
3049 call.share_project(project_local.clone(), cx)
3050 })
3051 .await
3052 .unwrap();
3053
3054 let project_remote = client_b.join_remote_project(project_id, cx_b).await;
3055
3056 // Wait for it to catch up to the new status
3057 executor.run_until_parked();
3058
3059 #[track_caller]
3060 fn assert_status(file: &str, status: Option<FileStatus>, project: &Project, cx: &App) {
3061 let file = repo_path(file);
3062 let repos = project
3063 .repositories(cx)
3064 .values()
3065 .cloned()
3066 .collect::<Vec<_>>();
3067 assert_eq!(repos.len(), 1);
3068 let repo = repos.into_iter().next().unwrap();
3069 assert_eq!(
3070 repo.read(cx)
3071 .status_for_path(&file)
3072 .map(|entry| entry.status),
3073 status
3074 );
3075 }
3076
3077 project_local.read_with(cx_a, |project, cx| {
3078 assert_status("a.txt", Some(A_STATUS_START), project, cx);
3079 assert_status("b.txt", Some(B_STATUS_START), project, cx);
3080 assert_status("c.txt", None, project, cx);
3081 });
3082
3083 project_remote.read_with(cx_b, |project, cx| {
3084 assert_status("a.txt", Some(A_STATUS_START), project, cx);
3085 assert_status("b.txt", Some(B_STATUS_START), project, cx);
3086 assert_status("c.txt", None, project, cx);
3087 });
3088
3089 const A_STATUS_END: FileStatus = FileStatus::Tracked(TrackedStatus {
3090 index_status: StatusCode::Added,
3091 worktree_status: StatusCode::Unmodified,
3092 });
3093 const B_STATUS_END: FileStatus = FileStatus::Tracked(TrackedStatus {
3094 index_status: StatusCode::Deleted,
3095 worktree_status: StatusCode::Added,
3096 });
3097 const C_STATUS_END: FileStatus = FileStatus::Tracked(TrackedStatus {
3098 index_status: StatusCode::Unmodified,
3099 worktree_status: StatusCode::Modified,
3100 });
3101
3102 // Delete b.txt from the index, mark conflict as resolved,
3103 // and modify c.txt in the working copy.
3104 client_a.fs().set_index_for_repo(
3105 path!("/dir/.git").as_ref(),
3106 &[("a.txt", "a".into()), ("c.txt", "c".into())],
3107 );
3108 client_a
3109 .fs()
3110 .set_unmerged_paths_for_repo(path!("/dir/.git").as_ref(), &[]);
3111 client_a
3112 .fs()
3113 .atomic_write(path!("/dir/c.txt").into(), "CC".into())
3114 .await
3115 .unwrap();
3116
3117 // Wait for buffer_local_a to receive it
3118 executor.run_until_parked();
3119
3120 // Smoke test status reading
3121 project_local.read_with(cx_a, |project, cx| {
3122 assert_status("a.txt", Some(A_STATUS_END), project, cx);
3123 assert_status("b.txt", Some(B_STATUS_END), project, cx);
3124 assert_status("c.txt", Some(C_STATUS_END), project, cx);
3125 });
3126
3127 project_remote.read_with(cx_b, |project, cx| {
3128 assert_status("a.txt", Some(A_STATUS_END), project, cx);
3129 assert_status("b.txt", Some(B_STATUS_END), project, cx);
3130 assert_status("c.txt", Some(C_STATUS_END), project, cx);
3131 });
3132
3133 // And synchronization while joining
3134 let project_remote_c = client_c.join_remote_project(project_id, cx_c).await;
3135 executor.run_until_parked();
3136
3137 project_remote_c.read_with(cx_c, |project, cx| {
3138 assert_status("a.txt", Some(A_STATUS_END), project, cx);
3139 assert_status("b.txt", Some(B_STATUS_END), project, cx);
3140 assert_status("c.txt", Some(C_STATUS_END), project, cx);
3141 });
3142
3143 // Now remove the original git repository and check that collaborators are notified.
3144 client_a
3145 .fs()
3146 .remove_dir(path!("/dir/.git").as_ref(), RemoveOptions::default())
3147 .await
3148 .unwrap();
3149
3150 executor.run_until_parked();
3151 project_remote.update(cx_b, |project, cx| {
3152 pretty_assertions::assert_eq!(
3153 project.git_store().read(cx).repo_snapshots(cx),
3154 HashMap::default()
3155 );
3156 });
3157 project_remote_c.update(cx_c, |project, cx| {
3158 pretty_assertions::assert_eq!(
3159 project.git_store().read(cx).repo_snapshots(cx),
3160 HashMap::default()
3161 );
3162 });
3163}
3164
3165#[gpui::test(iterations = 10)]
3166async fn test_fs_operations(
3167 executor: BackgroundExecutor,
3168 cx_a: &mut TestAppContext,
3169 cx_b: &mut TestAppContext,
3170) {
3171 let mut server = TestServer::start(executor.clone()).await;
3172 let client_a = server.create_client(cx_a, "user_a").await;
3173 let client_b = server.create_client(cx_b, "user_b").await;
3174 server
3175 .create_room(&mut [(&client_a, cx_a), (&client_b, cx_b)])
3176 .await;
3177 let active_call_a = cx_a.read(ActiveCall::global);
3178
3179 client_a
3180 .fs()
3181 .insert_tree(
3182 path!("/dir"),
3183 json!({
3184 "a.txt": "a-contents",
3185 "b.txt": "b-contents",
3186 }),
3187 )
3188 .await;
3189 let (project_a, worktree_id) = client_a.build_local_project(path!("/dir"), cx_a).await;
3190 let project_id = active_call_a
3191 .update(cx_a, |call, cx| call.share_project(project_a.clone(), cx))
3192 .await
3193 .unwrap();
3194 let project_b = client_b.join_remote_project(project_id, cx_b).await;
3195
3196 let worktree_a = project_a.read_with(cx_a, |project, cx| project.worktrees(cx).next().unwrap());
3197 let worktree_b = project_b.read_with(cx_b, |project, cx| project.worktrees(cx).next().unwrap());
3198
3199 let entry = project_b
3200 .update(cx_b, |project, cx| {
3201 project.create_entry((worktree_id, rel_path("c.txt")), false, cx)
3202 })
3203 .await
3204 .unwrap()
3205 .into_included()
3206 .unwrap();
3207
3208 worktree_a.read_with(cx_a, |worktree, _| {
3209 assert_eq!(
3210 worktree.paths().collect::<Vec<_>>(),
3211 [rel_path("a.txt"), rel_path("b.txt"), rel_path("c.txt")]
3212 );
3213 });
3214
3215 worktree_b.read_with(cx_b, |worktree, _| {
3216 assert_eq!(
3217 worktree.paths().collect::<Vec<_>>(),
3218 [rel_path("a.txt"), rel_path("b.txt"), rel_path("c.txt")]
3219 );
3220 });
3221
3222 project_b
3223 .update(cx_b, |project, cx| {
3224 project.rename_entry(entry.id, (worktree_id, rel_path("d.txt")).into(), cx)
3225 })
3226 .await
3227 .unwrap()
3228 .into_included()
3229 .unwrap();
3230
3231 worktree_a.read_with(cx_a, |worktree, _| {
3232 assert_eq!(
3233 worktree.paths().collect::<Vec<_>>(),
3234 [rel_path("a.txt"), rel_path("b.txt"), rel_path("d.txt")]
3235 );
3236 });
3237
3238 worktree_b.read_with(cx_b, |worktree, _| {
3239 assert_eq!(
3240 worktree
3241 .paths()
3242 .map(|p| p.as_unix_str())
3243 .collect::<Vec<_>>(),
3244 ["a.txt", "b.txt", "d.txt"]
3245 );
3246 });
3247
3248 let dir_entry = project_b
3249 .update(cx_b, |project, cx| {
3250 project.create_entry((worktree_id, rel_path("DIR")), true, cx)
3251 })
3252 .await
3253 .unwrap()
3254 .into_included()
3255 .unwrap();
3256
3257 worktree_a.read_with(cx_a, |worktree, _| {
3258 assert_eq!(
3259 worktree
3260 .paths()
3261 .map(|p| p.as_unix_str())
3262 .collect::<Vec<_>>(),
3263 ["DIR", "a.txt", "b.txt", "d.txt"]
3264 );
3265 });
3266
3267 worktree_b.read_with(cx_b, |worktree, _| {
3268 assert_eq!(
3269 worktree
3270 .paths()
3271 .map(|p| p.as_unix_str())
3272 .collect::<Vec<_>>(),
3273 ["DIR", "a.txt", "b.txt", "d.txt"]
3274 );
3275 });
3276
3277 project_b
3278 .update(cx_b, |project, cx| {
3279 project.create_entry((worktree_id, rel_path("DIR/e.txt")), false, cx)
3280 })
3281 .await
3282 .unwrap()
3283 .into_included()
3284 .unwrap();
3285
3286 project_b
3287 .update(cx_b, |project, cx| {
3288 project.create_entry((worktree_id, rel_path("DIR/SUBDIR")), true, cx)
3289 })
3290 .await
3291 .unwrap()
3292 .into_included()
3293 .unwrap();
3294
3295 project_b
3296 .update(cx_b, |project, cx| {
3297 project.create_entry((worktree_id, rel_path("DIR/SUBDIR/f.txt")), false, cx)
3298 })
3299 .await
3300 .unwrap()
3301 .into_included()
3302 .unwrap();
3303
3304 worktree_a.read_with(cx_a, |worktree, _| {
3305 assert_eq!(
3306 worktree.paths().collect::<Vec<_>>(),
3307 [
3308 rel_path("DIR"),
3309 rel_path("DIR/SUBDIR"),
3310 rel_path("DIR/SUBDIR/f.txt"),
3311 rel_path("DIR/e.txt"),
3312 rel_path("a.txt"),
3313 rel_path("b.txt"),
3314 rel_path("d.txt")
3315 ]
3316 );
3317 });
3318
3319 worktree_b.read_with(cx_b, |worktree, _| {
3320 assert_eq!(
3321 worktree.paths().collect::<Vec<_>>(),
3322 [
3323 rel_path("DIR"),
3324 rel_path("DIR/SUBDIR"),
3325 rel_path("DIR/SUBDIR/f.txt"),
3326 rel_path("DIR/e.txt"),
3327 rel_path("a.txt"),
3328 rel_path("b.txt"),
3329 rel_path("d.txt")
3330 ]
3331 );
3332 });
3333
3334 project_b
3335 .update(cx_b, |project, cx| {
3336 project.copy_entry(
3337 entry.id,
3338 (worktree_b.read(cx).id(), rel_path("f.txt")).into(),
3339 cx,
3340 )
3341 })
3342 .await
3343 .unwrap()
3344 .unwrap();
3345
3346 worktree_a.read_with(cx_a, |worktree, _| {
3347 assert_eq!(
3348 worktree.paths().collect::<Vec<_>>(),
3349 [
3350 rel_path("DIR"),
3351 rel_path("DIR/SUBDIR"),
3352 rel_path("DIR/SUBDIR/f.txt"),
3353 rel_path("DIR/e.txt"),
3354 rel_path("a.txt"),
3355 rel_path("b.txt"),
3356 rel_path("d.txt"),
3357 rel_path("f.txt")
3358 ]
3359 );
3360 });
3361
3362 worktree_b.read_with(cx_b, |worktree, _| {
3363 assert_eq!(
3364 worktree.paths().collect::<Vec<_>>(),
3365 [
3366 rel_path("DIR"),
3367 rel_path("DIR/SUBDIR"),
3368 rel_path("DIR/SUBDIR/f.txt"),
3369 rel_path("DIR/e.txt"),
3370 rel_path("a.txt"),
3371 rel_path("b.txt"),
3372 rel_path("d.txt"),
3373 rel_path("f.txt")
3374 ]
3375 );
3376 });
3377
3378 project_b
3379 .update(cx_b, |project, cx| {
3380 project.delete_entry(dir_entry.id, false, cx).unwrap()
3381 })
3382 .await
3383 .unwrap();
3384 executor.run_until_parked();
3385
3386 worktree_a.read_with(cx_a, |worktree, _| {
3387 assert_eq!(
3388 worktree
3389 .paths()
3390 .map(|p| p.as_unix_str())
3391 .collect::<Vec<_>>(),
3392 ["a.txt", "b.txt", "d.txt", "f.txt"]
3393 );
3394 });
3395
3396 worktree_b.read_with(cx_b, |worktree, _| {
3397 assert_eq!(
3398 worktree
3399 .paths()
3400 .map(|p| p.as_unix_str())
3401 .collect::<Vec<_>>(),
3402 ["a.txt", "b.txt", "d.txt", "f.txt"]
3403 );
3404 });
3405
3406 project_b
3407 .update(cx_b, |project, cx| {
3408 project.delete_entry(entry.id, false, cx).unwrap()
3409 })
3410 .await
3411 .unwrap();
3412
3413 worktree_a.read_with(cx_a, |worktree, _| {
3414 assert_eq!(
3415 worktree
3416 .paths()
3417 .map(|p| p.as_unix_str())
3418 .collect::<Vec<_>>(),
3419 ["a.txt", "b.txt", "f.txt"]
3420 );
3421 });
3422
3423 worktree_b.read_with(cx_b, |worktree, _| {
3424 assert_eq!(
3425 worktree
3426 .paths()
3427 .map(|p| p.as_unix_str())
3428 .collect::<Vec<_>>(),
3429 ["a.txt", "b.txt", "f.txt"]
3430 );
3431 });
3432}
3433
3434#[gpui::test(iterations = 10)]
3435async fn test_local_settings(
3436 executor: BackgroundExecutor,
3437 cx_a: &mut TestAppContext,
3438 cx_b: &mut TestAppContext,
3439) {
3440 let mut server = TestServer::start(executor.clone()).await;
3441 let client_a = server.create_client(cx_a, "user_a").await;
3442 let client_b = server.create_client(cx_b, "user_b").await;
3443 server
3444 .create_room(&mut [(&client_a, cx_a), (&client_b, cx_b)])
3445 .await;
3446 let active_call_a = cx_a.read(ActiveCall::global);
3447
3448 // As client A, open a project that contains some local settings files
3449 client_a
3450 .fs()
3451 .insert_tree(
3452 "/dir",
3453 json!({
3454 ".zed": {
3455 "settings.json": r#"{ "tab_size": 2 }"#
3456 },
3457 "a": {
3458 ".zed": {
3459 "settings.json": r#"{ "tab_size": 8 }"#
3460 },
3461 "a.txt": "a-contents",
3462 },
3463 "b": {
3464 "b.txt": "b-contents",
3465 }
3466 }),
3467 )
3468 .await;
3469 let (project_a, _) = client_a.build_local_project("/dir", cx_a).await;
3470 executor.run_until_parked();
3471 let project_id = active_call_a
3472 .update(cx_a, |call, cx| call.share_project(project_a.clone(), cx))
3473 .await
3474 .unwrap();
3475 executor.run_until_parked();
3476
3477 // As client B, join that project and observe the local settings.
3478 let project_b = client_b.join_remote_project(project_id, cx_b).await;
3479
3480 let worktree_b = project_b.read_with(cx_b, |project, cx| project.worktrees(cx).next().unwrap());
3481 executor.run_until_parked();
3482 cx_b.read(|cx| {
3483 let store = cx.global::<SettingsStore>();
3484 assert_eq!(
3485 store
3486 .local_settings(worktree_b.read(cx).id())
3487 .map(|(path, content)| (
3488 path,
3489 content.all_languages.defaults.tab_size.map(Into::into)
3490 ))
3491 .collect::<Vec<_>>(),
3492 &[
3493 (rel_path("").into(), Some(2)),
3494 (rel_path("a").into(), Some(8)),
3495 ]
3496 )
3497 });
3498
3499 // As client A, update a settings file. As Client B, see the changed settings.
3500 client_a
3501 .fs()
3502 .insert_file("/dir/.zed/settings.json", r#"{}"#.into())
3503 .await;
3504 executor.run_until_parked();
3505 cx_b.read(|cx| {
3506 let store = cx.global::<SettingsStore>();
3507 assert_eq!(
3508 store
3509 .local_settings(worktree_b.read(cx).id())
3510 .map(|(path, content)| (
3511 path,
3512 content.all_languages.defaults.tab_size.map(Into::into)
3513 ))
3514 .collect::<Vec<_>>(),
3515 &[(rel_path("").into(), None), (rel_path("a").into(), Some(8)),]
3516 )
3517 });
3518
3519 // As client A, create and remove some settings files. As client B, see the changed settings.
3520 client_a
3521 .fs()
3522 .remove_file("/dir/.zed/settings.json".as_ref(), Default::default())
3523 .await
3524 .unwrap();
3525 client_a
3526 .fs()
3527 .create_dir("/dir/b/.zed".as_ref())
3528 .await
3529 .unwrap();
3530 client_a
3531 .fs()
3532 .insert_file("/dir/b/.zed/settings.json", r#"{"tab_size": 4}"#.into())
3533 .await;
3534 executor.run_until_parked();
3535 cx_b.read(|cx| {
3536 let store = cx.global::<SettingsStore>();
3537 assert_eq!(
3538 store
3539 .local_settings(worktree_b.read(cx).id())
3540 .map(|(path, content)| (
3541 path,
3542 content.all_languages.defaults.tab_size.map(Into::into)
3543 ))
3544 .collect::<Vec<_>>(),
3545 &[
3546 (rel_path("a").into(), Some(8)),
3547 (rel_path("b").into(), Some(4)),
3548 ]
3549 )
3550 });
3551
3552 // As client B, disconnect.
3553 server.forbid_connections();
3554 server.disconnect_client(client_b.peer_id().unwrap());
3555
3556 // As client A, change and remove settings files while client B is disconnected.
3557 client_a
3558 .fs()
3559 .insert_file("/dir/a/.zed/settings.json", r#"{"hard_tabs":true}"#.into())
3560 .await;
3561 client_a
3562 .fs()
3563 .remove_file("/dir/b/.zed/settings.json".as_ref(), Default::default())
3564 .await
3565 .unwrap();
3566 executor.run_until_parked();
3567
3568 // As client B, reconnect and see the changed settings.
3569 server.allow_connections();
3570 executor.advance_clock(RECEIVE_TIMEOUT);
3571 cx_b.read(|cx| {
3572 let store = cx.global::<SettingsStore>();
3573 assert_eq!(
3574 store
3575 .local_settings(worktree_b.read(cx).id())
3576 .map(|(path, content)| (path, content.all_languages.defaults.hard_tabs))
3577 .collect::<Vec<_>>(),
3578 &[(rel_path("a").into(), Some(true))],
3579 )
3580 });
3581}
3582
3583#[gpui::test(iterations = 10)]
3584async fn test_buffer_conflict_after_save(
3585 executor: BackgroundExecutor,
3586 cx_a: &mut TestAppContext,
3587 cx_b: &mut TestAppContext,
3588) {
3589 let mut server = TestServer::start(executor.clone()).await;
3590 let client_a = server.create_client(cx_a, "user_a").await;
3591 let client_b = server.create_client(cx_b, "user_b").await;
3592 server
3593 .create_room(&mut [(&client_a, cx_a), (&client_b, cx_b)])
3594 .await;
3595 let active_call_a = cx_a.read(ActiveCall::global);
3596
3597 client_a
3598 .fs()
3599 .insert_tree(
3600 path!("/dir"),
3601 json!({
3602 "a.txt": "a-contents",
3603 }),
3604 )
3605 .await;
3606 let (project_a, worktree_id) = client_a.build_local_project(path!("/dir"), cx_a).await;
3607 let project_id = active_call_a
3608 .update(cx_a, |call, cx| call.share_project(project_a.clone(), cx))
3609 .await
3610 .unwrap();
3611 let project_b = client_b.join_remote_project(project_id, cx_b).await;
3612
3613 // Open a buffer as client B
3614 let buffer_b = project_b
3615 .update(cx_b, |p, cx| {
3616 p.open_buffer((worktree_id, rel_path("a.txt")), cx)
3617 })
3618 .await
3619 .unwrap();
3620
3621 buffer_b.update(cx_b, |buf, cx| buf.edit([(0..0, "world ")], None, cx));
3622
3623 buffer_b.read_with(cx_b, |buf, _| {
3624 assert!(buf.is_dirty());
3625 assert!(!buf.has_conflict());
3626 });
3627
3628 project_b
3629 .update(cx_b, |project, cx| {
3630 project.save_buffer(buffer_b.clone(), cx)
3631 })
3632 .await
3633 .unwrap();
3634
3635 buffer_b.read_with(cx_b, |buffer_b, _| assert!(!buffer_b.is_dirty()));
3636
3637 buffer_b.read_with(cx_b, |buf, _| {
3638 assert!(!buf.has_conflict());
3639 });
3640
3641 buffer_b.update(cx_b, |buf, cx| buf.edit([(0..0, "hello ")], None, cx));
3642
3643 buffer_b.read_with(cx_b, |buf, _| {
3644 assert!(buf.is_dirty());
3645 assert!(!buf.has_conflict());
3646 });
3647}
3648
3649#[gpui::test(iterations = 10)]
3650async fn test_buffer_reloading(
3651 executor: BackgroundExecutor,
3652 cx_a: &mut TestAppContext,
3653 cx_b: &mut TestAppContext,
3654) {
3655 let mut server = TestServer::start(executor.clone()).await;
3656 let client_a = server.create_client(cx_a, "user_a").await;
3657 let client_b = server.create_client(cx_b, "user_b").await;
3658 server
3659 .create_room(&mut [(&client_a, cx_a), (&client_b, cx_b)])
3660 .await;
3661 let active_call_a = cx_a.read(ActiveCall::global);
3662
3663 client_a
3664 .fs()
3665 .insert_tree(
3666 path!("/dir"),
3667 json!({
3668 "a.txt": "a\nb\nc",
3669 }),
3670 )
3671 .await;
3672 let (project_a, worktree_id) = client_a.build_local_project(path!("/dir"), cx_a).await;
3673 let project_id = active_call_a
3674 .update(cx_a, |call, cx| call.share_project(project_a.clone(), cx))
3675 .await
3676 .unwrap();
3677 let project_b = client_b.join_remote_project(project_id, cx_b).await;
3678
3679 // Open a buffer as client B
3680 let buffer_b = project_b
3681 .update(cx_b, |p, cx| {
3682 p.open_buffer((worktree_id, rel_path("a.txt")), cx)
3683 })
3684 .await
3685 .unwrap();
3686
3687 buffer_b.read_with(cx_b, |buf, _| {
3688 assert!(!buf.is_dirty());
3689 assert!(!buf.has_conflict());
3690 assert_eq!(buf.line_ending(), LineEnding::Unix);
3691 });
3692
3693 let new_contents = Rope::from("d\ne\nf");
3694 client_a
3695 .fs()
3696 .save(
3697 path!("/dir/a.txt").as_ref(),
3698 &new_contents,
3699 LineEnding::Windows,
3700 )
3701 .await
3702 .unwrap();
3703
3704 executor.run_until_parked();
3705
3706 buffer_b.read_with(cx_b, |buf, _| {
3707 assert_eq!(buf.text(), new_contents.to_string());
3708 assert!(!buf.is_dirty());
3709 assert!(!buf.has_conflict());
3710 assert_eq!(buf.line_ending(), LineEnding::Windows);
3711 });
3712}
3713
3714#[gpui::test(iterations = 10)]
3715async fn test_editing_while_guest_opens_buffer(
3716 executor: BackgroundExecutor,
3717 cx_a: &mut TestAppContext,
3718 cx_b: &mut TestAppContext,
3719) {
3720 let mut server = TestServer::start(executor.clone()).await;
3721 let client_a = server.create_client(cx_a, "user_a").await;
3722 let client_b = server.create_client(cx_b, "user_b").await;
3723 server
3724 .create_room(&mut [(&client_a, cx_a), (&client_b, cx_b)])
3725 .await;
3726 let active_call_a = cx_a.read(ActiveCall::global);
3727
3728 client_a
3729 .fs()
3730 .insert_tree(path!("/dir"), json!({ "a.txt": "a-contents" }))
3731 .await;
3732 let (project_a, worktree_id) = client_a.build_local_project(path!("/dir"), cx_a).await;
3733 let project_id = active_call_a
3734 .update(cx_a, |call, cx| call.share_project(project_a.clone(), cx))
3735 .await
3736 .unwrap();
3737 let project_b = client_b.join_remote_project(project_id, cx_b).await;
3738
3739 // Open a buffer as client A
3740 let buffer_a = project_a
3741 .update(cx_a, |p, cx| {
3742 p.open_buffer((worktree_id, rel_path("a.txt")), cx)
3743 })
3744 .await
3745 .unwrap();
3746
3747 // Start opening the same buffer as client B
3748 let open_buffer = project_b.update(cx_b, |p, cx| {
3749 p.open_buffer((worktree_id, rel_path("a.txt")), cx)
3750 });
3751 let buffer_b = cx_b.executor().spawn(open_buffer);
3752
3753 // Edit the buffer as client A while client B is still opening it.
3754 cx_b.executor().simulate_random_delay().await;
3755 buffer_a.update(cx_a, |buf, cx| buf.edit([(0..0, "X")], None, cx));
3756 cx_b.executor().simulate_random_delay().await;
3757 buffer_a.update(cx_a, |buf, cx| buf.edit([(1..1, "Y")], None, cx));
3758
3759 let text = buffer_a.read_with(cx_a, |buf, _| buf.text());
3760 let buffer_b = buffer_b.await.unwrap();
3761 executor.run_until_parked();
3762
3763 buffer_b.read_with(cx_b, |buf, _| assert_eq!(buf.text(), text));
3764}
3765
3766#[gpui::test(iterations = 10)]
3767async fn test_leaving_worktree_while_opening_buffer(
3768 executor: BackgroundExecutor,
3769 cx_a: &mut TestAppContext,
3770 cx_b: &mut TestAppContext,
3771) {
3772 let mut server = TestServer::start(executor.clone()).await;
3773 let client_a = server.create_client(cx_a, "user_a").await;
3774 let client_b = server.create_client(cx_b, "user_b").await;
3775 server
3776 .create_room(&mut [(&client_a, cx_a), (&client_b, cx_b)])
3777 .await;
3778 let active_call_a = cx_a.read(ActiveCall::global);
3779
3780 client_a
3781 .fs()
3782 .insert_tree("/dir", json!({ "a.txt": "a-contents" }))
3783 .await;
3784 let (project_a, worktree_id) = client_a.build_local_project("/dir", cx_a).await;
3785 let project_id = active_call_a
3786 .update(cx_a, |call, cx| call.share_project(project_a.clone(), cx))
3787 .await
3788 .unwrap();
3789 let project_b = client_b.join_remote_project(project_id, cx_b).await;
3790
3791 // See that a guest has joined as client A.
3792 executor.run_until_parked();
3793
3794 project_a.read_with(cx_a, |p, _| assert_eq!(p.collaborators().len(), 1));
3795
3796 // Begin opening a buffer as client B, but leave the project before the open completes.
3797 let open_buffer = project_b.update(cx_b, |p, cx| {
3798 p.open_buffer((worktree_id, rel_path("a.txt")), cx)
3799 });
3800 let buffer_b = cx_b.executor().spawn(open_buffer);
3801 cx_b.update(|_| drop(project_b));
3802 drop(buffer_b);
3803
3804 // See that the guest has left.
3805 executor.run_until_parked();
3806
3807 project_a.read_with(cx_a, |p, _| assert!(p.collaborators().is_empty()));
3808}
3809
3810#[gpui::test(iterations = 10)]
3811async fn test_canceling_buffer_opening(
3812 executor: BackgroundExecutor,
3813 cx_a: &mut TestAppContext,
3814 cx_b: &mut TestAppContext,
3815) {
3816 let mut server = TestServer::start(executor.clone()).await;
3817 let client_a = server.create_client(cx_a, "user_a").await;
3818 let client_b = server.create_client(cx_b, "user_b").await;
3819 server
3820 .create_room(&mut [(&client_a, cx_a), (&client_b, cx_b)])
3821 .await;
3822 let active_call_a = cx_a.read(ActiveCall::global);
3823
3824 client_a
3825 .fs()
3826 .insert_tree(
3827 "/dir",
3828 json!({
3829 "a.txt": "abc",
3830 }),
3831 )
3832 .await;
3833 let (project_a, worktree_id) = client_a.build_local_project("/dir", cx_a).await;
3834 let project_id = active_call_a
3835 .update(cx_a, |call, cx| call.share_project(project_a.clone(), cx))
3836 .await
3837 .unwrap();
3838 let project_b = client_b.join_remote_project(project_id, cx_b).await;
3839
3840 let buffer_a = project_a
3841 .update(cx_a, |p, cx| {
3842 p.open_buffer((worktree_id, rel_path("a.txt")), cx)
3843 })
3844 .await
3845 .unwrap();
3846
3847 // Open a buffer as client B but cancel after a random amount of time.
3848 let buffer_b = project_b.update(cx_b, |p, cx| {
3849 p.open_buffer_by_id(buffer_a.read_with(cx_a, |a, _| a.remote_id()), cx)
3850 });
3851 executor.simulate_random_delay().await;
3852 drop(buffer_b);
3853
3854 // Try opening the same buffer again as client B, and ensure we can
3855 // still do it despite the cancellation above.
3856 let buffer_b = project_b
3857 .update(cx_b, |p, cx| {
3858 p.open_buffer_by_id(buffer_a.read_with(cx_a, |a, _| a.remote_id()), cx)
3859 })
3860 .await
3861 .unwrap();
3862
3863 buffer_b.read_with(cx_b, |buf, _| assert_eq!(buf.text(), "abc"));
3864}
3865
3866#[gpui::test(iterations = 10)]
3867async fn test_leaving_project(
3868 executor: BackgroundExecutor,
3869 cx_a: &mut TestAppContext,
3870 cx_b: &mut TestAppContext,
3871 cx_c: &mut TestAppContext,
3872) {
3873 let mut server = TestServer::start(executor.clone()).await;
3874 let client_a = server.create_client(cx_a, "user_a").await;
3875 let client_b = server.create_client(cx_b, "user_b").await;
3876 let client_c = server.create_client(cx_c, "user_c").await;
3877 server
3878 .create_room(&mut [(&client_a, cx_a), (&client_b, cx_b), (&client_c, cx_c)])
3879 .await;
3880 let active_call_a = cx_a.read(ActiveCall::global);
3881
3882 client_a
3883 .fs()
3884 .insert_tree(
3885 "/a",
3886 json!({
3887 "a.txt": "a-contents",
3888 "b.txt": "b-contents",
3889 }),
3890 )
3891 .await;
3892 let (project_a, _) = client_a.build_local_project("/a", cx_a).await;
3893 let project_id = active_call_a
3894 .update(cx_a, |call, cx| call.share_project(project_a.clone(), cx))
3895 .await
3896 .unwrap();
3897 let project_b1 = client_b.join_remote_project(project_id, cx_b).await;
3898 let project_c = client_c.join_remote_project(project_id, cx_c).await;
3899
3900 // Client A sees that a guest has joined.
3901 executor.run_until_parked();
3902
3903 project_a.read_with(cx_a, |project, _| {
3904 assert_eq!(project.collaborators().len(), 2);
3905 });
3906
3907 project_b1.read_with(cx_b, |project, _| {
3908 assert_eq!(project.collaborators().len(), 2);
3909 });
3910
3911 project_c.read_with(cx_c, |project, _| {
3912 assert_eq!(project.collaborators().len(), 2);
3913 });
3914
3915 // Client B opens a buffer.
3916 let buffer_b1 = project_b1
3917 .update(cx_b, |project, cx| {
3918 let worktree_id = project.worktrees(cx).next().unwrap().read(cx).id();
3919 project.open_buffer((worktree_id, rel_path("a.txt")), cx)
3920 })
3921 .await
3922 .unwrap();
3923
3924 buffer_b1.read_with(cx_b, |buffer, _| assert_eq!(buffer.text(), "a-contents"));
3925
3926 // Drop client B's project and ensure client A and client C observe client B leaving.
3927 cx_b.update(|_| drop(project_b1));
3928 executor.run_until_parked();
3929
3930 project_a.read_with(cx_a, |project, _| {
3931 assert_eq!(project.collaborators().len(), 1);
3932 });
3933
3934 project_c.read_with(cx_c, |project, _| {
3935 assert_eq!(project.collaborators().len(), 1);
3936 });
3937
3938 // Client B re-joins the project and can open buffers as before.
3939 let project_b2 = client_b.join_remote_project(project_id, cx_b).await;
3940 executor.run_until_parked();
3941
3942 project_a.read_with(cx_a, |project, _| {
3943 assert_eq!(project.collaborators().len(), 2);
3944 });
3945
3946 project_b2.read_with(cx_b, |project, _| {
3947 assert_eq!(project.collaborators().len(), 2);
3948 });
3949
3950 project_c.read_with(cx_c, |project, _| {
3951 assert_eq!(project.collaborators().len(), 2);
3952 });
3953
3954 let buffer_b2 = project_b2
3955 .update(cx_b, |project, cx| {
3956 let worktree_id = project.worktrees(cx).next().unwrap().read(cx).id();
3957 project.open_buffer((worktree_id, rel_path("a.txt")), cx)
3958 })
3959 .await
3960 .unwrap();
3961
3962 buffer_b2.read_with(cx_b, |buffer, _| assert_eq!(buffer.text(), "a-contents"));
3963
3964 project_a.read_with(cx_a, |project, _| {
3965 assert_eq!(project.collaborators().len(), 2);
3966 });
3967
3968 // Drop client B's connection and ensure client A and client C observe client B leaving.
3969 client_b.disconnect(&cx_b.to_async());
3970 executor.advance_clock(RECONNECT_TIMEOUT);
3971
3972 project_a.read_with(cx_a, |project, _| {
3973 assert_eq!(project.collaborators().len(), 1);
3974 });
3975
3976 project_b2.read_with(cx_b, |project, cx| {
3977 assert!(project.is_disconnected(cx));
3978 });
3979
3980 project_c.read_with(cx_c, |project, _| {
3981 assert_eq!(project.collaborators().len(), 1);
3982 });
3983
3984 // Client B can't join the project, unless they re-join the room.
3985 cx_b.spawn(|cx| {
3986 Project::in_room(
3987 project_id,
3988 client_b.app_state.client.clone(),
3989 client_b.user_store().clone(),
3990 client_b.language_registry().clone(),
3991 FakeFs::new(cx.background_executor().clone()),
3992 cx,
3993 )
3994 })
3995 .await
3996 .unwrap_err();
3997
3998 // Simulate connection loss for client C and ensure client A observes client C leaving the project.
3999 client_c.wait_for_current_user(cx_c).await;
4000 server.forbid_connections();
4001 server.disconnect_client(client_c.peer_id().unwrap());
4002 executor.advance_clock(RECEIVE_TIMEOUT + RECONNECT_TIMEOUT);
4003 executor.run_until_parked();
4004
4005 project_a.read_with(cx_a, |project, _| {
4006 assert_eq!(project.collaborators().len(), 0);
4007 });
4008
4009 project_b2.read_with(cx_b, |project, cx| {
4010 assert!(project.is_disconnected(cx));
4011 });
4012
4013 project_c.read_with(cx_c, |project, cx| {
4014 assert!(project.is_disconnected(cx));
4015 });
4016}
4017
4018#[gpui::test(iterations = 10)]
4019async fn test_collaborating_with_diagnostics(
4020 executor: BackgroundExecutor,
4021 cx_a: &mut TestAppContext,
4022 cx_b: &mut TestAppContext,
4023 cx_c: &mut TestAppContext,
4024) {
4025 let mut server = TestServer::start(executor.clone()).await;
4026 let client_a = server.create_client(cx_a, "user_a").await;
4027 let client_b = server.create_client(cx_b, "user_b").await;
4028 let client_c = server.create_client(cx_c, "user_c").await;
4029 server
4030 .create_room(&mut [(&client_a, cx_a), (&client_b, cx_b), (&client_c, cx_c)])
4031 .await;
4032 let active_call_a = cx_a.read(ActiveCall::global);
4033
4034 client_a.language_registry().add(Arc::new(Language::new(
4035 LanguageConfig {
4036 name: "Rust".into(),
4037 matcher: LanguageMatcher {
4038 path_suffixes: vec!["rs".to_string()],
4039 ..Default::default()
4040 },
4041 ..Default::default()
4042 },
4043 Some(tree_sitter_rust::LANGUAGE.into()),
4044 )));
4045 let mut fake_language_servers = client_a
4046 .language_registry()
4047 .register_fake_lsp("Rust", Default::default());
4048
4049 // Share a project as client A
4050 client_a
4051 .fs()
4052 .insert_tree(
4053 path!("/a"),
4054 json!({
4055 "a.rs": "let one = two",
4056 "other.rs": "",
4057 }),
4058 )
4059 .await;
4060 let (project_a, worktree_id) = client_a.build_local_project(path!("/a"), cx_a).await;
4061
4062 // Cause the language server to start.
4063 let _buffer = project_a
4064 .update(cx_a, |project, cx| {
4065 project.open_local_buffer_with_lsp(path!("/a/other.rs"), cx)
4066 })
4067 .await
4068 .unwrap();
4069
4070 // Simulate a language server reporting errors for a file.
4071 let mut fake_language_server = fake_language_servers.next().await.unwrap();
4072 fake_language_server
4073 .receive_notification::<lsp::notification::DidOpenTextDocument>()
4074 .await;
4075 fake_language_server.notify::<lsp::notification::PublishDiagnostics>(
4076 lsp::PublishDiagnosticsParams {
4077 uri: lsp::Uri::from_file_path(path!("/a/a.rs")).unwrap(),
4078 version: None,
4079 diagnostics: vec![lsp::Diagnostic {
4080 severity: Some(lsp::DiagnosticSeverity::WARNING),
4081 range: lsp::Range::new(lsp::Position::new(0, 4), lsp::Position::new(0, 7)),
4082 message: "message 0".to_string(),
4083 ..Default::default()
4084 }],
4085 },
4086 );
4087
4088 // Client A shares the project and, simultaneously, the language server
4089 // publishes a diagnostic. This is done to ensure that the server always
4090 // observes the latest diagnostics for a worktree.
4091 let project_id = active_call_a
4092 .update(cx_a, |call, cx| call.share_project(project_a.clone(), cx))
4093 .await
4094 .unwrap();
4095 fake_language_server.notify::<lsp::notification::PublishDiagnostics>(
4096 lsp::PublishDiagnosticsParams {
4097 uri: lsp::Uri::from_file_path(path!("/a/a.rs")).unwrap(),
4098 version: None,
4099 diagnostics: vec![lsp::Diagnostic {
4100 severity: Some(lsp::DiagnosticSeverity::ERROR),
4101 range: lsp::Range::new(lsp::Position::new(0, 4), lsp::Position::new(0, 7)),
4102 message: "message 1".to_string(),
4103 ..Default::default()
4104 }],
4105 },
4106 );
4107
4108 // Join the worktree as client B.
4109 let project_b = client_b.join_remote_project(project_id, cx_b).await;
4110
4111 // Wait for server to see the diagnostics update.
4112 executor.run_until_parked();
4113
4114 // Ensure client B observes the new diagnostics.
4115
4116 project_b.read_with(cx_b, |project, cx| {
4117 assert_eq!(
4118 project.diagnostic_summaries(false, cx).collect::<Vec<_>>(),
4119 &[(
4120 ProjectPath {
4121 worktree_id,
4122 path: rel_path("a.rs").into(),
4123 },
4124 LanguageServerId(0),
4125 DiagnosticSummary {
4126 error_count: 1,
4127 warning_count: 0,
4128 },
4129 )]
4130 )
4131 });
4132
4133 // Join project as client C and observe the diagnostics.
4134 let project_c = client_c.join_remote_project(project_id, cx_c).await;
4135 executor.run_until_parked();
4136 let project_c_diagnostic_summaries =
4137 Rc::new(RefCell::new(project_c.read_with(cx_c, |project, cx| {
4138 project.diagnostic_summaries(false, cx).collect::<Vec<_>>()
4139 })));
4140 project_c.update(cx_c, |_, cx| {
4141 let summaries = project_c_diagnostic_summaries.clone();
4142 cx.subscribe(&project_c, {
4143 move |p, _, event, cx| {
4144 if let project::Event::DiskBasedDiagnosticsFinished { .. } = event {
4145 *summaries.borrow_mut() = p.diagnostic_summaries(false, cx).collect();
4146 }
4147 }
4148 })
4149 .detach();
4150 });
4151
4152 executor.run_until_parked();
4153 assert_eq!(
4154 project_c_diagnostic_summaries.borrow().as_slice(),
4155 &[(
4156 ProjectPath {
4157 worktree_id,
4158 path: rel_path("a.rs").into(),
4159 },
4160 LanguageServerId(0),
4161 DiagnosticSummary {
4162 error_count: 1,
4163 warning_count: 0,
4164 },
4165 )]
4166 );
4167
4168 // Simulate a language server reporting more errors for a file.
4169 fake_language_server.notify::<lsp::notification::PublishDiagnostics>(
4170 lsp::PublishDiagnosticsParams {
4171 uri: lsp::Uri::from_file_path(path!("/a/a.rs")).unwrap(),
4172 version: None,
4173 diagnostics: vec![
4174 lsp::Diagnostic {
4175 severity: Some(lsp::DiagnosticSeverity::ERROR),
4176 range: lsp::Range::new(lsp::Position::new(0, 4), lsp::Position::new(0, 7)),
4177 message: "message 1".to_string(),
4178 ..Default::default()
4179 },
4180 lsp::Diagnostic {
4181 severity: Some(lsp::DiagnosticSeverity::WARNING),
4182 range: lsp::Range::new(lsp::Position::new(0, 10), lsp::Position::new(0, 13)),
4183 message: "message 2".to_string(),
4184 ..Default::default()
4185 },
4186 ],
4187 },
4188 );
4189
4190 // Clients B and C get the updated summaries
4191 executor.run_until_parked();
4192
4193 project_b.read_with(cx_b, |project, cx| {
4194 assert_eq!(
4195 project.diagnostic_summaries(false, cx).collect::<Vec<_>>(),
4196 [(
4197 ProjectPath {
4198 worktree_id,
4199 path: rel_path("a.rs").into(),
4200 },
4201 LanguageServerId(0),
4202 DiagnosticSummary {
4203 error_count: 1,
4204 warning_count: 1,
4205 },
4206 )]
4207 );
4208 });
4209
4210 project_c.read_with(cx_c, |project, cx| {
4211 assert_eq!(
4212 project.diagnostic_summaries(false, cx).collect::<Vec<_>>(),
4213 [(
4214 ProjectPath {
4215 worktree_id,
4216 path: rel_path("a.rs").into(),
4217 },
4218 LanguageServerId(0),
4219 DiagnosticSummary {
4220 error_count: 1,
4221 warning_count: 1,
4222 },
4223 )]
4224 );
4225 });
4226
4227 // Open the file with the errors on client B. They should be present.
4228 let open_buffer = project_b.update(cx_b, |p, cx| {
4229 p.open_buffer((worktree_id, rel_path("a.rs")), cx)
4230 });
4231 let buffer_b = cx_b.executor().spawn(open_buffer).await.unwrap();
4232
4233 buffer_b.read_with(cx_b, |buffer, _| {
4234 assert_eq!(
4235 buffer
4236 .snapshot()
4237 .diagnostics_in_range::<_, Point>(0..buffer.len(), false)
4238 .collect::<Vec<_>>(),
4239 &[
4240 DiagnosticEntry {
4241 range: Point::new(0, 4)..Point::new(0, 7),
4242 diagnostic: Diagnostic {
4243 group_id: 2,
4244 message: "message 1".to_string(),
4245 severity: lsp::DiagnosticSeverity::ERROR,
4246 is_primary: true,
4247 source_kind: DiagnosticSourceKind::Pushed,
4248 ..Diagnostic::default()
4249 }
4250 },
4251 DiagnosticEntry {
4252 range: Point::new(0, 10)..Point::new(0, 13),
4253 diagnostic: Diagnostic {
4254 group_id: 3,
4255 severity: lsp::DiagnosticSeverity::WARNING,
4256 message: "message 2".to_string(),
4257 is_primary: true,
4258 source_kind: DiagnosticSourceKind::Pushed,
4259 ..Diagnostic::default()
4260 }
4261 }
4262 ]
4263 );
4264 });
4265
4266 // Simulate a language server reporting no errors for a file.
4267 fake_language_server.notify::<lsp::notification::PublishDiagnostics>(
4268 lsp::PublishDiagnosticsParams {
4269 uri: lsp::Uri::from_file_path(path!("/a/a.rs")).unwrap(),
4270 version: None,
4271 diagnostics: Vec::new(),
4272 },
4273 );
4274 executor.run_until_parked();
4275
4276 project_a.read_with(cx_a, |project, cx| {
4277 assert_eq!(
4278 project.diagnostic_summaries(false, cx).collect::<Vec<_>>(),
4279 []
4280 )
4281 });
4282
4283 project_b.read_with(cx_b, |project, cx| {
4284 assert_eq!(
4285 project.diagnostic_summaries(false, cx).collect::<Vec<_>>(),
4286 []
4287 )
4288 });
4289
4290 project_c.read_with(cx_c, |project, cx| {
4291 assert_eq!(
4292 project.diagnostic_summaries(false, cx).collect::<Vec<_>>(),
4293 []
4294 )
4295 });
4296}
4297
4298#[gpui::test(iterations = 10)]
4299async fn test_collaborating_with_lsp_progress_updates_and_diagnostics_ordering(
4300 executor: BackgroundExecutor,
4301 cx_a: &mut TestAppContext,
4302 cx_b: &mut TestAppContext,
4303) {
4304 let mut server = TestServer::start(executor.clone()).await;
4305 let client_a = server.create_client(cx_a, "user_a").await;
4306 let client_b = server.create_client(cx_b, "user_b").await;
4307 server
4308 .create_room(&mut [(&client_a, cx_a), (&client_b, cx_b)])
4309 .await;
4310
4311 client_a.language_registry().add(rust_lang());
4312 let mut fake_language_servers = client_a.language_registry().register_fake_lsp(
4313 "Rust",
4314 FakeLspAdapter {
4315 disk_based_diagnostics_progress_token: Some("the-disk-based-token".into()),
4316 disk_based_diagnostics_sources: vec!["the-disk-based-diagnostics-source".into()],
4317 ..Default::default()
4318 },
4319 );
4320
4321 let file_names = &["one.rs", "two.rs", "three.rs", "four.rs", "five.rs"];
4322 client_a
4323 .fs()
4324 .insert_tree(
4325 path!("/test"),
4326 json!({
4327 "one.rs": "const ONE: usize = 1;",
4328 "two.rs": "const TWO: usize = 2;",
4329 "three.rs": "const THREE: usize = 3;",
4330 "four.rs": "const FOUR: usize = 3;",
4331 "five.rs": "const FIVE: usize = 3;",
4332 }),
4333 )
4334 .await;
4335
4336 let (project_a, worktree_id) = client_a.build_local_project(path!("/test"), cx_a).await;
4337
4338 // Share a project as client A
4339 let active_call_a = cx_a.read(ActiveCall::global);
4340 let project_id = active_call_a
4341 .update(cx_a, |call, cx| call.share_project(project_a.clone(), cx))
4342 .await
4343 .unwrap();
4344
4345 // Join the project as client B and open all three files.
4346 let project_b = client_b.join_remote_project(project_id, cx_b).await;
4347 let guest_buffers = futures::future::try_join_all(file_names.iter().map(|file_name| {
4348 project_b.update(cx_b, |p, cx| {
4349 p.open_buffer_with_lsp((worktree_id, rel_path(file_name)), cx)
4350 })
4351 }))
4352 .await
4353 .unwrap();
4354
4355 // Simulate a language server reporting errors for a file.
4356 let fake_language_server = fake_language_servers.next().await.unwrap();
4357 executor.run_until_parked();
4358 fake_language_server
4359 .request::<lsp::request::WorkDoneProgressCreate>(
4360 lsp::WorkDoneProgressCreateParams {
4361 token: lsp::NumberOrString::String("the-disk-based-token".to_string()),
4362 },
4363 DEFAULT_LSP_REQUEST_TIMEOUT,
4364 )
4365 .await
4366 .into_response()
4367 .unwrap();
4368 fake_language_server.notify::<lsp::notification::Progress>(lsp::ProgressParams {
4369 token: lsp::NumberOrString::String("the-disk-based-token".to_string()),
4370 value: lsp::ProgressParamsValue::WorkDone(lsp::WorkDoneProgress::Begin(
4371 lsp::WorkDoneProgressBegin {
4372 title: "Progress Began".into(),
4373 ..Default::default()
4374 },
4375 )),
4376 });
4377 for file_name in file_names {
4378 fake_language_server.notify::<lsp::notification::PublishDiagnostics>(
4379 lsp::PublishDiagnosticsParams {
4380 uri: lsp::Uri::from_file_path(Path::new(path!("/test")).join(file_name)).unwrap(),
4381 version: None,
4382 diagnostics: vec![lsp::Diagnostic {
4383 severity: Some(lsp::DiagnosticSeverity::WARNING),
4384 source: Some("the-disk-based-diagnostics-source".into()),
4385 range: lsp::Range::new(lsp::Position::new(0, 0), lsp::Position::new(0, 0)),
4386 message: "message one".to_string(),
4387 ..Default::default()
4388 }],
4389 },
4390 );
4391 }
4392 fake_language_server.notify::<lsp::notification::Progress>(lsp::ProgressParams {
4393 token: lsp::NumberOrString::String("the-disk-based-token".to_string()),
4394 value: lsp::ProgressParamsValue::WorkDone(lsp::WorkDoneProgress::End(
4395 lsp::WorkDoneProgressEnd { message: None },
4396 )),
4397 });
4398
4399 // When the "disk base diagnostics finished" message is received, the buffers'
4400 // diagnostics are expected to be present.
4401 let disk_based_diagnostics_finished = Arc::new(AtomicBool::new(false));
4402 project_b.update(cx_b, {
4403 let project_b = project_b.clone();
4404 let disk_based_diagnostics_finished = disk_based_diagnostics_finished.clone();
4405 move |_, cx| {
4406 cx.subscribe(&project_b, move |_, _, event, cx| {
4407 if let project::Event::DiskBasedDiagnosticsFinished { .. } = event {
4408 disk_based_diagnostics_finished.store(true, SeqCst);
4409 for (buffer, _) in &guest_buffers {
4410 assert_eq!(
4411 buffer
4412 .read(cx)
4413 .snapshot()
4414 .diagnostics_in_range::<_, usize>(0..5, false)
4415 .count(),
4416 1,
4417 "expected a diagnostic for buffer {:?}",
4418 buffer.read(cx).file().unwrap().path(),
4419 );
4420 }
4421 }
4422 })
4423 .detach();
4424 }
4425 });
4426
4427 executor.run_until_parked();
4428 assert!(disk_based_diagnostics_finished.load(SeqCst));
4429}
4430
4431#[gpui::test(iterations = 10)]
4432async fn test_reloading_buffer_manually(
4433 executor: BackgroundExecutor,
4434 cx_a: &mut TestAppContext,
4435 cx_b: &mut TestAppContext,
4436) {
4437 let mut server = TestServer::start(executor.clone()).await;
4438 let client_a = server.create_client(cx_a, "user_a").await;
4439 let client_b = server.create_client(cx_b, "user_b").await;
4440 server
4441 .create_room(&mut [(&client_a, cx_a), (&client_b, cx_b)])
4442 .await;
4443 let active_call_a = cx_a.read(ActiveCall::global);
4444
4445 client_a
4446 .fs()
4447 .insert_tree(path!("/a"), json!({ "a.rs": "let one = 1;" }))
4448 .await;
4449 let (project_a, worktree_id) = client_a.build_local_project(path!("/a"), cx_a).await;
4450 let buffer_a = project_a
4451 .update(cx_a, |p, cx| {
4452 p.open_buffer((worktree_id, rel_path("a.rs")), cx)
4453 })
4454 .await
4455 .unwrap();
4456 let project_id = active_call_a
4457 .update(cx_a, |call, cx| call.share_project(project_a.clone(), cx))
4458 .await
4459 .unwrap();
4460
4461 let project_b = client_b.join_remote_project(project_id, cx_b).await;
4462
4463 let open_buffer = project_b.update(cx_b, |p, cx| {
4464 p.open_buffer((worktree_id, rel_path("a.rs")), cx)
4465 });
4466 let buffer_b = cx_b.executor().spawn(open_buffer).await.unwrap();
4467 buffer_b.update(cx_b, |buffer, cx| {
4468 buffer.edit([(4..7, "six")], None, cx);
4469 buffer.edit([(10..11, "6")], None, cx);
4470 assert_eq!(buffer.text(), "let six = 6;");
4471 assert!(buffer.is_dirty());
4472 assert!(!buffer.has_conflict());
4473 });
4474 executor.run_until_parked();
4475
4476 buffer_a.read_with(cx_a, |buffer, _| assert_eq!(buffer.text(), "let six = 6;"));
4477
4478 client_a
4479 .fs()
4480 .save(
4481 path!("/a/a.rs").as_ref(),
4482 &Rope::from("let seven = 7;"),
4483 LineEnding::Unix,
4484 )
4485 .await
4486 .unwrap();
4487 executor.run_until_parked();
4488
4489 buffer_a.read_with(cx_a, |buffer, _| assert!(buffer.has_conflict()));
4490
4491 buffer_b.read_with(cx_b, |buffer, _| assert!(buffer.has_conflict()));
4492
4493 project_b
4494 .update(cx_b, |project, cx| {
4495 project.reload_buffers(HashSet::from_iter([buffer_b.clone()]), true, cx)
4496 })
4497 .await
4498 .unwrap();
4499
4500 buffer_a.read_with(cx_a, |buffer, _| {
4501 assert_eq!(buffer.text(), "let seven = 7;");
4502 assert!(!buffer.is_dirty());
4503 assert!(!buffer.has_conflict());
4504 });
4505
4506 buffer_b.read_with(cx_b, |buffer, _| {
4507 assert_eq!(buffer.text(), "let seven = 7;");
4508 assert!(!buffer.is_dirty());
4509 assert!(!buffer.has_conflict());
4510 });
4511
4512 buffer_a.update(cx_a, |buffer, cx| {
4513 // Undoing on the host is a no-op when the reload was initiated by the guest.
4514 buffer.undo(cx);
4515 assert_eq!(buffer.text(), "let seven = 7;");
4516 assert!(!buffer.is_dirty());
4517 assert!(!buffer.has_conflict());
4518 });
4519 buffer_b.update(cx_b, |buffer, cx| {
4520 // Undoing on the guest rolls back the buffer to before it was reloaded but the conflict gets cleared.
4521 buffer.undo(cx);
4522 assert_eq!(buffer.text(), "let six = 6;");
4523 assert!(buffer.is_dirty());
4524 assert!(!buffer.has_conflict());
4525 });
4526}
4527
4528#[gpui::test(iterations = 10)]
4529async fn test_formatting_buffer(
4530 executor: BackgroundExecutor,
4531 cx_a: &mut TestAppContext,
4532 cx_b: &mut TestAppContext,
4533) {
4534 let mut server = TestServer::start(executor.clone()).await;
4535 let client_a = server.create_client(cx_a, "user_a").await;
4536 let client_b = server.create_client(cx_b, "user_b").await;
4537 server
4538 .create_room(&mut [(&client_a, cx_a), (&client_b, cx_b)])
4539 .await;
4540 let active_call_a = cx_a.read(ActiveCall::global);
4541
4542 client_a.language_registry().add(rust_lang());
4543 let mut fake_language_servers = client_a
4544 .language_registry()
4545 .register_fake_lsp("Rust", FakeLspAdapter::default());
4546
4547 // Here we insert a fake tree with a directory that exists on disk. This is needed
4548 // because later we'll invoke a command, which requires passing a working directory
4549 // that points to a valid location on disk.
4550 let directory = env::current_dir().unwrap();
4551 client_a
4552 .fs()
4553 .insert_tree(&directory, json!({ "a.rs": "let one = \"two\"" }))
4554 .await;
4555 let (project_a, worktree_id) = client_a.build_local_project(&directory, cx_a).await;
4556 let project_id = active_call_a
4557 .update(cx_a, |call, cx| call.share_project(project_a.clone(), cx))
4558 .await
4559 .unwrap();
4560 let project_b = client_b.join_remote_project(project_id, cx_b).await;
4561
4562 let buffer_b = project_b
4563 .update(cx_b, |p, cx| {
4564 p.open_buffer((worktree_id, rel_path("a.rs")), cx)
4565 })
4566 .await
4567 .unwrap();
4568
4569 let _handle = project_b.update(cx_b, |project, cx| {
4570 project.register_buffer_with_language_servers(&buffer_b, cx)
4571 });
4572 let fake_language_server = fake_language_servers.next().await.unwrap();
4573 executor.run_until_parked();
4574 fake_language_server.set_request_handler::<lsp::request::Formatting, _, _>(|_, _| async move {
4575 Ok(Some(vec![
4576 lsp::TextEdit {
4577 range: lsp::Range::new(lsp::Position::new(0, 4), lsp::Position::new(0, 4)),
4578 new_text: "h".to_string(),
4579 },
4580 lsp::TextEdit {
4581 range: lsp::Range::new(lsp::Position::new(0, 7), lsp::Position::new(0, 7)),
4582 new_text: "y".to_string(),
4583 },
4584 ]))
4585 });
4586
4587 project_b
4588 .update(cx_b, |project, cx| {
4589 project.format(
4590 HashSet::from_iter([buffer_b.clone()]),
4591 LspFormatTarget::Buffers,
4592 true,
4593 FormatTrigger::Save,
4594 cx,
4595 )
4596 })
4597 .await
4598 .unwrap();
4599
4600 // The edits from the LSP are applied, and a final newline is added.
4601 assert_eq!(
4602 buffer_b.read_with(cx_b, |buffer, _| buffer.text()),
4603 "let honey = \"two\"\n"
4604 );
4605
4606 // There is no `awk` command on Windows.
4607 #[cfg(not(target_os = "windows"))]
4608 {
4609 // Ensure buffer can be formatted using an external command. Notice how the
4610 // host's configuration is honored as opposed to using the guest's settings.
4611 cx_a.update(|cx| {
4612 SettingsStore::update_global(cx, |store, cx| {
4613 store.update_user_settings(cx, |file| {
4614 file.project.all_languages.defaults.formatter =
4615 Some(FormatterList::Single(Formatter::External {
4616 command: "awk".into(),
4617 arguments: Some(vec!["{sub(/two/,\"{buffer_path}\")}1".to_string()]),
4618 }));
4619 });
4620 });
4621 });
4622
4623 executor.allow_parking();
4624 project_b
4625 .update(cx_b, |project, cx| {
4626 project.format(
4627 HashSet::from_iter([buffer_b.clone()]),
4628 LspFormatTarget::Buffers,
4629 true,
4630 FormatTrigger::Save,
4631 cx,
4632 )
4633 })
4634 .await
4635 .unwrap();
4636 assert_eq!(
4637 buffer_b.read_with(cx_b, |buffer, _| buffer.text()),
4638 format!("let honey = \"{}/a.rs\"\n", directory.to_str().unwrap())
4639 );
4640 }
4641}
4642
4643#[gpui::test(iterations = 10)]
4644async fn test_range_formatting_buffer(
4645 executor: BackgroundExecutor,
4646 cx_a: &mut TestAppContext,
4647 cx_b: &mut TestAppContext,
4648) {
4649 let mut server = TestServer::start(executor.clone()).await;
4650 let client_a = server.create_client(cx_a, "user_a").await;
4651 let client_b = server.create_client(cx_b, "user_b").await;
4652 server
4653 .create_room(&mut [(&client_a, cx_a), (&client_b, cx_b)])
4654 .await;
4655 let active_call_a = cx_a.read(ActiveCall::global);
4656
4657 client_a.language_registry().add(rust_lang());
4658 let mut fake_language_servers = client_a.language_registry().register_fake_lsp(
4659 "Rust",
4660 FakeLspAdapter {
4661 capabilities: lsp::ServerCapabilities {
4662 document_range_formatting_provider: Some(OneOf::Left(true)),
4663 ..Default::default()
4664 },
4665 ..Default::default()
4666 },
4667 );
4668
4669 let directory = env::current_dir().unwrap();
4670 client_a
4671 .fs()
4672 .insert_tree(&directory, json!({ "a.rs": "one\ntwo\nthree\n" }))
4673 .await;
4674 let (project_a, worktree_id) = client_a.build_local_project(&directory, cx_a).await;
4675 let project_id = active_call_a
4676 .update(cx_a, |call, cx| call.share_project(project_a.clone(), cx))
4677 .await
4678 .unwrap();
4679 let project_b = client_b.join_remote_project(project_id, cx_b).await;
4680
4681 let buffer_b = project_b
4682 .update(cx_b, |p, cx| {
4683 p.open_buffer((worktree_id, rel_path("a.rs")), cx)
4684 })
4685 .await
4686 .unwrap();
4687
4688 let _handle = project_b.update(cx_b, |project, cx| {
4689 project.register_buffer_with_language_servers(&buffer_b, cx)
4690 });
4691 let fake_language_server = fake_language_servers.next().await.unwrap();
4692 executor.run_until_parked();
4693
4694 fake_language_server.set_request_handler::<lsp::request::RangeFormatting, _, _>(
4695 |params, _| async move {
4696 assert_eq!(params.range.start, lsp::Position::new(0, 0));
4697 assert_eq!(params.range.end, lsp::Position::new(1, 3));
4698 Ok(Some(vec![lsp::TextEdit::new(
4699 lsp::Range::new(lsp::Position::new(0, 3), lsp::Position::new(1, 0)),
4700 ", ".to_string(),
4701 )]))
4702 },
4703 );
4704
4705 let buffer_id = buffer_b.read_with(cx_b, |buffer, _| buffer.remote_id());
4706 let ranges = buffer_b.read_with(cx_b, |buffer, _| {
4707 let start = buffer.anchor_before(0);
4708 let end = buffer.anchor_after(7);
4709 vec![start..end]
4710 });
4711
4712 let mut ranges_map = BTreeMap::new();
4713 ranges_map.insert(buffer_id, ranges);
4714
4715 project_b
4716 .update(cx_b, |project, cx| {
4717 project.format(
4718 HashSet::from_iter([buffer_b.clone()]),
4719 LspFormatTarget::Ranges(ranges_map),
4720 true,
4721 FormatTrigger::Manual,
4722 cx,
4723 )
4724 })
4725 .await
4726 .unwrap();
4727
4728 assert_eq!(
4729 buffer_b.read_with(cx_b, |buffer, _| buffer.text()),
4730 "one, two\nthree\n"
4731 );
4732}
4733
4734#[gpui::test(iterations = 10)]
4735async fn test_prettier_formatting_buffer(
4736 executor: BackgroundExecutor,
4737 cx_a: &mut TestAppContext,
4738 cx_b: &mut TestAppContext,
4739) {
4740 let mut server = TestServer::start(executor.clone()).await;
4741 let client_a = server.create_client(cx_a, "user_a").await;
4742 let client_b = server.create_client(cx_b, "user_b").await;
4743 server
4744 .create_room(&mut [(&client_a, cx_a), (&client_b, cx_b)])
4745 .await;
4746 let active_call_a = cx_a.read(ActiveCall::global);
4747
4748 let test_plugin = "test_plugin";
4749
4750 client_a.language_registry().add(Arc::new(Language::new(
4751 LanguageConfig {
4752 name: "TypeScript".into(),
4753 matcher: LanguageMatcher {
4754 path_suffixes: vec!["ts".to_string()],
4755 ..Default::default()
4756 },
4757 ..Default::default()
4758 },
4759 Some(tree_sitter_typescript::LANGUAGE_TYPESCRIPT.into()),
4760 )));
4761 let mut fake_language_servers = client_a.language_registry().register_fake_lsp(
4762 "TypeScript",
4763 FakeLspAdapter {
4764 prettier_plugins: vec![test_plugin],
4765 ..Default::default()
4766 },
4767 );
4768
4769 // Here we insert a fake tree with a directory that exists on disk. This is needed
4770 // because later we'll invoke a command, which requires passing a working directory
4771 // that points to a valid location on disk.
4772 let directory = env::current_dir().unwrap();
4773 let buffer_text = "let one = \"two\"";
4774 client_a
4775 .fs()
4776 .insert_tree(&directory, json!({ "a.ts": buffer_text }))
4777 .await;
4778 let (project_a, worktree_id) = client_a.build_local_project(&directory, cx_a).await;
4779 let prettier_format_suffix = project::TEST_PRETTIER_FORMAT_SUFFIX;
4780 let open_buffer = project_a.update(cx_a, |p, cx| {
4781 p.open_buffer((worktree_id, rel_path("a.ts")), cx)
4782 });
4783 let buffer_a = cx_a.executor().spawn(open_buffer).await.unwrap();
4784
4785 let project_id = active_call_a
4786 .update(cx_a, |call, cx| call.share_project(project_a.clone(), cx))
4787 .await
4788 .unwrap();
4789 let project_b = client_b.join_remote_project(project_id, cx_b).await;
4790 let (buffer_b, _) = project_b
4791 .update(cx_b, |p, cx| {
4792 p.open_buffer_with_lsp((worktree_id, rel_path("a.ts")), cx)
4793 })
4794 .await
4795 .unwrap();
4796
4797 cx_a.update(|cx| {
4798 SettingsStore::update_global(cx, |store, cx| {
4799 store.update_user_settings(cx, |file| {
4800 file.project.all_languages.defaults.formatter = Some(FormatterList::default());
4801 file.project.all_languages.defaults.prettier = Some(PrettierSettingsContent {
4802 allowed: Some(true),
4803 ..Default::default()
4804 });
4805 });
4806 });
4807 });
4808 cx_b.update(|cx| {
4809 SettingsStore::update_global(cx, |store, cx| {
4810 store.update_user_settings(cx, |file| {
4811 file.project.all_languages.defaults.formatter = Some(FormatterList::Single(
4812 Formatter::LanguageServer(LanguageServerFormatterSpecifier::Current),
4813 ));
4814 file.project.all_languages.defaults.prettier = Some(PrettierSettingsContent {
4815 allowed: Some(true),
4816 ..Default::default()
4817 });
4818 });
4819 });
4820 });
4821 let fake_language_server = fake_language_servers.next().await.unwrap();
4822 fake_language_server.set_request_handler::<lsp::request::Formatting, _, _>(|_, _| async move {
4823 panic!(
4824 "Unexpected: prettier should be preferred since it's enabled and language supports it"
4825 )
4826 });
4827
4828 project_b
4829 .update(cx_b, |project, cx| {
4830 project.format(
4831 HashSet::from_iter([buffer_b.clone()]),
4832 LspFormatTarget::Buffers,
4833 true,
4834 FormatTrigger::Save,
4835 cx,
4836 )
4837 })
4838 .await
4839 .unwrap();
4840
4841 executor.run_until_parked();
4842 assert_eq!(
4843 buffer_b.read_with(cx_b, |buffer, _| buffer.text()),
4844 buffer_text.to_string() + "\n" + prettier_format_suffix,
4845 "Prettier formatting was not applied to client buffer after client's request"
4846 );
4847
4848 project_a
4849 .update(cx_a, |project, cx| {
4850 project.format(
4851 HashSet::from_iter([buffer_a.clone()]),
4852 LspFormatTarget::Buffers,
4853 true,
4854 FormatTrigger::Manual,
4855 cx,
4856 )
4857 })
4858 .await
4859 .unwrap();
4860
4861 executor.run_until_parked();
4862 assert_eq!(
4863 buffer_b.read_with(cx_b, |buffer, _| buffer.text()),
4864 buffer_text.to_string() + "\n" + prettier_format_suffix + "\n" + prettier_format_suffix,
4865 "Prettier formatting was not applied to client buffer after host's request"
4866 );
4867}
4868
4869#[gpui::test(iterations = 10)]
4870async fn test_definition(
4871 executor: BackgroundExecutor,
4872 cx_a: &mut TestAppContext,
4873 cx_b: &mut TestAppContext,
4874) {
4875 let mut server = TestServer::start(executor.clone()).await;
4876 let client_a = server.create_client(cx_a, "user_a").await;
4877 let client_b = server.create_client(cx_b, "user_b").await;
4878 server
4879 .create_room(&mut [(&client_a, cx_a), (&client_b, cx_b)])
4880 .await;
4881 let active_call_a = cx_a.read(ActiveCall::global);
4882
4883 let capabilities = lsp::ServerCapabilities {
4884 definition_provider: Some(OneOf::Left(true)),
4885 type_definition_provider: Some(lsp::TypeDefinitionProviderCapability::Simple(true)),
4886 ..lsp::ServerCapabilities::default()
4887 };
4888 client_a.language_registry().add(rust_lang());
4889 let mut fake_language_servers = client_a.language_registry().register_fake_lsp(
4890 "Rust",
4891 FakeLspAdapter {
4892 capabilities: capabilities.clone(),
4893 ..FakeLspAdapter::default()
4894 },
4895 );
4896 client_b.language_registry().add(rust_lang());
4897 client_b.language_registry().register_fake_lsp_adapter(
4898 "Rust",
4899 FakeLspAdapter {
4900 capabilities,
4901 ..FakeLspAdapter::default()
4902 },
4903 );
4904
4905 client_a
4906 .fs()
4907 .insert_tree(
4908 path!("/root"),
4909 json!({
4910 "dir-1": {
4911 "a.rs": "const ONE: usize = b::TWO + b::THREE;",
4912 },
4913 "dir-2": {
4914 "b.rs": "const TWO: c::T2 = 2;\nconst THREE: usize = 3;",
4915 "c.rs": "type T2 = usize;",
4916 }
4917 }),
4918 )
4919 .await;
4920 let (project_a, worktree_id) = client_a
4921 .build_local_project(path!("/root/dir-1"), cx_a)
4922 .await;
4923 let project_id = active_call_a
4924 .update(cx_a, |call, cx| call.share_project(project_a.clone(), cx))
4925 .await
4926 .unwrap();
4927 let project_b = client_b.join_remote_project(project_id, cx_b).await;
4928
4929 // Open the file on client B.
4930 let (buffer_b, _handle) = project_b
4931 .update(cx_b, |p, cx| {
4932 p.open_buffer_with_lsp((worktree_id, rel_path("a.rs")), cx)
4933 })
4934 .await
4935 .unwrap();
4936
4937 // Request the definition of a symbol as the guest.
4938 let fake_language_server = fake_language_servers.next().await.unwrap();
4939 fake_language_server.set_request_handler::<lsp::request::GotoDefinition, _, _>(
4940 |_, _| async move {
4941 Ok(Some(lsp::GotoDefinitionResponse::Scalar(
4942 lsp::Location::new(
4943 lsp::Uri::from_file_path(path!("/root/dir-2/b.rs")).unwrap(),
4944 lsp::Range::new(lsp::Position::new(0, 6), lsp::Position::new(0, 9)),
4945 ),
4946 )))
4947 },
4948 );
4949 cx_a.run_until_parked();
4950 cx_b.run_until_parked();
4951
4952 let definitions_1 = project_b
4953 .update(cx_b, |p, cx| p.definitions(&buffer_b, 23, cx))
4954 .await
4955 .unwrap()
4956 .unwrap();
4957 cx_b.read(|cx| {
4958 assert_eq!(
4959 definitions_1.len(),
4960 1,
4961 "Unexpected definitions: {definitions_1:?}"
4962 );
4963 assert_eq!(project_b.read(cx).worktrees(cx).count(), 2);
4964 let target_buffer = definitions_1[0].target.buffer.read(cx);
4965 assert_eq!(
4966 target_buffer.text(),
4967 "const TWO: c::T2 = 2;\nconst THREE: usize = 3;"
4968 );
4969 assert_eq!(
4970 definitions_1[0].target.range.to_point(target_buffer),
4971 Point::new(0, 6)..Point::new(0, 9)
4972 );
4973 });
4974
4975 // Try getting more definitions for the same buffer, ensuring the buffer gets reused from
4976 // the previous call to `definition`.
4977 fake_language_server.set_request_handler::<lsp::request::GotoDefinition, _, _>(
4978 |_, _| async move {
4979 Ok(Some(lsp::GotoDefinitionResponse::Scalar(
4980 lsp::Location::new(
4981 lsp::Uri::from_file_path(path!("/root/dir-2/b.rs")).unwrap(),
4982 lsp::Range::new(lsp::Position::new(1, 6), lsp::Position::new(1, 11)),
4983 ),
4984 )))
4985 },
4986 );
4987
4988 let definitions_2 = project_b
4989 .update(cx_b, |p, cx| p.definitions(&buffer_b, 33, cx))
4990 .await
4991 .unwrap()
4992 .unwrap();
4993 cx_b.read(|cx| {
4994 assert_eq!(definitions_2.len(), 1);
4995 assert_eq!(project_b.read(cx).worktrees(cx).count(), 2);
4996 let target_buffer = definitions_2[0].target.buffer.read(cx);
4997 assert_eq!(
4998 target_buffer.text(),
4999 "const TWO: c::T2 = 2;\nconst THREE: usize = 3;"
5000 );
5001 assert_eq!(
5002 definitions_2[0].target.range.to_point(target_buffer),
5003 Point::new(1, 6)..Point::new(1, 11)
5004 );
5005 });
5006 assert_eq!(
5007 definitions_1[0].target.buffer,
5008 definitions_2[0].target.buffer
5009 );
5010
5011 fake_language_server.set_request_handler::<lsp::request::GotoTypeDefinition, _, _>(
5012 |req, _| async move {
5013 assert_eq!(
5014 req.text_document_position_params.position,
5015 lsp::Position::new(0, 7)
5016 );
5017 Ok(Some(lsp::GotoDefinitionResponse::Scalar(
5018 lsp::Location::new(
5019 lsp::Uri::from_file_path(path!("/root/dir-2/c.rs")).unwrap(),
5020 lsp::Range::new(lsp::Position::new(0, 5), lsp::Position::new(0, 7)),
5021 ),
5022 )))
5023 },
5024 );
5025
5026 let type_definitions = project_b
5027 .update(cx_b, |p, cx| p.type_definitions(&buffer_b, 7, cx))
5028 .await
5029 .unwrap()
5030 .unwrap();
5031 cx_b.read(|cx| {
5032 assert_eq!(
5033 type_definitions.len(),
5034 1,
5035 "Unexpected type definitions: {type_definitions:?}"
5036 );
5037 let target_buffer = type_definitions[0].target.buffer.read(cx);
5038 assert_eq!(target_buffer.text(), "type T2 = usize;");
5039 assert_eq!(
5040 type_definitions[0].target.range.to_point(target_buffer),
5041 Point::new(0, 5)..Point::new(0, 7)
5042 );
5043 });
5044}
5045
5046#[gpui::test(iterations = 10)]
5047async fn test_references(
5048 executor: BackgroundExecutor,
5049 cx_a: &mut TestAppContext,
5050 cx_b: &mut TestAppContext,
5051) {
5052 let mut server = TestServer::start(executor.clone()).await;
5053 let client_a = server.create_client(cx_a, "user_a").await;
5054 let client_b = server.create_client(cx_b, "user_b").await;
5055 server
5056 .create_room(&mut [(&client_a, cx_a), (&client_b, cx_b)])
5057 .await;
5058 let active_call_a = cx_a.read(ActiveCall::global);
5059
5060 let capabilities = lsp::ServerCapabilities {
5061 references_provider: Some(lsp::OneOf::Left(true)),
5062 ..lsp::ServerCapabilities::default()
5063 };
5064 client_a.language_registry().add(rust_lang());
5065 let mut fake_language_servers = client_a.language_registry().register_fake_lsp(
5066 "Rust",
5067 FakeLspAdapter {
5068 name: "my-fake-lsp-adapter",
5069 capabilities: capabilities.clone(),
5070 ..FakeLspAdapter::default()
5071 },
5072 );
5073 client_b.language_registry().add(rust_lang());
5074 client_b.language_registry().register_fake_lsp_adapter(
5075 "Rust",
5076 FakeLspAdapter {
5077 name: "my-fake-lsp-adapter",
5078 capabilities,
5079 ..FakeLspAdapter::default()
5080 },
5081 );
5082
5083 client_a
5084 .fs()
5085 .insert_tree(
5086 path!("/root"),
5087 json!({
5088 "dir-1": {
5089 "one.rs": "const ONE: usize = 1;",
5090 "two.rs": "const TWO: usize = one::ONE + one::ONE;",
5091 },
5092 "dir-2": {
5093 "three.rs": "const THREE: usize = two::TWO + one::ONE;",
5094 }
5095 }),
5096 )
5097 .await;
5098 let (project_a, worktree_id) = client_a
5099 .build_local_project(path!("/root/dir-1"), cx_a)
5100 .await;
5101 let project_id = active_call_a
5102 .update(cx_a, |call, cx| call.share_project(project_a.clone(), cx))
5103 .await
5104 .unwrap();
5105 let project_b = client_b.join_remote_project(project_id, cx_b).await;
5106
5107 // Open the file on client B.
5108 let (buffer_b, _handle) = project_b
5109 .update(cx_b, |p, cx| {
5110 p.open_buffer_with_lsp((worktree_id, rel_path("one.rs")), cx)
5111 })
5112 .await
5113 .unwrap();
5114
5115 // Request references to a symbol as the guest.
5116 let fake_language_server = fake_language_servers.next().await.unwrap();
5117 let (lsp_response_tx, rx) = mpsc::unbounded::<Result<Option<Vec<lsp::Location>>>>();
5118 fake_language_server.set_request_handler::<lsp::request::References, _, _>({
5119 let rx = Arc::new(Mutex::new(Some(rx)));
5120 move |params, _| {
5121 assert_eq!(
5122 params.text_document_position.text_document.uri.as_str(),
5123 uri!("file:///root/dir-1/one.rs")
5124 );
5125 let rx = rx.clone();
5126 async move {
5127 let mut response_rx = rx.lock().take().unwrap();
5128 let result = response_rx.next().await.unwrap();
5129 *rx.lock() = Some(response_rx);
5130 result
5131 }
5132 }
5133 });
5134 cx_a.run_until_parked();
5135 cx_b.run_until_parked();
5136
5137 let references = project_b.update(cx_b, |p, cx| p.references(&buffer_b, 7, cx));
5138
5139 // User is informed that a request is pending.
5140 executor.run_until_parked();
5141 project_b.read_with(cx_b, |project, cx| {
5142 let status = project.language_server_statuses(cx).next().unwrap().1;
5143 assert_eq!(status.name.0, "my-fake-lsp-adapter");
5144 assert_eq!(
5145 status.pending_work.values().next().unwrap().message,
5146 Some("Finding references...".into())
5147 );
5148 });
5149
5150 // Cause the language server to respond.
5151 lsp_response_tx
5152 .unbounded_send(Ok(Some(vec![
5153 lsp::Location {
5154 uri: lsp::Uri::from_file_path(path!("/root/dir-1/two.rs")).unwrap(),
5155 range: lsp::Range::new(lsp::Position::new(0, 24), lsp::Position::new(0, 27)),
5156 },
5157 lsp::Location {
5158 uri: lsp::Uri::from_file_path(path!("/root/dir-1/two.rs")).unwrap(),
5159 range: lsp::Range::new(lsp::Position::new(0, 35), lsp::Position::new(0, 38)),
5160 },
5161 lsp::Location {
5162 uri: lsp::Uri::from_file_path(path!("/root/dir-2/three.rs")).unwrap(),
5163 range: lsp::Range::new(lsp::Position::new(0, 37), lsp::Position::new(0, 40)),
5164 },
5165 ])))
5166 .unwrap();
5167
5168 let references = references.await.unwrap().unwrap();
5169 executor.run_until_parked();
5170 project_b.read_with(cx_b, |project, cx| {
5171 // User is informed that a request is no longer pending.
5172 let status = project.language_server_statuses(cx).next().unwrap().1;
5173 assert!(status.pending_work.is_empty());
5174
5175 assert_eq!(references.len(), 3);
5176 assert_eq!(project.worktrees(cx).count(), 2);
5177
5178 let two_buffer = references[0].buffer.read(cx);
5179 let three_buffer = references[2].buffer.read(cx);
5180 assert_eq!(
5181 two_buffer.file().unwrap().path().as_ref(),
5182 rel_path("two.rs")
5183 );
5184 assert_eq!(references[1].buffer, references[0].buffer);
5185 assert_eq!(
5186 three_buffer.file().unwrap().full_path(cx),
5187 Path::new(path!("/root/dir-2/three.rs"))
5188 );
5189
5190 assert_eq!(references[0].range.to_offset(two_buffer), 24..27);
5191 assert_eq!(references[1].range.to_offset(two_buffer), 35..38);
5192 assert_eq!(references[2].range.to_offset(three_buffer), 37..40);
5193 });
5194
5195 let references = project_b.update(cx_b, |p, cx| p.references(&buffer_b, 7, cx));
5196
5197 // User is informed that a request is pending.
5198 executor.run_until_parked();
5199 project_b.read_with(cx_b, |project, cx| {
5200 let status = project.language_server_statuses(cx).next().unwrap().1;
5201 assert_eq!(status.name.0, "my-fake-lsp-adapter");
5202 assert_eq!(
5203 status.pending_work.values().next().unwrap().message,
5204 Some("Finding references...".into())
5205 );
5206 });
5207
5208 // Cause the LSP request to fail.
5209 lsp_response_tx
5210 .unbounded_send(Err(anyhow!("can't find references")))
5211 .unwrap();
5212 assert_eq!(references.await.unwrap().unwrap(), []);
5213
5214 // User is informed that the request is no longer pending.
5215 executor.run_until_parked();
5216 project_b.read_with(cx_b, |project, cx| {
5217 let status = project.language_server_statuses(cx).next().unwrap().1;
5218 assert!(status.pending_work.is_empty());
5219 });
5220}
5221
5222#[gpui::test(iterations = 10)]
5223async fn test_project_search(
5224 executor: BackgroundExecutor,
5225 cx_a: &mut TestAppContext,
5226 cx_b: &mut TestAppContext,
5227) {
5228 let mut server = TestServer::start(executor.clone()).await;
5229 let client_a = server.create_client(cx_a, "user_a").await;
5230 let client_b = server.create_client(cx_b, "user_b").await;
5231 server
5232 .create_room(&mut [(&client_a, cx_a), (&client_b, cx_b)])
5233 .await;
5234 let active_call_a = cx_a.read(ActiveCall::global);
5235
5236 client_a
5237 .fs()
5238 .insert_tree(
5239 "/root",
5240 json!({
5241 "dir-1": {
5242 "a": "hello world",
5243 "b": "goodnight moon",
5244 "c": "a world of goo",
5245 "d": "world champion of clown world",
5246 },
5247 "dir-2": {
5248 "e": "disney world is fun",
5249 }
5250 }),
5251 )
5252 .await;
5253 let (project_a, _) = client_a.build_local_project("/root/dir-1", cx_a).await;
5254 let (worktree_2, _) = project_a
5255 .update(cx_a, |p, cx| {
5256 p.find_or_create_worktree("/root/dir-2", true, cx)
5257 })
5258 .await
5259 .unwrap();
5260 worktree_2
5261 .read_with(cx_a, |tree, _| tree.as_local().unwrap().scan_complete())
5262 .await;
5263 let project_id = active_call_a
5264 .update(cx_a, |call, cx| call.share_project(project_a.clone(), cx))
5265 .await
5266 .unwrap();
5267
5268 let project_b = client_b.join_remote_project(project_id, cx_b).await;
5269
5270 // Perform a search as the guest.
5271 let mut results = HashMap::default();
5272 let search_rx = project_b.update(cx_b, |project, cx| {
5273 project.search(
5274 SearchQuery::text(
5275 "world",
5276 false,
5277 false,
5278 false,
5279 Default::default(),
5280 Default::default(),
5281 false,
5282 None,
5283 )
5284 .unwrap(),
5285 cx,
5286 )
5287 });
5288 while let Ok(result) = search_rx.rx.recv().await {
5289 match result {
5290 SearchResult::Buffer { buffer, ranges } => {
5291 results.entry(buffer).or_insert(ranges);
5292 }
5293 SearchResult::LimitReached => {
5294 panic!(
5295 "Unexpectedly reached search limit in tests. If you do want to assert limit-reached, change this panic call."
5296 )
5297 }
5298 SearchResult::WaitingForScan => {}
5299 };
5300 }
5301
5302 let mut ranges_by_path = results
5303 .into_iter()
5304 .map(|(buffer, ranges)| {
5305 buffer.read_with(cx_b, |buffer, cx| {
5306 let path = buffer.file().unwrap().full_path(cx);
5307 let offset_ranges = ranges
5308 .into_iter()
5309 .map(|range| range.to_offset(buffer))
5310 .collect::<Vec<_>>();
5311 (path, offset_ranges)
5312 })
5313 })
5314 .collect::<Vec<_>>();
5315 ranges_by_path.sort_by_key(|(path, _)| path.clone());
5316
5317 assert_eq!(
5318 ranges_by_path,
5319 &[
5320 (PathBuf::from("dir-1/a"), vec![6..11]),
5321 (PathBuf::from("dir-1/c"), vec![2..7]),
5322 (PathBuf::from("dir-1/d"), vec![0..5, 24..29]),
5323 (PathBuf::from("dir-2/e"), vec![7..12]),
5324 ]
5325 );
5326}
5327
5328#[gpui::test(iterations = 10)]
5329async fn test_document_highlights(
5330 executor: BackgroundExecutor,
5331 cx_a: &mut TestAppContext,
5332 cx_b: &mut TestAppContext,
5333) {
5334 let mut server = TestServer::start(executor.clone()).await;
5335 let client_a = server.create_client(cx_a, "user_a").await;
5336 let client_b = server.create_client(cx_b, "user_b").await;
5337 server
5338 .create_room(&mut [(&client_a, cx_a), (&client_b, cx_b)])
5339 .await;
5340 let active_call_a = cx_a.read(ActiveCall::global);
5341
5342 client_a
5343 .fs()
5344 .insert_tree(
5345 path!("/root-1"),
5346 json!({
5347 "main.rs": "fn double(number: i32) -> i32 { number + number }",
5348 }),
5349 )
5350 .await;
5351
5352 client_a.language_registry().add(rust_lang());
5353 let capabilities = lsp::ServerCapabilities {
5354 document_highlight_provider: Some(lsp::OneOf::Left(true)),
5355 ..lsp::ServerCapabilities::default()
5356 };
5357 let mut fake_language_servers = client_a.language_registry().register_fake_lsp(
5358 "Rust",
5359 FakeLspAdapter {
5360 capabilities: capabilities.clone(),
5361 ..FakeLspAdapter::default()
5362 },
5363 );
5364 client_b.language_registry().add(rust_lang());
5365 client_b.language_registry().register_fake_lsp_adapter(
5366 "Rust",
5367 FakeLspAdapter {
5368 capabilities,
5369 ..FakeLspAdapter::default()
5370 },
5371 );
5372
5373 let (project_a, worktree_id) = client_a.build_local_project(path!("/root-1"), cx_a).await;
5374 let project_id = active_call_a
5375 .update(cx_a, |call, cx| call.share_project(project_a.clone(), cx))
5376 .await
5377 .unwrap();
5378 let project_b = client_b.join_remote_project(project_id, cx_b).await;
5379
5380 // Open the file on client B.
5381 let (buffer_b, _handle) = project_b
5382 .update(cx_b, |p, cx| {
5383 p.open_buffer_with_lsp((worktree_id, rel_path("main.rs")), cx)
5384 })
5385 .await
5386 .unwrap();
5387
5388 // Request document highlights as the guest.
5389 let fake_language_server = fake_language_servers.next().await.unwrap();
5390 fake_language_server.set_request_handler::<lsp::request::DocumentHighlightRequest, _, _>(
5391 |params, _| async move {
5392 assert_eq!(
5393 params
5394 .text_document_position_params
5395 .text_document
5396 .uri
5397 .as_str(),
5398 uri!("file:///root-1/main.rs")
5399 );
5400 assert_eq!(
5401 params.text_document_position_params.position,
5402 lsp::Position::new(0, 34)
5403 );
5404 Ok(Some(vec![
5405 lsp::DocumentHighlight {
5406 kind: Some(lsp::DocumentHighlightKind::WRITE),
5407 range: lsp::Range::new(lsp::Position::new(0, 10), lsp::Position::new(0, 16)),
5408 },
5409 lsp::DocumentHighlight {
5410 kind: Some(lsp::DocumentHighlightKind::READ),
5411 range: lsp::Range::new(lsp::Position::new(0, 32), lsp::Position::new(0, 38)),
5412 },
5413 lsp::DocumentHighlight {
5414 kind: Some(lsp::DocumentHighlightKind::READ),
5415 range: lsp::Range::new(lsp::Position::new(0, 41), lsp::Position::new(0, 47)),
5416 },
5417 ]))
5418 },
5419 );
5420 cx_a.run_until_parked();
5421 cx_b.run_until_parked();
5422
5423 let highlights = project_b
5424 .update(cx_b, |p, cx| p.document_highlights(&buffer_b, 34, cx))
5425 .await
5426 .unwrap();
5427
5428 buffer_b.read_with(cx_b, |buffer, _| {
5429 let snapshot = buffer.snapshot();
5430
5431 let highlights = highlights
5432 .into_iter()
5433 .map(|highlight| (highlight.kind, highlight.range.to_offset(&snapshot)))
5434 .collect::<Vec<_>>();
5435 assert_eq!(
5436 highlights,
5437 &[
5438 (lsp::DocumentHighlightKind::WRITE, 10..16),
5439 (lsp::DocumentHighlightKind::READ, 32..38),
5440 (lsp::DocumentHighlightKind::READ, 41..47)
5441 ]
5442 )
5443 });
5444}
5445
5446#[gpui::test(iterations = 10)]
5447async fn test_lsp_hover(
5448 executor: BackgroundExecutor,
5449 cx_a: &mut TestAppContext,
5450 cx_b: &mut TestAppContext,
5451) {
5452 let mut server = TestServer::start(executor.clone()).await;
5453 let client_a = server.create_client(cx_a, "user_a").await;
5454 let client_b = server.create_client(cx_b, "user_b").await;
5455 server
5456 .create_room(&mut [(&client_a, cx_a), (&client_b, cx_b)])
5457 .await;
5458 let active_call_a = cx_a.read(ActiveCall::global);
5459
5460 client_a
5461 .fs()
5462 .insert_tree(
5463 path!("/root-1"),
5464 json!({
5465 "main.rs": "use std::collections::HashMap;",
5466 }),
5467 )
5468 .await;
5469
5470 client_a.language_registry().add(rust_lang());
5471 let language_server_names = ["rust-analyzer", "CrabLang-ls"];
5472 let capabilities_1 = lsp::ServerCapabilities {
5473 hover_provider: Some(lsp::HoverProviderCapability::Simple(true)),
5474 ..lsp::ServerCapabilities::default()
5475 };
5476 let capabilities_2 = lsp::ServerCapabilities {
5477 hover_provider: Some(lsp::HoverProviderCapability::Simple(true)),
5478 ..lsp::ServerCapabilities::default()
5479 };
5480 let mut language_servers = [
5481 client_a.language_registry().register_fake_lsp(
5482 "Rust",
5483 FakeLspAdapter {
5484 name: language_server_names[0],
5485 capabilities: capabilities_1.clone(),
5486 ..FakeLspAdapter::default()
5487 },
5488 ),
5489 client_a.language_registry().register_fake_lsp(
5490 "Rust",
5491 FakeLspAdapter {
5492 name: language_server_names[1],
5493 capabilities: capabilities_2.clone(),
5494 ..FakeLspAdapter::default()
5495 },
5496 ),
5497 ];
5498 client_b.language_registry().add(rust_lang());
5499 client_b.language_registry().register_fake_lsp_adapter(
5500 "Rust",
5501 FakeLspAdapter {
5502 name: language_server_names[0],
5503 capabilities: capabilities_1,
5504 ..FakeLspAdapter::default()
5505 },
5506 );
5507 client_b.language_registry().register_fake_lsp_adapter(
5508 "Rust",
5509 FakeLspAdapter {
5510 name: language_server_names[1],
5511 capabilities: capabilities_2,
5512 ..FakeLspAdapter::default()
5513 },
5514 );
5515
5516 let (project_a, worktree_id) = client_a.build_local_project(path!("/root-1"), cx_a).await;
5517 let project_id = active_call_a
5518 .update(cx_a, |call, cx| call.share_project(project_a.clone(), cx))
5519 .await
5520 .unwrap();
5521 let project_b = client_b.join_remote_project(project_id, cx_b).await;
5522
5523 // Open the file as the guest
5524 let (buffer_b, _handle) = project_b
5525 .update(cx_b, |p, cx| {
5526 p.open_buffer_with_lsp((worktree_id, rel_path("main.rs")), cx)
5527 })
5528 .await
5529 .unwrap();
5530
5531 let mut servers_with_hover_requests = HashMap::default();
5532 for i in 0..language_server_names.len() {
5533 let new_server = language_servers[i].next().await.unwrap_or_else(|| {
5534 panic!(
5535 "Failed to get language server #{i} with name {}",
5536 &language_server_names[i]
5537 )
5538 });
5539 let new_server_name = new_server.server.name();
5540 assert!(
5541 !servers_with_hover_requests.contains_key(&new_server_name),
5542 "Unexpected: initialized server with the same name twice. Name: `{new_server_name}`"
5543 );
5544 match new_server_name.as_ref() {
5545 "CrabLang-ls" => {
5546 servers_with_hover_requests.insert(
5547 new_server_name.clone(),
5548 new_server.set_request_handler::<lsp::request::HoverRequest, _, _>(
5549 move |params, _| {
5550 assert_eq!(
5551 params
5552 .text_document_position_params
5553 .text_document
5554 .uri
5555 .as_str(),
5556 uri!("file:///root-1/main.rs")
5557 );
5558 let name = new_server_name.clone();
5559 async move {
5560 Ok(Some(lsp::Hover {
5561 contents: lsp::HoverContents::Scalar(
5562 lsp::MarkedString::String(format!("{name} hover")),
5563 ),
5564 range: None,
5565 }))
5566 }
5567 },
5568 ),
5569 );
5570 }
5571 "rust-analyzer" => {
5572 servers_with_hover_requests.insert(
5573 new_server_name.clone(),
5574 new_server.set_request_handler::<lsp::request::HoverRequest, _, _>(
5575 |params, _| async move {
5576 assert_eq!(
5577 params
5578 .text_document_position_params
5579 .text_document
5580 .uri
5581 .as_str(),
5582 uri!("file:///root-1/main.rs")
5583 );
5584 assert_eq!(
5585 params.text_document_position_params.position,
5586 lsp::Position::new(0, 22)
5587 );
5588 Ok(Some(lsp::Hover {
5589 contents: lsp::HoverContents::Array(vec![
5590 lsp::MarkedString::String("Test hover content.".to_string()),
5591 lsp::MarkedString::LanguageString(lsp::LanguageString {
5592 language: "Rust".to_string(),
5593 value: "let foo = 42;".to_string(),
5594 }),
5595 ]),
5596 range: Some(lsp::Range::new(
5597 lsp::Position::new(0, 22),
5598 lsp::Position::new(0, 29),
5599 )),
5600 }))
5601 },
5602 ),
5603 );
5604 }
5605 unexpected => panic!("Unexpected server name: {unexpected}"),
5606 }
5607 }
5608 cx_a.run_until_parked();
5609 cx_b.run_until_parked();
5610
5611 // Request hover information as the guest.
5612 let mut hovers = project_b
5613 .update(cx_b, |p, cx| p.hover(&buffer_b, 22, cx))
5614 .await
5615 .unwrap();
5616 assert_eq!(
5617 hovers.len(),
5618 2,
5619 "Expected two hovers from both language servers, but got: {hovers:?}"
5620 );
5621
5622 let _: Vec<()> = futures::future::join_all(servers_with_hover_requests.into_values().map(
5623 |mut hover_request| async move {
5624 hover_request
5625 .next()
5626 .await
5627 .expect("All hover requests should have been triggered")
5628 },
5629 ))
5630 .await;
5631
5632 hovers.sort_by_key(|hover| hover.contents.len());
5633 let first_hover = hovers.first().cloned().unwrap();
5634 assert_eq!(
5635 first_hover.contents,
5636 vec![project::HoverBlock {
5637 text: "CrabLang-ls hover".to_string(),
5638 kind: HoverBlockKind::Markdown,
5639 },]
5640 );
5641 let second_hover = hovers.last().cloned().unwrap();
5642 assert_eq!(
5643 second_hover.contents,
5644 vec![
5645 project::HoverBlock {
5646 text: "Test hover content.".to_string(),
5647 kind: HoverBlockKind::Markdown,
5648 },
5649 project::HoverBlock {
5650 text: "let foo = 42;".to_string(),
5651 kind: HoverBlockKind::Code {
5652 language: "Rust".to_string()
5653 },
5654 }
5655 ]
5656 );
5657 buffer_b.read_with(cx_b, |buffer, _| {
5658 let snapshot = buffer.snapshot();
5659 assert_eq!(second_hover.range.unwrap().to_offset(&snapshot), 22..29);
5660 });
5661}
5662
5663#[gpui::test(iterations = 10)]
5664async fn test_project_symbols(
5665 executor: BackgroundExecutor,
5666 cx_a: &mut TestAppContext,
5667 cx_b: &mut TestAppContext,
5668) {
5669 let mut server = TestServer::start(executor.clone()).await;
5670 let client_a = server.create_client(cx_a, "user_a").await;
5671 let client_b = server.create_client(cx_b, "user_b").await;
5672 server
5673 .create_room(&mut [(&client_a, cx_a), (&client_b, cx_b)])
5674 .await;
5675 let active_call_a = cx_a.read(ActiveCall::global);
5676
5677 client_a.language_registry().add(rust_lang());
5678 let mut fake_language_servers = client_a.language_registry().register_fake_lsp(
5679 "Rust",
5680 FakeLspAdapter {
5681 capabilities: lsp::ServerCapabilities {
5682 workspace_symbol_provider: Some(OneOf::Left(true)),
5683 ..Default::default()
5684 },
5685 ..Default::default()
5686 },
5687 );
5688
5689 client_a
5690 .fs()
5691 .insert_tree(
5692 path!("/code"),
5693 json!({
5694 "crate-1": {
5695 "one.rs": "const ONE: usize = 1;",
5696 },
5697 "crate-2": {
5698 "two.rs": "const TWO: usize = 2; const THREE: usize = 3;",
5699 },
5700 "private": {
5701 "passwords.txt": "the-password",
5702 }
5703 }),
5704 )
5705 .await;
5706 let (project_a, worktree_id) = client_a
5707 .build_local_project(path!("/code/crate-1"), cx_a)
5708 .await;
5709 let project_id = active_call_a
5710 .update(cx_a, |call, cx| call.share_project(project_a.clone(), cx))
5711 .await
5712 .unwrap();
5713 let project_b = client_b.join_remote_project(project_id, cx_b).await;
5714
5715 // Cause the language server to start.
5716 let _buffer = project_b
5717 .update(cx_b, |p, cx| {
5718 p.open_buffer_with_lsp((worktree_id, rel_path("one.rs")), cx)
5719 })
5720 .await
5721 .unwrap();
5722
5723 let fake_language_server = fake_language_servers.next().await.unwrap();
5724 executor.run_until_parked();
5725 fake_language_server.set_request_handler::<lsp::WorkspaceSymbolRequest, _, _>(
5726 |_, _| async move {
5727 Ok(Some(lsp::WorkspaceSymbolResponse::Flat(vec![
5728 #[allow(deprecated)]
5729 lsp::SymbolInformation {
5730 name: "TWO".into(),
5731 location: lsp::Location {
5732 uri: lsp::Uri::from_file_path(path!("/code/crate-2/two.rs")).unwrap(),
5733 range: lsp::Range::new(lsp::Position::new(0, 6), lsp::Position::new(0, 9)),
5734 },
5735 kind: lsp::SymbolKind::CONSTANT,
5736 tags: None,
5737 container_name: None,
5738 deprecated: None,
5739 },
5740 ])))
5741 },
5742 );
5743
5744 // Request the definition of a symbol as the guest.
5745 let symbols = project_b
5746 .update(cx_b, |p, cx| p.symbols("two", cx))
5747 .await
5748 .unwrap();
5749 assert_eq!(symbols.len(), 1);
5750 assert_eq!(symbols[0].name, "TWO");
5751
5752 // Open one of the returned symbols.
5753 let buffer_b_2 = project_b
5754 .update(cx_b, |project, cx| {
5755 project.open_buffer_for_symbol(&symbols[0], cx)
5756 })
5757 .await
5758 .unwrap();
5759
5760 buffer_b_2.read_with(cx_b, |buffer, cx| {
5761 assert_eq!(
5762 buffer.file().unwrap().full_path(cx),
5763 Path::new(path!("/code/crate-2/two.rs"))
5764 );
5765 });
5766
5767 // Attempt to craft a symbol and violate host's privacy by opening an arbitrary file.
5768 let mut fake_symbol = symbols[0].clone();
5769 fake_symbol.path = SymbolLocation::OutsideProject {
5770 abs_path: Path::new(path!("/code/secrets")).into(),
5771 signature: [0x17; 32],
5772 };
5773 let error = project_b
5774 .update(cx_b, |project, cx| {
5775 project.open_buffer_for_symbol(&fake_symbol, cx)
5776 })
5777 .await
5778 .unwrap_err();
5779 assert!(error.to_string().contains("invalid symbol signature"));
5780}
5781
5782#[gpui::test(iterations = 10)]
5783async fn test_open_buffer_while_getting_definition_pointing_to_it(
5784 executor: BackgroundExecutor,
5785 cx_a: &mut TestAppContext,
5786 cx_b: &mut TestAppContext,
5787 mut rng: StdRng,
5788) {
5789 let mut server = TestServer::start(executor.clone()).await;
5790 let client_a = server.create_client(cx_a, "user_a").await;
5791 let client_b = server.create_client(cx_b, "user_b").await;
5792 server
5793 .create_room(&mut [(&client_a, cx_a), (&client_b, cx_b)])
5794 .await;
5795 let active_call_a = cx_a.read(ActiveCall::global);
5796
5797 let capabilities = lsp::ServerCapabilities {
5798 definition_provider: Some(OneOf::Left(true)),
5799 ..lsp::ServerCapabilities::default()
5800 };
5801 client_a.language_registry().add(rust_lang());
5802 let mut fake_language_servers = client_a.language_registry().register_fake_lsp(
5803 "Rust",
5804 FakeLspAdapter {
5805 capabilities: capabilities.clone(),
5806 ..FakeLspAdapter::default()
5807 },
5808 );
5809 client_b.language_registry().add(rust_lang());
5810 client_b.language_registry().register_fake_lsp_adapter(
5811 "Rust",
5812 FakeLspAdapter {
5813 capabilities,
5814 ..FakeLspAdapter::default()
5815 },
5816 );
5817
5818 client_a
5819 .fs()
5820 .insert_tree(
5821 path!("/root"),
5822 json!({
5823 "a.rs": "const ONE: usize = b::TWO;",
5824 "b.rs": "const TWO: usize = 2",
5825 }),
5826 )
5827 .await;
5828 let (project_a, worktree_id) = client_a.build_local_project(path!("/root"), cx_a).await;
5829 let project_id = active_call_a
5830 .update(cx_a, |call, cx| call.share_project(project_a.clone(), cx))
5831 .await
5832 .unwrap();
5833 let project_b = client_b.join_remote_project(project_id, cx_b).await;
5834
5835 let (buffer_b1, _lsp) = project_b
5836 .update(cx_b, |p, cx| {
5837 p.open_buffer_with_lsp((worktree_id, rel_path("a.rs")), cx)
5838 })
5839 .await
5840 .unwrap();
5841
5842 let fake_language_server = fake_language_servers.next().await.unwrap();
5843 fake_language_server.set_request_handler::<lsp::request::GotoDefinition, _, _>(
5844 |_, _| async move {
5845 Ok(Some(lsp::GotoDefinitionResponse::Scalar(
5846 lsp::Location::new(
5847 lsp::Uri::from_file_path(path!("/root/b.rs")).unwrap(),
5848 lsp::Range::new(lsp::Position::new(0, 6), lsp::Position::new(0, 9)),
5849 ),
5850 )))
5851 },
5852 );
5853
5854 let definitions;
5855 let buffer_b2;
5856 if rng.random() {
5857 cx_a.run_until_parked();
5858 cx_b.run_until_parked();
5859 definitions = project_b.update(cx_b, |p, cx| p.definitions(&buffer_b1, 23, cx));
5860 (buffer_b2, _) = project_b
5861 .update(cx_b, |p, cx| {
5862 p.open_buffer_with_lsp((worktree_id, rel_path("b.rs")), cx)
5863 })
5864 .await
5865 .unwrap();
5866 } else {
5867 (buffer_b2, _) = project_b
5868 .update(cx_b, |p, cx| {
5869 p.open_buffer_with_lsp((worktree_id, rel_path("b.rs")), cx)
5870 })
5871 .await
5872 .unwrap();
5873 cx_a.run_until_parked();
5874 cx_b.run_until_parked();
5875 definitions = project_b.update(cx_b, |p, cx| p.definitions(&buffer_b1, 23, cx));
5876 }
5877
5878 let definitions = definitions.await.unwrap().unwrap();
5879 assert_eq!(
5880 definitions.len(),
5881 1,
5882 "Unexpected definitions: {definitions:?}"
5883 );
5884 assert_eq!(definitions[0].target.buffer, buffer_b2);
5885}
5886
5887#[gpui::test(iterations = 10)]
5888async fn test_contacts(
5889 executor: BackgroundExecutor,
5890 cx_a: &mut TestAppContext,
5891 cx_b: &mut TestAppContext,
5892 cx_c: &mut TestAppContext,
5893 cx_d: &mut TestAppContext,
5894) {
5895 let mut server = TestServer::start(executor.clone()).await;
5896 let client_a = server.create_client(cx_a, "user_a").await;
5897 let client_b = server.create_client(cx_b, "user_b").await;
5898 let client_c = server.create_client(cx_c, "user_c").await;
5899 let client_d = server.create_client(cx_d, "user_d").await;
5900 server
5901 .make_contacts(&mut [(&client_a, cx_a), (&client_b, cx_b), (&client_c, cx_c)])
5902 .await;
5903 let active_call_a = cx_a.read(ActiveCall::global);
5904 let active_call_b = cx_b.read(ActiveCall::global);
5905 let active_call_c = cx_c.read(ActiveCall::global);
5906 let _active_call_d = cx_d.read(ActiveCall::global);
5907
5908 executor.run_until_parked();
5909 assert_eq!(
5910 contacts(&client_a, cx_a),
5911 [
5912 ("user_b".to_string(), "online", "free"),
5913 ("user_c".to_string(), "online", "free")
5914 ]
5915 );
5916 assert_eq!(
5917 contacts(&client_b, cx_b),
5918 [
5919 ("user_a".to_string(), "online", "free"),
5920 ("user_c".to_string(), "online", "free")
5921 ]
5922 );
5923 assert_eq!(
5924 contacts(&client_c, cx_c),
5925 [
5926 ("user_a".to_string(), "online", "free"),
5927 ("user_b".to_string(), "online", "free")
5928 ]
5929 );
5930 assert_eq!(contacts(&client_d, cx_d), []);
5931
5932 server.disconnect_client(client_c.peer_id().unwrap());
5933 server.forbid_connections();
5934 executor.advance_clock(RECEIVE_TIMEOUT + RECONNECT_TIMEOUT);
5935 assert_eq!(
5936 contacts(&client_a, cx_a),
5937 [
5938 ("user_b".to_string(), "online", "free"),
5939 ("user_c".to_string(), "offline", "free")
5940 ]
5941 );
5942 assert_eq!(
5943 contacts(&client_b, cx_b),
5944 [
5945 ("user_a".to_string(), "online", "free"),
5946 ("user_c".to_string(), "offline", "free")
5947 ]
5948 );
5949 assert_eq!(contacts(&client_c, cx_c), []);
5950 assert_eq!(contacts(&client_d, cx_d), []);
5951
5952 server.allow_connections();
5953 client_c
5954 .connect(false, &cx_c.to_async())
5955 .await
5956 .into_response()
5957 .unwrap();
5958
5959 executor.run_until_parked();
5960 assert_eq!(
5961 contacts(&client_a, cx_a),
5962 [
5963 ("user_b".to_string(), "online", "free"),
5964 ("user_c".to_string(), "online", "free")
5965 ]
5966 );
5967 assert_eq!(
5968 contacts(&client_b, cx_b),
5969 [
5970 ("user_a".to_string(), "online", "free"),
5971 ("user_c".to_string(), "online", "free")
5972 ]
5973 );
5974 assert_eq!(
5975 contacts(&client_c, cx_c),
5976 [
5977 ("user_a".to_string(), "online", "free"),
5978 ("user_b".to_string(), "online", "free")
5979 ]
5980 );
5981 assert_eq!(contacts(&client_d, cx_d), []);
5982
5983 active_call_a
5984 .update(cx_a, |call, cx| {
5985 call.invite(client_b.user_id().unwrap(), None, cx)
5986 })
5987 .await
5988 .unwrap();
5989 executor.run_until_parked();
5990 assert_eq!(
5991 contacts(&client_a, cx_a),
5992 [
5993 ("user_b".to_string(), "online", "busy"),
5994 ("user_c".to_string(), "online", "free")
5995 ]
5996 );
5997 assert_eq!(
5998 contacts(&client_b, cx_b),
5999 [
6000 ("user_a".to_string(), "online", "busy"),
6001 ("user_c".to_string(), "online", "free")
6002 ]
6003 );
6004 assert_eq!(
6005 contacts(&client_c, cx_c),
6006 [
6007 ("user_a".to_string(), "online", "busy"),
6008 ("user_b".to_string(), "online", "busy")
6009 ]
6010 );
6011 assert_eq!(contacts(&client_d, cx_d), []);
6012
6013 // Client B and client D become contacts while client B is being called.
6014 server
6015 .make_contacts(&mut [(&client_b, cx_b), (&client_d, cx_d)])
6016 .await;
6017 executor.run_until_parked();
6018 assert_eq!(
6019 contacts(&client_a, cx_a),
6020 [
6021 ("user_b".to_string(), "online", "busy"),
6022 ("user_c".to_string(), "online", "free")
6023 ]
6024 );
6025 assert_eq!(
6026 contacts(&client_b, cx_b),
6027 [
6028 ("user_a".to_string(), "online", "busy"),
6029 ("user_c".to_string(), "online", "free"),
6030 ("user_d".to_string(), "online", "free"),
6031 ]
6032 );
6033 assert_eq!(
6034 contacts(&client_c, cx_c),
6035 [
6036 ("user_a".to_string(), "online", "busy"),
6037 ("user_b".to_string(), "online", "busy")
6038 ]
6039 );
6040 assert_eq!(
6041 contacts(&client_d, cx_d),
6042 [("user_b".to_string(), "online", "busy")]
6043 );
6044
6045 active_call_b.update(cx_b, |call, cx| call.decline_incoming(cx).unwrap());
6046 executor.run_until_parked();
6047 assert_eq!(
6048 contacts(&client_a, cx_a),
6049 [
6050 ("user_b".to_string(), "online", "free"),
6051 ("user_c".to_string(), "online", "free")
6052 ]
6053 );
6054 assert_eq!(
6055 contacts(&client_b, cx_b),
6056 [
6057 ("user_a".to_string(), "online", "free"),
6058 ("user_c".to_string(), "online", "free"),
6059 ("user_d".to_string(), "online", "free")
6060 ]
6061 );
6062 assert_eq!(
6063 contacts(&client_c, cx_c),
6064 [
6065 ("user_a".to_string(), "online", "free"),
6066 ("user_b".to_string(), "online", "free")
6067 ]
6068 );
6069 assert_eq!(
6070 contacts(&client_d, cx_d),
6071 [("user_b".to_string(), "online", "free")]
6072 );
6073
6074 active_call_c
6075 .update(cx_c, |call, cx| {
6076 call.invite(client_a.user_id().unwrap(), None, cx)
6077 })
6078 .await
6079 .unwrap();
6080 executor.run_until_parked();
6081 assert_eq!(
6082 contacts(&client_a, cx_a),
6083 [
6084 ("user_b".to_string(), "online", "free"),
6085 ("user_c".to_string(), "online", "busy")
6086 ]
6087 );
6088 assert_eq!(
6089 contacts(&client_b, cx_b),
6090 [
6091 ("user_a".to_string(), "online", "busy"),
6092 ("user_c".to_string(), "online", "busy"),
6093 ("user_d".to_string(), "online", "free")
6094 ]
6095 );
6096 assert_eq!(
6097 contacts(&client_c, cx_c),
6098 [
6099 ("user_a".to_string(), "online", "busy"),
6100 ("user_b".to_string(), "online", "free")
6101 ]
6102 );
6103 assert_eq!(
6104 contacts(&client_d, cx_d),
6105 [("user_b".to_string(), "online", "free")]
6106 );
6107
6108 active_call_a
6109 .update(cx_a, |call, cx| call.accept_incoming(cx))
6110 .await
6111 .unwrap();
6112 executor.run_until_parked();
6113 assert_eq!(
6114 contacts(&client_a, cx_a),
6115 [
6116 ("user_b".to_string(), "online", "free"),
6117 ("user_c".to_string(), "online", "busy")
6118 ]
6119 );
6120 assert_eq!(
6121 contacts(&client_b, cx_b),
6122 [
6123 ("user_a".to_string(), "online", "busy"),
6124 ("user_c".to_string(), "online", "busy"),
6125 ("user_d".to_string(), "online", "free")
6126 ]
6127 );
6128 assert_eq!(
6129 contacts(&client_c, cx_c),
6130 [
6131 ("user_a".to_string(), "online", "busy"),
6132 ("user_b".to_string(), "online", "free")
6133 ]
6134 );
6135 assert_eq!(
6136 contacts(&client_d, cx_d),
6137 [("user_b".to_string(), "online", "free")]
6138 );
6139
6140 active_call_a
6141 .update(cx_a, |call, cx| {
6142 call.invite(client_b.user_id().unwrap(), None, cx)
6143 })
6144 .await
6145 .unwrap();
6146 executor.run_until_parked();
6147 assert_eq!(
6148 contacts(&client_a, cx_a),
6149 [
6150 ("user_b".to_string(), "online", "busy"),
6151 ("user_c".to_string(), "online", "busy")
6152 ]
6153 );
6154 assert_eq!(
6155 contacts(&client_b, cx_b),
6156 [
6157 ("user_a".to_string(), "online", "busy"),
6158 ("user_c".to_string(), "online", "busy"),
6159 ("user_d".to_string(), "online", "free")
6160 ]
6161 );
6162 assert_eq!(
6163 contacts(&client_c, cx_c),
6164 [
6165 ("user_a".to_string(), "online", "busy"),
6166 ("user_b".to_string(), "online", "busy")
6167 ]
6168 );
6169 assert_eq!(
6170 contacts(&client_d, cx_d),
6171 [("user_b".to_string(), "online", "busy")]
6172 );
6173
6174 active_call_a
6175 .update(cx_a, |call, cx| call.hang_up(cx))
6176 .await
6177 .unwrap();
6178 executor.run_until_parked();
6179 assert_eq!(
6180 contacts(&client_a, cx_a),
6181 [
6182 ("user_b".to_string(), "online", "free"),
6183 ("user_c".to_string(), "online", "free")
6184 ]
6185 );
6186 assert_eq!(
6187 contacts(&client_b, cx_b),
6188 [
6189 ("user_a".to_string(), "online", "free"),
6190 ("user_c".to_string(), "online", "free"),
6191 ("user_d".to_string(), "online", "free")
6192 ]
6193 );
6194 assert_eq!(
6195 contacts(&client_c, cx_c),
6196 [
6197 ("user_a".to_string(), "online", "free"),
6198 ("user_b".to_string(), "online", "free")
6199 ]
6200 );
6201 assert_eq!(
6202 contacts(&client_d, cx_d),
6203 [("user_b".to_string(), "online", "free")]
6204 );
6205
6206 active_call_a
6207 .update(cx_a, |call, cx| {
6208 call.invite(client_b.user_id().unwrap(), None, cx)
6209 })
6210 .await
6211 .unwrap();
6212 executor.run_until_parked();
6213 assert_eq!(
6214 contacts(&client_a, cx_a),
6215 [
6216 ("user_b".to_string(), "online", "busy"),
6217 ("user_c".to_string(), "online", "free")
6218 ]
6219 );
6220 assert_eq!(
6221 contacts(&client_b, cx_b),
6222 [
6223 ("user_a".to_string(), "online", "busy"),
6224 ("user_c".to_string(), "online", "free"),
6225 ("user_d".to_string(), "online", "free")
6226 ]
6227 );
6228 assert_eq!(
6229 contacts(&client_c, cx_c),
6230 [
6231 ("user_a".to_string(), "online", "busy"),
6232 ("user_b".to_string(), "online", "busy")
6233 ]
6234 );
6235 assert_eq!(
6236 contacts(&client_d, cx_d),
6237 [("user_b".to_string(), "online", "busy")]
6238 );
6239
6240 server.forbid_connections();
6241 server.disconnect_client(client_a.peer_id().unwrap());
6242 executor.advance_clock(RECEIVE_TIMEOUT + RECONNECT_TIMEOUT);
6243 assert_eq!(contacts(&client_a, cx_a), []);
6244 assert_eq!(
6245 contacts(&client_b, cx_b),
6246 [
6247 ("user_a".to_string(), "offline", "free"),
6248 ("user_c".to_string(), "online", "free"),
6249 ("user_d".to_string(), "online", "free")
6250 ]
6251 );
6252 assert_eq!(
6253 contacts(&client_c, cx_c),
6254 [
6255 ("user_a".to_string(), "offline", "free"),
6256 ("user_b".to_string(), "online", "free")
6257 ]
6258 );
6259 assert_eq!(
6260 contacts(&client_d, cx_d),
6261 [("user_b".to_string(), "online", "free")]
6262 );
6263
6264 // Test removing a contact
6265 client_b
6266 .user_store()
6267 .update(cx_b, |store, cx| {
6268 store.remove_contact(client_c.user_id().unwrap(), cx)
6269 })
6270 .await
6271 .unwrap();
6272 executor.run_until_parked();
6273 assert_eq!(
6274 contacts(&client_b, cx_b),
6275 [
6276 ("user_a".to_string(), "offline", "free"),
6277 ("user_d".to_string(), "online", "free")
6278 ]
6279 );
6280 assert_eq!(
6281 contacts(&client_c, cx_c),
6282 [("user_a".to_string(), "offline", "free"),]
6283 );
6284
6285 fn contacts(
6286 client: &TestClient,
6287 cx: &TestAppContext,
6288 ) -> Vec<(String, &'static str, &'static str)> {
6289 client.user_store().read_with(cx, |store, _| {
6290 store
6291 .contacts()
6292 .iter()
6293 .map(|contact| {
6294 (
6295 contact.user.github_login.clone().to_string(),
6296 if contact.online { "online" } else { "offline" },
6297 if contact.busy { "busy" } else { "free" },
6298 )
6299 })
6300 .collect()
6301 })
6302 }
6303}
6304
6305#[gpui::test(iterations = 10)]
6306async fn test_contact_requests(
6307 executor: BackgroundExecutor,
6308 cx_a: &mut TestAppContext,
6309 cx_a2: &mut TestAppContext,
6310 cx_b: &mut TestAppContext,
6311 cx_b2: &mut TestAppContext,
6312 cx_c: &mut TestAppContext,
6313 cx_c2: &mut TestAppContext,
6314) {
6315 // Connect to a server as 3 clients.
6316 let mut server = TestServer::start(executor.clone()).await;
6317 let client_a = server.create_client(cx_a, "user_a").await;
6318 let client_a2 = server.create_client(cx_a2, "user_a").await;
6319 let client_b = server.create_client(cx_b, "user_b").await;
6320 let client_b2 = server.create_client(cx_b2, "user_b").await;
6321 let client_c = server.create_client(cx_c, "user_c").await;
6322 let client_c2 = server.create_client(cx_c2, "user_c").await;
6323
6324 assert_eq!(client_a.user_id().unwrap(), client_a2.user_id().unwrap());
6325 assert_eq!(client_b.user_id().unwrap(), client_b2.user_id().unwrap());
6326 assert_eq!(client_c.user_id().unwrap(), client_c2.user_id().unwrap());
6327
6328 // User A and User C request that user B become their contact.
6329 client_a
6330 .user_store()
6331 .update(cx_a, |store, cx| {
6332 store.request_contact(client_b.user_id().unwrap(), cx)
6333 })
6334 .await
6335 .unwrap();
6336 client_c
6337 .user_store()
6338 .update(cx_c, |store, cx| {
6339 store.request_contact(client_b.user_id().unwrap(), cx)
6340 })
6341 .await
6342 .unwrap();
6343 executor.run_until_parked();
6344
6345 // All users see the pending request appear in all their clients.
6346 assert_eq!(
6347 client_a.summarize_contacts(cx_a).outgoing_requests,
6348 &["user_b"]
6349 );
6350 assert_eq!(
6351 client_a2.summarize_contacts(cx_a2).outgoing_requests,
6352 &["user_b"]
6353 );
6354 assert_eq!(
6355 client_b.summarize_contacts(cx_b).incoming_requests,
6356 &["user_a", "user_c"]
6357 );
6358 assert_eq!(
6359 client_b2.summarize_contacts(cx_b2).incoming_requests,
6360 &["user_a", "user_c"]
6361 );
6362 assert_eq!(
6363 client_c.summarize_contacts(cx_c).outgoing_requests,
6364 &["user_b"]
6365 );
6366 assert_eq!(
6367 client_c2.summarize_contacts(cx_c2).outgoing_requests,
6368 &["user_b"]
6369 );
6370
6371 // Contact requests are present upon connecting (tested here via disconnect/reconnect)
6372 disconnect_and_reconnect(&client_a, cx_a).await;
6373 disconnect_and_reconnect(&client_b, cx_b).await;
6374 disconnect_and_reconnect(&client_c, cx_c).await;
6375 executor.run_until_parked();
6376 assert_eq!(
6377 client_a.summarize_contacts(cx_a).outgoing_requests,
6378 &["user_b"]
6379 );
6380 assert_eq!(
6381 client_b.summarize_contacts(cx_b).incoming_requests,
6382 &["user_a", "user_c"]
6383 );
6384 assert_eq!(
6385 client_c.summarize_contacts(cx_c).outgoing_requests,
6386 &["user_b"]
6387 );
6388
6389 // User B accepts the request from user A.
6390 client_b
6391 .user_store()
6392 .update(cx_b, |store, cx| {
6393 store.respond_to_contact_request(client_a.user_id().unwrap(), true, cx)
6394 })
6395 .await
6396 .unwrap();
6397
6398 executor.run_until_parked();
6399
6400 // User B sees user A as their contact now in all client, and the incoming request from them is removed.
6401 let contacts_b = client_b.summarize_contacts(cx_b);
6402 assert_eq!(contacts_b.current, &["user_a"]);
6403 assert_eq!(contacts_b.incoming_requests, &["user_c"]);
6404 let contacts_b2 = client_b2.summarize_contacts(cx_b2);
6405 assert_eq!(contacts_b2.current, &["user_a"]);
6406 assert_eq!(contacts_b2.incoming_requests, &["user_c"]);
6407
6408 // User A sees user B as their contact now in all clients, and the outgoing request to them is removed.
6409 let contacts_a = client_a.summarize_contacts(cx_a);
6410 assert_eq!(contacts_a.current, &["user_b"]);
6411 assert!(contacts_a.outgoing_requests.is_empty());
6412 let contacts_a2 = client_a2.summarize_contacts(cx_a2);
6413 assert_eq!(contacts_a2.current, &["user_b"]);
6414 assert!(contacts_a2.outgoing_requests.is_empty());
6415
6416 // Contacts are present upon connecting (tested here via disconnect/reconnect)
6417 disconnect_and_reconnect(&client_a, cx_a).await;
6418 disconnect_and_reconnect(&client_b, cx_b).await;
6419 disconnect_and_reconnect(&client_c, cx_c).await;
6420 executor.run_until_parked();
6421 assert_eq!(client_a.summarize_contacts(cx_a).current, &["user_b"]);
6422 assert_eq!(client_b.summarize_contacts(cx_b).current, &["user_a"]);
6423 assert_eq!(
6424 client_b.summarize_contacts(cx_b).incoming_requests,
6425 &["user_c"]
6426 );
6427 assert!(client_c.summarize_contacts(cx_c).current.is_empty());
6428 assert_eq!(
6429 client_c.summarize_contacts(cx_c).outgoing_requests,
6430 &["user_b"]
6431 );
6432
6433 // User B rejects the request from user C.
6434 client_b
6435 .user_store()
6436 .update(cx_b, |store, cx| {
6437 store.respond_to_contact_request(client_c.user_id().unwrap(), false, cx)
6438 })
6439 .await
6440 .unwrap();
6441
6442 executor.run_until_parked();
6443
6444 // User B doesn't see user C as their contact, and the incoming request from them is removed.
6445 let contacts_b = client_b.summarize_contacts(cx_b);
6446 assert_eq!(contacts_b.current, &["user_a"]);
6447 assert!(contacts_b.incoming_requests.is_empty());
6448 let contacts_b2 = client_b2.summarize_contacts(cx_b2);
6449 assert_eq!(contacts_b2.current, &["user_a"]);
6450 assert!(contacts_b2.incoming_requests.is_empty());
6451
6452 // User C doesn't see user B as their contact, and the outgoing request to them is removed.
6453 let contacts_c = client_c.summarize_contacts(cx_c);
6454 assert!(contacts_c.current.is_empty());
6455 assert!(contacts_c.outgoing_requests.is_empty());
6456 let contacts_c2 = client_c2.summarize_contacts(cx_c2);
6457 assert!(contacts_c2.current.is_empty());
6458 assert!(contacts_c2.outgoing_requests.is_empty());
6459
6460 // Incoming/outgoing requests are not present upon connecting (tested here via disconnect/reconnect)
6461 disconnect_and_reconnect(&client_a, cx_a).await;
6462 disconnect_and_reconnect(&client_b, cx_b).await;
6463 disconnect_and_reconnect(&client_c, cx_c).await;
6464 executor.run_until_parked();
6465 assert_eq!(client_a.summarize_contacts(cx_a).current, &["user_b"]);
6466 assert_eq!(client_b.summarize_contacts(cx_b).current, &["user_a"]);
6467 assert!(
6468 client_b
6469 .summarize_contacts(cx_b)
6470 .incoming_requests
6471 .is_empty()
6472 );
6473 assert!(client_c.summarize_contacts(cx_c).current.is_empty());
6474 assert!(
6475 client_c
6476 .summarize_contacts(cx_c)
6477 .outgoing_requests
6478 .is_empty()
6479 );
6480
6481 async fn disconnect_and_reconnect(client: &TestClient, cx: &mut TestAppContext) {
6482 client.disconnect(&cx.to_async());
6483 client.clear_contacts(cx).await;
6484 client
6485 .connect(false, &cx.to_async())
6486 .await
6487 .into_response()
6488 .unwrap();
6489 }
6490}
6491
6492#[gpui::test(iterations = 10)]
6493async fn test_join_call_after_screen_was_shared(
6494 executor: BackgroundExecutor,
6495 cx_a: &mut TestAppContext,
6496 cx_b: &mut TestAppContext,
6497) {
6498 let mut server = TestServer::start(executor.clone()).await;
6499
6500 let client_a = server.create_client(cx_a, "user_a").await;
6501 let client_b = server.create_client(cx_b, "user_b").await;
6502 server
6503 .make_contacts(&mut [(&client_a, cx_a), (&client_b, cx_b)])
6504 .await;
6505
6506 let active_call_a = cx_a.read(ActiveCall::global);
6507 let active_call_b = cx_b.read(ActiveCall::global);
6508
6509 // Call users B and C from client A.
6510 active_call_a
6511 .update(cx_a, |call, cx| {
6512 call.invite(client_b.user_id().unwrap(), None, cx)
6513 })
6514 .await
6515 .unwrap();
6516
6517 let room_a = active_call_a.read_with(cx_a, |call, _| call.room().unwrap().clone());
6518 executor.run_until_parked();
6519 assert_eq!(
6520 room_participants(&room_a, cx_a),
6521 RoomParticipants {
6522 remote: Default::default(),
6523 pending: vec!["user_b".to_string()]
6524 }
6525 );
6526
6527 // User B receives the call.
6528
6529 let mut incoming_call_b = active_call_b.read_with(cx_b, |call, _| call.incoming());
6530 let call_b = incoming_call_b.next().await.unwrap().unwrap();
6531 assert_eq!(call_b.calling_user.github_login, "user_a");
6532
6533 // User A shares their screen
6534 let display = gpui::TestScreenCaptureSource::new();
6535 cx_a.set_screen_capture_sources(vec![display]);
6536 let screen_a = cx_a
6537 .update(|cx| cx.screen_capture_sources())
6538 .await
6539 .unwrap()
6540 .unwrap()
6541 .into_iter()
6542 .next()
6543 .unwrap();
6544
6545 active_call_a
6546 .update(cx_a, |call, cx| {
6547 call.room()
6548 .unwrap()
6549 .update(cx, |room, cx| room.share_screen(screen_a, cx))
6550 })
6551 .await
6552 .unwrap();
6553
6554 client_b.user_store().update(cx_b, |user_store, _| {
6555 user_store.clear_cache();
6556 });
6557
6558 // User B joins the room
6559 active_call_b
6560 .update(cx_b, |call, cx| call.accept_incoming(cx))
6561 .await
6562 .unwrap();
6563
6564 let room_b = active_call_b.read_with(cx_b, |call, _| call.room().unwrap().clone());
6565 assert!(incoming_call_b.next().await.unwrap().is_none());
6566
6567 executor.run_until_parked();
6568 assert_eq!(
6569 room_participants(&room_a, cx_a),
6570 RoomParticipants {
6571 remote: vec!["user_b".to_string()],
6572 pending: vec![],
6573 }
6574 );
6575 assert_eq!(
6576 room_participants(&room_b, cx_b),
6577 RoomParticipants {
6578 remote: vec!["user_a".to_string()],
6579 pending: vec![],
6580 }
6581 );
6582
6583 // Ensure User B sees User A's screenshare.
6584
6585 room_b.read_with(cx_b, |room, _| {
6586 assert_eq!(
6587 room.remote_participants()
6588 .get(&client_a.user_id().unwrap())
6589 .unwrap()
6590 .video_tracks
6591 .len(),
6592 1
6593 );
6594 });
6595}
6596
6597#[cfg(target_os = "linux")]
6598#[gpui::test(iterations = 10)]
6599async fn test_share_screen_wayland(
6600 executor: BackgroundExecutor,
6601 cx_a: &mut TestAppContext,
6602 cx_b: &mut TestAppContext,
6603) {
6604 let mut server = TestServer::start(executor.clone()).await;
6605
6606 let client_a = server.create_client(cx_a, "user_a").await;
6607 let client_b = server.create_client(cx_b, "user_b").await;
6608 server
6609 .make_contacts(&mut [(&client_a, cx_a), (&client_b, cx_b)])
6610 .await;
6611
6612 let active_call_a = cx_a.read(ActiveCall::global);
6613 let active_call_b = cx_b.read(ActiveCall::global);
6614
6615 // User A calls user B.
6616 active_call_a
6617 .update(cx_a, |call, cx| {
6618 call.invite(client_b.user_id().unwrap(), None, cx)
6619 })
6620 .await
6621 .unwrap();
6622
6623 // User B accepts.
6624 let mut incoming_call_b = active_call_b.read_with(cx_b, |call, _| call.incoming());
6625 executor.run_until_parked();
6626 incoming_call_b.next().await.unwrap().unwrap();
6627 active_call_b
6628 .update(cx_b, |call, cx| call.accept_incoming(cx))
6629 .await
6630 .unwrap();
6631
6632 let room_a = active_call_a.read_with(cx_a, |call, _| call.room().unwrap().clone());
6633 let room_b = active_call_b.read_with(cx_b, |call, _| call.room().unwrap().clone());
6634 executor.run_until_parked();
6635
6636 // User A shares their screen via the Wayland path.
6637 let events_b = active_call_events(cx_b);
6638 active_call_a
6639 .update(cx_a, |call, cx| {
6640 call.room()
6641 .unwrap()
6642 .update(cx, |room, cx| room.share_screen_wayland(cx))
6643 })
6644 .await
6645 .unwrap();
6646
6647 executor.run_until_parked();
6648
6649 // Room A is sharing and has a nonzero synthetic screen ID.
6650 room_a.read_with(cx_a, |room, _| {
6651 assert!(room.is_sharing_screen());
6652 let screen_id = room.shared_screen_id();
6653 assert!(screen_id.is_some(), "shared_screen_id should be Some");
6654 assert_ne!(screen_id.unwrap(), 0, "synthetic ID must be nonzero");
6655 });
6656
6657 // User B observes the remote screen sharing track.
6658 assert_eq!(events_b.borrow().len(), 1);
6659 if let call::room::Event::RemoteVideoTracksChanged { participant_id } =
6660 events_b.borrow().first().unwrap()
6661 {
6662 assert_eq!(*participant_id, client_a.peer_id().unwrap());
6663 room_b.read_with(cx_b, |room, _| {
6664 assert_eq!(
6665 room.remote_participants()[&client_a.user_id().unwrap()]
6666 .video_tracks
6667 .len(),
6668 1
6669 );
6670 });
6671 } else {
6672 panic!("expected RemoteVideoTracksChanged event");
6673 }
6674}
6675
6676#[cfg(target_os = "linux")]
6677#[gpui::test(iterations = 10)]
6678async fn test_unshare_screen_wayland(
6679 executor: BackgroundExecutor,
6680 cx_a: &mut TestAppContext,
6681 cx_b: &mut TestAppContext,
6682) {
6683 let mut server = TestServer::start(executor.clone()).await;
6684
6685 let client_a = server.create_client(cx_a, "user_a").await;
6686 let client_b = server.create_client(cx_b, "user_b").await;
6687 server
6688 .make_contacts(&mut [(&client_a, cx_a), (&client_b, cx_b)])
6689 .await;
6690
6691 let active_call_a = cx_a.read(ActiveCall::global);
6692 let active_call_b = cx_b.read(ActiveCall::global);
6693
6694 // User A calls user B.
6695 active_call_a
6696 .update(cx_a, |call, cx| {
6697 call.invite(client_b.user_id().unwrap(), None, cx)
6698 })
6699 .await
6700 .unwrap();
6701
6702 // User B accepts.
6703 let mut incoming_call_b = active_call_b.read_with(cx_b, |call, _| call.incoming());
6704 executor.run_until_parked();
6705 incoming_call_b.next().await.unwrap().unwrap();
6706 active_call_b
6707 .update(cx_b, |call, cx| call.accept_incoming(cx))
6708 .await
6709 .unwrap();
6710
6711 let room_a = active_call_a.read_with(cx_a, |call, _| call.room().unwrap().clone());
6712 executor.run_until_parked();
6713
6714 // User A shares their screen via the Wayland path.
6715 active_call_a
6716 .update(cx_a, |call, cx| {
6717 call.room()
6718 .unwrap()
6719 .update(cx, |room, cx| room.share_screen_wayland(cx))
6720 })
6721 .await
6722 .unwrap();
6723 executor.run_until_parked();
6724
6725 room_a.read_with(cx_a, |room, _| {
6726 assert!(room.is_sharing_screen());
6727 });
6728
6729 // User A stops sharing.
6730 room_a
6731 .update(cx_a, |room, cx| room.unshare_screen(true, cx))
6732 .unwrap();
6733 executor.run_until_parked();
6734
6735 // Room A is no longer sharing, screen ID is gone.
6736 room_a.read_with(cx_a, |room, _| {
6737 assert!(!room.is_sharing_screen());
6738 assert!(room.shared_screen_id().is_none());
6739 });
6740}
6741
6742#[gpui::test]
6743async fn test_right_click_menu_behind_collab_panel(cx: &mut TestAppContext) {
6744 let mut server = TestServer::start(cx.executor().clone()).await;
6745 let client_a = server.create_client(cx, "user_a").await;
6746 let (_workspace_a, cx) = client_a.build_test_workspace(cx).await;
6747
6748 cx.simulate_resize(size(px(300.), px(300.)));
6749
6750 cx.simulate_keystrokes("cmd-n cmd-n cmd-n");
6751 cx.update(|window, _cx| window.refresh());
6752
6753 let new_tab_button_bounds = cx.debug_bounds("ICON-Plus").unwrap();
6754
6755 cx.simulate_event(MouseDownEvent {
6756 button: MouseButton::Right,
6757 position: new_tab_button_bounds.center(),
6758 modifiers: Modifiers::default(),
6759 click_count: 1,
6760 first_mouse: false,
6761 });
6762
6763 // regression test that the right click menu for tabs does not open.
6764 assert!(cx.debug_bounds("MENU_ITEM-Close").is_none());
6765
6766 let tab_bounds = cx.debug_bounds("TAB-1").unwrap();
6767 cx.simulate_event(MouseDownEvent {
6768 button: MouseButton::Right,
6769 position: tab_bounds.center(),
6770 modifiers: Modifiers::default(),
6771 click_count: 1,
6772 first_mouse: false,
6773 });
6774 assert!(cx.debug_bounds("MENU_ITEM-Close").is_some());
6775}
6776
6777#[gpui::test]
6778async fn test_pane_split_left(cx: &mut TestAppContext) {
6779 let (_, client) = TestServer::start1(cx).await;
6780 let (workspace, cx) = client.build_test_workspace(cx).await;
6781
6782 cx.simulate_keystrokes("cmd-n");
6783 workspace.update(cx, |workspace, cx| {
6784 assert!(workspace.items(cx).collect::<Vec<_>>().len() == 1);
6785 });
6786 cx.simulate_keystrokes("cmd-k left");
6787 workspace.update(cx, |workspace, cx| {
6788 assert!(workspace.items(cx).collect::<Vec<_>>().len() == 2);
6789 });
6790 cx.simulate_keystrokes("cmd-k");
6791 // Sleep past the historical timeout to ensure the multi-stroke binding
6792 // still fires now that unambiguous prefixes no longer auto-expire.
6793 cx.executor().advance_clock(Duration::from_secs(2));
6794 cx.simulate_keystrokes("left");
6795 workspace.update(cx, |workspace, cx| {
6796 assert!(workspace.items(cx).collect::<Vec<_>>().len() == 3);
6797 });
6798}
6799
6800#[gpui::test]
6801async fn test_join_after_restart(cx1: &mut TestAppContext, cx2: &mut TestAppContext) {
6802 let (mut server, client) = TestServer::start1(cx1).await;
6803 let channel1 = server.make_public_channel("channel1", &client, cx1).await;
6804 let channel2 = server.make_public_channel("channel2", &client, cx1).await;
6805
6806 join_channel(channel1, &client, cx1).await.unwrap();
6807 drop(client);
6808
6809 let client2 = server.create_client(cx2, "user_a").await;
6810 join_channel(channel2, &client2, cx2).await.unwrap();
6811}
6812
6813#[gpui::test]
6814async fn test_preview_tabs(cx: &mut TestAppContext) {
6815 let (_server, client) = TestServer::start1(cx).await;
6816 let (workspace, cx) = client.build_test_workspace(cx).await;
6817 let project = workspace.read_with(cx, |workspace, _| workspace.project().clone());
6818
6819 let worktree_id = project.update(cx, |project, cx| {
6820 project.worktrees(cx).next().unwrap().read(cx).id()
6821 });
6822
6823 let path_1 = ProjectPath {
6824 worktree_id,
6825 path: rel_path("1.txt").into(),
6826 };
6827 let path_2 = ProjectPath {
6828 worktree_id,
6829 path: rel_path("2.js").into(),
6830 };
6831 let path_3 = ProjectPath {
6832 worktree_id,
6833 path: rel_path("3.rs").into(),
6834 };
6835
6836 let pane = workspace.read_with(cx, |workspace, _| workspace.active_pane().clone());
6837
6838 let get_path = |pane: &Pane, idx: usize, cx: &App| {
6839 pane.item_for_index(idx).unwrap().project_path(cx).unwrap()
6840 };
6841
6842 // Opening item 3 as a "permanent" tab
6843 workspace
6844 .update_in(cx, |workspace, window, cx| {
6845 workspace.open_path(path_3.clone(), None, false, window, cx)
6846 })
6847 .await
6848 .unwrap();
6849
6850 pane.update(cx, |pane, cx| {
6851 assert_eq!(pane.items_len(), 1);
6852 assert_eq!(get_path(pane, 0, cx), path_3.clone());
6853 assert_eq!(pane.preview_item_id(), None);
6854
6855 assert!(!pane.can_navigate_backward());
6856 assert!(!pane.can_navigate_forward());
6857 });
6858
6859 // Open item 1 as preview
6860 workspace
6861 .update_in(cx, |workspace, window, cx| {
6862 workspace.open_path_preview(path_1.clone(), None, true, true, true, window, cx)
6863 })
6864 .await
6865 .unwrap();
6866
6867 pane.update(cx, |pane, cx| {
6868 assert_eq!(pane.items_len(), 2);
6869 assert_eq!(get_path(pane, 0, cx), path_3.clone());
6870 assert_eq!(get_path(pane, 1, cx), path_1.clone());
6871 assert_eq!(
6872 pane.preview_item_id(),
6873 Some(pane.items().nth(1).unwrap().item_id())
6874 );
6875
6876 assert!(pane.can_navigate_backward());
6877 assert!(!pane.can_navigate_forward());
6878 });
6879
6880 // Open item 2 as preview
6881 workspace
6882 .update_in(cx, |workspace, window, cx| {
6883 workspace.open_path_preview(path_2.clone(), None, true, true, true, window, cx)
6884 })
6885 .await
6886 .unwrap();
6887
6888 pane.update(cx, |pane, cx| {
6889 assert_eq!(pane.items_len(), 2);
6890 assert_eq!(get_path(pane, 0, cx), path_3.clone());
6891 assert_eq!(get_path(pane, 1, cx), path_2.clone());
6892 assert_eq!(
6893 pane.preview_item_id(),
6894 Some(pane.items().nth(1).unwrap().item_id())
6895 );
6896
6897 assert!(pane.can_navigate_backward());
6898 assert!(!pane.can_navigate_forward());
6899 });
6900
6901 // Going back should show item 1 as preview
6902 workspace
6903 .update_in(cx, |workspace, window, cx| {
6904 workspace.go_back(pane.downgrade(), window, cx)
6905 })
6906 .await
6907 .unwrap();
6908
6909 pane.update(cx, |pane, cx| {
6910 assert_eq!(pane.items_len(), 2);
6911 assert_eq!(get_path(pane, 0, cx), path_3.clone());
6912 assert_eq!(get_path(pane, 1, cx), path_1.clone());
6913 assert_eq!(
6914 pane.preview_item_id(),
6915 Some(pane.items().nth(1).unwrap().item_id())
6916 );
6917
6918 assert!(pane.can_navigate_backward());
6919 assert!(pane.can_navigate_forward());
6920 });
6921
6922 // Closing item 1
6923 pane.update_in(cx, |pane, window, cx| {
6924 pane.close_item_by_id(
6925 pane.active_item().unwrap().item_id(),
6926 workspace::SaveIntent::Skip,
6927 window,
6928 cx,
6929 )
6930 })
6931 .await
6932 .unwrap();
6933
6934 pane.update(cx, |pane, cx| {
6935 assert_eq!(pane.items_len(), 1);
6936 assert_eq!(get_path(pane, 0, cx), path_3.clone());
6937 assert_eq!(pane.preview_item_id(), None);
6938
6939 assert!(pane.can_navigate_backward());
6940 assert!(!pane.can_navigate_forward());
6941 });
6942
6943 // Going back should show item 1 as preview
6944 workspace
6945 .update_in(cx, |workspace, window, cx| {
6946 workspace.go_back(pane.downgrade(), window, cx)
6947 })
6948 .await
6949 .unwrap();
6950
6951 pane.update(cx, |pane, cx| {
6952 assert_eq!(pane.items_len(), 2);
6953 assert_eq!(get_path(pane, 0, cx), path_3.clone());
6954 assert_eq!(get_path(pane, 1, cx), path_1.clone());
6955 assert_eq!(
6956 pane.preview_item_id(),
6957 Some(pane.items().nth(1).unwrap().item_id())
6958 );
6959
6960 assert!(pane.can_navigate_backward());
6961 assert!(pane.can_navigate_forward());
6962 });
6963
6964 // Close permanent tab
6965 pane.update_in(cx, |pane, window, cx| {
6966 let id = pane.items().next().unwrap().item_id();
6967 pane.close_item_by_id(id, workspace::SaveIntent::Skip, window, cx)
6968 })
6969 .await
6970 .unwrap();
6971
6972 pane.update(cx, |pane, cx| {
6973 assert_eq!(pane.items_len(), 1);
6974 assert_eq!(get_path(pane, 0, cx), path_1.clone());
6975 assert_eq!(
6976 pane.preview_item_id(),
6977 Some(pane.items().next().unwrap().item_id())
6978 );
6979
6980 assert!(pane.can_navigate_backward());
6981 assert!(pane.can_navigate_forward());
6982 });
6983
6984 // Split pane to the right
6985 pane.update_in(cx, |pane, window, cx| {
6986 pane.split(
6987 workspace::SplitDirection::Right,
6988 workspace::SplitMode::default(),
6989 window,
6990 cx,
6991 );
6992 });
6993 cx.run_until_parked();
6994 let right_pane = workspace.read_with(cx, |workspace, _| workspace.active_pane().clone());
6995
6996 right_pane.update(cx, |pane, cx| {
6997 // Nav history is now cloned in an pane split, but that's inconvenient
6998 // for this test, which uses the presence of a backwards history item as
6999 // an indication that a preview item was successfully opened
7000 pane.nav_history_mut().clear(cx);
7001 });
7002
7003 pane.update(cx, |pane, cx| {
7004 assert_eq!(pane.items_len(), 1);
7005 assert_eq!(get_path(pane, 0, cx), path_1.clone());
7006 assert_eq!(
7007 pane.preview_item_id(),
7008 Some(pane.items().next().unwrap().item_id())
7009 );
7010
7011 assert!(pane.can_navigate_backward());
7012 assert!(pane.can_navigate_forward());
7013 });
7014
7015 right_pane.update(cx, |pane, cx| {
7016 assert_eq!(pane.items_len(), 1);
7017 assert_eq!(get_path(pane, 0, cx), path_1.clone());
7018 assert_eq!(pane.preview_item_id(), None);
7019
7020 assert!(!pane.can_navigate_backward());
7021 assert!(!pane.can_navigate_forward());
7022 });
7023
7024 // Open item 2 as preview in right pane
7025 workspace
7026 .update_in(cx, |workspace, window, cx| {
7027 workspace.open_path_preview(path_2.clone(), None, true, true, true, window, cx)
7028 })
7029 .await
7030 .unwrap();
7031
7032 pane.update(cx, |pane, cx| {
7033 assert_eq!(pane.items_len(), 1);
7034 assert_eq!(get_path(pane, 0, cx), path_1.clone());
7035 assert_eq!(
7036 pane.preview_item_id(),
7037 Some(pane.items().next().unwrap().item_id())
7038 );
7039
7040 assert!(pane.can_navigate_backward());
7041 assert!(pane.can_navigate_forward());
7042 });
7043
7044 right_pane.update(cx, |pane, cx| {
7045 assert_eq!(pane.items_len(), 2);
7046 assert_eq!(get_path(pane, 0, cx), path_1.clone());
7047 assert_eq!(get_path(pane, 1, cx), path_2.clone());
7048 assert_eq!(
7049 pane.preview_item_id(),
7050 Some(pane.items().nth(1).unwrap().item_id())
7051 );
7052
7053 assert!(pane.can_navigate_backward());
7054 assert!(!pane.can_navigate_forward());
7055 });
7056
7057 // Focus left pane
7058 workspace.update_in(cx, |workspace, window, cx| {
7059 workspace.activate_pane_in_direction(workspace::SplitDirection::Left, window, cx)
7060 });
7061
7062 // Open item 2 as preview in left pane
7063 workspace
7064 .update_in(cx, |workspace, window, cx| {
7065 workspace.open_path_preview(path_2.clone(), None, true, true, true, window, cx)
7066 })
7067 .await
7068 .unwrap();
7069
7070 pane.update(cx, |pane, cx| {
7071 assert_eq!(pane.items_len(), 1);
7072 assert_eq!(get_path(pane, 0, cx), path_2.clone());
7073 assert_eq!(
7074 pane.preview_item_id(),
7075 Some(pane.items().next().unwrap().item_id())
7076 );
7077
7078 assert!(pane.can_navigate_backward());
7079 assert!(!pane.can_navigate_forward());
7080 });
7081
7082 right_pane.update(cx, |pane, cx| {
7083 assert_eq!(pane.items_len(), 2);
7084 assert_eq!(get_path(pane, 0, cx), path_1.clone());
7085 assert_eq!(get_path(pane, 1, cx), path_2.clone());
7086 assert_eq!(
7087 pane.preview_item_id(),
7088 Some(pane.items().nth(1).unwrap().item_id())
7089 );
7090
7091 assert!(pane.can_navigate_backward());
7092 assert!(!pane.can_navigate_forward());
7093 });
7094}
7095
7096#[gpui::test]
7097async fn test_remote_git_branches(
7098 executor: BackgroundExecutor,
7099 cx_a: &mut TestAppContext,
7100 cx_b: &mut TestAppContext,
7101) {
7102 let mut server = TestServer::start(executor.clone()).await;
7103 let client_a = server.create_client(cx_a, "user_a").await;
7104 let client_b = server.create_client(cx_b, "user_b").await;
7105 server
7106 .create_room(&mut [(&client_a, cx_a), (&client_b, cx_b)])
7107 .await;
7108 let active_call_a = cx_a.read(ActiveCall::global);
7109
7110 client_a
7111 .fs()
7112 .insert_tree("/project", serde_json::json!({ ".git":{} }))
7113 .await;
7114 let branches = ["main", "dev", "feature-1"];
7115 client_a
7116 .fs()
7117 .insert_branches(Path::new("/project/.git"), &branches);
7118 let branches_set = branches
7119 .into_iter()
7120 .map(ToString::to_string)
7121 .collect::<HashSet<_>>();
7122
7123 let (project_a, _) = client_a.build_local_project("/project", cx_a).await;
7124
7125 let project_id = active_call_a
7126 .update(cx_a, |call, cx| call.share_project(project_a.clone(), cx))
7127 .await
7128 .unwrap();
7129 let project_b = client_b.join_remote_project(project_id, cx_b).await;
7130
7131 // Client A sees that a guest has joined and the repo has been populated
7132 executor.run_until_parked();
7133
7134 let repo_b = cx_b.update(|cx| project_b.read(cx).active_repository(cx).unwrap());
7135
7136 let branches_b = cx_b
7137 .update(|cx| repo_b.update(cx, |repository, _| repository.branches()))
7138 .await
7139 .unwrap()
7140 .unwrap();
7141
7142 let new_branch = branches[2];
7143
7144 let branches_b = branches_b
7145 .into_iter()
7146 .map(|branch| branch.name().to_string())
7147 .collect::<HashSet<_>>();
7148
7149 assert_eq!(branches_b, branches_set);
7150
7151 cx_b.update(|cx| {
7152 repo_b.update(cx, |repository, _cx| {
7153 repository.change_branch(new_branch.to_string())
7154 })
7155 })
7156 .await
7157 .unwrap()
7158 .unwrap();
7159
7160 executor.run_until_parked();
7161
7162 let host_branch = cx_a.update(|cx| {
7163 project_a.update(cx, |project, cx| {
7164 project
7165 .repositories(cx)
7166 .values()
7167 .next()
7168 .unwrap()
7169 .read(cx)
7170 .branch
7171 .as_ref()
7172 .unwrap()
7173 .clone()
7174 })
7175 });
7176
7177 assert_eq!(host_branch.name(), branches[2]);
7178
7179 // Also try creating a new branch
7180 cx_b.update(|cx| {
7181 repo_b.update(cx, |repository, _cx| {
7182 repository.create_branch("totally-new-branch".to_string(), None)
7183 })
7184 })
7185 .await
7186 .unwrap()
7187 .unwrap();
7188
7189 cx_b.update(|cx| {
7190 repo_b.update(cx, |repository, _cx| {
7191 repository.change_branch("totally-new-branch".to_string())
7192 })
7193 })
7194 .await
7195 .unwrap()
7196 .unwrap();
7197
7198 executor.run_until_parked();
7199
7200 let host_branch = cx_a.update(|cx| {
7201 project_a.update(cx, |project, cx| {
7202 project
7203 .repositories(cx)
7204 .values()
7205 .next()
7206 .unwrap()
7207 .read(cx)
7208 .branch
7209 .as_ref()
7210 .unwrap()
7211 .clone()
7212 })
7213 });
7214
7215 assert_eq!(host_branch.name(), "totally-new-branch");
7216}
7217
7218#[gpui::test]
7219async fn test_guest_can_rejoin_shared_project_after_leaving_call(
7220 executor: BackgroundExecutor,
7221 cx_a: &mut TestAppContext,
7222 cx_b: &mut TestAppContext,
7223 cx_c: &mut TestAppContext,
7224) {
7225 let mut server = TestServer::start(executor.clone()).await;
7226 let client_a = server.create_client(cx_a, "user_a").await;
7227 let client_b = server.create_client(cx_b, "user_b").await;
7228 let client_c = server.create_client(cx_c, "user_c").await;
7229
7230 server
7231 .create_room(&mut [(&client_a, cx_a), (&client_b, cx_b), (&client_c, cx_c)])
7232 .await;
7233
7234 client_a
7235 .fs()
7236 .insert_tree(
7237 path!("/project"),
7238 json!({
7239 "file.txt": "hello\n",
7240 }),
7241 )
7242 .await;
7243
7244 let (project_a, _worktree_id) = client_a.build_local_project(path!("/project"), cx_a).await;
7245 let active_call_a = cx_a.read(ActiveCall::global);
7246 let project_id = active_call_a
7247 .update(cx_a, |call, cx| call.share_project(project_a.clone(), cx))
7248 .await
7249 .unwrap();
7250
7251 let _project_b = client_b.join_remote_project(project_id, cx_b).await;
7252 executor.run_until_parked();
7253
7254 // third client joins call to prevent room from being torn down
7255 let _project_c = client_c.join_remote_project(project_id, cx_c).await;
7256 executor.run_until_parked();
7257
7258 let active_call_b = cx_b.read(ActiveCall::global);
7259 active_call_b
7260 .update(cx_b, |call, cx| call.hang_up(cx))
7261 .await
7262 .unwrap();
7263 executor.run_until_parked();
7264
7265 let user_id_b = client_b.current_user_id(cx_b).to_proto();
7266 let active_call_a = cx_a.read(ActiveCall::global);
7267 active_call_a
7268 .update(cx_a, |call, cx| call.invite(user_id_b, None, cx))
7269 .await
7270 .unwrap();
7271 executor.run_until_parked();
7272 let active_call_b = cx_b.read(ActiveCall::global);
7273 active_call_b
7274 .update(cx_b, |call, cx| call.accept_incoming(cx))
7275 .await
7276 .unwrap();
7277 executor.run_until_parked();
7278
7279 let _project_b2 = client_b.join_remote_project(project_id, cx_b).await;
7280 executor.run_until_parked();
7281
7282 project_a.read_with(cx_a, |project, _| {
7283 let guest_count = project
7284 .collaborators()
7285 .values()
7286 .filter(|c| !c.is_host)
7287 .count();
7288
7289 assert_eq!(
7290 guest_count, 2,
7291 "host should have exactly one guest collaborator after rejoin"
7292 );
7293 });
7294
7295 _project_b.read_with(cx_b, |project, _| {
7296 assert_eq!(
7297 project.client_subscriptions().len(),
7298 0,
7299 "We should clear all host subscriptions after leaving the project"
7300 );
7301 })
7302}