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