-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient_test.go
More file actions
393 lines (360 loc) · 13.1 KB
/
Copy pathclient_test.go
File metadata and controls
393 lines (360 loc) · 13.1 KB
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
package foxfire
import (
"context"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"net/http/httptest"
"strings"
"testing"
"time"
)
// newFakeBridge stands up a TLS server speaking enough CLIP v2 to exercise
// the client. Using httptest's own certificate and transport sidesteps the
// bridge-identity machinery, which is tested separately.
func newFakeBridge(t *testing.T, h http.Handler) (*Client, *httptest.Server) {
t.Helper()
srv := httptest.NewTLSServer(h)
t.Cleanup(srv.Close)
c, err := New(srv.Listener.Addr().String(), "test-key",
WithTransport(srv.Client().Transport),
WithRateLimits(1000, 1000)) // do not make tests wait on token buckets
if err != nil {
t.Fatalf("New: %v", err)
}
return c, srv
}
func TestListLights(t *testing.T) {
c, _ := newFakeBridge(t, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if got := r.Header.Get(keyHeader); got != "test-key" {
t.Errorf("application key header = %q, want test-key", got)
}
if r.URL.Path != "/clip/v2/resource/light" {
t.Errorf("path = %q", r.URL.Path)
}
fmt.Fprint(w, `{"errors":[],"data":[
{"id":"abc","type":"light","metadata":{"name":"Desk"},
"on":{"on":true},"dimming":{"brightness":42.5}}]}`)
}))
lights, err := c.Lights.List(context.Background())
if err != nil {
t.Fatalf("List: %v", err)
}
if len(lights) != 1 {
t.Fatalf("got %d lights, want 1", len(lights))
}
if lights[0].Name() != "Desk" || !lights[0].On.On {
t.Errorf("unexpected light: %+v", lights[0])
}
if lights[0].Dimming == nil || lights[0].Dimming.Brightness != 42.5 {
t.Errorf("brightness not decoded: %+v", lights[0].Dimming)
}
}
// The bridge returns 200 with a populated errors array for partially applied
// updates. Treating that as success is the bug this test exists to prevent.
func TestErrorsArrayOnSuccessStatus(t *testing.T) {
c, _ := newFakeBridge(t, http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
fmt.Fprint(w, `{"errors":[{"description":"device (light) is not powered"}],"data":[]}`)
}))
err := c.Lights.SetOn(context.Background(), "abc", true)
if err == nil {
t.Fatal("expected an error for a populated errors array")
}
if !strings.Contains(err.Error(), "not powered") {
t.Errorf("error did not carry the description: %v", err)
}
}
func TestUnauthorizedMapsToSentinel(t *testing.T) {
c, _ := newFakeBridge(t, http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusUnauthorized)
fmt.Fprint(w, `{"errors":[{"description":"unauthorized user"}],"data":[]}`)
}))
_, err := c.Lights.List(context.Background())
if !errors.Is(err, ErrUnauthorized) {
t.Fatalf("got %v, want ErrUnauthorized", err)
}
}
// Partial-update semantics: an update that sets only brightness must not
// serialize an "on" field, because doing so would command a state change the
// caller never asked for.
func TestPartialUpdateOmitsUnsetFields(t *testing.T) {
var body []byte
c, _ := newFakeBridge(t, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
body, _ = io.ReadAll(r.Body)
fmt.Fprint(w, `{"errors":[],"data":[]}`)
}))
if err := c.Lights.SetBrightness(context.Background(), "abc", 30, 400); err != nil {
t.Fatalf("SetBrightness: %v", err)
}
var decoded map[string]any
if err := json.Unmarshal(body, &decoded); err != nil {
t.Fatalf("request body was not JSON: %v", err)
}
if _, present := decoded["on"]; present {
t.Errorf("brightness-only update sent an 'on' field: %s", body)
}
if _, present := decoded["dimming"]; !present {
t.Errorf("update omitted dimming: %s", body)
}
if dyn, ok := decoded["dynamics"].(map[string]any); !ok || dyn["duration"] != float64(400) {
t.Errorf("transition not encoded: %s", body)
}
}
// Brightness zero is a real command, not an absent field. This is the exact
// case pointer-valued update fields exist to disambiguate.
func TestZeroBrightnessIsSent(t *testing.T) {
var body []byte
c, _ := newFakeBridge(t, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
body, _ = io.ReadAll(r.Body)
fmt.Fprint(w, `{"errors":[],"data":[]}`)
}))
if err := c.Lights.SetBrightness(context.Background(), "abc", 0, 0); err != nil {
t.Fatalf("SetBrightness: %v", err)
}
if !strings.Contains(string(body), `"brightness":0`) {
t.Errorf("zero brightness was dropped: %s", body)
}
}
func TestRoomGroupedLightID(t *testing.T) {
r := Room{Services: []Ref{
{RID: "svc-1", RType: TypeZigbeeConnect},
{RID: "svc-2", RType: TypeGroupedLight},
}}
id, ok := r.GroupedLightID()
if !ok || id != "svc-2" {
t.Errorf("got (%q, %v), want (svc-2, true)", id, ok)
}
if _, ok := (Room{}).GroupedLightID(); ok {
t.Error("empty room reported a grouped light")
}
}
func TestSubscribeDecodesBatches(t *testing.T) {
c, _ := newFakeBridge(t, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != eventStreamPath {
t.Errorf("stream path = %q", r.URL.Path)
}
w.Header().Set("Content-Type", "text/event-stream")
w.WriteHeader(http.StatusOK)
fmt.Fprint(w, ": hi\n\n")
fmt.Fprint(w, "id: 1\ndata: [{\"creationtime\":\"2026-07-18T10:00:00Z\",\"type\":\"update\","+
"\"data\":[{\"id\":\"abc\",\"type\":\"light\",\"on\":{\"on\":false}}]}]\n\n")
w.(http.Flusher).Flush()
// Leave the connection open briefly so the reader is not racing a
// close, then let the handler return to end the stream.
time.Sleep(100 * time.Millisecond)
}))
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()
batches, _ := c.Subscribe(ctx)
select {
case b := <-batches:
if b.Type != "update" || len(b.Events) != 1 {
t.Fatalf("unexpected batch: %+v", b)
}
ev := b.Events[0]
if ev.ID != "abc" || ev.On == nil || ev.On.On {
t.Errorf("unexpected event: %+v", ev)
}
if ev.Dimming != nil {
t.Error("event carried a dimming field the bridge did not send")
}
case <-ctx.Done():
t.Fatal("no batch received")
}
}
// A button switch presents several button services owned by one device, told
// apart by control_id. Validating the decode here rather than on hardware: no
// button device is available, and the value that matters (the press) arrives
// on the stream, tested separately below.
func TestListButtons(t *testing.T) {
c, _ := newFakeBridge(t, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/clip/v2/resource/button" {
t.Errorf("path = %q", r.URL.Path)
}
fmt.Fprint(w, `{"errors":[],"data":[
{"id":"btn-1","type":"button","owner":{"rid":"dev","rtype":"device"},
"metadata":{"control_id":1},
"button":{"last_event":"short_release","event_values":["initial_press","short_release"]}},
{"id":"btn-2","type":"button","owner":{"rid":"dev","rtype":"device"},
"metadata":{"control_id":2},"button":{}}]}`)
}))
btns, err := c.Buttons.List(context.Background())
if err != nil {
t.Fatalf("List: %v", err)
}
if len(btns) != 2 {
t.Fatalf("got %d buttons, want 2", len(btns))
}
if btns[0].Metadata.ControlID != 1 || btns[0].Button.LastEvent != "short_release" {
t.Errorf("button 1 decoded wrong: %+v", btns[0])
}
if len(btns[0].Button.EventValues) != 2 {
t.Errorf("event_values not decoded: %+v", btns[0].Button.EventValues)
}
if btns[1].Metadata.ControlID != 2 {
t.Errorf("button 2 control_id wrong: %+v", btns[1])
}
}
// Button presses arrive on the event stream as a ButtonReport, not from a GET.
// This is the path a caller actually watches, so it is the one that must decode.
func TestButtonEventDecodes(t *testing.T) {
c, _ := newFakeBridge(t, http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
w.Header().Set("Content-Type", "text/event-stream")
w.WriteHeader(http.StatusOK)
fmt.Fprint(w, "data: [{\"creationtime\":\"2026-07-18T10:00:00Z\",\"type\":\"update\","+
"\"data\":[{\"id\":\"btn-1\",\"type\":\"button\","+
"\"button\":{\"last_event\":\"initial_press\"}}]}]\n\n")
w.(http.Flusher).Flush()
time.Sleep(100 * time.Millisecond)
}))
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()
batches, _ := c.Subscribe(ctx)
select {
case b := <-batches:
if len(b.Events) != 1 || b.Events[0].Button == nil {
t.Fatalf("button event not decoded: %+v", b)
}
if b.Events[0].Button.LastEvent != "initial_press" {
t.Errorf("last_event = %q, want initial_press", b.Events[0].Button.LastEvent)
}
case <-ctx.Done():
t.Fatal("no batch received")
}
}
// Scene creation goes through POST and returns the bridge-assigned reference
// in the update envelope. This checks the method, the body, and that the
// returned Ref is decoded -- and that Zone.Create fills in the archetype the
// bridge requires on create but treats as optional on read.
func TestSceneAndZoneCreate(t *testing.T) {
var sceneBody, zoneBody []byte
c, _ := newFakeBridge(t, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
t.Errorf("method = %s, want POST", r.Method)
}
switch r.URL.Path {
case "/clip/v2/resource/zone":
zoneBody, _ = io.ReadAll(r.Body)
fmt.Fprint(w, `{"errors":[],"data":[{"rid":"zone-1","rtype":"zone"}]}`)
case "/clip/v2/resource/scene":
sceneBody, _ = io.ReadAll(r.Body)
fmt.Fprint(w, `{"errors":[],"data":[{"rid":"scene-1","rtype":"scene"}]}`)
default:
t.Errorf("unexpected path %q", r.URL.Path)
}
}))
zref, err := c.Zones.Create(context.Background(), ZoneCreate{
Metadata: Metadata{Name: "Z"},
Children: []Ref{{RID: "light-1", RType: TypeLight}},
})
if err != nil {
t.Fatalf("Zone.Create: %v", err)
}
if zref.RID != "zone-1" || zref.RType != TypeZone {
t.Errorf("zone ref = %+v", zref)
}
// The archetype the read shape omits must be supplied on create.
var zm map[string]any
_ = json.Unmarshal(zoneBody, &zm)
if md, ok := zm["metadata"].(map[string]any); !ok || md["archetype"] != "other" {
t.Errorf("zone create did not default archetype: %s", zoneBody)
}
sref, err := c.Scenes.Create(context.Background(), SceneCreate{
Metadata: Metadata{Name: "S"},
Group: Ref{RID: "zone-1", RType: TypeZone},
Actions: []SceneAction{{
Target: Ref{RID: "light-1", RType: TypeLight},
Action: SceneTargetState{On: &On{On: true}},
}},
})
if err != nil {
t.Fatalf("Scene.Create: %v", err)
}
if sref.RID != "scene-1" {
t.Errorf("scene ref = %+v", sref)
}
if !strings.Contains(string(sceneBody), `"actions"`) {
t.Errorf("scene body missing actions: %s", sceneBody)
}
}
// A create that reports no error but returns no reference is a bridge contract
// violation, and post must surface it rather than return a zero Ref that reads
// as a valid ID.
func TestCreateWithNoReferenceErrors(t *testing.T) {
c, _ := newFakeBridge(t, http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
fmt.Fprint(w, `{"errors":[],"data":[]}`)
}))
_, err := c.Zones.Create(context.Background(), ZoneCreate{Metadata: Metadata{Name: "Z"}})
if err == nil {
t.Fatal("expected an error when create returns no reference")
}
}
// Rename is a metadata edit, not a command. It must PUT only the name, and it
// must not send any of the state fields that would command a change.
func TestDeviceRename(t *testing.T) {
var body []byte
var method, path string
c, _ := newFakeBridge(t, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
method, path = r.Method, r.URL.Path
body, _ = io.ReadAll(r.Body)
fmt.Fprint(w, `{"errors":[],"data":[{"rid":"dev-1","rtype":"device"}]}`)
}))
if err := c.Devices.Rename(context.Background(), "dev-1", "Kitchen"); err != nil {
t.Fatalf("Rename: %v", err)
}
if method != http.MethodPut || path != "/clip/v2/resource/device/dev-1" {
t.Errorf("got %s %s, want PUT /clip/v2/resource/device/dev-1", method, path)
}
var decoded map[string]any
if err := json.Unmarshal(body, &decoded); err != nil {
t.Fatalf("body not JSON: %v", err)
}
md, ok := decoded["metadata"].(map[string]any)
if !ok || md["name"] != "Kitchen" {
t.Errorf("rename body did not carry the name: %s", body)
}
if _, present := decoded["on"]; present {
t.Errorf("rename sent an 'on' field: %s", body)
}
}
// A smart plug is a light with archetype "plug". IsPlug is what lets a UI
// avoid showing a brightness slider for an outlet.
func TestLightIsPlug(t *testing.T) {
plug := Light{Metadata: Metadata{Name: "Outlet", Archetype: "plug"}}
if !plug.IsPlug() {
t.Error("plug archetype not detected")
}
bulb := Light{Metadata: Metadata{Name: "Lamp", Archetype: "sultan_bulb"}}
if bulb.IsPlug() {
t.Error("bulb misidentified as a plug")
}
if (Light{}).IsPlug() {
t.Error("archetype-less light misidentified as a plug")
}
}
func TestNewRequiresExplicitTLSPosture(t *testing.T) {
if _, err := New("192.0.2.1", "key"); err == nil {
t.Fatal("expected New to refuse an unconfigured trust posture")
}
if _, err := New("192.0.2.1", "key", WithInsecureTLS()); err != nil {
t.Fatalf("explicit insecure should be accepted: %v", err)
}
}
func TestBackoffIsBoundedAndJittered(t *testing.T) {
seen := map[time.Duration]bool{}
for i := 0; i < 50; i++ {
d := backoff(3)
if d <= 0 || d > 30*time.Second {
t.Fatalf("backoff(3) = %v, out of bounds", d)
}
seen[d] = true
}
if len(seen) < 2 {
t.Error("backoff produced no jitter")
}
if got := backoff(100); got > 30*time.Second {
t.Errorf("backoff(100) = %v, exceeds cap", got)
}
}