multi_workspace_tests.rs

  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().collect();
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().count(), 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().collect();
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().collect();
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}