-
Notifications
You must be signed in to change notification settings - Fork 2
/
gateway_test.go
114 lines (86 loc) · 3.45 KB
/
gateway_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
package zda
import (
"context"
"github.com/shimmeringbee/da/capabilities"
"github.com/shimmeringbee/logwrap"
"github.com/shimmeringbee/logwrap/impl/discard"
"github.com/shimmeringbee/persistence/impl/memory"
"github.com/shimmeringbee/zigbee"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"io"
"testing"
)
var testGatewayIEEEAddress = zigbee.GenerateLocalAdministeredIEEEAddress()
func newTestGateway() (*ZDA, *zigbee.MockProvider, *mock.Call, func(*testing.T)) {
mp := new(zigbee.MockProvider)
mp.On("AdapterNode").Return(zigbee.Node{
IEEEAddress: testGatewayIEEEAddress,
}).Maybe()
mRE := mp.On("ReadEvent", mock.Anything).Return(nil, context.Canceled).Maybe()
gw := New(context.Background(), memory.New(), mp, nil)
gw.WithLogWrapLogger(logwrap.New(discard.Discard()))
gw.events = make(chan any, 0xffff)
return gw, mp, mRE, func(t *testing.T) {
err := gw.Stop(nil)
assert.NoError(t, err)
mp.AssertExpectations(t)
}
}
func Test_gateway_New(t *testing.T) {
t.Run("calling the new constructor returns a valid gateway, with the zigbee.Provider specified", func(t *testing.T) {
gw, mp, _, stop := newTestGateway()
defer stop(t)
assert.NotNil(t, gw)
assert.Equal(t, mp, gw.provider)
})
}
func Test_gateway_Start(t *testing.T) {
t.Run("initialises state from the zigbee.Provider, registers endpoints and returns a Self device with valid information", func(t *testing.T) {
gw, mp, _, stop := newTestGateway()
defer stop(t)
mp.On("RegisterAdapterEndpoint", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(nil).Once()
err := gw.Start(nil)
assert.NoError(t, err)
self := gw.Self()
assert.Equal(t, IEEEAddressWithSubIdentifier{IEEEAddress: testGatewayIEEEAddress, SubIdentifier: 0}, self.Identifier())
assert.Equal(t, gw, self.Gateway())
assert.Contains(t, self.Capabilities(), capabilities.DeviceDiscoveryFlag)
})
}
func Test_gateway_Stop(t *testing.T) {
t.Run("cancels the context upon call", func(t *testing.T) {
gw, mp, _, _ := newTestGateway()
mp.On("RegisterAdapterEndpoint", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(nil).Once()
err := gw.Start(nil)
assert.NoError(t, err)
assert.NoError(t, gw.ctx.Err())
err = gw.Stop(nil)
assert.NoError(t, err)
assert.ErrorIs(t, gw.ctx.Err(), context.Canceled)
})
}
func Test_gateway_Devices(t *testing.T) {
t.Run("returns any devices plus gateway self", func(t *testing.T) {
gw, mp, _, stop := newTestGateway()
defer stop(t)
mp.On("RegisterAdapterEndpoint", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(nil).Once()
mp.On("QueryNodeDescription", mock.Anything, mock.Anything).Return(zigbee.NodeDescription{}, io.EOF).Maybe()
err := gw.Start(nil)
assert.NoError(t, err)
addr := zigbee.GenerateLocalAdministeredIEEEAddress()
gw.receiveNodeJoinEvent(zigbee.NodeJoinEvent{Node: zigbee.Node{IEEEAddress: addr}})
devices := gw.Devices()
assert.Len(t, devices, 2)
assert.Contains(t, devices, gw.Self())
})
}
func Test_gateway_Capabilities(t *testing.T) {
t.Run("contains expected capabilities", func(t *testing.T) {
gw := ZDA{}
caps := gw.Capabilities()
assert.Contains(t, caps, capabilities.DeviceRemovalFlag)
assert.Contains(t, caps, capabilities.EnumerateDeviceFlag)
assert.Contains(t, caps, capabilities.ProductInformationFlag)
})
}