1use super::*;
2use fs::FakeFs;
3use gpui::TestAppContext;
4use project::{DisableAiSettings, ProjectGroupKey};
5use serde_json::json;
6use settings::SettingsStore;
7
8fn init_test(cx: &mut TestAppContext) {
9 cx.update(|cx| {
10 let settings_store = SettingsStore::test(cx);
11 cx.set_global(settings_store);
12 theme_settings::init(theme::LoadThemes::JustBase, cx);
13 DisableAiSettings::register(cx);
14 });
15}
16
17#[gpui::test]
18async fn test_sidebar_disabled_when_disable_ai_is_enabled(cx: &mut TestAppContext) {
19 init_test(cx);
20 let fs = FakeFs::new(cx.executor());
21 let project = Project::test(fs, [], cx).await;
22
23 let (multi_workspace, cx) =
24 cx.add_window_view(|window, cx| MultiWorkspace::test_new(project, window, cx));
25
26 multi_workspace.read_with(cx, |mw, cx| {
27 assert!(mw.multi_workspace_enabled(cx));
28 });
29
30 multi_workspace.update_in(cx, |mw, _window, cx| {
31 mw.open_sidebar(cx);
32 assert!(mw.sidebar_open());
33 });
34
35 cx.update(|_window, cx| {
36 DisableAiSettings::override_global(DisableAiSettings { disable_ai: true }, cx);
37 });
38 cx.run_until_parked();
39
40 multi_workspace.read_with(cx, |mw, cx| {
41 assert!(
42 !mw.sidebar_open(),
43 "Sidebar should be closed when disable_ai is true"
44 );
45 assert!(
46 !mw.multi_workspace_enabled(cx),
47 "Multi-workspace should be disabled when disable_ai is true"
48 );
49 });
50
51 multi_workspace.update_in(cx, |mw, window, cx| {
52 mw.toggle_sidebar(window, cx);
53 });
54 multi_workspace.read_with(cx, |mw, _cx| {
55 assert!(
56 !mw.sidebar_open(),
57 "Sidebar should remain closed when toggled with disable_ai true"
58 );
59 });
60
61 cx.update(|_window, cx| {
62 DisableAiSettings::override_global(DisableAiSettings { disable_ai: false }, cx);
63 });
64 cx.run_until_parked();
65
66 multi_workspace.read_with(cx, |mw, cx| {
67 assert!(
68 mw.multi_workspace_enabled(cx),
69 "Multi-workspace should be enabled after re-enabling AI"
70 );
71 assert!(
72 !mw.sidebar_open(),
73 "Sidebar should still be closed after re-enabling AI (not auto-opened)"
74 );
75 });
76
77 multi_workspace.update_in(cx, |mw, window, cx| {
78 mw.toggle_sidebar(window, cx);
79 });
80 multi_workspace.read_with(cx, |mw, _cx| {
81 assert!(
82 mw.sidebar_open(),
83 "Sidebar should open when toggled after re-enabling AI"
84 );
85 });
86}
87
88#[gpui::test]
89async fn test_project_group_keys_initial(cx: &mut TestAppContext) {
90 init_test(cx);
91 let fs = FakeFs::new(cx.executor());
92 fs.insert_tree("/root_a", json!({ "file.txt": "" })).await;
93 let project = Project::test(fs, ["/root_a".as_ref()], cx).await;
94
95 let expected_key = project.read_with(cx, |project, cx| project.project_group_key(cx));
96
97 let (multi_workspace, cx) =
98 cx.add_window_view(|window, cx| MultiWorkspace::test_new(project, window, cx));
99
100 multi_workspace.update(cx, |mw, cx| {
101 mw.open_sidebar(cx);
102 });
103
104 multi_workspace.read_with(cx, |mw, cx| {
105 let keys: Vec<ProjectGroupKey> = mw.project_group_keys(cx);
106 assert_eq!(keys.len(), 1, "should have exactly one key on creation");
107 assert_eq!(keys[0], expected_key);
108 });
109}
110
111#[gpui::test]
112async fn test_project_group_keys_add_workspace(cx: &mut TestAppContext) {
113 init_test(cx);
114 let fs = FakeFs::new(cx.executor());
115 fs.insert_tree("/root_a", json!({ "file.txt": "" })).await;
116 fs.insert_tree("/root_b", json!({ "file.txt": "" })).await;
117 let project_a = Project::test(fs.clone(), ["/root_a".as_ref()], cx).await;
118 let project_b = Project::test(fs.clone(), ["/root_b".as_ref()], cx).await;
119
120 let key_a = project_a.read_with(cx, |p, cx| p.project_group_key(cx));
121 let key_b = project_b.read_with(cx, |p, cx| p.project_group_key(cx));
122 assert_ne!(
123 key_a, key_b,
124 "different roots should produce different keys"
125 );
126
127 let (multi_workspace, cx) =
128 cx.add_window_view(|window, cx| MultiWorkspace::test_new(project_a, window, cx));
129
130 multi_workspace.update(cx, |mw, cx| {
131 mw.open_sidebar(cx);
132 });
133
134 multi_workspace.read_with(cx, |mw, cx| {
135 assert_eq!(mw.project_group_keys(cx).len(), 1);
136 });
137
138 // Adding a workspace with a different project root adds a new key.
139 multi_workspace.update_in(cx, |mw, window, cx| {
140 mw.test_add_workspace(project_b, window, cx);
141 });
142
143 multi_workspace.read_with(cx, |mw, cx| {
144 let keys: Vec<ProjectGroupKey> = mw.project_group_keys(cx);
145 assert_eq!(
146 keys.len(),
147 2,
148 "should have two keys after adding a second workspace"
149 );
150 assert_eq!(keys[0], key_b);
151 assert_eq!(keys[1], key_a);
152 });
153}
154
155#[gpui::test]
156async fn test_project_group_keys_duplicate_not_added(cx: &mut TestAppContext) {
157 init_test(cx);
158 let fs = FakeFs::new(cx.executor());
159 fs.insert_tree("/root_a", json!({ "file.txt": "" })).await;
160 let project_a = Project::test(fs.clone(), ["/root_a".as_ref()], cx).await;
161 // A second project entity pointing at the same path produces the same key.
162 let project_a2 = Project::test(fs.clone(), ["/root_a".as_ref()], cx).await;
163
164 let key_a = project_a.read_with(cx, |p, cx| p.project_group_key(cx));
165 let key_a2 = project_a2.read_with(cx, |p, cx| p.project_group_key(cx));
166 assert_eq!(key_a, key_a2, "same root path should produce the same key");
167
168 let (multi_workspace, cx) =
169 cx.add_window_view(|window, cx| MultiWorkspace::test_new(project_a, window, cx));
170
171 multi_workspace.update(cx, |mw, cx| {
172 mw.open_sidebar(cx);
173 });
174
175 multi_workspace.update_in(cx, |mw, window, cx| {
176 mw.test_add_workspace(project_a2, window, cx);
177 });
178
179 multi_workspace.read_with(cx, |mw, cx| {
180 let keys: Vec<ProjectGroupKey> = mw.project_group_keys(cx);
181 assert_eq!(
182 keys.len(),
183 1,
184 "duplicate key should not be added when a workspace with the same root is inserted"
185 );
186 });
187}
188
189#[gpui::test]
190async fn test_groups_with_same_paths_merge(cx: &mut TestAppContext) {
191 init_test(cx);
192 let fs = FakeFs::new(cx.executor());
193 fs.insert_tree("/a", json!({ "file.txt": "" })).await;
194 fs.insert_tree("/b", json!({ "file.txt": "" })).await;
195 let project_a = Project::test(fs.clone(), ["/a".as_ref()], cx).await;
196 let project_b = Project::test(fs.clone(), ["/b".as_ref()], cx).await;
197
198 let (multi_workspace, cx) =
199 cx.add_window_view(|window, cx| MultiWorkspace::test_new(project_a, window, cx));
200
201 // Open the sidebar so workspaces get grouped.
202 multi_workspace.update(cx, |mw, cx| {
203 mw.open_sidebar(cx);
204 });
205 cx.run_until_parked();
206
207 // Add a second workspace, creating group_b with path [/b].
208 let group_a_id = multi_workspace.update_in(cx, |mw, window, cx| {
209 let group_a_id = mw.project_groups()[0].read(cx).id;
210 mw.test_add_workspace(project_b, window, cx);
211 group_a_id
212 });
213 cx.run_until_parked();
214
215 // Now add /b to group_a so it has [/a, /b].
216 multi_workspace.update(cx, |mw, cx| {
217 mw.add_folders_to_project_group(group_a_id, vec!["/b".into()], cx);
218 });
219 cx.run_until_parked();
220
221 // Verify we have two groups.
222 multi_workspace.read_with(cx, |mw, _cx| {
223 assert_eq!(
224 mw.project_groups().len(),
225 2,
226 "should have two groups before the merge"
227 );
228 });
229
230 // Remove /a from group_a, making its key [/b] — same as group_b.
231 multi_workspace.update(cx, |mw, cx| {
232 mw.remove_folder_from_project_group(group_a_id, Path::new("/a"), cx);
233 });
234 cx.run_until_parked();
235
236 // The two groups now have identical keys [/b] and should have been merged.
237 multi_workspace.read_with(cx, |mw, _cx| {
238 assert_eq!(
239 mw.project_groups().len(),
240 1,
241 "groups with identical paths should be merged into one"
242 );
243 });
244}