1use crate::{attach_modal::Candidate, *};
2use attach_modal::AttachModal;
3use dap::{client::SessionId, FakeAdapter};
4use gpui::{BackgroundExecutor, TestAppContext, VisualTestContext};
5use menu::Confirm;
6use project::{FakeFs, Project};
7use serde_json::json;
8use task::{AttachConfig, DebugTaskDefinition, TCPHost};
9use tests::{init_test, init_test_workspace};
10
11#[gpui::test]
12async fn test_direct_attach_to_process(executor: BackgroundExecutor, cx: &mut TestAppContext) {
13 init_test(cx);
14
15 let fs = FakeFs::new(executor.clone());
16
17 fs.insert_tree(
18 "/project",
19 json!({
20 "main.rs": "First line\nSecond line\nThird line\nFourth line",
21 }),
22 )
23 .await;
24
25 let project = Project::test(fs, ["/project".as_ref()], cx).await;
26 let workspace = init_test_workspace(&project, cx).await;
27 let cx = &mut VisualTestContext::from_window(*workspace, cx);
28
29 let task = project.update(cx, |project, cx| {
30 project.fake_debug_session(
31 dap::DebugRequestType::Attach(AttachConfig {
32 process_id: Some(10),
33 }),
34 None,
35 false,
36 cx,
37 )
38 });
39
40 let session = task.await.unwrap();
41
42 cx.run_until_parked();
43
44 // assert we didn't show the attach modal
45 workspace
46 .update(cx, |workspace, _window, cx| {
47 assert!(workspace.active_modal::<AttachModal>(cx).is_none());
48 })
49 .unwrap();
50
51 let shutdown_session = project.update(cx, |project, cx| {
52 project.dap_store().update(cx, |dap_store, cx| {
53 dap_store.shutdown_session(session.read(cx).session_id(), cx)
54 })
55 });
56
57 shutdown_session.await.unwrap();
58}
59
60#[gpui::test]
61async fn test_show_attach_modal_and_select_process(
62 executor: BackgroundExecutor,
63 cx: &mut TestAppContext,
64) {
65 init_test(cx);
66
67 let fs = FakeFs::new(executor.clone());
68
69 fs.insert_tree(
70 "/project",
71 json!({
72 "main.rs": "First line\nSecond line\nThird line\nFourth line",
73 }),
74 )
75 .await;
76
77 let project = Project::test(fs, ["/project".as_ref()], cx).await;
78 let workspace = init_test_workspace(&project, cx).await;
79 let cx = &mut VisualTestContext::from_window(*workspace, cx);
80
81 let attach_modal = workspace
82 .update(cx, |workspace, window, cx| {
83 workspace.toggle_modal(window, cx, |window, cx| {
84 AttachModal::with_processes(
85 project.clone(),
86 DebugTaskDefinition {
87 adapter: FakeAdapter::ADAPTER_NAME.into(),
88 request: dap::DebugRequestType::Attach(AttachConfig::default()),
89 label: "attach example".into(),
90 initialize_args: None,
91 tcp_connection: Some(TCPHost::default()),
92 },
93 vec![
94 Candidate {
95 pid: 0,
96 name: "fake-binary-1".into(),
97 command: vec![],
98 },
99 Candidate {
100 pid: 3,
101 name: "non-fake-binary-1".into(),
102 command: vec![],
103 },
104 Candidate {
105 pid: 1,
106 name: "fake-binary-2".into(),
107 command: vec![],
108 },
109 ],
110 window,
111 cx,
112 )
113 });
114
115 workspace.active_modal::<AttachModal>(cx).unwrap()
116 })
117 .unwrap();
118
119 cx.run_until_parked();
120
121 // assert we got the expected processes
122 workspace
123 .update(cx, |_, _, cx| {
124 let names =
125 attach_modal.update(cx, |modal, cx| attach_modal::_process_names(&modal, cx));
126
127 // we filtered out all processes that are not starting with `fake-binary`
128 assert_eq!(2, names.len());
129 })
130 .unwrap();
131
132 // select the only existing process
133 cx.dispatch_action(Confirm);
134
135 cx.run_until_parked();
136
137 // assert attach modal was dismissed
138 workspace
139 .update(cx, |workspace, _window, cx| {
140 assert!(workspace.active_modal::<AttachModal>(cx).is_none());
141 })
142 .unwrap();
143
144 let shutdown_session = project.update(cx, |project, cx| {
145 project.dap_store().update(cx, |dap_store, cx| {
146 let session = dap_store.session_by_id(SessionId(0)).unwrap();
147
148 dap_store.shutdown_session(session.read(cx).session_id(), cx)
149 })
150 });
151
152 shutdown_session.await.unwrap();
153}