-
Notifications
You must be signed in to change notification settings - Fork 1
/
weatherflow_test.go
150 lines (129 loc) · 3.73 KB
/
weatherflow_test.go
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
package weatherflow_test
import (
"context"
"fmt"
"net"
"net/http"
"testing"
"time"
"github.com/tris/weatherflow"
"nhooyr.io/websocket"
"nhooyr.io/websocket/wsjson"
)
func startMockServer() (string, func()) {
mux := http.NewServeMux()
mux.HandleFunc("/ws", mockServerHandler)
server := &http.Server{
Handler: mux,
}
ln, err := net.Listen("tcp", "localhost:0")
if err != nil {
panic(err)
}
go func() {
err := server.Serve(ln)
if err != nil && err != http.ErrServerClosed {
panic(err)
}
}()
port := ln.Addr().(*net.TCPAddr).Port
wsURL := fmt.Sprintf("ws://localhost:%d/ws", port)
stopServer := func() {
server.Shutdown(context.Background())
}
return wsURL, stopServer
}
func mockServerHandler(w http.ResponseWriter, r *http.Request) {
c, err := websocket.Accept(w, r, nil)
if err != nil {
panic(err)
}
defer c.Close(websocket.StatusInternalError, "Internal error")
// Send connection_opened message
openMsg := map[string]string{"type": "connection_opened"}
if err := wsjson.Write(r.Context(), c, openMsg); err != nil {
return
}
for {
var msg map[string]interface{}
err := wsjson.Read(r.Context(), c, &msg)
if err != nil {
return
}
switch msg["type"].(string) {
case "listen_start":
// Send ack and obs_st messages
_ = wsjson.Write(r.Context(), c, map[string]string{"type": "ack", "id": msg["id"].(string)})
_ = wsjson.Write(r.Context(), c, map[string]interface{}{
"status": map[string]interface{}{
"status_code": 0,
"status_message": "SUCCESS",
},
"device_id": 121037,
"type": "obs_st",
"source": "cache",
"summary": map[string]interface{}{
"pressure_trend": "steady",
"strike_count_1h": 0,
"strike_count_3h": 0,
"precip_total_1h": 0.0,
"strike_last_dist": 38,
"strike_last_epoch": 1679435903,
"precip_accum_local_yesterday": 0.0,
"precip_accum_local_yesterday_final": 0.0,
"precip_analysis_type_yesterday": 0,
"raining_minutes": []int{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
"precip_minutes_local_day": 0,
"precip_minutes_local_yesterday": 0,
},
"obs": [][]interface{}{
{
1681767864, 4.19, 4.24, 4.27, 285, 20, 722.7, nil, nil, 109435, 6.19, 912, 0, 0, 0, 0, 2.46, 1, 0, 0, 0, 0,
},
},
})
case "listen_rapid_start":
// Send ack and rapid_wind messages
_ = wsjson.Write(r.Context(), c, map[string]string{"type": "ack", "id": msg["id"].(string)})
_ = wsjson.Write(r.Context(), c, map[string]interface{}{
"type": "rapid_wind",
"device_id": 121037,
"serial_number": "ST-00026524",
"hub_sn": "HB-00039816",
"ob": []interface{}{
1681768025, 4.27, 282,
},
})
}
}
}
func TestNewClient(t *testing.T) {
// Start a local WebSocket server for testing
url, stopServer := startMockServer()
defer stopServer()
// Create client
client := weatherflow.NewClient("your_token", nil, t.Logf)
client.AddDevice(12345)
// Override URL to point to our mock server
client.SetURL(url)
// Create a channel to receive messages
msgCh := make(chan weatherflow.Message)
// Start the client
client.Start(func(msg weatherflow.Message) {
msgCh <- msg
})
// Use a select statement with a timeout to check if the expected messages are received
timeout := 5 * time.Second
expectedMessages := 2
for i := 0; i < expectedMessages; i++ {
select {
case msg := <-msgCh:
t.Logf("Received message: %#v", msg)
// TODO: add specific checks for the received messages
case <-time.After(timeout):
t.Fatalf("Timed out waiting for message %d", i+1)
}
}
// Stop the client
client.Stop()
}