1use std::{path::Path, sync::Arc};
2
3use call::ActiveCall;
4use editor::Editor;
5use fs::Fs;
6use gpui::{TestAppContext, VisualTestContext, WindowHandle};
7use rpc::{proto::DevServerStatus, ErrorCode, ErrorExt};
8use serde_json::json;
9use workspace::{AppState, Workspace};
10
11use crate::tests::{following_tests::join_channel, TestServer};
12
13use super::TestClient;
14
15#[gpui::test]
16async fn test_dev_server(cx: &mut gpui::TestAppContext, cx2: &mut gpui::TestAppContext) {
17 let (server, client) = TestServer::start1(cx).await;
18
19 let store = cx.update(|cx| dev_server_projects::Store::global(cx).clone());
20
21 let resp = store
22 .update(cx, |store, cx| {
23 store.create_dev_server("server-1".to_string(), cx)
24 })
25 .await
26 .unwrap();
27
28 store.update(cx, |store, _| {
29 assert_eq!(store.dev_servers().len(), 1);
30 assert_eq!(store.dev_servers()[0].name, "server-1");
31 assert_eq!(store.dev_servers()[0].status, DevServerStatus::Offline);
32 });
33
34 let dev_server = server.create_dev_server(resp.access_token, cx2).await;
35 cx.executor().run_until_parked();
36 store.update(cx, |store, _| {
37 assert_eq!(store.dev_servers()[0].status, DevServerStatus::Online);
38 });
39
40 dev_server
41 .fs()
42 .insert_tree(
43 "/remote",
44 json!({
45 "1.txt": "remote\nremote\nremote",
46 "2.js": "function two() { return 2; }",
47 "3.rs": "mod test",
48 }),
49 )
50 .await;
51
52 store
53 .update(cx, |store, cx| {
54 store.create_dev_server_project(
55 client::DevServerId(resp.dev_server_id),
56 "/remote".to_string(),
57 cx,
58 )
59 })
60 .await
61 .unwrap();
62
63 cx.executor().run_until_parked();
64
65 let remote_workspace = store
66 .update(cx, |store, cx| {
67 let projects = store.dev_server_projects();
68 assert_eq!(projects.len(), 1);
69 assert_eq!(projects[0].path, "/remote");
70 workspace::join_dev_server_project(
71 projects[0].project_id.unwrap(),
72 client.app_state.clone(),
73 None,
74 cx,
75 )
76 })
77 .await
78 .unwrap();
79
80 cx.executor().run_until_parked();
81
82 let cx = VisualTestContext::from_window(remote_workspace.into(), cx).as_mut();
83 cx.simulate_keystrokes("cmd-p 1 enter");
84
85 let editor = remote_workspace
86 .update(cx, |ws, cx| {
87 ws.active_item_as::<Editor>(cx).unwrap().clone()
88 })
89 .unwrap();
90 editor.update(cx, |ed, cx| {
91 assert_eq!(ed.text(cx).to_string(), "remote\nremote\nremote");
92 });
93 cx.simulate_input("wow!");
94 cx.simulate_keystrokes("cmd-s");
95
96 let content = dev_server
97 .fs()
98 .load(&Path::new("/remote/1.txt"))
99 .await
100 .unwrap();
101 assert_eq!(content, "wow!remote\nremote\nremote\n");
102}
103
104#[gpui::test]
105async fn test_dev_server_env_files(
106 cx1: &mut gpui::TestAppContext,
107 cx2: &mut gpui::TestAppContext,
108 cx3: &mut gpui::TestAppContext,
109) {
110 let (server, client1, client2, channel_id) = TestServer::start2(cx1, cx2).await;
111
112 let (_dev_server, remote_workspace) =
113 create_dev_server_project(&server, client1.app_state.clone(), cx1, cx3).await;
114
115 cx1.executor().run_until_parked();
116
117 let cx1 = VisualTestContext::from_window(remote_workspace.into(), cx1).as_mut();
118 cx1.simulate_keystrokes("cmd-p . e enter");
119
120 let editor = remote_workspace
121 .update(cx1, |ws, cx| {
122 ws.active_item_as::<Editor>(cx).unwrap().clone()
123 })
124 .unwrap();
125 editor.update(cx1, |ed, cx| {
126 assert_eq!(ed.text(cx).to_string(), "SECRET");
127 });
128
129 cx1.update(|cx| {
130 workspace::join_channel(
131 channel_id,
132 client1.app_state.clone(),
133 Some(remote_workspace),
134 cx,
135 )
136 })
137 .await
138 .unwrap();
139 cx1.executor().run_until_parked();
140
141 remote_workspace
142 .update(cx1, |ws, cx| {
143 assert!(ws.project().read(cx).is_shared());
144 })
145 .unwrap();
146
147 join_channel(channel_id, &client2, cx2).await.unwrap();
148 cx2.executor().run_until_parked();
149
150 let (workspace2, cx2) = client2.active_workspace(cx2);
151 let editor = workspace2.update(cx2, |ws, cx| {
152 ws.active_item_as::<Editor>(cx).unwrap().clone()
153 });
154 // TODO: it'd be nice to hide .env files from other people
155 editor.update(cx2, |ed, cx| {
156 assert_eq!(ed.text(cx).to_string(), "SECRET");
157 });
158}
159
160async fn create_dev_server_project(
161 server: &TestServer,
162 client_app_state: Arc<AppState>,
163 cx: &mut TestAppContext,
164 cx_devserver: &mut TestAppContext,
165) -> (TestClient, WindowHandle<Workspace>) {
166 let store = cx.update(|cx| dev_server_projects::Store::global(cx).clone());
167
168 let resp = store
169 .update(cx, |store, cx| {
170 store.create_dev_server("server-1".to_string(), cx)
171 })
172 .await
173 .unwrap();
174 let dev_server = server
175 .create_dev_server(resp.access_token, cx_devserver)
176 .await;
177
178 cx.executor().run_until_parked();
179
180 dev_server
181 .fs()
182 .insert_tree(
183 "/remote",
184 json!({
185 "1.txt": "remote\nremote\nremote",
186 ".env": "SECRET",
187 }),
188 )
189 .await;
190
191 store
192 .update(cx, |store, cx| {
193 store.create_dev_server_project(
194 client::DevServerId(resp.dev_server_id),
195 "/remote".to_string(),
196 cx,
197 )
198 })
199 .await
200 .unwrap();
201
202 cx.executor().run_until_parked();
203
204 let workspace = store
205 .update(cx, |store, cx| {
206 let projects = store.dev_server_projects();
207 assert_eq!(projects.len(), 1);
208 assert_eq!(projects[0].path, "/remote");
209 workspace::join_dev_server_project(
210 projects[0].project_id.unwrap(),
211 client_app_state,
212 None,
213 cx,
214 )
215 })
216 .await
217 .unwrap();
218
219 cx.executor().run_until_parked();
220
221 (dev_server, workspace)
222}
223
224#[gpui::test]
225async fn test_dev_server_leave_room(
226 cx1: &mut gpui::TestAppContext,
227 cx2: &mut gpui::TestAppContext,
228 cx3: &mut gpui::TestAppContext,
229) {
230 let (server, client1, client2, channel_id) = TestServer::start2(cx1, cx2).await;
231
232 let (_dev_server, remote_workspace) =
233 create_dev_server_project(&server, client1.app_state.clone(), cx1, cx3).await;
234
235 cx1.update(|cx| {
236 workspace::join_channel(
237 channel_id,
238 client1.app_state.clone(),
239 Some(remote_workspace),
240 cx,
241 )
242 })
243 .await
244 .unwrap();
245 cx1.executor().run_until_parked();
246
247 remote_workspace
248 .update(cx1, |ws, cx| {
249 assert!(ws.project().read(cx).is_shared());
250 })
251 .unwrap();
252
253 join_channel(channel_id, &client2, cx2).await.unwrap();
254 cx2.executor().run_until_parked();
255
256 cx1.update(|cx| ActiveCall::global(cx).update(cx, |active_call, cx| active_call.hang_up(cx)))
257 .await
258 .unwrap();
259
260 cx1.executor().run_until_parked();
261
262 let (workspace, cx2) = client2.active_workspace(cx2);
263 cx2.update(|cx| assert!(workspace.read(cx).project().read(cx).is_disconnected()));
264}
265
266#[gpui::test]
267async fn test_dev_server_delete(
268 cx1: &mut gpui::TestAppContext,
269 cx2: &mut gpui::TestAppContext,
270 cx3: &mut gpui::TestAppContext,
271) {
272 let (server, client1, client2, channel_id) = TestServer::start2(cx1, cx2).await;
273
274 let (_dev_server, remote_workspace) =
275 create_dev_server_project(&server, client1.app_state.clone(), cx1, cx3).await;
276
277 cx1.update(|cx| {
278 workspace::join_channel(
279 channel_id,
280 client1.app_state.clone(),
281 Some(remote_workspace),
282 cx,
283 )
284 })
285 .await
286 .unwrap();
287 cx1.executor().run_until_parked();
288
289 remote_workspace
290 .update(cx1, |ws, cx| {
291 assert!(ws.project().read(cx).is_shared());
292 })
293 .unwrap();
294
295 join_channel(channel_id, &client2, cx2).await.unwrap();
296 cx2.executor().run_until_parked();
297
298 cx1.update(|cx| {
299 dev_server_projects::Store::global(cx).update(cx, |store, cx| {
300 store.delete_dev_server_project(store.dev_server_projects().first().unwrap().id, cx)
301 })
302 })
303 .await
304 .unwrap();
305
306 cx1.executor().run_until_parked();
307
308 let (workspace, cx2) = client2.active_workspace(cx2);
309 cx2.update(|cx| assert!(workspace.read(cx).project().read(cx).is_disconnected()));
310
311 cx1.update(|cx| {
312 dev_server_projects::Store::global(cx).update(cx, |store, _| {
313 assert_eq!(store.dev_server_projects().len(), 0);
314 })
315 })
316}
317
318#[gpui::test]
319async fn test_dev_server_rename(
320 cx1: &mut gpui::TestAppContext,
321 cx2: &mut gpui::TestAppContext,
322 cx3: &mut gpui::TestAppContext,
323) {
324 let (server, client1, client2, channel_id) = TestServer::start2(cx1, cx2).await;
325
326 let (_dev_server, remote_workspace) =
327 create_dev_server_project(&server, client1.app_state.clone(), cx1, cx3).await;
328
329 cx1.update(|cx| {
330 workspace::join_channel(
331 channel_id,
332 client1.app_state.clone(),
333 Some(remote_workspace),
334 cx,
335 )
336 })
337 .await
338 .unwrap();
339 cx1.executor().run_until_parked();
340
341 remote_workspace
342 .update(cx1, |ws, cx| {
343 assert!(ws.project().read(cx).is_shared());
344 })
345 .unwrap();
346
347 join_channel(channel_id, &client2, cx2).await.unwrap();
348 cx2.executor().run_until_parked();
349
350 cx1.update(|cx| {
351 dev_server_projects::Store::global(cx).update(cx, |store, cx| {
352 store.rename_dev_server(
353 store.dev_servers().first().unwrap().id,
354 "name-edited".to_string(),
355 cx,
356 )
357 })
358 })
359 .await
360 .unwrap();
361
362 cx1.executor().run_until_parked();
363
364 cx1.update(|cx| {
365 dev_server_projects::Store::global(cx).update(cx, |store, _| {
366 assert_eq!(store.dev_servers().first().unwrap().name, "name-edited");
367 })
368 })
369}
370
371#[gpui::test]
372async fn test_dev_server_refresh_access_token(
373 cx1: &mut gpui::TestAppContext,
374 cx2: &mut gpui::TestAppContext,
375 cx3: &mut gpui::TestAppContext,
376 cx4: &mut gpui::TestAppContext,
377) {
378 let (server, client1, client2, channel_id) = TestServer::start2(cx1, cx2).await;
379
380 let (_dev_server, remote_workspace) =
381 create_dev_server_project(&server, client1.app_state.clone(), cx1, cx3).await;
382
383 cx1.update(|cx| {
384 workspace::join_channel(
385 channel_id,
386 client1.app_state.clone(),
387 Some(remote_workspace),
388 cx,
389 )
390 })
391 .await
392 .unwrap();
393 cx1.executor().run_until_parked();
394
395 remote_workspace
396 .update(cx1, |ws, cx| {
397 assert!(ws.project().read(cx).is_shared());
398 })
399 .unwrap();
400
401 join_channel(channel_id, &client2, cx2).await.unwrap();
402 cx2.executor().run_until_parked();
403
404 // Regenerate the access token
405 let new_token_response = cx1
406 .update(|cx| {
407 dev_server_projects::Store::global(cx).update(cx, |store, cx| {
408 store.regenerate_dev_server_token(store.dev_servers().first().unwrap().id, cx)
409 })
410 })
411 .await
412 .unwrap();
413
414 cx1.executor().run_until_parked();
415
416 // Assert that the other client was disconnected
417 let (workspace, cx2) = client2.active_workspace(cx2);
418 cx2.update(|cx| assert!(workspace.read(cx).project().read(cx).is_disconnected()));
419
420 // Assert that the owner of the dev server does not see the dev server as online anymore
421 let (workspace, cx1) = client1.active_workspace(cx1);
422 cx1.update(|cx| {
423 assert!(workspace.read(cx).project().read(cx).is_disconnected());
424 dev_server_projects::Store::global(cx).update(cx, |store, _| {
425 assert_eq!(
426 store.dev_servers().first().unwrap().status,
427 DevServerStatus::Offline
428 );
429 })
430 });
431
432 // Reconnect the dev server with the new token
433 let _dev_server = server
434 .create_dev_server(new_token_response.access_token, cx4)
435 .await;
436
437 cx1.executor().run_until_parked();
438
439 // Assert that the dev server is online again
440 cx1.update(|cx| {
441 dev_server_projects::Store::global(cx).update(cx, |store, _| {
442 assert_eq!(store.dev_servers().len(), 1);
443 assert_eq!(
444 store.dev_servers().first().unwrap().status,
445 DevServerStatus::Online
446 );
447 })
448 });
449}
450
451#[gpui::test]
452async fn test_dev_server_reconnect(
453 cx1: &mut gpui::TestAppContext,
454 cx2: &mut gpui::TestAppContext,
455 cx3: &mut gpui::TestAppContext,
456) {
457 let (mut server, client1) = TestServer::start1(cx1).await;
458 let channel_id = server
459 .make_channel("test", None, (&client1, cx1), &mut [])
460 .await;
461
462 let (_dev_server, remote_workspace) =
463 create_dev_server_project(&server, client1.app_state.clone(), cx1, cx3).await;
464
465 cx1.update(|cx| {
466 workspace::join_channel(
467 channel_id,
468 client1.app_state.clone(),
469 Some(remote_workspace),
470 cx,
471 )
472 })
473 .await
474 .unwrap();
475 cx1.executor().run_until_parked();
476
477 remote_workspace
478 .update(cx1, |ws, cx| {
479 assert!(ws.project().read(cx).is_shared());
480 })
481 .unwrap();
482
483 drop(client1);
484
485 let client2 = server.create_client(cx2, "user_a").await;
486
487 let store = cx2.update(|cx| dev_server_projects::Store::global(cx).clone());
488
489 store
490 .update(cx2, |store, cx| {
491 let projects = store.dev_server_projects();
492 workspace::join_dev_server_project(
493 projects[0].project_id.unwrap(),
494 client2.app_state.clone(),
495 None,
496 cx,
497 )
498 })
499 .await
500 .unwrap();
501}
502
503#[gpui::test]
504async fn test_create_dev_server_project_path_validation(
505 cx1: &mut gpui::TestAppContext,
506 cx2: &mut gpui::TestAppContext,
507 cx3: &mut gpui::TestAppContext,
508) {
509 let (server, client1) = TestServer::start1(cx1).await;
510 let _channel_id = server
511 .make_channel("test", None, (&client1, cx1), &mut [])
512 .await;
513
514 // Creating a project with a path that does exist should not fail
515 let (_dev_server, _) =
516 create_dev_server_project(&server, client1.app_state.clone(), cx1, cx2).await;
517
518 cx1.executor().run_until_parked();
519
520 let store = cx1.update(|cx| dev_server_projects::Store::global(cx).clone());
521
522 let resp = store
523 .update(cx1, |store, cx| {
524 store.create_dev_server("server-2".to_string(), cx)
525 })
526 .await
527 .unwrap();
528
529 cx1.executor().run_until_parked();
530
531 let _dev_server = server.create_dev_server(resp.access_token, cx3).await;
532
533 cx1.executor().run_until_parked();
534
535 // Creating a remote project with a path that does not exist should fail
536 let result = store
537 .update(cx1, |store, cx| {
538 store.create_dev_server_project(
539 client::DevServerId(resp.dev_server_id),
540 "/notfound".to_string(),
541 cx,
542 )
543 })
544 .await;
545
546 cx1.executor().run_until_parked();
547
548 let error = result.unwrap_err();
549 assert!(matches!(
550 error.error_code(),
551 ErrorCode::DevServerProjectPathDoesNotExist
552 ));
553}
554
555#[gpui::test]
556async fn test_save_as_remote(cx1: &mut gpui::TestAppContext, cx2: &mut gpui::TestAppContext) {
557 let (server, client1) = TestServer::start1(cx1).await;
558
559 // Creating a project with a path that does exist should not fail
560 let (dev_server, remote_workspace) =
561 create_dev_server_project(&server, client1.app_state.clone(), cx1, cx2).await;
562
563 let mut cx = VisualTestContext::from_window(remote_workspace.into(), cx1);
564
565 cx.simulate_keystrokes("cmd-p 1 enter");
566 cx.simulate_keystrokes("cmd-shift-s");
567 cx.simulate_input("2.txt");
568 cx.simulate_keystrokes("enter");
569
570 cx.executor().run_until_parked();
571
572 let title = remote_workspace
573 .update(&mut cx, |ws, cx| {
574 ws.active_item(cx).unwrap().tab_description(0, &cx).unwrap()
575 })
576 .unwrap();
577
578 assert_eq!(title, "2.txt");
579
580 let path = Path::new("/remote/2.txt");
581 assert_eq!(
582 dev_server.fs().load(&path).await.unwrap(),
583 "remote\nremote\nremote"
584 );
585}
586
587#[gpui::test]
588async fn test_new_file_remote(cx1: &mut gpui::TestAppContext, cx2: &mut gpui::TestAppContext) {
589 let (server, client1) = TestServer::start1(cx1).await;
590
591 // Creating a project with a path that does exist should not fail
592 let (dev_server, remote_workspace) =
593 create_dev_server_project(&server, client1.app_state.clone(), cx1, cx2).await;
594
595 let mut cx = VisualTestContext::from_window(remote_workspace.into(), cx1);
596
597 cx.simulate_keystrokes("cmd-n");
598 cx.simulate_input("new!");
599 cx.simulate_keystrokes("cmd-shift-s");
600 cx.simulate_input("2.txt");
601 cx.simulate_keystrokes("enter");
602
603 cx.executor().run_until_parked();
604
605 let title = remote_workspace
606 .update(&mut cx, |ws, cx| {
607 ws.active_item(cx).unwrap().tab_description(0, &cx).unwrap()
608 })
609 .unwrap();
610
611 assert_eq!(title, "2.txt");
612
613 let path = Path::new("/remote/2.txt");
614 assert_eq!(dev_server.fs().load(&path).await.unwrap(), "new!");
615}