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