1use std::sync::Arc;
2
3use anyhow::{Context as _, Result};
4use dap::adapters::DebugTaskDefinition;
5use dap::client::DebugAdapterClient;
6use gpui::{Entity, TestAppContext, WindowHandle};
7use project::{Project, debugger::session::Session};
8use settings::SettingsStore;
9use task::TaskContext;
10use terminal_view::terminal_panel::TerminalPanel;
11use workspace::Workspace;
12
13use crate::{debugger_panel::DebugPanel, session::DebugSession};
14
15#[cfg(test)]
16mod attach_modal;
17#[cfg(test)]
18mod console;
19#[cfg(test)]
20mod dap_logger;
21#[cfg(test)]
22mod debugger_panel;
23#[cfg(test)]
24mod inline_values;
25#[cfg(test)]
26mod module_list;
27#[cfg(test)]
28mod new_process_modal;
29#[cfg(test)]
30mod persistence;
31#[cfg(test)]
32mod stack_frame_list;
33#[cfg(test)]
34mod variable_list;
35
36pub fn init_test(cx: &mut gpui::TestAppContext) {
37 #[cfg(test)]
38 zlog::init_test();
39
40 cx.update(|cx| {
41 let settings = SettingsStore::test(cx);
42 cx.set_global(settings);
43 terminal_view::init(cx);
44 theme::init(theme::LoadThemes::JustBase, cx);
45 command_palette_hooks::init(cx);
46 language::init(cx);
47 workspace::init_settings(cx);
48 Project::init_settings(cx);
49 editor::init(cx);
50 crate::init(cx);
51 dap_adapters::init(cx);
52 });
53}
54
55pub async fn init_test_workspace(
56 project: &Entity<Project>,
57 cx: &mut TestAppContext,
58) -> WindowHandle<Workspace> {
59 let workspace_handle =
60 cx.add_window(|window, cx| Workspace::test_new(project.clone(), window, cx));
61
62 let debugger_panel = workspace_handle
63 .update(cx, |_, window, cx| {
64 cx.spawn_in(window, async move |this, cx| {
65 DebugPanel::load(this, cx).await
66 })
67 })
68 .unwrap()
69 .await
70 .expect("Failed to load debug panel");
71
72 let terminal_panel = workspace_handle
73 .update(cx, |_, window, cx| {
74 cx.spawn_in(window, async |this, cx| {
75 TerminalPanel::load(this, cx.clone()).await
76 })
77 })
78 .unwrap()
79 .await
80 .expect("Failed to load terminal panel");
81
82 workspace_handle
83 .update(cx, |workspace, window, cx| {
84 workspace.add_panel(debugger_panel, window, cx);
85 workspace.add_panel(terminal_panel, window, cx);
86 })
87 .unwrap();
88 workspace_handle
89}
90
91#[track_caller]
92pub fn active_debug_session_panel(
93 workspace: WindowHandle<Workspace>,
94 cx: &mut TestAppContext,
95) -> Entity<DebugSession> {
96 workspace
97 .update(cx, |workspace, _window, cx| {
98 let debug_panel = workspace.panel::<DebugPanel>(cx).unwrap();
99 debug_panel
100 .update(cx, |this, _| this.active_session())
101 .unwrap()
102 })
103 .unwrap()
104}
105
106pub fn start_debug_session_with<T: Fn(&Arc<DebugAdapterClient>) + 'static>(
107 workspace: &WindowHandle<Workspace>,
108 cx: &mut gpui::TestAppContext,
109 config: DebugTaskDefinition,
110 configure: T,
111) -> Result<Entity<Session>> {
112 let _subscription = project::debugger::test::intercept_debug_sessions(cx, configure);
113 workspace.update(cx, |workspace, window, cx| {
114 workspace.start_debug_session(
115 config.to_scenario(),
116 TaskContext::default(),
117 None,
118 None,
119 window,
120 cx,
121 )
122 })?;
123 cx.run_until_parked();
124 let session = workspace.read_with(cx, |workspace, cx| {
125 workspace
126 .panel::<DebugPanel>(cx)
127 .and_then(|panel| panel.read(cx).active_session())
128 .map(|session| session.read(cx).running_state().read(cx).session())
129 .cloned()
130 .context("Failed to get active session")
131 })??;
132
133 Ok(session)
134}
135
136pub fn start_debug_session<T: Fn(&Arc<DebugAdapterClient>) + 'static>(
137 workspace: &WindowHandle<Workspace>,
138 cx: &mut gpui::TestAppContext,
139 configure: T,
140) -> Result<Entity<Session>> {
141 use serde_json::json;
142
143 start_debug_session_with(
144 workspace,
145 cx,
146 DebugTaskDefinition {
147 adapter: "fake-adapter".into(),
148 label: "test".into(),
149 config: json!({
150 "request": "launch"
151 }),
152 tcp_connection: None,
153 },
154 configure,
155 )
156}