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