1use std::path::Path;
2
3use call::ActiveCall;
4use git::status::{FileStatus, StatusCode, TrackedStatus};
5use git_ui::project_diff::ProjectDiff;
6use gpui::{TestAppContext, VisualTestContext};
7use project::ProjectPath;
8use serde_json::json;
9use util::{path, rel_path::rel_path};
10use workspace::Workspace;
11
12//
13use crate::tests::TestServer;
14
15#[gpui::test]
16async fn test_project_diff(cx_a: &mut TestAppContext, cx_b: &mut TestAppContext) {
17 let mut server = TestServer::start(cx_a.background_executor.clone()).await;
18 let client_a = server.create_client(cx_a, "user_a").await;
19 let client_b = server.create_client(cx_b, "user_b").await;
20 cx_a.set_name("cx_a");
21 cx_b.set_name("cx_b");
22
23 server
24 .create_room(&mut [(&client_a, cx_a), (&client_b, cx_b)])
25 .await;
26
27 client_a
28 .fs()
29 .insert_tree(
30 path!("/a"),
31 json!({
32 ".git": {},
33 "changed.txt": "after\n",
34 "unchanged.txt": "unchanged\n",
35 "created.txt": "created\n",
36 "secret.pem": "secret-changed\n",
37 }),
38 )
39 .await;
40
41 client_a.fs().set_head_and_index_for_repo(
42 Path::new(path!("/a/.git")),
43 &[
44 ("changed.txt", "before\n".to_string()),
45 ("unchanged.txt", "unchanged\n".to_string()),
46 ("deleted.txt", "deleted\n".to_string()),
47 ("secret.pem", "shh\n".to_string()),
48 ],
49 );
50 let (project_a, worktree_id) = client_a.build_local_project(path!("/a"), cx_a).await;
51 let active_call_a = cx_a.read(ActiveCall::global);
52 let project_id = active_call_a
53 .update(cx_a, |call, cx| call.share_project(project_a.clone(), cx))
54 .await
55 .unwrap();
56
57 cx_b.update(editor::init);
58 cx_b.update(git_ui::init);
59 let project_b = client_b.join_remote_project(project_id, cx_b).await;
60 let workspace_b = cx_b.add_window(|window, cx| {
61 Workspace::new(
62 None,
63 project_b.clone(),
64 client_b.app_state.clone(),
65 window,
66 cx,
67 )
68 });
69 let cx_b = &mut VisualTestContext::from_window(*workspace_b, cx_b);
70 let workspace_b = workspace_b.root(cx_b).unwrap();
71
72 cx_b.update(|window, cx| {
73 window
74 .focused(cx)
75 .unwrap()
76 .dispatch_action(&git_ui::project_diff::Diff, window, cx)
77 });
78 let diff = workspace_b.update(cx_b, |workspace, cx| {
79 workspace.active_item(cx).unwrap().act_as::<ProjectDiff>(cx)
80 });
81 let diff = diff.unwrap();
82 cx_b.run_until_parked();
83
84 diff.update(cx_b, |diff, cx| {
85 assert_eq!(
86 diff.excerpt_paths(cx),
87 vec!["changed.txt", "deleted.txt", "created.txt"]
88 );
89 });
90
91 client_a
92 .fs()
93 .insert_tree(
94 path!("/a"),
95 json!({
96 ".git": {},
97 "changed.txt": "before\n",
98 "unchanged.txt": "changed\n",
99 "created.txt": "created\n",
100 "secret.pem": "secret-changed\n",
101 }),
102 )
103 .await;
104 cx_b.run_until_parked();
105
106 project_b.update(cx_b, |project, cx| {
107 let project_path = ProjectPath {
108 worktree_id,
109 path: rel_path("unchanged.txt").into(),
110 };
111 let status = project.project_path_git_status(&project_path, cx);
112 assert_eq!(
113 status.unwrap(),
114 FileStatus::Tracked(TrackedStatus {
115 worktree_status: StatusCode::Modified,
116 index_status: StatusCode::Unmodified,
117 })
118 );
119 });
120
121 diff.update(cx_b, |diff, cx| {
122 assert_eq!(
123 diff.excerpt_paths(cx),
124 vec!["deleted.txt", "unchanged.txt", "created.txt"]
125 );
126 });
127}