|
| 1 | +package identify |
| 2 | + |
| 3 | +import ( |
| 4 | + "github.com/shimmeringbee/bytecodec" |
| 5 | + "github.com/shimmeringbee/zcl" |
| 6 | + "github.com/shimmeringbee/zigbee" |
| 7 | + "github.com/stretchr/testify/assert" |
| 8 | + "testing" |
| 9 | +) |
| 10 | + |
| 11 | +func Test_Identify(t *testing.T) { |
| 12 | + t.Run("marshals and unmarshalls correctly", func(t *testing.T) { |
| 13 | + expectedCommand := Identify{ |
| 14 | + IdentifyTime: 0x1122, |
| 15 | + } |
| 16 | + actualCommand := Identify{} |
| 17 | + expectedBytes := []byte{0x22, 0x11} |
| 18 | + |
| 19 | + actualBytes, err := bytecodec.Marshal(&expectedCommand) |
| 20 | + assert.NoError(t, err) |
| 21 | + assert.Equal(t, expectedBytes, actualBytes) |
| 22 | + |
| 23 | + err = bytecodec.Unmarshal(expectedBytes, &actualCommand) |
| 24 | + assert.NoError(t, err) |
| 25 | + assert.Equal(t, expectedCommand, actualCommand) |
| 26 | + }) |
| 27 | + |
| 28 | + t.Run("the message is registered in the command registry", func(t *testing.T) { |
| 29 | + cr := zcl.NewCommandRegistry() |
| 30 | + Register(cr) |
| 31 | + |
| 32 | + id, err := cr.GetLocalCommandIdentifier(zcl.IdentifyId, zigbee.NoManufacturer, zcl.ClientToServer, &Identify{}) |
| 33 | + assert.NoError(t, err) |
| 34 | + assert.Equal(t, IdentifyId, id) |
| 35 | + }) |
| 36 | +} |
| 37 | + |
| 38 | +func Test_IdentifyQuery(t *testing.T) { |
| 39 | + t.Run("marshals and unmarshalls correctly", func(t *testing.T) { |
| 40 | + expectedCommand := IdentifyQuery{} |
| 41 | + actualCommand := IdentifyQuery{} |
| 42 | + expectedBytes := []byte(nil) |
| 43 | + |
| 44 | + actualBytes, err := bytecodec.Marshal(&expectedCommand) |
| 45 | + assert.NoError(t, err) |
| 46 | + assert.Equal(t, expectedBytes, actualBytes) |
| 47 | + |
| 48 | + err = bytecodec.Unmarshal(expectedBytes, &actualCommand) |
| 49 | + assert.NoError(t, err) |
| 50 | + assert.Equal(t, expectedCommand, actualCommand) |
| 51 | + }) |
| 52 | + |
| 53 | + t.Run("the message is registered in the command registry", func(t *testing.T) { |
| 54 | + cr := zcl.NewCommandRegistry() |
| 55 | + Register(cr) |
| 56 | + |
| 57 | + id, err := cr.GetLocalCommandIdentifier(zcl.IdentifyId, zigbee.NoManufacturer, zcl.ClientToServer, &IdentifyQuery{}) |
| 58 | + assert.NoError(t, err) |
| 59 | + assert.Equal(t, IdentifyQueryId, id) |
| 60 | + }) |
| 61 | +} |
| 62 | + |
| 63 | +func Test_TriggerEffect(t *testing.T) { |
| 64 | + t.Run("marshals and unmarshalls correctly", func(t *testing.T) { |
| 65 | + expectedCommand := TriggerEffect{ |
| 66 | + EffectIdentifier: 0x11, |
| 67 | + EffectVariant: 0x22, |
| 68 | + } |
| 69 | + actualCommand := TriggerEffect{} |
| 70 | + expectedBytes := []byte{0x11, 0x22} |
| 71 | + |
| 72 | + actualBytes, err := bytecodec.Marshal(&expectedCommand) |
| 73 | + assert.NoError(t, err) |
| 74 | + assert.Equal(t, expectedBytes, actualBytes) |
| 75 | + |
| 76 | + err = bytecodec.Unmarshal(expectedBytes, &actualCommand) |
| 77 | + assert.NoError(t, err) |
| 78 | + assert.Equal(t, expectedCommand, actualCommand) |
| 79 | + }) |
| 80 | + |
| 81 | + t.Run("the message is registered in the command registry", func(t *testing.T) { |
| 82 | + cr := zcl.NewCommandRegistry() |
| 83 | + Register(cr) |
| 84 | + |
| 85 | + id, err := cr.GetLocalCommandIdentifier(zcl.IdentifyId, zigbee.NoManufacturer, zcl.ClientToServer, &TriggerEffect{}) |
| 86 | + assert.NoError(t, err) |
| 87 | + assert.Equal(t, TriggerEffectId, id) |
| 88 | + }) |
| 89 | +} |
| 90 | + |
| 91 | +func Test_IdentifyQueryResponse(t *testing.T) { |
| 92 | + t.Run("marshals and unmarshalls correctly", func(t *testing.T) { |
| 93 | + expectedCommand := IdentifyQueryResponse{ |
| 94 | + Timeout: 0x1122, |
| 95 | + } |
| 96 | + actualCommand := IdentifyQueryResponse{} |
| 97 | + expectedBytes := []byte{0x22, 0x11} |
| 98 | + |
| 99 | + actualBytes, err := bytecodec.Marshal(&expectedCommand) |
| 100 | + assert.NoError(t, err) |
| 101 | + assert.Equal(t, expectedBytes, actualBytes) |
| 102 | + |
| 103 | + err = bytecodec.Unmarshal(expectedBytes, &actualCommand) |
| 104 | + assert.NoError(t, err) |
| 105 | + assert.Equal(t, expectedCommand, actualCommand) |
| 106 | + }) |
| 107 | + |
| 108 | + t.Run("the message is registered in the command registry", func(t *testing.T) { |
| 109 | + cr := zcl.NewCommandRegistry() |
| 110 | + Register(cr) |
| 111 | + |
| 112 | + id, err := cr.GetLocalCommandIdentifier(zcl.IdentifyId, zigbee.NoManufacturer, zcl.ServerToClient, &IdentifyQueryResponse{}) |
| 113 | + assert.NoError(t, err) |
| 114 | + assert.Equal(t, IdentifyQueryResponseId, id) |
| 115 | + }) |
| 116 | +} |
0 commit comments