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