diff --git a/internal/processor/test/osc-message-decode_test.go b/internal/processor/test/osc-message-decode_test.go index 29911a1..ba5fa03 100644 --- a/internal/processor/test/osc-message-decode_test.go +++ b/internal/processor/test/osc-message-decode_test.go @@ -1,8 +1,10 @@ package processor_test import ( + "reflect" "testing" + "github.com/jwetzell/osc-go" "github.com/jwetzell/showbridge-go/internal/config" "github.com/jwetzell/showbridge-go/internal/processor" ) @@ -25,3 +27,67 @@ func TestOSCMessageDecodeFromRegistry(t *testing.T) { t.Fatalf("osc.message.decode processor has wrong type: %s", processorInstance.Type()) } } + +func TestGoodOSCMessageDecode(t *testing.T) { + processorInstance := processor.OSCMessageDecode{} + tests := []struct { + name string + payload []byte + expected osc.OSCMessage + }{ + { + name: "basic OSC message", + payload: []byte{47, 116, 101, 115, 116, 0, 0, 0, 44, 0, 0, 0}, + expected: osc.OSCMessage{ + Address: "/test", + }, + }, + } + + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + got, err := processorInstance.Process(t.Context(), test.payload) + + if err != nil { + t.Fatalf("osc.message.decode processing failed: %s", err) + } + + gotMessage, ok := got.(osc.OSCMessage) + if !ok { + t.Fatalf("osc.message.decode returned a %T payload: %s", got, got) + } + + if !reflect.DeepEqual(gotMessage, test.expected) { + t.Fatalf("osc.message.decode got %+v, expected %+v", gotMessage, test.expected) + } + }) + } +} + +func TestBadOSCMessageDecode(t *testing.T) { + processorInstance := processor.OSCMessageDecode{} + tests := []struct { + name string + payload any + errorString string + }{ + { + name: "non byte slice message input", + payload: "test", + errorString: "osc.message.decode processor only accepts a []byte payload", + }, + } + + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + got, err := processorInstance.Process(t.Context(), test.payload) + + if err == nil { + t.Fatalf("osc.message.decode expected to fail but got payload: %s", got) + } + if err.Error() != test.errorString { + t.Fatalf("osc.message.decode got error '%s', expected '%s'", err.Error(), test.errorString) + } + }) + } +} diff --git a/internal/processor/test/osc-message-encode_test.go b/internal/processor/test/osc-message-encode_test.go index c3b3480..df9b199 100644 --- a/internal/processor/test/osc-message-encode_test.go +++ b/internal/processor/test/osc-message-encode_test.go @@ -1,8 +1,10 @@ package processor_test import ( + "slices" "testing" + "github.com/jwetzell/osc-go" "github.com/jwetzell/showbridge-go/internal/config" "github.com/jwetzell/showbridge-go/internal/processor" ) @@ -25,3 +27,66 @@ func TestOSCMessageEncodeFromRegistry(t *testing.T) { t.Fatalf("osc.message.encode processor has wrong type: %s", processorInstance.Type()) } } + +func TestGoodOSCMessageEncode(t *testing.T) { + processorInstance := processor.OSCMessageEncode{} + tests := []struct { + name string + payload any + expected []byte + }{ + { + name: "basic OSC message", + payload: osc.OSCMessage{ + Address: "/test", + }, + expected: []byte{47, 116, 101, 115, 116, 0, 0, 0, 44, 0, 0, 0}, + }, + } + + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + got, err := processorInstance.Process(t.Context(), test.payload) + if err != nil { + t.Fatalf("osc.message.encode processing failed: %s", err) + } + + gotBytes, ok := got.([]byte) + if !ok { + t.Fatalf("osc.message.encode returned a %T payload: %s", got, got) + } + + if !slices.Equal(gotBytes, test.expected) { + t.Fatalf("osc.message.encode got %+v, expected %+v", got, test.expected) + } + }) + } +} + +func TestBadOSCMessageEncode(t *testing.T) { + processorInstance := processor.OSCMessageEncode{} + tests := []struct { + name string + payload any + errorString string + }{ + { + name: "non-osc message input", + payload: "test", + errorString: "osc.message.encode processor only accepts an OSCMessage", + }, + } + + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + got, err := processorInstance.Process(t.Context(), test.payload) + + if err == nil { + t.Fatalf("osc.message.encode expected to fail but got payload: %s", got) + } + if err.Error() != test.errorString { + t.Fatalf("osc.message.encode got error '%s', expected '%s'", err.Error(), test.errorString) + } + }) + } +}