src/tests.rs

227 lines · 7206 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        fetching: false,
157        fetch_id: RoomFetchStatus::NotStarted,
158        fetch_last: None,
159        users_typing: None,
160    }
161}
162
163pub fn mock_dirs() -> DirectoryValues {
164    DirectoryValues {
165        cache: PathBuf::new(),
166        logs: PathBuf::new(),
167        downloads: PathBuf::new(),
168    }
169}
170
171pub fn mock_tunables() -> TunableValues {
172    TunableValues {
173        default_room: None,
174        reaction_display: true,
175        reaction_shortcode_display: false,
176        read_receipt_send: true,
177        read_receipt_display: true,
178        request_timeout: 120,
179        typing_notice_send: true,
180        typing_notice_display: true,
181        users: vec![(TEST_USER5.clone(), UserDisplayTunables {
182            color: Some(UserColor(Color::Black)),
183            name: Some("USER 5".into()),
184        })]
185        .into_iter()
186        .collect::<HashMap<_, _>>(),
187    }
188}
189
190pub fn mock_settings() -> ApplicationSettings {
191    ApplicationSettings {
192        matrix_dir: PathBuf::new(),
193        session_json: PathBuf::new(),
194        profile_name: "test".into(),
195        profile: ProfileConfig {
196            user_id: user_id!("@user:example.com").to_owned(),
197            url: Url::parse("https://example.com").unwrap(),
198            settings: None,
199            dirs: None,
200        },
201        tunables: mock_tunables(),
202        dirs: mock_dirs(),
203    }
204}
205
206pub async fn mock_store() -> ProgramStore {
207    let (tx, _) = unbounded_channel();
208    let homeserver = Url::parse("https://localhost").unwrap();
209    let client = matrix_sdk::Client::new(homeserver).await.unwrap();
210    let worker = Requester { tx, client };
211
212    let mut store = ChatStore::new(worker, mock_settings());
213
214    // Add presence information.
215    store.presences.get_or_default(TEST_USER1.clone());
216    store.presences.get_or_default(TEST_USER2.clone());
217    store.presences.get_or_default(TEST_USER3.clone());
218    store.presences.get_or_default(TEST_USER4.clone());
219    store.presences.get_or_default(TEST_USER5.clone());
220
221    let room_id = TEST_ROOM1_ID.clone();
222    let info = mock_room();
223
224    store.rooms.insert(room_id, info);
225
226    ProgramStore::new(store)
227}