src/tests.rs
266 lines · 8914 bytes
1use std::path::PathBuf;
2use std::{collections::HashMap, iter::FromIterator as _};
3
4use matrix_sdk::ruma::{
5 event_id,
6 events::room::message::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 ratatui::style::{Color, Style};
19use serde_json::{Map, Value};
20use tokio::sync::mpsc::unbounded_channel;
21
22use crate::{
23 base::{ChatStore, EventLocation, ProgramStore, RoomInfo},
24 config::{
25 user_color,
26 user_style_from_color,
27 ApplicationSettings,
28 DirectoryValues,
29 Encryption,
30 Notifications,
31 NotifyVia,
32 ProfileConfig,
33 SortOverrides,
34 Terminal,
35 TunableValues,
36 UserColor,
37 UserDisplayStyle,
38 UserDisplayTunables,
39 },
40 message::{
41 Message,
42 MessageEvent,
43 MessageKey,
44 MessageTimeStamp::{LocalEcho, OriginServer},
45 Messages,
46 },
47 worker::Requester,
48};
49
50const TEST_ROOM1_ALIAS: &str = "#room1:example.com";
51
52lazy_static! {
53 pub static ref TEST_ROOM1_ID: OwnedRoomId =
54 RoomId::new_v1(server_name!("example.com")).to_owned();
55 pub static ref TEST_USER1: OwnedUserId = user_id!("@user1:example.com").to_owned();
56 pub static ref TEST_USER2: OwnedUserId = user_id!("@user2:example.com").to_owned();
57 pub static ref TEST_USER3: OwnedUserId = user_id!("@user3:example.com").to_owned();
58 pub static ref TEST_USER4: OwnedUserId = user_id!("@user4:example.com").to_owned();
59 pub static ref TEST_USER5: OwnedUserId = user_id!("@user5:example.com").to_owned();
60 pub static ref MSG1_EVID: OwnedEventId = EventId::new_v1(server_name!("example.com"));
61 pub static ref MSG2_EVID: OwnedEventId = EventId::new_v1(server_name!("example.com"));
62 pub static ref MSG3_EVID: OwnedEventId =
63 event_id!("$5jRz3KfVhaUzXtVj7k:example.com").to_owned();
64 pub static ref MSG4_EVID: OwnedEventId =
65 event_id!("$JP6qFV7WyXk5ZnexM3:example.com").to_owned();
66 pub static ref MSG5_EVID: OwnedEventId = EventId::new_v1(server_name!("example.com"));
67 pub static ref MSG1_KEY: MessageKey = (LocalEcho, MSG1_EVID.clone());
68 pub static ref MSG2_KEY: MessageKey = (OriginServer(UInt::new(1).unwrap()), MSG2_EVID.clone());
69 pub static ref MSG3_KEY: MessageKey = (OriginServer(UInt::new(2).unwrap()), MSG3_EVID.clone());
70 pub static ref MSG4_KEY: MessageKey = (OriginServer(UInt::new(2).unwrap()), MSG4_EVID.clone());
71 pub static ref MSG5_KEY: MessageKey = (OriginServer(UInt::new(8).unwrap()), MSG5_EVID.clone());
72}
73
74pub fn user_style(user: &str) -> Style {
75 user_style_from_color(user_color(user))
76}
77
78pub fn mock_room1_message(
79 content: RoomMessageEventContent,
80 sender: OwnedUserId,
81 key: MessageKey,
82) -> Message {
83 let timestamp = key.0.as_millis().unwrap();
84 let event_id = key.1;
85
86 let event = serde_json::from_value(Value::Object(Map::from_iter([
87 ("type".to_owned(), Value::String("m.room.message".into())),
88 ("content".to_owned(), serde_json::to_value(&content).unwrap()),
89 ("event_id".to_owned(), serde_json::to_value(&event_id).unwrap()),
90 ("sender".to_owned(), serde_json::to_value(&sender).unwrap()),
91 ("origin_server_ts".to_owned(), serde_json::to_value(timestamp).unwrap()),
92 ("room_id".to_owned(), serde_json::to_value(&*TEST_ROOM1_ID).unwrap()),
93 ])))
94 .unwrap();
95
96 Message::new(MessageEvent::Original(event), sender, timestamp.into())
97}
98
99pub fn mock_message1() -> Message {
100 let content = RoomMessageEventContent::text_plain("writhe");
101 let content = MessageEvent::Local(MSG1_EVID.clone(), content.into());
102
103 Message::new(content, TEST_USER1.clone(), MSG1_KEY.0)
104}
105
106pub fn mock_message2() -> Message {
107 let content = RoomMessageEventContent::text_plain("helium");
108
109 mock_room1_message(content, TEST_USER2.clone(), MSG2_KEY.clone())
110}
111
112pub fn mock_message3() -> Message {
113 let content = RoomMessageEventContent::text_plain("this\nis\na\nmultiline\nmessage");
114
115 mock_room1_message(content, TEST_USER2.clone(), MSG3_KEY.clone())
116}
117
118pub fn mock_message4() -> Message {
119 let content = RoomMessageEventContent::text_plain("help");
120
121 mock_room1_message(content, TEST_USER1.clone(), MSG4_KEY.clone())
122}
123
124pub fn mock_message5() -> Message {
125 let content = RoomMessageEventContent::text_plain("character");
126
127 mock_room1_message(content, TEST_USER2.clone(), MSG4_KEY.clone())
128}
129
130pub fn mock_keys() -> HashMap<OwnedEventId, EventLocation> {
131 let mut keys = HashMap::new();
132
133 keys.insert(MSG1_EVID.clone(), EventLocation::Message(None, MSG1_KEY.clone()));
134 keys.insert(MSG2_EVID.clone(), EventLocation::Message(None, MSG2_KEY.clone()));
135 keys.insert(MSG3_EVID.clone(), EventLocation::Message(None, MSG3_KEY.clone()));
136 keys.insert(MSG4_EVID.clone(), EventLocation::Message(None, MSG4_KEY.clone()));
137 keys.insert(MSG5_EVID.clone(), EventLocation::Message(None, MSG5_KEY.clone()));
138
139 keys
140}
141
142pub fn mock_messages() -> Messages {
143 let mut messages = Messages::main();
144
145 messages.insert(MSG1_KEY.clone(), mock_message1());
146 messages.insert(MSG2_KEY.clone(), mock_message2());
147 messages.insert(MSG3_KEY.clone(), mock_message3());
148 messages.insert(MSG4_KEY.clone(), mock_message4());
149 messages.insert(MSG5_KEY.clone(), mock_message5());
150
151 messages
152}
153
154pub fn mock_room() -> RoomInfo {
155 let mut room = RoomInfo::default();
156 room.name = Some("Watercooler Discussion".into());
157 room.keys = mock_keys();
158 *room.get_thread_mut(None) = mock_messages();
159 room
160}
161
162pub fn mock_dirs() -> DirectoryValues {
163 DirectoryValues {
164 cache: PathBuf::new(),
165 data: PathBuf::new(),
166 logs: PathBuf::new(),
167 downloads: None,
168 image_previews: PathBuf::new(),
169 }
170}
171
172pub fn mock_tunables() -> TunableValues {
173 TunableValues {
174 default_room: None,
175 encryption: Encryption::default().values(),
176 log_level: "warn".into(),
177 max_log_files: 7,
178 message_shortcode_display: false,
179 normal_after_send: true,
180 reaction_display: true,
181 reaction_shortcode_display: false,
182 read_receipt_send: true,
183 read_receipt_display: true,
184 request_timeout: 120,
185 sort: SortOverrides::default().values(),
186 state_event_display: true,
187 terminal: Terminal::default().values(),
188 typing_notice_send: true,
189 typing_notice_display: true,
190 users: vec![(TEST_USER5.clone(), UserDisplayTunables {
191 color: Some(UserColor(Color::Black)),
192 name: Some("USER 5".into()),
193 })]
194 .into_iter()
195 .collect::<HashMap<_, _>>(),
196 open_command: None,
197 external_edit_file_suffix: String::from(".md"),
198 username_display: UserDisplayStyle::Username,
199 message_user_color: false,
200 mouse: Default::default(),
201 notifications: Notifications {
202 enabled: false,
203 via: NotifyVia::default(),
204 show_message: true,
205 sound_hint: None,
206 },
207 image_preview: None,
208 user_gutter_width: 30,
209 tabstop: 4,
210 ssl_verify: true,
211 }
212}
213
214pub fn mock_settings() -> ApplicationSettings {
215 ApplicationSettings {
216 layout_json: PathBuf::new(),
217 session_json: PathBuf::new(),
218 session_json_old: PathBuf::new(),
219 sled_dir: PathBuf::new(),
220 sqlite_dir: PathBuf::new(),
221
222 profile_name: "test".into(),
223 profile: ProfileConfig {
224 user_id: user_id!("@user:example.com").to_owned(),
225 password_file: None,
226 url: None,
227 settings: None,
228 dirs: None,
229 layout: None,
230 macros: None,
231 },
232 tunables: mock_tunables(),
233 dirs: mock_dirs(),
234 layout: Default::default(),
235 macros: HashMap::default(),
236 }
237}
238
239pub async fn mock_store() -> ProgramStore {
240 let (tx, _) = unbounded_channel();
241 let client = matrix_sdk::Client::builder()
242 .homeserver_url("https://localhost")
243 // don't panic if no certs are available like in a nix build sandbox
244 .disable_ssl_verification()
245 .build()
246 .await
247 .unwrap();
248 let worker = Requester { tx, client };
249
250 let mut store = ChatStore::new(worker, mock_settings());
251
252 // Add presence information.
253 store.presences.get_or_default(TEST_USER1.clone());
254 store.presences.get_or_default(TEST_USER2.clone());
255 store.presences.get_or_default(TEST_USER3.clone());
256 store.presences.get_or_default(TEST_USER4.clone());
257 store.presences.get_or_default(TEST_USER5.clone());
258
259 let room_id = TEST_ROOM1_ID.clone();
260 let info = mock_room();
261
262 store.rooms.insert(room_id.clone(), info);
263 store.names.insert(TEST_ROOM1_ALIAS.to_string(), room_id);
264
265 ProgramStore::new(store)
266}