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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#[cfg(test)]
mod tests {
    use mockito;

    use crate::{
        structs::govee::{GoveeCommand, PayloadBody},
        GoveeClient,
    };

    // Mock API key for testing
    const MOCK_API_KEY: &str = "mock-api-key";

    #[tokio::test]
    async fn test_control_device() {
        // Arrange
        let mut server = mockito::Server::new();
        let _mock = server.mock("PUT", "/v1/devices/control")
            .match_header("Govee-API-Key", MOCK_API_KEY)
            .with_status(200)
            .create();

        let test_client = GoveeClient::new(MOCK_API_KEY);

        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,
        };

        // Act
        test_client.control_device(payload).await;

        // Assert that the mock expectations were satisfied
        // mock.assert();
    }

    #[tokio::test]
    async fn test_control_appliance() {
        let mut server = mockito::Server::new();
        let govee_api_key = "1234567890";
        let _mock_endpoint = server
            .mock("PUT", "/v1/appliance/devices/control")
            .match_header("Govee-API-Key", MOCK_API_KEY)
            .with_status(200)
            .create();
        let command = GoveeCommand {
            name: "mode".to_string(),
            value: "16".to_string(),
        };
        let payload = PayloadBody {
            device: "device_id".to_string(),
            model: "model_id".to_string(),
            cmd: command,
        };
        // Create the GoveeClient instance
        let govee_client = GoveeClient::new(govee_api_key);
        govee_client.control_appliance(payload).await;
        // mock_endpoint.assert();
    }

    #[tokio::test]
    async fn test_get_devices() {
        let mut server = mockito::Server::new();
        let govee_api_key = "1234567890";
        let _mock_endpoint = server
            .mock("GET", "https://developer-api.govee.com/v1/devices")
            .match_header("Govee-API-Key", MOCK_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();
        let govee_client = GoveeClient::new(govee_api_key);
        govee_client.get_devices().await;
        // mock_endpoint.assert();
    }

    #[tokio::test]
    async fn test_get_appliances() {
        let mut server = mockito::Server::new();
        let govee_api_key = "1234567890";
        let _mock_endpoint = server
            .mock("get", "/v1/appliance/devices")
            .match_header("govee-api-key", govee_api_key)
            .with_status(200)
            .with_body(
                r#"{
                    "code": 200,
                    "message": "Success",
                    "devices": [
                        {
                            "device": "appliance_id",
                            "model": "model_id",
                            "deviceName": "device_name",
                            "controllable": true,
                            "retrievable": true,
                            "supportCmds": [
                                "turn",
                                "mode"
                            ],
                            "properties": {
                                "mode": {
                                    "options": [
                                        {
                                            "name": "Low",
                                            "value": 1
                                        },
                                        {
                                            "name": "Medium",
                                            "value": 2
                                        },
                                        {
                                            "name": "High",
                                            "value": 3
                                        },
                                                                                {
                                            "name": "Sleep",
                                            "value": 4
                                        }
                                    ]
                                }
                            }
                        }
                    ]
                }"#,
            )
            .create();
        let govee_client = GoveeClient::new(govee_api_key);
        govee_client.get_appliances().await;
        // mock_endpoint.assert();
    }
}