src/tests.rs

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