- Switch OnMessage → OnTypedMessage with natsutil.Decode[messages.VoiceRequest] - Return *messages.VoiceResponse with raw []byte audio (no base64) - Use messages.DocumentSource for RAG sources - Remove strVal/boolVal helpers - Add .dockerignore, GOAMD64=v3 in Dockerfile - Update tests for typed structs (7 tests pass)
84 lines
2.1 KiB
Go
84 lines
2.1 KiB
Go
package main
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"git.daviestechlabs.io/daviestechlabs/handler-base/messages"
|
|
"git.daviestechlabs.io/daviestechlabs/handler-base/natsutil"
|
|
"github.com/vmihailenco/msgpack/v5"
|
|
)
|
|
|
|
func TestVoiceRequestDecode(t *testing.T) {
|
|
req := messages.VoiceRequest{
|
|
RequestID: "req-123",
|
|
Audio: []byte{0x01, 0x02, 0x03},
|
|
Language: "en",
|
|
Collection: "docs",
|
|
}
|
|
data, err := msgpack.Marshal(&req)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
decoded, err := natsutil.Decode[messages.VoiceRequest](data)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if decoded.RequestID != "req-123" {
|
|
t.Errorf("RequestID = %q", decoded.RequestID)
|
|
}
|
|
if len(decoded.Audio) != 3 {
|
|
t.Errorf("Audio len = %d", len(decoded.Audio))
|
|
}
|
|
}
|
|
|
|
func TestVoiceResponseRoundtrip(t *testing.T) {
|
|
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}},
|
|
}
|
|
data, err := msgpack.Marshal(&resp)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var got messages.VoiceResponse
|
|
if err := msgpack.Unmarshal(data, &got); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if got.Response != "It is sunny today." {
|
|
t.Errorf("Response = %q", got.Response)
|
|
}
|
|
if len(got.Audio) != 8000 {
|
|
t.Errorf("Audio len = %d", len(got.Audio))
|
|
}
|
|
if len(got.Sources) != 1 || got.Sources[0].Text != "weather doc" {
|
|
t.Errorf("Sources = %v", got.Sources)
|
|
}
|
|
}
|
|
|
|
func TestGetEnvHelpers(t *testing.T) {
|
|
t.Setenv("VA_TEST", "hello")
|
|
if got := getEnv("VA_TEST", "x"); got != "hello" {
|
|
t.Errorf("getEnv = %q", got)
|
|
}
|
|
t.Setenv("VA_PORT", "9090")
|
|
if got := getEnvInt("VA_PORT", 0); got != 9090 {
|
|
t.Errorf("getEnvInt = %d", got)
|
|
}
|
|
t.Setenv("VA_FLAG", "true")
|
|
if got := getEnvBool("VA_FLAG", false); !got {
|
|
t.Error("getEnvBool should be true")
|
|
}
|
|
}
|
|
|
|
func TestTruncate(t *testing.T) {
|
|
if got := truncate("hello world", 5); got != "hello..." {
|
|
t.Errorf("truncate = %q", got)
|
|
}
|
|
if got := truncate("hi", 10); got != "hi" {
|
|
t.Errorf("truncate short = %q", got)
|
|
}
|
|
}
|