1#[cfg(test)]
2mod tests {
3 use mockito;
4
5 use crate::{
6 structs::govee::{GoveeCommand, PayloadBody},
7 GoveeClient,
8 };
9
10 const MOCK_API_KEY: &str = "mock-api-key";
12
13 #[tokio::test]
14 async fn test_control_device() {
15 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 if let Err(err) = results {
39 panic!("Error occurred: {:?}", err);
40 }
41 }
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 let govee_client = GoveeClient::new(govee_api_key);
65 let results = govee_client.control_appliance(payload).await;
66 if let Err(err) = results {
68 panic!("Error occurred: {:?}", err);
69 }
70 }
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 if let Err(err) = result {
115 panic!("Error occurred: {:?}", err);
116 }
117 }
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 if let Err(err) = result {
174 panic!("Error occurred: {:?}", err);
175 }
176 }
178}