-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlib.rs
More file actions
143 lines (117 loc) · 4 KB
/
Copy pathlib.rs
File metadata and controls
143 lines (117 loc) · 4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
use matrix_sdk::{
self, Client, RoomState,
config::SyncSettings,
ruma::{
OwnedRoomId, OwnedUserId,
api::client::{
account::register::v3::Request as RegistrationRequest,
room::create_room::v3::Request as CreateRoomRequest,
},
assign,
events::room::message::RoomMessageEventContent,
},
};
use microbot::{MatrixMessenger, MatrixMessengerSignals};
use ruma::api::{client::uiaa, error::ErrorKind};
use tokio::{sync::watch::Receiver, task::JoinHandle};
use tracing::instrument;
pub struct TestSetup {
pub client: Client,
pub room: OwnedRoomId,
pub bots: Vec<OwnedUserId>,
}
pub async fn make_client(homeserver: &str) -> Client {
let url = matrix_sdk::reqwest::Url::parse(homeserver).expect("Failed to parse homeserver url");
Client::new(url).await.expect("Failed to construct client")
}
#[instrument]
pub async fn register(homeserver: &str, user: &str, password: &str) -> Client {
tracing::info!("Registering user");
let client = make_client(homeserver).await;
let request = assign!(RegistrationRequest::new(), {
username: Some(user.to_string()),
password: Some(password.to_string()),
auth: Some(uiaa::AuthData::Dummy(
uiaa::Dummy::new(),
)),
});
client
.matrix_auth()
.register(request)
.await
.map(|_| ())
.or_else(|err| {
// Ignore errors reporting that the user has already been registered
if err.client_api_error_kind() == Some(&ErrorKind::UserInUse) {
return Ok(());
}
Err(err)
})
.expect("Failed to register user");
client
}
#[instrument]
pub async fn setup(homeserver: &str, sender: &str, bots: &[&str]) -> TestSetup {
let mut bot_ids = vec![];
for bot in bots {
let client = register(homeserver, bot, bot).await;
bot_ids.push(
client
.user_id()
.expect("Failed to get bot id from client")
.to_owned(),
);
}
tracing::info!("Registered bots");
let sender_client = register(homeserver, sender, sender).await;
let request = assign!(CreateRoomRequest::new(), { invite: bot_ids.clone() });
let room_resp = sender_client
.create_room(request)
.await
.expect("Failed to create test room");
tracing::info!("Created room");
sender_client
.sync_once(SyncSettings::default())
.await
.expect("Failed to sync with server");
TestSetup {
client: sender_client,
room: room_resp.room_id().to_owned(),
bots: bot_ids,
}
}
pub async fn spawn_bot(
mut bot: MatrixMessenger,
) -> (JoinHandle<()>, Receiver<MatrixMessengerSignals>) {
let user = bot.user().to_string();
let signals = bot.signals();
bot.start().await.expect("Bot failed to run to start");
tracing::info!(user, signal = ?signals.borrow(), "Started bot");
let handle: JoinHandle<()> = tokio::spawn(async move {
bot.await.expect("Bot ran to completion");
});
tracing::info!(user, signal = ?signals.borrow(), "Bot spawned");
(handle, signals)
}
impl TestSetup {
pub async fn send_cmd(&self, cmd: &str, message: &str) {
let room = self
.client
.get_room(&self.room)
.expect("Failed to find requested room");
tracing::debug!(?cmd, ?message, "Sending test command");
if room.state() == RoomState::Joined {
room.send(RoomMessageEventContent::text_plain(format!(
"!{} {}",
cmd, message
)))
.await
.expect("Failed to send test command");
} else {
panic!("Attempted to send command to a room that sender has not joined")
}
}
}