src/tests.rs

245 lines · 7800 bytes

  1use std::collections::{BTreeMap, HashMap};
  2use std::path::PathBuf;
  3
  4use matrix_sdk::ruma::{
  5    event_id,
  6    events::room::message::{OriginalRoomMessageEvent, RoomMessageEventContent},
  7    server_name,
  8    user_id,
  9    EventId,
 10    OwnedEventId,
 11    OwnedRoomId,
 12    OwnedUserId,
 13    RoomId,
 14    UInt,
 15};
 16
 17use lazy_static::lazy_static;
 18use ratatui::style::{Color, Style};
 19use tokio::sync::mpsc::unbounded_channel;
 20use tracing::Level;
 21use url::Url;
 22
 23use crate::{
 24    base::{ChatStore, EventLocation, ProgramStore, RoomFetchStatus, RoomInfo},
 25    config::{
 26        user_color,
 27        user_style_from_color,
 28        ApplicationSettings,
 29        DirectoryValues,
 30        ProfileConfig,
 31        SortOverrides,
 32        TunableValues,
 33        UserColor,
 34        UserDisplayStyle,
 35        UserDisplayTunables,
 36    },
 37    message::{
 38        Message,
 39        MessageEvent,
 40        MessageKey,
 41        MessageTimeStamp::{LocalEcho, OriginServer},
 42        Messages,
 43    },
 44    worker::Requester,
 45};
 46
 47const TEST_ROOM1_ALIAS: &str = "#room1:example.com";
 48
 49lazy_static! {
 50    pub static ref TEST_ROOM1_ID: OwnedRoomId = RoomId::new(server_name!("example.com")).to_owned();
 51    pub static ref TEST_USER1: OwnedUserId = user_id!("@user1:example.com").to_owned();
 52    pub static ref TEST_USER2: OwnedUserId = user_id!("@user2:example.com").to_owned();
 53    pub static ref TEST_USER3: OwnedUserId = user_id!("@user3:example.com").to_owned();
 54    pub static ref TEST_USER4: OwnedUserId = user_id!("@user4:example.com").to_owned();
 55    pub static ref TEST_USER5: OwnedUserId = user_id!("@user5:example.com").to_owned();
 56    pub static ref MSG1_EVID: OwnedEventId = EventId::new(server_name!("example.com"));
 57    pub static ref MSG2_EVID: OwnedEventId = EventId::new(server_name!("example.com"));
 58    pub static ref MSG3_EVID: OwnedEventId =
 59        event_id!("$5jRz3KfVhaUzXtVj7k:example.com").to_owned();
 60    pub static ref MSG4_EVID: OwnedEventId =
 61        event_id!("$JP6qFV7WyXk5ZnexM3:example.com").to_owned();
 62    pub static ref MSG5_EVID: OwnedEventId = EventId::new(server_name!("example.com"));
 63    pub static ref MSG1_KEY: MessageKey = (LocalEcho, MSG1_EVID.clone());
 64    pub static ref MSG2_KEY: MessageKey = (OriginServer(UInt::new(1).unwrap()), MSG2_EVID.clone());
 65    pub static ref MSG3_KEY: MessageKey = (OriginServer(UInt::new(2).unwrap()), MSG3_EVID.clone());
 66    pub static ref MSG4_KEY: MessageKey = (OriginServer(UInt::new(2).unwrap()), MSG4_EVID.clone());
 67    pub static ref MSG5_KEY: MessageKey = (OriginServer(UInt::new(8).unwrap()), MSG5_EVID.clone());
 68}
 69
 70pub fn user_style(user: &str) -> Style {
 71    user_style_from_color(user_color(user))
 72}
 73
 74pub fn mock_room1_message(
 75    content: RoomMessageEventContent,
 76    sender: OwnedUserId,
 77    key: MessageKey,
 78) -> Message {
 79    let origin_server_ts = key.0.as_millis().unwrap();
 80    let event_id = key.1;
 81
 82    let event = OriginalRoomMessageEvent {
 83        content,
 84        event_id,
 85        sender,
 86        origin_server_ts,
 87        room_id: TEST_ROOM1_ID.clone(),
 88        unsigned: Default::default(),
 89    };
 90
 91    event.into()
 92}
 93
 94pub fn mock_message1() -> Message {
 95    let content = RoomMessageEventContent::text_plain("writhe");
 96    let content = MessageEvent::Local(MSG1_EVID.clone(), content.into());
 97
 98    Message::new(content, TEST_USER1.clone(), MSG1_KEY.0)
 99}
100
101pub fn mock_message2() -> Message {
102    let content = RoomMessageEventContent::text_plain("helium");
103
104    mock_room1_message(content, TEST_USER2.clone(), MSG2_KEY.clone())
105}
106
107pub fn mock_message3() -> Message {
108    let content = RoomMessageEventContent::text_plain("this\nis\na\nmultiline\nmessage");
109
110    mock_room1_message(content, TEST_USER2.clone(), MSG3_KEY.clone())
111}
112
113pub fn mock_message4() -> Message {
114    let content = RoomMessageEventContent::text_plain("help");
115
116    mock_room1_message(content, TEST_USER1.clone(), MSG4_KEY.clone())
117}
118
119pub fn mock_message5() -> Message {
120    let content = RoomMessageEventContent::text_plain("character");
121
122    mock_room1_message(content, TEST_USER2.clone(), MSG4_KEY.clone())
123}
124
125pub fn mock_keys() -> HashMap<OwnedEventId, EventLocation> {
126    let mut keys = HashMap::new();
127
128    keys.insert(MSG1_EVID.clone(), EventLocation::Message(MSG1_KEY.clone()));
129    keys.insert(MSG2_EVID.clone(), EventLocation::Message(MSG2_KEY.clone()));
130    keys.insert(MSG3_EVID.clone(), EventLocation::Message(MSG3_KEY.clone()));
131    keys.insert(MSG4_EVID.clone(), EventLocation::Message(MSG4_KEY.clone()));
132    keys.insert(MSG5_EVID.clone(), EventLocation::Message(MSG5_KEY.clone()));
133
134    keys
135}
136
137pub fn mock_messages() -> Messages {
138    let mut messages = BTreeMap::new();
139
140    messages.insert(MSG1_KEY.clone(), mock_message1());
141    messages.insert(MSG2_KEY.clone(), mock_message2());
142    messages.insert(MSG3_KEY.clone(), mock_message3());
143    messages.insert(MSG4_KEY.clone(), mock_message4());
144    messages.insert(MSG5_KEY.clone(), mock_message5());
145
146    messages
147}
148
149pub fn mock_room() -> RoomInfo {
150    RoomInfo {
151        name: Some("Watercooler Discussion".into()),
152        tags: None,
153
154        keys: mock_keys(),
155        messages: mock_messages(),
156
157        event_receipts: HashMap::new(),
158        user_receipts: HashMap::new(),
159        read_till: None,
160        reactions: HashMap::new(),
161
162        fetching: false,
163        fetch_id: RoomFetchStatus::NotStarted,
164        fetch_last: None,
165        users_typing: None,
166        display_names: HashMap::new(),
167    }
168}
169
170pub fn mock_dirs() -> DirectoryValues {
171    DirectoryValues {
172        cache: PathBuf::new(),
173        logs: PathBuf::new(),
174        downloads: None,
175        image_previews: PathBuf::new(),
176    }
177}
178
179pub fn mock_tunables() -> TunableValues {
180    TunableValues {
181        default_room: None,
182        log_level: Level::INFO,
183        reaction_display: true,
184        reaction_shortcode_display: false,
185        read_receipt_send: true,
186        read_receipt_display: true,
187        request_timeout: 120,
188        sort: SortOverrides::default().values(),
189        typing_notice_send: true,
190        typing_notice_display: true,
191        users: vec![(TEST_USER5.clone(), UserDisplayTunables {
192            color: Some(UserColor(Color::Black)),
193            name: Some("USER 5".into()),
194        })]
195        .into_iter()
196        .collect::<HashMap<_, _>>(),
197        open_command: None,
198        username_display: UserDisplayStyle::Username,
199        image_preview: None,
200    }
201}
202
203pub fn mock_settings() -> ApplicationSettings {
204    ApplicationSettings {
205        matrix_dir: PathBuf::new(),
206        layout_json: PathBuf::new(),
207        session_json: PathBuf::new(),
208
209        profile_name: "test".into(),
210        profile: ProfileConfig {
211            user_id: user_id!("@user:example.com").to_owned(),
212            url: Url::parse("https://example.com").unwrap(),
213            settings: None,
214            dirs: None,
215            layout: None,
216        },
217        tunables: mock_tunables(),
218        dirs: mock_dirs(),
219        layout: Default::default(),
220    }
221}
222
223pub async fn mock_store() -> ProgramStore {
224    let (tx, _) = unbounded_channel();
225    let homeserver = Url::parse("https://localhost").unwrap();
226    let client = matrix_sdk::Client::new(homeserver).await.unwrap();
227    let worker = Requester { tx, client };
228
229    let mut store = ChatStore::new(worker, mock_settings());
230
231    // Add presence information.
232    store.presences.get_or_default(TEST_USER1.clone());
233    store.presences.get_or_default(TEST_USER2.clone());
234    store.presences.get_or_default(TEST_USER3.clone());
235    store.presences.get_or_default(TEST_USER4.clone());
236    store.presences.get_or_default(TEST_USER5.clone());
237
238    let room_id = TEST_ROOM1_ID.clone();
239    let info = mock_room();
240
241    store.rooms.insert(room_id.clone(), info);
242    store.names.insert(TEST_ROOM1_ALIAS.to_string(), room_id);
243
244    ProgramStore::new(store)
245}