channel_store_tests.rs

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