matroskin 0.1.0

whatsminer api client lib
Documentation
//! Implement `set.miner.fastboot` command
//!
//! This command is used to set the fastboot flag for the miner.
//!
//! - Command: [SetMinerFastboot]
//! - ApiDoc: <https://apidoc.whatsminer.com/#api-Miner-btminer_set_fastboot>
use core::str;

use crate::{
    command::{Command, CommandLevel},
    error::Result,
};

/// This command represents the `set.miner.fastboot` operation.
///
/// It is used to set the fastboot flag for the miner.
///
/// - ApiDoc: <https://apidoc.whatsminer.com/#api-Miner-btminer_set_fastboot>
///
/// # Example
/// ```rust,no_run
/// use matroskin::actor::Actor;
/// use matroskin::command::set_miner_fastboot::SetMinerFastboot;
/// use matroskin::account::Account;
/// use matroskin::password::Password;
///
/// #[tokio::main]
/// async fn main() -> Result<(), Box<dyn std::error::Error>> {
///     let addr = "10.10.10.10:4433";
///     let username = Account::Super;
///     let password = Password::Super;
///
///     let actor = Actor::new(addr, username, password);
///     actor.connect()?;
///
///     let cmd = SetMinerFastboot::new(true); // Enable fastboot
///     let response = actor.send(cmd).await?;
///     println!("Response: {:#?}", response);
///
///     Ok(())
/// }
/// ```
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
pub struct SetMinerFastboot {
    /// Whether fastboot is enabled
    pub enabled: bool,
}

impl SetMinerFastboot {
    /// Create a new `set.miner.fastboot` command.
    pub const fn new(enabled: bool) -> Self {
        Self { enabled }
    }
}

impl Command for SetMinerFastboot {
    type Response = String;
    const CMD_NAME: &'static str = "set.miner.fastboot";
    const LEVEL: CommandLevel = CommandLevel::Secured;
    fn params(&self) -> Result<Option<impl serde::Serialize>> {
        Ok(Some(if self.enabled { "enable" } else { "disable" }))
    }
}

#[cfg(test)]
mod tests {
    use crate::account::Account;
    use crate::auth_data::AuthData;

    use super::*;

    const RESPONSE_JSON: &str =
        r#"{"code":0,"when":1692685512,"msg":"ok","desc":"set.miner.fastboot"}"#;

    #[test]
    fn request_enable_has_param() {
        let cmd = SetMinerFastboot::new(true);
        assert!(cmd.is_secured());
        assert!(!cmd.is_encrypted());

        let auth = AuthData::new::<SetMinerFastboot>(Account::Super, "pass", "salt").unwrap();
        let encoded = cmd.encode(Some(auth)).unwrap();
        let json: serde_json::Value = serde_json::from_slice(&encoded.0).unwrap();

        assert_eq!(json["cmd"], "set.miner.fastboot");
        assert_eq!(json["param"], "enable");
        assert_eq!(json["account"], "super");
        assert!(json["ts"].as_u64().unwrap() > 0);
        assert_eq!(json["token"].as_str().unwrap().len(), 8);
    }

    #[test]
    fn request_disable_has_param() {
        let cmd = SetMinerFastboot::new(false);
        let auth = AuthData::new::<SetMinerFastboot>(Account::Super, "pass", "salt").unwrap();
        let encoded = cmd.encode(Some(auth)).unwrap();
        let json: serde_json::Value = serde_json::from_slice(&encoded.0).unwrap();

        assert_eq!(json["param"], "disable");
    }

    #[test]
    fn request_without_auth_omits_token() {
        let cmd = SetMinerFastboot::new(true);
        let encoded = cmd.encode(None).unwrap();
        let json: serde_json::Value = serde_json::from_slice(&encoded.0).unwrap();
        assert!(json.get("token").is_none());
        assert_eq!(json["param"], "enable");
    }

    #[test]
    fn deserialize_response_fixture() {
        let response = SetMinerFastboot::parse_response(RESPONSE_JSON.as_bytes()).unwrap();
        assert!(response.code.is_success());
        assert_eq!(response.desc, "set.miner.fastboot");
        assert_eq!(response.when, 1692685512);
        assert_eq!(response.msg, "ok");
    }
}