1use crate::TestServer;
2use collab_ui::CollabPanel;
3use collab_ui::collab_panel::{MoveChannelDown, MoveChannelUp, ToggleSelectedChannelFavorite};
4use gpui::TestAppContext;
5use menu::{SelectNext, SelectPrevious};
6
7#[gpui::test]
8async fn test_reorder_favorite_channels_independently_of_channels(cx: &mut TestAppContext) {
9 let (server, client) = TestServer::start1(cx).await;
10 let root = server
11 .make_channel("root", None, (&client, cx), &mut [])
12 .await;
13 let _ = server
14 .make_channel("channel-a", Some(root), (&client, cx), &mut [])
15 .await;
16 let _ = server
17 .make_channel("channel-b", Some(root), (&client, cx), &mut [])
18 .await;
19 let _ = server
20 .make_channel("channel-c", Some(root), (&client, cx), &mut [])
21 .await;
22
23 let (workspace, cx) = client.build_test_workspace(cx).await;
24 let panel = workspace.update_in(cx, |workspace, window, cx| {
25 let panel = CollabPanel::new(workspace, window, cx);
26 workspace.add_panel(panel.clone(), window, cx);
27 panel
28 });
29 cx.run_until_parked();
30
31 // Verify initial state.
32 assert_eq!(
33 panel.read_with(cx, |panel, _| panel.entries_as_strings()),
34 &[
35 "[Channels]",
36 " v root",
37 " #️⃣ channel-a",
38 " #️⃣ channel-b",
39 " #️⃣ channel-c",
40 "[Contacts]",
41 ]
42 );
43
44 // Select channel-b.
45 panel.update_in(cx, |panel, window, cx| {
46 panel.select_next(&SelectNext, window, cx);
47 panel.select_next(&SelectNext, window, cx);
48 panel.select_next(&SelectNext, window, cx);
49 panel.select_next(&SelectNext, window, cx);
50 });
51 assert_eq!(
52 panel.read_with(cx, |panel, _| panel.entries_as_strings()),
53 &[
54 "[Channels]",
55 " v root",
56 " #️⃣ channel-a",
57 " #️⃣ channel-b <== selected",
58 " #️⃣ channel-c",
59 "[Contacts]",
60 ]
61 );
62
63 // Favorite channel-b.
64 panel.update_in(cx, |panel, window, cx| {
65 panel.toggle_selected_channel_favorite(&ToggleSelectedChannelFavorite, window, cx);
66 });
67 assert_eq!(
68 panel.read_with(cx, |panel, _| panel.entries_as_strings()),
69 &[
70 "[Favorites]",
71 " #️⃣ channel-b",
72 "[Channels]",
73 " v root",
74 " #️⃣ channel-a",
75 " #️⃣ channel-b <== selected",
76 " #️⃣ channel-c",
77 "[Contacts]",
78 ]
79 );
80
81 // Select channel-c.
82 panel.update_in(cx, |panel, window, cx| {
83 panel.select_next(&SelectNext, window, cx);
84 });
85 // Favorite channel-c.
86 panel.update_in(cx, |panel, window, cx| {
87 panel.toggle_selected_channel_favorite(&ToggleSelectedChannelFavorite, window, cx);
88 });
89 assert_eq!(
90 panel.read_with(cx, |panel, _| panel.entries_as_strings()),
91 &[
92 "[Favorites]",
93 " #️⃣ channel-b",
94 " #️⃣ channel-c",
95 "[Channels]",
96 " v root",
97 " #️⃣ channel-a",
98 " #️⃣ channel-b",
99 " #️⃣ channel-c <== selected",
100 "[Contacts]",
101 ]
102 );
103
104 // Navigate up to favorite channel-b .
105 panel.update_in(cx, |panel, window, cx| {
106 panel.select_previous(&SelectPrevious, window, cx);
107 panel.select_previous(&SelectPrevious, window, cx);
108 panel.select_previous(&SelectPrevious, window, cx);
109 panel.select_previous(&SelectPrevious, window, cx);
110 panel.select_previous(&SelectPrevious, window, cx);
111 panel.select_previous(&SelectPrevious, window, cx);
112 });
113 assert_eq!(
114 panel.read_with(cx, |panel, _| panel.entries_as_strings()),
115 &[
116 "[Favorites]",
117 " #️⃣ channel-b <== selected",
118 " #️⃣ channel-c",
119 "[Channels]",
120 " v root",
121 " #️⃣ channel-a",
122 " #️⃣ channel-b",
123 " #️⃣ channel-c",
124 "[Contacts]",
125 ]
126 );
127
128 // Move favorite channel-b down.
129 // The Channels section should remain unchanged
130 panel.update_in(cx, |panel, window, cx| {
131 panel.move_channel_down(&MoveChannelDown, window, cx);
132 });
133 assert_eq!(
134 panel.read_with(cx, |panel, _| panel.entries_as_strings()),
135 &[
136 "[Favorites]",
137 " #️⃣ channel-c",
138 " #️⃣ channel-b <== selected",
139 "[Channels]",
140 " v root",
141 " #️⃣ channel-a",
142 " #️⃣ channel-b",
143 " #️⃣ channel-c",
144 "[Contacts]",
145 ]
146 );
147
148 // Move favorite channel-b down again when it's already last (should be no-op).
149 panel.update_in(cx, |panel, window, cx| {
150 panel.move_channel_down(&MoveChannelDown, window, cx);
151 });
152 assert_eq!(
153 panel.read_with(cx, |panel, _| panel.entries_as_strings()),
154 &[
155 "[Favorites]",
156 " #️⃣ channel-c",
157 " #️⃣ channel-b <== selected",
158 "[Channels]",
159 " v root",
160 " #️⃣ channel-a",
161 " #️⃣ channel-b",
162 " #️⃣ channel-c",
163 "[Contacts]",
164 ]
165 );
166
167 // Move favorite channel-b back up.
168 // The Channels section should remain unchanged.
169 panel.update_in(cx, |panel, window, cx| {
170 panel.move_channel_up(&MoveChannelUp, window, cx);
171 });
172 assert_eq!(
173 panel.read_with(cx, |panel, _| panel.entries_as_strings()),
174 &[
175 "[Favorites]",
176 " #️⃣ channel-b <== selected",
177 " #️⃣ channel-c",
178 "[Channels]",
179 " v root",
180 " #️⃣ channel-a",
181 " #️⃣ channel-b",
182 " #️⃣ channel-c",
183 "[Contacts]",
184 ]
185 );
186
187 // Move favorite channel-b up again when it's already first (should be no-op).
188 panel.update_in(cx, |panel, window, cx| {
189 panel.move_channel_up(&MoveChannelUp, window, cx);
190 });
191 assert_eq!(
192 panel.read_with(cx, |panel, _| panel.entries_as_strings()),
193 &[
194 "[Favorites]",
195 " #️⃣ channel-b <== selected",
196 " #️⃣ channel-c",
197 "[Channels]",
198 " v root",
199 " #️⃣ channel-a",
200 " #️⃣ channel-b",
201 " #️⃣ channel-c",
202 "[Contacts]",
203 ]
204 );
205
206 // Unfavorite channel-b.
207 // Selection should move to the next favorite (channel-c).
208 panel.update_in(cx, |panel, window, cx| {
209 panel.toggle_selected_channel_favorite(&ToggleSelectedChannelFavorite, window, cx);
210 });
211 assert_eq!(
212 panel.read_with(cx, |panel, _| panel.entries_as_strings()),
213 &[
214 "[Favorites]",
215 " #️⃣ channel-c <== selected",
216 "[Channels]",
217 " v root",
218 " #️⃣ channel-a",
219 " #️⃣ channel-b",
220 " #️⃣ channel-c",
221 "[Contacts]",
222 ]
223 );
224
225 // Unfavorite channel-c.
226 // Favorites section should disappear entirely.
227 // Selection should move to the next available item.
228 panel.update_in(cx, |panel, window, cx| {
229 panel.toggle_selected_channel_favorite(&ToggleSelectedChannelFavorite, window, cx);
230 });
231 assert_eq!(
232 panel.read_with(cx, |panel, _| panel.entries_as_strings()),
233 &[
234 "[Channels]",
235 " v root <== selected",
236 " #️⃣ channel-a",
237 " #️⃣ channel-b",
238 " #️⃣ channel-c",
239 "[Contacts]",
240 ]
241 );
242}
243
244#[gpui::test]
245async fn test_reorder_channels_independently_of_favorites(cx: &mut TestAppContext) {
246 let (server, client) = TestServer::start1(cx).await;
247 let root = server
248 .make_channel("root", None, (&client, cx), &mut [])
249 .await;
250 let _ = server
251 .make_channel("channel-a", Some(root), (&client, cx), &mut [])
252 .await;
253 let _ = server
254 .make_channel("channel-b", Some(root), (&client, cx), &mut [])
255 .await;
256 let _ = server
257 .make_channel("channel-c", Some(root), (&client, cx), &mut [])
258 .await;
259
260 let (workspace, cx) = client.build_test_workspace(cx).await;
261 let panel = workspace.update_in(cx, |workspace, window, cx| {
262 let panel = CollabPanel::new(workspace, window, cx);
263 workspace.add_panel(panel.clone(), window, cx);
264 panel
265 });
266 cx.run_until_parked();
267
268 // Select channel-a.
269 panel.update_in(cx, |panel, window, cx| {
270 panel.select_next(&SelectNext, window, cx);
271 panel.select_next(&SelectNext, window, cx);
272 panel.select_next(&SelectNext, window, cx);
273 });
274 assert_eq!(
275 panel.read_with(cx, |panel, _| panel.entries_as_strings()),
276 &[
277 "[Channels]",
278 " v root",
279 " #️⃣ channel-a <== selected",
280 " #️⃣ channel-b",
281 " #️⃣ channel-c",
282 "[Contacts]",
283 ]
284 );
285
286 // Favorite channel-a.
287 panel.update_in(cx, |panel, window, cx| {
288 panel.toggle_selected_channel_favorite(&ToggleSelectedChannelFavorite, window, cx);
289 });
290
291 // Select channel-b.
292 // Favorite channel-b.
293 panel.update_in(cx, |panel, window, cx| {
294 panel.select_next(&SelectNext, window, cx);
295 panel.toggle_selected_channel_favorite(&ToggleSelectedChannelFavorite, window, cx);
296 });
297 cx.run_until_parked();
298
299 assert_eq!(
300 panel.read_with(cx, |panel, _| panel.entries_as_strings()),
301 &[
302 "[Favorites]",
303 " #️⃣ channel-a",
304 " #️⃣ channel-b",
305 "[Channels]",
306 " v root",
307 " #️⃣ channel-a",
308 " #️⃣ channel-b <== selected",
309 " #️⃣ channel-c",
310 "[Contacts]",
311 ]
312 );
313
314 // Select channel-a in the Channels section.
315 panel.update_in(cx, |panel, window, cx| {
316 panel.select_previous(&SelectPrevious, window, cx);
317 });
318 assert_eq!(
319 panel.read_with(cx, |panel, _| panel.entries_as_strings()),
320 &[
321 "[Favorites]",
322 " #️⃣ channel-a",
323 " #️⃣ channel-b",
324 "[Channels]",
325 " v root",
326 " #️⃣ channel-a <== selected",
327 " #️⃣ channel-b",
328 " #️⃣ channel-c",
329 "[Contacts]",
330 ]
331 );
332
333 // Move channel-a down.
334 // The Favorites section should remain unchanged.
335 // Selection should remain on channel-a in the Channels section,
336 // not jump to channel-a in Favorites.
337 panel.update_in(cx, |panel, window, cx| {
338 panel.move_channel_down(&MoveChannelDown, window, cx);
339 });
340 cx.run_until_parked();
341
342 assert_eq!(
343 panel.read_with(cx, |panel, _| panel.entries_as_strings()),
344 &[
345 "[Favorites]",
346 " #️⃣ channel-a",
347 " #️⃣ channel-b",
348 "[Channels]",
349 " v root",
350 " #️⃣ channel-b",
351 " #️⃣ channel-a <== selected",
352 " #️⃣ channel-c",
353 "[Contacts]",
354 ]
355 );
356}