diff --git a/src/commands/score.rs b/src/commands/score.rs index 0c4eac6..0a3f1d1 100644 --- a/src/commands/score.rs +++ b/src/commands/score.rs @@ -28,17 +28,18 @@ pub async fn score(_ctx: Context<'_>) -> Result<(), Error> { async fn magic_impl( ctx: &mut C, files: Vec, -) -> Result<(), Error> { +) -> Result, Error> { let user = get_user!(ctx); let files = ctx.download_images(&files).await?; if files.len() == 0 { ctx.reply("No images found attached to message").await?; - return Ok(()); + return Ok(vec![]); } let mut embeds = Vec::with_capacity(files.len()); let mut attachments = Vec::with_capacity(files.len()); + let mut plays = Vec::with_capacity(files.len()); let mut analyzer = ImageAnalyzer::default(); for (i, (attachment, bytes)) in files.into_iter().enumerate() { @@ -116,6 +117,7 @@ async fn magic_impl( play.to_embed(ctx.data(), &user, &song, &chart, i, None)? }); + plays.push(play); embeds.push(embed); attachments.extend(attachment); // }}} @@ -133,7 +135,7 @@ async fn magic_impl( .await?; } - Ok(()) + Ok(plays) } // }}} // {{{ Tests @@ -142,10 +144,16 @@ mod magic_tests { use std::path::PathBuf; - use crate::with_test_ctx; + use crate::{ + arcaea::score::ScoringSystem, commands::discord::mock::MockContext, with_test_ctx, + }; use super::*; + fn play_song_title<'a>(ctx: &'a MockContext, play: &'a Play) -> Result<&'a str, Error> { + Ok(&ctx.data().song_cache.lookup_chart(play.chart_id)?.0.title) + } + #[tokio::test] async fn no_pics() -> Result<(), Error> { with_test_ctx!("test/commands/score/magic/no_pics", async |ctx| { @@ -156,29 +164,45 @@ mod magic_tests { #[tokio::test] async fn simple_pic() -> Result<(), Error> { - with_test_ctx!("test/commands/score/magic/single_pic", async |ctx| { - magic_impl( - ctx, - vec![PathBuf::from_str("test/screenshots/alter_ego.jpg")?], - ) - .await?; - Ok(()) - }) + with_test_ctx!( + "test/commands/score/magic/single_pic", + async |ctx: &mut MockContext| { + let plays = magic_impl( + ctx, + vec![PathBuf::from_str("test/screenshots/alter_ego.jpg")?], + ) + .await?; + assert_eq!(plays.len(), 1); + assert_eq!(plays[0].score(ScoringSystem::Standard).0, 9926250); + assert_eq!(play_song_title(ctx, &plays[0])?, "ALTER EGO"); + Ok(()) + } + ) } #[tokio::test] async fn weird_kerning() -> Result<(), Error> { - with_test_ctx!("test/commands/score/magic/weird_kerning", async |ctx| { - magic_impl( - ctx, - vec![ - PathBuf::from_str("test/screenshots/antithese_74_kerning.jpg")?, - PathBuf::from_str("test/screenshots/genocider_24_kerning.jpg")?, - ], - ) - .await?; - Ok(()) - }) + with_test_ctx!( + "test/commands/score/magic/weird_kerning", + async |ctx: &mut MockContext| { + let plays = magic_impl( + ctx, + vec![ + PathBuf::from_str("test/screenshots/antithese_74_kerning.jpg")?, + PathBuf::from_str("test/screenshots/genocider_24_kerning.jpg")?, + ], + ) + .await?; + + assert_eq!(plays.len(), 2); + assert_eq!(plays[0].score(ScoringSystem::Standard).0, 9983744); + assert_eq!(play_song_title(ctx, &plays[0])?, "Antithese"); + assert_eq!(plays[1].score(ScoringSystem::Standard).0, 9724775); + assert_eq!(play_song_title(ctx, &plays[1])?, "GENOCIDER"); + + Ok(()) + } + ) } } // }}} diff --git a/src/commands/utils/mod.rs b/src/commands/utils/mod.rs index 9f17135..9b4594c 100644 --- a/src/commands/utils/mod.rs +++ b/src/commands/utils/mod.rs @@ -31,13 +31,16 @@ macro_rules! assert_is_pookie { #[macro_export] macro_rules! reply_errors { - ($ctx:expr, $value:expr) => { + ($ctx:expr, $default:expr, $value:expr) => { match $value { Ok(v) => v, Err(err) => { crate::commands::discord::MessageContext::reply($ctx, &format!("{err}")).await?; - return Ok(()); + return Ok($default); } } }; + ($ctx:expr, $value:expr) => { + crate::reply_errors!($ctx, Default::default(), $value) + }; }