forked from lavina/lavina
add irc integration tests
This commit is contained in:
parent
b1b8ec800e
commit
315b7e638b
|
@ -232,7 +232,10 @@ impl Player {
|
||||||
connection_id,
|
connection_id,
|
||||||
body,
|
body,
|
||||||
} => {
|
} => {
|
||||||
tracing::info!("Handling incoming message");
|
tracing::info!(
|
||||||
|
"Handling incoming message, player_id={:?}",
|
||||||
|
player_id.clone()
|
||||||
|
);
|
||||||
for socket in &self.sockets {
|
for socket in &self.sockets {
|
||||||
log::info!("Send message to socket");
|
log::info!("Send message to socket");
|
||||||
socket
|
socket
|
||||||
|
|
|
@ -142,9 +142,8 @@ async fn handle_registered_socket<'a>(
|
||||||
let mut buffer = vec![];
|
let mut buffer = vec![];
|
||||||
log::info!("Handling registered user: {user:?}");
|
log::info!("Handling registered user: {user:?}");
|
||||||
|
|
||||||
let mut connection = players
|
let player_id = PlayerId(user.nickname.clone());
|
||||||
.connect_to_player(PlayerId(user.nickname.clone()))
|
let mut connection = players.connect_to_player(player_id.clone()).await;
|
||||||
.await;
|
|
||||||
|
|
||||||
ServerMessage {
|
ServerMessage {
|
||||||
tags: vec![],
|
tags: vec![],
|
||||||
|
@ -217,7 +216,7 @@ async fn handle_registered_socket<'a>(
|
||||||
match update.unwrap() {
|
match update.unwrap() {
|
||||||
Updates::RoomJoined { room_id } => {},
|
Updates::RoomJoined { room_id } => {},
|
||||||
Updates::NewMessage { author_id, connection_id, room_id, body } => {
|
Updates::NewMessage { author_id, connection_id, room_id, body } => {
|
||||||
if connection.connection_id != connection_id {
|
if player_id != author_id || connection.connection_id != connection_id {
|
||||||
ServerMessage {
|
ServerMessage {
|
||||||
tags: vec![],
|
tags: vec![],
|
||||||
sender: Some(author_id.0.clone()),
|
sender: Some(author_id.0.clone()),
|
||||||
|
|
|
@ -1,4 +1,6 @@
|
||||||
use tokio::{net::{TcpStream, tcp::{ReadHalf, WriteHalf}}, io::{BufReader, AsyncBufReadExt, AsyncWriteExt}};
|
use tokio::io::{AsyncBufReadExt, AsyncWriteExt, BufReader};
|
||||||
|
use tokio::net::tcp::{ReadHalf, WriteHalf};
|
||||||
|
use tokio::net::TcpStream;
|
||||||
|
|
||||||
use crate::prelude::*;
|
use crate::prelude::*;
|
||||||
|
|
||||||
|
@ -8,15 +10,15 @@ struct TestScope<'a> {
|
||||||
buffer: Vec<u8>,
|
buffer: Vec<u8>,
|
||||||
}
|
}
|
||||||
impl<'a> TestScope<'a> {
|
impl<'a> TestScope<'a> {
|
||||||
async fn send(&mut self, str: &str) -> Result<()> {
|
async fn send(&mut self, str: &(impl AsRef<str> + ?Sized)) -> Result<()> {
|
||||||
self.writer.write_all(str.as_bytes()).await?;
|
self.writer.write_all(str.as_ref().as_bytes()).await?;
|
||||||
self.writer.flush().await?;
|
self.writer.flush().await?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn expect(&mut self, str: &str) -> Result<()> {
|
async fn expect(&mut self, str: &(impl AsRef<str> + ?Sized)) -> Result<()> {
|
||||||
let len = self.reader.read_until(b'\n', &mut self.buffer).await?;
|
let len = self.reader.read_until(b'\n', &mut self.buffer).await?;
|
||||||
assert_eq!(std::str::from_utf8(&self.buffer[0..len])?, str);
|
assert_eq!(std::str::from_utf8(&self.buffer[0..len])?, str.as_ref());
|
||||||
self.buffer.clear();
|
self.buffer.clear();
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
@ -26,27 +28,43 @@ async fn init_client(stream: &mut TcpStream) -> Result<TestScope> {
|
||||||
let (reader, writer) = stream.split();
|
let (reader, writer) = stream.split();
|
||||||
let reader = BufReader::new(reader);
|
let reader = BufReader::new(reader);
|
||||||
let buffer = vec![];
|
let buffer = vec![];
|
||||||
Ok(TestScope { reader, writer, buffer })
|
Ok(TestScope {
|
||||||
|
reader,
|
||||||
|
writer,
|
||||||
|
buffer,
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn registration(scope: &mut TestScope<'_>) -> Result<()> {
|
macro_rules! send {
|
||||||
scope.expect(":irc.localhost NOTICE * :Welcome to my server!\n").await?;
|
($scope: expr, $($arg:tt),*) => {{
|
||||||
scope.send("NICK NickName\n").await?;
|
$scope.send(&format!($($arg,)*)).await?;
|
||||||
scope.send("USER UserName 0 * :Real Name\n").await?;
|
}};
|
||||||
scope.expect(":irc.localhost 001 NickName :Welcome to Kek Server\n").await?;
|
}
|
||||||
scope.expect(":irc.localhost 002 NickName :Welcome to Kek Server\n").await?;
|
|
||||||
scope.expect(":irc.localhost 003 NickName :Welcome to Kek Server\n").await?;
|
macro_rules! expect {
|
||||||
scope.expect(":irc.localhost 004 NickName irc.localhost kek-0.1.alpha.3 DGMQRSZagiloswz CFILPQbcefgijklmnopqrstvz bkloveqjfI\n").await?;
|
($scope: expr, $($arg:tt),*) => {{
|
||||||
scope.expect(":irc.localhost 005 NickName CHANTYPES=# :are supported by this server\n").await?;
|
$scope.expect(&format!($($arg,)*)).await?;
|
||||||
|
}};
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn registration(scope: &mut TestScope<'_>, nickname: &str) -> Result<()> {
|
||||||
|
expect!(scope, ":irc.localhost NOTICE * :Welcome to my server!\n");
|
||||||
|
send!(scope, "NICK {nickname}\n");
|
||||||
|
send!(scope, "USER UserName 0 * :Real Name\n");
|
||||||
|
expect!(scope, ":irc.localhost 001 {nickname} :Welcome to Kek Server\n");
|
||||||
|
expect!(scope, ":irc.localhost 002 {nickname} :Welcome to Kek Server\n");
|
||||||
|
expect!(scope, ":irc.localhost 003 {nickname} :Welcome to Kek Server\n");
|
||||||
|
expect!(scope, ":irc.localhost 004 {nickname} irc.localhost kek-0.1.alpha.3 DGMQRSZagiloswz CFILPQbcefgijklmnopqrstvz bkloveqjfI\n");
|
||||||
|
expect!(scope, ":irc.localhost 005 {nickname} CHANTYPES=# :are supported by this server\n");
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn join(scope: &mut TestScope<'_>) -> Result<()> {
|
async fn join(scope: &mut TestScope<'_>, nickname: &str) -> Result<()> {
|
||||||
scope.send("JOIN #channol\n").await?;
|
send!(scope, "JOIN #channol\n");
|
||||||
scope.expect(":NickName JOIN #channol\n").await?;
|
expect!(scope, ":{nickname} JOIN #channol\n");
|
||||||
scope.expect(":irc.localhost 332 NickName #channol :chan topic lol\n").await?;
|
expect!(scope, ":irc.localhost 332 {nickname} #channol :chan topic lol\n");
|
||||||
scope.expect(":irc.localhost 353 NickName = #channol :NickName\n").await?;
|
expect!(scope, ":irc.localhost 353 {nickname} = #channol :{nickname}\n");
|
||||||
scope.expect(":irc.localhost 366 NickName #channol :End of /NAMES list\n").await?;
|
expect!(scope, ":irc.localhost 366 {nickname} #channol :End of /NAMES list\n");
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -55,26 +73,45 @@ async fn test_registration() -> Result<()> {
|
||||||
let mut stream = TcpStream::connect("127.0.0.1:6667").await?;
|
let mut stream = TcpStream::connect("127.0.0.1:6667").await?;
|
||||||
let mut scope = init_client(&mut stream).await?;
|
let mut scope = init_client(&mut stream).await?;
|
||||||
|
|
||||||
registration(&mut scope).await?;
|
registration(&mut scope, "NickName").await?;
|
||||||
join(&mut scope).await?;
|
join(&mut scope, "NickName").await?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
async fn test_two_persons() -> Result<()> {
|
async fn test_two_connections_one_player() -> Result<()> {
|
||||||
let mut stream1 = TcpStream::connect("127.0.0.1:6667").await?;
|
let mut stream1 = TcpStream::connect("127.0.0.1:6667").await?;
|
||||||
let mut scope1 = init_client(&mut stream1).await?;
|
let mut scope1 = init_client(&mut stream1).await?;
|
||||||
let mut stream2 = TcpStream::connect("127.0.0.1:6667").await?;
|
let mut stream2 = TcpStream::connect("127.0.0.1:6667").await?;
|
||||||
let mut scope2 = init_client(&mut stream2).await?;
|
let mut scope2 = init_client(&mut stream2).await?;
|
||||||
|
|
||||||
registration(&mut scope1).await?;
|
registration(&mut scope1, "NickName").await?;
|
||||||
registration(&mut scope2).await?;
|
registration(&mut scope2, "NickName").await?;
|
||||||
join(&mut scope1).await?;
|
join(&mut scope1, "NickName").await?;
|
||||||
join(&mut scope2).await?;
|
join(&mut scope2, "NickName").await?;
|
||||||
scope1.send("PRIVMSG #channol :Chmoki vsem v etam chati!\n").await?;
|
send!(scope1, "PRIVMSG #channol :Chmoki vsem v etam chati!\n");
|
||||||
scope2.expect(":NickName PRIVMSG #channol :Chmoki vsem v etam chati!\n").await?;
|
expect!(scope2, ":NickName PRIVMSG #channol :Chmoki vsem v etam chati!\n");
|
||||||
scope2.send("PRIVMSG #channol :I tebe privetiki\n").await?;
|
send!(scope2, "PRIVMSG #channol :I tebe privetiki\n");
|
||||||
scope1.expect(":NickName PRIVMSG #channol :I tebe privetiki\n").await?;
|
expect!(scope1, ":NickName PRIVMSG #channol :I tebe privetiki\n");
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn test_two_players() -> Result<()> {
|
||||||
|
let mut stream1 = TcpStream::connect("127.0.0.1:6667").await?;
|
||||||
|
let mut scope1 = init_client(&mut stream1).await?;
|
||||||
|
let mut stream2 = TcpStream::connect("127.0.0.1:6667").await?;
|
||||||
|
let mut scope2 = init_client(&mut stream2).await?;
|
||||||
|
|
||||||
|
registration(&mut scope1, "NickName1").await?;
|
||||||
|
registration(&mut scope2, "NickName2").await?;
|
||||||
|
join(&mut scope1, "NickName1").await?;
|
||||||
|
join(&mut scope2, "NickName2").await?;
|
||||||
|
send!(scope1, "PRIVMSG #channol :Chmoki vsem v etam chati!\n");
|
||||||
|
expect!(scope2, ":NickName1 PRIVMSG #channol :Chmoki vsem v etam chati!\n");
|
||||||
|
send!(scope2, "PRIVMSG #channol :I tebe privetiki\n");
|
||||||
|
expect!(scope1, ":NickName2 PRIVMSG #channol :I tebe privetiki\n");
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
Loading…
Reference in New Issue