1use crate::{attach_modal::Candidate, tests::start_debug_session_with, *};
2use attach_modal::AttachModal;
3use dap::FakeAdapter;
4use gpui::{BackgroundExecutor, TestAppContext, VisualTestContext};
5use menu::Confirm;
6use project::{FakeFs, Project};
7use serde_json::json;
8use task::{AttachRequest, DebugTaskDefinition, TcpArgumentsTemplate};
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 session = start_debug_session_with(
30 &workspace,
31 cx,
32 DebugTaskDefinition {
33 adapter: "fake-adapter".to_string(),
34 request: dap::DebugRequest::Attach(AttachRequest {
35 process_id: Some(10),
36 }),
37 label: "label".to_string(),
38 initialize_args: None,
39 tcp_connection: None,
40 stop_on_entry: None,
41 },
42 |client| {
43 client.on_request::<dap::requests::Attach, _>(move |_, args| {
44 assert_eq!(json!({"request": "attach", "process_id": 10}), args.raw);
45
46 Ok(())
47 });
48 },
49 )
50 .unwrap();
51
52 cx.run_until_parked();
53
54 // assert we didn't show the attach modal
55 workspace
56 .update(cx, |workspace, _window, cx| {
57 assert!(workspace.active_modal::<AttachModal>(cx).is_none());
58 })
59 .unwrap();
60
61 let shutdown_session = project.update(cx, |project, cx| {
62 project.dap_store().update(cx, |dap_store, cx| {
63 dap_store.shutdown_session(session.read(cx).session_id(), cx)
64 })
65 });
66
67 shutdown_session.await.unwrap();
68}
69
70#[gpui::test]
71async fn test_show_attach_modal_and_select_process(
72 executor: BackgroundExecutor,
73 cx: &mut TestAppContext,
74) {
75 init_test(cx);
76
77 let fs = FakeFs::new(executor.clone());
78
79 fs.insert_tree(
80 "/project",
81 json!({
82 "main.rs": "First line\nSecond line\nThird line\nFourth line",
83 }),
84 )
85 .await;
86
87 let project = Project::test(fs, ["/project".as_ref()], cx).await;
88 let workspace = init_test_workspace(&project, cx).await;
89 let cx = &mut VisualTestContext::from_window(*workspace, cx);
90 // Set up handlers for sessions spawned via modal.
91 let _initialize_subscription =
92 project::debugger::test::intercept_debug_sessions(cx, |client| {
93 client.on_request::<dap::requests::Attach, _>(move |_, args| {
94 assert_eq!(json!({"request": "attach", "process_id": 1}), args.raw);
95
96 Ok(())
97 });
98 });
99 let attach_modal = workspace
100 .update(cx, |workspace, window, cx| {
101 let workspace_handle = cx.entity();
102 workspace.toggle_modal(window, cx, |window, cx| {
103 AttachModal::with_processes(
104 workspace_handle,
105 DebugTaskDefinition {
106 adapter: FakeAdapter::ADAPTER_NAME.into(),
107 request: dap::DebugRequest::Attach(AttachRequest::default()),
108 label: "attach example".into(),
109 initialize_args: None,
110 tcp_connection: Some(TcpArgumentsTemplate::default()),
111 stop_on_entry: None,
112 },
113 vec![
114 Candidate {
115 pid: 0,
116 name: "fake-binary-1".into(),
117 command: vec![],
118 },
119 Candidate {
120 pid: 3,
121 name: "real-binary-1".into(),
122 command: vec![],
123 },
124 Candidate {
125 pid: 1,
126 name: "fake-binary-2".into(),
127 command: vec![],
128 },
129 ]
130 .into_iter()
131 .collect(),
132 true,
133 window,
134 cx,
135 )
136 });
137
138 workspace.active_modal::<AttachModal>(cx).unwrap()
139 })
140 .unwrap();
141
142 cx.run_until_parked();
143
144 // assert we got the expected processes
145 workspace
146 .update(cx, |_, window, cx| {
147 let names =
148 attach_modal.update(cx, |modal, cx| attach_modal::_process_names(&modal, cx));
149 // Initially all processes are visible.
150 assert_eq!(3, names.len());
151 attach_modal.update(cx, |this, cx| {
152 this.picker.update(cx, |this, cx| {
153 this.set_query("fakb", window, cx);
154 })
155 })
156 })
157 .unwrap();
158 cx.run_until_parked();
159 // assert we got the expected processes
160 workspace
161 .update(cx, |_, _, cx| {
162 let names =
163 attach_modal.update(cx, |modal, cx| attach_modal::_process_names(&modal, cx));
164 // Initially all processes are visible.
165 assert_eq!(2, names.len());
166 })
167 .unwrap();
168 // select the only existing process
169 cx.dispatch_action(Confirm);
170
171 cx.run_until_parked();
172
173 // assert attach modal was dismissed
174 workspace
175 .update(cx, |workspace, _window, cx| {
176 assert!(workspace.active_modal::<AttachModal>(cx).is_none());
177 })
178 .unwrap();
179}