randomized_integration_tests.rs

   1use crate::{
   2    db::{self, NewUserParams, UserId},
   3    rpc::{CLEANUP_TIMEOUT, RECONNECT_TIMEOUT},
   4    tests::{TestClient, TestServer},
   5};
   6use anyhow::{anyhow, Result};
   7use call::ActiveCall;
   8use client::RECEIVE_TIMEOUT;
   9use collections::BTreeMap;
  10use editor::Bias;
  11use fs::{FakeFs, Fs as _};
  12use futures::StreamExt as _;
  13use gpui::{executor::Deterministic, ModelHandle, Task, TestAppContext};
  14use language::{range_to_lsp, FakeLspAdapter, Language, LanguageConfig, PointUtf16};
  15use lsp::FakeLanguageServer;
  16use parking_lot::Mutex;
  17use project::{search::SearchQuery, Project, ProjectPath};
  18use rand::{
  19    distributions::{Alphanumeric, DistString},
  20    prelude::*,
  21};
  22use serde::{Deserialize, Serialize};
  23use settings::Settings;
  24use std::{
  25    env,
  26    ops::Range,
  27    path::{Path, PathBuf},
  28    rc::Rc,
  29    sync::{
  30        atomic::{AtomicBool, Ordering::SeqCst},
  31        Arc,
  32    },
  33};
  34use util::ResultExt;
  35
  36lazy_static::lazy_static! {
  37    static ref PLAN_LOAD_PATH: Option<PathBuf> = path_env_var("LOAD_PLAN");
  38    static ref PLAN_SAVE_PATH: Option<PathBuf> = path_env_var("SAVE_PLAN");
  39    static ref LOADED_PLAN_JSON: Mutex<Option<Vec<u8>>> = Default::default();
  40    static ref PLAN: Mutex<Option<Arc<Mutex<TestPlan>>>> = Default::default();
  41}
  42
  43#[gpui::test(iterations = 100, on_failure = "on_failure")]
  44async fn test_random_collaboration(
  45    cx: &mut TestAppContext,
  46    deterministic: Arc<Deterministic>,
  47    rng: StdRng,
  48) {
  49    deterministic.forbid_parking();
  50
  51    let max_peers = env::var("MAX_PEERS")
  52        .map(|i| i.parse().expect("invalid `MAX_PEERS` variable"))
  53        .unwrap_or(3);
  54    let max_operations = env::var("OPERATIONS")
  55        .map(|i| i.parse().expect("invalid `OPERATIONS` variable"))
  56        .unwrap_or(10);
  57
  58    let mut server = TestServer::start(&deterministic).await;
  59    let db = server.app_state.db.clone();
  60
  61    let mut users = Vec::new();
  62    for ix in 0..max_peers {
  63        let username = format!("user-{}", ix + 1);
  64        let user_id = db
  65            .create_user(
  66                &format!("{username}@example.com"),
  67                false,
  68                NewUserParams {
  69                    github_login: username.clone(),
  70                    github_user_id: (ix + 1) as i32,
  71                    invite_count: 0,
  72                },
  73            )
  74            .await
  75            .unwrap()
  76            .user_id;
  77        users.push(UserTestPlan {
  78            user_id,
  79            username,
  80            online: false,
  81            next_root_id: 0,
  82            operation_ix: 0,
  83        });
  84    }
  85
  86    for (ix, user_a) in users.iter().enumerate() {
  87        for user_b in &users[ix + 1..] {
  88            server
  89                .app_state
  90                .db
  91                .send_contact_request(user_a.user_id, user_b.user_id)
  92                .await
  93                .unwrap();
  94            server
  95                .app_state
  96                .db
  97                .respond_to_contact_request(user_b.user_id, user_a.user_id, true)
  98                .await
  99                .unwrap();
 100        }
 101    }
 102
 103    let plan = Arc::new(Mutex::new(TestPlan::new(rng, users, max_operations)));
 104
 105    if let Some(path) = &*PLAN_LOAD_PATH {
 106        let json = LOADED_PLAN_JSON
 107            .lock()
 108            .get_or_insert_with(|| {
 109                eprintln!("loaded test plan from path {:?}", path);
 110                std::fs::read(path).unwrap()
 111            })
 112            .clone();
 113        plan.lock().deserialize(json);
 114    }
 115
 116    PLAN.lock().replace(plan.clone());
 117
 118    let mut clients = Vec::new();
 119    let mut client_tasks = Vec::new();
 120    let mut operation_channels = Vec::new();
 121
 122    loop {
 123        let Some((next_operation, applied)) = plan.lock().next_server_operation(&clients) else { break };
 124        applied.store(true, SeqCst);
 125        let did_apply = apply_server_operation(
 126            deterministic.clone(),
 127            &mut server,
 128            &mut clients,
 129            &mut client_tasks,
 130            &mut operation_channels,
 131            plan.clone(),
 132            next_operation,
 133            cx,
 134        )
 135        .await;
 136        if !did_apply {
 137            applied.store(false, SeqCst);
 138        }
 139    }
 140
 141    drop(operation_channels);
 142    deterministic.start_waiting();
 143    futures::future::join_all(client_tasks).await;
 144    deterministic.finish_waiting();
 145    deterministic.run_until_parked();
 146
 147    check_consistency_between_clients(&clients);
 148
 149    for (client, mut cx) in clients {
 150        cx.update(|cx| {
 151            cx.clear_globals();
 152            cx.set_global(Settings::test(cx));
 153            drop(client);
 154        });
 155    }
 156
 157    deterministic.run_until_parked();
 158}
 159
 160fn on_failure() {
 161    if let Some(plan) = PLAN.lock().clone() {
 162        if let Some(path) = &*PLAN_SAVE_PATH {
 163            eprintln!("saved test plan to path {:?}", path);
 164            std::fs::write(path, plan.lock().serialize()).unwrap();
 165        }
 166    }
 167}
 168
 169async fn apply_server_operation(
 170    deterministic: Arc<Deterministic>,
 171    server: &mut TestServer,
 172    clients: &mut Vec<(Rc<TestClient>, TestAppContext)>,
 173    client_tasks: &mut Vec<Task<()>>,
 174    operation_channels: &mut Vec<futures::channel::mpsc::UnboundedSender<usize>>,
 175    plan: Arc<Mutex<TestPlan>>,
 176    operation: Operation,
 177    cx: &mut TestAppContext,
 178) -> bool {
 179    match operation {
 180        Operation::AddConnection { user_id } => {
 181            let username;
 182            {
 183                let mut plan = plan.lock();
 184                let mut user = plan.user(user_id);
 185                if user.online {
 186                    return false;
 187                }
 188                user.online = true;
 189                username = user.username.clone();
 190            };
 191            log::info!("Adding new connection for {}", username);
 192            let next_entity_id = (user_id.0 * 10_000) as usize;
 193            let mut client_cx = TestAppContext::new(
 194                cx.foreground_platform(),
 195                cx.platform(),
 196                deterministic.build_foreground(user_id.0 as usize),
 197                deterministic.build_background(),
 198                cx.font_cache(),
 199                cx.leak_detector(),
 200                next_entity_id,
 201                cx.function_name.clone(),
 202            );
 203
 204            let (operation_tx, operation_rx) = futures::channel::mpsc::unbounded();
 205            let client = Rc::new(server.create_client(&mut client_cx, &username).await);
 206            operation_channels.push(operation_tx);
 207            clients.push((client.clone(), client_cx.clone()));
 208            client_tasks.push(client_cx.foreground().spawn(simulate_client(
 209                client,
 210                operation_rx,
 211                plan.clone(),
 212                client_cx,
 213            )));
 214
 215            log::info!("Added connection for {}", username);
 216        }
 217
 218        Operation::RemoveConnection {
 219            user_id: removed_user_id,
 220        } => {
 221            log::info!("Simulating full disconnection of user {}", removed_user_id);
 222            let client_ix = clients
 223                .iter()
 224                .position(|(client, cx)| client.current_user_id(cx) == removed_user_id);
 225            let Some(client_ix) = client_ix else { return false };
 226            let user_connection_ids = server
 227                .connection_pool
 228                .lock()
 229                .user_connection_ids(removed_user_id)
 230                .collect::<Vec<_>>();
 231            assert_eq!(user_connection_ids.len(), 1);
 232            let removed_peer_id = user_connection_ids[0].into();
 233            let (client, mut client_cx) = clients.remove(client_ix);
 234            let client_task = client_tasks.remove(client_ix);
 235            operation_channels.remove(client_ix);
 236            server.forbid_connections();
 237            server.disconnect_client(removed_peer_id);
 238            deterministic.advance_clock(RECEIVE_TIMEOUT + RECONNECT_TIMEOUT);
 239            deterministic.start_waiting();
 240            log::info!("Waiting for user {} to exit...", removed_user_id);
 241            client_task.await;
 242            deterministic.finish_waiting();
 243            server.allow_connections();
 244
 245            for project in client.remote_projects().iter() {
 246                project.read_with(&client_cx, |project, _| {
 247                    assert!(
 248                        project.is_read_only(),
 249                        "project {:?} should be read only",
 250                        project.remote_id()
 251                    )
 252                });
 253            }
 254
 255            for (client, cx) in clients {
 256                let contacts = server
 257                    .app_state
 258                    .db
 259                    .get_contacts(client.current_user_id(cx))
 260                    .await
 261                    .unwrap();
 262                let pool = server.connection_pool.lock();
 263                for contact in contacts {
 264                    if let db::Contact::Accepted { user_id, busy, .. } = contact {
 265                        if user_id == removed_user_id {
 266                            assert!(!pool.is_user_online(user_id));
 267                            assert!(!busy);
 268                        }
 269                    }
 270                }
 271            }
 272
 273            log::info!("{} removed", client.username);
 274            plan.lock().user(removed_user_id).online = false;
 275            client_cx.update(|cx| {
 276                cx.clear_globals();
 277                drop(client);
 278            });
 279        }
 280
 281        Operation::BounceConnection { user_id } => {
 282            log::info!("Simulating temporary disconnection of user {}", user_id);
 283            let user_connection_ids = server
 284                .connection_pool
 285                .lock()
 286                .user_connection_ids(user_id)
 287                .collect::<Vec<_>>();
 288            if user_connection_ids.is_empty() {
 289                return false;
 290            }
 291            assert_eq!(user_connection_ids.len(), 1);
 292            let peer_id = user_connection_ids[0].into();
 293            server.disconnect_client(peer_id);
 294            deterministic.advance_clock(RECEIVE_TIMEOUT + RECONNECT_TIMEOUT);
 295        }
 296
 297        Operation::RestartServer => {
 298            log::info!("Simulating server restart");
 299            server.reset().await;
 300            deterministic.advance_clock(RECEIVE_TIMEOUT);
 301            server.start().await.unwrap();
 302            deterministic.advance_clock(CLEANUP_TIMEOUT);
 303            let environment = &server.app_state.config.zed_environment;
 304            let stale_room_ids = server
 305                .app_state
 306                .db
 307                .stale_room_ids(environment, server.id())
 308                .await
 309                .unwrap();
 310            assert_eq!(stale_room_ids, vec![]);
 311        }
 312
 313        Operation::MutateClients {
 314            user_ids,
 315            batch_id,
 316            quiesce,
 317        } => {
 318            let mut applied = false;
 319            for user_id in user_ids {
 320                let client_ix = clients
 321                    .iter()
 322                    .position(|(client, cx)| client.current_user_id(cx) == user_id);
 323                let Some(client_ix) = client_ix else { continue };
 324                applied = true;
 325                if let Err(err) = operation_channels[client_ix].unbounded_send(batch_id) {
 326                    log::error!("error signaling user {user_id}: {err}");
 327                }
 328            }
 329
 330            if quiesce && applied {
 331                deterministic.run_until_parked();
 332                check_consistency_between_clients(&clients);
 333            }
 334
 335            return applied;
 336        }
 337    }
 338    true
 339}
 340
 341async fn apply_client_operation(
 342    client: &TestClient,
 343    operation: ClientOperation,
 344    cx: &mut TestAppContext,
 345) -> Result<(), TestError> {
 346    match operation {
 347        ClientOperation::AcceptIncomingCall => {
 348            let active_call = cx.read(ActiveCall::global);
 349            if active_call.read_with(cx, |call, _| call.incoming().borrow().is_none()) {
 350                Err(TestError::Inapplicable)?;
 351            }
 352
 353            log::info!("{}: accepting incoming call", client.username);
 354            active_call
 355                .update(cx, |call, cx| call.accept_incoming(cx))
 356                .await?;
 357        }
 358
 359        ClientOperation::RejectIncomingCall => {
 360            let active_call = cx.read(ActiveCall::global);
 361            if active_call.read_with(cx, |call, _| call.incoming().borrow().is_none()) {
 362                Err(TestError::Inapplicable)?;
 363            }
 364
 365            log::info!("{}: declining incoming call", client.username);
 366            active_call.update(cx, |call, _| call.decline_incoming())?;
 367        }
 368
 369        ClientOperation::LeaveCall => {
 370            let active_call = cx.read(ActiveCall::global);
 371            if active_call.read_with(cx, |call, _| call.room().is_none()) {
 372                Err(TestError::Inapplicable)?;
 373            }
 374
 375            log::info!("{}: hanging up", client.username);
 376            active_call.update(cx, |call, cx| call.hang_up(cx)).await?;
 377        }
 378
 379        ClientOperation::InviteContactToCall { user_id } => {
 380            let active_call = cx.read(ActiveCall::global);
 381
 382            log::info!("{}: inviting {}", client.username, user_id,);
 383            active_call
 384                .update(cx, |call, cx| call.invite(user_id.to_proto(), None, cx))
 385                .await
 386                .log_err();
 387        }
 388
 389        ClientOperation::OpenLocalProject { first_root_name } => {
 390            log::info!(
 391                "{}: opening local project at {:?}",
 392                client.username,
 393                first_root_name
 394            );
 395
 396            let root_path = Path::new("/").join(&first_root_name);
 397            client.fs.create_dir(&root_path).await.unwrap();
 398            client
 399                .fs
 400                .create_file(&root_path.join("main.rs"), Default::default())
 401                .await
 402                .unwrap();
 403            let project = client.build_local_project(root_path, cx).await.0;
 404            ensure_project_shared(&project, client, cx).await;
 405            client.local_projects_mut().push(project.clone());
 406        }
 407
 408        ClientOperation::AddWorktreeToProject {
 409            project_root_name,
 410            new_root_path,
 411        } => {
 412            let project = project_for_root_name(client, &project_root_name, cx)
 413                .ok_or(TestError::Inapplicable)?;
 414
 415            log::info!(
 416                "{}: finding/creating local worktree at {:?} to project with root path {}",
 417                client.username,
 418                new_root_path,
 419                project_root_name
 420            );
 421
 422            ensure_project_shared(&project, client, cx).await;
 423            if !client.fs.paths().contains(&new_root_path) {
 424                client.fs.create_dir(&new_root_path).await.unwrap();
 425            }
 426            project
 427                .update(cx, |project, cx| {
 428                    project.find_or_create_local_worktree(&new_root_path, true, cx)
 429                })
 430                .await
 431                .unwrap();
 432        }
 433
 434        ClientOperation::CloseRemoteProject { project_root_name } => {
 435            let project = project_for_root_name(client, &project_root_name, cx)
 436                .ok_or(TestError::Inapplicable)?;
 437
 438            log::info!(
 439                "{}: closing remote project with root path {}",
 440                client.username,
 441                project_root_name,
 442            );
 443
 444            let ix = client
 445                .remote_projects()
 446                .iter()
 447                .position(|p| p == &project)
 448                .unwrap();
 449            cx.update(|_| {
 450                client.remote_projects_mut().remove(ix);
 451                client.buffers().retain(|p, _| *p != project);
 452                drop(project);
 453            });
 454        }
 455
 456        ClientOperation::OpenRemoteProject {
 457            host_id,
 458            first_root_name,
 459        } => {
 460            let active_call = cx.read(ActiveCall::global);
 461            let project = active_call
 462                .update(cx, |call, cx| {
 463                    let room = call.room().cloned()?;
 464                    let participant = room
 465                        .read(cx)
 466                        .remote_participants()
 467                        .get(&host_id.to_proto())?;
 468                    let project_id = participant
 469                        .projects
 470                        .iter()
 471                        .find(|project| project.worktree_root_names[0] == first_root_name)?
 472                        .id;
 473                    Some(room.update(cx, |room, cx| {
 474                        room.join_project(
 475                            project_id,
 476                            client.language_registry.clone(),
 477                            FakeFs::new(cx.background().clone()),
 478                            cx,
 479                        )
 480                    }))
 481                })
 482                .ok_or(TestError::Inapplicable)?;
 483
 484            log::info!(
 485                "{}: joining remote project of user {}, root name {}",
 486                client.username,
 487                host_id,
 488                first_root_name,
 489            );
 490
 491            let project = project.await?;
 492            client.remote_projects_mut().push(project.clone());
 493        }
 494
 495        ClientOperation::CreateWorktreeEntry {
 496            project_root_name,
 497            is_local,
 498            full_path,
 499            is_dir,
 500        } => {
 501            let project = project_for_root_name(client, &project_root_name, cx)
 502                .ok_or(TestError::Inapplicable)?;
 503            let project_path = project_path_for_full_path(&project, &full_path, cx)
 504                .ok_or(TestError::Inapplicable)?;
 505
 506            log::info!(
 507                "{}: creating {} at path {:?} in {} project {}",
 508                client.username,
 509                if is_dir { "dir" } else { "file" },
 510                full_path,
 511                if is_local { "local" } else { "remote" },
 512                project_root_name,
 513            );
 514
 515            ensure_project_shared(&project, client, cx).await;
 516            project
 517                .update(cx, |p, cx| p.create_entry(project_path, is_dir, cx))
 518                .unwrap()
 519                .await?;
 520        }
 521
 522        ClientOperation::OpenBuffer {
 523            project_root_name,
 524            is_local,
 525            full_path,
 526        } => {
 527            let project = project_for_root_name(client, &project_root_name, cx)
 528                .ok_or(TestError::Inapplicable)?;
 529            let project_path = project_path_for_full_path(&project, &full_path, cx)
 530                .ok_or(TestError::Inapplicable)?;
 531
 532            log::info!(
 533                "{}: opening buffer {:?} in {} project {}",
 534                client.username,
 535                full_path,
 536                if is_local { "local" } else { "remote" },
 537                project_root_name,
 538            );
 539
 540            ensure_project_shared(&project, client, cx).await;
 541            let buffer = project
 542                .update(cx, |project, cx| project.open_buffer(project_path, cx))
 543                .await?;
 544            client.buffers_for_project(&project).insert(buffer);
 545        }
 546
 547        ClientOperation::EditBuffer {
 548            project_root_name,
 549            is_local,
 550            full_path,
 551            edits,
 552        } => {
 553            let project = project_for_root_name(client, &project_root_name, cx)
 554                .ok_or(TestError::Inapplicable)?;
 555            let buffer = buffer_for_full_path(client, &project, &full_path, cx)
 556                .ok_or(TestError::Inapplicable)?;
 557
 558            log::info!(
 559                "{}: editing buffer {:?} in {} project {} with {:?}",
 560                client.username,
 561                full_path,
 562                if is_local { "local" } else { "remote" },
 563                project_root_name,
 564                edits
 565            );
 566
 567            ensure_project_shared(&project, client, cx).await;
 568            buffer.update(cx, |buffer, cx| {
 569                let snapshot = buffer.snapshot();
 570                buffer.edit(
 571                    edits.into_iter().map(|(range, text)| {
 572                        let start = snapshot.clip_offset(range.start, Bias::Left);
 573                        let end = snapshot.clip_offset(range.end, Bias::Right);
 574                        (start..end, text)
 575                    }),
 576                    None,
 577                    cx,
 578                );
 579            });
 580        }
 581
 582        ClientOperation::CloseBuffer {
 583            project_root_name,
 584            is_local,
 585            full_path,
 586        } => {
 587            let project = project_for_root_name(client, &project_root_name, cx)
 588                .ok_or(TestError::Inapplicable)?;
 589            let buffer = buffer_for_full_path(client, &project, &full_path, cx)
 590                .ok_or(TestError::Inapplicable)?;
 591
 592            log::info!(
 593                "{}: closing buffer {:?} in {} project {}",
 594                client.username,
 595                full_path,
 596                if is_local { "local" } else { "remote" },
 597                project_root_name
 598            );
 599
 600            ensure_project_shared(&project, client, cx).await;
 601            cx.update(|_| {
 602                client.buffers_for_project(&project).remove(&buffer);
 603                drop(buffer);
 604            });
 605        }
 606
 607        ClientOperation::SaveBuffer {
 608            project_root_name,
 609            is_local,
 610            full_path,
 611            detach,
 612        } => {
 613            let project = project_for_root_name(client, &project_root_name, cx)
 614                .ok_or(TestError::Inapplicable)?;
 615            let buffer = buffer_for_full_path(client, &project, &full_path, cx)
 616                .ok_or(TestError::Inapplicable)?;
 617
 618            log::info!(
 619                "{}: saving buffer {:?} in {} project {}, {}",
 620                client.username,
 621                full_path,
 622                if is_local { "local" } else { "remote" },
 623                project_root_name,
 624                if detach { "detaching" } else { "awaiting" }
 625            );
 626
 627            ensure_project_shared(&project, client, cx).await;
 628            let requested_version = buffer.read_with(cx, |buffer, _| buffer.version());
 629            let save = project.update(cx, |project, cx| project.save_buffer(buffer, cx));
 630            let save = cx.background().spawn(async move {
 631                let (saved_version, _, _) = save
 632                    .await
 633                    .map_err(|err| anyhow!("save request failed: {:?}", err))?;
 634                assert!(saved_version.observed_all(&requested_version));
 635                anyhow::Ok(())
 636            });
 637            if detach {
 638                cx.update(|cx| save.detach_and_log_err(cx));
 639            } else {
 640                save.await?;
 641            }
 642        }
 643
 644        ClientOperation::RequestLspDataInBuffer {
 645            project_root_name,
 646            is_local,
 647            full_path,
 648            offset,
 649            kind,
 650            detach,
 651        } => {
 652            let project = project_for_root_name(client, &project_root_name, cx)
 653                .ok_or(TestError::Inapplicable)?;
 654            let buffer = buffer_for_full_path(client, &project, &full_path, cx)
 655                .ok_or(TestError::Inapplicable)?;
 656
 657            log::info!(
 658                "{}: request LSP {:?} for buffer {:?} in {} project {}, {}",
 659                client.username,
 660                kind,
 661                full_path,
 662                if is_local { "local" } else { "remote" },
 663                project_root_name,
 664                if detach { "detaching" } else { "awaiting" }
 665            );
 666
 667            use futures::{FutureExt as _, TryFutureExt as _};
 668            let offset = buffer.read_with(cx, |b, _| b.clip_offset(offset, Bias::Left));
 669            let request = cx.foreground().spawn(project.update(cx, |project, cx| {
 670                match kind {
 671                    LspRequestKind::Rename => project
 672                        .prepare_rename(buffer, offset, cx)
 673                        .map_ok(|_| ())
 674                        .boxed(),
 675                    LspRequestKind::Completion => project
 676                        .completions(&buffer, offset, cx)
 677                        .map_ok(|_| ())
 678                        .boxed(),
 679                    LspRequestKind::CodeAction => project
 680                        .code_actions(&buffer, offset..offset, cx)
 681                        .map_ok(|_| ())
 682                        .boxed(),
 683                    LspRequestKind::Definition => project
 684                        .definition(&buffer, offset, cx)
 685                        .map_ok(|_| ())
 686                        .boxed(),
 687                    LspRequestKind::Highlights => project
 688                        .document_highlights(&buffer, offset, cx)
 689                        .map_ok(|_| ())
 690                        .boxed(),
 691                }
 692            }));
 693            if detach {
 694                request.detach();
 695            } else {
 696                request.await?;
 697            }
 698        }
 699
 700        ClientOperation::SearchProject {
 701            project_root_name,
 702            is_local,
 703            query,
 704            detach,
 705        } => {
 706            let project = project_for_root_name(client, &project_root_name, cx)
 707                .ok_or(TestError::Inapplicable)?;
 708
 709            log::info!(
 710                "{}: search {} project {} for {:?}, {}",
 711                client.username,
 712                if is_local { "local" } else { "remote" },
 713                project_root_name,
 714                query,
 715                if detach { "detaching" } else { "awaiting" }
 716            );
 717
 718            let search = project.update(cx, |project, cx| {
 719                project.search(SearchQuery::text(query, false, false), cx)
 720            });
 721            drop(project);
 722            let search = cx.background().spawn(async move {
 723                search
 724                    .await
 725                    .map_err(|err| anyhow!("search request failed: {:?}", err))
 726            });
 727            if detach {
 728                cx.update(|cx| search.detach_and_log_err(cx));
 729            } else {
 730                search.await?;
 731            }
 732        }
 733
 734        ClientOperation::WriteFsEntry {
 735            path,
 736            is_dir,
 737            content,
 738        } => {
 739            if !client
 740                .fs
 741                .directories()
 742                .contains(&path.parent().unwrap().to_owned())
 743            {
 744                return Err(TestError::Inapplicable);
 745            }
 746
 747            if is_dir {
 748                log::info!("{}: creating dir at {:?}", client.username, path);
 749                client.fs.create_dir(&path).await.unwrap();
 750            } else {
 751                let exists = client.fs.metadata(&path).await?.is_some();
 752                let verb = if exists { "updating" } else { "creating" };
 753                log::info!("{}: {} file at {:?}", verb, client.username, path);
 754
 755                client
 756                    .fs
 757                    .save(&path, &content.as_str().into(), fs::LineEnding::Unix)
 758                    .await
 759                    .unwrap();
 760            }
 761        }
 762
 763        ClientOperation::WriteGitIndex {
 764            repo_path,
 765            contents,
 766        } => {
 767            if !client.fs.directories().contains(&repo_path) {
 768                return Err(TestError::Inapplicable);
 769            }
 770
 771            log::info!(
 772                "{}: writing git index for repo {:?}: {:?}",
 773                client.username,
 774                repo_path,
 775                contents
 776            );
 777
 778            let dot_git_dir = repo_path.join(".git");
 779            let contents = contents
 780                .iter()
 781                .map(|(path, contents)| (path.as_path(), contents.clone()))
 782                .collect::<Vec<_>>();
 783            if client.fs.metadata(&dot_git_dir).await?.is_none() {
 784                client.fs.create_dir(&dot_git_dir).await?;
 785            }
 786            client.fs.set_index_for_repo(&dot_git_dir, &contents).await;
 787        }
 788
 789        ClientOperation::WriteGitBranch {
 790            repo_path,
 791            new_branch,
 792        } => {
 793            if !client.fs.directories().contains(&repo_path) {
 794                return Err(TestError::Inapplicable);
 795            }
 796
 797            log::info!(
 798                "{}: writing git branch for repo {:?}: {:?}",
 799                client.username,
 800                repo_path,
 801                new_branch
 802            );
 803
 804            let dot_git_dir = repo_path.join(".git");
 805            if client.fs.metadata(&dot_git_dir).await?.is_none() {
 806                client.fs.create_dir(&dot_git_dir).await?;
 807            }
 808            client.fs.set_branch_name(&dot_git_dir, new_branch).await;
 809        }
 810    }
 811    Ok(())
 812}
 813
 814fn check_consistency_between_clients(clients: &[(Rc<TestClient>, TestAppContext)]) {
 815    for (client, client_cx) in clients {
 816        for guest_project in client.remote_projects().iter() {
 817            guest_project.read_with(client_cx, |guest_project, cx| {
 818                let host_project = clients.iter().find_map(|(client, cx)| {
 819                    let project = client
 820                        .local_projects()
 821                        .iter()
 822                        .find(|host_project| {
 823                            host_project.read_with(cx, |host_project, _| {
 824                                host_project.remote_id() == guest_project.remote_id()
 825                            })
 826                        })?
 827                        .clone();
 828                    Some((project, cx))
 829                });
 830
 831                if !guest_project.is_read_only() {
 832                    if let Some((host_project, host_cx)) = host_project {
 833                        let host_worktree_snapshots =
 834                            host_project.read_with(host_cx, |host_project, cx| {
 835                                host_project
 836                                    .worktrees(cx)
 837                                    .map(|worktree| {
 838                                        let worktree = worktree.read(cx);
 839                                        (worktree.id(), worktree.snapshot())
 840                                    })
 841                                    .collect::<BTreeMap<_, _>>()
 842                            });
 843                        let guest_worktree_snapshots = guest_project
 844                            .worktrees(cx)
 845                            .map(|worktree| {
 846                                let worktree = worktree.read(cx);
 847                                (worktree.id(), worktree.snapshot())
 848                            })
 849                            .collect::<BTreeMap<_, _>>();
 850
 851                        assert_eq!(
 852                            guest_worktree_snapshots.values().map(|w| w.abs_path()).collect::<Vec<_>>(),
 853                            host_worktree_snapshots.values().map(|w| w.abs_path()).collect::<Vec<_>>(),
 854                            "{} has different worktrees than the host for project {:?}",
 855                            client.username, guest_project.remote_id(),
 856                        );
 857
 858                        for (id, host_snapshot) in &host_worktree_snapshots {
 859                            let guest_snapshot = &guest_worktree_snapshots[id];
 860                            assert_eq!(
 861                                guest_snapshot.root_name(),
 862                                host_snapshot.root_name(),
 863                                "{} has different root name than the host for worktree {}, project {:?}",
 864                                client.username,
 865                                id,
 866                                guest_project.remote_id(),
 867                            );
 868                            assert_eq!(
 869                                guest_snapshot.abs_path(),
 870                                host_snapshot.abs_path(),
 871                                "{} has different abs path than the host for worktree {}, project: {:?}",
 872                                client.username,
 873                                id,
 874                                guest_project.remote_id(),
 875                            );
 876                            assert_eq!(
 877                                guest_snapshot.entries(false).collect::<Vec<_>>(),
 878                                host_snapshot.entries(false).collect::<Vec<_>>(),
 879                                "{} has different snapshot than the host for worktree {:?} and project {:?}",
 880                                client.username,
 881                                host_snapshot.abs_path(),
 882                                guest_project.remote_id(),
 883                            );
 884                            assert_eq!(guest_snapshot.repositories().collect::<Vec<_>>(), host_snapshot.repositories().collect::<Vec<_>>(),
 885                                "{} has different repositories than the host for worktree {:?} and project {:?}",
 886                                client.username,
 887                                host_snapshot.abs_path(),
 888                                guest_project.remote_id(),
 889                            );
 890                            assert_eq!(guest_snapshot.scan_id(), host_snapshot.scan_id(),
 891                                "{} has different scan id than the host for worktree {:?} and project {:?}",
 892                                client.username,
 893                                host_snapshot.abs_path(),
 894                                guest_project.remote_id(),
 895                            );
 896                        }
 897                    }
 898                }
 899
 900                for buffer in guest_project.opened_buffers(cx) {
 901                    let buffer = buffer.read(cx);
 902                    assert_eq!(
 903                        buffer.deferred_ops_len(),
 904                        0,
 905                        "{} has deferred operations for buffer {:?} in project {:?}",
 906                        client.username,
 907                        buffer.file().unwrap().full_path(cx),
 908                        guest_project.remote_id(),
 909                    );
 910                }
 911            });
 912        }
 913
 914        let buffers = client.buffers().clone();
 915        for (guest_project, guest_buffers) in &buffers {
 916            let project_id = if guest_project.read_with(client_cx, |project, _| {
 917                project.is_local() || project.is_read_only()
 918            }) {
 919                continue;
 920            } else {
 921                guest_project
 922                    .read_with(client_cx, |project, _| project.remote_id())
 923                    .unwrap()
 924            };
 925            let guest_user_id = client.user_id().unwrap();
 926
 927            let host_project = clients.iter().find_map(|(client, cx)| {
 928                let project = client
 929                    .local_projects()
 930                    .iter()
 931                    .find(|host_project| {
 932                        host_project.read_with(cx, |host_project, _| {
 933                            host_project.remote_id() == Some(project_id)
 934                        })
 935                    })?
 936                    .clone();
 937                Some((client.user_id().unwrap(), project, cx))
 938            });
 939
 940            let (host_user_id, host_project, host_cx) =
 941                if let Some((host_user_id, host_project, host_cx)) = host_project {
 942                    (host_user_id, host_project, host_cx)
 943                } else {
 944                    continue;
 945                };
 946
 947            for guest_buffer in guest_buffers {
 948                let buffer_id = guest_buffer.read_with(client_cx, |buffer, _| buffer.remote_id());
 949                let host_buffer = host_project.read_with(host_cx, |project, cx| {
 950                    project.buffer_for_id(buffer_id, cx).unwrap_or_else(|| {
 951                        panic!(
 952                            "host does not have buffer for guest:{}, peer:{:?}, id:{}",
 953                            client.username,
 954                            client.peer_id(),
 955                            buffer_id
 956                        )
 957                    })
 958                });
 959                let path = host_buffer
 960                    .read_with(host_cx, |buffer, cx| buffer.file().unwrap().full_path(cx));
 961
 962                assert_eq!(
 963                    guest_buffer.read_with(client_cx, |buffer, _| buffer.deferred_ops_len()),
 964                    0,
 965                    "{}, buffer {}, path {:?} has deferred operations",
 966                    client.username,
 967                    buffer_id,
 968                    path,
 969                );
 970                assert_eq!(
 971                    guest_buffer.read_with(client_cx, |buffer, _| buffer.text()),
 972                    host_buffer.read_with(host_cx, |buffer, _| buffer.text()),
 973                    "{}, buffer {}, path {:?}, differs from the host's buffer",
 974                    client.username,
 975                    buffer_id,
 976                    path
 977                );
 978
 979                let host_file = host_buffer.read_with(host_cx, |b, _| b.file().cloned());
 980                let guest_file = guest_buffer.read_with(client_cx, |b, _| b.file().cloned());
 981                match (host_file, guest_file) {
 982                    (Some(host_file), Some(guest_file)) => {
 983                        assert_eq!(guest_file.path(), host_file.path());
 984                        assert_eq!(guest_file.is_deleted(), host_file.is_deleted());
 985                        assert_eq!(
 986                            guest_file.mtime(),
 987                            host_file.mtime(),
 988                            "guest {} mtime does not match host {} for path {:?} in project {}",
 989                            guest_user_id,
 990                            host_user_id,
 991                            guest_file.path(),
 992                            project_id,
 993                        );
 994                    }
 995                    (None, None) => {}
 996                    (None, _) => panic!("host's file is None, guest's isn't"),
 997                    (_, None) => panic!("guest's file is None, hosts's isn't"),
 998                }
 999
1000                let host_diff_base =
1001                    host_buffer.read_with(host_cx, |b, _| b.diff_base().map(ToString::to_string));
1002                let guest_diff_base = guest_buffer
1003                    .read_with(client_cx, |b, _| b.diff_base().map(ToString::to_string));
1004                assert_eq!(
1005                    guest_diff_base, host_diff_base,
1006                    "guest {} diff base does not match host's for path {path:?} in project {project_id}",
1007                    client.username
1008                );
1009
1010                let host_saved_version =
1011                    host_buffer.read_with(host_cx, |b, _| b.saved_version().clone());
1012                let guest_saved_version =
1013                    guest_buffer.read_with(client_cx, |b, _| b.saved_version().clone());
1014                assert_eq!(
1015                    guest_saved_version, host_saved_version,
1016                    "guest {} saved version does not match host's for path {path:?} in project {project_id}",
1017                    client.username
1018                );
1019
1020                let host_saved_version_fingerprint =
1021                    host_buffer.read_with(host_cx, |b, _| b.saved_version_fingerprint());
1022                let guest_saved_version_fingerprint =
1023                    guest_buffer.read_with(client_cx, |b, _| b.saved_version_fingerprint());
1024                assert_eq!(
1025                    guest_saved_version_fingerprint, host_saved_version_fingerprint,
1026                    "guest {} saved fingerprint does not match host's for path {path:?} in project {project_id}",
1027                    client.username
1028                );
1029
1030                let host_saved_mtime = host_buffer.read_with(host_cx, |b, _| b.saved_mtime());
1031                let guest_saved_mtime = guest_buffer.read_with(client_cx, |b, _| b.saved_mtime());
1032                assert_eq!(
1033                    guest_saved_mtime, host_saved_mtime,
1034                    "guest {} saved mtime does not match host's for path {path:?} in project {project_id}",
1035                    client.username
1036                );
1037
1038                let host_is_dirty = host_buffer.read_with(host_cx, |b, _| b.is_dirty());
1039                let guest_is_dirty = guest_buffer.read_with(client_cx, |b, _| b.is_dirty());
1040                assert_eq!(guest_is_dirty, host_is_dirty,
1041                    "guest {} dirty status does not match host's for path {path:?} in project {project_id}",
1042                    client.username
1043                );
1044
1045                let host_has_conflict = host_buffer.read_with(host_cx, |b, _| b.has_conflict());
1046                let guest_has_conflict = guest_buffer.read_with(client_cx, |b, _| b.has_conflict());
1047                assert_eq!(guest_has_conflict, host_has_conflict,
1048                    "guest {} conflict status does not match host's for path {path:?} in project {project_id}",
1049                    client.username
1050                );
1051            }
1052        }
1053    }
1054}
1055
1056struct TestPlan {
1057    rng: StdRng,
1058    replay: bool,
1059    stored_operations: Vec<(StoredOperation, Arc<AtomicBool>)>,
1060    max_operations: usize,
1061    operation_ix: usize,
1062    users: Vec<UserTestPlan>,
1063    next_batch_id: usize,
1064    allow_server_restarts: bool,
1065    allow_client_reconnection: bool,
1066    allow_client_disconnection: bool,
1067}
1068
1069struct UserTestPlan {
1070    user_id: UserId,
1071    username: String,
1072    next_root_id: usize,
1073    operation_ix: usize,
1074    online: bool,
1075}
1076
1077#[derive(Clone, Debug, Serialize, Deserialize)]
1078#[serde(untagged)]
1079enum StoredOperation {
1080    Server(Operation),
1081    Client {
1082        user_id: UserId,
1083        batch_id: usize,
1084        operation: ClientOperation,
1085    },
1086}
1087
1088#[derive(Clone, Debug, Serialize, Deserialize)]
1089enum Operation {
1090    AddConnection {
1091        user_id: UserId,
1092    },
1093    RemoveConnection {
1094        user_id: UserId,
1095    },
1096    BounceConnection {
1097        user_id: UserId,
1098    },
1099    RestartServer,
1100    MutateClients {
1101        batch_id: usize,
1102        #[serde(skip_serializing)]
1103        #[serde(skip_deserializing)]
1104        user_ids: Vec<UserId>,
1105        quiesce: bool,
1106    },
1107}
1108
1109#[derive(Clone, Debug, Serialize, Deserialize)]
1110enum ClientOperation {
1111    AcceptIncomingCall,
1112    RejectIncomingCall,
1113    LeaveCall,
1114    InviteContactToCall {
1115        user_id: UserId,
1116    },
1117    OpenLocalProject {
1118        first_root_name: String,
1119    },
1120    OpenRemoteProject {
1121        host_id: UserId,
1122        first_root_name: String,
1123    },
1124    AddWorktreeToProject {
1125        project_root_name: String,
1126        new_root_path: PathBuf,
1127    },
1128    CloseRemoteProject {
1129        project_root_name: String,
1130    },
1131    OpenBuffer {
1132        project_root_name: String,
1133        is_local: bool,
1134        full_path: PathBuf,
1135    },
1136    SearchProject {
1137        project_root_name: String,
1138        is_local: bool,
1139        query: String,
1140        detach: bool,
1141    },
1142    EditBuffer {
1143        project_root_name: String,
1144        is_local: bool,
1145        full_path: PathBuf,
1146        edits: Vec<(Range<usize>, Arc<str>)>,
1147    },
1148    CloseBuffer {
1149        project_root_name: String,
1150        is_local: bool,
1151        full_path: PathBuf,
1152    },
1153    SaveBuffer {
1154        project_root_name: String,
1155        is_local: bool,
1156        full_path: PathBuf,
1157        detach: bool,
1158    },
1159    RequestLspDataInBuffer {
1160        project_root_name: String,
1161        is_local: bool,
1162        full_path: PathBuf,
1163        offset: usize,
1164        kind: LspRequestKind,
1165        detach: bool,
1166    },
1167    CreateWorktreeEntry {
1168        project_root_name: String,
1169        is_local: bool,
1170        full_path: PathBuf,
1171        is_dir: bool,
1172    },
1173    WriteFsEntry {
1174        path: PathBuf,
1175        is_dir: bool,
1176        content: String,
1177    },
1178    WriteGitIndex {
1179        repo_path: PathBuf,
1180        contents: Vec<(PathBuf, String)>,
1181    },
1182    WriteGitBranch {
1183        repo_path: PathBuf,
1184        new_branch: Option<String>,
1185    },
1186}
1187
1188#[derive(Clone, Debug, Serialize, Deserialize)]
1189enum LspRequestKind {
1190    Rename,
1191    Completion,
1192    CodeAction,
1193    Definition,
1194    Highlights,
1195}
1196
1197enum TestError {
1198    Inapplicable,
1199    Other(anyhow::Error),
1200}
1201
1202impl From<anyhow::Error> for TestError {
1203    fn from(value: anyhow::Error) -> Self {
1204        Self::Other(value)
1205    }
1206}
1207
1208impl TestPlan {
1209    fn new(mut rng: StdRng, users: Vec<UserTestPlan>, max_operations: usize) -> Self {
1210        Self {
1211            replay: false,
1212            allow_server_restarts: rng.gen_bool(0.7),
1213            allow_client_reconnection: rng.gen_bool(0.7),
1214            allow_client_disconnection: rng.gen_bool(0.1),
1215            stored_operations: Vec::new(),
1216            operation_ix: 0,
1217            next_batch_id: 0,
1218            max_operations,
1219            users,
1220            rng,
1221        }
1222    }
1223
1224    fn deserialize(&mut self, json: Vec<u8>) {
1225        let stored_operations: Vec<StoredOperation> = serde_json::from_slice(&json).unwrap();
1226        self.replay = true;
1227        self.stored_operations = stored_operations
1228            .iter()
1229            .cloned()
1230            .enumerate()
1231            .map(|(i, mut operation)| {
1232                if let StoredOperation::Server(Operation::MutateClients {
1233                    batch_id: current_batch_id,
1234                    user_ids,
1235                    ..
1236                }) = &mut operation
1237                {
1238                    assert!(user_ids.is_empty());
1239                    user_ids.extend(stored_operations[i + 1..].iter().filter_map(|operation| {
1240                        if let StoredOperation::Client {
1241                            user_id, batch_id, ..
1242                        } = operation
1243                        {
1244                            if batch_id == current_batch_id {
1245                                return Some(user_id);
1246                            }
1247                        }
1248                        None
1249                    }));
1250                    user_ids.sort_unstable();
1251                }
1252                (operation, Arc::new(AtomicBool::new(false)))
1253            })
1254            .collect()
1255    }
1256
1257    fn serialize(&mut self) -> Vec<u8> {
1258        // Format each operation as one line
1259        let mut json = Vec::new();
1260        json.push(b'[');
1261        for (operation, applied) in &self.stored_operations {
1262            if !applied.load(SeqCst) {
1263                continue;
1264            }
1265            if json.len() > 1 {
1266                json.push(b',');
1267            }
1268            json.extend_from_slice(b"\n  ");
1269            serde_json::to_writer(&mut json, operation).unwrap();
1270        }
1271        json.extend_from_slice(b"\n]\n");
1272        json
1273    }
1274
1275    fn next_server_operation(
1276        &mut self,
1277        clients: &[(Rc<TestClient>, TestAppContext)],
1278    ) -> Option<(Operation, Arc<AtomicBool>)> {
1279        if self.replay {
1280            while let Some(stored_operation) = self.stored_operations.get(self.operation_ix) {
1281                self.operation_ix += 1;
1282                if let (StoredOperation::Server(operation), applied) = stored_operation {
1283                    return Some((operation.clone(), applied.clone()));
1284                }
1285            }
1286            None
1287        } else {
1288            let operation = self.generate_server_operation(clients)?;
1289            let applied = Arc::new(AtomicBool::new(false));
1290            self.stored_operations
1291                .push((StoredOperation::Server(operation.clone()), applied.clone()));
1292            Some((operation, applied))
1293        }
1294    }
1295
1296    fn next_client_operation(
1297        &mut self,
1298        client: &TestClient,
1299        current_batch_id: usize,
1300        cx: &TestAppContext,
1301    ) -> Option<(ClientOperation, Arc<AtomicBool>)> {
1302        let current_user_id = client.current_user_id(cx);
1303        let user_ix = self
1304            .users
1305            .iter()
1306            .position(|user| user.user_id == current_user_id)
1307            .unwrap();
1308        let user_plan = &mut self.users[user_ix];
1309
1310        if self.replay {
1311            while let Some(stored_operation) = self.stored_operations.get(user_plan.operation_ix) {
1312                user_plan.operation_ix += 1;
1313                if let (
1314                    StoredOperation::Client {
1315                        user_id, operation, ..
1316                    },
1317                    applied,
1318                ) = stored_operation
1319                {
1320                    if user_id == &current_user_id {
1321                        return Some((operation.clone(), applied.clone()));
1322                    }
1323                }
1324            }
1325            None
1326        } else {
1327            let operation = self.generate_client_operation(current_user_id, client, cx)?;
1328            let applied = Arc::new(AtomicBool::new(false));
1329            self.stored_operations.push((
1330                StoredOperation::Client {
1331                    user_id: current_user_id,
1332                    batch_id: current_batch_id,
1333                    operation: operation.clone(),
1334                },
1335                applied.clone(),
1336            ));
1337            Some((operation, applied))
1338        }
1339    }
1340
1341    fn generate_server_operation(
1342        &mut self,
1343        clients: &[(Rc<TestClient>, TestAppContext)],
1344    ) -> Option<Operation> {
1345        if self.operation_ix == self.max_operations {
1346            return None;
1347        }
1348
1349        Some(loop {
1350            break match self.rng.gen_range(0..100) {
1351                0..=29 if clients.len() < self.users.len() => {
1352                    let user = self
1353                        .users
1354                        .iter()
1355                        .filter(|u| !u.online)
1356                        .choose(&mut self.rng)
1357                        .unwrap();
1358                    self.operation_ix += 1;
1359                    Operation::AddConnection {
1360                        user_id: user.user_id,
1361                    }
1362                }
1363                30..=34 if clients.len() > 1 && self.allow_client_disconnection => {
1364                    let (client, cx) = &clients[self.rng.gen_range(0..clients.len())];
1365                    let user_id = client.current_user_id(cx);
1366                    self.operation_ix += 1;
1367                    Operation::RemoveConnection { user_id }
1368                }
1369                35..=39 if clients.len() > 1 && self.allow_client_reconnection => {
1370                    let (client, cx) = &clients[self.rng.gen_range(0..clients.len())];
1371                    let user_id = client.current_user_id(cx);
1372                    self.operation_ix += 1;
1373                    Operation::BounceConnection { user_id }
1374                }
1375                40..=44 if self.allow_server_restarts && clients.len() > 1 => {
1376                    self.operation_ix += 1;
1377                    Operation::RestartServer
1378                }
1379                _ if !clients.is_empty() => {
1380                    let count = self
1381                        .rng
1382                        .gen_range(1..10)
1383                        .min(self.max_operations - self.operation_ix);
1384                    let batch_id = util::post_inc(&mut self.next_batch_id);
1385                    let mut user_ids = (0..count)
1386                        .map(|_| {
1387                            let ix = self.rng.gen_range(0..clients.len());
1388                            let (client, cx) = &clients[ix];
1389                            client.current_user_id(cx)
1390                        })
1391                        .collect::<Vec<_>>();
1392                    user_ids.sort_unstable();
1393                    Operation::MutateClients {
1394                        user_ids,
1395                        batch_id,
1396                        quiesce: self.rng.gen_bool(0.7),
1397                    }
1398                }
1399                _ => continue,
1400            };
1401        })
1402    }
1403
1404    fn generate_client_operation(
1405        &mut self,
1406        user_id: UserId,
1407        client: &TestClient,
1408        cx: &TestAppContext,
1409    ) -> Option<ClientOperation> {
1410        if self.operation_ix == self.max_operations {
1411            return None;
1412        }
1413
1414        self.operation_ix += 1;
1415        let call = cx.read(ActiveCall::global);
1416        Some(loop {
1417            match self.rng.gen_range(0..100_u32) {
1418                // Mutate the call
1419                0..=29 => {
1420                    // Respond to an incoming call
1421                    if call.read_with(cx, |call, _| call.incoming().borrow().is_some()) {
1422                        break if self.rng.gen_bool(0.7) {
1423                            ClientOperation::AcceptIncomingCall
1424                        } else {
1425                            ClientOperation::RejectIncomingCall
1426                        };
1427                    }
1428
1429                    match self.rng.gen_range(0..100_u32) {
1430                        // Invite a contact to the current call
1431                        0..=70 => {
1432                            let available_contacts =
1433                                client.user_store.read_with(cx, |user_store, _| {
1434                                    user_store
1435                                        .contacts()
1436                                        .iter()
1437                                        .filter(|contact| contact.online && !contact.busy)
1438                                        .cloned()
1439                                        .collect::<Vec<_>>()
1440                                });
1441                            if !available_contacts.is_empty() {
1442                                let contact = available_contacts.choose(&mut self.rng).unwrap();
1443                                break ClientOperation::InviteContactToCall {
1444                                    user_id: UserId(contact.user.id as i32),
1445                                };
1446                            }
1447                        }
1448
1449                        // Leave the current call
1450                        71.. => {
1451                            if self.allow_client_disconnection
1452                                && call.read_with(cx, |call, _| call.room().is_some())
1453                            {
1454                                break ClientOperation::LeaveCall;
1455                            }
1456                        }
1457                    }
1458                }
1459
1460                // Mutate projects
1461                30..=59 => match self.rng.gen_range(0..100_u32) {
1462                    // Open a new project
1463                    0..=70 => {
1464                        // Open a remote project
1465                        if let Some(room) = call.read_with(cx, |call, _| call.room().cloned()) {
1466                            let existing_remote_project_ids = cx.read(|cx| {
1467                                client
1468                                    .remote_projects()
1469                                    .iter()
1470                                    .map(|p| p.read(cx).remote_id().unwrap())
1471                                    .collect::<Vec<_>>()
1472                            });
1473                            let new_remote_projects = room.read_with(cx, |room, _| {
1474                                room.remote_participants()
1475                                    .values()
1476                                    .flat_map(|participant| {
1477                                        participant.projects.iter().filter_map(|project| {
1478                                            if existing_remote_project_ids.contains(&project.id) {
1479                                                None
1480                                            } else {
1481                                                Some((
1482                                                    UserId::from_proto(participant.user.id),
1483                                                    project.worktree_root_names[0].clone(),
1484                                                ))
1485                                            }
1486                                        })
1487                                    })
1488                                    .collect::<Vec<_>>()
1489                            });
1490                            if !new_remote_projects.is_empty() {
1491                                let (host_id, first_root_name) =
1492                                    new_remote_projects.choose(&mut self.rng).unwrap().clone();
1493                                break ClientOperation::OpenRemoteProject {
1494                                    host_id,
1495                                    first_root_name,
1496                                };
1497                            }
1498                        }
1499                        // Open a local project
1500                        else {
1501                            let first_root_name = self.next_root_dir_name(user_id);
1502                            break ClientOperation::OpenLocalProject { first_root_name };
1503                        }
1504                    }
1505
1506                    // Close a remote project
1507                    71..=80 => {
1508                        if !client.remote_projects().is_empty() {
1509                            let project = client
1510                                .remote_projects()
1511                                .choose(&mut self.rng)
1512                                .unwrap()
1513                                .clone();
1514                            let first_root_name = root_name_for_project(&project, cx);
1515                            break ClientOperation::CloseRemoteProject {
1516                                project_root_name: first_root_name,
1517                            };
1518                        }
1519                    }
1520
1521                    // Mutate project worktrees
1522                    81.. => match self.rng.gen_range(0..100_u32) {
1523                        // Add a worktree to a local project
1524                        0..=50 => {
1525                            let Some(project) = client
1526                                .local_projects()
1527                                .choose(&mut self.rng)
1528                                .cloned() else { continue };
1529                            let project_root_name = root_name_for_project(&project, cx);
1530                            let mut paths = client.fs.paths();
1531                            paths.remove(0);
1532                            let new_root_path = if paths.is_empty() || self.rng.gen() {
1533                                Path::new("/").join(&self.next_root_dir_name(user_id))
1534                            } else {
1535                                paths.choose(&mut self.rng).unwrap().clone()
1536                            };
1537                            break ClientOperation::AddWorktreeToProject {
1538                                project_root_name,
1539                                new_root_path,
1540                            };
1541                        }
1542
1543                        // Add an entry to a worktree
1544                        _ => {
1545                            let Some(project) = choose_random_project(client, &mut self.rng) else { continue };
1546                            let project_root_name = root_name_for_project(&project, cx);
1547                            let is_local = project.read_with(cx, |project, _| project.is_local());
1548                            let worktree = project.read_with(cx, |project, cx| {
1549                                project
1550                                    .worktrees(cx)
1551                                    .filter(|worktree| {
1552                                        let worktree = worktree.read(cx);
1553                                        worktree.is_visible()
1554                                            && worktree.entries(false).any(|e| e.is_file())
1555                                            && worktree.root_entry().map_or(false, |e| e.is_dir())
1556                                    })
1557                                    .choose(&mut self.rng)
1558                            });
1559                            let Some(worktree) = worktree else { continue };
1560                            let is_dir = self.rng.gen::<bool>();
1561                            let mut full_path =
1562                                worktree.read_with(cx, |w, _| PathBuf::from(w.root_name()));
1563                            full_path.push(gen_file_name(&mut self.rng));
1564                            if !is_dir {
1565                                full_path.set_extension("rs");
1566                            }
1567                            break ClientOperation::CreateWorktreeEntry {
1568                                project_root_name,
1569                                is_local,
1570                                full_path,
1571                                is_dir,
1572                            };
1573                        }
1574                    },
1575                },
1576
1577                // Query and mutate buffers
1578                60..=90 => {
1579                    let Some(project) = choose_random_project(client, &mut self.rng) else { continue };
1580                    let project_root_name = root_name_for_project(&project, cx);
1581                    let is_local = project.read_with(cx, |project, _| project.is_local());
1582
1583                    match self.rng.gen_range(0..100_u32) {
1584                        // Manipulate an existing buffer
1585                        0..=70 => {
1586                            let Some(buffer) = client
1587                                .buffers_for_project(&project)
1588                                .iter()
1589                                .choose(&mut self.rng)
1590                                .cloned() else { continue };
1591
1592                            let full_path = buffer
1593                                .read_with(cx, |buffer, cx| buffer.file().unwrap().full_path(cx));
1594
1595                            match self.rng.gen_range(0..100_u32) {
1596                                // Close the buffer
1597                                0..=15 => {
1598                                    break ClientOperation::CloseBuffer {
1599                                        project_root_name,
1600                                        is_local,
1601                                        full_path,
1602                                    };
1603                                }
1604                                // Save the buffer
1605                                16..=29 if buffer.read_with(cx, |b, _| b.is_dirty()) => {
1606                                    let detach = self.rng.gen_bool(0.3);
1607                                    break ClientOperation::SaveBuffer {
1608                                        project_root_name,
1609                                        is_local,
1610                                        full_path,
1611                                        detach,
1612                                    };
1613                                }
1614                                // Edit the buffer
1615                                30..=69 => {
1616                                    let edits = buffer.read_with(cx, |buffer, _| {
1617                                        buffer.get_random_edits(&mut self.rng, 3)
1618                                    });
1619                                    break ClientOperation::EditBuffer {
1620                                        project_root_name,
1621                                        is_local,
1622                                        full_path,
1623                                        edits,
1624                                    };
1625                                }
1626                                // Make an LSP request
1627                                _ => {
1628                                    let offset = buffer.read_with(cx, |buffer, _| {
1629                                        buffer.clip_offset(
1630                                            self.rng.gen_range(0..=buffer.len()),
1631                                            language::Bias::Left,
1632                                        )
1633                                    });
1634                                    let detach = self.rng.gen();
1635                                    break ClientOperation::RequestLspDataInBuffer {
1636                                        project_root_name,
1637                                        full_path,
1638                                        offset,
1639                                        is_local,
1640                                        kind: match self.rng.gen_range(0..5_u32) {
1641                                            0 => LspRequestKind::Rename,
1642                                            1 => LspRequestKind::Highlights,
1643                                            2 => LspRequestKind::Definition,
1644                                            3 => LspRequestKind::CodeAction,
1645                                            4.. => LspRequestKind::Completion,
1646                                        },
1647                                        detach,
1648                                    };
1649                                }
1650                            }
1651                        }
1652
1653                        71..=80 => {
1654                            let query = self.rng.gen_range('a'..='z').to_string();
1655                            let detach = self.rng.gen_bool(0.3);
1656                            break ClientOperation::SearchProject {
1657                                project_root_name,
1658                                is_local,
1659                                query,
1660                                detach,
1661                            };
1662                        }
1663
1664                        // Open a buffer
1665                        81.. => {
1666                            let worktree = project.read_with(cx, |project, cx| {
1667                                project
1668                                    .worktrees(cx)
1669                                    .filter(|worktree| {
1670                                        let worktree = worktree.read(cx);
1671                                        worktree.is_visible()
1672                                            && worktree.entries(false).any(|e| e.is_file())
1673                                    })
1674                                    .choose(&mut self.rng)
1675                            });
1676                            let Some(worktree) = worktree else { continue };
1677                            let full_path = worktree.read_with(cx, |worktree, _| {
1678                                let entry = worktree
1679                                    .entries(false)
1680                                    .filter(|e| e.is_file())
1681                                    .choose(&mut self.rng)
1682                                    .unwrap();
1683                                if entry.path.as_ref() == Path::new("") {
1684                                    Path::new(worktree.root_name()).into()
1685                                } else {
1686                                    Path::new(worktree.root_name()).join(&entry.path)
1687                                }
1688                            });
1689                            break ClientOperation::OpenBuffer {
1690                                project_root_name,
1691                                is_local,
1692                                full_path,
1693                            };
1694                        }
1695                    }
1696                }
1697
1698                // Update a git index
1699                91..=93 => {
1700                    let repo_path = client
1701                        .fs
1702                        .directories()
1703                        .into_iter()
1704                        .choose(&mut self.rng)
1705                        .unwrap()
1706                        .clone();
1707
1708                    let mut file_paths = client
1709                        .fs
1710                        .files()
1711                        .into_iter()
1712                        .filter(|path| path.starts_with(&repo_path))
1713                        .collect::<Vec<_>>();
1714                    let count = self.rng.gen_range(0..=file_paths.len());
1715                    file_paths.shuffle(&mut self.rng);
1716                    file_paths.truncate(count);
1717
1718                    let mut contents = Vec::new();
1719                    for abs_child_file_path in &file_paths {
1720                        let child_file_path = abs_child_file_path
1721                            .strip_prefix(&repo_path)
1722                            .unwrap()
1723                            .to_path_buf();
1724                        let new_base = Alphanumeric.sample_string(&mut self.rng, 16);
1725                        contents.push((child_file_path, new_base));
1726                    }
1727
1728                    break ClientOperation::WriteGitIndex {
1729                        repo_path,
1730                        contents,
1731                    };
1732                }
1733
1734                // Update a git branch
1735                94..=95 => {
1736                    let repo_path = client
1737                        .fs
1738                        .directories()
1739                        .choose(&mut self.rng)
1740                        .unwrap()
1741                        .clone();
1742
1743                    let new_branch = (self.rng.gen_range(0..10) > 3)
1744                        .then(|| Alphanumeric.sample_string(&mut self.rng, 8));
1745
1746                    break ClientOperation::WriteGitBranch {
1747                        repo_path,
1748                        new_branch,
1749                    };
1750                }
1751
1752                // Create or update a file or directory
1753                96.. => {
1754                    let is_dir = self.rng.gen::<bool>();
1755                    let content;
1756                    let mut path;
1757                    let dir_paths = client.fs.directories();
1758
1759                    if is_dir {
1760                        content = String::new();
1761                        path = dir_paths.choose(&mut self.rng).unwrap().clone();
1762                        path.push(gen_file_name(&mut self.rng));
1763                    } else {
1764                        content = Alphanumeric.sample_string(&mut self.rng, 16);
1765
1766                        // Create a new file or overwrite an existing file
1767                        let file_paths = client.fs.files();
1768                        if file_paths.is_empty() || self.rng.gen_bool(0.5) {
1769                            path = dir_paths.choose(&mut self.rng).unwrap().clone();
1770                            path.push(gen_file_name(&mut self.rng));
1771                            path.set_extension("rs");
1772                        } else {
1773                            path = file_paths.choose(&mut self.rng).unwrap().clone()
1774                        };
1775                    }
1776                    break ClientOperation::WriteFsEntry {
1777                        path,
1778                        is_dir,
1779                        content,
1780                    };
1781                }
1782            }
1783        })
1784    }
1785
1786    fn next_root_dir_name(&mut self, user_id: UserId) -> String {
1787        let user_ix = self
1788            .users
1789            .iter()
1790            .position(|user| user.user_id == user_id)
1791            .unwrap();
1792        let root_id = util::post_inc(&mut self.users[user_ix].next_root_id);
1793        format!("dir-{user_id}-{root_id}")
1794    }
1795
1796    fn user(&mut self, user_id: UserId) -> &mut UserTestPlan {
1797        let ix = self
1798            .users
1799            .iter()
1800            .position(|user| user.user_id == user_id)
1801            .unwrap();
1802        &mut self.users[ix]
1803    }
1804}
1805
1806async fn simulate_client(
1807    client: Rc<TestClient>,
1808    mut operation_rx: futures::channel::mpsc::UnboundedReceiver<usize>,
1809    plan: Arc<Mutex<TestPlan>>,
1810    mut cx: TestAppContext,
1811) {
1812    // Setup language server
1813    let mut language = Language::new(
1814        LanguageConfig {
1815            name: "Rust".into(),
1816            path_suffixes: vec!["rs".to_string()],
1817            ..Default::default()
1818        },
1819        None,
1820    );
1821    let _fake_language_servers = language
1822        .set_fake_lsp_adapter(Arc::new(FakeLspAdapter {
1823            name: "the-fake-language-server",
1824            capabilities: lsp::LanguageServer::full_capabilities(),
1825            initializer: Some(Box::new({
1826                let fs = client.fs.clone();
1827                move |fake_server: &mut FakeLanguageServer| {
1828                    fake_server.handle_request::<lsp::request::Completion, _, _>(
1829                        |_, _| async move {
1830                            Ok(Some(lsp::CompletionResponse::Array(vec![
1831                                lsp::CompletionItem {
1832                                    text_edit: Some(lsp::CompletionTextEdit::Edit(lsp::TextEdit {
1833                                        range: lsp::Range::new(
1834                                            lsp::Position::new(0, 0),
1835                                            lsp::Position::new(0, 0),
1836                                        ),
1837                                        new_text: "the-new-text".to_string(),
1838                                    })),
1839                                    ..Default::default()
1840                                },
1841                            ])))
1842                        },
1843                    );
1844
1845                    fake_server.handle_request::<lsp::request::CodeActionRequest, _, _>(
1846                        |_, _| async move {
1847                            Ok(Some(vec![lsp::CodeActionOrCommand::CodeAction(
1848                                lsp::CodeAction {
1849                                    title: "the-code-action".to_string(),
1850                                    ..Default::default()
1851                                },
1852                            )]))
1853                        },
1854                    );
1855
1856                    fake_server.handle_request::<lsp::request::PrepareRenameRequest, _, _>(
1857                        |params, _| async move {
1858                            Ok(Some(lsp::PrepareRenameResponse::Range(lsp::Range::new(
1859                                params.position,
1860                                params.position,
1861                            ))))
1862                        },
1863                    );
1864
1865                    fake_server.handle_request::<lsp::request::GotoDefinition, _, _>({
1866                        let fs = fs.clone();
1867                        move |_, cx| {
1868                            let background = cx.background();
1869                            let mut rng = background.rng();
1870                            let count = rng.gen_range::<usize, _>(1..3);
1871                            let files = fs.files();
1872                            let files = (0..count)
1873                                .map(|_| files.choose(&mut *rng).unwrap().clone())
1874                                .collect::<Vec<_>>();
1875                            async move {
1876                                log::info!("LSP: Returning definitions in files {:?}", &files);
1877                                Ok(Some(lsp::GotoDefinitionResponse::Array(
1878                                    files
1879                                        .into_iter()
1880                                        .map(|file| lsp::Location {
1881                                            uri: lsp::Url::from_file_path(file).unwrap(),
1882                                            range: Default::default(),
1883                                        })
1884                                        .collect(),
1885                                )))
1886                            }
1887                        }
1888                    });
1889
1890                    fake_server.handle_request::<lsp::request::DocumentHighlightRequest, _, _>(
1891                        move |_, cx| {
1892                            let mut highlights = Vec::new();
1893                            let background = cx.background();
1894                            let mut rng = background.rng();
1895
1896                            let highlight_count = rng.gen_range(1..=5);
1897                            for _ in 0..highlight_count {
1898                                let start_row = rng.gen_range(0..100);
1899                                let start_column = rng.gen_range(0..100);
1900                                let end_row = rng.gen_range(0..100);
1901                                let end_column = rng.gen_range(0..100);
1902                                let start = PointUtf16::new(start_row, start_column);
1903                                let end = PointUtf16::new(end_row, end_column);
1904                                let range = if start > end { end..start } else { start..end };
1905                                highlights.push(lsp::DocumentHighlight {
1906                                    range: range_to_lsp(range.clone()),
1907                                    kind: Some(lsp::DocumentHighlightKind::READ),
1908                                });
1909                            }
1910                            highlights.sort_unstable_by_key(|highlight| {
1911                                (highlight.range.start, highlight.range.end)
1912                            });
1913                            async move { Ok(Some(highlights)) }
1914                        },
1915                    );
1916                }
1917            })),
1918            ..Default::default()
1919        }))
1920        .await;
1921    client.language_registry.add(Arc::new(language));
1922
1923    while let Some(batch_id) = operation_rx.next().await {
1924        let Some((operation, applied)) = plan.lock().next_client_operation(&client, batch_id, &cx) else { break };
1925        applied.store(true, SeqCst);
1926        match apply_client_operation(&client, operation, &mut cx).await {
1927            Ok(()) => {}
1928            Err(TestError::Inapplicable) => {
1929                applied.store(false, SeqCst);
1930                log::info!("skipped operation");
1931            }
1932            Err(TestError::Other(error)) => {
1933                log::error!("{} error: {}", client.username, error);
1934            }
1935        }
1936        cx.background().simulate_random_delay().await;
1937    }
1938    log::info!("{}: done", client.username);
1939}
1940
1941fn buffer_for_full_path(
1942    client: &TestClient,
1943    project: &ModelHandle<Project>,
1944    full_path: &PathBuf,
1945    cx: &TestAppContext,
1946) -> Option<ModelHandle<language::Buffer>> {
1947    client
1948        .buffers_for_project(project)
1949        .iter()
1950        .find(|buffer| {
1951            buffer.read_with(cx, |buffer, cx| {
1952                buffer.file().unwrap().full_path(cx) == *full_path
1953            })
1954        })
1955        .cloned()
1956}
1957
1958fn project_for_root_name(
1959    client: &TestClient,
1960    root_name: &str,
1961    cx: &TestAppContext,
1962) -> Option<ModelHandle<Project>> {
1963    if let Some(ix) = project_ix_for_root_name(&*client.local_projects(), root_name, cx) {
1964        return Some(client.local_projects()[ix].clone());
1965    }
1966    if let Some(ix) = project_ix_for_root_name(&*client.remote_projects(), root_name, cx) {
1967        return Some(client.remote_projects()[ix].clone());
1968    }
1969    None
1970}
1971
1972fn project_ix_for_root_name(
1973    projects: &[ModelHandle<Project>],
1974    root_name: &str,
1975    cx: &TestAppContext,
1976) -> Option<usize> {
1977    projects.iter().position(|project| {
1978        project.read_with(cx, |project, cx| {
1979            let worktree = project.visible_worktrees(cx).next().unwrap();
1980            worktree.read(cx).root_name() == root_name
1981        })
1982    })
1983}
1984
1985fn root_name_for_project(project: &ModelHandle<Project>, cx: &TestAppContext) -> String {
1986    project.read_with(cx, |project, cx| {
1987        project
1988            .visible_worktrees(cx)
1989            .next()
1990            .unwrap()
1991            .read(cx)
1992            .root_name()
1993            .to_string()
1994    })
1995}
1996
1997fn project_path_for_full_path(
1998    project: &ModelHandle<Project>,
1999    full_path: &Path,
2000    cx: &TestAppContext,
2001) -> Option<ProjectPath> {
2002    let mut components = full_path.components();
2003    let root_name = components.next().unwrap().as_os_str().to_str().unwrap();
2004    let path = components.as_path().into();
2005    let worktree_id = project.read_with(cx, |project, cx| {
2006        project.worktrees(cx).find_map(|worktree| {
2007            let worktree = worktree.read(cx);
2008            if worktree.root_name() == root_name {
2009                Some(worktree.id())
2010            } else {
2011                None
2012            }
2013        })
2014    })?;
2015    Some(ProjectPath { worktree_id, path })
2016}
2017
2018async fn ensure_project_shared(
2019    project: &ModelHandle<Project>,
2020    client: &TestClient,
2021    cx: &mut TestAppContext,
2022) {
2023    let first_root_name = root_name_for_project(project, cx);
2024    let active_call = cx.read(ActiveCall::global);
2025    if active_call.read_with(cx, |call, _| call.room().is_some())
2026        && project.read_with(cx, |project, _| project.is_local() && !project.is_shared())
2027    {
2028        match active_call
2029            .update(cx, |call, cx| call.share_project(project.clone(), cx))
2030            .await
2031        {
2032            Ok(project_id) => {
2033                log::info!(
2034                    "{}: shared project {} with id {}",
2035                    client.username,
2036                    first_root_name,
2037                    project_id
2038                );
2039            }
2040            Err(error) => {
2041                log::error!(
2042                    "{}: error sharing project {}: {:?}",
2043                    client.username,
2044                    first_root_name,
2045                    error
2046                );
2047            }
2048        }
2049    }
2050}
2051
2052fn choose_random_project(client: &TestClient, rng: &mut StdRng) -> Option<ModelHandle<Project>> {
2053    client
2054        .local_projects()
2055        .iter()
2056        .chain(client.remote_projects().iter())
2057        .choose(rng)
2058        .cloned()
2059}
2060
2061fn gen_file_name(rng: &mut StdRng) -> String {
2062    let mut name = String::new();
2063    for _ in 0..10 {
2064        let letter = rng.gen_range('a'..='z');
2065        name.push(letter);
2066    }
2067    name
2068}
2069
2070fn path_env_var(name: &str) -> Option<PathBuf> {
2071    let value = env::var(name).ok()?;
2072    let mut path = PathBuf::from(value);
2073    if path.is_relative() {
2074        let mut abs_path = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
2075        abs_path.pop();
2076        abs_path.pop();
2077        abs_path.push(path);
2078        path = abs_path
2079    }
2080    Some(path)
2081}