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 mime::Mime;
use serde::{Deserialize, Serialize};
use thiserror::Error; use thiserror::Error;
#[derive(Debug, 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); pub struct AssetId(String);
impl Display for AssetId { impl Display for AssetId {

View File

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

View File

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