Solstice client.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 

390 lines
9.6 KiB

use crate::room::Room;
use serde::{Deserialize, Serialize};
use solstice_proto::User;
/// This enumeration is the list of possible control responses from the client
/// to the controller.
#[derive(Debug, PartialEq, Eq, Serialize, Deserialize)]
pub enum Response {
LoginStatusResponse(LoginStatusResponse),
RoomJoinResponse(RoomJoinResponse),
RoomLeaveResponse(RoomLeaveResponse),
RoomListResponse(RoomListResponse),
RoomMessageResponse(RoomMessageResponse),
RoomUserJoinedResponse(RoomUserJoinedResponse),
RoomUserLeftResponse(RoomUserLeftResponse),
UserInfoResponse(UserInfoResponse),
UserListResponse(UserListResponse),
}
#[derive(Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct RoomJoinResponse {
pub room_name: String,
pub room: Room,
}
#[derive(Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct RoomLeaveResponse {
pub room_name: String,
}
/// This enumeration is the list of possible login states, and the associated
/// information.
#[derive(Debug, PartialEq, Eq, Serialize, Deserialize)]
pub enum LoginStatusResponse {
/// The login request has been sent to the server, but the response hasn't
/// been received yet.
Pending {
/// The username used to log in.
username: String,
},
/// Login was successful.
Success {
/// The username used to log in.
username: String,
/// The message of the day sent by the server.
motd: String,
},
/// Login failed.
Failure {
/// The username used to log in.
username: String,
/// The reason the server gave for refusing the login request.
reason: String,
},
}
/// This structure contains the list of all visible rooms, and their associated
/// data.
#[derive(Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct RoomListResponse {
/// The list of (room name, room data) pairs.
pub rooms: Vec<(String, Room)>,
}
/// This structure contains a message said in a chat room the user is a member
/// of.
#[derive(Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct RoomMessageResponse {
/// The name of the room in which the message was said.
pub room_name: String,
/// The name of the user who said the message.
pub user_name: String,
/// The message itself.
pub message: String,
}
/// This struct describes the fact that the given user joined the given room.
#[derive(Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct RoomUserJoinedResponse {
pub room_name: String,
pub user_name: String,
}
/// This struct describes the fact that the given user left the given room.
#[derive(Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct RoomUserLeftResponse {
pub room_name: String,
pub user_name: String,
}
/// This struct contains the last known information about a given user.
#[derive(Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct UserInfoResponse {
pub user_name: String,
pub user_info: User,
}
/// This stuct contains the last known information about every user.
#[derive(Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct UserListResponse {
pub user_list: Vec<(String, User)>,
}
#[cfg(test)]
mod tests {
use std::collections::HashSet;
use solstice_proto::{User, UserStatus};
use crate::room::{Membership, Room, Visibility};
use super::{
LoginStatusResponse, RoomJoinResponse, RoomLeaveResponse, RoomListResponse,
RoomMessageResponse, RoomUserJoinedResponse, RoomUserLeftResponse,
UserInfoResponse, UserListResponse,
};
#[test]
fn deserialize_login_status_response_pending() {
assert_eq!(
serde_json::from_str::<LoginStatusResponse>(
r#"{
"Pending": { "username": "karandeep" }
}"#
)
.unwrap(),
LoginStatusResponse::Pending {
username: "karandeep".to_string()
}
);
}
#[test]
fn deserialize_login_status_response_success() {
assert_eq!(
serde_json::from_str::<LoginStatusResponse>(
r#"{
"Success": {
"username": "karandeep",
"motd": "message of the day"
}
}"#
)
.unwrap(),
LoginStatusResponse::Success {
username: "karandeep".to_string(),
motd: "message of the day".to_string(),
}
);
}
#[test]
fn deserialize_login_status_response_failure() {
assert_eq!(
serde_json::from_str::<LoginStatusResponse>(
r#"{
"Failure": {
"username": "karandeep",
"reason": "garbage!"
}
}"#
)
.unwrap(),
LoginStatusResponse::Failure {
username: "karandeep".to_string(),
reason: "garbage!".to_string(),
}
);
}
#[test]
fn deserialize_room_join_response() {
assert_eq!(
serde_json::from_str::<RoomJoinResponse>(
r#"{
"room_name": "bleep",
"room": {
"membership": "Joining",
"visibility": "PrivateOwned",
"operated": false,
"user_count": 3,
"owner": null,
"operators": [],
"members": [],
"messages": [],
"tickers": []
}
}"#
)
.unwrap(),
RoomJoinResponse {
room_name: "bleep".to_string(),
room: Room {
membership: Membership::Joining,
visibility: Visibility::PrivateOwned,
operated: false,
user_count: 3,
owner: None,
operators: HashSet::new(),
members: HashSet::new(),
messages: vec![],
tickers: vec![],
}
}
);
}
#[test]
fn deserialize_room_leave_response() {
assert_eq!(
serde_json::from_str::<RoomLeaveResponse>(r#"{ "room_name": "bleep" }"#)
.unwrap(),
RoomLeaveResponse {
room_name: "bleep".to_string(),
}
);
}
#[test]
fn deserialize_room_message_response() {
assert_eq!(
serde_json::from_str::<RoomMessageResponse>(
r#"{
"room_name": "bleep",
"user_name": "karandeep",
"message": "namaste"
}"#
)
.unwrap(),
RoomMessageResponse {
room_name: "bleep".to_string(),
user_name: "karandeep".to_string(),
message: "namaste".to_string(),
}
);
}
#[test]
fn deserialize_room_list_response() {
assert_eq!(
serde_json::from_str::<RoomListResponse>(
r#"{
"rooms": [
["bleep", {
"membership": "Joining",
"visibility": "PrivateOwned",
"operated": false,
"user_count": 3,
"owner": null,
"operators": [],
"members": [],
"messages": [],
"tickers": []
}]
]
}"#
)
.unwrap(),
RoomListResponse {
rooms: vec![(
"bleep".to_string(),
Room {
membership: Membership::Joining,
visibility: Visibility::PrivateOwned,
operated: false,
user_count: 3,
owner: None,
operators: HashSet::new(),
members: HashSet::new(),
messages: vec![],
tickers: vec![],
}
)],
}
);
}
#[test]
fn deserialize_room_user_joined_response() {
assert_eq!(
serde_json::from_str::<RoomUserJoinedResponse>(
r#"{
"room_name": "bleep",
"user_name": "karandeep"
}"#
)
.unwrap(),
RoomUserJoinedResponse {
room_name: "bleep".to_string(),
user_name: "karandeep".to_string(),
}
);
}
#[test]
fn deserialize_room_user_left_response() {
assert_eq!(
serde_json::from_str::<RoomUserLeftResponse>(
r#"{
"room_name": "bleep",
"user_name": "karandeep"
}"#
)
.unwrap(),
RoomUserLeftResponse {
room_name: "bleep".to_string(),
user_name: "karandeep".to_string(),
}
);
}
#[test]
fn deserialize_user_info_response() {
assert_eq!(
serde_json::from_str::<UserInfoResponse>(
r#"{
"user_name": "karandeep",
"user_info": {
"name": "karandeep",
"status": "Online",
"average_speed": 1,
"num_downloads": 2,
"unknown": 3,
"num_files": 4,
"num_folders": 5,
"num_free_slots": 6,
"country": "IN"
}
}"#
)
.unwrap(),
UserInfoResponse {
user_name: "karandeep".to_string(),
user_info: User {
name: "karandeep".to_string(),
status: UserStatus::Online,
average_speed: 1,
num_downloads: 2,
unknown: 3,
num_files: 4,
num_folders: 5,
num_free_slots: 6,
country: "IN".to_string(),
}
}
);
}
#[test]
fn deserialize_user_list_response() {
assert_eq!(
serde_json::from_str::<UserListResponse>(
r#"{
"user_list": [
["karandeep", {
"name": "karandeep",
"status": "Online",
"average_speed": 1,
"num_downloads": 2,
"unknown": 3,
"num_files": 4,
"num_folders": 5,
"num_free_slots": 6,
"country": "IN"
}]
]
}"#
)
.unwrap(),
UserListResponse {
user_list: vec![(
"karandeep".to_string(),
User {
name: "karandeep".to_string(),
status: UserStatus::Online,
average_speed: 1,
num_downloads: 2,
unknown: 3,
num_files: 4,
num_folders: 5,
num_free_slots: 6,
country: "IN".to_string(),
}
)],
}
);
}
}