add basic tests for mqtt.message.encode

This commit is contained in:
Joel Wetzell
2026-02-09 23:18:19 -06:00
parent 1d8baa8c17
commit 4526e805af

View File

@@ -1,8 +1,10 @@
package processor_test
import (
"slices"
"testing"
mqtt "github.com/eclipse/paho.mqtt.golang"
"github.com/jwetzell/showbridge-go/internal/config"
"github.com/jwetzell/showbridge-go/internal/processor"
)
@@ -25,3 +27,63 @@ func TestMQTTMessageEncodeFromRegistry(t *testing.T) {
t.Fatalf("mqtt.message.encode processor has wrong type: %s", processorInstance.Type())
}
}
func TestGoodMQTTMessageEncode(t *testing.T) {
stringEncoder := processor.MQTTMessageEncode{}
tests := []struct {
name string
payload mqtt.Message
expected []byte
}{
{
name: "basic string",
payload: processor.NewMQTTMessage("test/topic", 1, true, []byte("hello")),
expected: []byte{0x68, 0x65, 0x6c, 0x6c, 0x6f},
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
got, err := stringEncoder.Process(t.Context(), test.payload)
gotBytes, ok := got.([]byte)
if !ok {
t.Fatalf("mqtt.message.encode returned a %T payload: %s", got, got)
}
if err != nil {
t.Fatalf("mqtt.message.encode failed: %s", err)
}
if !slices.Equal(gotBytes, test.expected) {
t.Fatalf("mqtt.message.encode got %s, expected %s", got, test.expected)
}
})
}
}
func TestBadMQTTMessageEncode(t *testing.T) {
stringEncoder := processor.MQTTMessageEncode{}
tests := []struct {
name string
payload any
errorString string
}{
{
name: "non-mqtt message input",
payload: []byte{0x68, 0x65, 0x6c, 0x6c, 0x6f},
errorString: "mqtt.message.encode processor only accepts an mqtt.Message",
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
got, err := stringEncoder.Process(t.Context(), test.payload)
if err == nil {
t.Fatalf("mqtt.message.encode expected to fail but got payload: %s", got)
}
if err.Error() != test.errorString {
t.Fatalf("mqtt.message.encode got error '%s', expected '%s'", err.Error(), test.errorString)
}
})
}
}