integration_tests.rs

   1use crate::{
   2    rpc::{CLEANUP_TIMEOUT, RECONNECT_TIMEOUT},
   3    tests::{channel_id, room_participants, rust_lang, RoomParticipants, TestClient, TestServer},
   4};
   5use call::{room, ActiveCall, ParticipantLocation, Room};
   6use client::{User, RECEIVE_TIMEOUT};
   7use collections::{HashMap, HashSet};
   8use fs::{repository::GitFileStatus, FakeFs, Fs as _, RemoveOptions};
   9use futures::StreamExt as _;
  10use gpui::{
  11    px, size, AppContext, BackgroundExecutor, BorrowAppContext, Model, Modifiers, MouseButton,
  12    MouseDownEvent, TestAppContext,
  13};
  14use language::{
  15    language_settings::{AllLanguageSettings, Formatter},
  16    tree_sitter_rust, Diagnostic, DiagnosticEntry, FakeLspAdapter, Language, LanguageConfig,
  17    LanguageMatcher, LineEnding, OffsetRangeExt, Point, Rope,
  18};
  19use live_kit_client::MacOSDisplay;
  20use lsp::LanguageServerId;
  21use project::{
  22    search::SearchQuery, DiagnosticSummary, FormatTrigger, HoverBlockKind, Project, ProjectPath,
  23    SearchResult,
  24};
  25use rand::prelude::*;
  26use serde_json::json;
  27use settings::SettingsStore;
  28use std::{
  29    cell::{Cell, RefCell},
  30    env, future, mem,
  31    path::{Path, PathBuf},
  32    rc::Rc,
  33    sync::{
  34        atomic::{AtomicBool, Ordering::SeqCst},
  35        Arc,
  36    },
  37    time::Duration,
  38};
  39use unindent::Unindent as _;
  40
  41#[ctor::ctor]
  42fn init_logger() {
  43    if std::env::var("RUST_LOG").is_ok() {
  44        env_logger::init();
  45    }
  46}
  47
  48#[gpui::test(iterations = 10)]
  49async fn test_basic_calls(
  50    executor: BackgroundExecutor,
  51    cx_a: &mut TestAppContext,
  52    cx_b: &mut TestAppContext,
  53    cx_b2: &mut TestAppContext,
  54    cx_c: &mut TestAppContext,
  55) {
  56    let mut server = TestServer::start(executor.clone()).await;
  57
  58    let client_a = server.create_client(cx_a, "user_a").await;
  59    let client_b = server.create_client(cx_b, "user_b").await;
  60    let client_c = server.create_client(cx_c, "user_c").await;
  61    server
  62        .make_contacts(&mut [(&client_a, cx_a), (&client_b, cx_b), (&client_c, cx_c)])
  63        .await;
  64
  65    let active_call_a = cx_a.read(ActiveCall::global);
  66    let active_call_b = cx_b.read(ActiveCall::global);
  67    let active_call_c = cx_c.read(ActiveCall::global);
  68
  69    // Call user B from client A.
  70    active_call_a
  71        .update(cx_a, |call, cx| {
  72            call.invite(client_b.user_id().unwrap(), None, cx)
  73        })
  74        .await
  75        .unwrap();
  76    let room_a = active_call_a.read_with(cx_a, |call, _| call.room().unwrap().clone());
  77    executor.run_until_parked();
  78    assert_eq!(
  79        room_participants(&room_a, cx_a),
  80        RoomParticipants {
  81            remote: Default::default(),
  82            pending: vec!["user_b".to_string()]
  83        }
  84    );
  85
  86    // User B receives the call.
  87
  88    let mut incoming_call_b = active_call_b.read_with(cx_b, |call, _| call.incoming());
  89    let call_b = incoming_call_b.next().await.unwrap().unwrap();
  90    assert_eq!(call_b.calling_user.github_login, "user_a");
  91
  92    // User B connects via another client and also receives a ring on the newly-connected client.
  93    let _client_b2 = server.create_client(cx_b2, "user_b").await;
  94    let active_call_b2 = cx_b2.read(ActiveCall::global);
  95
  96    let mut incoming_call_b2 = active_call_b2.read_with(cx_b2, |call, _| call.incoming());
  97    executor.run_until_parked();
  98    let call_b2 = incoming_call_b2.next().await.unwrap().unwrap();
  99    assert_eq!(call_b2.calling_user.github_login, "user_a");
 100
 101    // User B joins the room using the first client.
 102    active_call_b
 103        .update(cx_b, |call, cx| call.accept_incoming(cx))
 104        .await
 105        .unwrap();
 106
 107    let room_b = active_call_b.read_with(cx_b, |call, _| call.room().unwrap().clone());
 108    assert!(incoming_call_b.next().await.unwrap().is_none());
 109
 110    executor.run_until_parked();
 111    assert_eq!(
 112        room_participants(&room_a, cx_a),
 113        RoomParticipants {
 114            remote: vec!["user_b".to_string()],
 115            pending: Default::default()
 116        }
 117    );
 118    assert_eq!(
 119        room_participants(&room_b, cx_b),
 120        RoomParticipants {
 121            remote: vec!["user_a".to_string()],
 122            pending: Default::default()
 123        }
 124    );
 125
 126    // Call user C from client B.
 127
 128    let mut incoming_call_c = active_call_c.read_with(cx_c, |call, _| call.incoming());
 129    active_call_b
 130        .update(cx_b, |call, cx| {
 131            call.invite(client_c.user_id().unwrap(), None, cx)
 132        })
 133        .await
 134        .unwrap();
 135
 136    executor.run_until_parked();
 137    assert_eq!(
 138        room_participants(&room_a, cx_a),
 139        RoomParticipants {
 140            remote: vec!["user_b".to_string()],
 141            pending: vec!["user_c".to_string()]
 142        }
 143    );
 144    assert_eq!(
 145        room_participants(&room_b, cx_b),
 146        RoomParticipants {
 147            remote: vec!["user_a".to_string()],
 148            pending: vec!["user_c".to_string()]
 149        }
 150    );
 151
 152    // User C receives the call, but declines it.
 153    let call_c = incoming_call_c.next().await.unwrap().unwrap();
 154    assert_eq!(call_c.calling_user.github_login, "user_b");
 155    active_call_c.update(cx_c, |call, cx| call.decline_incoming(cx).unwrap());
 156    assert!(incoming_call_c.next().await.unwrap().is_none());
 157
 158    executor.run_until_parked();
 159    assert_eq!(
 160        room_participants(&room_a, cx_a),
 161        RoomParticipants {
 162            remote: vec!["user_b".to_string()],
 163            pending: Default::default()
 164        }
 165    );
 166    assert_eq!(
 167        room_participants(&room_b, cx_b),
 168        RoomParticipants {
 169            remote: vec!["user_a".to_string()],
 170            pending: Default::default()
 171        }
 172    );
 173
 174    // Call user C again from user A.
 175    active_call_a
 176        .update(cx_a, |call, cx| {
 177            call.invite(client_c.user_id().unwrap(), None, cx)
 178        })
 179        .await
 180        .unwrap();
 181
 182    executor.run_until_parked();
 183    assert_eq!(
 184        room_participants(&room_a, cx_a),
 185        RoomParticipants {
 186            remote: vec!["user_b".to_string()],
 187            pending: vec!["user_c".to_string()]
 188        }
 189    );
 190    assert_eq!(
 191        room_participants(&room_b, cx_b),
 192        RoomParticipants {
 193            remote: vec!["user_a".to_string()],
 194            pending: vec!["user_c".to_string()]
 195        }
 196    );
 197
 198    // User C accepts the call.
 199    let call_c = incoming_call_c.next().await.unwrap().unwrap();
 200    assert_eq!(call_c.calling_user.github_login, "user_a");
 201    active_call_c
 202        .update(cx_c, |call, cx| call.accept_incoming(cx))
 203        .await
 204        .unwrap();
 205    assert!(incoming_call_c.next().await.unwrap().is_none());
 206
 207    let room_c = active_call_c.read_with(cx_c, |call, _| call.room().unwrap().clone());
 208
 209    executor.run_until_parked();
 210    assert_eq!(
 211        room_participants(&room_a, cx_a),
 212        RoomParticipants {
 213            remote: vec!["user_b".to_string(), "user_c".to_string()],
 214            pending: Default::default()
 215        }
 216    );
 217    assert_eq!(
 218        room_participants(&room_b, cx_b),
 219        RoomParticipants {
 220            remote: vec!["user_a".to_string(), "user_c".to_string()],
 221            pending: Default::default()
 222        }
 223    );
 224    assert_eq!(
 225        room_participants(&room_c, cx_c),
 226        RoomParticipants {
 227            remote: vec!["user_a".to_string(), "user_b".to_string()],
 228            pending: Default::default()
 229        }
 230    );
 231
 232    // User A shares their screen
 233    let display = MacOSDisplay::new();
 234    let events_b = active_call_events(cx_b);
 235    let events_c = active_call_events(cx_c);
 236    active_call_a
 237        .update(cx_a, |call, cx| {
 238            call.room().unwrap().update(cx, |room, cx| {
 239                room.set_display_sources(vec![display.clone()]);
 240                room.share_screen(cx)
 241            })
 242        })
 243        .await
 244        .unwrap();
 245
 246    executor.run_until_parked();
 247
 248    // User B observes the remote screen sharing track.
 249    assert_eq!(events_b.borrow().len(), 1);
 250    let event_b = events_b.borrow().first().unwrap().clone();
 251    if let call::room::Event::RemoteVideoTracksChanged { participant_id } = event_b {
 252        assert_eq!(participant_id, client_a.peer_id().unwrap());
 253
 254        room_b.read_with(cx_b, |room, _| {
 255            assert_eq!(
 256                room.remote_participants()[&client_a.user_id().unwrap()]
 257                    .video_tracks
 258                    .len(),
 259                1
 260            );
 261        });
 262    } else {
 263        panic!("unexpected event")
 264    }
 265
 266    // User C observes the remote screen sharing track.
 267    assert_eq!(events_c.borrow().len(), 1);
 268    let event_c = events_c.borrow().first().unwrap().clone();
 269    if let call::room::Event::RemoteVideoTracksChanged { participant_id } = event_c {
 270        assert_eq!(participant_id, client_a.peer_id().unwrap());
 271
 272        room_c.read_with(cx_c, |room, _| {
 273            assert_eq!(
 274                room.remote_participants()[&client_a.user_id().unwrap()]
 275                    .video_tracks
 276                    .len(),
 277                1
 278            );
 279        });
 280    } else {
 281        panic!("unexpected event")
 282    }
 283
 284    // User A leaves the room.
 285    active_call_a
 286        .update(cx_a, |call, cx| {
 287            let hang_up = call.hang_up(cx);
 288            assert!(call.room().is_none());
 289            hang_up
 290        })
 291        .await
 292        .unwrap();
 293    executor.run_until_parked();
 294    assert_eq!(
 295        room_participants(&room_a, cx_a),
 296        RoomParticipants {
 297            remote: Default::default(),
 298            pending: Default::default()
 299        }
 300    );
 301    assert_eq!(
 302        room_participants(&room_b, cx_b),
 303        RoomParticipants {
 304            remote: vec!["user_c".to_string()],
 305            pending: Default::default()
 306        }
 307    );
 308    assert_eq!(
 309        room_participants(&room_c, cx_c),
 310        RoomParticipants {
 311            remote: vec!["user_b".to_string()],
 312            pending: Default::default()
 313        }
 314    );
 315
 316    // User B gets disconnected from the LiveKit server, which causes them
 317    // to automatically leave the room. User C leaves the room as well because
 318    // nobody else is in there.
 319    server
 320        .test_live_kit_server
 321        .disconnect_client(client_b.user_id().unwrap().to_string())
 322        .await;
 323    executor.run_until_parked();
 324
 325    active_call_b.read_with(cx_b, |call, _| assert!(call.room().is_none()));
 326
 327    active_call_c.read_with(cx_c, |call, _| assert!(call.room().is_none()));
 328    assert_eq!(
 329        room_participants(&room_a, cx_a),
 330        RoomParticipants {
 331            remote: Default::default(),
 332            pending: Default::default()
 333        }
 334    );
 335    assert_eq!(
 336        room_participants(&room_b, cx_b),
 337        RoomParticipants {
 338            remote: Default::default(),
 339            pending: Default::default()
 340        }
 341    );
 342    assert_eq!(
 343        room_participants(&room_c, cx_c),
 344        RoomParticipants {
 345            remote: Default::default(),
 346            pending: Default::default()
 347        }
 348    );
 349}
 350
 351#[gpui::test(iterations = 10)]
 352async fn test_calling_multiple_users_simultaneously(
 353    executor: BackgroundExecutor,
 354    cx_a: &mut TestAppContext,
 355    cx_b: &mut TestAppContext,
 356    cx_c: &mut TestAppContext,
 357    cx_d: &mut TestAppContext,
 358) {
 359    let mut server = TestServer::start(executor.clone()).await;
 360
 361    let client_a = server.create_client(cx_a, "user_a").await;
 362    let client_b = server.create_client(cx_b, "user_b").await;
 363    let client_c = server.create_client(cx_c, "user_c").await;
 364    let client_d = server.create_client(cx_d, "user_d").await;
 365    server
 366        .make_contacts(&mut [
 367            (&client_a, cx_a),
 368            (&client_b, cx_b),
 369            (&client_c, cx_c),
 370            (&client_d, cx_d),
 371        ])
 372        .await;
 373
 374    let active_call_a = cx_a.read(ActiveCall::global);
 375    let active_call_b = cx_b.read(ActiveCall::global);
 376    let active_call_c = cx_c.read(ActiveCall::global);
 377    let active_call_d = cx_d.read(ActiveCall::global);
 378
 379    // Simultaneously call user B and user C from client A.
 380    let b_invite = active_call_a.update(cx_a, |call, cx| {
 381        call.invite(client_b.user_id().unwrap(), None, cx)
 382    });
 383    let c_invite = active_call_a.update(cx_a, |call, cx| {
 384        call.invite(client_c.user_id().unwrap(), None, cx)
 385    });
 386    b_invite.await.unwrap();
 387    c_invite.await.unwrap();
 388
 389    let room_a = active_call_a.read_with(cx_a, |call, _| call.room().unwrap().clone());
 390    executor.run_until_parked();
 391    assert_eq!(
 392        room_participants(&room_a, cx_a),
 393        RoomParticipants {
 394            remote: Default::default(),
 395            pending: vec!["user_b".to_string(), "user_c".to_string()]
 396        }
 397    );
 398
 399    // Call client D from client A.
 400    active_call_a
 401        .update(cx_a, |call, cx| {
 402            call.invite(client_d.user_id().unwrap(), None, cx)
 403        })
 404        .await
 405        .unwrap();
 406    executor.run_until_parked();
 407    assert_eq!(
 408        room_participants(&room_a, cx_a),
 409        RoomParticipants {
 410            remote: Default::default(),
 411            pending: vec![
 412                "user_b".to_string(),
 413                "user_c".to_string(),
 414                "user_d".to_string()
 415            ]
 416        }
 417    );
 418
 419    // Accept the call on all clients simultaneously.
 420    let accept_b = active_call_b.update(cx_b, |call, cx| call.accept_incoming(cx));
 421    let accept_c = active_call_c.update(cx_c, |call, cx| call.accept_incoming(cx));
 422    let accept_d = active_call_d.update(cx_d, |call, cx| call.accept_incoming(cx));
 423    accept_b.await.unwrap();
 424    accept_c.await.unwrap();
 425    accept_d.await.unwrap();
 426
 427    executor.run_until_parked();
 428
 429    let room_b = active_call_b.read_with(cx_b, |call, _| call.room().unwrap().clone());
 430
 431    let room_c = active_call_c.read_with(cx_c, |call, _| call.room().unwrap().clone());
 432
 433    let room_d = active_call_d.read_with(cx_d, |call, _| call.room().unwrap().clone());
 434    assert_eq!(
 435        room_participants(&room_a, cx_a),
 436        RoomParticipants {
 437            remote: vec![
 438                "user_b".to_string(),
 439                "user_c".to_string(),
 440                "user_d".to_string(),
 441            ],
 442            pending: Default::default()
 443        }
 444    );
 445    assert_eq!(
 446        room_participants(&room_b, cx_b),
 447        RoomParticipants {
 448            remote: vec![
 449                "user_a".to_string(),
 450                "user_c".to_string(),
 451                "user_d".to_string(),
 452            ],
 453            pending: Default::default()
 454        }
 455    );
 456    assert_eq!(
 457        room_participants(&room_c, cx_c),
 458        RoomParticipants {
 459            remote: vec![
 460                "user_a".to_string(),
 461                "user_b".to_string(),
 462                "user_d".to_string(),
 463            ],
 464            pending: Default::default()
 465        }
 466    );
 467    assert_eq!(
 468        room_participants(&room_d, cx_d),
 469        RoomParticipants {
 470            remote: vec![
 471                "user_a".to_string(),
 472                "user_b".to_string(),
 473                "user_c".to_string(),
 474            ],
 475            pending: Default::default()
 476        }
 477    );
 478}
 479
 480#[gpui::test(iterations = 10)]
 481async fn test_joining_channels_and_calling_multiple_users_simultaneously(
 482    executor: BackgroundExecutor,
 483    cx_a: &mut TestAppContext,
 484    cx_b: &mut TestAppContext,
 485    cx_c: &mut TestAppContext,
 486) {
 487    let mut server = TestServer::start(executor.clone()).await;
 488
 489    let client_a = server.create_client(cx_a, "user_a").await;
 490    let client_b = server.create_client(cx_b, "user_b").await;
 491    let client_c = server.create_client(cx_c, "user_c").await;
 492    server
 493        .make_contacts(&mut [(&client_a, cx_a), (&client_b, cx_b), (&client_c, cx_c)])
 494        .await;
 495
 496    let channel_1 = server
 497        .make_channel(
 498            "channel1",
 499            None,
 500            (&client_a, cx_a),
 501            &mut [(&client_b, cx_b), (&client_c, cx_c)],
 502        )
 503        .await;
 504
 505    let channel_2 = server
 506        .make_channel(
 507            "channel2",
 508            None,
 509            (&client_a, cx_a),
 510            &mut [(&client_b, cx_b), (&client_c, cx_c)],
 511        )
 512        .await;
 513
 514    let active_call_a = cx_a.read(ActiveCall::global);
 515
 516    // Simultaneously join channel 1 and then channel 2
 517    active_call_a
 518        .update(cx_a, |call, cx| call.join_channel(channel_1, cx))
 519        .detach();
 520    let join_channel_2 = active_call_a.update(cx_a, |call, cx| call.join_channel(channel_2, cx));
 521
 522    join_channel_2.await.unwrap();
 523
 524    let room_a = active_call_a.read_with(cx_a, |call, _| call.room().unwrap().clone());
 525    executor.run_until_parked();
 526
 527    assert_eq!(channel_id(&room_a, cx_a), Some(channel_2));
 528
 529    // Leave the room
 530    active_call_a
 531        .update(cx_a, |call, cx| {
 532            let hang_up = call.hang_up(cx);
 533            hang_up
 534        })
 535        .await
 536        .unwrap();
 537
 538    // Initiating invites and then joining a channel should fail gracefully
 539    let b_invite = active_call_a.update(cx_a, |call, cx| {
 540        call.invite(client_b.user_id().unwrap(), None, cx)
 541    });
 542    let c_invite = active_call_a.update(cx_a, |call, cx| {
 543        call.invite(client_c.user_id().unwrap(), None, cx)
 544    });
 545
 546    let join_channel = active_call_a.update(cx_a, |call, cx| call.join_channel(channel_1, cx));
 547
 548    b_invite.await.unwrap();
 549    c_invite.await.unwrap();
 550    join_channel.await.unwrap();
 551
 552    let room_a = active_call_a.read_with(cx_a, |call, _| call.room().unwrap().clone());
 553    executor.run_until_parked();
 554
 555    assert_eq!(
 556        room_participants(&room_a, cx_a),
 557        RoomParticipants {
 558            remote: Default::default(),
 559            pending: vec!["user_b".to_string(), "user_c".to_string()]
 560        }
 561    );
 562
 563    assert_eq!(channel_id(&room_a, cx_a), None);
 564
 565    // Leave the room
 566    active_call_a
 567        .update(cx_a, |call, cx| {
 568            let hang_up = call.hang_up(cx);
 569            hang_up
 570        })
 571        .await
 572        .unwrap();
 573
 574    // Simultaneously join channel 1 and call user B and user C from client A.
 575    let join_channel = active_call_a.update(cx_a, |call, cx| call.join_channel(channel_1, cx));
 576
 577    let b_invite = active_call_a.update(cx_a, |call, cx| {
 578        call.invite(client_b.user_id().unwrap(), None, cx)
 579    });
 580    let c_invite = active_call_a.update(cx_a, |call, cx| {
 581        call.invite(client_c.user_id().unwrap(), None, cx)
 582    });
 583
 584    join_channel.await.unwrap();
 585    b_invite.await.unwrap();
 586    c_invite.await.unwrap();
 587
 588    active_call_a.read_with(cx_a, |call, _| call.room().unwrap().clone());
 589    executor.run_until_parked();
 590}
 591
 592#[gpui::test(iterations = 10)]
 593async fn test_room_uniqueness(
 594    executor: BackgroundExecutor,
 595    cx_a: &mut TestAppContext,
 596    cx_a2: &mut TestAppContext,
 597    cx_b: &mut TestAppContext,
 598    cx_b2: &mut TestAppContext,
 599    cx_c: &mut TestAppContext,
 600) {
 601    let mut server = TestServer::start(executor.clone()).await;
 602    let client_a = server.create_client(cx_a, "user_a").await;
 603    let _client_a2 = server.create_client(cx_a2, "user_a").await;
 604    let client_b = server.create_client(cx_b, "user_b").await;
 605    let _client_b2 = server.create_client(cx_b2, "user_b").await;
 606    let client_c = server.create_client(cx_c, "user_c").await;
 607    server
 608        .make_contacts(&mut [(&client_a, cx_a), (&client_b, cx_b), (&client_c, cx_c)])
 609        .await;
 610
 611    let active_call_a = cx_a.read(ActiveCall::global);
 612    let active_call_a2 = cx_a2.read(ActiveCall::global);
 613    let active_call_b = cx_b.read(ActiveCall::global);
 614    let active_call_b2 = cx_b2.read(ActiveCall::global);
 615    let active_call_c = cx_c.read(ActiveCall::global);
 616
 617    // Call user B from client A.
 618    active_call_a
 619        .update(cx_a, |call, cx| {
 620            call.invite(client_b.user_id().unwrap(), None, cx)
 621        })
 622        .await
 623        .unwrap();
 624
 625    // Ensure a new room can't be created given user A just created one.
 626    active_call_a2
 627        .update(cx_a2, |call, cx| {
 628            call.invite(client_c.user_id().unwrap(), None, cx)
 629        })
 630        .await
 631        .unwrap_err();
 632
 633    active_call_a2.read_with(cx_a2, |call, _| assert!(call.room().is_none()));
 634
 635    // User B receives the call from user A.
 636
 637    let mut incoming_call_b = active_call_b.read_with(cx_b, |call, _| call.incoming());
 638    let call_b1 = incoming_call_b.next().await.unwrap().unwrap();
 639    assert_eq!(call_b1.calling_user.github_login, "user_a");
 640
 641    // Ensure calling users A and B from client C fails.
 642    active_call_c
 643        .update(cx_c, |call, cx| {
 644            call.invite(client_a.user_id().unwrap(), None, cx)
 645        })
 646        .await
 647        .unwrap_err();
 648    active_call_c
 649        .update(cx_c, |call, cx| {
 650            call.invite(client_b.user_id().unwrap(), None, cx)
 651        })
 652        .await
 653        .unwrap_err();
 654
 655    // Ensure User B can't create a room while they still have an incoming call.
 656    active_call_b2
 657        .update(cx_b2, |call, cx| {
 658            call.invite(client_c.user_id().unwrap(), None, cx)
 659        })
 660        .await
 661        .unwrap_err();
 662
 663    active_call_b2.read_with(cx_b2, |call, _| assert!(call.room().is_none()));
 664
 665    // User B joins the room and calling them after they've joined still fails.
 666    active_call_b
 667        .update(cx_b, |call, cx| call.accept_incoming(cx))
 668        .await
 669        .unwrap();
 670    active_call_c
 671        .update(cx_c, |call, cx| {
 672            call.invite(client_b.user_id().unwrap(), None, cx)
 673        })
 674        .await
 675        .unwrap_err();
 676
 677    // Ensure User B can't create a room while they belong to another room.
 678    active_call_b2
 679        .update(cx_b2, |call, cx| {
 680            call.invite(client_c.user_id().unwrap(), None, cx)
 681        })
 682        .await
 683        .unwrap_err();
 684
 685    active_call_b2.read_with(cx_b2, |call, _| assert!(call.room().is_none()));
 686
 687    // Client C can successfully call client B after client B leaves the room.
 688    active_call_b
 689        .update(cx_b, |call, cx| call.hang_up(cx))
 690        .await
 691        .unwrap();
 692    executor.run_until_parked();
 693    active_call_c
 694        .update(cx_c, |call, cx| {
 695            call.invite(client_b.user_id().unwrap(), None, cx)
 696        })
 697        .await
 698        .unwrap();
 699    executor.run_until_parked();
 700    let call_b2 = incoming_call_b.next().await.unwrap().unwrap();
 701    assert_eq!(call_b2.calling_user.github_login, "user_c");
 702}
 703
 704#[gpui::test(iterations = 10)]
 705async fn test_client_disconnecting_from_room(
 706    executor: BackgroundExecutor,
 707    cx_a: &mut TestAppContext,
 708    cx_b: &mut TestAppContext,
 709) {
 710    let mut server = TestServer::start(executor.clone()).await;
 711    let client_a = server.create_client(cx_a, "user_a").await;
 712    let client_b = server.create_client(cx_b, "user_b").await;
 713    server
 714        .make_contacts(&mut [(&client_a, cx_a), (&client_b, cx_b)])
 715        .await;
 716
 717    let active_call_a = cx_a.read(ActiveCall::global);
 718    let active_call_b = cx_b.read(ActiveCall::global);
 719
 720    // Call user B from client A.
 721    active_call_a
 722        .update(cx_a, |call, cx| {
 723            call.invite(client_b.user_id().unwrap(), None, cx)
 724        })
 725        .await
 726        .unwrap();
 727
 728    let room_a = active_call_a.read_with(cx_a, |call, _| call.room().unwrap().clone());
 729
 730    // User B receives the call and joins the room.
 731
 732    let mut incoming_call_b = active_call_b.read_with(cx_b, |call, _| call.incoming());
 733    incoming_call_b.next().await.unwrap().unwrap();
 734    active_call_b
 735        .update(cx_b, |call, cx| call.accept_incoming(cx))
 736        .await
 737        .unwrap();
 738
 739    let room_b = active_call_b.read_with(cx_b, |call, _| call.room().unwrap().clone());
 740    executor.run_until_parked();
 741    assert_eq!(
 742        room_participants(&room_a, cx_a),
 743        RoomParticipants {
 744            remote: vec!["user_b".to_string()],
 745            pending: Default::default()
 746        }
 747    );
 748    assert_eq!(
 749        room_participants(&room_b, cx_b),
 750        RoomParticipants {
 751            remote: vec!["user_a".to_string()],
 752            pending: Default::default()
 753        }
 754    );
 755
 756    // User A automatically reconnects to the room upon disconnection.
 757    server.disconnect_client(client_a.peer_id().unwrap());
 758    executor.advance_clock(RECEIVE_TIMEOUT);
 759    executor.run_until_parked();
 760    assert_eq!(
 761        room_participants(&room_a, cx_a),
 762        RoomParticipants {
 763            remote: vec!["user_b".to_string()],
 764            pending: Default::default()
 765        }
 766    );
 767    assert_eq!(
 768        room_participants(&room_b, cx_b),
 769        RoomParticipants {
 770            remote: vec!["user_a".to_string()],
 771            pending: Default::default()
 772        }
 773    );
 774
 775    // When user A disconnects, both client A and B clear their room on the active call.
 776    server.forbid_connections();
 777    server.disconnect_client(client_a.peer_id().unwrap());
 778    executor.advance_clock(RECEIVE_TIMEOUT + RECONNECT_TIMEOUT);
 779
 780    active_call_a.read_with(cx_a, |call, _| assert!(call.room().is_none()));
 781
 782    active_call_b.read_with(cx_b, |call, _| assert!(call.room().is_none()));
 783    assert_eq!(
 784        room_participants(&room_a, cx_a),
 785        RoomParticipants {
 786            remote: Default::default(),
 787            pending: Default::default()
 788        }
 789    );
 790    assert_eq!(
 791        room_participants(&room_b, cx_b),
 792        RoomParticipants {
 793            remote: Default::default(),
 794            pending: Default::default()
 795        }
 796    );
 797
 798    // Allow user A to reconnect to the server.
 799    server.allow_connections();
 800    executor.advance_clock(RECEIVE_TIMEOUT);
 801
 802    // Call user B again from client A.
 803    active_call_a
 804        .update(cx_a, |call, cx| {
 805            call.invite(client_b.user_id().unwrap(), None, cx)
 806        })
 807        .await
 808        .unwrap();
 809
 810    let room_a = active_call_a.read_with(cx_a, |call, _| call.room().unwrap().clone());
 811
 812    // User B receives the call and joins the room.
 813
 814    let mut incoming_call_b = active_call_b.read_with(cx_b, |call, _| call.incoming());
 815    incoming_call_b.next().await.unwrap().unwrap();
 816    active_call_b
 817        .update(cx_b, |call, cx| call.accept_incoming(cx))
 818        .await
 819        .unwrap();
 820
 821    let room_b = active_call_b.read_with(cx_b, |call, _| call.room().unwrap().clone());
 822    executor.run_until_parked();
 823    assert_eq!(
 824        room_participants(&room_a, cx_a),
 825        RoomParticipants {
 826            remote: vec!["user_b".to_string()],
 827            pending: Default::default()
 828        }
 829    );
 830    assert_eq!(
 831        room_participants(&room_b, cx_b),
 832        RoomParticipants {
 833            remote: vec!["user_a".to_string()],
 834            pending: Default::default()
 835        }
 836    );
 837
 838    // User B gets disconnected from the LiveKit server, which causes it
 839    // to automatically leave the room.
 840    server
 841        .test_live_kit_server
 842        .disconnect_client(client_b.user_id().unwrap().to_string())
 843        .await;
 844    executor.run_until_parked();
 845    active_call_a.update(cx_a, |call, _| assert!(call.room().is_none()));
 846    active_call_b.update(cx_b, |call, _| assert!(call.room().is_none()));
 847    assert_eq!(
 848        room_participants(&room_a, cx_a),
 849        RoomParticipants {
 850            remote: Default::default(),
 851            pending: Default::default()
 852        }
 853    );
 854    assert_eq!(
 855        room_participants(&room_b, cx_b),
 856        RoomParticipants {
 857            remote: Default::default(),
 858            pending: Default::default()
 859        }
 860    );
 861}
 862
 863#[gpui::test(iterations = 10)]
 864async fn test_server_restarts(
 865    executor: BackgroundExecutor,
 866    cx_a: &mut TestAppContext,
 867    cx_b: &mut TestAppContext,
 868    cx_c: &mut TestAppContext,
 869    cx_d: &mut TestAppContext,
 870) {
 871    let mut server = TestServer::start(executor.clone()).await;
 872    let client_a = server.create_client(cx_a, "user_a").await;
 873    client_a
 874        .fs()
 875        .insert_tree("/a", json!({ "a.txt": "a-contents" }))
 876        .await;
 877
 878    // Invite client B to collaborate on a project
 879    let (project_a, _) = client_a.build_local_project("/a", cx_a).await;
 880
 881    let client_b = server.create_client(cx_b, "user_b").await;
 882    let client_c = server.create_client(cx_c, "user_c").await;
 883    let client_d = server.create_client(cx_d, "user_d").await;
 884    server
 885        .make_contacts(&mut [
 886            (&client_a, cx_a),
 887            (&client_b, cx_b),
 888            (&client_c, cx_c),
 889            (&client_d, cx_d),
 890        ])
 891        .await;
 892
 893    let active_call_a = cx_a.read(ActiveCall::global);
 894    let active_call_b = cx_b.read(ActiveCall::global);
 895    let active_call_c = cx_c.read(ActiveCall::global);
 896    let active_call_d = cx_d.read(ActiveCall::global);
 897
 898    // User A calls users B, C, and D.
 899    active_call_a
 900        .update(cx_a, |call, cx| {
 901            call.invite(client_b.user_id().unwrap(), Some(project_a.clone()), cx)
 902        })
 903        .await
 904        .unwrap();
 905    active_call_a
 906        .update(cx_a, |call, cx| {
 907            call.invite(client_c.user_id().unwrap(), Some(project_a.clone()), cx)
 908        })
 909        .await
 910        .unwrap();
 911    active_call_a
 912        .update(cx_a, |call, cx| {
 913            call.invite(client_d.user_id().unwrap(), Some(project_a.clone()), cx)
 914        })
 915        .await
 916        .unwrap();
 917
 918    let room_a = active_call_a.read_with(cx_a, |call, _| call.room().unwrap().clone());
 919
 920    // User B receives the call and joins the room.
 921
 922    let mut incoming_call_b = active_call_b.read_with(cx_b, |call, _| call.incoming());
 923    assert!(incoming_call_b.next().await.unwrap().is_some());
 924    active_call_b
 925        .update(cx_b, |call, cx| call.accept_incoming(cx))
 926        .await
 927        .unwrap();
 928
 929    let room_b = active_call_b.read_with(cx_b, |call, _| call.room().unwrap().clone());
 930
 931    // User C receives the call and joins the room.
 932
 933    let mut incoming_call_c = active_call_c.read_with(cx_c, |call, _| call.incoming());
 934    assert!(incoming_call_c.next().await.unwrap().is_some());
 935    active_call_c
 936        .update(cx_c, |call, cx| call.accept_incoming(cx))
 937        .await
 938        .unwrap();
 939
 940    let room_c = active_call_c.read_with(cx_c, |call, _| call.room().unwrap().clone());
 941
 942    // User D receives the call but doesn't join the room yet.
 943
 944    let mut incoming_call_d = active_call_d.read_with(cx_d, |call, _| call.incoming());
 945    assert!(incoming_call_d.next().await.unwrap().is_some());
 946
 947    executor.run_until_parked();
 948    assert_eq!(
 949        room_participants(&room_a, cx_a),
 950        RoomParticipants {
 951            remote: vec!["user_b".to_string(), "user_c".to_string()],
 952            pending: vec!["user_d".to_string()]
 953        }
 954    );
 955    assert_eq!(
 956        room_participants(&room_b, cx_b),
 957        RoomParticipants {
 958            remote: vec!["user_a".to_string(), "user_c".to_string()],
 959            pending: vec!["user_d".to_string()]
 960        }
 961    );
 962    assert_eq!(
 963        room_participants(&room_c, cx_c),
 964        RoomParticipants {
 965            remote: vec!["user_a".to_string(), "user_b".to_string()],
 966            pending: vec!["user_d".to_string()]
 967        }
 968    );
 969
 970    // The server is torn down.
 971    server.reset().await;
 972
 973    // Users A and B reconnect to the call. User C has troubles reconnecting, so it leaves the room.
 974    client_c.override_establish_connection(|_, cx| cx.spawn(|_| future::pending()));
 975    executor.advance_clock(RECONNECT_TIMEOUT);
 976    assert_eq!(
 977        room_participants(&room_a, cx_a),
 978        RoomParticipants {
 979            remote: vec!["user_b".to_string(), "user_c".to_string()],
 980            pending: vec!["user_d".to_string()]
 981        }
 982    );
 983    assert_eq!(
 984        room_participants(&room_b, cx_b),
 985        RoomParticipants {
 986            remote: vec!["user_a".to_string(), "user_c".to_string()],
 987            pending: vec!["user_d".to_string()]
 988        }
 989    );
 990    assert_eq!(
 991        room_participants(&room_c, cx_c),
 992        RoomParticipants {
 993            remote: vec![],
 994            pending: vec![]
 995        }
 996    );
 997
 998    // User D is notified again of the incoming call and accepts it.
 999    assert!(incoming_call_d.next().await.unwrap().is_some());
1000    active_call_d
1001        .update(cx_d, |call, cx| call.accept_incoming(cx))
1002        .await
1003        .unwrap();
1004    executor.run_until_parked();
1005
1006    let room_d = active_call_d.read_with(cx_d, |call, _| call.room().unwrap().clone());
1007    assert_eq!(
1008        room_participants(&room_a, cx_a),
1009        RoomParticipants {
1010            remote: vec![
1011                "user_b".to_string(),
1012                "user_c".to_string(),
1013                "user_d".to_string(),
1014            ],
1015            pending: vec![]
1016        }
1017    );
1018    assert_eq!(
1019        room_participants(&room_b, cx_b),
1020        RoomParticipants {
1021            remote: vec![
1022                "user_a".to_string(),
1023                "user_c".to_string(),
1024                "user_d".to_string(),
1025            ],
1026            pending: vec![]
1027        }
1028    );
1029    assert_eq!(
1030        room_participants(&room_c, cx_c),
1031        RoomParticipants {
1032            remote: vec![],
1033            pending: vec![]
1034        }
1035    );
1036    assert_eq!(
1037        room_participants(&room_d, cx_d),
1038        RoomParticipants {
1039            remote: vec![
1040                "user_a".to_string(),
1041                "user_b".to_string(),
1042                "user_c".to_string(),
1043            ],
1044            pending: vec![]
1045        }
1046    );
1047
1048    // The server finishes restarting, cleaning up stale connections.
1049    server.start().await.unwrap();
1050    executor.advance_clock(CLEANUP_TIMEOUT);
1051    assert_eq!(
1052        room_participants(&room_a, cx_a),
1053        RoomParticipants {
1054            remote: vec!["user_b".to_string(), "user_d".to_string()],
1055            pending: vec![]
1056        }
1057    );
1058    assert_eq!(
1059        room_participants(&room_b, cx_b),
1060        RoomParticipants {
1061            remote: vec!["user_a".to_string(), "user_d".to_string()],
1062            pending: vec![]
1063        }
1064    );
1065    assert_eq!(
1066        room_participants(&room_c, cx_c),
1067        RoomParticipants {
1068            remote: vec![],
1069            pending: vec![]
1070        }
1071    );
1072    assert_eq!(
1073        room_participants(&room_d, cx_d),
1074        RoomParticipants {
1075            remote: vec!["user_a".to_string(), "user_b".to_string()],
1076            pending: vec![]
1077        }
1078    );
1079
1080    // User D hangs up.
1081    active_call_d
1082        .update(cx_d, |call, cx| call.hang_up(cx))
1083        .await
1084        .unwrap();
1085    executor.run_until_parked();
1086    assert_eq!(
1087        room_participants(&room_a, cx_a),
1088        RoomParticipants {
1089            remote: vec!["user_b".to_string()],
1090            pending: vec![]
1091        }
1092    );
1093    assert_eq!(
1094        room_participants(&room_b, cx_b),
1095        RoomParticipants {
1096            remote: vec!["user_a".to_string()],
1097            pending: vec![]
1098        }
1099    );
1100    assert_eq!(
1101        room_participants(&room_c, cx_c),
1102        RoomParticipants {
1103            remote: vec![],
1104            pending: vec![]
1105        }
1106    );
1107    assert_eq!(
1108        room_participants(&room_d, cx_d),
1109        RoomParticipants {
1110            remote: vec![],
1111            pending: vec![]
1112        }
1113    );
1114
1115    // User B calls user D again.
1116    active_call_b
1117        .update(cx_b, |call, cx| {
1118            call.invite(client_d.user_id().unwrap(), None, cx)
1119        })
1120        .await
1121        .unwrap();
1122
1123    // User D receives the call but doesn't join the room yet.
1124
1125    let mut incoming_call_d = active_call_d.read_with(cx_d, |call, _| call.incoming());
1126    assert!(incoming_call_d.next().await.unwrap().is_some());
1127    executor.run_until_parked();
1128    assert_eq!(
1129        room_participants(&room_a, cx_a),
1130        RoomParticipants {
1131            remote: vec!["user_b".to_string()],
1132            pending: vec!["user_d".to_string()]
1133        }
1134    );
1135    assert_eq!(
1136        room_participants(&room_b, cx_b),
1137        RoomParticipants {
1138            remote: vec!["user_a".to_string()],
1139            pending: vec!["user_d".to_string()]
1140        }
1141    );
1142
1143    // The server is torn down.
1144    server.reset().await;
1145
1146    // Users A and B have troubles reconnecting, so they leave the room.
1147    client_a.override_establish_connection(|_, cx| cx.spawn(|_| future::pending()));
1148    client_b.override_establish_connection(|_, cx| cx.spawn(|_| future::pending()));
1149    client_c.override_establish_connection(|_, cx| cx.spawn(|_| future::pending()));
1150    executor.advance_clock(RECONNECT_TIMEOUT);
1151    assert_eq!(
1152        room_participants(&room_a, cx_a),
1153        RoomParticipants {
1154            remote: vec![],
1155            pending: vec![]
1156        }
1157    );
1158    assert_eq!(
1159        room_participants(&room_b, cx_b),
1160        RoomParticipants {
1161            remote: vec![],
1162            pending: vec![]
1163        }
1164    );
1165
1166    // User D is notified again of the incoming call but doesn't accept it.
1167    assert!(incoming_call_d.next().await.unwrap().is_some());
1168
1169    // The server finishes restarting, cleaning up stale connections and canceling the
1170    // call to user D because the room has become empty.
1171    server.start().await.unwrap();
1172    executor.advance_clock(CLEANUP_TIMEOUT);
1173    assert!(incoming_call_d.next().await.unwrap().is_none());
1174}
1175
1176#[gpui::test(iterations = 10)]
1177async fn test_calls_on_multiple_connections(
1178    executor: BackgroundExecutor,
1179    cx_a: &mut TestAppContext,
1180    cx_b1: &mut TestAppContext,
1181    cx_b2: &mut TestAppContext,
1182) {
1183    let mut server = TestServer::start(executor.clone()).await;
1184    let client_a = server.create_client(cx_a, "user_a").await;
1185    let client_b1 = server.create_client(cx_b1, "user_b").await;
1186    let client_b2 = server.create_client(cx_b2, "user_b").await;
1187    server
1188        .make_contacts(&mut [(&client_a, cx_a), (&client_b1, cx_b1)])
1189        .await;
1190
1191    let active_call_a = cx_a.read(ActiveCall::global);
1192    let active_call_b1 = cx_b1.read(ActiveCall::global);
1193    let active_call_b2 = cx_b2.read(ActiveCall::global);
1194
1195    let mut incoming_call_b1 = active_call_b1.read_with(cx_b1, |call, _| call.incoming());
1196
1197    let mut incoming_call_b2 = active_call_b2.read_with(cx_b2, |call, _| call.incoming());
1198    assert!(incoming_call_b1.next().await.unwrap().is_none());
1199    assert!(incoming_call_b2.next().await.unwrap().is_none());
1200
1201    // Call user B from client A, ensuring both clients for user B ring.
1202    active_call_a
1203        .update(cx_a, |call, cx| {
1204            call.invite(client_b1.user_id().unwrap(), None, cx)
1205        })
1206        .await
1207        .unwrap();
1208    executor.run_until_parked();
1209    assert!(incoming_call_b1.next().await.unwrap().is_some());
1210    assert!(incoming_call_b2.next().await.unwrap().is_some());
1211
1212    // User B declines the call on one of the two connections, causing both connections
1213    // to stop ringing.
1214    active_call_b2.update(cx_b2, |call, cx| call.decline_incoming(cx).unwrap());
1215    executor.run_until_parked();
1216    assert!(incoming_call_b1.next().await.unwrap().is_none());
1217    assert!(incoming_call_b2.next().await.unwrap().is_none());
1218
1219    // Call user B again from client A.
1220    active_call_a
1221        .update(cx_a, |call, cx| {
1222            call.invite(client_b1.user_id().unwrap(), None, cx)
1223        })
1224        .await
1225        .unwrap();
1226    executor.run_until_parked();
1227    assert!(incoming_call_b1.next().await.unwrap().is_some());
1228    assert!(incoming_call_b2.next().await.unwrap().is_some());
1229
1230    // User B accepts the call on one of the two connections, causing both connections
1231    // to stop ringing.
1232    active_call_b2
1233        .update(cx_b2, |call, cx| call.accept_incoming(cx))
1234        .await
1235        .unwrap();
1236    executor.run_until_parked();
1237    assert!(incoming_call_b1.next().await.unwrap().is_none());
1238    assert!(incoming_call_b2.next().await.unwrap().is_none());
1239
1240    // User B disconnects the client that is not on the call. Everything should be fine.
1241    client_b1.disconnect(&cx_b1.to_async());
1242    executor.advance_clock(RECEIVE_TIMEOUT);
1243    client_b1
1244        .authenticate_and_connect(false, &cx_b1.to_async())
1245        .await
1246        .unwrap();
1247
1248    // User B hangs up, and user A calls them again.
1249    active_call_b2
1250        .update(cx_b2, |call, cx| call.hang_up(cx))
1251        .await
1252        .unwrap();
1253    executor.run_until_parked();
1254    active_call_a
1255        .update(cx_a, |call, cx| {
1256            call.invite(client_b1.user_id().unwrap(), None, cx)
1257        })
1258        .await
1259        .unwrap();
1260    executor.run_until_parked();
1261    assert!(incoming_call_b1.next().await.unwrap().is_some());
1262    assert!(incoming_call_b2.next().await.unwrap().is_some());
1263
1264    // User A cancels the call, causing both connections to stop ringing.
1265    active_call_a
1266        .update(cx_a, |call, cx| {
1267            call.cancel_invite(client_b1.user_id().unwrap(), cx)
1268        })
1269        .await
1270        .unwrap();
1271    executor.run_until_parked();
1272    assert!(incoming_call_b1.next().await.unwrap().is_none());
1273    assert!(incoming_call_b2.next().await.unwrap().is_none());
1274
1275    // User A calls user B again.
1276    active_call_a
1277        .update(cx_a, |call, cx| {
1278            call.invite(client_b1.user_id().unwrap(), None, cx)
1279        })
1280        .await
1281        .unwrap();
1282    executor.run_until_parked();
1283    assert!(incoming_call_b1.next().await.unwrap().is_some());
1284    assert!(incoming_call_b2.next().await.unwrap().is_some());
1285
1286    // User A hangs up, causing both connections to stop ringing.
1287    active_call_a
1288        .update(cx_a, |call, cx| call.hang_up(cx))
1289        .await
1290        .unwrap();
1291    executor.run_until_parked();
1292    assert!(incoming_call_b1.next().await.unwrap().is_none());
1293    assert!(incoming_call_b2.next().await.unwrap().is_none());
1294
1295    // User A calls user B again.
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 disconnects, causing both connections to stop ringing.
1307    server.forbid_connections();
1308    server.disconnect_client(client_a.peer_id().unwrap());
1309    executor.advance_clock(RECEIVE_TIMEOUT + RECONNECT_TIMEOUT);
1310    assert!(incoming_call_b1.next().await.unwrap().is_none());
1311    assert!(incoming_call_b2.next().await.unwrap().is_none());
1312
1313    // User A reconnects automatically, then calls user B again.
1314    server.allow_connections();
1315    executor.advance_clock(RECEIVE_TIMEOUT);
1316    active_call_a
1317        .update(cx_a, |call, cx| {
1318            call.invite(client_b1.user_id().unwrap(), None, cx)
1319        })
1320        .await
1321        .unwrap();
1322    executor.run_until_parked();
1323    assert!(incoming_call_b1.next().await.unwrap().is_some());
1324    assert!(incoming_call_b2.next().await.unwrap().is_some());
1325
1326    // User B disconnects all clients, causing user A to no longer see a pending call for them.
1327    server.forbid_connections();
1328    server.disconnect_client(client_b1.peer_id().unwrap());
1329    server.disconnect_client(client_b2.peer_id().unwrap());
1330    executor.advance_clock(RECEIVE_TIMEOUT + RECONNECT_TIMEOUT);
1331
1332    active_call_a.read_with(cx_a, |call, _| assert!(call.room().is_none()));
1333}
1334
1335#[gpui::test(iterations = 10)]
1336async fn test_unshare_project(
1337    executor: BackgroundExecutor,
1338    cx_a: &mut TestAppContext,
1339    cx_b: &mut TestAppContext,
1340    cx_c: &mut TestAppContext,
1341) {
1342    let mut server = TestServer::start(executor.clone()).await;
1343    let client_a = server.create_client(cx_a, "user_a").await;
1344    let client_b = server.create_client(cx_b, "user_b").await;
1345    let client_c = server.create_client(cx_c, "user_c").await;
1346    server
1347        .create_room(&mut [(&client_a, cx_a), (&client_b, cx_b), (&client_c, cx_c)])
1348        .await;
1349
1350    let active_call_a = cx_a.read(ActiveCall::global);
1351    let active_call_b = cx_b.read(ActiveCall::global);
1352
1353    client_a
1354        .fs()
1355        .insert_tree(
1356            "/a",
1357            json!({
1358                "a.txt": "a-contents",
1359                "b.txt": "b-contents",
1360            }),
1361        )
1362        .await;
1363
1364    let (project_a, worktree_id) = client_a.build_local_project("/a", cx_a).await;
1365    let project_id = active_call_a
1366        .update(cx_a, |call, cx| call.share_project(project_a.clone(), cx))
1367        .await
1368        .unwrap();
1369
1370    let worktree_a = project_a.read_with(cx_a, |project, _| project.worktrees().next().unwrap());
1371    let project_b = client_b.build_remote_project(project_id, cx_b).await;
1372    executor.run_until_parked();
1373
1374    assert!(worktree_a.read_with(cx_a, |tree, _| tree.as_local().unwrap().is_shared()));
1375
1376    project_b
1377        .update(cx_b, |p, cx| p.open_buffer((worktree_id, "a.txt"), cx))
1378        .await
1379        .unwrap();
1380
1381    // When client B leaves the room, the project becomes read-only.
1382    active_call_b
1383        .update(cx_b, |call, cx| call.hang_up(cx))
1384        .await
1385        .unwrap();
1386    executor.run_until_parked();
1387
1388    assert!(project_b.read_with(cx_b, |project, _| project.is_disconnected()));
1389
1390    // Client C opens the project.
1391    let project_c = client_c.build_remote_project(project_id, cx_c).await;
1392
1393    // When client A unshares the project, client C's project becomes read-only.
1394    project_a
1395        .update(cx_a, |project, cx| project.unshare(cx))
1396        .unwrap();
1397    executor.run_until_parked();
1398
1399    assert!(worktree_a.read_with(cx_a, |tree, _| !tree.as_local().unwrap().is_shared()));
1400
1401    assert!(project_c.read_with(cx_c, |project, _| project.is_disconnected()));
1402
1403    // Client C can open the project again after client A re-shares.
1404    let project_id = active_call_a
1405        .update(cx_a, |call, cx| call.share_project(project_a.clone(), cx))
1406        .await
1407        .unwrap();
1408    let project_c2 = client_c.build_remote_project(project_id, cx_c).await;
1409    executor.run_until_parked();
1410
1411    assert!(worktree_a.read_with(cx_a, |tree, _| tree.as_local().unwrap().is_shared()));
1412    project_c2
1413        .update(cx_c, |p, cx| p.open_buffer((worktree_id, "a.txt"), cx))
1414        .await
1415        .unwrap();
1416
1417    // When client A (the host) leaves the room, the project gets unshared and guests are notified.
1418    active_call_a
1419        .update(cx_a, |call, cx| call.hang_up(cx))
1420        .await
1421        .unwrap();
1422    executor.run_until_parked();
1423
1424    project_a.read_with(cx_a, |project, _| assert!(!project.is_shared()));
1425
1426    project_c2.read_with(cx_c, |project, _| {
1427        assert!(project.is_disconnected());
1428        assert!(project.collaborators().is_empty());
1429    });
1430}
1431
1432#[gpui::test(iterations = 10)]
1433async fn test_project_reconnect(
1434    executor: BackgroundExecutor,
1435    cx_a: &mut TestAppContext,
1436    cx_b: &mut TestAppContext,
1437) {
1438    let mut server = TestServer::start(executor.clone()).await;
1439    let client_a = server.create_client(cx_a, "user_a").await;
1440    let client_b = server.create_client(cx_b, "user_b").await;
1441    server
1442        .create_room(&mut [(&client_a, cx_a), (&client_b, cx_b)])
1443        .await;
1444
1445    cx_b.update(editor::init);
1446
1447    client_a
1448        .fs()
1449        .insert_tree(
1450            "/root-1",
1451            json!({
1452                "dir1": {
1453                    "a.txt": "a",
1454                    "b.txt": "b",
1455                    "subdir1": {
1456                        "c.txt": "c",
1457                        "d.txt": "d",
1458                        "e.txt": "e",
1459                    }
1460                },
1461                "dir2": {
1462                    "v.txt": "v",
1463                },
1464                "dir3": {
1465                    "w.txt": "w",
1466                    "x.txt": "x",
1467                    "y.txt": "y",
1468                },
1469                "dir4": {
1470                    "z.txt": "z",
1471                },
1472            }),
1473        )
1474        .await;
1475    client_a
1476        .fs()
1477        .insert_tree(
1478            "/root-2",
1479            json!({
1480                "2.txt": "2",
1481            }),
1482        )
1483        .await;
1484    client_a
1485        .fs()
1486        .insert_tree(
1487            "/root-3",
1488            json!({
1489                "3.txt": "3",
1490            }),
1491        )
1492        .await;
1493
1494    let active_call_a = cx_a.read(ActiveCall::global);
1495    let (project_a1, _) = client_a.build_local_project("/root-1/dir1", cx_a).await;
1496    let (project_a2, _) = client_a.build_local_project("/root-2", cx_a).await;
1497    let (project_a3, _) = client_a.build_local_project("/root-3", cx_a).await;
1498    let worktree_a1 = project_a1.read_with(cx_a, |project, _| project.worktrees().next().unwrap());
1499    let project1_id = active_call_a
1500        .update(cx_a, |call, cx| call.share_project(project_a1.clone(), cx))
1501        .await
1502        .unwrap();
1503    let project2_id = active_call_a
1504        .update(cx_a, |call, cx| call.share_project(project_a2.clone(), cx))
1505        .await
1506        .unwrap();
1507    let project3_id = active_call_a
1508        .update(cx_a, |call, cx| call.share_project(project_a3.clone(), cx))
1509        .await
1510        .unwrap();
1511
1512    let project_b1 = client_b.build_remote_project(project1_id, cx_b).await;
1513    let project_b2 = client_b.build_remote_project(project2_id, cx_b).await;
1514    let project_b3 = client_b.build_remote_project(project3_id, cx_b).await;
1515    executor.run_until_parked();
1516
1517    let worktree1_id = worktree_a1.read_with(cx_a, |worktree, _| {
1518        assert!(worktree.as_local().unwrap().is_shared());
1519        worktree.id()
1520    });
1521    let (worktree_a2, _) = project_a1
1522        .update(cx_a, |p, cx| {
1523            p.find_or_create_local_worktree("/root-1/dir2", true, cx)
1524        })
1525        .await
1526        .unwrap();
1527    executor.run_until_parked();
1528
1529    let worktree2_id = worktree_a2.read_with(cx_a, |tree, _| {
1530        assert!(tree.as_local().unwrap().is_shared());
1531        tree.id()
1532    });
1533    executor.run_until_parked();
1534
1535    project_b1.read_with(cx_b, |project, cx| {
1536        assert!(project.worktree_for_id(worktree2_id, cx).is_some())
1537    });
1538
1539    let buffer_a1 = project_a1
1540        .update(cx_a, |p, cx| p.open_buffer((worktree1_id, "a.txt"), cx))
1541        .await
1542        .unwrap();
1543    let buffer_b1 = project_b1
1544        .update(cx_b, |p, cx| p.open_buffer((worktree1_id, "a.txt"), cx))
1545        .await
1546        .unwrap();
1547
1548    // Drop client A's connection.
1549    server.forbid_connections();
1550    server.disconnect_client(client_a.peer_id().unwrap());
1551    executor.advance_clock(RECEIVE_TIMEOUT);
1552
1553    project_a1.read_with(cx_a, |project, _| {
1554        assert!(project.is_shared());
1555        assert_eq!(project.collaborators().len(), 1);
1556    });
1557
1558    project_b1.read_with(cx_b, |project, _| {
1559        assert!(!project.is_disconnected());
1560        assert_eq!(project.collaborators().len(), 1);
1561    });
1562
1563    worktree_a1.read_with(cx_a, |tree, _| {
1564        assert!(tree.as_local().unwrap().is_shared())
1565    });
1566
1567    // While client A is disconnected, add and remove files from client A's project.
1568    client_a
1569        .fs()
1570        .insert_tree(
1571            "/root-1/dir1/subdir2",
1572            json!({
1573                "f.txt": "f-contents",
1574                "g.txt": "g-contents",
1575                "h.txt": "h-contents",
1576                "i.txt": "i-contents",
1577            }),
1578        )
1579        .await;
1580    client_a
1581        .fs()
1582        .remove_dir(
1583            "/root-1/dir1/subdir1".as_ref(),
1584            RemoveOptions {
1585                recursive: true,
1586                ..Default::default()
1587            },
1588        )
1589        .await
1590        .unwrap();
1591
1592    // While client A is disconnected, add and remove worktrees from client A's project.
1593    project_a1.update(cx_a, |project, cx| {
1594        project.remove_worktree(worktree2_id, cx)
1595    });
1596    let (worktree_a3, _) = project_a1
1597        .update(cx_a, |p, cx| {
1598            p.find_or_create_local_worktree("/root-1/dir3", true, cx)
1599        })
1600        .await
1601        .unwrap();
1602    worktree_a3
1603        .read_with(cx_a, |tree, _| tree.as_local().unwrap().scan_complete())
1604        .await;
1605
1606    let worktree3_id = worktree_a3.read_with(cx_a, |tree, _| {
1607        assert!(!tree.as_local().unwrap().is_shared());
1608        tree.id()
1609    });
1610    executor.run_until_parked();
1611
1612    // While client A is disconnected, close project 2
1613    cx_a.update(|_| drop(project_a2));
1614
1615    // While client A is disconnected, mutate a buffer on both the host and the guest.
1616    buffer_a1.update(cx_a, |buf, cx| buf.edit([(0..0, "W")], None, cx));
1617    buffer_b1.update(cx_b, |buf, cx| buf.edit([(1..1, "Z")], None, cx));
1618    executor.run_until_parked();
1619
1620    // Client A reconnects. Their project is re-shared, and client B re-joins it.
1621    server.allow_connections();
1622    client_a
1623        .authenticate_and_connect(false, &cx_a.to_async())
1624        .await
1625        .unwrap();
1626    executor.run_until_parked();
1627
1628    project_a1.read_with(cx_a, |project, cx| {
1629        assert!(project.is_shared());
1630        assert!(worktree_a1.read(cx).as_local().unwrap().is_shared());
1631        assert_eq!(
1632            worktree_a1
1633                .read(cx)
1634                .snapshot()
1635                .paths()
1636                .map(|p| p.to_str().unwrap())
1637                .collect::<Vec<_>>(),
1638            vec![
1639                "a.txt",
1640                "b.txt",
1641                "subdir2",
1642                "subdir2/f.txt",
1643                "subdir2/g.txt",
1644                "subdir2/h.txt",
1645                "subdir2/i.txt"
1646            ]
1647        );
1648        assert!(worktree_a3.read(cx).as_local().unwrap().is_shared());
1649        assert_eq!(
1650            worktree_a3
1651                .read(cx)
1652                .snapshot()
1653                .paths()
1654                .map(|p| p.to_str().unwrap())
1655                .collect::<Vec<_>>(),
1656            vec!["w.txt", "x.txt", "y.txt"]
1657        );
1658    });
1659
1660    project_b1.read_with(cx_b, |project, cx| {
1661        assert!(!project.is_disconnected());
1662        assert_eq!(
1663            project
1664                .worktree_for_id(worktree1_id, cx)
1665                .unwrap()
1666                .read(cx)
1667                .snapshot()
1668                .paths()
1669                .map(|p| p.to_str().unwrap())
1670                .collect::<Vec<_>>(),
1671            vec![
1672                "a.txt",
1673                "b.txt",
1674                "subdir2",
1675                "subdir2/f.txt",
1676                "subdir2/g.txt",
1677                "subdir2/h.txt",
1678                "subdir2/i.txt"
1679            ]
1680        );
1681        assert!(project.worktree_for_id(worktree2_id, cx).is_none());
1682        assert_eq!(
1683            project
1684                .worktree_for_id(worktree3_id, cx)
1685                .unwrap()
1686                .read(cx)
1687                .snapshot()
1688                .paths()
1689                .map(|p| p.to_str().unwrap())
1690                .collect::<Vec<_>>(),
1691            vec!["w.txt", "x.txt", "y.txt"]
1692        );
1693    });
1694
1695    project_b2.read_with(cx_b, |project, _| assert!(project.is_disconnected()));
1696
1697    project_b3.read_with(cx_b, |project, _| assert!(!project.is_disconnected()));
1698
1699    buffer_a1.read_with(cx_a, |buffer, _| assert_eq!(buffer.text(), "WaZ"));
1700
1701    buffer_b1.read_with(cx_b, |buffer, _| assert_eq!(buffer.text(), "WaZ"));
1702
1703    // Drop client B's connection.
1704    server.forbid_connections();
1705    server.disconnect_client(client_b.peer_id().unwrap());
1706    executor.advance_clock(RECEIVE_TIMEOUT);
1707
1708    // While client B is disconnected, add and remove files from client A's project
1709    client_a
1710        .fs()
1711        .insert_file("/root-1/dir1/subdir2/j.txt", "j-contents".into())
1712        .await;
1713    client_a
1714        .fs()
1715        .remove_file("/root-1/dir1/subdir2/i.txt".as_ref(), Default::default())
1716        .await
1717        .unwrap();
1718
1719    // While client B is disconnected, add and remove worktrees from client A's project.
1720    let (worktree_a4, _) = project_a1
1721        .update(cx_a, |p, cx| {
1722            p.find_or_create_local_worktree("/root-1/dir4", true, cx)
1723        })
1724        .await
1725        .unwrap();
1726    executor.run_until_parked();
1727
1728    let worktree4_id = worktree_a4.read_with(cx_a, |tree, _| {
1729        assert!(tree.as_local().unwrap().is_shared());
1730        tree.id()
1731    });
1732    project_a1.update(cx_a, |project, cx| {
1733        project.remove_worktree(worktree3_id, cx)
1734    });
1735    executor.run_until_parked();
1736
1737    // While client B is disconnected, mutate a buffer on both the host and the guest.
1738    buffer_a1.update(cx_a, |buf, cx| buf.edit([(1..1, "X")], None, cx));
1739    buffer_b1.update(cx_b, |buf, cx| buf.edit([(2..2, "Y")], None, cx));
1740    executor.run_until_parked();
1741
1742    // While disconnected, close project 3
1743    cx_a.update(|_| drop(project_a3));
1744
1745    // Client B reconnects. They re-join the room and the remaining shared project.
1746    server.allow_connections();
1747    client_b
1748        .authenticate_and_connect(false, &cx_b.to_async())
1749        .await
1750        .unwrap();
1751    executor.run_until_parked();
1752
1753    project_b1.read_with(cx_b, |project, cx| {
1754        assert!(!project.is_disconnected());
1755        assert_eq!(
1756            project
1757                .worktree_for_id(worktree1_id, cx)
1758                .unwrap()
1759                .read(cx)
1760                .snapshot()
1761                .paths()
1762                .map(|p| p.to_str().unwrap())
1763                .collect::<Vec<_>>(),
1764            vec![
1765                "a.txt",
1766                "b.txt",
1767                "subdir2",
1768                "subdir2/f.txt",
1769                "subdir2/g.txt",
1770                "subdir2/h.txt",
1771                "subdir2/j.txt"
1772            ]
1773        );
1774        assert!(project.worktree_for_id(worktree2_id, cx).is_none());
1775        assert_eq!(
1776            project
1777                .worktree_for_id(worktree4_id, cx)
1778                .unwrap()
1779                .read(cx)
1780                .snapshot()
1781                .paths()
1782                .map(|p| p.to_str().unwrap())
1783                .collect::<Vec<_>>(),
1784            vec!["z.txt"]
1785        );
1786    });
1787
1788    project_b3.read_with(cx_b, |project, _| assert!(project.is_disconnected()));
1789
1790    buffer_a1.read_with(cx_a, |buffer, _| assert_eq!(buffer.text(), "WXaYZ"));
1791
1792    buffer_b1.read_with(cx_b, |buffer, _| assert_eq!(buffer.text(), "WXaYZ"));
1793}
1794
1795#[gpui::test(iterations = 10)]
1796async fn test_active_call_events(
1797    executor: BackgroundExecutor,
1798    cx_a: &mut TestAppContext,
1799    cx_b: &mut TestAppContext,
1800) {
1801    let mut server = TestServer::start(executor.clone()).await;
1802    let client_a = server.create_client(cx_a, "user_a").await;
1803    let client_b = server.create_client(cx_b, "user_b").await;
1804    client_a.fs().insert_tree("/a", json!({})).await;
1805    client_b.fs().insert_tree("/b", json!({})).await;
1806
1807    let (project_a, _) = client_a.build_local_project("/a", cx_a).await;
1808    let (project_b, _) = client_b.build_local_project("/b", cx_b).await;
1809
1810    server
1811        .create_room(&mut [(&client_a, cx_a), (&client_b, cx_b)])
1812        .await;
1813    let active_call_a = cx_a.read(ActiveCall::global);
1814    let active_call_b = cx_b.read(ActiveCall::global);
1815
1816    let events_a = active_call_events(cx_a);
1817    let events_b = active_call_events(cx_b);
1818
1819    let project_a_id = active_call_a
1820        .update(cx_a, |call, cx| call.share_project(project_a.clone(), cx))
1821        .await
1822        .unwrap();
1823    executor.run_until_parked();
1824    assert_eq!(mem::take(&mut *events_a.borrow_mut()), vec![]);
1825    assert_eq!(
1826        mem::take(&mut *events_b.borrow_mut()),
1827        vec![room::Event::RemoteProjectShared {
1828            owner: Arc::new(User {
1829                id: client_a.user_id().unwrap(),
1830                github_login: "user_a".to_string(),
1831                avatar_uri: "avatar_a".into(),
1832            }),
1833            project_id: project_a_id,
1834            worktree_root_names: vec!["a".to_string()],
1835        }]
1836    );
1837
1838    let project_b_id = active_call_b
1839        .update(cx_b, |call, cx| call.share_project(project_b.clone(), cx))
1840        .await
1841        .unwrap();
1842    executor.run_until_parked();
1843    assert_eq!(
1844        mem::take(&mut *events_a.borrow_mut()),
1845        vec![room::Event::RemoteProjectShared {
1846            owner: Arc::new(User {
1847                id: client_b.user_id().unwrap(),
1848                github_login: "user_b".to_string(),
1849                avatar_uri: "avatar_b".into(),
1850            }),
1851            project_id: project_b_id,
1852            worktree_root_names: vec!["b".to_string()]
1853        }]
1854    );
1855    assert_eq!(mem::take(&mut *events_b.borrow_mut()), vec![]);
1856
1857    // Sharing a project twice is idempotent.
1858    let project_b_id_2 = active_call_b
1859        .update(cx_b, |call, cx| call.share_project(project_b.clone(), cx))
1860        .await
1861        .unwrap();
1862    assert_eq!(project_b_id_2, project_b_id);
1863    executor.run_until_parked();
1864    assert_eq!(mem::take(&mut *events_a.borrow_mut()), vec![]);
1865    assert_eq!(mem::take(&mut *events_b.borrow_mut()), vec![]);
1866}
1867
1868fn active_call_events(cx: &mut TestAppContext) -> Rc<RefCell<Vec<room::Event>>> {
1869    let events = Rc::new(RefCell::new(Vec::new()));
1870    let active_call = cx.read(ActiveCall::global);
1871    cx.update({
1872        let events = events.clone();
1873        |cx| {
1874            cx.subscribe(&active_call, move |_, event, _| {
1875                events.borrow_mut().push(event.clone())
1876            })
1877            .detach()
1878        }
1879    });
1880    events
1881}
1882
1883#[gpui::test]
1884async fn test_mute_deafen(
1885    executor: BackgroundExecutor,
1886    cx_a: &mut TestAppContext,
1887    cx_b: &mut TestAppContext,
1888    cx_c: &mut TestAppContext,
1889) {
1890    let mut server = TestServer::start(executor.clone()).await;
1891    let client_a = server.create_client(cx_a, "user_a").await;
1892    let client_b = server.create_client(cx_b, "user_b").await;
1893    let client_c = server.create_client(cx_c, "user_c").await;
1894
1895    server
1896        .make_contacts(&mut [(&client_a, cx_a), (&client_b, cx_b), (&client_c, cx_c)])
1897        .await;
1898
1899    let active_call_a = cx_a.read(ActiveCall::global);
1900    let active_call_b = cx_b.read(ActiveCall::global);
1901    let active_call_c = cx_c.read(ActiveCall::global);
1902
1903    // User A calls user B, B answers.
1904    active_call_a
1905        .update(cx_a, |call, cx| {
1906            call.invite(client_b.user_id().unwrap(), None, cx)
1907        })
1908        .await
1909        .unwrap();
1910    executor.run_until_parked();
1911    active_call_b
1912        .update(cx_b, |call, cx| call.accept_incoming(cx))
1913        .await
1914        .unwrap();
1915    executor.run_until_parked();
1916
1917    let room_a = active_call_a.read_with(cx_a, |call, _| call.room().unwrap().clone());
1918    let room_b = active_call_b.read_with(cx_b, |call, _| call.room().unwrap().clone());
1919
1920    room_a.read_with(cx_a, |room, _| assert!(!room.is_muted()));
1921    room_b.read_with(cx_b, |room, _| assert!(!room.is_muted()));
1922
1923    // Users A and B are both muted.
1924    assert_eq!(
1925        participant_audio_state(&room_a, cx_a),
1926        &[ParticipantAudioState {
1927            user_id: client_b.user_id().unwrap(),
1928            is_muted: false,
1929            audio_tracks_playing: vec![true],
1930        }]
1931    );
1932    assert_eq!(
1933        participant_audio_state(&room_b, cx_b),
1934        &[ParticipantAudioState {
1935            user_id: client_a.user_id().unwrap(),
1936            is_muted: false,
1937            audio_tracks_playing: vec![true],
1938        }]
1939    );
1940
1941    // User A mutes
1942    room_a.update(cx_a, |room, cx| room.toggle_mute(cx));
1943    executor.run_until_parked();
1944
1945    // User A hears user B, but B doesn't hear A.
1946    room_a.read_with(cx_a, |room, _| assert!(room.is_muted()));
1947    room_b.read_with(cx_b, |room, _| assert!(!room.is_muted()));
1948    assert_eq!(
1949        participant_audio_state(&room_a, cx_a),
1950        &[ParticipantAudioState {
1951            user_id: client_b.user_id().unwrap(),
1952            is_muted: false,
1953            audio_tracks_playing: vec![true],
1954        }]
1955    );
1956    assert_eq!(
1957        participant_audio_state(&room_b, cx_b),
1958        &[ParticipantAudioState {
1959            user_id: client_a.user_id().unwrap(),
1960            is_muted: true,
1961            audio_tracks_playing: vec![true],
1962        }]
1963    );
1964
1965    // User A deafens
1966    room_a.update(cx_a, |room, cx| room.toggle_deafen(cx));
1967    executor.run_until_parked();
1968
1969    // User A does not hear user B.
1970    room_a.read_with(cx_a, |room, _| assert!(room.is_muted()));
1971    room_b.read_with(cx_b, |room, _| assert!(!room.is_muted()));
1972    assert_eq!(
1973        participant_audio_state(&room_a, cx_a),
1974        &[ParticipantAudioState {
1975            user_id: client_b.user_id().unwrap(),
1976            is_muted: false,
1977            audio_tracks_playing: vec![false],
1978        }]
1979    );
1980    assert_eq!(
1981        participant_audio_state(&room_b, cx_b),
1982        &[ParticipantAudioState {
1983            user_id: client_a.user_id().unwrap(),
1984            is_muted: true,
1985            audio_tracks_playing: vec![true],
1986        }]
1987    );
1988
1989    // User B calls user C, C joins.
1990    active_call_b
1991        .update(cx_b, |call, cx| {
1992            call.invite(client_c.user_id().unwrap(), None, cx)
1993        })
1994        .await
1995        .unwrap();
1996    executor.run_until_parked();
1997    active_call_c
1998        .update(cx_c, |call, cx| call.accept_incoming(cx))
1999        .await
2000        .unwrap();
2001    executor.run_until_parked();
2002
2003    // User A does not hear users B or C.
2004    assert_eq!(
2005        participant_audio_state(&room_a, cx_a),
2006        &[
2007            ParticipantAudioState {
2008                user_id: client_b.user_id().unwrap(),
2009                is_muted: false,
2010                audio_tracks_playing: vec![false],
2011            },
2012            ParticipantAudioState {
2013                user_id: client_c.user_id().unwrap(),
2014                is_muted: false,
2015                audio_tracks_playing: vec![false],
2016            }
2017        ]
2018    );
2019    assert_eq!(
2020        participant_audio_state(&room_b, cx_b),
2021        &[
2022            ParticipantAudioState {
2023                user_id: client_a.user_id().unwrap(),
2024                is_muted: true,
2025                audio_tracks_playing: vec![true],
2026            },
2027            ParticipantAudioState {
2028                user_id: client_c.user_id().unwrap(),
2029                is_muted: false,
2030                audio_tracks_playing: vec![true],
2031            }
2032        ]
2033    );
2034
2035    #[derive(PartialEq, Eq, Debug)]
2036    struct ParticipantAudioState {
2037        user_id: u64,
2038        is_muted: bool,
2039        audio_tracks_playing: Vec<bool>,
2040    }
2041
2042    fn participant_audio_state(
2043        room: &Model<Room>,
2044        cx: &TestAppContext,
2045    ) -> Vec<ParticipantAudioState> {
2046        room.read_with(cx, |room, _| {
2047            room.remote_participants()
2048                .iter()
2049                .map(|(user_id, participant)| ParticipantAudioState {
2050                    user_id: *user_id,
2051                    is_muted: participant.muted,
2052                    audio_tracks_playing: participant
2053                        .audio_tracks
2054                        .values()
2055                        .map(|track| track.is_playing())
2056                        .collect(),
2057                })
2058                .collect::<Vec<_>>()
2059        })
2060    }
2061}
2062
2063#[gpui::test(iterations = 10)]
2064async fn test_room_location(
2065    executor: BackgroundExecutor,
2066    cx_a: &mut TestAppContext,
2067    cx_b: &mut TestAppContext,
2068) {
2069    let mut server = TestServer::start(executor.clone()).await;
2070    let client_a = server.create_client(cx_a, "user_a").await;
2071    let client_b = server.create_client(cx_b, "user_b").await;
2072    client_a.fs().insert_tree("/a", json!({})).await;
2073    client_b.fs().insert_tree("/b", json!({})).await;
2074
2075    let active_call_a = cx_a.read(ActiveCall::global);
2076    let active_call_b = cx_b.read(ActiveCall::global);
2077
2078    let a_notified = Rc::new(Cell::new(false));
2079    cx_a.update({
2080        let notified = a_notified.clone();
2081        |cx| {
2082            cx.observe(&active_call_a, move |_, _| notified.set(true))
2083                .detach()
2084        }
2085    });
2086
2087    let b_notified = Rc::new(Cell::new(false));
2088    cx_b.update({
2089        let b_notified = b_notified.clone();
2090        |cx| {
2091            cx.observe(&active_call_b, move |_, _| b_notified.set(true))
2092                .detach()
2093        }
2094    });
2095
2096    let (project_a, _) = client_a.build_local_project("/a", cx_a).await;
2097    active_call_a
2098        .update(cx_a, |call, cx| call.set_location(Some(&project_a), cx))
2099        .await
2100        .unwrap();
2101    let (project_b, _) = client_b.build_local_project("/b", cx_b).await;
2102
2103    server
2104        .create_room(&mut [(&client_a, cx_a), (&client_b, cx_b)])
2105        .await;
2106
2107    let room_a = active_call_a.read_with(cx_a, |call, _| call.room().unwrap().clone());
2108
2109    let room_b = active_call_b.read_with(cx_b, |call, _| call.room().unwrap().clone());
2110    executor.run_until_parked();
2111    assert!(a_notified.take());
2112    assert_eq!(
2113        participant_locations(&room_a, cx_a),
2114        vec![("user_b".to_string(), ParticipantLocation::External)]
2115    );
2116    assert!(b_notified.take());
2117    assert_eq!(
2118        participant_locations(&room_b, cx_b),
2119        vec![("user_a".to_string(), ParticipantLocation::UnsharedProject)]
2120    );
2121
2122    let project_a_id = active_call_a
2123        .update(cx_a, |call, cx| call.share_project(project_a.clone(), cx))
2124        .await
2125        .unwrap();
2126    executor.run_until_parked();
2127    assert!(a_notified.take());
2128    assert_eq!(
2129        participant_locations(&room_a, cx_a),
2130        vec![("user_b".to_string(), ParticipantLocation::External)]
2131    );
2132    assert!(b_notified.take());
2133    assert_eq!(
2134        participant_locations(&room_b, cx_b),
2135        vec![(
2136            "user_a".to_string(),
2137            ParticipantLocation::SharedProject {
2138                project_id: project_a_id
2139            }
2140        )]
2141    );
2142
2143    let project_b_id = active_call_b
2144        .update(cx_b, |call, cx| call.share_project(project_b.clone(), cx))
2145        .await
2146        .unwrap();
2147    executor.run_until_parked();
2148    assert!(a_notified.take());
2149    assert_eq!(
2150        participant_locations(&room_a, cx_a),
2151        vec![("user_b".to_string(), ParticipantLocation::External)]
2152    );
2153    assert!(b_notified.take());
2154    assert_eq!(
2155        participant_locations(&room_b, cx_b),
2156        vec![(
2157            "user_a".to_string(),
2158            ParticipantLocation::SharedProject {
2159                project_id: project_a_id
2160            }
2161        )]
2162    );
2163
2164    active_call_b
2165        .update(cx_b, |call, cx| call.set_location(Some(&project_b), cx))
2166        .await
2167        .unwrap();
2168    executor.run_until_parked();
2169    assert!(a_notified.take());
2170    assert_eq!(
2171        participant_locations(&room_a, cx_a),
2172        vec![(
2173            "user_b".to_string(),
2174            ParticipantLocation::SharedProject {
2175                project_id: project_b_id
2176            }
2177        )]
2178    );
2179    assert!(b_notified.take());
2180    assert_eq!(
2181        participant_locations(&room_b, cx_b),
2182        vec![(
2183            "user_a".to_string(),
2184            ParticipantLocation::SharedProject {
2185                project_id: project_a_id
2186            }
2187        )]
2188    );
2189
2190    active_call_b
2191        .update(cx_b, |call, cx| call.set_location(None, cx))
2192        .await
2193        .unwrap();
2194    executor.run_until_parked();
2195    assert!(a_notified.take());
2196    assert_eq!(
2197        participant_locations(&room_a, cx_a),
2198        vec![("user_b".to_string(), ParticipantLocation::External)]
2199    );
2200    assert!(b_notified.take());
2201    assert_eq!(
2202        participant_locations(&room_b, cx_b),
2203        vec![(
2204            "user_a".to_string(),
2205            ParticipantLocation::SharedProject {
2206                project_id: project_a_id
2207            }
2208        )]
2209    );
2210
2211    fn participant_locations(
2212        room: &Model<Room>,
2213        cx: &TestAppContext,
2214    ) -> Vec<(String, ParticipantLocation)> {
2215        room.read_with(cx, |room, _| {
2216            room.remote_participants()
2217                .values()
2218                .map(|participant| {
2219                    (
2220                        participant.user.github_login.to_string(),
2221                        participant.location,
2222                    )
2223                })
2224                .collect()
2225        })
2226    }
2227}
2228
2229#[gpui::test(iterations = 10)]
2230async fn test_propagate_saves_and_fs_changes(
2231    executor: BackgroundExecutor,
2232    cx_a: &mut TestAppContext,
2233    cx_b: &mut TestAppContext,
2234    cx_c: &mut TestAppContext,
2235) {
2236    let mut server = TestServer::start(executor.clone()).await;
2237    let client_a = server.create_client(cx_a, "user_a").await;
2238    let client_b = server.create_client(cx_b, "user_b").await;
2239    let client_c = server.create_client(cx_c, "user_c").await;
2240
2241    server
2242        .create_room(&mut [(&client_a, cx_a), (&client_b, cx_b), (&client_c, cx_c)])
2243        .await;
2244    let active_call_a = cx_a.read(ActiveCall::global);
2245
2246    let rust = Arc::new(Language::new(
2247        LanguageConfig {
2248            name: "Rust".into(),
2249            matcher: LanguageMatcher {
2250                path_suffixes: vec!["rs".to_string()],
2251                ..Default::default()
2252            },
2253            ..Default::default()
2254        },
2255        Some(tree_sitter_rust::language()),
2256    ));
2257    let javascript = Arc::new(Language::new(
2258        LanguageConfig {
2259            name: "JavaScript".into(),
2260            matcher: LanguageMatcher {
2261                path_suffixes: vec!["js".to_string()],
2262                ..Default::default()
2263            },
2264            ..Default::default()
2265        },
2266        Some(tree_sitter_rust::language()),
2267    ));
2268    for client in [&client_a, &client_b, &client_c] {
2269        client.language_registry().add(rust.clone());
2270        client.language_registry().add(javascript.clone());
2271    }
2272
2273    client_a
2274        .fs()
2275        .insert_tree(
2276            "/a",
2277            json!({
2278                "file1.rs": "",
2279                "file2": ""
2280            }),
2281        )
2282        .await;
2283    let (project_a, worktree_id) = client_a.build_local_project("/a", cx_a).await;
2284
2285    let worktree_a = project_a.read_with(cx_a, |p, _| p.worktrees().next().unwrap());
2286    let project_id = active_call_a
2287        .update(cx_a, |call, cx| call.share_project(project_a.clone(), cx))
2288        .await
2289        .unwrap();
2290
2291    // Join that worktree as clients B and C.
2292    let project_b = client_b.build_remote_project(project_id, cx_b).await;
2293    let project_c = client_c.build_remote_project(project_id, cx_c).await;
2294
2295    let worktree_b = project_b.read_with(cx_b, |p, _| p.worktrees().next().unwrap());
2296
2297    let worktree_c = project_c.read_with(cx_c, |p, _| p.worktrees().next().unwrap());
2298
2299    // Open and edit a buffer as both guests B and C.
2300    let buffer_b = project_b
2301        .update(cx_b, |p, cx| p.open_buffer((worktree_id, "file1.rs"), cx))
2302        .await
2303        .unwrap();
2304    let buffer_c = project_c
2305        .update(cx_c, |p, cx| p.open_buffer((worktree_id, "file1.rs"), cx))
2306        .await
2307        .unwrap();
2308
2309    buffer_b.read_with(cx_b, |buffer, _| {
2310        assert_eq!(&*buffer.language().unwrap().name(), "Rust");
2311    });
2312
2313    buffer_c.read_with(cx_c, |buffer, _| {
2314        assert_eq!(&*buffer.language().unwrap().name(), "Rust");
2315    });
2316    buffer_b.update(cx_b, |buf, cx| buf.edit([(0..0, "i-am-b, ")], None, cx));
2317    buffer_c.update(cx_c, |buf, cx| buf.edit([(0..0, "i-am-c, ")], None, cx));
2318
2319    // Open and edit that buffer as the host.
2320    let buffer_a = project_a
2321        .update(cx_a, |p, cx| p.open_buffer((worktree_id, "file1.rs"), cx))
2322        .await
2323        .unwrap();
2324
2325    executor.run_until_parked();
2326
2327    buffer_a.read_with(cx_a, |buf, _| assert_eq!(buf.text(), "i-am-c, i-am-b, "));
2328    buffer_a.update(cx_a, |buf, cx| {
2329        buf.edit([(buf.len()..buf.len(), "i-am-a")], None, cx)
2330    });
2331
2332    executor.run_until_parked();
2333
2334    buffer_a.read_with(cx_a, |buf, _| {
2335        assert_eq!(buf.text(), "i-am-c, i-am-b, i-am-a");
2336    });
2337
2338    buffer_b.read_with(cx_b, |buf, _| {
2339        assert_eq!(buf.text(), "i-am-c, i-am-b, i-am-a");
2340    });
2341
2342    buffer_c.read_with(cx_c, |buf, _| {
2343        assert_eq!(buf.text(), "i-am-c, i-am-b, i-am-a");
2344    });
2345
2346    // Edit the buffer as the host and concurrently save as guest B.
2347    let save_b = project_b.update(cx_b, |project, cx| {
2348        project.save_buffer(buffer_b.clone(), cx)
2349    });
2350    buffer_a.update(cx_a, |buf, cx| buf.edit([(0..0, "hi-a, ")], None, cx));
2351    save_b.await.unwrap();
2352    assert_eq!(
2353        client_a.fs().load("/a/file1.rs".as_ref()).await.unwrap(),
2354        "hi-a, i-am-c, i-am-b, i-am-a"
2355    );
2356
2357    executor.run_until_parked();
2358
2359    buffer_a.read_with(cx_a, |buf, _| assert!(!buf.is_dirty()));
2360
2361    buffer_b.read_with(cx_b, |buf, _| assert!(!buf.is_dirty()));
2362
2363    buffer_c.read_with(cx_c, |buf, _| assert!(!buf.is_dirty()));
2364
2365    // Make changes on host's file system, see those changes on guest worktrees.
2366    client_a
2367        .fs()
2368        .rename(
2369            "/a/file1.rs".as_ref(),
2370            "/a/file1.js".as_ref(),
2371            Default::default(),
2372        )
2373        .await
2374        .unwrap();
2375    client_a
2376        .fs()
2377        .rename("/a/file2".as_ref(), "/a/file3".as_ref(), Default::default())
2378        .await
2379        .unwrap();
2380    client_a.fs().insert_file("/a/file4", "4".into()).await;
2381    executor.run_until_parked();
2382
2383    worktree_a.read_with(cx_a, |tree, _| {
2384        assert_eq!(
2385            tree.paths()
2386                .map(|p| p.to_string_lossy())
2387                .collect::<Vec<_>>(),
2388            ["file1.js", "file3", "file4"]
2389        )
2390    });
2391
2392    worktree_b.read_with(cx_b, |tree, _| {
2393        assert_eq!(
2394            tree.paths()
2395                .map(|p| p.to_string_lossy())
2396                .collect::<Vec<_>>(),
2397            ["file1.js", "file3", "file4"]
2398        )
2399    });
2400
2401    worktree_c.read_with(cx_c, |tree, _| {
2402        assert_eq!(
2403            tree.paths()
2404                .map(|p| p.to_string_lossy())
2405                .collect::<Vec<_>>(),
2406            ["file1.js", "file3", "file4"]
2407        )
2408    });
2409
2410    // Ensure buffer files are updated as well.
2411
2412    buffer_a.read_with(cx_a, |buffer, _| {
2413        assert_eq!(buffer.file().unwrap().path().to_str(), Some("file1.js"));
2414        assert_eq!(&*buffer.language().unwrap().name(), "JavaScript");
2415    });
2416
2417    buffer_b.read_with(cx_b, |buffer, _| {
2418        assert_eq!(buffer.file().unwrap().path().to_str(), Some("file1.js"));
2419        assert_eq!(&*buffer.language().unwrap().name(), "JavaScript");
2420    });
2421
2422    buffer_c.read_with(cx_c, |buffer, _| {
2423        assert_eq!(buffer.file().unwrap().path().to_str(), Some("file1.js"));
2424        assert_eq!(&*buffer.language().unwrap().name(), "JavaScript");
2425    });
2426
2427    let new_buffer_a = project_a
2428        .update(cx_a, |p, cx| p.create_buffer("", None, cx))
2429        .unwrap();
2430
2431    let new_buffer_id = new_buffer_a.read_with(cx_a, |buffer, _| buffer.remote_id());
2432    let new_buffer_b = project_b
2433        .update(cx_b, |p, cx| p.open_buffer_by_id(new_buffer_id, cx))
2434        .await
2435        .unwrap();
2436
2437    new_buffer_b.read_with(cx_b, |buffer, _| {
2438        assert!(buffer.file().is_none());
2439    });
2440
2441    new_buffer_a.update(cx_a, |buffer, cx| {
2442        buffer.edit([(0..0, "ok")], None, cx);
2443    });
2444    project_a
2445        .update(cx_a, |project, cx| {
2446            project.save_buffer_as(new_buffer_a.clone(), "/a/file3.rs".into(), cx)
2447        })
2448        .await
2449        .unwrap();
2450
2451    executor.run_until_parked();
2452
2453    new_buffer_b.read_with(cx_b, |buffer_b, _| {
2454        assert_eq!(
2455            buffer_b.file().unwrap().path().as_ref(),
2456            Path::new("file3.rs")
2457        );
2458
2459        new_buffer_a.read_with(cx_a, |buffer_a, _| {
2460            assert_eq!(buffer_b.saved_mtime(), buffer_a.saved_mtime());
2461            assert_eq!(buffer_b.saved_version(), buffer_a.saved_version());
2462        });
2463    });
2464}
2465
2466#[gpui::test(iterations = 10)]
2467async fn test_git_diff_base_change(
2468    executor: BackgroundExecutor,
2469    cx_a: &mut TestAppContext,
2470    cx_b: &mut TestAppContext,
2471) {
2472    let mut server = TestServer::start(executor.clone()).await;
2473    let client_a = server.create_client(cx_a, "user_a").await;
2474    let client_b = server.create_client(cx_b, "user_b").await;
2475    server
2476        .create_room(&mut [(&client_a, cx_a), (&client_b, cx_b)])
2477        .await;
2478    let active_call_a = cx_a.read(ActiveCall::global);
2479
2480    client_a
2481        .fs()
2482        .insert_tree(
2483            "/dir",
2484            json!({
2485            ".git": {},
2486            "sub": {
2487                ".git": {},
2488                "b.txt": "
2489                    one
2490                    two
2491                    three
2492                ".unindent(),
2493            },
2494            "a.txt": "
2495                    one
2496                    two
2497                    three
2498                ".unindent(),
2499            }),
2500        )
2501        .await;
2502
2503    let (project_local, worktree_id) = client_a.build_local_project("/dir", cx_a).await;
2504    let project_id = active_call_a
2505        .update(cx_a, |call, cx| {
2506            call.share_project(project_local.clone(), cx)
2507        })
2508        .await
2509        .unwrap();
2510
2511    let project_remote = client_b.build_remote_project(project_id, cx_b).await;
2512
2513    let diff_base = "
2514        one
2515        three
2516    "
2517    .unindent();
2518
2519    let new_diff_base = "
2520        one
2521        two
2522    "
2523    .unindent();
2524
2525    client_a.fs().set_index_for_repo(
2526        Path::new("/dir/.git"),
2527        &[(Path::new("a.txt"), diff_base.clone())],
2528    );
2529
2530    // Create the buffer
2531    let buffer_local_a = project_local
2532        .update(cx_a, |p, cx| p.open_buffer((worktree_id, "a.txt"), cx))
2533        .await
2534        .unwrap();
2535
2536    // Wait for it to catch up to the new diff
2537    executor.run_until_parked();
2538
2539    // Smoke test diffing
2540
2541    buffer_local_a.read_with(cx_a, |buffer, _| {
2542        assert_eq!(buffer.diff_base(), Some(diff_base.as_ref()));
2543        git::diff::assert_hunks(
2544            buffer.snapshot().git_diff_hunks_in_row_range(0..4),
2545            &buffer,
2546            &diff_base,
2547            &[(1..2, "", "two\n")],
2548        );
2549    });
2550
2551    // Create remote buffer
2552    let buffer_remote_a = project_remote
2553        .update(cx_b, |p, cx| p.open_buffer((worktree_id, "a.txt"), cx))
2554        .await
2555        .unwrap();
2556
2557    // Wait remote buffer to catch up to the new diff
2558    executor.run_until_parked();
2559
2560    // Smoke test diffing
2561
2562    buffer_remote_a.read_with(cx_b, |buffer, _| {
2563        assert_eq!(buffer.diff_base(), Some(diff_base.as_ref()));
2564        git::diff::assert_hunks(
2565            buffer.snapshot().git_diff_hunks_in_row_range(0..4),
2566            &buffer,
2567            &diff_base,
2568            &[(1..2, "", "two\n")],
2569        );
2570    });
2571
2572    client_a.fs().set_index_for_repo(
2573        Path::new("/dir/.git"),
2574        &[(Path::new("a.txt"), new_diff_base.clone())],
2575    );
2576
2577    // Wait for buffer_local_a to receive it
2578    executor.run_until_parked();
2579
2580    // Smoke test new diffing
2581
2582    buffer_local_a.read_with(cx_a, |buffer, _| {
2583        assert_eq!(buffer.diff_base(), Some(new_diff_base.as_ref()));
2584
2585        git::diff::assert_hunks(
2586            buffer.snapshot().git_diff_hunks_in_row_range(0..4),
2587            &buffer,
2588            &diff_base,
2589            &[(2..3, "", "three\n")],
2590        );
2591    });
2592
2593    // Smoke test B
2594
2595    buffer_remote_a.read_with(cx_b, |buffer, _| {
2596        assert_eq!(buffer.diff_base(), Some(new_diff_base.as_ref()));
2597        git::diff::assert_hunks(
2598            buffer.snapshot().git_diff_hunks_in_row_range(0..4),
2599            &buffer,
2600            &diff_base,
2601            &[(2..3, "", "three\n")],
2602        );
2603    });
2604
2605    //Nested git dir
2606
2607    let diff_base = "
2608        one
2609        three
2610    "
2611    .unindent();
2612
2613    let new_diff_base = "
2614        one
2615        two
2616    "
2617    .unindent();
2618
2619    client_a.fs().set_index_for_repo(
2620        Path::new("/dir/sub/.git"),
2621        &[(Path::new("b.txt"), diff_base.clone())],
2622    );
2623
2624    // Create the buffer
2625    let buffer_local_b = project_local
2626        .update(cx_a, |p, cx| p.open_buffer((worktree_id, "sub/b.txt"), cx))
2627        .await
2628        .unwrap();
2629
2630    // Wait for it to catch up to the new diff
2631    executor.run_until_parked();
2632
2633    // Smoke test diffing
2634
2635    buffer_local_b.read_with(cx_a, |buffer, _| {
2636        assert_eq!(buffer.diff_base(), Some(diff_base.as_ref()));
2637        git::diff::assert_hunks(
2638            buffer.snapshot().git_diff_hunks_in_row_range(0..4),
2639            &buffer,
2640            &diff_base,
2641            &[(1..2, "", "two\n")],
2642        );
2643    });
2644
2645    // Create remote buffer
2646    let buffer_remote_b = project_remote
2647        .update(cx_b, |p, cx| p.open_buffer((worktree_id, "sub/b.txt"), cx))
2648        .await
2649        .unwrap();
2650
2651    // Wait remote buffer to catch up to the new diff
2652    executor.run_until_parked();
2653
2654    // Smoke test diffing
2655
2656    buffer_remote_b.read_with(cx_b, |buffer, _| {
2657        assert_eq!(buffer.diff_base(), Some(diff_base.as_ref()));
2658        git::diff::assert_hunks(
2659            buffer.snapshot().git_diff_hunks_in_row_range(0..4),
2660            &buffer,
2661            &diff_base,
2662            &[(1..2, "", "two\n")],
2663        );
2664    });
2665
2666    client_a.fs().set_index_for_repo(
2667        Path::new("/dir/sub/.git"),
2668        &[(Path::new("b.txt"), new_diff_base.clone())],
2669    );
2670
2671    // Wait for buffer_local_b to receive it
2672    executor.run_until_parked();
2673
2674    // Smoke test new diffing
2675
2676    buffer_local_b.read_with(cx_a, |buffer, _| {
2677        assert_eq!(buffer.diff_base(), Some(new_diff_base.as_ref()));
2678        println!("{:?}", buffer.as_rope().to_string());
2679        println!("{:?}", buffer.diff_base());
2680        println!(
2681            "{:?}",
2682            buffer
2683                .snapshot()
2684                .git_diff_hunks_in_row_range(0..4)
2685                .collect::<Vec<_>>()
2686        );
2687
2688        git::diff::assert_hunks(
2689            buffer.snapshot().git_diff_hunks_in_row_range(0..4),
2690            &buffer,
2691            &diff_base,
2692            &[(2..3, "", "three\n")],
2693        );
2694    });
2695
2696    // Smoke test B
2697
2698    buffer_remote_b.read_with(cx_b, |buffer, _| {
2699        assert_eq!(buffer.diff_base(), Some(new_diff_base.as_ref()));
2700        git::diff::assert_hunks(
2701            buffer.snapshot().git_diff_hunks_in_row_range(0..4),
2702            &buffer,
2703            &diff_base,
2704            &[(2..3, "", "three\n")],
2705        );
2706    });
2707}
2708
2709#[gpui::test]
2710async fn test_git_branch_name(
2711    executor: BackgroundExecutor,
2712    cx_a: &mut TestAppContext,
2713    cx_b: &mut TestAppContext,
2714    cx_c: &mut TestAppContext,
2715) {
2716    let mut server = TestServer::start(executor.clone()).await;
2717    let client_a = server.create_client(cx_a, "user_a").await;
2718    let client_b = server.create_client(cx_b, "user_b").await;
2719    let client_c = server.create_client(cx_c, "user_c").await;
2720    server
2721        .create_room(&mut [(&client_a, cx_a), (&client_b, cx_b), (&client_c, cx_c)])
2722        .await;
2723    let active_call_a = cx_a.read(ActiveCall::global);
2724
2725    client_a
2726        .fs()
2727        .insert_tree(
2728            "/dir",
2729            json!({
2730            ".git": {},
2731            }),
2732        )
2733        .await;
2734
2735    let (project_local, _worktree_id) = client_a.build_local_project("/dir", cx_a).await;
2736    let project_id = active_call_a
2737        .update(cx_a, |call, cx| {
2738            call.share_project(project_local.clone(), cx)
2739        })
2740        .await
2741        .unwrap();
2742
2743    let project_remote = client_b.build_remote_project(project_id, cx_b).await;
2744    client_a
2745        .fs()
2746        .set_branch_name(Path::new("/dir/.git"), Some("branch-1"));
2747
2748    // Wait for it to catch up to the new branch
2749    executor.run_until_parked();
2750
2751    #[track_caller]
2752    fn assert_branch(branch_name: Option<impl Into<String>>, project: &Project, cx: &AppContext) {
2753        let branch_name = branch_name.map(Into::into);
2754        let worktrees = project.visible_worktrees(cx).collect::<Vec<_>>();
2755        assert_eq!(worktrees.len(), 1);
2756        let worktree = worktrees[0].clone();
2757        let root_entry = worktree.read(cx).snapshot().root_git_entry().unwrap();
2758        assert_eq!(root_entry.branch(), branch_name.map(Into::into));
2759    }
2760
2761    // Smoke test branch reading
2762
2763    project_local.read_with(cx_a, |project, cx| {
2764        assert_branch(Some("branch-1"), project, cx)
2765    });
2766
2767    project_remote.read_with(cx_b, |project, cx| {
2768        assert_branch(Some("branch-1"), project, cx)
2769    });
2770
2771    client_a
2772        .fs()
2773        .set_branch_name(Path::new("/dir/.git"), Some("branch-2"));
2774
2775    // Wait for buffer_local_a to receive it
2776    executor.run_until_parked();
2777
2778    // Smoke test branch reading
2779
2780    project_local.read_with(cx_a, |project, cx| {
2781        assert_branch(Some("branch-2"), project, cx)
2782    });
2783
2784    project_remote.read_with(cx_b, |project, cx| {
2785        assert_branch(Some("branch-2"), project, cx)
2786    });
2787
2788    let project_remote_c = client_c.build_remote_project(project_id, cx_c).await;
2789    executor.run_until_parked();
2790
2791    project_remote_c.read_with(cx_c, |project, cx| {
2792        assert_branch(Some("branch-2"), project, cx)
2793    });
2794}
2795
2796#[gpui::test]
2797async fn test_git_status_sync(
2798    executor: BackgroundExecutor,
2799    cx_a: &mut TestAppContext,
2800    cx_b: &mut TestAppContext,
2801    cx_c: &mut TestAppContext,
2802) {
2803    let mut server = TestServer::start(executor.clone()).await;
2804    let client_a = server.create_client(cx_a, "user_a").await;
2805    let client_b = server.create_client(cx_b, "user_b").await;
2806    let client_c = server.create_client(cx_c, "user_c").await;
2807    server
2808        .create_room(&mut [(&client_a, cx_a), (&client_b, cx_b), (&client_c, cx_c)])
2809        .await;
2810    let active_call_a = cx_a.read(ActiveCall::global);
2811
2812    client_a
2813        .fs()
2814        .insert_tree(
2815            "/dir",
2816            json!({
2817            ".git": {},
2818            "a.txt": "a",
2819            "b.txt": "b",
2820            }),
2821        )
2822        .await;
2823
2824    const A_TXT: &str = "a.txt";
2825    const B_TXT: &str = "b.txt";
2826
2827    client_a.fs().set_status_for_repo_via_git_operation(
2828        Path::new("/dir/.git"),
2829        &[
2830            (&Path::new(A_TXT), GitFileStatus::Added),
2831            (&Path::new(B_TXT), GitFileStatus::Added),
2832        ],
2833    );
2834
2835    let (project_local, _worktree_id) = client_a.build_local_project("/dir", cx_a).await;
2836    let project_id = active_call_a
2837        .update(cx_a, |call, cx| {
2838            call.share_project(project_local.clone(), cx)
2839        })
2840        .await
2841        .unwrap();
2842
2843    let project_remote = client_b.build_remote_project(project_id, cx_b).await;
2844
2845    // Wait for it to catch up to the new status
2846    executor.run_until_parked();
2847
2848    #[track_caller]
2849    fn assert_status(
2850        file: &impl AsRef<Path>,
2851        status: Option<GitFileStatus>,
2852        project: &Project,
2853        cx: &AppContext,
2854    ) {
2855        let file = file.as_ref();
2856        let worktrees = project.visible_worktrees(cx).collect::<Vec<_>>();
2857        assert_eq!(worktrees.len(), 1);
2858        let worktree = worktrees[0].clone();
2859        let snapshot = worktree.read(cx).snapshot();
2860        assert_eq!(snapshot.status_for_file(file), status);
2861    }
2862
2863    // Smoke test status reading
2864
2865    project_local.read_with(cx_a, |project, cx| {
2866        assert_status(&Path::new(A_TXT), Some(GitFileStatus::Added), project, cx);
2867        assert_status(&Path::new(B_TXT), Some(GitFileStatus::Added), project, cx);
2868    });
2869
2870    project_remote.read_with(cx_b, |project, cx| {
2871        assert_status(&Path::new(A_TXT), Some(GitFileStatus::Added), project, cx);
2872        assert_status(&Path::new(B_TXT), Some(GitFileStatus::Added), project, cx);
2873    });
2874
2875    client_a.fs().set_status_for_repo_via_working_copy_change(
2876        Path::new("/dir/.git"),
2877        &[
2878            (&Path::new(A_TXT), GitFileStatus::Modified),
2879            (&Path::new(B_TXT), GitFileStatus::Modified),
2880        ],
2881    );
2882
2883    // Wait for buffer_local_a to receive it
2884    executor.run_until_parked();
2885
2886    // Smoke test status reading
2887
2888    project_local.read_with(cx_a, |project, cx| {
2889        assert_status(
2890            &Path::new(A_TXT),
2891            Some(GitFileStatus::Modified),
2892            project,
2893            cx,
2894        );
2895        assert_status(
2896            &Path::new(B_TXT),
2897            Some(GitFileStatus::Modified),
2898            project,
2899            cx,
2900        );
2901    });
2902
2903    project_remote.read_with(cx_b, |project, cx| {
2904        assert_status(
2905            &Path::new(A_TXT),
2906            Some(GitFileStatus::Modified),
2907            project,
2908            cx,
2909        );
2910        assert_status(
2911            &Path::new(B_TXT),
2912            Some(GitFileStatus::Modified),
2913            project,
2914            cx,
2915        );
2916    });
2917
2918    // And synchronization while joining
2919    let project_remote_c = client_c.build_remote_project(project_id, cx_c).await;
2920    executor.run_until_parked();
2921
2922    project_remote_c.read_with(cx_c, |project, cx| {
2923        assert_status(
2924            &Path::new(A_TXT),
2925            Some(GitFileStatus::Modified),
2926            project,
2927            cx,
2928        );
2929        assert_status(
2930            &Path::new(B_TXT),
2931            Some(GitFileStatus::Modified),
2932            project,
2933            cx,
2934        );
2935    });
2936}
2937
2938#[gpui::test(iterations = 10)]
2939async fn test_fs_operations(
2940    executor: BackgroundExecutor,
2941    cx_a: &mut TestAppContext,
2942    cx_b: &mut TestAppContext,
2943) {
2944    let mut server = TestServer::start(executor.clone()).await;
2945    let client_a = server.create_client(cx_a, "user_a").await;
2946    let client_b = server.create_client(cx_b, "user_b").await;
2947    server
2948        .create_room(&mut [(&client_a, cx_a), (&client_b, cx_b)])
2949        .await;
2950    let active_call_a = cx_a.read(ActiveCall::global);
2951
2952    client_a
2953        .fs()
2954        .insert_tree(
2955            "/dir",
2956            json!({
2957                "a.txt": "a-contents",
2958                "b.txt": "b-contents",
2959            }),
2960        )
2961        .await;
2962    let (project_a, worktree_id) = client_a.build_local_project("/dir", cx_a).await;
2963    let project_id = active_call_a
2964        .update(cx_a, |call, cx| call.share_project(project_a.clone(), cx))
2965        .await
2966        .unwrap();
2967    let project_b = client_b.build_remote_project(project_id, cx_b).await;
2968
2969    let worktree_a = project_a.read_with(cx_a, |project, _| project.worktrees().next().unwrap());
2970
2971    let worktree_b = project_b.read_with(cx_b, |project, _| project.worktrees().next().unwrap());
2972
2973    let entry = project_b
2974        .update(cx_b, |project, cx| {
2975            project.create_entry((worktree_id, "c.txt"), false, cx)
2976        })
2977        .await
2978        .unwrap()
2979        .unwrap();
2980
2981    worktree_a.read_with(cx_a, |worktree, _| {
2982        assert_eq!(
2983            worktree
2984                .paths()
2985                .map(|p| p.to_string_lossy())
2986                .collect::<Vec<_>>(),
2987            ["a.txt", "b.txt", "c.txt"]
2988        );
2989    });
2990
2991    worktree_b.read_with(cx_b, |worktree, _| {
2992        assert_eq!(
2993            worktree
2994                .paths()
2995                .map(|p| p.to_string_lossy())
2996                .collect::<Vec<_>>(),
2997            ["a.txt", "b.txt", "c.txt"]
2998        );
2999    });
3000
3001    project_b
3002        .update(cx_b, |project, cx| {
3003            project.rename_entry(entry.id, Path::new("d.txt"), cx)
3004        })
3005        .await
3006        .unwrap()
3007        .unwrap();
3008
3009    worktree_a.read_with(cx_a, |worktree, _| {
3010        assert_eq!(
3011            worktree
3012                .paths()
3013                .map(|p| p.to_string_lossy())
3014                .collect::<Vec<_>>(),
3015            ["a.txt", "b.txt", "d.txt"]
3016        );
3017    });
3018
3019    worktree_b.read_with(cx_b, |worktree, _| {
3020        assert_eq!(
3021            worktree
3022                .paths()
3023                .map(|p| p.to_string_lossy())
3024                .collect::<Vec<_>>(),
3025            ["a.txt", "b.txt", "d.txt"]
3026        );
3027    });
3028
3029    let dir_entry = project_b
3030        .update(cx_b, |project, cx| {
3031            project.create_entry((worktree_id, "DIR"), true, cx)
3032        })
3033        .await
3034        .unwrap()
3035        .unwrap();
3036
3037    worktree_a.read_with(cx_a, |worktree, _| {
3038        assert_eq!(
3039            worktree
3040                .paths()
3041                .map(|p| p.to_string_lossy())
3042                .collect::<Vec<_>>(),
3043            ["DIR", "a.txt", "b.txt", "d.txt"]
3044        );
3045    });
3046
3047    worktree_b.read_with(cx_b, |worktree, _| {
3048        assert_eq!(
3049            worktree
3050                .paths()
3051                .map(|p| p.to_string_lossy())
3052                .collect::<Vec<_>>(),
3053            ["DIR", "a.txt", "b.txt", "d.txt"]
3054        );
3055    });
3056
3057    project_b
3058        .update(cx_b, |project, cx| {
3059            project.create_entry((worktree_id, "DIR/e.txt"), false, cx)
3060        })
3061        .await
3062        .unwrap()
3063        .unwrap();
3064    project_b
3065        .update(cx_b, |project, cx| {
3066            project.create_entry((worktree_id, "DIR/SUBDIR"), true, cx)
3067        })
3068        .await
3069        .unwrap()
3070        .unwrap();
3071    project_b
3072        .update(cx_b, |project, cx| {
3073            project.create_entry((worktree_id, "DIR/SUBDIR/f.txt"), false, cx)
3074        })
3075        .await
3076        .unwrap()
3077        .unwrap();
3078
3079    worktree_a.read_with(cx_a, |worktree, _| {
3080        assert_eq!(
3081            worktree
3082                .paths()
3083                .map(|p| p.to_string_lossy())
3084                .collect::<Vec<_>>(),
3085            [
3086                "DIR",
3087                "DIR/SUBDIR",
3088                "DIR/SUBDIR/f.txt",
3089                "DIR/e.txt",
3090                "a.txt",
3091                "b.txt",
3092                "d.txt"
3093            ]
3094        );
3095    });
3096
3097    worktree_b.read_with(cx_b, |worktree, _| {
3098        assert_eq!(
3099            worktree
3100                .paths()
3101                .map(|p| p.to_string_lossy())
3102                .collect::<Vec<_>>(),
3103            [
3104                "DIR",
3105                "DIR/SUBDIR",
3106                "DIR/SUBDIR/f.txt",
3107                "DIR/e.txt",
3108                "a.txt",
3109                "b.txt",
3110                "d.txt"
3111            ]
3112        );
3113    });
3114
3115    project_b
3116        .update(cx_b, |project, cx| {
3117            project.copy_entry(entry.id, Path::new("f.txt"), cx)
3118        })
3119        .await
3120        .unwrap()
3121        .unwrap();
3122
3123    worktree_a.read_with(cx_a, |worktree, _| {
3124        assert_eq!(
3125            worktree
3126                .paths()
3127                .map(|p| p.to_string_lossy())
3128                .collect::<Vec<_>>(),
3129            [
3130                "DIR",
3131                "DIR/SUBDIR",
3132                "DIR/SUBDIR/f.txt",
3133                "DIR/e.txt",
3134                "a.txt",
3135                "b.txt",
3136                "d.txt",
3137                "f.txt"
3138            ]
3139        );
3140    });
3141
3142    worktree_b.read_with(cx_b, |worktree, _| {
3143        assert_eq!(
3144            worktree
3145                .paths()
3146                .map(|p| p.to_string_lossy())
3147                .collect::<Vec<_>>(),
3148            [
3149                "DIR",
3150                "DIR/SUBDIR",
3151                "DIR/SUBDIR/f.txt",
3152                "DIR/e.txt",
3153                "a.txt",
3154                "b.txt",
3155                "d.txt",
3156                "f.txt"
3157            ]
3158        );
3159    });
3160
3161    project_b
3162        .update(cx_b, |project, cx| {
3163            project.delete_entry(dir_entry.id, cx).unwrap()
3164        })
3165        .await
3166        .unwrap();
3167    executor.run_until_parked();
3168
3169    worktree_a.read_with(cx_a, |worktree, _| {
3170        assert_eq!(
3171            worktree
3172                .paths()
3173                .map(|p| p.to_string_lossy())
3174                .collect::<Vec<_>>(),
3175            ["a.txt", "b.txt", "d.txt", "f.txt"]
3176        );
3177    });
3178
3179    worktree_b.read_with(cx_b, |worktree, _| {
3180        assert_eq!(
3181            worktree
3182                .paths()
3183                .map(|p| p.to_string_lossy())
3184                .collect::<Vec<_>>(),
3185            ["a.txt", "b.txt", "d.txt", "f.txt"]
3186        );
3187    });
3188
3189    project_b
3190        .update(cx_b, |project, cx| {
3191            project.delete_entry(entry.id, cx).unwrap()
3192        })
3193        .await
3194        .unwrap();
3195
3196    worktree_a.read_with(cx_a, |worktree, _| {
3197        assert_eq!(
3198            worktree
3199                .paths()
3200                .map(|p| p.to_string_lossy())
3201                .collect::<Vec<_>>(),
3202            ["a.txt", "b.txt", "f.txt"]
3203        );
3204    });
3205
3206    worktree_b.read_with(cx_b, |worktree, _| {
3207        assert_eq!(
3208            worktree
3209                .paths()
3210                .map(|p| p.to_string_lossy())
3211                .collect::<Vec<_>>(),
3212            ["a.txt", "b.txt", "f.txt"]
3213        );
3214    });
3215}
3216
3217#[gpui::test(iterations = 10)]
3218async fn test_local_settings(
3219    executor: BackgroundExecutor,
3220    cx_a: &mut TestAppContext,
3221    cx_b: &mut TestAppContext,
3222) {
3223    let mut server = TestServer::start(executor.clone()).await;
3224    let client_a = server.create_client(cx_a, "user_a").await;
3225    let client_b = server.create_client(cx_b, "user_b").await;
3226    server
3227        .create_room(&mut [(&client_a, cx_a), (&client_b, cx_b)])
3228        .await;
3229    let active_call_a = cx_a.read(ActiveCall::global);
3230
3231    // As client A, open a project that contains some local settings files
3232    client_a
3233        .fs()
3234        .insert_tree(
3235            "/dir",
3236            json!({
3237                ".zed": {
3238                    "settings.json": r#"{ "tab_size": 2 }"#
3239                },
3240                "a": {
3241                    ".zed": {
3242                        "settings.json": r#"{ "tab_size": 8 }"#
3243                    },
3244                    "a.txt": "a-contents",
3245                },
3246                "b": {
3247                    "b.txt": "b-contents",
3248                }
3249            }),
3250        )
3251        .await;
3252    let (project_a, _) = client_a.build_local_project("/dir", cx_a).await;
3253    executor.run_until_parked();
3254    let project_id = active_call_a
3255        .update(cx_a, |call, cx| call.share_project(project_a.clone(), cx))
3256        .await
3257        .unwrap();
3258    executor.run_until_parked();
3259
3260    // As client B, join that project and observe the local settings.
3261    let project_b = client_b.build_remote_project(project_id, cx_b).await;
3262
3263    let worktree_b = project_b.read_with(cx_b, |project, _| project.worktrees().next().unwrap());
3264    executor.run_until_parked();
3265    cx_b.read(|cx| {
3266        let store = cx.global::<SettingsStore>();
3267        assert_eq!(
3268            store
3269                .local_settings(worktree_b.read(cx).id().to_usize())
3270                .collect::<Vec<_>>(),
3271            &[
3272                (Path::new("").into(), r#"{"tab_size":2}"#.to_string()),
3273                (Path::new("a").into(), r#"{"tab_size":8}"#.to_string()),
3274            ]
3275        )
3276    });
3277
3278    // As client A, update a settings file. As Client B, see the changed settings.
3279    client_a
3280        .fs()
3281        .insert_file("/dir/.zed/settings.json", r#"{}"#.into())
3282        .await;
3283    executor.run_until_parked();
3284    cx_b.read(|cx| {
3285        let store = cx.global::<SettingsStore>();
3286        assert_eq!(
3287            store
3288                .local_settings(worktree_b.read(cx).id().to_usize())
3289                .collect::<Vec<_>>(),
3290            &[
3291                (Path::new("").into(), r#"{}"#.to_string()),
3292                (Path::new("a").into(), r#"{"tab_size":8}"#.to_string()),
3293            ]
3294        )
3295    });
3296
3297    // As client A, create and remove some settings files. As client B, see the changed settings.
3298    client_a
3299        .fs()
3300        .remove_file("/dir/.zed/settings.json".as_ref(), Default::default())
3301        .await
3302        .unwrap();
3303    client_a
3304        .fs()
3305        .create_dir("/dir/b/.zed".as_ref())
3306        .await
3307        .unwrap();
3308    client_a
3309        .fs()
3310        .insert_file("/dir/b/.zed/settings.json", r#"{"tab_size": 4}"#.into())
3311        .await;
3312    executor.run_until_parked();
3313    cx_b.read(|cx| {
3314        let store = cx.global::<SettingsStore>();
3315        assert_eq!(
3316            store
3317                .local_settings(worktree_b.read(cx).id().to_usize())
3318                .collect::<Vec<_>>(),
3319            &[
3320                (Path::new("a").into(), r#"{"tab_size":8}"#.to_string()),
3321                (Path::new("b").into(), r#"{"tab_size":4}"#.to_string()),
3322            ]
3323        )
3324    });
3325
3326    // As client B, disconnect.
3327    server.forbid_connections();
3328    server.disconnect_client(client_b.peer_id().unwrap());
3329
3330    // As client A, change and remove settings files while client B is disconnected.
3331    client_a
3332        .fs()
3333        .insert_file("/dir/a/.zed/settings.json", r#"{"hard_tabs":true}"#.into())
3334        .await;
3335    client_a
3336        .fs()
3337        .remove_file("/dir/b/.zed/settings.json".as_ref(), Default::default())
3338        .await
3339        .unwrap();
3340    executor.run_until_parked();
3341
3342    // As client B, reconnect and see the changed settings.
3343    server.allow_connections();
3344    executor.advance_clock(RECEIVE_TIMEOUT);
3345    cx_b.read(|cx| {
3346        let store = cx.global::<SettingsStore>();
3347        assert_eq!(
3348            store
3349                .local_settings(worktree_b.read(cx).id().to_usize())
3350                .collect::<Vec<_>>(),
3351            &[(Path::new("a").into(), r#"{"hard_tabs":true}"#.to_string()),]
3352        )
3353    });
3354}
3355
3356#[gpui::test(iterations = 10)]
3357async fn test_buffer_conflict_after_save(
3358    executor: BackgroundExecutor,
3359    cx_a: &mut TestAppContext,
3360    cx_b: &mut TestAppContext,
3361) {
3362    let mut server = TestServer::start(executor.clone()).await;
3363    let client_a = server.create_client(cx_a, "user_a").await;
3364    let client_b = server.create_client(cx_b, "user_b").await;
3365    server
3366        .create_room(&mut [(&client_a, cx_a), (&client_b, cx_b)])
3367        .await;
3368    let active_call_a = cx_a.read(ActiveCall::global);
3369
3370    client_a
3371        .fs()
3372        .insert_tree(
3373            "/dir",
3374            json!({
3375                "a.txt": "a-contents",
3376            }),
3377        )
3378        .await;
3379    let (project_a, worktree_id) = client_a.build_local_project("/dir", cx_a).await;
3380    let project_id = active_call_a
3381        .update(cx_a, |call, cx| call.share_project(project_a.clone(), cx))
3382        .await
3383        .unwrap();
3384    let project_b = client_b.build_remote_project(project_id, cx_b).await;
3385
3386    // Open a buffer as client B
3387    let buffer_b = project_b
3388        .update(cx_b, |p, cx| p.open_buffer((worktree_id, "a.txt"), cx))
3389        .await
3390        .unwrap();
3391
3392    buffer_b.update(cx_b, |buf, cx| buf.edit([(0..0, "world ")], None, cx));
3393
3394    buffer_b.read_with(cx_b, |buf, _| {
3395        assert!(buf.is_dirty());
3396        assert!(!buf.has_conflict());
3397    });
3398
3399    project_b
3400        .update(cx_b, |project, cx| {
3401            project.save_buffer(buffer_b.clone(), cx)
3402        })
3403        .await
3404        .unwrap();
3405
3406    buffer_b.read_with(cx_b, |buffer_b, _| assert!(!buffer_b.is_dirty()));
3407
3408    buffer_b.read_with(cx_b, |buf, _| {
3409        assert!(!buf.has_conflict());
3410    });
3411
3412    buffer_b.update(cx_b, |buf, cx| buf.edit([(0..0, "hello ")], None, cx));
3413
3414    buffer_b.read_with(cx_b, |buf, _| {
3415        assert!(buf.is_dirty());
3416        assert!(!buf.has_conflict());
3417    });
3418}
3419
3420#[gpui::test(iterations = 10)]
3421async fn test_buffer_reloading(
3422    executor: BackgroundExecutor,
3423    cx_a: &mut TestAppContext,
3424    cx_b: &mut TestAppContext,
3425) {
3426    let mut server = TestServer::start(executor.clone()).await;
3427    let client_a = server.create_client(cx_a, "user_a").await;
3428    let client_b = server.create_client(cx_b, "user_b").await;
3429    server
3430        .create_room(&mut [(&client_a, cx_a), (&client_b, cx_b)])
3431        .await;
3432    let active_call_a = cx_a.read(ActiveCall::global);
3433
3434    client_a
3435        .fs()
3436        .insert_tree(
3437            "/dir",
3438            json!({
3439                "a.txt": "a\nb\nc",
3440            }),
3441        )
3442        .await;
3443    let (project_a, worktree_id) = client_a.build_local_project("/dir", cx_a).await;
3444    let project_id = active_call_a
3445        .update(cx_a, |call, cx| call.share_project(project_a.clone(), cx))
3446        .await
3447        .unwrap();
3448    let project_b = client_b.build_remote_project(project_id, cx_b).await;
3449
3450    // Open a buffer as client B
3451    let buffer_b = project_b
3452        .update(cx_b, |p, cx| p.open_buffer((worktree_id, "a.txt"), cx))
3453        .await
3454        .unwrap();
3455
3456    buffer_b.read_with(cx_b, |buf, _| {
3457        assert!(!buf.is_dirty());
3458        assert!(!buf.has_conflict());
3459        assert_eq!(buf.line_ending(), LineEnding::Unix);
3460    });
3461
3462    let new_contents = Rope::from("d\ne\nf");
3463    client_a
3464        .fs()
3465        .save("/dir/a.txt".as_ref(), &new_contents, LineEnding::Windows)
3466        .await
3467        .unwrap();
3468
3469    executor.run_until_parked();
3470
3471    buffer_b.read_with(cx_b, |buf, _| {
3472        assert_eq!(buf.text(), new_contents.to_string());
3473        assert!(!buf.is_dirty());
3474        assert!(!buf.has_conflict());
3475        assert_eq!(buf.line_ending(), LineEnding::Windows);
3476    });
3477}
3478
3479#[gpui::test(iterations = 10)]
3480async fn test_editing_while_guest_opens_buffer(
3481    executor: BackgroundExecutor,
3482    cx_a: &mut TestAppContext,
3483    cx_b: &mut TestAppContext,
3484) {
3485    let mut server = TestServer::start(executor.clone()).await;
3486    let client_a = server.create_client(cx_a, "user_a").await;
3487    let client_b = server.create_client(cx_b, "user_b").await;
3488    server
3489        .create_room(&mut [(&client_a, cx_a), (&client_b, cx_b)])
3490        .await;
3491    let active_call_a = cx_a.read(ActiveCall::global);
3492
3493    client_a
3494        .fs()
3495        .insert_tree("/dir", json!({ "a.txt": "a-contents" }))
3496        .await;
3497    let (project_a, worktree_id) = client_a.build_local_project("/dir", cx_a).await;
3498    let project_id = active_call_a
3499        .update(cx_a, |call, cx| call.share_project(project_a.clone(), cx))
3500        .await
3501        .unwrap();
3502    let project_b = client_b.build_remote_project(project_id, cx_b).await;
3503
3504    // Open a buffer as client A
3505    let buffer_a = project_a
3506        .update(cx_a, |p, cx| p.open_buffer((worktree_id, "a.txt"), cx))
3507        .await
3508        .unwrap();
3509
3510    // Start opening the same buffer as client B
3511    let open_buffer = project_b.update(cx_b, |p, cx| p.open_buffer((worktree_id, "a.txt"), cx));
3512    let buffer_b = cx_b.executor().spawn(open_buffer);
3513
3514    // Edit the buffer as client A while client B is still opening it.
3515    cx_b.executor().simulate_random_delay().await;
3516    buffer_a.update(cx_a, |buf, cx| buf.edit([(0..0, "X")], None, cx));
3517    cx_b.executor().simulate_random_delay().await;
3518    buffer_a.update(cx_a, |buf, cx| buf.edit([(1..1, "Y")], None, cx));
3519
3520    let text = buffer_a.read_with(cx_a, |buf, _| buf.text());
3521    let buffer_b = buffer_b.await.unwrap();
3522    executor.run_until_parked();
3523
3524    buffer_b.read_with(cx_b, |buf, _| assert_eq!(buf.text(), text));
3525}
3526
3527#[gpui::test(iterations = 10)]
3528async fn test_leaving_worktree_while_opening_buffer(
3529    executor: BackgroundExecutor,
3530    cx_a: &mut TestAppContext,
3531    cx_b: &mut TestAppContext,
3532) {
3533    let mut server = TestServer::start(executor.clone()).await;
3534    let client_a = server.create_client(cx_a, "user_a").await;
3535    let client_b = server.create_client(cx_b, "user_b").await;
3536    server
3537        .create_room(&mut [(&client_a, cx_a), (&client_b, cx_b)])
3538        .await;
3539    let active_call_a = cx_a.read(ActiveCall::global);
3540
3541    client_a
3542        .fs()
3543        .insert_tree("/dir", json!({ "a.txt": "a-contents" }))
3544        .await;
3545    let (project_a, worktree_id) = client_a.build_local_project("/dir", cx_a).await;
3546    let project_id = active_call_a
3547        .update(cx_a, |call, cx| call.share_project(project_a.clone(), cx))
3548        .await
3549        .unwrap();
3550    let project_b = client_b.build_remote_project(project_id, cx_b).await;
3551
3552    // See that a guest has joined as client A.
3553    executor.run_until_parked();
3554
3555    project_a.read_with(cx_a, |p, _| assert_eq!(p.collaborators().len(), 1));
3556
3557    // Begin opening a buffer as client B, but leave the project before the open completes.
3558    let open_buffer = project_b.update(cx_b, |p, cx| p.open_buffer((worktree_id, "a.txt"), cx));
3559    let buffer_b = cx_b.executor().spawn(open_buffer);
3560    cx_b.update(|_| drop(project_b));
3561    drop(buffer_b);
3562
3563    // See that the guest has left.
3564    executor.run_until_parked();
3565
3566    project_a.read_with(cx_a, |p, _| assert!(p.collaborators().is_empty()));
3567}
3568
3569#[gpui::test(iterations = 10)]
3570async fn test_canceling_buffer_opening(
3571    executor: BackgroundExecutor,
3572    cx_a: &mut TestAppContext,
3573    cx_b: &mut TestAppContext,
3574) {
3575    let mut server = TestServer::start(executor.clone()).await;
3576    let client_a = server.create_client(cx_a, "user_a").await;
3577    let client_b = server.create_client(cx_b, "user_b").await;
3578    server
3579        .create_room(&mut [(&client_a, cx_a), (&client_b, cx_b)])
3580        .await;
3581    let active_call_a = cx_a.read(ActiveCall::global);
3582
3583    client_a
3584        .fs()
3585        .insert_tree(
3586            "/dir",
3587            json!({
3588                "a.txt": "abc",
3589            }),
3590        )
3591        .await;
3592    let (project_a, worktree_id) = client_a.build_local_project("/dir", cx_a).await;
3593    let project_id = active_call_a
3594        .update(cx_a, |call, cx| call.share_project(project_a.clone(), cx))
3595        .await
3596        .unwrap();
3597    let project_b = client_b.build_remote_project(project_id, cx_b).await;
3598
3599    let buffer_a = project_a
3600        .update(cx_a, |p, cx| p.open_buffer((worktree_id, "a.txt"), cx))
3601        .await
3602        .unwrap();
3603
3604    // Open a buffer as client B but cancel after a random amount of time.
3605    let buffer_b = project_b.update(cx_b, |p, cx| {
3606        p.open_buffer_by_id(buffer_a.read_with(cx_a, |a, _| a.remote_id()), cx)
3607    });
3608    executor.simulate_random_delay().await;
3609    drop(buffer_b);
3610
3611    // Try opening the same buffer again as client B, and ensure we can
3612    // still do it despite the cancellation above.
3613    let buffer_b = project_b
3614        .update(cx_b, |p, cx| {
3615            p.open_buffer_by_id(buffer_a.read_with(cx_a, |a, _| a.remote_id()), cx)
3616        })
3617        .await
3618        .unwrap();
3619
3620    buffer_b.read_with(cx_b, |buf, _| assert_eq!(buf.text(), "abc"));
3621}
3622
3623#[gpui::test(iterations = 10)]
3624async fn test_leaving_project(
3625    executor: BackgroundExecutor,
3626    cx_a: &mut TestAppContext,
3627    cx_b: &mut TestAppContext,
3628    cx_c: &mut TestAppContext,
3629) {
3630    let mut server = TestServer::start(executor.clone()).await;
3631    let client_a = server.create_client(cx_a, "user_a").await;
3632    let client_b = server.create_client(cx_b, "user_b").await;
3633    let client_c = server.create_client(cx_c, "user_c").await;
3634    server
3635        .create_room(&mut [(&client_a, cx_a), (&client_b, cx_b), (&client_c, cx_c)])
3636        .await;
3637    let active_call_a = cx_a.read(ActiveCall::global);
3638
3639    client_a
3640        .fs()
3641        .insert_tree(
3642            "/a",
3643            json!({
3644                "a.txt": "a-contents",
3645                "b.txt": "b-contents",
3646            }),
3647        )
3648        .await;
3649    let (project_a, _) = client_a.build_local_project("/a", cx_a).await;
3650    let project_id = active_call_a
3651        .update(cx_a, |call, cx| call.share_project(project_a.clone(), cx))
3652        .await
3653        .unwrap();
3654    let project_b1 = client_b.build_remote_project(project_id, cx_b).await;
3655    let project_c = client_c.build_remote_project(project_id, cx_c).await;
3656
3657    // Client A sees that a guest has joined.
3658    executor.run_until_parked();
3659
3660    project_a.read_with(cx_a, |project, _| {
3661        assert_eq!(project.collaborators().len(), 2);
3662    });
3663
3664    project_b1.read_with(cx_b, |project, _| {
3665        assert_eq!(project.collaborators().len(), 2);
3666    });
3667
3668    project_c.read_with(cx_c, |project, _| {
3669        assert_eq!(project.collaborators().len(), 2);
3670    });
3671
3672    // Client B opens a buffer.
3673    let buffer_b1 = project_b1
3674        .update(cx_b, |project, cx| {
3675            let worktree_id = project.worktrees().next().unwrap().read(cx).id();
3676            project.open_buffer((worktree_id, "a.txt"), cx)
3677        })
3678        .await
3679        .unwrap();
3680
3681    buffer_b1.read_with(cx_b, |buffer, _| assert_eq!(buffer.text(), "a-contents"));
3682
3683    // Drop client B's project and ensure client A and client C observe client B leaving.
3684    cx_b.update(|_| drop(project_b1));
3685    executor.run_until_parked();
3686
3687    project_a.read_with(cx_a, |project, _| {
3688        assert_eq!(project.collaborators().len(), 1);
3689    });
3690
3691    project_c.read_with(cx_c, |project, _| {
3692        assert_eq!(project.collaborators().len(), 1);
3693    });
3694
3695    // Client B re-joins the project and can open buffers as before.
3696    let project_b2 = client_b.build_remote_project(project_id, cx_b).await;
3697    executor.run_until_parked();
3698
3699    project_a.read_with(cx_a, |project, _| {
3700        assert_eq!(project.collaborators().len(), 2);
3701    });
3702
3703    project_b2.read_with(cx_b, |project, _| {
3704        assert_eq!(project.collaborators().len(), 2);
3705    });
3706
3707    project_c.read_with(cx_c, |project, _| {
3708        assert_eq!(project.collaborators().len(), 2);
3709    });
3710
3711    let buffer_b2 = project_b2
3712        .update(cx_b, |project, cx| {
3713            let worktree_id = project.worktrees().next().unwrap().read(cx).id();
3714            project.open_buffer((worktree_id, "a.txt"), cx)
3715        })
3716        .await
3717        .unwrap();
3718
3719    buffer_b2.read_with(cx_b, |buffer, _| assert_eq!(buffer.text(), "a-contents"));
3720
3721    // Drop client B's connection and ensure client A and client C observe client B leaving.
3722    client_b.disconnect(&cx_b.to_async());
3723    executor.advance_clock(RECONNECT_TIMEOUT);
3724
3725    project_a.read_with(cx_a, |project, _| {
3726        assert_eq!(project.collaborators().len(), 1);
3727    });
3728
3729    project_b2.read_with(cx_b, |project, _| {
3730        assert!(project.is_disconnected());
3731    });
3732
3733    project_c.read_with(cx_c, |project, _| {
3734        assert_eq!(project.collaborators().len(), 1);
3735    });
3736
3737    // Client B can't join the project, unless they re-join the room.
3738    cx_b.spawn(|cx| {
3739        Project::remote(
3740            project_id,
3741            client_b.app_state.client.clone(),
3742            client_b.user_store().clone(),
3743            client_b.language_registry().clone(),
3744            FakeFs::new(cx.background_executor().clone()),
3745            cx,
3746        )
3747    })
3748    .await
3749    .unwrap_err();
3750
3751    // Simulate connection loss for client C and ensure client A observes client C leaving the project.
3752    client_c.wait_for_current_user(cx_c).await;
3753    server.forbid_connections();
3754    server.disconnect_client(client_c.peer_id().unwrap());
3755    executor.advance_clock(RECEIVE_TIMEOUT + RECONNECT_TIMEOUT);
3756    executor.run_until_parked();
3757
3758    project_a.read_with(cx_a, |project, _| {
3759        assert_eq!(project.collaborators().len(), 0);
3760    });
3761
3762    project_b2.read_with(cx_b, |project, _| {
3763        assert!(project.is_disconnected());
3764    });
3765
3766    project_c.read_with(cx_c, |project, _| {
3767        assert!(project.is_disconnected());
3768    });
3769}
3770
3771#[gpui::test(iterations = 10)]
3772async fn test_collaborating_with_diagnostics(
3773    executor: BackgroundExecutor,
3774    cx_a: &mut TestAppContext,
3775    cx_b: &mut TestAppContext,
3776    cx_c: &mut TestAppContext,
3777) {
3778    let mut server = TestServer::start(executor.clone()).await;
3779    let client_a = server.create_client(cx_a, "user_a").await;
3780    let client_b = server.create_client(cx_b, "user_b").await;
3781    let client_c = server.create_client(cx_c, "user_c").await;
3782    server
3783        .create_room(&mut [(&client_a, cx_a), (&client_b, cx_b), (&client_c, cx_c)])
3784        .await;
3785    let active_call_a = cx_a.read(ActiveCall::global);
3786
3787    client_a.language_registry().add(Arc::new(Language::new(
3788        LanguageConfig {
3789            name: "Rust".into(),
3790            matcher: LanguageMatcher {
3791                path_suffixes: vec!["rs".to_string()],
3792                ..Default::default()
3793            },
3794            ..Default::default()
3795        },
3796        Some(tree_sitter_rust::language()),
3797    )));
3798    let mut fake_language_servers = client_a
3799        .language_registry()
3800        .register_fake_lsp_adapter("Rust", Default::default());
3801
3802    // Share a project as client A
3803    client_a
3804        .fs()
3805        .insert_tree(
3806            "/a",
3807            json!({
3808                "a.rs": "let one = two",
3809                "other.rs": "",
3810            }),
3811        )
3812        .await;
3813    let (project_a, worktree_id) = client_a.build_local_project("/a", cx_a).await;
3814
3815    // Cause the language server to start.
3816    let _buffer = project_a
3817        .update(cx_a, |project, cx| {
3818            project.open_buffer(
3819                ProjectPath {
3820                    worktree_id,
3821                    path: Path::new("other.rs").into(),
3822                },
3823                cx,
3824            )
3825        })
3826        .await
3827        .unwrap();
3828
3829    // Simulate a language server reporting errors for a file.
3830    let mut fake_language_server = fake_language_servers.next().await.unwrap();
3831    fake_language_server
3832        .receive_notification::<lsp::notification::DidOpenTextDocument>()
3833        .await;
3834    fake_language_server.notify::<lsp::notification::PublishDiagnostics>(
3835        lsp::PublishDiagnosticsParams {
3836            uri: lsp::Url::from_file_path("/a/a.rs").unwrap(),
3837            version: None,
3838            diagnostics: vec![lsp::Diagnostic {
3839                severity: Some(lsp::DiagnosticSeverity::WARNING),
3840                range: lsp::Range::new(lsp::Position::new(0, 4), lsp::Position::new(0, 7)),
3841                message: "message 0".to_string(),
3842                ..Default::default()
3843            }],
3844        },
3845    );
3846
3847    // Client A shares the project and, simultaneously, the language server
3848    // publishes a diagnostic. This is done to ensure that the server always
3849    // observes the latest diagnostics for a worktree.
3850    let project_id = active_call_a
3851        .update(cx_a, |call, cx| call.share_project(project_a.clone(), cx))
3852        .await
3853        .unwrap();
3854    fake_language_server.notify::<lsp::notification::PublishDiagnostics>(
3855        lsp::PublishDiagnosticsParams {
3856            uri: lsp::Url::from_file_path("/a/a.rs").unwrap(),
3857            version: None,
3858            diagnostics: vec![lsp::Diagnostic {
3859                severity: Some(lsp::DiagnosticSeverity::ERROR),
3860                range: lsp::Range::new(lsp::Position::new(0, 4), lsp::Position::new(0, 7)),
3861                message: "message 1".to_string(),
3862                ..Default::default()
3863            }],
3864        },
3865    );
3866
3867    // Join the worktree as client B.
3868    let project_b = client_b.build_remote_project(project_id, cx_b).await;
3869
3870    // Wait for server to see the diagnostics update.
3871    executor.run_until_parked();
3872
3873    // Ensure client B observes the new diagnostics.
3874
3875    project_b.read_with(cx_b, |project, cx| {
3876        assert_eq!(
3877            project.diagnostic_summaries(false, cx).collect::<Vec<_>>(),
3878            &[(
3879                ProjectPath {
3880                    worktree_id,
3881                    path: Arc::from(Path::new("a.rs")),
3882                },
3883                LanguageServerId(0),
3884                DiagnosticSummary {
3885                    error_count: 1,
3886                    warning_count: 0,
3887                },
3888            )]
3889        )
3890    });
3891
3892    // Join project as client C and observe the diagnostics.
3893    let project_c = client_c.build_remote_project(project_id, cx_c).await;
3894    executor.run_until_parked();
3895    let project_c_diagnostic_summaries =
3896        Rc::new(RefCell::new(project_c.read_with(cx_c, |project, cx| {
3897            project.diagnostic_summaries(false, cx).collect::<Vec<_>>()
3898        })));
3899    project_c.update(cx_c, |_, cx| {
3900        let summaries = project_c_diagnostic_summaries.clone();
3901        cx.subscribe(&project_c, {
3902            move |p, _, event, cx| {
3903                if let project::Event::DiskBasedDiagnosticsFinished { .. } = event {
3904                    *summaries.borrow_mut() = p.diagnostic_summaries(false, cx).collect();
3905                }
3906            }
3907        })
3908        .detach();
3909    });
3910
3911    executor.run_until_parked();
3912    assert_eq!(
3913        project_c_diagnostic_summaries.borrow().as_slice(),
3914        &[(
3915            ProjectPath {
3916                worktree_id,
3917                path: Arc::from(Path::new("a.rs")),
3918            },
3919            LanguageServerId(0),
3920            DiagnosticSummary {
3921                error_count: 1,
3922                warning_count: 0,
3923            },
3924        )]
3925    );
3926
3927    // Simulate a language server reporting more errors for a file.
3928    fake_language_server.notify::<lsp::notification::PublishDiagnostics>(
3929        lsp::PublishDiagnosticsParams {
3930            uri: lsp::Url::from_file_path("/a/a.rs").unwrap(),
3931            version: None,
3932            diagnostics: vec![
3933                lsp::Diagnostic {
3934                    severity: Some(lsp::DiagnosticSeverity::ERROR),
3935                    range: lsp::Range::new(lsp::Position::new(0, 4), lsp::Position::new(0, 7)),
3936                    message: "message 1".to_string(),
3937                    ..Default::default()
3938                },
3939                lsp::Diagnostic {
3940                    severity: Some(lsp::DiagnosticSeverity::WARNING),
3941                    range: lsp::Range::new(lsp::Position::new(0, 10), lsp::Position::new(0, 13)),
3942                    message: "message 2".to_string(),
3943                    ..Default::default()
3944                },
3945            ],
3946        },
3947    );
3948
3949    // Clients B and C get the updated summaries
3950    executor.run_until_parked();
3951
3952    project_b.read_with(cx_b, |project, cx| {
3953        assert_eq!(
3954            project.diagnostic_summaries(false, cx).collect::<Vec<_>>(),
3955            [(
3956                ProjectPath {
3957                    worktree_id,
3958                    path: Arc::from(Path::new("a.rs")),
3959                },
3960                LanguageServerId(0),
3961                DiagnosticSummary {
3962                    error_count: 1,
3963                    warning_count: 1,
3964                },
3965            )]
3966        );
3967    });
3968
3969    project_c.read_with(cx_c, |project, cx| {
3970        assert_eq!(
3971            project.diagnostic_summaries(false, cx).collect::<Vec<_>>(),
3972            [(
3973                ProjectPath {
3974                    worktree_id,
3975                    path: Arc::from(Path::new("a.rs")),
3976                },
3977                LanguageServerId(0),
3978                DiagnosticSummary {
3979                    error_count: 1,
3980                    warning_count: 1,
3981                },
3982            )]
3983        );
3984    });
3985
3986    // Open the file with the errors on client B. They should be present.
3987    let open_buffer = project_b.update(cx_b, |p, cx| p.open_buffer((worktree_id, "a.rs"), cx));
3988    let buffer_b = cx_b.executor().spawn(open_buffer).await.unwrap();
3989
3990    buffer_b.read_with(cx_b, |buffer, _| {
3991        assert_eq!(
3992            buffer
3993                .snapshot()
3994                .diagnostics_in_range::<_, Point>(0..buffer.len(), false)
3995                .collect::<Vec<_>>(),
3996            &[
3997                DiagnosticEntry {
3998                    range: Point::new(0, 4)..Point::new(0, 7),
3999                    diagnostic: Diagnostic {
4000                        group_id: 2,
4001                        message: "message 1".to_string(),
4002                        severity: lsp::DiagnosticSeverity::ERROR,
4003                        is_primary: true,
4004                        ..Default::default()
4005                    }
4006                },
4007                DiagnosticEntry {
4008                    range: Point::new(0, 10)..Point::new(0, 13),
4009                    diagnostic: Diagnostic {
4010                        group_id: 3,
4011                        severity: lsp::DiagnosticSeverity::WARNING,
4012                        message: "message 2".to_string(),
4013                        is_primary: true,
4014                        ..Default::default()
4015                    }
4016                }
4017            ]
4018        );
4019    });
4020
4021    // Simulate a language server reporting no errors for a file.
4022    fake_language_server.notify::<lsp::notification::PublishDiagnostics>(
4023        lsp::PublishDiagnosticsParams {
4024            uri: lsp::Url::from_file_path("/a/a.rs").unwrap(),
4025            version: None,
4026            diagnostics: vec![],
4027        },
4028    );
4029    executor.run_until_parked();
4030
4031    project_a.read_with(cx_a, |project, cx| {
4032        assert_eq!(
4033            project.diagnostic_summaries(false, cx).collect::<Vec<_>>(),
4034            []
4035        )
4036    });
4037
4038    project_b.read_with(cx_b, |project, cx| {
4039        assert_eq!(
4040            project.diagnostic_summaries(false, cx).collect::<Vec<_>>(),
4041            []
4042        )
4043    });
4044
4045    project_c.read_with(cx_c, |project, cx| {
4046        assert_eq!(
4047            project.diagnostic_summaries(false, cx).collect::<Vec<_>>(),
4048            []
4049        )
4050    });
4051}
4052
4053#[gpui::test(iterations = 10)]
4054async fn test_collaborating_with_lsp_progress_updates_and_diagnostics_ordering(
4055    executor: BackgroundExecutor,
4056    cx_a: &mut TestAppContext,
4057    cx_b: &mut TestAppContext,
4058) {
4059    let mut server = TestServer::start(executor.clone()).await;
4060    let client_a = server.create_client(cx_a, "user_a").await;
4061    let client_b = server.create_client(cx_b, "user_b").await;
4062    server
4063        .create_room(&mut [(&client_a, cx_a), (&client_b, cx_b)])
4064        .await;
4065
4066    client_a.language_registry().add(rust_lang());
4067    let mut fake_language_servers = client_a.language_registry().register_fake_lsp_adapter(
4068        "Rust",
4069        FakeLspAdapter {
4070            disk_based_diagnostics_progress_token: Some("the-disk-based-token".into()),
4071            disk_based_diagnostics_sources: vec!["the-disk-based-diagnostics-source".into()],
4072            ..Default::default()
4073        },
4074    );
4075
4076    let file_names = &["one.rs", "two.rs", "three.rs", "four.rs", "five.rs"];
4077    client_a
4078        .fs()
4079        .insert_tree(
4080            "/test",
4081            json!({
4082                "one.rs": "const ONE: usize = 1;",
4083                "two.rs": "const TWO: usize = 2;",
4084                "three.rs": "const THREE: usize = 3;",
4085                "four.rs": "const FOUR: usize = 3;",
4086                "five.rs": "const FIVE: usize = 3;",
4087            }),
4088        )
4089        .await;
4090
4091    let (project_a, worktree_id) = client_a.build_local_project("/test", cx_a).await;
4092
4093    // Share a project as client A
4094    let active_call_a = cx_a.read(ActiveCall::global);
4095    let project_id = active_call_a
4096        .update(cx_a, |call, cx| call.share_project(project_a.clone(), cx))
4097        .await
4098        .unwrap();
4099
4100    // Join the project as client B and open all three files.
4101    let project_b = client_b.build_remote_project(project_id, cx_b).await;
4102    let guest_buffers = futures::future::try_join_all(file_names.iter().map(|file_name| {
4103        project_b.update(cx_b, |p, cx| p.open_buffer((worktree_id, file_name), cx))
4104    }))
4105    .await
4106    .unwrap();
4107
4108    // Simulate a language server reporting errors for a file.
4109    let fake_language_server = fake_language_servers.next().await.unwrap();
4110    fake_language_server
4111        .request::<lsp::request::WorkDoneProgressCreate>(lsp::WorkDoneProgressCreateParams {
4112            token: lsp::NumberOrString::String("the-disk-based-token".to_string()),
4113        })
4114        .await
4115        .unwrap();
4116    fake_language_server.notify::<lsp::notification::Progress>(lsp::ProgressParams {
4117        token: lsp::NumberOrString::String("the-disk-based-token".to_string()),
4118        value: lsp::ProgressParamsValue::WorkDone(lsp::WorkDoneProgress::Begin(
4119            lsp::WorkDoneProgressBegin {
4120                title: "Progress Began".into(),
4121                ..Default::default()
4122            },
4123        )),
4124    });
4125    for file_name in file_names {
4126        fake_language_server.notify::<lsp::notification::PublishDiagnostics>(
4127            lsp::PublishDiagnosticsParams {
4128                uri: lsp::Url::from_file_path(Path::new("/test").join(file_name)).unwrap(),
4129                version: None,
4130                diagnostics: vec![lsp::Diagnostic {
4131                    severity: Some(lsp::DiagnosticSeverity::WARNING),
4132                    source: Some("the-disk-based-diagnostics-source".into()),
4133                    range: lsp::Range::new(lsp::Position::new(0, 0), lsp::Position::new(0, 0)),
4134                    message: "message one".to_string(),
4135                    ..Default::default()
4136                }],
4137            },
4138        );
4139    }
4140    fake_language_server.notify::<lsp::notification::Progress>(lsp::ProgressParams {
4141        token: lsp::NumberOrString::String("the-disk-based-token".to_string()),
4142        value: lsp::ProgressParamsValue::WorkDone(lsp::WorkDoneProgress::End(
4143            lsp::WorkDoneProgressEnd { message: None },
4144        )),
4145    });
4146
4147    // When the "disk base diagnostics finished" message is received, the buffers'
4148    // diagnostics are expected to be present.
4149    let disk_based_diagnostics_finished = Arc::new(AtomicBool::new(false));
4150    project_b.update(cx_b, {
4151        let project_b = project_b.clone();
4152        let disk_based_diagnostics_finished = disk_based_diagnostics_finished.clone();
4153        move |_, cx| {
4154            cx.subscribe(&project_b, move |_, _, event, cx| {
4155                if let project::Event::DiskBasedDiagnosticsFinished { .. } = event {
4156                    disk_based_diagnostics_finished.store(true, SeqCst);
4157                    for buffer in &guest_buffers {
4158                        assert_eq!(
4159                            buffer
4160                                .read(cx)
4161                                .snapshot()
4162                                .diagnostics_in_range::<_, usize>(0..5, false)
4163                                .count(),
4164                            1,
4165                            "expected a diagnostic for buffer {:?}",
4166                            buffer.read(cx).file().unwrap().path(),
4167                        );
4168                    }
4169                }
4170            })
4171            .detach();
4172        }
4173    });
4174
4175    executor.run_until_parked();
4176    assert!(disk_based_diagnostics_finished.load(SeqCst));
4177}
4178
4179#[gpui::test(iterations = 10)]
4180async fn test_reloading_buffer_manually(
4181    executor: BackgroundExecutor,
4182    cx_a: &mut TestAppContext,
4183    cx_b: &mut TestAppContext,
4184) {
4185    let mut server = TestServer::start(executor.clone()).await;
4186    let client_a = server.create_client(cx_a, "user_a").await;
4187    let client_b = server.create_client(cx_b, "user_b").await;
4188    server
4189        .create_room(&mut [(&client_a, cx_a), (&client_b, cx_b)])
4190        .await;
4191    let active_call_a = cx_a.read(ActiveCall::global);
4192
4193    client_a
4194        .fs()
4195        .insert_tree("/a", json!({ "a.rs": "let one = 1;" }))
4196        .await;
4197    let (project_a, worktree_id) = client_a.build_local_project("/a", cx_a).await;
4198    let buffer_a = project_a
4199        .update(cx_a, |p, cx| p.open_buffer((worktree_id, "a.rs"), cx))
4200        .await
4201        .unwrap();
4202    let project_id = active_call_a
4203        .update(cx_a, |call, cx| call.share_project(project_a.clone(), cx))
4204        .await
4205        .unwrap();
4206
4207    let project_b = client_b.build_remote_project(project_id, cx_b).await;
4208
4209    let open_buffer = project_b.update(cx_b, |p, cx| p.open_buffer((worktree_id, "a.rs"), cx));
4210    let buffer_b = cx_b.executor().spawn(open_buffer).await.unwrap();
4211    buffer_b.update(cx_b, |buffer, cx| {
4212        buffer.edit([(4..7, "six")], None, cx);
4213        buffer.edit([(10..11, "6")], None, cx);
4214        assert_eq!(buffer.text(), "let six = 6;");
4215        assert!(buffer.is_dirty());
4216        assert!(!buffer.has_conflict());
4217    });
4218    executor.run_until_parked();
4219
4220    buffer_a.read_with(cx_a, |buffer, _| assert_eq!(buffer.text(), "let six = 6;"));
4221
4222    client_a
4223        .fs()
4224        .save(
4225            "/a/a.rs".as_ref(),
4226            &Rope::from("let seven = 7;"),
4227            LineEnding::Unix,
4228        )
4229        .await
4230        .unwrap();
4231    executor.run_until_parked();
4232
4233    buffer_a.read_with(cx_a, |buffer, _| assert!(buffer.has_conflict()));
4234
4235    buffer_b.read_with(cx_b, |buffer, _| assert!(buffer.has_conflict()));
4236
4237    project_b
4238        .update(cx_b, |project, cx| {
4239            project.reload_buffers(HashSet::from_iter([buffer_b.clone()]), true, cx)
4240        })
4241        .await
4242        .unwrap();
4243
4244    buffer_a.read_with(cx_a, |buffer, _| {
4245        assert_eq!(buffer.text(), "let seven = 7;");
4246        assert!(!buffer.is_dirty());
4247        assert!(!buffer.has_conflict());
4248    });
4249
4250    buffer_b.read_with(cx_b, |buffer, _| {
4251        assert_eq!(buffer.text(), "let seven = 7;");
4252        assert!(!buffer.is_dirty());
4253        assert!(!buffer.has_conflict());
4254    });
4255
4256    buffer_a.update(cx_a, |buffer, cx| {
4257        // Undoing on the host is a no-op when the reload was initiated by the guest.
4258        buffer.undo(cx);
4259        assert_eq!(buffer.text(), "let seven = 7;");
4260        assert!(!buffer.is_dirty());
4261        assert!(!buffer.has_conflict());
4262    });
4263    buffer_b.update(cx_b, |buffer, cx| {
4264        // Undoing on the guest rolls back the buffer to before it was reloaded but the conflict gets cleared.
4265        buffer.undo(cx);
4266        assert_eq!(buffer.text(), "let six = 6;");
4267        assert!(buffer.is_dirty());
4268        assert!(!buffer.has_conflict());
4269    });
4270}
4271
4272#[gpui::test(iterations = 10)]
4273async fn test_formatting_buffer(
4274    executor: BackgroundExecutor,
4275    cx_a: &mut TestAppContext,
4276    cx_b: &mut TestAppContext,
4277) {
4278    executor.allow_parking();
4279    let mut server = TestServer::start(executor.clone()).await;
4280    let client_a = server.create_client(cx_a, "user_a").await;
4281    let client_b = server.create_client(cx_b, "user_b").await;
4282    server
4283        .create_room(&mut [(&client_a, cx_a), (&client_b, cx_b)])
4284        .await;
4285    let active_call_a = cx_a.read(ActiveCall::global);
4286
4287    client_a.language_registry().add(rust_lang());
4288    let mut fake_language_servers = client_a
4289        .language_registry()
4290        .register_fake_lsp_adapter("Rust", FakeLspAdapter::default());
4291
4292    // Here we insert a fake tree with a directory that exists on disk. This is needed
4293    // because later we'll invoke a command, which requires passing a working directory
4294    // that points to a valid location on disk.
4295    let directory = env::current_dir().unwrap();
4296    client_a
4297        .fs()
4298        .insert_tree(&directory, json!({ "a.rs": "let one = \"two\"" }))
4299        .await;
4300    let (project_a, worktree_id) = client_a.build_local_project(&directory, cx_a).await;
4301    let project_id = active_call_a
4302        .update(cx_a, |call, cx| call.share_project(project_a.clone(), cx))
4303        .await
4304        .unwrap();
4305    let project_b = client_b.build_remote_project(project_id, cx_b).await;
4306
4307    let open_buffer = project_b.update(cx_b, |p, cx| p.open_buffer((worktree_id, "a.rs"), cx));
4308    let buffer_b = cx_b.executor().spawn(open_buffer).await.unwrap();
4309
4310    let fake_language_server = fake_language_servers.next().await.unwrap();
4311    fake_language_server.handle_request::<lsp::request::Formatting, _, _>(|_, _| async move {
4312        Ok(Some(vec![
4313            lsp::TextEdit {
4314                range: lsp::Range::new(lsp::Position::new(0, 4), lsp::Position::new(0, 4)),
4315                new_text: "h".to_string(),
4316            },
4317            lsp::TextEdit {
4318                range: lsp::Range::new(lsp::Position::new(0, 7), lsp::Position::new(0, 7)),
4319                new_text: "y".to_string(),
4320            },
4321        ]))
4322    });
4323
4324    project_b
4325        .update(cx_b, |project, cx| {
4326            project.format(
4327                HashSet::from_iter([buffer_b.clone()]),
4328                true,
4329                FormatTrigger::Save,
4330                cx,
4331            )
4332        })
4333        .await
4334        .unwrap();
4335
4336    // The edits from the LSP are applied, and a final newline is added.
4337    assert_eq!(
4338        buffer_b.read_with(cx_b, |buffer, _| buffer.text()),
4339        "let honey = \"two\"\n"
4340    );
4341
4342    // Ensure buffer can be formatted using an external command. Notice how the
4343    // host's configuration is honored as opposed to using the guest's settings.
4344    cx_a.update(|cx| {
4345        cx.update_global(|store: &mut SettingsStore, cx| {
4346            store.update_user_settings::<AllLanguageSettings>(cx, |file| {
4347                file.defaults.formatter = Some(Formatter::External {
4348                    command: "awk".into(),
4349                    arguments: vec!["{sub(/two/,\"{buffer_path}\")}1".to_string()].into(),
4350                });
4351            });
4352        });
4353    });
4354    project_b
4355        .update(cx_b, |project, cx| {
4356            project.format(
4357                HashSet::from_iter([buffer_b.clone()]),
4358                true,
4359                FormatTrigger::Save,
4360                cx,
4361            )
4362        })
4363        .await
4364        .unwrap();
4365    assert_eq!(
4366        buffer_b.read_with(cx_b, |buffer, _| buffer.text()),
4367        format!("let honey = \"{}/a.rs\"\n", directory.to_str().unwrap())
4368    );
4369}
4370
4371#[gpui::test(iterations = 10)]
4372async fn test_prettier_formatting_buffer(
4373    executor: BackgroundExecutor,
4374    cx_a: &mut TestAppContext,
4375    cx_b: &mut TestAppContext,
4376) {
4377    let mut server = TestServer::start(executor.clone()).await;
4378    let client_a = server.create_client(cx_a, "user_a").await;
4379    let client_b = server.create_client(cx_b, "user_b").await;
4380    server
4381        .create_room(&mut [(&client_a, cx_a), (&client_b, cx_b)])
4382        .await;
4383    let active_call_a = cx_a.read(ActiveCall::global);
4384
4385    let test_plugin = "test_plugin";
4386
4387    client_a.language_registry().add(Arc::new(Language::new(
4388        LanguageConfig {
4389            name: "Rust".into(),
4390            matcher: LanguageMatcher {
4391                path_suffixes: vec!["rs".to_string()],
4392                ..Default::default()
4393            },
4394            prettier_parser_name: Some("test_parser".to_string()),
4395            ..Default::default()
4396        },
4397        Some(tree_sitter_rust::language()),
4398    )));
4399    let mut fake_language_servers = client_a.language_registry().register_fake_lsp_adapter(
4400        "Rust",
4401        FakeLspAdapter {
4402            prettier_plugins: vec![test_plugin],
4403            ..Default::default()
4404        },
4405    );
4406
4407    // Here we insert a fake tree with a directory that exists on disk. This is needed
4408    // because later we'll invoke a command, which requires passing a working directory
4409    // that points to a valid location on disk.
4410    let directory = env::current_dir().unwrap();
4411    let buffer_text = "let one = \"two\"";
4412    client_a
4413        .fs()
4414        .insert_tree(&directory, json!({ "a.rs": buffer_text }))
4415        .await;
4416    let (project_a, worktree_id) = client_a.build_local_project(&directory, cx_a).await;
4417    let prettier_format_suffix = project::TEST_PRETTIER_FORMAT_SUFFIX;
4418    let open_buffer = project_a.update(cx_a, |p, cx| p.open_buffer((worktree_id, "a.rs"), cx));
4419    let buffer_a = cx_a.executor().spawn(open_buffer).await.unwrap();
4420
4421    let project_id = active_call_a
4422        .update(cx_a, |call, cx| call.share_project(project_a.clone(), cx))
4423        .await
4424        .unwrap();
4425    let project_b = client_b.build_remote_project(project_id, cx_b).await;
4426    let open_buffer = project_b.update(cx_b, |p, cx| p.open_buffer((worktree_id, "a.rs"), cx));
4427    let buffer_b = cx_b.executor().spawn(open_buffer).await.unwrap();
4428
4429    cx_a.update(|cx| {
4430        cx.update_global(|store: &mut SettingsStore, cx| {
4431            store.update_user_settings::<AllLanguageSettings>(cx, |file| {
4432                file.defaults.formatter = Some(Formatter::Auto);
4433            });
4434        });
4435    });
4436    cx_b.update(|cx| {
4437        cx.update_global(|store: &mut SettingsStore, cx| {
4438            store.update_user_settings::<AllLanguageSettings>(cx, |file| {
4439                file.defaults.formatter = Some(Formatter::LanguageServer);
4440            });
4441        });
4442    });
4443    let fake_language_server = fake_language_servers.next().await.unwrap();
4444    fake_language_server.handle_request::<lsp::request::Formatting, _, _>(|_, _| async move {
4445        panic!(
4446            "Unexpected: prettier should be preferred since it's enabled and language supports it"
4447        )
4448    });
4449
4450    project_b
4451        .update(cx_b, |project, cx| {
4452            project.format(
4453                HashSet::from_iter([buffer_b.clone()]),
4454                true,
4455                FormatTrigger::Save,
4456                cx,
4457            )
4458        })
4459        .await
4460        .unwrap();
4461
4462    executor.run_until_parked();
4463    assert_eq!(
4464        buffer_b.read_with(cx_b, |buffer, _| buffer.text()),
4465        buffer_text.to_string() + "\n" + prettier_format_suffix,
4466        "Prettier formatting was not applied to client buffer after client's request"
4467    );
4468
4469    project_a
4470        .update(cx_a, |project, cx| {
4471            project.format(
4472                HashSet::from_iter([buffer_a.clone()]),
4473                true,
4474                FormatTrigger::Manual,
4475                cx,
4476            )
4477        })
4478        .await
4479        .unwrap();
4480
4481    executor.run_until_parked();
4482    assert_eq!(
4483        buffer_b.read_with(cx_b, |buffer, _| buffer.text()),
4484        buffer_text.to_string() + "\n" + prettier_format_suffix + "\n" + prettier_format_suffix,
4485        "Prettier formatting was not applied to client buffer after host's request"
4486    );
4487}
4488
4489#[gpui::test(iterations = 10)]
4490async fn test_definition(
4491    executor: BackgroundExecutor,
4492    cx_a: &mut TestAppContext,
4493    cx_b: &mut TestAppContext,
4494) {
4495    let mut server = TestServer::start(executor.clone()).await;
4496    let client_a = server.create_client(cx_a, "user_a").await;
4497    let client_b = server.create_client(cx_b, "user_b").await;
4498    server
4499        .create_room(&mut [(&client_a, cx_a), (&client_b, cx_b)])
4500        .await;
4501    let active_call_a = cx_a.read(ActiveCall::global);
4502
4503    let mut fake_language_servers = client_a
4504        .language_registry()
4505        .register_fake_lsp_adapter("Rust", Default::default());
4506    client_a.language_registry().add(rust_lang());
4507
4508    client_a
4509        .fs()
4510        .insert_tree(
4511            "/root",
4512            json!({
4513                "dir-1": {
4514                    "a.rs": "const ONE: usize = b::TWO + b::THREE;",
4515                },
4516                "dir-2": {
4517                    "b.rs": "const TWO: c::T2 = 2;\nconst THREE: usize = 3;",
4518                    "c.rs": "type T2 = usize;",
4519                }
4520            }),
4521        )
4522        .await;
4523    let (project_a, worktree_id) = client_a.build_local_project("/root/dir-1", cx_a).await;
4524    let project_id = active_call_a
4525        .update(cx_a, |call, cx| call.share_project(project_a.clone(), cx))
4526        .await
4527        .unwrap();
4528    let project_b = client_b.build_remote_project(project_id, cx_b).await;
4529
4530    // Open the file on client B.
4531    let open_buffer = project_b.update(cx_b, |p, cx| p.open_buffer((worktree_id, "a.rs"), cx));
4532    let buffer_b = cx_b.executor().spawn(open_buffer).await.unwrap();
4533
4534    // Request the definition of a symbol as the guest.
4535    let fake_language_server = fake_language_servers.next().await.unwrap();
4536    fake_language_server.handle_request::<lsp::request::GotoDefinition, _, _>(|_, _| async move {
4537        Ok(Some(lsp::GotoDefinitionResponse::Scalar(
4538            lsp::Location::new(
4539                lsp::Url::from_file_path("/root/dir-2/b.rs").unwrap(),
4540                lsp::Range::new(lsp::Position::new(0, 6), lsp::Position::new(0, 9)),
4541            ),
4542        )))
4543    });
4544
4545    let definitions_1 = project_b
4546        .update(cx_b, |p, cx| p.definition(&buffer_b, 23, cx))
4547        .await
4548        .unwrap();
4549    cx_b.read(|cx| {
4550        assert_eq!(definitions_1.len(), 1);
4551        assert_eq!(project_b.read(cx).worktrees().count(), 2);
4552        let target_buffer = definitions_1[0].target.buffer.read(cx);
4553        assert_eq!(
4554            target_buffer.text(),
4555            "const TWO: c::T2 = 2;\nconst THREE: usize = 3;"
4556        );
4557        assert_eq!(
4558            definitions_1[0].target.range.to_point(target_buffer),
4559            Point::new(0, 6)..Point::new(0, 9)
4560        );
4561    });
4562
4563    // Try getting more definitions for the same buffer, ensuring the buffer gets reused from
4564    // the previous call to `definition`.
4565    fake_language_server.handle_request::<lsp::request::GotoDefinition, _, _>(|_, _| async move {
4566        Ok(Some(lsp::GotoDefinitionResponse::Scalar(
4567            lsp::Location::new(
4568                lsp::Url::from_file_path("/root/dir-2/b.rs").unwrap(),
4569                lsp::Range::new(lsp::Position::new(1, 6), lsp::Position::new(1, 11)),
4570            ),
4571        )))
4572    });
4573
4574    let definitions_2 = project_b
4575        .update(cx_b, |p, cx| p.definition(&buffer_b, 33, cx))
4576        .await
4577        .unwrap();
4578    cx_b.read(|cx| {
4579        assert_eq!(definitions_2.len(), 1);
4580        assert_eq!(project_b.read(cx).worktrees().count(), 2);
4581        let target_buffer = definitions_2[0].target.buffer.read(cx);
4582        assert_eq!(
4583            target_buffer.text(),
4584            "const TWO: c::T2 = 2;\nconst THREE: usize = 3;"
4585        );
4586        assert_eq!(
4587            definitions_2[0].target.range.to_point(target_buffer),
4588            Point::new(1, 6)..Point::new(1, 11)
4589        );
4590    });
4591    assert_eq!(
4592        definitions_1[0].target.buffer,
4593        definitions_2[0].target.buffer
4594    );
4595
4596    fake_language_server.handle_request::<lsp::request::GotoTypeDefinition, _, _>(
4597        |req, _| async move {
4598            assert_eq!(
4599                req.text_document_position_params.position,
4600                lsp::Position::new(0, 7)
4601            );
4602            Ok(Some(lsp::GotoDefinitionResponse::Scalar(
4603                lsp::Location::new(
4604                    lsp::Url::from_file_path("/root/dir-2/c.rs").unwrap(),
4605                    lsp::Range::new(lsp::Position::new(0, 5), lsp::Position::new(0, 7)),
4606                ),
4607            )))
4608        },
4609    );
4610
4611    let type_definitions = project_b
4612        .update(cx_b, |p, cx| p.type_definition(&buffer_b, 7, cx))
4613        .await
4614        .unwrap();
4615    cx_b.read(|cx| {
4616        assert_eq!(type_definitions.len(), 1);
4617        let target_buffer = type_definitions[0].target.buffer.read(cx);
4618        assert_eq!(target_buffer.text(), "type T2 = usize;");
4619        assert_eq!(
4620            type_definitions[0].target.range.to_point(target_buffer),
4621            Point::new(0, 5)..Point::new(0, 7)
4622        );
4623    });
4624}
4625
4626#[gpui::test(iterations = 10)]
4627async fn test_references(
4628    executor: BackgroundExecutor,
4629    cx_a: &mut TestAppContext,
4630    cx_b: &mut TestAppContext,
4631) {
4632    let mut server = TestServer::start(executor.clone()).await;
4633    let client_a = server.create_client(cx_a, "user_a").await;
4634    let client_b = server.create_client(cx_b, "user_b").await;
4635    server
4636        .create_room(&mut [(&client_a, cx_a), (&client_b, cx_b)])
4637        .await;
4638    let active_call_a = cx_a.read(ActiveCall::global);
4639
4640    client_a.language_registry().add(rust_lang());
4641    let mut fake_language_servers = client_a
4642        .language_registry()
4643        .register_fake_lsp_adapter("Rust", Default::default());
4644
4645    client_a
4646        .fs()
4647        .insert_tree(
4648            "/root",
4649            json!({
4650                "dir-1": {
4651                    "one.rs": "const ONE: usize = 1;",
4652                    "two.rs": "const TWO: usize = one::ONE + one::ONE;",
4653                },
4654                "dir-2": {
4655                    "three.rs": "const THREE: usize = two::TWO + one::ONE;",
4656                }
4657            }),
4658        )
4659        .await;
4660    let (project_a, worktree_id) = client_a.build_local_project("/root/dir-1", cx_a).await;
4661    let project_id = active_call_a
4662        .update(cx_a, |call, cx| call.share_project(project_a.clone(), cx))
4663        .await
4664        .unwrap();
4665    let project_b = client_b.build_remote_project(project_id, cx_b).await;
4666
4667    // Open the file on client B.
4668    let open_buffer = project_b.update(cx_b, |p, cx| p.open_buffer((worktree_id, "one.rs"), cx));
4669    let buffer_b = cx_b.executor().spawn(open_buffer).await.unwrap();
4670
4671    // Request references to a symbol as the guest.
4672    let fake_language_server = fake_language_servers.next().await.unwrap();
4673    fake_language_server.handle_request::<lsp::request::References, _, _>(|params, _| async move {
4674        assert_eq!(
4675            params.text_document_position.text_document.uri.as_str(),
4676            "file:///root/dir-1/one.rs"
4677        );
4678        Ok(Some(vec![
4679            lsp::Location {
4680                uri: lsp::Url::from_file_path("/root/dir-1/two.rs").unwrap(),
4681                range: lsp::Range::new(lsp::Position::new(0, 24), lsp::Position::new(0, 27)),
4682            },
4683            lsp::Location {
4684                uri: lsp::Url::from_file_path("/root/dir-1/two.rs").unwrap(),
4685                range: lsp::Range::new(lsp::Position::new(0, 35), lsp::Position::new(0, 38)),
4686            },
4687            lsp::Location {
4688                uri: lsp::Url::from_file_path("/root/dir-2/three.rs").unwrap(),
4689                range: lsp::Range::new(lsp::Position::new(0, 37), lsp::Position::new(0, 40)),
4690            },
4691        ]))
4692    });
4693
4694    let references = project_b
4695        .update(cx_b, |p, cx| p.references(&buffer_b, 7, cx))
4696        .await
4697        .unwrap();
4698    cx_b.read(|cx| {
4699        assert_eq!(references.len(), 3);
4700        assert_eq!(project_b.read(cx).worktrees().count(), 2);
4701
4702        let two_buffer = references[0].buffer.read(cx);
4703        let three_buffer = references[2].buffer.read(cx);
4704        assert_eq!(
4705            two_buffer.file().unwrap().path().as_ref(),
4706            Path::new("two.rs")
4707        );
4708        assert_eq!(references[1].buffer, references[0].buffer);
4709        assert_eq!(
4710            three_buffer.file().unwrap().full_path(cx),
4711            Path::new("/root/dir-2/three.rs")
4712        );
4713
4714        assert_eq!(references[0].range.to_offset(two_buffer), 24..27);
4715        assert_eq!(references[1].range.to_offset(two_buffer), 35..38);
4716        assert_eq!(references[2].range.to_offset(three_buffer), 37..40);
4717    });
4718}
4719
4720#[gpui::test(iterations = 10)]
4721async fn test_project_search(
4722    executor: BackgroundExecutor,
4723    cx_a: &mut TestAppContext,
4724    cx_b: &mut TestAppContext,
4725) {
4726    let mut server = TestServer::start(executor.clone()).await;
4727    let client_a = server.create_client(cx_a, "user_a").await;
4728    let client_b = server.create_client(cx_b, "user_b").await;
4729    server
4730        .create_room(&mut [(&client_a, cx_a), (&client_b, cx_b)])
4731        .await;
4732    let active_call_a = cx_a.read(ActiveCall::global);
4733
4734    client_a
4735        .fs()
4736        .insert_tree(
4737            "/root",
4738            json!({
4739                "dir-1": {
4740                    "a": "hello world",
4741                    "b": "goodnight moon",
4742                    "c": "a world of goo",
4743                    "d": "world champion of clown world",
4744                },
4745                "dir-2": {
4746                    "e": "disney world is fun",
4747                }
4748            }),
4749        )
4750        .await;
4751    let (project_a, _) = client_a.build_local_project("/root/dir-1", cx_a).await;
4752    let (worktree_2, _) = project_a
4753        .update(cx_a, |p, cx| {
4754            p.find_or_create_local_worktree("/root/dir-2", true, cx)
4755        })
4756        .await
4757        .unwrap();
4758    worktree_2
4759        .read_with(cx_a, |tree, _| tree.as_local().unwrap().scan_complete())
4760        .await;
4761    let project_id = active_call_a
4762        .update(cx_a, |call, cx| call.share_project(project_a.clone(), cx))
4763        .await
4764        .unwrap();
4765
4766    let project_b = client_b.build_remote_project(project_id, cx_b).await;
4767
4768    // Perform a search as the guest.
4769    let mut results = HashMap::default();
4770    let mut search_rx = project_b.update(cx_b, |project, cx| {
4771        project.search(
4772            SearchQuery::text("world", false, false, false, Vec::new(), Vec::new()).unwrap(),
4773            cx,
4774        )
4775    });
4776    while let Some(result) = search_rx.next().await {
4777        match result {
4778            SearchResult::Buffer { buffer, ranges } => {
4779                results.entry(buffer).or_insert(ranges);
4780            }
4781            SearchResult::LimitReached => {
4782                panic!("Unexpectedly reached search limit in tests. If you do want to assert limit-reached, change this panic call.")
4783            }
4784        };
4785    }
4786
4787    let mut ranges_by_path = results
4788        .into_iter()
4789        .map(|(buffer, ranges)| {
4790            buffer.read_with(cx_b, |buffer, cx| {
4791                let path = buffer.file().unwrap().full_path(cx);
4792                let offset_ranges = ranges
4793                    .into_iter()
4794                    .map(|range| range.to_offset(buffer))
4795                    .collect::<Vec<_>>();
4796                (path, offset_ranges)
4797            })
4798        })
4799        .collect::<Vec<_>>();
4800    ranges_by_path.sort_by_key(|(path, _)| path.clone());
4801
4802    assert_eq!(
4803        ranges_by_path,
4804        &[
4805            (PathBuf::from("dir-1/a"), vec![6..11]),
4806            (PathBuf::from("dir-1/c"), vec![2..7]),
4807            (PathBuf::from("dir-1/d"), vec![0..5, 24..29]),
4808            (PathBuf::from("dir-2/e"), vec![7..12]),
4809        ]
4810    );
4811}
4812
4813#[gpui::test(iterations = 10)]
4814async fn test_document_highlights(
4815    executor: BackgroundExecutor,
4816    cx_a: &mut TestAppContext,
4817    cx_b: &mut TestAppContext,
4818) {
4819    let mut server = TestServer::start(executor.clone()).await;
4820    let client_a = server.create_client(cx_a, "user_a").await;
4821    let client_b = server.create_client(cx_b, "user_b").await;
4822    server
4823        .create_room(&mut [(&client_a, cx_a), (&client_b, cx_b)])
4824        .await;
4825    let active_call_a = cx_a.read(ActiveCall::global);
4826
4827    client_a
4828        .fs()
4829        .insert_tree(
4830            "/root-1",
4831            json!({
4832                "main.rs": "fn double(number: i32) -> i32 { number + number }",
4833            }),
4834        )
4835        .await;
4836
4837    let mut fake_language_servers = client_a
4838        .language_registry()
4839        .register_fake_lsp_adapter("Rust", Default::default());
4840    client_a.language_registry().add(rust_lang());
4841
4842    let (project_a, worktree_id) = client_a.build_local_project("/root-1", cx_a).await;
4843    let project_id = active_call_a
4844        .update(cx_a, |call, cx| call.share_project(project_a.clone(), cx))
4845        .await
4846        .unwrap();
4847    let project_b = client_b.build_remote_project(project_id, cx_b).await;
4848
4849    // Open the file on client B.
4850    let open_b = project_b.update(cx_b, |p, cx| p.open_buffer((worktree_id, "main.rs"), cx));
4851    let buffer_b = cx_b.executor().spawn(open_b).await.unwrap();
4852
4853    // Request document highlights as the guest.
4854    let fake_language_server = fake_language_servers.next().await.unwrap();
4855    fake_language_server.handle_request::<lsp::request::DocumentHighlightRequest, _, _>(
4856        |params, _| async move {
4857            assert_eq!(
4858                params
4859                    .text_document_position_params
4860                    .text_document
4861                    .uri
4862                    .as_str(),
4863                "file:///root-1/main.rs"
4864            );
4865            assert_eq!(
4866                params.text_document_position_params.position,
4867                lsp::Position::new(0, 34)
4868            );
4869            Ok(Some(vec![
4870                lsp::DocumentHighlight {
4871                    kind: Some(lsp::DocumentHighlightKind::WRITE),
4872                    range: lsp::Range::new(lsp::Position::new(0, 10), lsp::Position::new(0, 16)),
4873                },
4874                lsp::DocumentHighlight {
4875                    kind: Some(lsp::DocumentHighlightKind::READ),
4876                    range: lsp::Range::new(lsp::Position::new(0, 32), lsp::Position::new(0, 38)),
4877                },
4878                lsp::DocumentHighlight {
4879                    kind: Some(lsp::DocumentHighlightKind::READ),
4880                    range: lsp::Range::new(lsp::Position::new(0, 41), lsp::Position::new(0, 47)),
4881                },
4882            ]))
4883        },
4884    );
4885
4886    let highlights = project_b
4887        .update(cx_b, |p, cx| p.document_highlights(&buffer_b, 34, cx))
4888        .await
4889        .unwrap();
4890
4891    buffer_b.read_with(cx_b, |buffer, _| {
4892        let snapshot = buffer.snapshot();
4893
4894        let highlights = highlights
4895            .into_iter()
4896            .map(|highlight| (highlight.kind, highlight.range.to_offset(&snapshot)))
4897            .collect::<Vec<_>>();
4898        assert_eq!(
4899            highlights,
4900            &[
4901                (lsp::DocumentHighlightKind::WRITE, 10..16),
4902                (lsp::DocumentHighlightKind::READ, 32..38),
4903                (lsp::DocumentHighlightKind::READ, 41..47)
4904            ]
4905        )
4906    });
4907}
4908
4909#[gpui::test(iterations = 10)]
4910async fn test_lsp_hover(
4911    executor: BackgroundExecutor,
4912    cx_a: &mut TestAppContext,
4913    cx_b: &mut TestAppContext,
4914) {
4915    let mut server = TestServer::start(executor.clone()).await;
4916    let client_a = server.create_client(cx_a, "user_a").await;
4917    let client_b = server.create_client(cx_b, "user_b").await;
4918    server
4919        .create_room(&mut [(&client_a, cx_a), (&client_b, cx_b)])
4920        .await;
4921    let active_call_a = cx_a.read(ActiveCall::global);
4922
4923    client_a
4924        .fs()
4925        .insert_tree(
4926            "/root-1",
4927            json!({
4928                "main.rs": "use std::collections::HashMap;",
4929            }),
4930        )
4931        .await;
4932
4933    client_a.language_registry().add(rust_lang());
4934    let mut fake_language_servers = client_a
4935        .language_registry()
4936        .register_fake_lsp_adapter("Rust", Default::default());
4937
4938    let (project_a, worktree_id) = client_a.build_local_project("/root-1", cx_a).await;
4939    let project_id = active_call_a
4940        .update(cx_a, |call, cx| call.share_project(project_a.clone(), cx))
4941        .await
4942        .unwrap();
4943    let project_b = client_b.build_remote_project(project_id, cx_b).await;
4944
4945    // Open the file as the guest
4946    let open_buffer = project_b.update(cx_b, |p, cx| p.open_buffer((worktree_id, "main.rs"), cx));
4947    let buffer_b = cx_b.executor().spawn(open_buffer).await.unwrap();
4948
4949    // Request hover information as the guest.
4950    let fake_language_server = fake_language_servers.next().await.unwrap();
4951    fake_language_server.handle_request::<lsp::request::HoverRequest, _, _>(
4952        |params, _| async move {
4953            assert_eq!(
4954                params
4955                    .text_document_position_params
4956                    .text_document
4957                    .uri
4958                    .as_str(),
4959                "file:///root-1/main.rs"
4960            );
4961            assert_eq!(
4962                params.text_document_position_params.position,
4963                lsp::Position::new(0, 22)
4964            );
4965            Ok(Some(lsp::Hover {
4966                contents: lsp::HoverContents::Array(vec![
4967                    lsp::MarkedString::String("Test hover content.".to_string()),
4968                    lsp::MarkedString::LanguageString(lsp::LanguageString {
4969                        language: "Rust".to_string(),
4970                        value: "let foo = 42;".to_string(),
4971                    }),
4972                ]),
4973                range: Some(lsp::Range::new(
4974                    lsp::Position::new(0, 22),
4975                    lsp::Position::new(0, 29),
4976                )),
4977            }))
4978        },
4979    );
4980
4981    let hovers = project_b
4982        .update(cx_b, |p, cx| p.hover(&buffer_b, 22, cx))
4983        .await
4984        .unwrap();
4985    assert_eq!(
4986        hovers.len(),
4987        1,
4988        "Expected exactly one hover but got: {hovers:?}"
4989    );
4990    let hover_info = hovers.into_iter().next().unwrap();
4991
4992    buffer_b.read_with(cx_b, |buffer, _| {
4993        let snapshot = buffer.snapshot();
4994        assert_eq!(hover_info.range.unwrap().to_offset(&snapshot), 22..29);
4995        assert_eq!(
4996            hover_info.contents,
4997            vec![
4998                project::HoverBlock {
4999                    text: "Test hover content.".to_string(),
5000                    kind: HoverBlockKind::Markdown,
5001                },
5002                project::HoverBlock {
5003                    text: "let foo = 42;".to_string(),
5004                    kind: HoverBlockKind::Code {
5005                        language: "Rust".to_string()
5006                    },
5007                }
5008            ]
5009        );
5010    });
5011}
5012
5013#[gpui::test(iterations = 10)]
5014async fn test_project_symbols(
5015    executor: BackgroundExecutor,
5016    cx_a: &mut TestAppContext,
5017    cx_b: &mut TestAppContext,
5018) {
5019    let mut server = TestServer::start(executor.clone()).await;
5020    let client_a = server.create_client(cx_a, "user_a").await;
5021    let client_b = server.create_client(cx_b, "user_b").await;
5022    server
5023        .create_room(&mut [(&client_a, cx_a), (&client_b, cx_b)])
5024        .await;
5025    let active_call_a = cx_a.read(ActiveCall::global);
5026
5027    client_a.language_registry().add(rust_lang());
5028    let mut fake_language_servers = client_a
5029        .language_registry()
5030        .register_fake_lsp_adapter("Rust", Default::default());
5031
5032    client_a
5033        .fs()
5034        .insert_tree(
5035            "/code",
5036            json!({
5037                "crate-1": {
5038                    "one.rs": "const ONE: usize = 1;",
5039                },
5040                "crate-2": {
5041                    "two.rs": "const TWO: usize = 2; const THREE: usize = 3;",
5042                },
5043                "private": {
5044                    "passwords.txt": "the-password",
5045                }
5046            }),
5047        )
5048        .await;
5049    let (project_a, worktree_id) = client_a.build_local_project("/code/crate-1", cx_a).await;
5050    let project_id = active_call_a
5051        .update(cx_a, |call, cx| call.share_project(project_a.clone(), cx))
5052        .await
5053        .unwrap();
5054    let project_b = client_b.build_remote_project(project_id, cx_b).await;
5055
5056    // Cause the language server to start.
5057    let open_buffer_task =
5058        project_b.update(cx_b, |p, cx| p.open_buffer((worktree_id, "one.rs"), cx));
5059    let _buffer = cx_b.executor().spawn(open_buffer_task).await.unwrap();
5060
5061    let fake_language_server = fake_language_servers.next().await.unwrap();
5062    fake_language_server.handle_request::<lsp::WorkspaceSymbolRequest, _, _>(|_, _| async move {
5063        Ok(Some(lsp::WorkspaceSymbolResponse::Flat(vec![
5064            #[allow(deprecated)]
5065            lsp::SymbolInformation {
5066                name: "TWO".into(),
5067                location: lsp::Location {
5068                    uri: lsp::Url::from_file_path("/code/crate-2/two.rs").unwrap(),
5069                    range: lsp::Range::new(lsp::Position::new(0, 6), lsp::Position::new(0, 9)),
5070                },
5071                kind: lsp::SymbolKind::CONSTANT,
5072                tags: None,
5073                container_name: None,
5074                deprecated: None,
5075            },
5076        ])))
5077    });
5078
5079    // Request the definition of a symbol as the guest.
5080    let symbols = project_b
5081        .update(cx_b, |p, cx| p.symbols("two", cx))
5082        .await
5083        .unwrap();
5084    assert_eq!(symbols.len(), 1);
5085    assert_eq!(symbols[0].name, "TWO");
5086
5087    // Open one of the returned symbols.
5088    let buffer_b_2 = project_b
5089        .update(cx_b, |project, cx| {
5090            project.open_buffer_for_symbol(&symbols[0], cx)
5091        })
5092        .await
5093        .unwrap();
5094
5095    buffer_b_2.read_with(cx_b, |buffer, cx| {
5096        assert_eq!(
5097            buffer.file().unwrap().full_path(cx),
5098            Path::new("/code/crate-2/two.rs")
5099        );
5100    });
5101
5102    // Attempt to craft a symbol and violate host's privacy by opening an arbitrary file.
5103    let mut fake_symbol = symbols[0].clone();
5104    fake_symbol.path.path = Path::new("/code/secrets").into();
5105    let error = project_b
5106        .update(cx_b, |project, cx| {
5107            project.open_buffer_for_symbol(&fake_symbol, cx)
5108        })
5109        .await
5110        .unwrap_err();
5111    assert!(error.to_string().contains("invalid symbol signature"));
5112}
5113
5114#[gpui::test(iterations = 10)]
5115async fn test_open_buffer_while_getting_definition_pointing_to_it(
5116    executor: BackgroundExecutor,
5117    cx_a: &mut TestAppContext,
5118    cx_b: &mut TestAppContext,
5119    mut rng: StdRng,
5120) {
5121    let mut server = TestServer::start(executor.clone()).await;
5122    let client_a = server.create_client(cx_a, "user_a").await;
5123    let client_b = server.create_client(cx_b, "user_b").await;
5124    server
5125        .create_room(&mut [(&client_a, cx_a), (&client_b, cx_b)])
5126        .await;
5127    let active_call_a = cx_a.read(ActiveCall::global);
5128
5129    client_a.language_registry().add(rust_lang());
5130    let mut fake_language_servers = client_a
5131        .language_registry()
5132        .register_fake_lsp_adapter("Rust", Default::default());
5133
5134    client_a
5135        .fs()
5136        .insert_tree(
5137            "/root",
5138            json!({
5139                "a.rs": "const ONE: usize = b::TWO;",
5140                "b.rs": "const TWO: usize = 2",
5141            }),
5142        )
5143        .await;
5144    let (project_a, worktree_id) = client_a.build_local_project("/root", cx_a).await;
5145    let project_id = active_call_a
5146        .update(cx_a, |call, cx| call.share_project(project_a.clone(), cx))
5147        .await
5148        .unwrap();
5149    let project_b = client_b.build_remote_project(project_id, cx_b).await;
5150
5151    let open_buffer_task = project_b.update(cx_b, |p, cx| p.open_buffer((worktree_id, "a.rs"), cx));
5152    let buffer_b1 = cx_b.executor().spawn(open_buffer_task).await.unwrap();
5153
5154    let fake_language_server = fake_language_servers.next().await.unwrap();
5155    fake_language_server.handle_request::<lsp::request::GotoDefinition, _, _>(|_, _| async move {
5156        Ok(Some(lsp::GotoDefinitionResponse::Scalar(
5157            lsp::Location::new(
5158                lsp::Url::from_file_path("/root/b.rs").unwrap(),
5159                lsp::Range::new(lsp::Position::new(0, 6), lsp::Position::new(0, 9)),
5160            ),
5161        )))
5162    });
5163
5164    let definitions;
5165    let buffer_b2;
5166    if rng.gen() {
5167        definitions = project_b.update(cx_b, |p, cx| p.definition(&buffer_b1, 23, cx));
5168        buffer_b2 = project_b.update(cx_b, |p, cx| p.open_buffer((worktree_id, "b.rs"), cx));
5169    } else {
5170        buffer_b2 = project_b.update(cx_b, |p, cx| p.open_buffer((worktree_id, "b.rs"), cx));
5171        definitions = project_b.update(cx_b, |p, cx| p.definition(&buffer_b1, 23, cx));
5172    }
5173
5174    let buffer_b2 = buffer_b2.await.unwrap();
5175    let definitions = definitions.await.unwrap();
5176    assert_eq!(definitions.len(), 1);
5177    assert_eq!(definitions[0].target.buffer, buffer_b2);
5178}
5179
5180#[gpui::test(iterations = 10)]
5181async fn test_contacts(
5182    executor: BackgroundExecutor,
5183    cx_a: &mut TestAppContext,
5184    cx_b: &mut TestAppContext,
5185    cx_c: &mut TestAppContext,
5186    cx_d: &mut TestAppContext,
5187) {
5188    let mut server = TestServer::start(executor.clone()).await;
5189    let client_a = server.create_client(cx_a, "user_a").await;
5190    let client_b = server.create_client(cx_b, "user_b").await;
5191    let client_c = server.create_client(cx_c, "user_c").await;
5192    let client_d = server.create_client(cx_d, "user_d").await;
5193    server
5194        .make_contacts(&mut [(&client_a, cx_a), (&client_b, cx_b), (&client_c, cx_c)])
5195        .await;
5196    let active_call_a = cx_a.read(ActiveCall::global);
5197    let active_call_b = cx_b.read(ActiveCall::global);
5198    let active_call_c = cx_c.read(ActiveCall::global);
5199    let _active_call_d = cx_d.read(ActiveCall::global);
5200
5201    executor.run_until_parked();
5202    assert_eq!(
5203        contacts(&client_a, cx_a),
5204        [
5205            ("user_b".to_string(), "online", "free"),
5206            ("user_c".to_string(), "online", "free")
5207        ]
5208    );
5209    assert_eq!(
5210        contacts(&client_b, cx_b),
5211        [
5212            ("user_a".to_string(), "online", "free"),
5213            ("user_c".to_string(), "online", "free")
5214        ]
5215    );
5216    assert_eq!(
5217        contacts(&client_c, cx_c),
5218        [
5219            ("user_a".to_string(), "online", "free"),
5220            ("user_b".to_string(), "online", "free")
5221        ]
5222    );
5223    assert_eq!(contacts(&client_d, cx_d), []);
5224
5225    server.disconnect_client(client_c.peer_id().unwrap());
5226    server.forbid_connections();
5227    executor.advance_clock(RECEIVE_TIMEOUT + RECONNECT_TIMEOUT);
5228    assert_eq!(
5229        contacts(&client_a, cx_a),
5230        [
5231            ("user_b".to_string(), "online", "free"),
5232            ("user_c".to_string(), "offline", "free")
5233        ]
5234    );
5235    assert_eq!(
5236        contacts(&client_b, cx_b),
5237        [
5238            ("user_a".to_string(), "online", "free"),
5239            ("user_c".to_string(), "offline", "free")
5240        ]
5241    );
5242    assert_eq!(contacts(&client_c, cx_c), []);
5243    assert_eq!(contacts(&client_d, cx_d), []);
5244
5245    server.allow_connections();
5246    client_c
5247        .authenticate_and_connect(false, &cx_c.to_async())
5248        .await
5249        .unwrap();
5250
5251    executor.run_until_parked();
5252    assert_eq!(
5253        contacts(&client_a, cx_a),
5254        [
5255            ("user_b".to_string(), "online", "free"),
5256            ("user_c".to_string(), "online", "free")
5257        ]
5258    );
5259    assert_eq!(
5260        contacts(&client_b, cx_b),
5261        [
5262            ("user_a".to_string(), "online", "free"),
5263            ("user_c".to_string(), "online", "free")
5264        ]
5265    );
5266    assert_eq!(
5267        contacts(&client_c, cx_c),
5268        [
5269            ("user_a".to_string(), "online", "free"),
5270            ("user_b".to_string(), "online", "free")
5271        ]
5272    );
5273    assert_eq!(contacts(&client_d, cx_d), []);
5274
5275    active_call_a
5276        .update(cx_a, |call, cx| {
5277            call.invite(client_b.user_id().unwrap(), None, cx)
5278        })
5279        .await
5280        .unwrap();
5281    executor.run_until_parked();
5282    assert_eq!(
5283        contacts(&client_a, cx_a),
5284        [
5285            ("user_b".to_string(), "online", "busy"),
5286            ("user_c".to_string(), "online", "free")
5287        ]
5288    );
5289    assert_eq!(
5290        contacts(&client_b, cx_b),
5291        [
5292            ("user_a".to_string(), "online", "busy"),
5293            ("user_c".to_string(), "online", "free")
5294        ]
5295    );
5296    assert_eq!(
5297        contacts(&client_c, cx_c),
5298        [
5299            ("user_a".to_string(), "online", "busy"),
5300            ("user_b".to_string(), "online", "busy")
5301        ]
5302    );
5303    assert_eq!(contacts(&client_d, cx_d), []);
5304
5305    // Client B and client D become contacts while client B is being called.
5306    server
5307        .make_contacts(&mut [(&client_b, cx_b), (&client_d, cx_d)])
5308        .await;
5309    executor.run_until_parked();
5310    assert_eq!(
5311        contacts(&client_a, cx_a),
5312        [
5313            ("user_b".to_string(), "online", "busy"),
5314            ("user_c".to_string(), "online", "free")
5315        ]
5316    );
5317    assert_eq!(
5318        contacts(&client_b, cx_b),
5319        [
5320            ("user_a".to_string(), "online", "busy"),
5321            ("user_c".to_string(), "online", "free"),
5322            ("user_d".to_string(), "online", "free"),
5323        ]
5324    );
5325    assert_eq!(
5326        contacts(&client_c, cx_c),
5327        [
5328            ("user_a".to_string(), "online", "busy"),
5329            ("user_b".to_string(), "online", "busy")
5330        ]
5331    );
5332    assert_eq!(
5333        contacts(&client_d, cx_d),
5334        [("user_b".to_string(), "online", "busy")]
5335    );
5336
5337    active_call_b.update(cx_b, |call, cx| call.decline_incoming(cx).unwrap());
5338    executor.run_until_parked();
5339    assert_eq!(
5340        contacts(&client_a, cx_a),
5341        [
5342            ("user_b".to_string(), "online", "free"),
5343            ("user_c".to_string(), "online", "free")
5344        ]
5345    );
5346    assert_eq!(
5347        contacts(&client_b, cx_b),
5348        [
5349            ("user_a".to_string(), "online", "free"),
5350            ("user_c".to_string(), "online", "free"),
5351            ("user_d".to_string(), "online", "free")
5352        ]
5353    );
5354    assert_eq!(
5355        contacts(&client_c, cx_c),
5356        [
5357            ("user_a".to_string(), "online", "free"),
5358            ("user_b".to_string(), "online", "free")
5359        ]
5360    );
5361    assert_eq!(
5362        contacts(&client_d, cx_d),
5363        [("user_b".to_string(), "online", "free")]
5364    );
5365
5366    active_call_c
5367        .update(cx_c, |call, cx| {
5368            call.invite(client_a.user_id().unwrap(), None, cx)
5369        })
5370        .await
5371        .unwrap();
5372    executor.run_until_parked();
5373    assert_eq!(
5374        contacts(&client_a, cx_a),
5375        [
5376            ("user_b".to_string(), "online", "free"),
5377            ("user_c".to_string(), "online", "busy")
5378        ]
5379    );
5380    assert_eq!(
5381        contacts(&client_b, cx_b),
5382        [
5383            ("user_a".to_string(), "online", "busy"),
5384            ("user_c".to_string(), "online", "busy"),
5385            ("user_d".to_string(), "online", "free")
5386        ]
5387    );
5388    assert_eq!(
5389        contacts(&client_c, cx_c),
5390        [
5391            ("user_a".to_string(), "online", "busy"),
5392            ("user_b".to_string(), "online", "free")
5393        ]
5394    );
5395    assert_eq!(
5396        contacts(&client_d, cx_d),
5397        [("user_b".to_string(), "online", "free")]
5398    );
5399
5400    active_call_a
5401        .update(cx_a, |call, cx| call.accept_incoming(cx))
5402        .await
5403        .unwrap();
5404    executor.run_until_parked();
5405    assert_eq!(
5406        contacts(&client_a, cx_a),
5407        [
5408            ("user_b".to_string(), "online", "free"),
5409            ("user_c".to_string(), "online", "busy")
5410        ]
5411    );
5412    assert_eq!(
5413        contacts(&client_b, cx_b),
5414        [
5415            ("user_a".to_string(), "online", "busy"),
5416            ("user_c".to_string(), "online", "busy"),
5417            ("user_d".to_string(), "online", "free")
5418        ]
5419    );
5420    assert_eq!(
5421        contacts(&client_c, cx_c),
5422        [
5423            ("user_a".to_string(), "online", "busy"),
5424            ("user_b".to_string(), "online", "free")
5425        ]
5426    );
5427    assert_eq!(
5428        contacts(&client_d, cx_d),
5429        [("user_b".to_string(), "online", "free")]
5430    );
5431
5432    active_call_a
5433        .update(cx_a, |call, cx| {
5434            call.invite(client_b.user_id().unwrap(), None, cx)
5435        })
5436        .await
5437        .unwrap();
5438    executor.run_until_parked();
5439    assert_eq!(
5440        contacts(&client_a, cx_a),
5441        [
5442            ("user_b".to_string(), "online", "busy"),
5443            ("user_c".to_string(), "online", "busy")
5444        ]
5445    );
5446    assert_eq!(
5447        contacts(&client_b, cx_b),
5448        [
5449            ("user_a".to_string(), "online", "busy"),
5450            ("user_c".to_string(), "online", "busy"),
5451            ("user_d".to_string(), "online", "free")
5452        ]
5453    );
5454    assert_eq!(
5455        contacts(&client_c, cx_c),
5456        [
5457            ("user_a".to_string(), "online", "busy"),
5458            ("user_b".to_string(), "online", "busy")
5459        ]
5460    );
5461    assert_eq!(
5462        contacts(&client_d, cx_d),
5463        [("user_b".to_string(), "online", "busy")]
5464    );
5465
5466    active_call_a
5467        .update(cx_a, |call, cx| call.hang_up(cx))
5468        .await
5469        .unwrap();
5470    executor.run_until_parked();
5471    assert_eq!(
5472        contacts(&client_a, cx_a),
5473        [
5474            ("user_b".to_string(), "online", "free"),
5475            ("user_c".to_string(), "online", "free")
5476        ]
5477    );
5478    assert_eq!(
5479        contacts(&client_b, cx_b),
5480        [
5481            ("user_a".to_string(), "online", "free"),
5482            ("user_c".to_string(), "online", "free"),
5483            ("user_d".to_string(), "online", "free")
5484        ]
5485    );
5486    assert_eq!(
5487        contacts(&client_c, cx_c),
5488        [
5489            ("user_a".to_string(), "online", "free"),
5490            ("user_b".to_string(), "online", "free")
5491        ]
5492    );
5493    assert_eq!(
5494        contacts(&client_d, cx_d),
5495        [("user_b".to_string(), "online", "free")]
5496    );
5497
5498    active_call_a
5499        .update(cx_a, |call, cx| {
5500            call.invite(client_b.user_id().unwrap(), None, cx)
5501        })
5502        .await
5503        .unwrap();
5504    executor.run_until_parked();
5505    assert_eq!(
5506        contacts(&client_a, cx_a),
5507        [
5508            ("user_b".to_string(), "online", "busy"),
5509            ("user_c".to_string(), "online", "free")
5510        ]
5511    );
5512    assert_eq!(
5513        contacts(&client_b, cx_b),
5514        [
5515            ("user_a".to_string(), "online", "busy"),
5516            ("user_c".to_string(), "online", "free"),
5517            ("user_d".to_string(), "online", "free")
5518        ]
5519    );
5520    assert_eq!(
5521        contacts(&client_c, cx_c),
5522        [
5523            ("user_a".to_string(), "online", "busy"),
5524            ("user_b".to_string(), "online", "busy")
5525        ]
5526    );
5527    assert_eq!(
5528        contacts(&client_d, cx_d),
5529        [("user_b".to_string(), "online", "busy")]
5530    );
5531
5532    server.forbid_connections();
5533    server.disconnect_client(client_a.peer_id().unwrap());
5534    executor.advance_clock(RECEIVE_TIMEOUT + RECONNECT_TIMEOUT);
5535    assert_eq!(contacts(&client_a, cx_a), []);
5536    assert_eq!(
5537        contacts(&client_b, cx_b),
5538        [
5539            ("user_a".to_string(), "offline", "free"),
5540            ("user_c".to_string(), "online", "free"),
5541            ("user_d".to_string(), "online", "free")
5542        ]
5543    );
5544    assert_eq!(
5545        contacts(&client_c, cx_c),
5546        [
5547            ("user_a".to_string(), "offline", "free"),
5548            ("user_b".to_string(), "online", "free")
5549        ]
5550    );
5551    assert_eq!(
5552        contacts(&client_d, cx_d),
5553        [("user_b".to_string(), "online", "free")]
5554    );
5555
5556    // Test removing a contact
5557    client_b
5558        .user_store()
5559        .update(cx_b, |store, cx| {
5560            store.remove_contact(client_c.user_id().unwrap(), cx)
5561        })
5562        .await
5563        .unwrap();
5564    executor.run_until_parked();
5565    assert_eq!(
5566        contacts(&client_b, cx_b),
5567        [
5568            ("user_a".to_string(), "offline", "free"),
5569            ("user_d".to_string(), "online", "free")
5570        ]
5571    );
5572    assert_eq!(
5573        contacts(&client_c, cx_c),
5574        [("user_a".to_string(), "offline", "free"),]
5575    );
5576
5577    fn contacts(
5578        client: &TestClient,
5579        cx: &TestAppContext,
5580    ) -> Vec<(String, &'static str, &'static str)> {
5581        client.user_store().read_with(cx, |store, _| {
5582            store
5583                .contacts()
5584                .iter()
5585                .map(|contact| {
5586                    (
5587                        contact.user.github_login.clone(),
5588                        if contact.online { "online" } else { "offline" },
5589                        if contact.busy { "busy" } else { "free" },
5590                    )
5591                })
5592                .collect()
5593        })
5594    }
5595}
5596
5597#[gpui::test(iterations = 10)]
5598async fn test_contact_requests(
5599    executor: BackgroundExecutor,
5600    cx_a: &mut TestAppContext,
5601    cx_a2: &mut TestAppContext,
5602    cx_b: &mut TestAppContext,
5603    cx_b2: &mut TestAppContext,
5604    cx_c: &mut TestAppContext,
5605    cx_c2: &mut TestAppContext,
5606) {
5607    // Connect to a server as 3 clients.
5608    let mut server = TestServer::start(executor.clone()).await;
5609    let client_a = server.create_client(cx_a, "user_a").await;
5610    let client_a2 = server.create_client(cx_a2, "user_a").await;
5611    let client_b = server.create_client(cx_b, "user_b").await;
5612    let client_b2 = server.create_client(cx_b2, "user_b").await;
5613    let client_c = server.create_client(cx_c, "user_c").await;
5614    let client_c2 = server.create_client(cx_c2, "user_c").await;
5615
5616    assert_eq!(client_a.user_id().unwrap(), client_a2.user_id().unwrap());
5617    assert_eq!(client_b.user_id().unwrap(), client_b2.user_id().unwrap());
5618    assert_eq!(client_c.user_id().unwrap(), client_c2.user_id().unwrap());
5619
5620    // User A and User C request that user B become their contact.
5621    client_a
5622        .user_store()
5623        .update(cx_a, |store, cx| {
5624            store.request_contact(client_b.user_id().unwrap(), cx)
5625        })
5626        .await
5627        .unwrap();
5628    client_c
5629        .user_store()
5630        .update(cx_c, |store, cx| {
5631            store.request_contact(client_b.user_id().unwrap(), cx)
5632        })
5633        .await
5634        .unwrap();
5635    executor.run_until_parked();
5636
5637    // All users see the pending request appear in all their clients.
5638    assert_eq!(
5639        client_a.summarize_contacts(cx_a).outgoing_requests,
5640        &["user_b"]
5641    );
5642    assert_eq!(
5643        client_a2.summarize_contacts(cx_a2).outgoing_requests,
5644        &["user_b"]
5645    );
5646    assert_eq!(
5647        client_b.summarize_contacts(cx_b).incoming_requests,
5648        &["user_a", "user_c"]
5649    );
5650    assert_eq!(
5651        client_b2.summarize_contacts(cx_b2).incoming_requests,
5652        &["user_a", "user_c"]
5653    );
5654    assert_eq!(
5655        client_c.summarize_contacts(cx_c).outgoing_requests,
5656        &["user_b"]
5657    );
5658    assert_eq!(
5659        client_c2.summarize_contacts(cx_c2).outgoing_requests,
5660        &["user_b"]
5661    );
5662
5663    // Contact requests are present upon connecting (tested here via disconnect/reconnect)
5664    disconnect_and_reconnect(&client_a, cx_a).await;
5665    disconnect_and_reconnect(&client_b, cx_b).await;
5666    disconnect_and_reconnect(&client_c, cx_c).await;
5667    executor.run_until_parked();
5668    assert_eq!(
5669        client_a.summarize_contacts(cx_a).outgoing_requests,
5670        &["user_b"]
5671    );
5672    assert_eq!(
5673        client_b.summarize_contacts(cx_b).incoming_requests,
5674        &["user_a", "user_c"]
5675    );
5676    assert_eq!(
5677        client_c.summarize_contacts(cx_c).outgoing_requests,
5678        &["user_b"]
5679    );
5680
5681    // User B accepts the request from user A.
5682    client_b
5683        .user_store()
5684        .update(cx_b, |store, cx| {
5685            store.respond_to_contact_request(client_a.user_id().unwrap(), true, cx)
5686        })
5687        .await
5688        .unwrap();
5689
5690    executor.run_until_parked();
5691
5692    // User B sees user A as their contact now in all client, and the incoming request from them is removed.
5693    let contacts_b = client_b.summarize_contacts(cx_b);
5694    assert_eq!(contacts_b.current, &["user_a"]);
5695    assert_eq!(contacts_b.incoming_requests, &["user_c"]);
5696    let contacts_b2 = client_b2.summarize_contacts(cx_b2);
5697    assert_eq!(contacts_b2.current, &["user_a"]);
5698    assert_eq!(contacts_b2.incoming_requests, &["user_c"]);
5699
5700    // User A sees user B as their contact now in all clients, and the outgoing request to them is removed.
5701    let contacts_a = client_a.summarize_contacts(cx_a);
5702    assert_eq!(contacts_a.current, &["user_b"]);
5703    assert!(contacts_a.outgoing_requests.is_empty());
5704    let contacts_a2 = client_a2.summarize_contacts(cx_a2);
5705    assert_eq!(contacts_a2.current, &["user_b"]);
5706    assert!(contacts_a2.outgoing_requests.is_empty());
5707
5708    // Contacts are present upon connecting (tested here via disconnect/reconnect)
5709    disconnect_and_reconnect(&client_a, cx_a).await;
5710    disconnect_and_reconnect(&client_b, cx_b).await;
5711    disconnect_and_reconnect(&client_c, cx_c).await;
5712    executor.run_until_parked();
5713    assert_eq!(client_a.summarize_contacts(cx_a).current, &["user_b"]);
5714    assert_eq!(client_b.summarize_contacts(cx_b).current, &["user_a"]);
5715    assert_eq!(
5716        client_b.summarize_contacts(cx_b).incoming_requests,
5717        &["user_c"]
5718    );
5719    assert!(client_c.summarize_contacts(cx_c).current.is_empty());
5720    assert_eq!(
5721        client_c.summarize_contacts(cx_c).outgoing_requests,
5722        &["user_b"]
5723    );
5724
5725    // User B rejects the request from user C.
5726    client_b
5727        .user_store()
5728        .update(cx_b, |store, cx| {
5729            store.respond_to_contact_request(client_c.user_id().unwrap(), false, cx)
5730        })
5731        .await
5732        .unwrap();
5733
5734    executor.run_until_parked();
5735
5736    // User B doesn't see user C as their contact, and the incoming request from them is removed.
5737    let contacts_b = client_b.summarize_contacts(cx_b);
5738    assert_eq!(contacts_b.current, &["user_a"]);
5739    assert!(contacts_b.incoming_requests.is_empty());
5740    let contacts_b2 = client_b2.summarize_contacts(cx_b2);
5741    assert_eq!(contacts_b2.current, &["user_a"]);
5742    assert!(contacts_b2.incoming_requests.is_empty());
5743
5744    // User C doesn't see user B as their contact, and the outgoing request to them is removed.
5745    let contacts_c = client_c.summarize_contacts(cx_c);
5746    assert!(contacts_c.current.is_empty());
5747    assert!(contacts_c.outgoing_requests.is_empty());
5748    let contacts_c2 = client_c2.summarize_contacts(cx_c2);
5749    assert!(contacts_c2.current.is_empty());
5750    assert!(contacts_c2.outgoing_requests.is_empty());
5751
5752    // Incoming/outgoing requests are not present upon connecting (tested here via disconnect/reconnect)
5753    disconnect_and_reconnect(&client_a, cx_a).await;
5754    disconnect_and_reconnect(&client_b, cx_b).await;
5755    disconnect_and_reconnect(&client_c, cx_c).await;
5756    executor.run_until_parked();
5757    assert_eq!(client_a.summarize_contacts(cx_a).current, &["user_b"]);
5758    assert_eq!(client_b.summarize_contacts(cx_b).current, &["user_a"]);
5759    assert!(client_b
5760        .summarize_contacts(cx_b)
5761        .incoming_requests
5762        .is_empty());
5763    assert!(client_c.summarize_contacts(cx_c).current.is_empty());
5764    assert!(client_c
5765        .summarize_contacts(cx_c)
5766        .outgoing_requests
5767        .is_empty());
5768
5769    async fn disconnect_and_reconnect(client: &TestClient, cx: &mut TestAppContext) {
5770        client.disconnect(&cx.to_async());
5771        client.clear_contacts(cx).await;
5772        client
5773            .authenticate_and_connect(false, &cx.to_async())
5774            .await
5775            .unwrap();
5776    }
5777}
5778
5779#[gpui::test(iterations = 10)]
5780async fn test_join_call_after_screen_was_shared(
5781    executor: BackgroundExecutor,
5782    cx_a: &mut TestAppContext,
5783    cx_b: &mut TestAppContext,
5784) {
5785    let mut server = TestServer::start(executor.clone()).await;
5786
5787    let client_a = server.create_client(cx_a, "user_a").await;
5788    let client_b = server.create_client(cx_b, "user_b").await;
5789    server
5790        .make_contacts(&mut [(&client_a, cx_a), (&client_b, cx_b)])
5791        .await;
5792
5793    let active_call_a = cx_a.read(ActiveCall::global);
5794    let active_call_b = cx_b.read(ActiveCall::global);
5795
5796    // Call users B and C from client A.
5797    active_call_a
5798        .update(cx_a, |call, cx| {
5799            call.invite(client_b.user_id().unwrap(), None, cx)
5800        })
5801        .await
5802        .unwrap();
5803
5804    let room_a = active_call_a.read_with(cx_a, |call, _| call.room().unwrap().clone());
5805    executor.run_until_parked();
5806    assert_eq!(
5807        room_participants(&room_a, cx_a),
5808        RoomParticipants {
5809            remote: Default::default(),
5810            pending: vec!["user_b".to_string()]
5811        }
5812    );
5813
5814    // User B receives the call.
5815
5816    let mut incoming_call_b = active_call_b.read_with(cx_b, |call, _| call.incoming());
5817    let call_b = incoming_call_b.next().await.unwrap().unwrap();
5818    assert_eq!(call_b.calling_user.github_login, "user_a");
5819
5820    // User A shares their screen
5821    let display = MacOSDisplay::new();
5822    active_call_a
5823        .update(cx_a, |call, cx| {
5824            call.room().unwrap().update(cx, |room, cx| {
5825                room.set_display_sources(vec![display.clone()]);
5826                room.share_screen(cx)
5827            })
5828        })
5829        .await
5830        .unwrap();
5831
5832    client_b.user_store().update(cx_b, |user_store, _| {
5833        user_store.clear_cache();
5834    });
5835
5836    // User B joins the room
5837    active_call_b
5838        .update(cx_b, |call, cx| call.accept_incoming(cx))
5839        .await
5840        .unwrap();
5841
5842    let room_b = active_call_b.read_with(cx_b, |call, _| call.room().unwrap().clone());
5843    assert!(incoming_call_b.next().await.unwrap().is_none());
5844
5845    executor.run_until_parked();
5846    assert_eq!(
5847        room_participants(&room_a, cx_a),
5848        RoomParticipants {
5849            remote: vec!["user_b".to_string()],
5850            pending: vec![],
5851        }
5852    );
5853    assert_eq!(
5854        room_participants(&room_b, cx_b),
5855        RoomParticipants {
5856            remote: vec!["user_a".to_string()],
5857            pending: vec![],
5858        }
5859    );
5860
5861    // Ensure User B sees User A's screenshare.
5862
5863    room_b.read_with(cx_b, |room, _| {
5864        assert_eq!(
5865            room.remote_participants()
5866                .get(&client_a.user_id().unwrap())
5867                .unwrap()
5868                .video_tracks
5869                .len(),
5870            1
5871        );
5872    });
5873}
5874
5875#[gpui::test]
5876async fn test_right_click_menu_behind_collab_panel(cx: &mut TestAppContext) {
5877    let mut server = TestServer::start(cx.executor().clone()).await;
5878    let client_a = server.create_client(cx, "user_a").await;
5879    let (_workspace_a, cx) = client_a.build_test_workspace(cx).await;
5880
5881    cx.simulate_resize(size(px(300.), px(300.)));
5882
5883    cx.simulate_keystrokes("cmd-n cmd-n cmd-n");
5884    cx.update(|cx| cx.refresh());
5885
5886    let tab_bounds = cx.debug_bounds("TAB-2").unwrap();
5887    let new_tab_button_bounds = cx.debug_bounds("ICON-Plus").unwrap();
5888
5889    assert!(
5890        tab_bounds.intersects(&new_tab_button_bounds),
5891        "Tab should overlap with the new tab button, if this is failing check if there's been a redesign!"
5892    );
5893
5894    cx.simulate_event(MouseDownEvent {
5895        button: MouseButton::Right,
5896        position: new_tab_button_bounds.center(),
5897        modifiers: Modifiers::default(),
5898        click_count: 1,
5899        first_mouse: false,
5900    });
5901
5902    // regression test that the right click menu for tabs does not open.
5903    assert!(cx.debug_bounds("MENU_ITEM-Close").is_none());
5904
5905    let tab_bounds = cx.debug_bounds("TAB-1").unwrap();
5906    cx.simulate_event(MouseDownEvent {
5907        button: MouseButton::Right,
5908        position: tab_bounds.center(),
5909        modifiers: Modifiers::default(),
5910        click_count: 1,
5911        first_mouse: false,
5912    });
5913    assert!(cx.debug_bounds("MENU_ITEM-Close").is_some());
5914}
5915
5916#[gpui::test]
5917async fn test_cmd_k_left(cx: &mut TestAppContext) {
5918    let client = TestServer::start1(cx).await;
5919    let (workspace, cx) = client.build_test_workspace(cx).await;
5920
5921    cx.simulate_keystrokes("cmd-n");
5922    workspace.update(cx, |workspace, cx| {
5923        assert!(workspace.items(cx).collect::<Vec<_>>().len() == 1);
5924    });
5925    cx.simulate_keystrokes("cmd-k left");
5926    workspace.update(cx, |workspace, cx| {
5927        assert!(workspace.items(cx).collect::<Vec<_>>().len() == 2);
5928    });
5929    cx.simulate_keystrokes("cmd-k");
5930    // sleep for longer than the timeout in keyboard shortcut handling
5931    // to verify that it doesn't fire in this case.
5932    cx.executor().advance_clock(Duration::from_secs(2));
5933    cx.simulate_keystrokes("left");
5934    workspace.update(cx, |workspace, cx| {
5935        assert!(workspace.items(cx).collect::<Vec<_>>().len() == 2);
5936    });
5937}