Verify that the tabletop can be set and retrieved

This commit is contained in:
Savanni D'Gerinel 2024-11-24 09:35:25 -05:00
parent 71b114c9b2
commit cadb3ab435
4 changed files with 35 additions and 11 deletions

View File

@ -5,6 +5,7 @@ use std::{
};
use mime::Mime;
use serde::{Deserialize, Serialize};
use thiserror::Error;
#[derive(Debug, Error)]
@ -30,7 +31,7 @@ impl From<std::io::Error> for Error {
}
}
#[derive(Clone, Debug, Hash, Eq, PartialEq)]
#[derive(Clone, Debug, Deserialize, Eq, Hash, PartialEq, Serialize)]
pub struct AssetId(String);
impl Display for AssetId {

View File

@ -15,6 +15,12 @@ use crate::{
types::{AppError, Message, Tabletop, RGB},
};
const DEFAULT_BACKGROUND_COLOR: RGB = RGB {
red: 0xca,
green: 0xb9,
blue: 0xbb,
};
#[derive(Debug)]
struct WebsocketClient {
sender: Option<UnboundedSender<Message>>,
@ -39,11 +45,7 @@ impl Core {
asset_db: Box::new(assetdb),
clients: HashMap::new(),
tabletop: Tabletop {
background_color: RGB {
red: 0xca,
green: 0xb9,
blue: 0xbb,
},
background_color: DEFAULT_BACKGROUND_COLOR,
background_image: None,
},
})))
@ -112,10 +114,10 @@ impl Core {
.collect()
}
pub fn set_background_image(&self, path: String) -> Result<(), AppError> {
pub fn set_background_image(&self, asset: AssetId) -> Result<(), AppError> {
let tabletop = {
let mut state = self.0.write().unwrap();
state.tabletop.background_image = Some(path.clone());
state.tabletop.background_image = Some(asset.clone());
state.tabletop.clone()
};
self.publish(Message::UpdateTabletop(tabletop));
@ -187,4 +189,23 @@ mod test {
assert_eq!(data, "abcdefg".as_bytes());
});
}
#[tokio::test]
async fn it_can_retrieve_the_default_tabletop() {
let core = test_core();
assert_matches!(core.tabletop(), Tabletop{ background_color, background_image } => {
assert_eq!(background_color, DEFAULT_BACKGROUND_COLOR);
assert_eq!(background_image, None);
});
}
#[tokio::test]
async fn it_can_change_the_tabletop_background() {
let core = test_core();
assert_matches!(core.set_background_image(AssetId::from("asset_1")), Ok(()));
assert_matches!(core.tabletop(), Tabletop{ background_color, background_image } => {
assert_eq!(background_color, DEFAULT_BACKGROUND_COLOR);
assert_eq!(background_image, Some(AssetId::from("asset_1")));
});
}
}

View File

@ -143,7 +143,7 @@ pub async fn handle_connect_websocket(
pub async fn handle_set_background_image(core: Core, image_name: String) -> impl Reply {
handler(async move {
let _ = core.set_background_image(image_name);
let _ = core.set_background_image(AssetId::from(image_name));
Ok(Response::builder()
.header("Access-Control-Allow-Origin", "*")

View File

@ -2,6 +2,8 @@ use serde::{Deserialize, Serialize};
use typeshare::typeshare;
use crate::asset_db::AssetId;
#[derive(Debug)]
pub enum AppError {
NotFound(String),
@ -10,7 +12,7 @@ pub enum AppError {
UnexpectedError(String),
}
#[derive(Clone, Debug, Deserialize, Serialize)]
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
#[serde(rename_all = "camelCase")]
#[typeshare]
pub struct RGB {
@ -24,7 +26,7 @@ pub struct RGB {
#[typeshare]
pub struct Tabletop {
pub background_color: RGB,
pub background_image: Option<String>,
pub background_image: Option<AssetId>,
}
#[derive(Clone, Debug, Deserialize, Serialize)]