src/tests.rs

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