src/tests.rs

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