src/tests.rs

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