random_project_collaboration_tests.rs

   1use super::{RandomizedTest, TestClient, TestError, TestServer, UserTestPlan};
   2use crate::{db::UserId, tests::run_randomized_test};
   3use anyhow::{anyhow, Result};
   4use async_trait::async_trait;
   5use call::ActiveCall;
   6use collections::{BTreeMap, HashMap};
   7use editor::Bias;
   8use fs::{FakeFs, Fs as _};
   9use futures::StreamExt;
  10use git::repository::GitFileStatus;
  11use gpui::{BackgroundExecutor, Model, TestAppContext};
  12use language::{
  13    range_to_lsp, FakeLspAdapter, Language, LanguageConfig, LanguageMatcher, PointUtf16,
  14};
  15use lsp::FakeLanguageServer;
  16use pretty_assertions::assert_eq;
  17use project::{
  18    search::SearchQuery, search::SearchResult, Project, ProjectPath, DEFAULT_COMPLETION_CONTEXT,
  19};
  20use rand::{
  21    distributions::{Alphanumeric, DistString},
  22    prelude::*,
  23};
  24use serde::{Deserialize, Serialize};
  25use std::{
  26    ops::{Deref, Range},
  27    path::{Path, PathBuf},
  28    rc::Rc,
  29    sync::Arc,
  30};
  31use util::ResultExt;
  32
  33#[gpui::test(
  34    iterations = 100,
  35    on_failure = "crate::tests::save_randomized_test_plan"
  36)]
  37async fn test_random_project_collaboration(
  38    cx: &mut TestAppContext,
  39    executor: BackgroundExecutor,
  40    rng: StdRng,
  41) {
  42    run_randomized_test::<ProjectCollaborationTest>(cx, executor, rng).await;
  43}
  44
  45#[derive(Clone, Debug, Serialize, Deserialize)]
  46enum ClientOperation {
  47    AcceptIncomingCall,
  48    RejectIncomingCall,
  49    LeaveCall,
  50    InviteContactToCall {
  51        user_id: UserId,
  52    },
  53    OpenLocalProject {
  54        first_root_name: String,
  55    },
  56    OpenRemoteProject {
  57        host_id: UserId,
  58        first_root_name: String,
  59    },
  60    AddWorktreeToProject {
  61        project_root_name: String,
  62        new_root_path: PathBuf,
  63    },
  64    CloseRemoteProject {
  65        project_root_name: String,
  66    },
  67    OpenBuffer {
  68        project_root_name: String,
  69        is_local: bool,
  70        full_path: PathBuf,
  71    },
  72    SearchProject {
  73        project_root_name: String,
  74        is_local: bool,
  75        query: String,
  76        detach: bool,
  77    },
  78    EditBuffer {
  79        project_root_name: String,
  80        is_local: bool,
  81        full_path: PathBuf,
  82        edits: Vec<(Range<usize>, Arc<str>)>,
  83    },
  84    CloseBuffer {
  85        project_root_name: String,
  86        is_local: bool,
  87        full_path: PathBuf,
  88    },
  89    SaveBuffer {
  90        project_root_name: String,
  91        is_local: bool,
  92        full_path: PathBuf,
  93        detach: bool,
  94    },
  95    RequestLspDataInBuffer {
  96        project_root_name: String,
  97        is_local: bool,
  98        full_path: PathBuf,
  99        offset: usize,
 100        kind: LspRequestKind,
 101        detach: bool,
 102    },
 103    CreateWorktreeEntry {
 104        project_root_name: String,
 105        is_local: bool,
 106        full_path: PathBuf,
 107        is_dir: bool,
 108    },
 109    WriteFsEntry {
 110        path: PathBuf,
 111        is_dir: bool,
 112        content: String,
 113    },
 114    GitOperation {
 115        operation: GitOperation,
 116    },
 117}
 118
 119#[derive(Clone, Debug, Serialize, Deserialize)]
 120enum GitOperation {
 121    WriteGitIndex {
 122        repo_path: PathBuf,
 123        contents: Vec<(PathBuf, String)>,
 124    },
 125    WriteGitBranch {
 126        repo_path: PathBuf,
 127        new_branch: Option<String>,
 128    },
 129    WriteGitStatuses {
 130        repo_path: PathBuf,
 131        statuses: Vec<(PathBuf, GitFileStatus)>,
 132        git_operation: bool,
 133    },
 134}
 135
 136#[derive(Clone, Debug, Serialize, Deserialize)]
 137enum LspRequestKind {
 138    Rename,
 139    Completion,
 140    CodeAction,
 141    Definition,
 142    Highlights,
 143}
 144
 145struct ProjectCollaborationTest;
 146
 147#[async_trait(?Send)]
 148impl RandomizedTest for ProjectCollaborationTest {
 149    type Operation = ClientOperation;
 150
 151    async fn initialize(server: &mut TestServer, users: &[UserTestPlan]) {
 152        let db = &server.app_state.db;
 153        for (ix, user_a) in users.iter().enumerate() {
 154            for user_b in &users[ix + 1..] {
 155                db.send_contact_request(user_a.user_id, user_b.user_id)
 156                    .await
 157                    .unwrap();
 158                db.respond_to_contact_request(user_b.user_id, user_a.user_id, true)
 159                    .await
 160                    .unwrap();
 161            }
 162        }
 163    }
 164
 165    fn generate_operation(
 166        client: &TestClient,
 167        rng: &mut StdRng,
 168        plan: &mut UserTestPlan,
 169        cx: &TestAppContext,
 170    ) -> ClientOperation {
 171        let call = cx.read(ActiveCall::global);
 172        loop {
 173            match rng.gen_range(0..100_u32) {
 174                // Mutate the call
 175                0..=29 => {
 176                    // Respond to an incoming call
 177                    if call.read_with(cx, |call, _| call.incoming().borrow().is_some()) {
 178                        break if rng.gen_bool(0.7) {
 179                            ClientOperation::AcceptIncomingCall
 180                        } else {
 181                            ClientOperation::RejectIncomingCall
 182                        };
 183                    }
 184
 185                    match rng.gen_range(0..100_u32) {
 186                        // Invite a contact to the current call
 187                        0..=70 => {
 188                            let available_contacts =
 189                                client.user_store().read_with(cx, |user_store, _| {
 190                                    user_store
 191                                        .contacts()
 192                                        .iter()
 193                                        .filter(|contact| contact.online && !contact.busy)
 194                                        .cloned()
 195                                        .collect::<Vec<_>>()
 196                                });
 197                            if !available_contacts.is_empty() {
 198                                let contact = available_contacts.choose(rng).unwrap();
 199                                break ClientOperation::InviteContactToCall {
 200                                    user_id: UserId(contact.user.id as i32),
 201                                };
 202                            }
 203                        }
 204
 205                        // Leave the current call
 206                        71.. => {
 207                            if plan.allow_client_disconnection
 208                                && call.read_with(cx, |call, _| call.room().is_some())
 209                            {
 210                                break ClientOperation::LeaveCall;
 211                            }
 212                        }
 213                    }
 214                }
 215
 216                // Mutate projects
 217                30..=59 => match rng.gen_range(0..100_u32) {
 218                    // Open a new project
 219                    0..=70 => {
 220                        // Open a remote project
 221                        if let Some(room) = call.read_with(cx, |call, _| call.room().cloned()) {
 222                            let existing_dev_server_project_ids = cx.read(|cx| {
 223                                client
 224                                    .dev_server_projects()
 225                                    .iter()
 226                                    .map(|p| p.read(cx).remote_id().unwrap())
 227                                    .collect::<Vec<_>>()
 228                            });
 229                            let new_dev_server_projects = room.read_with(cx, |room, _| {
 230                                room.remote_participants()
 231                                    .values()
 232                                    .flat_map(|participant| {
 233                                        participant.projects.iter().filter_map(|project| {
 234                                            if existing_dev_server_project_ids.contains(&project.id)
 235                                            {
 236                                                None
 237                                            } else {
 238                                                Some((
 239                                                    UserId::from_proto(participant.user.id),
 240                                                    project.worktree_root_names[0].clone(),
 241                                                ))
 242                                            }
 243                                        })
 244                                    })
 245                                    .collect::<Vec<_>>()
 246                            });
 247                            if !new_dev_server_projects.is_empty() {
 248                                let (host_id, first_root_name) =
 249                                    new_dev_server_projects.choose(rng).unwrap().clone();
 250                                break ClientOperation::OpenRemoteProject {
 251                                    host_id,
 252                                    first_root_name,
 253                                };
 254                            }
 255                        }
 256                        // Open a local project
 257                        else {
 258                            let first_root_name = plan.next_root_dir_name();
 259                            break ClientOperation::OpenLocalProject { first_root_name };
 260                        }
 261                    }
 262
 263                    // Close a remote project
 264                    71..=80 => {
 265                        if !client.dev_server_projects().is_empty() {
 266                            let project = client.dev_server_projects().choose(rng).unwrap().clone();
 267                            let first_root_name = root_name_for_project(&project, cx);
 268                            break ClientOperation::CloseRemoteProject {
 269                                project_root_name: first_root_name,
 270                            };
 271                        }
 272                    }
 273
 274                    // Mutate project worktrees
 275                    81.. => match rng.gen_range(0..100_u32) {
 276                        // Add a worktree to a local project
 277                        0..=50 => {
 278                            let Some(project) = client.local_projects().choose(rng).cloned() else {
 279                                continue;
 280                            };
 281                            let project_root_name = root_name_for_project(&project, cx);
 282                            let mut paths = client.fs().paths(false);
 283                            paths.remove(0);
 284                            let new_root_path = if paths.is_empty() || rng.gen() {
 285                                Path::new("/").join(&plan.next_root_dir_name())
 286                            } else {
 287                                paths.choose(rng).unwrap().clone()
 288                            };
 289                            break ClientOperation::AddWorktreeToProject {
 290                                project_root_name,
 291                                new_root_path,
 292                            };
 293                        }
 294
 295                        // Add an entry to a worktree
 296                        _ => {
 297                            let Some(project) = choose_random_project(client, rng) else {
 298                                continue;
 299                            };
 300                            let project_root_name = root_name_for_project(&project, cx);
 301                            let is_local =
 302                                project.read_with(cx, |project, _| project.is_local_or_ssh());
 303                            let worktree = project.read_with(cx, |project, cx| {
 304                                project
 305                                    .worktrees(cx)
 306                                    .filter(|worktree| {
 307                                        let worktree = worktree.read(cx);
 308                                        worktree.is_visible()
 309                                            && worktree.entries(false, 0).any(|e| e.is_file())
 310                                            && worktree.root_entry().map_or(false, |e| e.is_dir())
 311                                    })
 312                                    .choose(rng)
 313                            });
 314                            let Some(worktree) = worktree else { continue };
 315                            let is_dir = rng.gen::<bool>();
 316                            let mut full_path =
 317                                worktree.read_with(cx, |w, _| PathBuf::from(w.root_name()));
 318                            full_path.push(gen_file_name(rng));
 319                            if !is_dir {
 320                                full_path.set_extension("rs");
 321                            }
 322                            break ClientOperation::CreateWorktreeEntry {
 323                                project_root_name,
 324                                is_local,
 325                                full_path,
 326                                is_dir,
 327                            };
 328                        }
 329                    },
 330                },
 331
 332                // Query and mutate buffers
 333                60..=90 => {
 334                    let Some(project) = choose_random_project(client, rng) else {
 335                        continue;
 336                    };
 337                    let project_root_name = root_name_for_project(&project, cx);
 338                    let is_local = project.read_with(cx, |project, _| project.is_local_or_ssh());
 339
 340                    match rng.gen_range(0..100_u32) {
 341                        // Manipulate an existing buffer
 342                        0..=70 => {
 343                            let Some(buffer) = client
 344                                .buffers_for_project(&project)
 345                                .iter()
 346                                .choose(rng)
 347                                .cloned()
 348                            else {
 349                                continue;
 350                            };
 351
 352                            let full_path = buffer
 353                                .read_with(cx, |buffer, cx| buffer.file().unwrap().full_path(cx));
 354
 355                            match rng.gen_range(0..100_u32) {
 356                                // Close the buffer
 357                                0..=15 => {
 358                                    break ClientOperation::CloseBuffer {
 359                                        project_root_name,
 360                                        is_local,
 361                                        full_path,
 362                                    };
 363                                }
 364                                // Save the buffer
 365                                16..=29 if buffer.read_with(cx, |b, _| b.is_dirty()) => {
 366                                    let detach = rng.gen_bool(0.3);
 367                                    break ClientOperation::SaveBuffer {
 368                                        project_root_name,
 369                                        is_local,
 370                                        full_path,
 371                                        detach,
 372                                    };
 373                                }
 374                                // Edit the buffer
 375                                30..=69 => {
 376                                    let edits = buffer
 377                                        .read_with(cx, |buffer, _| buffer.get_random_edits(rng, 3));
 378                                    break ClientOperation::EditBuffer {
 379                                        project_root_name,
 380                                        is_local,
 381                                        full_path,
 382                                        edits,
 383                                    };
 384                                }
 385                                // Make an LSP request
 386                                _ => {
 387                                    let offset = buffer.read_with(cx, |buffer, _| {
 388                                        buffer.clip_offset(
 389                                            rng.gen_range(0..=buffer.len()),
 390                                            language::Bias::Left,
 391                                        )
 392                                    });
 393                                    let detach = rng.gen();
 394                                    break ClientOperation::RequestLspDataInBuffer {
 395                                        project_root_name,
 396                                        full_path,
 397                                        offset,
 398                                        is_local,
 399                                        kind: match rng.gen_range(0..5_u32) {
 400                                            0 => LspRequestKind::Rename,
 401                                            1 => LspRequestKind::Highlights,
 402                                            2 => LspRequestKind::Definition,
 403                                            3 => LspRequestKind::CodeAction,
 404                                            4.. => LspRequestKind::Completion,
 405                                        },
 406                                        detach,
 407                                    };
 408                                }
 409                            }
 410                        }
 411
 412                        71..=80 => {
 413                            let query = rng.gen_range('a'..='z').to_string();
 414                            let detach = rng.gen_bool(0.3);
 415                            break ClientOperation::SearchProject {
 416                                project_root_name,
 417                                is_local,
 418                                query,
 419                                detach,
 420                            };
 421                        }
 422
 423                        // Open a buffer
 424                        81.. => {
 425                            let worktree = project.read_with(cx, |project, cx| {
 426                                project
 427                                    .worktrees(cx)
 428                                    .filter(|worktree| {
 429                                        let worktree = worktree.read(cx);
 430                                        worktree.is_visible()
 431                                            && worktree.entries(false, 0).any(|e| e.is_file())
 432                                    })
 433                                    .choose(rng)
 434                            });
 435                            let Some(worktree) = worktree else { continue };
 436                            let full_path = worktree.read_with(cx, |worktree, _| {
 437                                let entry = worktree
 438                                    .entries(false, 0)
 439                                    .filter(|e| e.is_file())
 440                                    .choose(rng)
 441                                    .unwrap();
 442                                if entry.path.as_ref() == Path::new("") {
 443                                    Path::new(worktree.root_name()).into()
 444                                } else {
 445                                    Path::new(worktree.root_name()).join(&entry.path)
 446                                }
 447                            });
 448                            break ClientOperation::OpenBuffer {
 449                                project_root_name,
 450                                is_local,
 451                                full_path,
 452                            };
 453                        }
 454                    }
 455                }
 456
 457                // Update a git related action
 458                91..=95 => {
 459                    break ClientOperation::GitOperation {
 460                        operation: generate_git_operation(rng, client),
 461                    };
 462                }
 463
 464                // Create or update a file or directory
 465                96.. => {
 466                    let is_dir = rng.gen::<bool>();
 467                    let content;
 468                    let mut path;
 469                    let dir_paths = client.fs().directories(false);
 470
 471                    if is_dir {
 472                        content = String::new();
 473                        path = dir_paths.choose(rng).unwrap().clone();
 474                        path.push(gen_file_name(rng));
 475                    } else {
 476                        content = Alphanumeric.sample_string(rng, 16);
 477
 478                        // Create a new file or overwrite an existing file
 479                        let file_paths = client.fs().files();
 480                        if file_paths.is_empty() || rng.gen_bool(0.5) {
 481                            path = dir_paths.choose(rng).unwrap().clone();
 482                            path.push(gen_file_name(rng));
 483                            path.set_extension("rs");
 484                        } else {
 485                            path = file_paths.choose(rng).unwrap().clone()
 486                        };
 487                    }
 488                    break ClientOperation::WriteFsEntry {
 489                        path,
 490                        is_dir,
 491                        content,
 492                    };
 493                }
 494            }
 495        }
 496    }
 497
 498    async fn apply_operation(
 499        client: &TestClient,
 500        operation: ClientOperation,
 501        cx: &mut TestAppContext,
 502    ) -> Result<(), TestError> {
 503        match operation {
 504            ClientOperation::AcceptIncomingCall => {
 505                let active_call = cx.read(ActiveCall::global);
 506                if active_call.read_with(cx, |call, _| call.incoming().borrow().is_none()) {
 507                    Err(TestError::Inapplicable)?;
 508                }
 509
 510                log::info!("{}: accepting incoming call", client.username);
 511                active_call
 512                    .update(cx, |call, cx| call.accept_incoming(cx))
 513                    .await?;
 514            }
 515
 516            ClientOperation::RejectIncomingCall => {
 517                let active_call = cx.read(ActiveCall::global);
 518                if active_call.read_with(cx, |call, _| call.incoming().borrow().is_none()) {
 519                    Err(TestError::Inapplicable)?;
 520                }
 521
 522                log::info!("{}: declining incoming call", client.username);
 523                active_call.update(cx, |call, cx| call.decline_incoming(cx))?;
 524            }
 525
 526            ClientOperation::LeaveCall => {
 527                let active_call = cx.read(ActiveCall::global);
 528                if active_call.read_with(cx, |call, _| call.room().is_none()) {
 529                    Err(TestError::Inapplicable)?;
 530                }
 531
 532                log::info!("{}: hanging up", client.username);
 533                active_call.update(cx, |call, cx| call.hang_up(cx)).await?;
 534            }
 535
 536            ClientOperation::InviteContactToCall { user_id } => {
 537                let active_call = cx.read(ActiveCall::global);
 538
 539                log::info!("{}: inviting {}", client.username, user_id,);
 540                active_call
 541                    .update(cx, |call, cx| call.invite(user_id.to_proto(), None, cx))
 542                    .await
 543                    .log_err();
 544            }
 545
 546            ClientOperation::OpenLocalProject { first_root_name } => {
 547                log::info!(
 548                    "{}: opening local project at {:?}",
 549                    client.username,
 550                    first_root_name
 551                );
 552
 553                let root_path = Path::new("/").join(&first_root_name);
 554                client.fs().create_dir(&root_path).await.unwrap();
 555                client
 556                    .fs()
 557                    .create_file(&root_path.join("main.rs"), Default::default())
 558                    .await
 559                    .unwrap();
 560                let project = client.build_local_project(root_path, cx).await.0;
 561                ensure_project_shared(&project, client, cx).await;
 562                client.local_projects_mut().push(project.clone());
 563            }
 564
 565            ClientOperation::AddWorktreeToProject {
 566                project_root_name,
 567                new_root_path,
 568            } => {
 569                let project = project_for_root_name(client, &project_root_name, cx)
 570                    .ok_or(TestError::Inapplicable)?;
 571
 572                log::info!(
 573                    "{}: finding/creating local worktree at {:?} to project with root path {}",
 574                    client.username,
 575                    new_root_path,
 576                    project_root_name
 577                );
 578
 579                ensure_project_shared(&project, client, cx).await;
 580                if !client.fs().paths(false).contains(&new_root_path) {
 581                    client.fs().create_dir(&new_root_path).await.unwrap();
 582                }
 583                project
 584                    .update(cx, |project, cx| {
 585                        project.find_or_create_worktree(&new_root_path, true, cx)
 586                    })
 587                    .await
 588                    .unwrap();
 589            }
 590
 591            ClientOperation::CloseRemoteProject { project_root_name } => {
 592                let project = project_for_root_name(client, &project_root_name, cx)
 593                    .ok_or(TestError::Inapplicable)?;
 594
 595                log::info!(
 596                    "{}: closing remote project with root path {}",
 597                    client.username,
 598                    project_root_name,
 599                );
 600
 601                let ix = client
 602                    .dev_server_projects()
 603                    .iter()
 604                    .position(|p| p == &project)
 605                    .unwrap();
 606                cx.update(|_| {
 607                    client.dev_server_projects_mut().remove(ix);
 608                    client.buffers().retain(|p, _| *p != project);
 609                    drop(project);
 610                });
 611            }
 612
 613            ClientOperation::OpenRemoteProject {
 614                host_id,
 615                first_root_name,
 616            } => {
 617                let active_call = cx.read(ActiveCall::global);
 618                let project = active_call
 619                    .update(cx, |call, cx| {
 620                        let room = call.room().cloned()?;
 621                        let participant = room
 622                            .read(cx)
 623                            .remote_participants()
 624                            .get(&host_id.to_proto())?;
 625                        let project_id = participant
 626                            .projects
 627                            .iter()
 628                            .find(|project| project.worktree_root_names[0] == first_root_name)?
 629                            .id;
 630                        Some(room.update(cx, |room, cx| {
 631                            room.join_project(
 632                                project_id,
 633                                client.language_registry().clone(),
 634                                FakeFs::new(cx.background_executor().clone()),
 635                                cx,
 636                            )
 637                        }))
 638                    })
 639                    .ok_or(TestError::Inapplicable)?;
 640
 641                log::info!(
 642                    "{}: joining remote project of user {}, root name {}",
 643                    client.username,
 644                    host_id,
 645                    first_root_name,
 646                );
 647
 648                let project = project.await?;
 649                client.dev_server_projects_mut().push(project.clone());
 650            }
 651
 652            ClientOperation::CreateWorktreeEntry {
 653                project_root_name,
 654                is_local,
 655                full_path,
 656                is_dir,
 657            } => {
 658                let project = project_for_root_name(client, &project_root_name, cx)
 659                    .ok_or(TestError::Inapplicable)?;
 660                let project_path = project_path_for_full_path(&project, &full_path, cx)
 661                    .ok_or(TestError::Inapplicable)?;
 662
 663                log::info!(
 664                    "{}: creating {} at path {:?} in {} project {}",
 665                    client.username,
 666                    if is_dir { "dir" } else { "file" },
 667                    full_path,
 668                    if is_local { "local" } else { "remote" },
 669                    project_root_name,
 670                );
 671
 672                ensure_project_shared(&project, client, cx).await;
 673                project
 674                    .update(cx, |p, cx| p.create_entry(project_path, is_dir, cx))
 675                    .await?;
 676            }
 677
 678            ClientOperation::OpenBuffer {
 679                project_root_name,
 680                is_local,
 681                full_path,
 682            } => {
 683                let project = project_for_root_name(client, &project_root_name, cx)
 684                    .ok_or(TestError::Inapplicable)?;
 685                let project_path = project_path_for_full_path(&project, &full_path, cx)
 686                    .ok_or(TestError::Inapplicable)?;
 687
 688                log::info!(
 689                    "{}: opening buffer {:?} in {} project {}",
 690                    client.username,
 691                    full_path,
 692                    if is_local { "local" } else { "remote" },
 693                    project_root_name,
 694                );
 695
 696                ensure_project_shared(&project, client, cx).await;
 697                let buffer = project
 698                    .update(cx, |project, cx| project.open_buffer(project_path, cx))
 699                    .await?;
 700                client.buffers_for_project(&project).insert(buffer);
 701            }
 702
 703            ClientOperation::EditBuffer {
 704                project_root_name,
 705                is_local,
 706                full_path,
 707                edits,
 708            } => {
 709                let project = project_for_root_name(client, &project_root_name, cx)
 710                    .ok_or(TestError::Inapplicable)?;
 711                let buffer = buffer_for_full_path(client, &project, &full_path, cx)
 712                    .ok_or(TestError::Inapplicable)?;
 713
 714                log::info!(
 715                    "{}: editing buffer {:?} in {} project {} with {:?}",
 716                    client.username,
 717                    full_path,
 718                    if is_local { "local" } else { "remote" },
 719                    project_root_name,
 720                    edits
 721                );
 722
 723                ensure_project_shared(&project, client, cx).await;
 724                buffer.update(cx, |buffer, cx| {
 725                    let snapshot = buffer.snapshot();
 726                    buffer.edit(
 727                        edits.into_iter().map(|(range, text)| {
 728                            let start = snapshot.clip_offset(range.start, Bias::Left);
 729                            let end = snapshot.clip_offset(range.end, Bias::Right);
 730                            (start..end, text)
 731                        }),
 732                        None,
 733                        cx,
 734                    );
 735                });
 736            }
 737
 738            ClientOperation::CloseBuffer {
 739                project_root_name,
 740                is_local,
 741                full_path,
 742            } => {
 743                let project = project_for_root_name(client, &project_root_name, cx)
 744                    .ok_or(TestError::Inapplicable)?;
 745                let buffer = buffer_for_full_path(client, &project, &full_path, cx)
 746                    .ok_or(TestError::Inapplicable)?;
 747
 748                log::info!(
 749                    "{}: closing buffer {:?} in {} project {}",
 750                    client.username,
 751                    full_path,
 752                    if is_local { "local" } else { "remote" },
 753                    project_root_name
 754                );
 755
 756                ensure_project_shared(&project, client, cx).await;
 757                cx.update(|_| {
 758                    client.buffers_for_project(&project).remove(&buffer);
 759                    drop(buffer);
 760                });
 761            }
 762
 763            ClientOperation::SaveBuffer {
 764                project_root_name,
 765                is_local,
 766                full_path,
 767                detach,
 768            } => {
 769                let project = project_for_root_name(client, &project_root_name, cx)
 770                    .ok_or(TestError::Inapplicable)?;
 771                let buffer = buffer_for_full_path(client, &project, &full_path, cx)
 772                    .ok_or(TestError::Inapplicable)?;
 773
 774                log::info!(
 775                    "{}: saving buffer {:?} in {} project {}, {}",
 776                    client.username,
 777                    full_path,
 778                    if is_local { "local" } else { "remote" },
 779                    project_root_name,
 780                    if detach { "detaching" } else { "awaiting" }
 781                );
 782
 783                ensure_project_shared(&project, client, cx).await;
 784                let requested_version = buffer.read_with(cx, |buffer, _| buffer.version());
 785                let save =
 786                    project.update(cx, |project, cx| project.save_buffer(buffer.clone(), cx));
 787                let save = cx.spawn(|cx| async move {
 788                    save.await
 789                        .map_err(|err| anyhow!("save request failed: {:?}", err))?;
 790                    assert!(buffer
 791                        .read_with(&cx, |buffer, _| { buffer.saved_version().to_owned() })
 792                        .expect("App should not be dropped")
 793                        .observed_all(&requested_version));
 794                    anyhow::Ok(())
 795                });
 796                if detach {
 797                    cx.update(|cx| save.detach_and_log_err(cx));
 798                } else {
 799                    save.await?;
 800                }
 801            }
 802
 803            ClientOperation::RequestLspDataInBuffer {
 804                project_root_name,
 805                is_local,
 806                full_path,
 807                offset,
 808                kind,
 809                detach,
 810            } => {
 811                let project = project_for_root_name(client, &project_root_name, cx)
 812                    .ok_or(TestError::Inapplicable)?;
 813                let buffer = buffer_for_full_path(client, &project, &full_path, cx)
 814                    .ok_or(TestError::Inapplicable)?;
 815
 816                log::info!(
 817                    "{}: request LSP {:?} for buffer {:?} in {} project {}, {}",
 818                    client.username,
 819                    kind,
 820                    full_path,
 821                    if is_local { "local" } else { "remote" },
 822                    project_root_name,
 823                    if detach { "detaching" } else { "awaiting" }
 824                );
 825
 826                use futures::{FutureExt as _, TryFutureExt as _};
 827                let offset = buffer.read_with(cx, |b, _| b.clip_offset(offset, Bias::Left));
 828
 829                let process_lsp_request = project.update(cx, |project, cx| match kind {
 830                    LspRequestKind::Rename => project
 831                        .prepare_rename(buffer, offset, cx)
 832                        .map_ok(|_| ())
 833                        .boxed(),
 834                    LspRequestKind::Completion => project
 835                        .completions(&buffer, offset, DEFAULT_COMPLETION_CONTEXT, cx)
 836                        .map_ok(|_| ())
 837                        .boxed(),
 838                    LspRequestKind::CodeAction => project
 839                        .code_actions(&buffer, offset..offset, cx)
 840                        .map(|_| Ok(()))
 841                        .boxed(),
 842                    LspRequestKind::Definition => project
 843                        .definition(&buffer, offset, cx)
 844                        .map_ok(|_| ())
 845                        .boxed(),
 846                    LspRequestKind::Highlights => project
 847                        .document_highlights(&buffer, offset, cx)
 848                        .map_ok(|_| ())
 849                        .boxed(),
 850                });
 851                let request = cx.foreground_executor().spawn(process_lsp_request);
 852                if detach {
 853                    request.detach();
 854                } else {
 855                    request.await?;
 856                }
 857            }
 858
 859            ClientOperation::SearchProject {
 860                project_root_name,
 861                is_local,
 862                query,
 863                detach,
 864            } => {
 865                let project = project_for_root_name(client, &project_root_name, cx)
 866                    .ok_or(TestError::Inapplicable)?;
 867
 868                log::info!(
 869                    "{}: search {} project {} for {:?}, {}",
 870                    client.username,
 871                    if is_local { "local" } else { "remote" },
 872                    project_root_name,
 873                    query,
 874                    if detach { "detaching" } else { "awaiting" }
 875                );
 876
 877                let mut search = project.update(cx, |project, cx| {
 878                    project.search(
 879                        SearchQuery::text(
 880                            query,
 881                            false,
 882                            false,
 883                            false,
 884                            Default::default(),
 885                            Default::default(),
 886                        )
 887                        .unwrap(),
 888                        cx,
 889                    )
 890                });
 891                drop(project);
 892                let search = cx.executor().spawn(async move {
 893                    let mut results = HashMap::default();
 894                    while let Some(result) = search.next().await {
 895                        if let SearchResult::Buffer { buffer, ranges } = result {
 896                            results.entry(buffer).or_insert(ranges);
 897                        }
 898                    }
 899                    results
 900                });
 901                search.await;
 902            }
 903
 904            ClientOperation::WriteFsEntry {
 905                path,
 906                is_dir,
 907                content,
 908            } => {
 909                if !client
 910                    .fs()
 911                    .directories(false)
 912                    .contains(&path.parent().unwrap().to_owned())
 913                {
 914                    return Err(TestError::Inapplicable);
 915                }
 916
 917                if is_dir {
 918                    log::info!("{}: creating dir at {:?}", client.username, path);
 919                    client.fs().create_dir(&path).await.unwrap();
 920                } else {
 921                    let exists = client.fs().metadata(&path).await?.is_some();
 922                    let verb = if exists { "updating" } else { "creating" };
 923                    log::info!("{}: {} file at {:?}", verb, client.username, path);
 924
 925                    client
 926                        .fs()
 927                        .save(&path, &content.as_str().into(), text::LineEnding::Unix)
 928                        .await
 929                        .unwrap();
 930                }
 931            }
 932
 933            ClientOperation::GitOperation { operation } => match operation {
 934                GitOperation::WriteGitIndex {
 935                    repo_path,
 936                    contents,
 937                } => {
 938                    if !client.fs().directories(false).contains(&repo_path) {
 939                        return Err(TestError::Inapplicable);
 940                    }
 941
 942                    for (path, _) in contents.iter() {
 943                        if !client.fs().files().contains(&repo_path.join(path)) {
 944                            return Err(TestError::Inapplicable);
 945                        }
 946                    }
 947
 948                    log::info!(
 949                        "{}: writing git index for repo {:?}: {:?}",
 950                        client.username,
 951                        repo_path,
 952                        contents
 953                    );
 954
 955                    let dot_git_dir = repo_path.join(".git");
 956                    let contents = contents
 957                        .iter()
 958                        .map(|(path, contents)| (path.as_path(), contents.clone()))
 959                        .collect::<Vec<_>>();
 960                    if client.fs().metadata(&dot_git_dir).await?.is_none() {
 961                        client.fs().create_dir(&dot_git_dir).await?;
 962                    }
 963                    client.fs().set_index_for_repo(&dot_git_dir, &contents);
 964                }
 965                GitOperation::WriteGitBranch {
 966                    repo_path,
 967                    new_branch,
 968                } => {
 969                    if !client.fs().directories(false).contains(&repo_path) {
 970                        return Err(TestError::Inapplicable);
 971                    }
 972
 973                    log::info!(
 974                        "{}: writing git branch for repo {:?}: {:?}",
 975                        client.username,
 976                        repo_path,
 977                        new_branch
 978                    );
 979
 980                    let dot_git_dir = repo_path.join(".git");
 981                    if client.fs().metadata(&dot_git_dir).await?.is_none() {
 982                        client.fs().create_dir(&dot_git_dir).await?;
 983                    }
 984                    client
 985                        .fs()
 986                        .set_branch_name(&dot_git_dir, new_branch.clone());
 987                }
 988                GitOperation::WriteGitStatuses {
 989                    repo_path,
 990                    statuses,
 991                    git_operation,
 992                } => {
 993                    if !client.fs().directories(false).contains(&repo_path) {
 994                        return Err(TestError::Inapplicable);
 995                    }
 996                    for (path, _) in statuses.iter() {
 997                        if !client.fs().files().contains(&repo_path.join(path)) {
 998                            return Err(TestError::Inapplicable);
 999                        }
1000                    }
1001
1002                    log::info!(
1003                        "{}: writing git statuses for repo {:?}: {:?}",
1004                        client.username,
1005                        repo_path,
1006                        statuses
1007                    );
1008
1009                    let dot_git_dir = repo_path.join(".git");
1010
1011                    let statuses = statuses
1012                        .iter()
1013                        .map(|(path, val)| (path.as_path(), *val))
1014                        .collect::<Vec<_>>();
1015
1016                    if client.fs().metadata(&dot_git_dir).await?.is_none() {
1017                        client.fs().create_dir(&dot_git_dir).await?;
1018                    }
1019
1020                    if git_operation {
1021                        client.fs().set_status_for_repo_via_git_operation(
1022                            &dot_git_dir,
1023                            statuses.as_slice(),
1024                        );
1025                    } else {
1026                        client.fs().set_status_for_repo_via_working_copy_change(
1027                            &dot_git_dir,
1028                            statuses.as_slice(),
1029                        );
1030                    }
1031                }
1032            },
1033        }
1034        Ok(())
1035    }
1036
1037    async fn on_client_added(client: &Rc<TestClient>, _: &mut TestAppContext) {
1038        client.language_registry().add(Arc::new(Language::new(
1039            LanguageConfig {
1040                name: "Rust".into(),
1041                matcher: LanguageMatcher {
1042                    path_suffixes: vec!["rs".to_string()],
1043                    ..Default::default()
1044                },
1045                ..Default::default()
1046            },
1047            None,
1048        )));
1049        client.language_registry().register_fake_lsp_adapter(
1050            "Rust",
1051            FakeLspAdapter {
1052                name: "the-fake-language-server",
1053                capabilities: lsp::LanguageServer::full_capabilities(),
1054                initializer: Some(Box::new({
1055                    let fs = client.app_state.fs.clone();
1056                    move |fake_server: &mut FakeLanguageServer| {
1057                        fake_server.handle_request::<lsp::request::Completion, _, _>(
1058                            |_, _| async move {
1059                                Ok(Some(lsp::CompletionResponse::Array(vec![
1060                                    lsp::CompletionItem {
1061                                        text_edit: Some(lsp::CompletionTextEdit::Edit(
1062                                            lsp::TextEdit {
1063                                                range: lsp::Range::new(
1064                                                    lsp::Position::new(0, 0),
1065                                                    lsp::Position::new(0, 0),
1066                                                ),
1067                                                new_text: "the-new-text".to_string(),
1068                                            },
1069                                        )),
1070                                        ..Default::default()
1071                                    },
1072                                ])))
1073                            },
1074                        );
1075
1076                        fake_server.handle_request::<lsp::request::CodeActionRequest, _, _>(
1077                            |_, _| async move {
1078                                Ok(Some(vec![lsp::CodeActionOrCommand::CodeAction(
1079                                    lsp::CodeAction {
1080                                        title: "the-code-action".to_string(),
1081                                        ..Default::default()
1082                                    },
1083                                )]))
1084                            },
1085                        );
1086
1087                        fake_server.handle_request::<lsp::request::PrepareRenameRequest, _, _>(
1088                            |params, _| async move {
1089                                Ok(Some(lsp::PrepareRenameResponse::Range(lsp::Range::new(
1090                                    params.position,
1091                                    params.position,
1092                                ))))
1093                            },
1094                        );
1095
1096                        fake_server.handle_request::<lsp::request::GotoDefinition, _, _>({
1097                            let fs = fs.clone();
1098                            move |_, cx| {
1099                                let background = cx.background_executor();
1100                                let mut rng = background.rng();
1101                                let count = rng.gen_range::<usize, _>(1..3);
1102                                let files = fs.as_fake().files();
1103                                let files = (0..count)
1104                                    .map(|_| files.choose(&mut rng).unwrap().clone())
1105                                    .collect::<Vec<_>>();
1106                                async move {
1107                                    log::info!("LSP: Returning definitions in files {:?}", &files);
1108                                    Ok(Some(lsp::GotoDefinitionResponse::Array(
1109                                        files
1110                                            .into_iter()
1111                                            .map(|file| lsp::Location {
1112                                                uri: lsp::Url::from_file_path(file).unwrap(),
1113                                                range: Default::default(),
1114                                            })
1115                                            .collect(),
1116                                    )))
1117                                }
1118                            }
1119                        });
1120
1121                        fake_server.handle_request::<lsp::request::DocumentHighlightRequest, _, _>(
1122                            move |_, cx| {
1123                                let mut highlights = Vec::new();
1124                                let background = cx.background_executor();
1125                                let mut rng = background.rng();
1126
1127                                let highlight_count = rng.gen_range(1..=5);
1128                                for _ in 0..highlight_count {
1129                                    let start_row = rng.gen_range(0..100);
1130                                    let start_column = rng.gen_range(0..100);
1131                                    let end_row = rng.gen_range(0..100);
1132                                    let end_column = rng.gen_range(0..100);
1133                                    let start = PointUtf16::new(start_row, start_column);
1134                                    let end = PointUtf16::new(end_row, end_column);
1135                                    let range = if start > end { end..start } else { start..end };
1136                                    highlights.push(lsp::DocumentHighlight {
1137                                        range: range_to_lsp(range.clone()),
1138                                        kind: Some(lsp::DocumentHighlightKind::READ),
1139                                    });
1140                                }
1141                                highlights.sort_unstable_by_key(|highlight| {
1142                                    (highlight.range.start, highlight.range.end)
1143                                });
1144                                async move { Ok(Some(highlights)) }
1145                            },
1146                        );
1147                    }
1148                })),
1149                ..Default::default()
1150            },
1151        );
1152    }
1153
1154    async fn on_quiesce(_: &mut TestServer, clients: &mut [(Rc<TestClient>, TestAppContext)]) {
1155        for (client, client_cx) in clients.iter() {
1156            for guest_project in client.dev_server_projects().iter() {
1157                guest_project.read_with(client_cx, |guest_project, cx| {
1158                        let host_project = clients.iter().find_map(|(client, cx)| {
1159                            let project = client
1160                                .local_projects()
1161                                .iter()
1162                                .find(|host_project| {
1163                                    host_project.read_with(cx, |host_project, _| {
1164                                        host_project.remote_id() == guest_project.remote_id()
1165                                    })
1166                                })?
1167                                .clone();
1168                            Some((project, cx))
1169                        });
1170
1171                        if !guest_project.is_disconnected() {
1172                            if let Some((host_project, host_cx)) = host_project {
1173                                let host_worktree_snapshots =
1174                                    host_project.read_with(host_cx, |host_project, cx| {
1175                                        host_project
1176                                            .worktrees(cx)
1177                                            .map(|worktree| {
1178                                                let worktree = worktree.read(cx);
1179                                                (worktree.id(), worktree.snapshot())
1180                                            })
1181                                            .collect::<BTreeMap<_, _>>()
1182                                    });
1183                                let guest_worktree_snapshots = guest_project
1184                                    .worktrees(cx)
1185                                    .map(|worktree| {
1186                                        let worktree = worktree.read(cx);
1187                                        (worktree.id(), worktree.snapshot())
1188                                    })
1189                                    .collect::<BTreeMap<_, _>>();
1190
1191                                assert_eq!(
1192                                    guest_worktree_snapshots.values().map(|w| w.abs_path()).collect::<Vec<_>>(),
1193                                    host_worktree_snapshots.values().map(|w| w.abs_path()).collect::<Vec<_>>(),
1194                                    "{} has different worktrees than the host for project {:?}",
1195                                    client.username, guest_project.remote_id(),
1196                                );
1197
1198                                for (id, host_snapshot) in &host_worktree_snapshots {
1199                                    let guest_snapshot = &guest_worktree_snapshots[id];
1200                                    assert_eq!(
1201                                        guest_snapshot.root_name(),
1202                                        host_snapshot.root_name(),
1203                                        "{} has different root name than the host for worktree {}, project {:?}",
1204                                        client.username,
1205                                        id,
1206                                        guest_project.remote_id(),
1207                                    );
1208                                    assert_eq!(
1209                                        guest_snapshot.abs_path(),
1210                                        host_snapshot.abs_path(),
1211                                        "{} has different abs path than the host for worktree {}, project: {:?}",
1212                                        client.username,
1213                                        id,
1214                                        guest_project.remote_id(),
1215                                    );
1216                                    assert_eq!(
1217                                        guest_snapshot.entries(false, 0).collect::<Vec<_>>(),
1218                                        host_snapshot.entries(false, 0).collect::<Vec<_>>(),
1219                                        "{} has different snapshot than the host for worktree {:?} ({:?}) and project {:?}",
1220                                        client.username,
1221                                        host_snapshot.abs_path(),
1222                                        id,
1223                                        guest_project.remote_id(),
1224                                    );
1225                                    assert_eq!(guest_snapshot.repositories().collect::<Vec<_>>(), host_snapshot.repositories().collect::<Vec<_>>(),
1226                                        "{} has different repositories than the host for worktree {:?} and project {:?}",
1227                                        client.username,
1228                                        host_snapshot.abs_path(),
1229                                        guest_project.remote_id(),
1230                                    );
1231                                    assert_eq!(guest_snapshot.scan_id(), host_snapshot.scan_id(),
1232                                        "{} has different scan id than the host for worktree {:?} and project {:?}",
1233                                        client.username,
1234                                        host_snapshot.abs_path(),
1235                                        guest_project.remote_id(),
1236                                    );
1237                                }
1238                            }
1239                        }
1240
1241                        for buffer in guest_project.opened_buffers(cx) {
1242                            let buffer = buffer.read(cx);
1243                            assert_eq!(
1244                                buffer.deferred_ops_len(),
1245                                0,
1246                                "{} has deferred operations for buffer {:?} in project {:?}",
1247                                client.username,
1248                                buffer.file().unwrap().full_path(cx),
1249                                guest_project.remote_id(),
1250                            );
1251                        }
1252                    });
1253            }
1254
1255            let buffers = client.buffers().clone();
1256            for (guest_project, guest_buffers) in &buffers {
1257                let project_id = if guest_project.read_with(client_cx, |project, _| {
1258                    project.is_local_or_ssh() || project.is_disconnected()
1259                }) {
1260                    continue;
1261                } else {
1262                    guest_project
1263                        .read_with(client_cx, |project, _| project.remote_id())
1264                        .unwrap()
1265                };
1266                let guest_user_id = client.user_id().unwrap();
1267
1268                let host_project = clients.iter().find_map(|(client, cx)| {
1269                    let project = client
1270                        .local_projects()
1271                        .iter()
1272                        .find(|host_project| {
1273                            host_project.read_with(cx, |host_project, _| {
1274                                host_project.remote_id() == Some(project_id)
1275                            })
1276                        })?
1277                        .clone();
1278                    Some((client.user_id().unwrap(), project, cx))
1279                });
1280
1281                let (host_user_id, host_project, host_cx) =
1282                    if let Some((host_user_id, host_project, host_cx)) = host_project {
1283                        (host_user_id, host_project, host_cx)
1284                    } else {
1285                        continue;
1286                    };
1287
1288                for guest_buffer in guest_buffers {
1289                    let buffer_id =
1290                        guest_buffer.read_with(client_cx, |buffer, _| buffer.remote_id());
1291                    let host_buffer = host_project.read_with(host_cx, |project, cx| {
1292                        project.buffer_for_id(buffer_id, cx).unwrap_or_else(|| {
1293                            panic!(
1294                                "host does not have buffer for guest:{}, peer:{:?}, id:{}",
1295                                client.username,
1296                                client.peer_id(),
1297                                buffer_id
1298                            )
1299                        })
1300                    });
1301                    let path = host_buffer
1302                        .read_with(host_cx, |buffer, cx| buffer.file().unwrap().full_path(cx));
1303
1304                    assert_eq!(
1305                        guest_buffer.read_with(client_cx, |buffer, _| buffer.deferred_ops_len()),
1306                        0,
1307                        "{}, buffer {}, path {:?} has deferred operations",
1308                        client.username,
1309                        buffer_id,
1310                        path,
1311                    );
1312                    assert_eq!(
1313                        guest_buffer.read_with(client_cx, |buffer, _| buffer.text()),
1314                        host_buffer.read_with(host_cx, |buffer, _| buffer.text()),
1315                        "{}, buffer {}, path {:?}, differs from the host's buffer",
1316                        client.username,
1317                        buffer_id,
1318                        path
1319                    );
1320
1321                    let host_file = host_buffer.read_with(host_cx, |b, _| b.file().cloned());
1322                    let guest_file = guest_buffer.read_with(client_cx, |b, _| b.file().cloned());
1323                    match (host_file, guest_file) {
1324                        (Some(host_file), Some(guest_file)) => {
1325                            assert_eq!(guest_file.path(), host_file.path());
1326                            assert_eq!(guest_file.is_deleted(), host_file.is_deleted());
1327                            assert_eq!(
1328                                guest_file.mtime(),
1329                                host_file.mtime(),
1330                                "guest {} mtime does not match host {} for path {:?} in project {}",
1331                                guest_user_id,
1332                                host_user_id,
1333                                guest_file.path(),
1334                                project_id,
1335                            );
1336                        }
1337                        (None, None) => {}
1338                        (None, _) => panic!("host's file is None, guest's isn't"),
1339                        (_, None) => panic!("guest's file is None, hosts's isn't"),
1340                    }
1341
1342                    let host_diff_base = host_buffer
1343                        .read_with(host_cx, |b, _| b.diff_base().map(ToString::to_string));
1344                    let guest_diff_base = guest_buffer
1345                        .read_with(client_cx, |b, _| b.diff_base().map(ToString::to_string));
1346                    assert_eq!(
1347                            guest_diff_base, host_diff_base,
1348                            "guest {} diff base does not match host's for path {path:?} in project {project_id}",
1349                            client.username
1350                        );
1351
1352                    let host_saved_version =
1353                        host_buffer.read_with(host_cx, |b, _| b.saved_version().clone());
1354                    let guest_saved_version =
1355                        guest_buffer.read_with(client_cx, |b, _| b.saved_version().clone());
1356                    assert_eq!(
1357                            guest_saved_version, host_saved_version,
1358                            "guest {} saved version does not match host's for path {path:?} in project {project_id}",
1359                            client.username
1360                        );
1361
1362                    let host_is_dirty = host_buffer.read_with(host_cx, |b, _| b.is_dirty());
1363                    let guest_is_dirty = guest_buffer.read_with(client_cx, |b, _| b.is_dirty());
1364                    assert_eq!(
1365                            guest_is_dirty, host_is_dirty,
1366                            "guest {} dirty state does not match host's for path {path:?} in project {project_id}",
1367                            client.username
1368                        );
1369
1370                    let host_saved_mtime = host_buffer.read_with(host_cx, |b, _| b.saved_mtime());
1371                    let guest_saved_mtime =
1372                        guest_buffer.read_with(client_cx, |b, _| b.saved_mtime());
1373                    assert_eq!(
1374                            guest_saved_mtime, host_saved_mtime,
1375                            "guest {} saved mtime does not match host's for path {path:?} in project {project_id}",
1376                            client.username
1377                        );
1378
1379                    let host_is_dirty = host_buffer.read_with(host_cx, |b, _| b.is_dirty());
1380                    let guest_is_dirty = guest_buffer.read_with(client_cx, |b, _| b.is_dirty());
1381                    assert_eq!(guest_is_dirty, host_is_dirty,
1382                            "guest {} dirty status does not match host's for path {path:?} in project {project_id}",
1383                            client.username
1384                        );
1385
1386                    let host_has_conflict = host_buffer.read_with(host_cx, |b, _| b.has_conflict());
1387                    let guest_has_conflict =
1388                        guest_buffer.read_with(client_cx, |b, _| b.has_conflict());
1389                    assert_eq!(guest_has_conflict, host_has_conflict,
1390                            "guest {} conflict status does not match host's for path {path:?} in project {project_id}",
1391                            client.username
1392                        );
1393                }
1394            }
1395        }
1396    }
1397}
1398
1399fn generate_git_operation(rng: &mut StdRng, client: &TestClient) -> GitOperation {
1400    fn generate_file_paths(
1401        repo_path: &Path,
1402        rng: &mut StdRng,
1403        client: &TestClient,
1404    ) -> Vec<PathBuf> {
1405        let mut paths = client
1406            .fs()
1407            .files()
1408            .into_iter()
1409            .filter(|path| path.starts_with(repo_path))
1410            .collect::<Vec<_>>();
1411
1412        let count = rng.gen_range(0..=paths.len());
1413        paths.shuffle(rng);
1414        paths.truncate(count);
1415
1416        paths
1417            .iter()
1418            .map(|path| path.strip_prefix(repo_path).unwrap().to_path_buf())
1419            .collect::<Vec<_>>()
1420    }
1421
1422    let repo_path = client.fs().directories(false).choose(rng).unwrap().clone();
1423
1424    match rng.gen_range(0..100_u32) {
1425        0..=25 => {
1426            let file_paths = generate_file_paths(&repo_path, rng, client);
1427
1428            let contents = file_paths
1429                .into_iter()
1430                .map(|path| (path, Alphanumeric.sample_string(rng, 16)))
1431                .collect();
1432
1433            GitOperation::WriteGitIndex {
1434                repo_path,
1435                contents,
1436            }
1437        }
1438        26..=63 => {
1439            let new_branch = (rng.gen_range(0..10) > 3).then(|| Alphanumeric.sample_string(rng, 8));
1440
1441            GitOperation::WriteGitBranch {
1442                repo_path,
1443                new_branch,
1444            }
1445        }
1446        64..=100 => {
1447            let file_paths = generate_file_paths(&repo_path, rng, client);
1448
1449            let statuses = file_paths
1450                .into_iter()
1451                .map(|paths| {
1452                    (
1453                        paths,
1454                        match rng.gen_range(0..3_u32) {
1455                            0 => GitFileStatus::Added,
1456                            1 => GitFileStatus::Modified,
1457                            2 => GitFileStatus::Conflict,
1458                            _ => unreachable!(),
1459                        },
1460                    )
1461                })
1462                .collect::<Vec<_>>();
1463
1464            let git_operation = rng.gen::<bool>();
1465
1466            GitOperation::WriteGitStatuses {
1467                repo_path,
1468                statuses,
1469                git_operation,
1470            }
1471        }
1472        _ => unreachable!(),
1473    }
1474}
1475
1476fn buffer_for_full_path(
1477    client: &TestClient,
1478    project: &Model<Project>,
1479    full_path: &PathBuf,
1480    cx: &TestAppContext,
1481) -> Option<Model<language::Buffer>> {
1482    client
1483        .buffers_for_project(project)
1484        .iter()
1485        .find(|buffer| {
1486            buffer.read_with(cx, |buffer, cx| {
1487                buffer.file().unwrap().full_path(cx) == *full_path
1488            })
1489        })
1490        .cloned()
1491}
1492
1493fn project_for_root_name(
1494    client: &TestClient,
1495    root_name: &str,
1496    cx: &TestAppContext,
1497) -> Option<Model<Project>> {
1498    if let Some(ix) = project_ix_for_root_name(client.local_projects().deref(), root_name, cx) {
1499        return Some(client.local_projects()[ix].clone());
1500    }
1501    if let Some(ix) = project_ix_for_root_name(client.dev_server_projects().deref(), root_name, cx)
1502    {
1503        return Some(client.dev_server_projects()[ix].clone());
1504    }
1505    None
1506}
1507
1508fn project_ix_for_root_name(
1509    projects: &[Model<Project>],
1510    root_name: &str,
1511    cx: &TestAppContext,
1512) -> Option<usize> {
1513    projects.iter().position(|project| {
1514        project.read_with(cx, |project, cx| {
1515            let worktree = project.visible_worktrees(cx).next().unwrap();
1516            worktree.read(cx).root_name() == root_name
1517        })
1518    })
1519}
1520
1521fn root_name_for_project(project: &Model<Project>, cx: &TestAppContext) -> String {
1522    project.read_with(cx, |project, cx| {
1523        project
1524            .visible_worktrees(cx)
1525            .next()
1526            .unwrap()
1527            .read(cx)
1528            .root_name()
1529            .to_string()
1530    })
1531}
1532
1533fn project_path_for_full_path(
1534    project: &Model<Project>,
1535    full_path: &Path,
1536    cx: &TestAppContext,
1537) -> Option<ProjectPath> {
1538    let mut components = full_path.components();
1539    let root_name = components.next().unwrap().as_os_str().to_str().unwrap();
1540    let path = components.as_path().into();
1541    let worktree_id = project.read_with(cx, |project, cx| {
1542        project.worktrees(cx).find_map(|worktree| {
1543            let worktree = worktree.read(cx);
1544            if worktree.root_name() == root_name {
1545                Some(worktree.id())
1546            } else {
1547                None
1548            }
1549        })
1550    })?;
1551    Some(ProjectPath { worktree_id, path })
1552}
1553
1554async fn ensure_project_shared(
1555    project: &Model<Project>,
1556    client: &TestClient,
1557    cx: &mut TestAppContext,
1558) {
1559    let first_root_name = root_name_for_project(project, cx);
1560    let active_call = cx.read(ActiveCall::global);
1561    if active_call.read_with(cx, |call, _| call.room().is_some())
1562        && project.read_with(cx, |project, _| {
1563            project.is_local_or_ssh() && !project.is_shared()
1564        })
1565    {
1566        match active_call
1567            .update(cx, |call, cx| call.share_project(project.clone(), cx))
1568            .await
1569        {
1570            Ok(project_id) => {
1571                log::info!(
1572                    "{}: shared project {} with id {}",
1573                    client.username,
1574                    first_root_name,
1575                    project_id
1576                );
1577            }
1578            Err(error) => {
1579                log::error!(
1580                    "{}: error sharing project {}: {:?}",
1581                    client.username,
1582                    first_root_name,
1583                    error
1584                );
1585            }
1586        }
1587    }
1588}
1589
1590fn choose_random_project(client: &TestClient, rng: &mut StdRng) -> Option<Model<Project>> {
1591    client
1592        .local_projects()
1593        .deref()
1594        .iter()
1595        .chain(client.dev_server_projects().iter())
1596        .choose(rng)
1597        .cloned()
1598}
1599
1600fn gen_file_name(rng: &mut StdRng) -> String {
1601    let mut name = String::new();
1602    for _ in 0..10 {
1603        let letter = rng.gen_range('a'..='z');
1604        name.push(letter);
1605    }
1606    name
1607}