git_tests.rs

  1use std::path::{Path, PathBuf};
  2
  3use call::ActiveCall;
  4use client::RECEIVE_TIMEOUT;
  5use collections::HashMap;
  6use git::{
  7    repository::{RepoPath, Worktree as GitWorktree},
  8    status::{DiffStat, FileStatus, StatusCode, TrackedStatus},
  9};
 10use git_ui::{git_panel::GitPanel, project_diff::ProjectDiff};
 11use gpui::{AppContext as _, BackgroundExecutor, TestAppContext, VisualTestContext};
 12use project::ProjectPath;
 13use serde_json::json;
 14
 15use util::{path, rel_path::rel_path};
 16use workspace::{MultiWorkspace, Workspace};
 17
 18use crate::TestServer;
 19
 20fn collect_diff_stats<C: gpui::AppContext>(
 21    panel: &gpui::Entity<GitPanel>,
 22    cx: &C,
 23) -> HashMap<RepoPath, DiffStat> {
 24    panel.read_with(cx, |panel, cx| {
 25        let Some(repo) = panel.active_repository() else {
 26            return HashMap::default();
 27        };
 28        let snapshot = repo.read(cx).snapshot();
 29        let mut stats = HashMap::default();
 30        for entry in snapshot.statuses_by_path.iter() {
 31            if let Some(diff_stat) = entry.diff_stat {
 32                stats.insert(entry.repo_path.clone(), diff_stat);
 33            }
 34        }
 35        stats
 36    })
 37}
 38
 39#[gpui::test]
 40async fn test_project_diff(cx_a: &mut TestAppContext, cx_b: &mut TestAppContext) {
 41    let mut server = TestServer::start(cx_a.background_executor.clone()).await;
 42    let client_a = server.create_client(cx_a, "user_a").await;
 43    let client_b = server.create_client(cx_b, "user_b").await;
 44    cx_a.set_name("cx_a");
 45    cx_b.set_name("cx_b");
 46
 47    server
 48        .create_room(&mut [(&client_a, cx_a), (&client_b, cx_b)])
 49        .await;
 50
 51    client_a
 52        .fs()
 53        .insert_tree(
 54            path!("/a"),
 55            json!({
 56                ".git": {},
 57                "changed.txt": "after\n",
 58                "unchanged.txt": "unchanged\n",
 59                "created.txt": "created\n",
 60                "secret.pem": "secret-changed\n",
 61            }),
 62        )
 63        .await;
 64
 65    client_a.fs().set_head_and_index_for_repo(
 66        Path::new(path!("/a/.git")),
 67        &[
 68            ("changed.txt", "before\n".to_string()),
 69            ("unchanged.txt", "unchanged\n".to_string()),
 70            ("deleted.txt", "deleted\n".to_string()),
 71            ("secret.pem", "shh\n".to_string()),
 72        ],
 73    );
 74    let (project_a, worktree_id) = client_a.build_local_project(path!("/a"), cx_a).await;
 75    let active_call_a = cx_a.read(ActiveCall::global);
 76    let project_id = active_call_a
 77        .update(cx_a, |call, cx| call.share_project(project_a.clone(), cx))
 78        .await
 79        .unwrap();
 80
 81    cx_b.update(editor::init);
 82    cx_b.update(git_ui::init);
 83    let project_b = client_b.join_remote_project(project_id, cx_b).await;
 84    let window_b = cx_b.add_window(|window, cx| {
 85        let workspace = cx.new(|cx| {
 86            Workspace::new(
 87                None,
 88                project_b.clone(),
 89                client_b.app_state.clone(),
 90                window,
 91                cx,
 92            )
 93        });
 94        MultiWorkspace::new(workspace, window, cx)
 95    });
 96    let cx_b = &mut VisualTestContext::from_window(*window_b, cx_b);
 97    let workspace_b = window_b
 98        .root(cx_b)
 99        .unwrap()
100        .read_with(cx_b, |multi_workspace, _| {
101            multi_workspace.workspace().clone()
102        });
103
104    cx_b.update(|window, cx| {
105        window
106            .focused(cx)
107            .unwrap()
108            .dispatch_action(&git_ui::project_diff::Diff, window, cx)
109    });
110    let diff = workspace_b.update(cx_b, |workspace, cx| {
111        workspace.active_item(cx).unwrap().act_as::<ProjectDiff>(cx)
112    });
113    let diff = diff.unwrap();
114    cx_b.run_until_parked();
115
116    diff.update(cx_b, |diff, cx| {
117        assert_eq!(
118            diff.excerpt_paths(cx),
119            vec![
120                rel_path("changed.txt").into_arc(),
121                rel_path("deleted.txt").into_arc(),
122                rel_path("created.txt").into_arc()
123            ]
124        );
125    });
126
127    client_a
128        .fs()
129        .insert_tree(
130            path!("/a"),
131            json!({
132                ".git": {},
133                "changed.txt": "before\n",
134                "unchanged.txt": "changed\n",
135                "created.txt": "created\n",
136                "secret.pem": "secret-changed\n",
137            }),
138        )
139        .await;
140    cx_b.run_until_parked();
141
142    project_b.update(cx_b, |project, cx| {
143        let project_path = ProjectPath {
144            worktree_id,
145            path: rel_path("unchanged.txt").into(),
146        };
147        let status = project.project_path_git_status(&project_path, cx);
148        assert_eq!(
149            status.unwrap(),
150            FileStatus::Tracked(TrackedStatus {
151                worktree_status: StatusCode::Modified,
152                index_status: StatusCode::Unmodified,
153            })
154        );
155    });
156
157    diff.update(cx_b, |diff, cx| {
158        assert_eq!(
159            diff.excerpt_paths(cx),
160            vec![
161                rel_path("deleted.txt").into_arc(),
162                rel_path("unchanged.txt").into_arc(),
163                rel_path("created.txt").into_arc()
164            ]
165        );
166    });
167}
168
169#[gpui::test]
170async fn test_remote_git_worktrees(
171    executor: BackgroundExecutor,
172    cx_a: &mut TestAppContext,
173    cx_b: &mut TestAppContext,
174) {
175    let mut server = TestServer::start(executor.clone()).await;
176    let client_a = server.create_client(cx_a, "user_a").await;
177    let client_b = server.create_client(cx_b, "user_b").await;
178    server
179        .create_room(&mut [(&client_a, cx_a), (&client_b, cx_b)])
180        .await;
181    let active_call_a = cx_a.read(ActiveCall::global);
182
183    client_a
184        .fs()
185        .insert_tree(
186            path!("/project"),
187            json!({ ".git": {}, "file.txt": "content" }),
188        )
189        .await;
190
191    let (project_a, _) = client_a.build_local_project(path!("/project"), cx_a).await;
192
193    let project_id = active_call_a
194        .update(cx_a, |call, cx| call.share_project(project_a.clone(), cx))
195        .await
196        .unwrap();
197    let project_b = client_b.join_remote_project(project_id, cx_b).await;
198
199    executor.run_until_parked();
200
201    let repo_b = cx_b.update(|cx| project_b.read(cx).active_repository(cx).unwrap());
202
203    // Initially only the main worktree (the repo itself) should be present
204    let worktrees = cx_b
205        .update(|cx| repo_b.update(cx, |repository, _| repository.worktrees()))
206        .await
207        .unwrap()
208        .unwrap();
209    assert_eq!(worktrees.len(), 1);
210    assert_eq!(worktrees[0].path, PathBuf::from(path!("/project")));
211
212    // Client B creates a git worktree via the remote project
213    let worktree_directory = PathBuf::from(path!("/project"));
214    cx_b.update(|cx| {
215        repo_b.update(cx, |repository, _| {
216            repository.create_worktree(
217                "feature-branch".to_string(),
218                worktree_directory.join("feature-branch"),
219                Some("abc123".to_string()),
220            )
221        })
222    })
223    .await
224    .unwrap()
225    .unwrap();
226
227    executor.run_until_parked();
228
229    // Client B lists worktrees — should see main + the one just created
230    let worktrees = cx_b
231        .update(|cx| repo_b.update(cx, |repository, _| repository.worktrees()))
232        .await
233        .unwrap()
234        .unwrap();
235    assert_eq!(worktrees.len(), 2);
236    assert_eq!(worktrees[0].path, PathBuf::from(path!("/project")));
237    assert_eq!(worktrees[1].path, worktree_directory.join("feature-branch"));
238    assert_eq!(
239        worktrees[1].ref_name,
240        Some("refs/heads/feature-branch".into())
241    );
242    assert_eq!(worktrees[1].sha.as_ref(), "abc123");
243
244    // Verify from the host side that the worktree was actually created
245    let host_worktrees = {
246        let repo_a = cx_a.update(|cx| {
247            project_a
248                .read(cx)
249                .repositories(cx)
250                .values()
251                .next()
252                .unwrap()
253                .clone()
254        });
255        cx_a.update(|cx| repo_a.update(cx, |repository, _| repository.worktrees()))
256            .await
257            .unwrap()
258            .unwrap()
259    };
260    assert_eq!(host_worktrees.len(), 2);
261    assert_eq!(host_worktrees[0].path, PathBuf::from(path!("/project")));
262    assert_eq!(
263        host_worktrees[1].path,
264        worktree_directory.join("feature-branch")
265    );
266
267    // Client B creates a second git worktree without an explicit commit
268    cx_b.update(|cx| {
269        repo_b.update(cx, |repository, _| {
270            repository.create_worktree(
271                "bugfix-branch".to_string(),
272                worktree_directory.join("bugfix-branch"),
273                None,
274            )
275        })
276    })
277    .await
278    .unwrap()
279    .unwrap();
280
281    executor.run_until_parked();
282
283    // Client B lists worktrees — should now have main + two created
284    let worktrees = cx_b
285        .update(|cx| repo_b.update(cx, |repository, _| repository.worktrees()))
286        .await
287        .unwrap()
288        .unwrap();
289    assert_eq!(worktrees.len(), 3);
290
291    let feature_worktree = worktrees
292        .iter()
293        .find(|worktree| worktree.ref_name == Some("refs/heads/feature-branch".into()))
294        .expect("should find feature-branch worktree");
295    assert_eq!(
296        feature_worktree.path,
297        worktree_directory.join("feature-branch")
298    );
299
300    let bugfix_worktree = worktrees
301        .iter()
302        .find(|worktree| worktree.ref_name == Some("refs/heads/bugfix-branch".into()))
303        .expect("should find bugfix-branch worktree");
304    assert_eq!(
305        bugfix_worktree.path,
306        worktree_directory.join("bugfix-branch")
307    );
308    assert_eq!(bugfix_worktree.sha.as_ref(), "fake-sha");
309
310    // Client B (guest) attempts to rename a worktree. This should fail
311    // because worktree renaming is not forwarded through collab
312    let rename_result = cx_b
313        .update(|cx| {
314            repo_b.update(cx, |repository, _| {
315                repository.rename_worktree(
316                    worktree_directory.join("feature-branch"),
317                    worktree_directory.join("renamed-branch"),
318                )
319            })
320        })
321        .await
322        .unwrap();
323    assert!(
324        rename_result.is_err(),
325        "Guest should not be able to rename worktrees via collab"
326    );
327
328    executor.run_until_parked();
329
330    // Verify worktrees are unchanged — still 3
331    let worktrees = cx_b
332        .update(|cx| repo_b.update(cx, |repository, _| repository.worktrees()))
333        .await
334        .unwrap()
335        .unwrap();
336    assert_eq!(
337        worktrees.len(),
338        3,
339        "Worktree count should be unchanged after failed rename"
340    );
341
342    // Client B (guest) attempts to remove a worktree. This should fail
343    // because worktree removal is not forwarded through collab
344    let remove_result = cx_b
345        .update(|cx| {
346            repo_b.update(cx, |repository, _| {
347                repository.remove_worktree(worktree_directory.join("feature-branch"), false)
348            })
349        })
350        .await
351        .unwrap();
352    assert!(
353        remove_result.is_err(),
354        "Guest should not be able to remove worktrees via collab"
355    );
356
357    executor.run_until_parked();
358
359    // Verify worktrees are unchanged — still 3
360    let worktrees = cx_b
361        .update(|cx| repo_b.update(cx, |repository, _| repository.worktrees()))
362        .await
363        .unwrap()
364        .unwrap();
365    assert_eq!(
366        worktrees.len(),
367        3,
368        "Worktree count should be unchanged after failed removal"
369    );
370}
371
372#[gpui::test]
373async fn test_linked_worktrees_sync(
374    executor: BackgroundExecutor,
375    cx_a: &mut TestAppContext,
376    cx_b: &mut TestAppContext,
377    cx_c: &mut TestAppContext,
378) {
379    let mut server = TestServer::start(executor.clone()).await;
380    let client_a = server.create_client(cx_a, "user_a").await;
381    let client_b = server.create_client(cx_b, "user_b").await;
382    let client_c = server.create_client(cx_c, "user_c").await;
383    server
384        .create_room(&mut [(&client_a, cx_a), (&client_b, cx_b), (&client_c, cx_c)])
385        .await;
386    let active_call_a = cx_a.read(ActiveCall::global);
387
388    // Set up a git repo with two linked worktrees already present.
389    client_a
390        .fs()
391        .insert_tree(
392            path!("/project"),
393            json!({ ".git": {}, "file.txt": "content" }),
394        )
395        .await;
396
397    client_a
398        .fs()
399        .with_git_state(Path::new(path!("/project/.git")), true, |state| {
400            state.worktrees.push(GitWorktree {
401                path: PathBuf::from(path!("/project")),
402                ref_name: Some("refs/heads/main".into()),
403                sha: "aaa111".into(),
404            });
405            state.worktrees.push(GitWorktree {
406                path: PathBuf::from(path!("/project/feature-branch")),
407                ref_name: Some("refs/heads/feature-branch".into()),
408                sha: "bbb222".into(),
409            });
410            state.worktrees.push(GitWorktree {
411                path: PathBuf::from(path!("/project/bugfix-branch")),
412                ref_name: Some("refs/heads/bugfix-branch".into()),
413                sha: "ccc333".into(),
414            });
415        })
416        .unwrap();
417
418    let (project_a, _) = client_a.build_local_project(path!("/project"), cx_a).await;
419
420    // Wait for git scanning to complete on the host.
421    executor.run_until_parked();
422
423    // Verify the host sees 2 linked worktrees (main worktree is filtered out).
424    let host_linked = project_a.read_with(cx_a, |project, cx| {
425        let repos = project.repositories(cx);
426        assert_eq!(repos.len(), 1, "host should have exactly 1 repository");
427        let repo = repos.values().next().unwrap();
428        repo.read(cx).linked_worktrees().to_vec()
429    });
430    assert_eq!(
431        host_linked.len(),
432        2,
433        "host should have 2 linked worktrees (main filtered out)"
434    );
435    assert_eq!(
436        host_linked[0].path,
437        PathBuf::from(path!("/project/feature-branch"))
438    );
439    assert_eq!(
440        host_linked[0].ref_name,
441        Some("refs/heads/feature-branch".into())
442    );
443    assert_eq!(host_linked[0].sha.as_ref(), "bbb222");
444    assert_eq!(
445        host_linked[1].path,
446        PathBuf::from(path!("/project/bugfix-branch"))
447    );
448    assert_eq!(
449        host_linked[1].ref_name,
450        Some("refs/heads/bugfix-branch".into())
451    );
452    assert_eq!(host_linked[1].sha.as_ref(), "ccc333");
453
454    // Share the project and have client B join.
455    let project_id = active_call_a
456        .update(cx_a, |call, cx| call.share_project(project_a.clone(), cx))
457        .await
458        .unwrap();
459    let project_b = client_b.join_remote_project(project_id, cx_b).await;
460
461    executor.run_until_parked();
462
463    // Verify the guest sees the same linked worktrees as the host.
464    let guest_linked = project_b.read_with(cx_b, |project, cx| {
465        let repos = project.repositories(cx);
466        assert_eq!(repos.len(), 1, "guest should have exactly 1 repository");
467        let repo = repos.values().next().unwrap();
468        repo.read(cx).linked_worktrees().to_vec()
469    });
470    assert_eq!(
471        guest_linked, host_linked,
472        "guest's linked_worktrees should match host's after initial sync"
473    );
474
475    // Now mutate: add a third linked worktree on the host side.
476    client_a
477        .fs()
478        .with_git_state(Path::new(path!("/project/.git")), true, |state| {
479            state.worktrees.push(GitWorktree {
480                path: PathBuf::from(path!("/project/hotfix-branch")),
481                ref_name: Some("refs/heads/hotfix-branch".into()),
482                sha: "ddd444".into(),
483            });
484        })
485        .unwrap();
486
487    // Wait for the host to re-scan and propagate the update.
488    executor.run_until_parked();
489
490    // Verify host now sees 3 linked worktrees.
491    let host_linked_updated = project_a.read_with(cx_a, |project, cx| {
492        let repos = project.repositories(cx);
493        let repo = repos.values().next().unwrap();
494        repo.read(cx).linked_worktrees().to_vec()
495    });
496    assert_eq!(
497        host_linked_updated.len(),
498        3,
499        "host should now have 3 linked worktrees"
500    );
501    assert_eq!(
502        host_linked_updated[2].path,
503        PathBuf::from(path!("/project/hotfix-branch"))
504    );
505
506    // Verify the guest also received the update.
507    let guest_linked_updated = project_b.read_with(cx_b, |project, cx| {
508        let repos = project.repositories(cx);
509        let repo = repos.values().next().unwrap();
510        repo.read(cx).linked_worktrees().to_vec()
511    });
512    assert_eq!(
513        guest_linked_updated, host_linked_updated,
514        "guest's linked_worktrees should match host's after update"
515    );
516
517    // Now mutate: remove one linked worktree from the host side.
518    client_a
519        .fs()
520        .with_git_state(Path::new(path!("/project/.git")), true, |state| {
521            state
522                .worktrees
523                .retain(|wt| wt.ref_name != Some("refs/heads/bugfix-branch".into()));
524        })
525        .unwrap();
526
527    executor.run_until_parked();
528
529    // Verify host now sees 2 linked worktrees (feature-branch and hotfix-branch).
530    let host_linked_after_removal = project_a.read_with(cx_a, |project, cx| {
531        let repos = project.repositories(cx);
532        let repo = repos.values().next().unwrap();
533        repo.read(cx).linked_worktrees().to_vec()
534    });
535    assert_eq!(
536        host_linked_after_removal.len(),
537        2,
538        "host should have 2 linked worktrees after removal"
539    );
540    assert!(
541        host_linked_after_removal
542            .iter()
543            .all(|wt| wt.ref_name != Some("refs/heads/bugfix-branch".into())),
544        "bugfix-branch should have been removed"
545    );
546
547    // Verify the guest also reflects the removal.
548    let guest_linked_after_removal = project_b.read_with(cx_b, |project, cx| {
549        let repos = project.repositories(cx);
550        let repo = repos.values().next().unwrap();
551        repo.read(cx).linked_worktrees().to_vec()
552    });
553    assert_eq!(
554        guest_linked_after_removal, host_linked_after_removal,
555        "guest's linked_worktrees should match host's after removal"
556    );
557
558    // Test DB roundtrip: client C joins late, getting state from the database.
559    // This verifies that linked_worktrees are persisted and restored correctly.
560    let project_c = client_c.join_remote_project(project_id, cx_c).await;
561    executor.run_until_parked();
562
563    let late_joiner_linked = project_c.read_with(cx_c, |project, cx| {
564        let repos = project.repositories(cx);
565        assert_eq!(
566            repos.len(),
567            1,
568            "late joiner should have exactly 1 repository"
569        );
570        let repo = repos.values().next().unwrap();
571        repo.read(cx).linked_worktrees().to_vec()
572    });
573    assert_eq!(
574        late_joiner_linked, host_linked_after_removal,
575        "late-joining client's linked_worktrees should match host's (DB roundtrip)"
576    );
577
578    // Test reconnection: disconnect client B (guest) and reconnect.
579    // After rejoining, client B should get linked_worktrees back from the DB.
580    server.disconnect_client(client_b.peer_id().unwrap());
581    executor.advance_clock(RECEIVE_TIMEOUT);
582    executor.run_until_parked();
583
584    // Client B reconnects automatically.
585    executor.advance_clock(RECEIVE_TIMEOUT);
586    executor.run_until_parked();
587
588    // Verify client B still has the correct linked worktrees after reconnection.
589    let guest_linked_after_reconnect = project_b.read_with(cx_b, |project, cx| {
590        let repos = project.repositories(cx);
591        assert_eq!(
592            repos.len(),
593            1,
594            "guest should still have exactly 1 repository after reconnect"
595        );
596        let repo = repos.values().next().unwrap();
597        repo.read(cx).linked_worktrees().to_vec()
598    });
599    assert_eq!(
600        guest_linked_after_reconnect, host_linked_after_removal,
601        "guest's linked_worktrees should survive guest disconnect/reconnect"
602    );
603}
604
605#[gpui::test]
606async fn test_diff_stat_sync_between_host_and_downstream_client(
607    cx_a: &mut TestAppContext,
608    cx_b: &mut TestAppContext,
609    cx_c: &mut TestAppContext,
610) {
611    let mut server = TestServer::start(cx_a.background_executor.clone()).await;
612    let client_a = server.create_client(cx_a, "user_a").await;
613    let client_b = server.create_client(cx_b, "user_b").await;
614    let client_c = server.create_client(cx_c, "user_c").await;
615
616    server
617        .create_room(&mut [(&client_a, cx_a), (&client_b, cx_b), (&client_c, cx_c)])
618        .await;
619
620    let fs = client_a.fs();
621    fs.insert_tree(
622        path!("/code"),
623        json!({
624            "project1": {
625                ".git": {},
626                "src": {
627                    "lib.rs": "line1\nline2\nline3\n",
628                    "new_file.rs": "added1\nadded2\n",
629                },
630                "README.md": "# project 1",
631            }
632        }),
633    )
634    .await;
635
636    let dot_git = Path::new(path!("/code/project1/.git"));
637    fs.set_head_for_repo(
638        dot_git,
639        &[
640            ("src/lib.rs", "line1\nold_line2\n".into()),
641            ("src/deleted.rs", "was_here\n".into()),
642        ],
643        "deadbeef",
644    );
645    fs.set_index_for_repo(
646        dot_git,
647        &[
648            ("src/lib.rs", "line1\nold_line2\nline3\nline4\n".into()),
649            ("src/staged_only.rs", "x\ny\n".into()),
650            ("src/new_file.rs", "added1\nadded2\n".into()),
651            ("README.md", "# project 1".into()),
652        ],
653    );
654
655    let (project_a, worktree_id) = client_a
656        .build_local_project(path!("/code/project1"), cx_a)
657        .await;
658    let active_call_a = cx_a.read(ActiveCall::global);
659    let project_id = active_call_a
660        .update(cx_a, |call, cx| call.share_project(project_a.clone(), cx))
661        .await
662        .unwrap();
663    let project_b = client_b.join_remote_project(project_id, cx_b).await;
664    let _project_c = client_c.join_remote_project(project_id, cx_c).await;
665    cx_a.run_until_parked();
666
667    let (workspace_a, cx_a) = client_a.build_workspace(&project_a, cx_a);
668    let (workspace_b, cx_b) = client_b.build_workspace(&project_b, cx_b);
669
670    let panel_a = workspace_a.update_in(cx_a, GitPanel::new_test);
671    workspace_a.update_in(cx_a, |workspace, window, cx| {
672        workspace.add_panel(panel_a.clone(), window, cx);
673    });
674
675    let panel_b = workspace_b.update_in(cx_b, GitPanel::new_test);
676    workspace_b.update_in(cx_b, |workspace, window, cx| {
677        workspace.add_panel(panel_b.clone(), window, cx);
678    });
679
680    cx_a.run_until_parked();
681
682    let stats_a = collect_diff_stats(&panel_a, cx_a);
683    let stats_b = collect_diff_stats(&panel_b, cx_b);
684
685    let mut expected: HashMap<RepoPath, DiffStat> = HashMap::default();
686    expected.insert(
687        RepoPath::new("src/lib.rs").unwrap(),
688        DiffStat {
689            added: 3,
690            deleted: 2,
691        },
692    );
693    expected.insert(
694        RepoPath::new("src/deleted.rs").unwrap(),
695        DiffStat {
696            added: 0,
697            deleted: 1,
698        },
699    );
700    expected.insert(
701        RepoPath::new("src/new_file.rs").unwrap(),
702        DiffStat {
703            added: 2,
704            deleted: 0,
705        },
706    );
707    expected.insert(
708        RepoPath::new("README.md").unwrap(),
709        DiffStat {
710            added: 1,
711            deleted: 0,
712        },
713    );
714    assert_eq!(stats_a, expected, "host diff stats should match expected");
715    assert_eq!(stats_a, stats_b, "host and remote should agree");
716
717    let buffer_a = project_a
718        .update(cx_a, |p, cx| {
719            p.open_buffer((worktree_id, rel_path("src/lib.rs")), cx)
720        })
721        .await
722        .unwrap();
723
724    let _buffer_b = project_b
725        .update(cx_b, |p, cx| {
726            p.open_buffer((worktree_id, rel_path("src/lib.rs")), cx)
727        })
728        .await
729        .unwrap();
730    cx_a.run_until_parked();
731
732    buffer_a.update(cx_a, |buf, cx| {
733        buf.edit([(buf.len()..buf.len(), "line4\n")], None, cx);
734    });
735    project_a
736        .update(cx_a, |project, cx| {
737            project.save_buffer(buffer_a.clone(), cx)
738        })
739        .await
740        .unwrap();
741    cx_a.run_until_parked();
742
743    let stats_a = collect_diff_stats(&panel_a, cx_a);
744    let stats_b = collect_diff_stats(&panel_b, cx_b);
745
746    let mut expected_after_edit = expected.clone();
747    expected_after_edit.insert(
748        RepoPath::new("src/lib.rs").unwrap(),
749        DiffStat {
750            added: 4,
751            deleted: 2,
752        },
753    );
754    assert_eq!(
755        stats_a, expected_after_edit,
756        "host diff stats should reflect the edit"
757    );
758    assert_eq!(
759        stats_b, expected_after_edit,
760        "remote diff stats should reflect the host's edit"
761    );
762
763    let active_call_b = cx_b.read(ActiveCall::global);
764    active_call_b
765        .update(cx_b, |call, cx| call.hang_up(cx))
766        .await
767        .unwrap();
768    cx_a.run_until_parked();
769
770    let user_id_b = client_b.current_user_id(cx_b).to_proto();
771    active_call_a
772        .update(cx_a, |call, cx| call.invite(user_id_b, None, cx))
773        .await
774        .unwrap();
775    cx_b.run_until_parked();
776    let active_call_b = cx_b.read(ActiveCall::global);
777    active_call_b
778        .update(cx_b, |call, cx| call.accept_incoming(cx))
779        .await
780        .unwrap();
781    cx_a.run_until_parked();
782
783    let project_b = client_b.join_remote_project(project_id, cx_b).await;
784    cx_a.run_until_parked();
785
786    let (workspace_b, cx_b) = client_b.build_workspace(&project_b, cx_b);
787    let panel_b = workspace_b.update_in(cx_b, GitPanel::new_test);
788    workspace_b.update_in(cx_b, |workspace, window, cx| {
789        workspace.add_panel(panel_b.clone(), window, cx);
790    });
791    cx_b.run_until_parked();
792
793    let stats_b = collect_diff_stats(&panel_b, cx_b);
794    assert_eq!(
795        stats_b, expected_after_edit,
796        "remote diff stats should be restored from the database after rejoining the call"
797    );
798}