src/tests.rs

254 lines · 8124 bytes

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