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