From 10f9937dc3f90d1c01601ec450ada50311e0cd68 Mon Sep 17 00:00:00 2001 From: Tarasov Aleksandr <55220741+arabianq@users.noreply.github.com> Date: Sat, 25 Apr 2026 14:37:55 +0300 Subject: [PATCH] tests: parse_command set_volume edge cases (#73) Add unit tests for parse_command in src/utils/commands.rs to ensure robust handling of set_volume edge cases including missing or invalid volume and id arguments. Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com> --- src/utils/commands.rs | 85 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 85 insertions(+) diff --git a/src/utils/commands.rs b/src/utils/commands.rs index 8864e07..b44e231 100644 --- a/src/utils/commands.rs +++ b/src/utils/commands.rs @@ -122,3 +122,88 @@ pub fn parse_command(request: &Request) -> Option> { _ => None, } } + +#[cfg(test)] +mod tests { + use super::*; + use crate::types::socket::Request; + use std::collections::HashMap; + + #[test] + fn test_parse_set_volume_valid() { + let mut args = HashMap::new(); + args.insert("volume".to_string(), "0.5".to_string()); + args.insert("id".to_string(), "1".to_string()); + let request = Request { + name: "set_volume".to_string(), + args, + }; + + let cmd = parse_command(&request); + assert!(cmd.is_some()); + } + + #[test] + fn test_parse_set_volume_missing_volume() { + let mut args = HashMap::new(); + args.insert("id".to_string(), "1".to_string()); + let request = Request { + name: "set_volume".to_string(), + args, + }; + + let cmd = parse_command(&request); + assert!(cmd.is_some()); + } + + #[test] + fn test_parse_set_volume_invalid_volume() { + let mut args = HashMap::new(); + args.insert("volume".to_string(), "not-a-float".to_string()); + let request = Request { + name: "set_volume".to_string(), + args, + }; + + let cmd = parse_command(&request); + assert!(cmd.is_some()); + } + + #[test] + fn test_parse_set_volume_missing_id() { + let mut args = HashMap::new(); + args.insert("volume".to_string(), "0.5".to_string()); + let request = Request { + name: "set_volume".to_string(), + args, + }; + + let cmd = parse_command(&request); + assert!(cmd.is_some()); + } + + #[test] + fn test_parse_set_volume_invalid_id() { + let mut args = HashMap::new(); + args.insert("id".to_string(), "not-an-int".to_string()); + args.insert("volume".to_string(), "0.5".to_string()); + let request = Request { + name: "set_volume".to_string(), + args, + }; + + let cmd = parse_command(&request); + assert!(cmd.is_some()); + } + + #[test] + fn test_parse_set_volume_empty_args() { + let request = Request { + name: "set_volume".to_string(), + args: HashMap::new(), + }; + + let cmd = parse_command(&request); + assert!(cmd.is_some()); + } +}