govee_api/tests/
api.rs

1#[cfg(test)]
2mod tests {
3    use mockito;
4
5    use crate::{
6        structs::govee::{GoveeCommand, PayloadBody},
7        GoveeClient,
8    };
9
10    // Mock API key for testing
11    const MOCK_API_KEY: &str = "mock-api-key";
12
13    #[tokio::test]
14    async fn test_control_device() {
15        // Arrange
16        let mut server = mockito::Server::new();
17        let _mock = server
18            .mock("PUT", "/v1/devices/control")
19            .match_header("Govee-API-Key", MOCK_API_KEY)
20            .with_status(200)
21            .create();
22
23        let test_client = GoveeClient::new(MOCK_API_KEY);
24
25        let command = GoveeCommand {
26            name: "turn".to_string(),
27            value: "on".to_string(),
28        };
29        let payload = PayloadBody {
30            device: "device_id".to_string(),
31            model: "model_id".to_string(),
32            cmd: command,
33        };
34
35        let results = test_client.control_device(payload).await;
36
37        // Handle the result appropriately
38        if let Err(err) = results {
39            panic!("Error occurred: {:?}", err);
40        }
41        // Assert that the mock expectations were satisfied
42        // mock.assert();
43    }
44
45    #[tokio::test]
46    async fn test_control_appliance() {
47        let mut server = mockito::Server::new();
48        let govee_api_key = "1234567890";
49        let _mock_endpoint = server
50            .mock("PUT", "/v1/appliance/devices/control")
51            .match_header("Govee-API-Key", MOCK_API_KEY)
52            .with_status(200)
53            .create();
54        let command = GoveeCommand {
55            name: "mode".to_string(),
56            value: "16".to_string(),
57        };
58        let payload = PayloadBody {
59            device: "device_id".to_string(),
60            model: "model_id".to_string(),
61            cmd: command,
62        };
63        // Create the GoveeClient instance
64        let govee_client = GoveeClient::new(govee_api_key);
65        let results = govee_client.control_appliance(payload).await;
66        // Handle the result appropriately
67        if let Err(err) = results {
68            panic!("Error occurred: {:?}", err);
69        }
70        // mock_endpoint.assert();
71    }
72
73    #[tokio::test]
74    async fn test_get_devices() {
75        let mut server = mockito::Server::new();
76        let govee_api_key = "1234567890";
77        let _mock_endpoint = server
78            .mock("GET", "https://developer-api.govee.com/v1/devices")
79            .match_header("Govee-API-Key", MOCK_API_KEY)
80            .with_status(200)
81            .with_body(
82                r#"{
83                    "code": 200,
84                    "message": "Success",
85                    "devices": [
86                        {
87                            "device": "device_id",
88                            "model": "model_id",
89                            "deviceName": "device_name",
90                            "controllable": true,
91                            "retrievable": true,
92                            "supportCmds": [
93                                "turn",
94                                "brightness",
95                                "color",
96                                "colorTem"
97                            ],
98                            "properties": {
99                                "colorTem": {
100                                    "range": {
101                                        "min": 2000,
102                                        "max": 9001
103                                    }
104                                }
105                            }
106                        }
107                    ]
108                }"#,
109            )
110            .create();
111        let govee_client = GoveeClient::new(govee_api_key);
112        let result = govee_client.get_devices().await;
113        // Handle the result appropriately
114        if let Err(err) = result {
115            panic!("Error occurred: {:?}", err);
116        }
117        // mock_endpoint.assert();
118    }
119
120    #[tokio::test]
121    async fn test_get_appliances() {
122        let mut server = mockito::Server::new();
123        let govee_api_key = "1234567890";
124        let _mock_endpoint = server
125            .mock("get", "/v1/appliance/devices")
126            .match_header("govee-api-key", govee_api_key)
127            .with_status(200)
128            .with_body(
129                r#"{
130                    "code": 200,
131                    "message": "Success",
132                    "devices": [
133                        {
134                            "device": "appliance_id",
135                            "model": "model_id",
136                            "deviceName": "device_name",
137                            "controllable": true,
138                            "retrievable": true,
139                            "supportCmds": [
140                                "turn",
141                                "mode"
142                            ],
143                            "properties": {
144                                "mode": {
145                                    "options": [
146                                        {
147                                            "name": "Low",
148                                            "value": 1
149                                        },
150                                        {
151                                            "name": "Medium",
152                                            "value": 2
153                                        },
154                                        {
155                                            "name": "High",
156                                            "value": 3
157                                        },
158                                                                                {
159                                            "name": "Sleep",
160                                            "value": 4
161                                        }
162                                    ]
163                                }
164                            }
165                        }
166                    ]
167                }"#,
168            )
169            .create();
170        let govee_client = GoveeClient::new(govee_api_key);
171        let result = govee_client.get_appliances().await;
172        // Handle the result appropriately
173        if let Err(err) = result {
174            panic!("Error occurred: {:?}", err);
175        }
176        // mock_endpoint.assert();
177    }
178}