1use crate::tests::TestServer;
2use call::ActiveCall;
3use fs::{FakeFs, Fs as _};
4use gpui::{Context as _, TestAppContext};
5use language::language_settings::all_language_settings;
6use project::ProjectPath;
7use remote::SshSession;
8use remote_server::HeadlessProject;
9use serde_json::json;
10use std::{path::Path, sync::Arc};
11
12#[gpui::test]
13async fn test_sharing_an_ssh_remote_project(
14 cx_a: &mut TestAppContext,
15 cx_b: &mut TestAppContext,
16 server_cx: &mut TestAppContext,
17) {
18 let executor = cx_a.executor();
19 let mut server = TestServer::start(executor.clone()).await;
20 let client_a = server.create_client(cx_a, "user_a").await;
21 let client_b = server.create_client(cx_b, "user_b").await;
22 server
23 .create_room(&mut [(&client_a, cx_a), (&client_b, cx_b)])
24 .await;
25
26 // Set up project on remote FS
27 let (client_ssh, server_ssh) = SshSession::fake(cx_a, server_cx);
28 let remote_fs = FakeFs::new(server_cx.executor());
29 remote_fs
30 .insert_tree(
31 "/code",
32 json!({
33 "project1": {
34 ".zed": {
35 "settings.json": r#"{"languages":{"Rust":{"language_servers":["override-rust-analyzer"]}}}"#
36 },
37 "README.md": "# project 1",
38 "src": {
39 "lib.rs": "fn one() -> usize { 1 }"
40 }
41 },
42 "project2": {
43 "README.md": "# project 2",
44 },
45 }),
46 )
47 .await;
48
49 // User A connects to the remote project via SSH.
50 server_cx.update(HeadlessProject::init);
51 let _headless_project =
52 server_cx.new_model(|cx| HeadlessProject::new(server_ssh, remote_fs.clone(), cx));
53
54 let (project_a, worktree_id) = client_a
55 .build_ssh_project("/code/project1", client_ssh, cx_a)
56 .await;
57 executor.run_until_parked();
58
59 // User A shares the remote project.
60 let active_call_a = cx_a.read(ActiveCall::global);
61 let project_id = active_call_a
62 .update(cx_a, |call, cx| call.share_project(project_a.clone(), cx))
63 .await
64 .unwrap();
65
66 // User B joins the project.
67 let project_b = client_b.build_dev_server_project(project_id, cx_b).await;
68 let worktree_b = project_b
69 .update(cx_b, |project, cx| project.worktree_for_id(worktree_id, cx))
70 .unwrap();
71
72 executor.run_until_parked();
73 worktree_b.update(cx_b, |worktree, _cx| {
74 assert_eq!(
75 worktree.paths().map(Arc::as_ref).collect::<Vec<_>>(),
76 vec![
77 Path::new(".zed"),
78 Path::new(".zed/settings.json"),
79 Path::new("README.md"),
80 Path::new("src"),
81 Path::new("src/lib.rs"),
82 ]
83 );
84 });
85
86 // User B can open buffers in the remote project.
87 let buffer_b = project_b
88 .update(cx_b, |project, cx| {
89 project.open_buffer((worktree_id, "src/lib.rs"), cx)
90 })
91 .await
92 .unwrap();
93 buffer_b.update(cx_b, |buffer, cx| {
94 assert_eq!(buffer.text(), "fn one() -> usize { 1 }");
95 let ix = buffer.text().find('1').unwrap();
96 buffer.edit([(ix..ix + 1, "100")], None, cx);
97 });
98
99 executor.run_until_parked();
100
101 cx_b.read(|cx| {
102 let file = buffer_b.read(cx).file();
103 assert_eq!(
104 all_language_settings(file, cx)
105 .language(Some(&("Rust".into())))
106 .language_servers,
107 ["override-rust-analyzer".to_string()]
108 )
109 });
110
111 project_b
112 .update(cx_b, |project, cx| {
113 project.save_buffer_as(
114 buffer_b.clone(),
115 ProjectPath {
116 worktree_id: worktree_id.to_owned(),
117 path: Arc::from(Path::new("src/renamed.rs")),
118 },
119 cx,
120 )
121 })
122 .await
123 .unwrap();
124 assert_eq!(
125 remote_fs
126 .load("/code/project1/src/renamed.rs".as_ref())
127 .await
128 .unwrap(),
129 "fn one() -> usize { 100 }"
130 );
131 cx_b.run_until_parked();
132 cx_b.update(|cx| {
133 assert_eq!(
134 buffer_b
135 .read(cx)
136 .file()
137 .unwrap()
138 .path()
139 .to_string_lossy()
140 .to_string(),
141 "src/renamed.rs".to_string()
142 );
143 });
144}