1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#[cfg(test)]
mod tests {
    use mockito;

    use crate::{get_all_devices, get_device_status, sent_put_request, GoveeCommand, PayloadBody};

    #[tokio::test]
    async fn test_sent_put_request() {
        let mut server = mockito::Server::new();
        let govee_root_url = server.url();
        let govee_api_key = "1234567890";
        let mock_endpoint = server
            .mock("put", "/v1/devices/control")
            .match_header("govee-api-key", govee_api_key)
            .with_status(200)
            .create();
        let command = GoveeCommand {
            name: "turn".to_string(),
            value: "on".to_string(),
        };
        let payload = PayloadBody {
            device: "device_id".to_string(),
            model: "model_id".to_string(),
            cmd: command,
        };
        sent_put_request(&govee_root_url, &govee_api_key, payload).await;
        mock_endpoint.assert();
    }

    #[tokio::test]
    async fn test_get_all_devices() {
        let mut server = mockito::Server::new();
        let govee_root_url = server.url();
        let govee_api_key = "1234567890";
        let mock_endpoint = server
            .mock("get", "/v1/devices")
            .match_header("govee-api-key", govee_api_key)
            .with_status(200)
            .with_body(
                r#"{
                    "code": 200,
                    "message": "Success",
                    "devices": [
                        {
                            "device": "device_id",
                            "model": "model_id",
                            "deviceName": "device_name",
                            "controllable": true,
                            "retrievable": true,
                            "supportCmds": [
                                "turn",
                                "brightness",
                                "color",
                                "colorTem"
                            ],
                            "properties": {
                                "colorTem": {
                                    "range": {
                                        "min": 2000,
                                        "max": 9001
                                    }
                                }
                            }
                        }
                    ]
                }"#,
            )
            .create();
        get_all_devices(&govee_root_url, &govee_api_key).await;
        mock_endpoint.assert();
    }

    #[tokio::test]
    async fn test_get_device_status() {
        let mut server = mockito::Server::new();
        let gooee_root_url = server.url();
        let govee_api_key = "1234567890";
        let device = "device_name";
        let model = "model_name";
        let mock_endpoint = server
            .mock("get", "/v1/devices/state")
            .match_header("govee-api-key", govee_api_key)
            .match_query(mockito::Matcher::AllOf(vec![
                mockito::Matcher::UrlEncoded("device".into(), device.into()),
                mockito::Matcher::UrlEncoded("model".into(), model.into()),
            ]))
            .with_status(200)
            .with_body(
                r#"{
                    "code": 200,
                    "message": "Success",
                    "device": "device_id",
                    "model": "model_id",
                    "properties": [
                        {
                            "online": true
                        },
                        {
                            "powerState": "on"
                        },
                        {
                            "brightness": 100
                        },
                        {
                            "colorTemInKelvin": 2000
                        },
                        {
                            "colorTem": 2000
                        }
                    ]
                }"#,
            )
            .create();
        get_device_status(&gooee_root_url, &govee_api_key, device, model).await;
        mock_endpoint.assert();
    }
}