feat: migrate from msgpack to protobuf (handler-base v1.0.0)
- Replace msgpack encoding with protobuf wire format - Update field names to proto convention - Use pointer slices for repeated message fields ([]*DocumentSource) - Rewrite tests for proto round-trips
This commit is contained in:
26
main_test.go
26
main_test.go
@@ -5,26 +5,26 @@ import (
|
||||
|
||||
"git.daviestechlabs.io/daviestechlabs/handler-base/messages"
|
||||
"git.daviestechlabs.io/daviestechlabs/handler-base/natsutil"
|
||||
"github.com/vmihailenco/msgpack/v5"
|
||||
"google.golang.org/protobuf/proto"
|
||||
)
|
||||
|
||||
func TestVoiceRequestDecode(t *testing.T) {
|
||||
req := messages.VoiceRequest{
|
||||
RequestID: "req-123",
|
||||
req := &messages.VoiceRequest{
|
||||
RequestId: "req-123",
|
||||
Audio: []byte{0x01, 0x02, 0x03},
|
||||
Language: "en",
|
||||
Collection: "docs",
|
||||
}
|
||||
data, err := msgpack.Marshal(&req)
|
||||
data, err := proto.Marshal(req)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
decoded, err := natsutil.Decode[messages.VoiceRequest](data)
|
||||
if err != nil {
|
||||
var decoded messages.VoiceRequest
|
||||
if err := natsutil.Decode(data, &decoded); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if decoded.RequestID != "req-123" {
|
||||
t.Errorf("RequestID = %q", decoded.RequestID)
|
||||
if decoded.RequestId != "req-123" {
|
||||
t.Errorf("RequestId = %q", decoded.RequestId)
|
||||
}
|
||||
if len(decoded.Audio) != 3 {
|
||||
t.Errorf("Audio len = %d", len(decoded.Audio))
|
||||
@@ -32,19 +32,19 @@ func TestVoiceRequestDecode(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestVoiceResponseRoundtrip(t *testing.T) {
|
||||
resp := messages.VoiceResponse{
|
||||
RequestID: "req-456",
|
||||
resp := &messages.VoiceResponse{
|
||||
RequestId: "req-456",
|
||||
Response: "It is sunny today.",
|
||||
Audio: make([]byte, 8000),
|
||||
Transcription: "What is the weather?",
|
||||
Sources: []messages.DocumentSource{{Text: "weather doc", Score: 0.9}},
|
||||
Sources: []*messages.DocumentSource{{Text: "weather doc", Score: 0.9}},
|
||||
}
|
||||
data, err := msgpack.Marshal(&resp)
|
||||
data, err := proto.Marshal(resp)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var got messages.VoiceResponse
|
||||
if err := msgpack.Unmarshal(data, &got); err != nil {
|
||||
if err := proto.Unmarshal(data, &got); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got.Response != "It is sunny today." {
|
||||
|
||||
Reference in New Issue
Block a user