1use super::*;
2use util::http::FakeHttpClient;
3
4#[gpui::test]
5fn test_update_channels(cx: &mut AppContext) {
6 let http = FakeHttpClient::with_404_response();
7 let client = Client::new(http.clone(), cx);
8 let user_store = cx.add_model(|cx| UserStore::new(client.clone(), http, cx));
9
10 let channel_store = cx.add_model(|cx| ChannelStore::new(client, user_store, cx));
11
12 update_channels(
13 &channel_store,
14 proto::UpdateChannels {
15 channels: vec![
16 proto::Channel {
17 id: 1,
18 name: "b".to_string(),
19 parent_id: None,
20 },
21 proto::Channel {
22 id: 2,
23 name: "a".to_string(),
24 parent_id: None,
25 },
26 ],
27 channel_permissions: vec![proto::ChannelPermission {
28 channel_id: 1,
29 is_admin: true,
30 }],
31 ..Default::default()
32 },
33 cx,
34 );
35 assert_channels(
36 &channel_store,
37 &[
38 //
39 (0, "a".to_string(), false),
40 (0, "b".to_string(), true),
41 ],
42 cx,
43 );
44
45 update_channels(
46 &channel_store,
47 proto::UpdateChannels {
48 channels: vec![
49 proto::Channel {
50 id: 3,
51 name: "x".to_string(),
52 parent_id: Some(1),
53 },
54 proto::Channel {
55 id: 4,
56 name: "y".to_string(),
57 parent_id: Some(2),
58 },
59 ],
60 ..Default::default()
61 },
62 cx,
63 );
64 assert_channels(
65 &channel_store,
66 &[
67 (0, "a".to_string(), false),
68 (1, "y".to_string(), false),
69 (0, "b".to_string(), true),
70 (1, "x".to_string(), true),
71 ],
72 cx,
73 );
74}
75
76#[gpui::test]
77fn test_dangling_channel_paths(cx: &mut AppContext) {
78 let http = FakeHttpClient::with_404_response();
79 let client = Client::new(http.clone(), cx);
80 let user_store = cx.add_model(|cx| UserStore::new(client.clone(), http, cx));
81
82 let channel_store = cx.add_model(|cx| ChannelStore::new(client, user_store, cx));
83
84 update_channels(
85 &channel_store,
86 proto::UpdateChannels {
87 channels: vec![
88 proto::Channel {
89 id: 0,
90 name: "a".to_string(),
91 parent_id: None,
92 },
93 proto::Channel {
94 id: 1,
95 name: "b".to_string(),
96 parent_id: Some(0),
97 },
98 proto::Channel {
99 id: 2,
100 name: "c".to_string(),
101 parent_id: Some(1),
102 },
103 ],
104 channel_permissions: vec![proto::ChannelPermission {
105 channel_id: 0,
106 is_admin: true,
107 }],
108 ..Default::default()
109 },
110 cx,
111 );
112 // Sanity check
113 assert_channels(
114 &channel_store,
115 &[
116 //
117 (0, "a".to_string(), true),
118 (1, "b".to_string(), true),
119 (2, "c".to_string(), true),
120 ],
121 cx,
122 );
123
124 update_channels(
125 &channel_store,
126 proto::UpdateChannels {
127 remove_channels: vec![1, 2],
128 ..Default::default()
129 },
130 cx,
131 );
132
133 // Make sure that the 1/2/3 path is gone
134 assert_channels(&channel_store, &[(0, "a".to_string(), true)], cx);
135}
136
137fn update_channels(
138 channel_store: &ModelHandle<ChannelStore>,
139 message: proto::UpdateChannels,
140 cx: &mut AppContext,
141) {
142 let task = channel_store.update(cx, |store, cx| store.update_channels(message, cx));
143 assert!(task.is_none());
144}
145
146#[track_caller]
147fn assert_channels(
148 channel_store: &ModelHandle<ChannelStore>,
149 expected_channels: &[(usize, String, bool)],
150 cx: &AppContext,
151) {
152 let actual = channel_store.read_with(cx, |store, _| {
153 store
154 .channels()
155 .map(|(depth, channel)| {
156 (
157 depth,
158 channel.name.to_string(),
159 store.is_user_admin(channel.id),
160 )
161 })
162 .collect::<Vec<_>>()
163 });
164 assert_eq!(actual, expected_channels);
165}