- Replace msgpack encoding with protobuf wire format - Update field names to proto convention (UserId, RequestId, EnableRag, etc.) - Use messages.EffectiveQuery() standalone function - Cast TopK to int32 for proto compatibility - Rewrite tests for proto round-trips
97 lines
2.2 KiB
Go
97 lines
2.2 KiB
Go
package main
|
|
|
|
import (
|
|
"os"
|
|
"testing"
|
|
|
|
"git.daviestechlabs.io/daviestechlabs/handler-base/messages"
|
|
"google.golang.org/protobuf/proto"
|
|
)
|
|
|
|
func TestChatRequestDecode(t *testing.T) {
|
|
// Verify a proto-encoded struct round-trips cleanly.
|
|
original := &messages.ChatRequest{
|
|
RequestId: "req-1",
|
|
UserId: "user-1",
|
|
Message: "hello",
|
|
Premium: true,
|
|
TopK: 10,
|
|
}
|
|
data, err := proto.Marshal(original)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var req messages.ChatRequest
|
|
if err := proto.Unmarshal(data, &req); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if req.RequestId != "req-1" {
|
|
t.Errorf("RequestID = %q", req.RequestId)
|
|
}
|
|
if messages.EffectiveQuery(&req) != "hello" {
|
|
t.Errorf("EffectiveQuery = %q", messages.EffectiveQuery(&req))
|
|
}
|
|
if !req.Premium {
|
|
t.Error("Premium should be true")
|
|
}
|
|
if req.TopK != 10 {
|
|
t.Errorf("TopK = %d", req.TopK)
|
|
}
|
|
}
|
|
|
|
func TestChatResponseRoundtrip(t *testing.T) {
|
|
resp := &messages.ChatResponse{
|
|
UserId: "user-1",
|
|
Response: "answer",
|
|
Success: true,
|
|
Audio: []byte{0x01, 0x02, 0x03},
|
|
}
|
|
data, err := proto.Marshal(resp)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var decoded messages.ChatResponse
|
|
if err := proto.Unmarshal(data, &decoded); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if decoded.UserId != "user-1" || !decoded.Success {
|
|
t.Errorf("decoded = %+v", decoded)
|
|
}
|
|
if len(decoded.Audio) != 3 {
|
|
t.Errorf("audio len = %d", len(decoded.Audio))
|
|
}
|
|
}
|
|
|
|
func TestGetEnvHelpers(t *testing.T) {
|
|
t.Setenv("CHAT_TEST", "hello")
|
|
if got := getEnv("CHAT_TEST", "x"); got != "hello" {
|
|
t.Errorf("getEnv = %q", got)
|
|
}
|
|
if got := getEnv("NO_SUCH_VAR", "x"); got != "x" {
|
|
t.Errorf("getEnv fallback = %q", got)
|
|
}
|
|
|
|
t.Setenv("CHAT_PORT", "9090")
|
|
if got := getEnvInt("CHAT_PORT", 0); got != 9090 {
|
|
t.Errorf("getEnvInt = %d", got)
|
|
}
|
|
if got := getEnvInt("NO_SUCH_VAR", 80); got != 80 {
|
|
t.Errorf("getEnvInt fallback = %d", got)
|
|
}
|
|
|
|
t.Setenv("CHAT_FLAG", "true")
|
|
if got := getEnvBool("CHAT_FLAG", false); !got {
|
|
t.Error("getEnvBool should be true")
|
|
}
|
|
if got := getEnvBool("NO_SUCH_VAR", false); got {
|
|
t.Error("getEnvBool fallback should be false")
|
|
}
|
|
}
|
|
|
|
func TestMainBinaryBuilds(t *testing.T) {
|
|
// Verify the binary exists after build
|
|
if _, err := os.Stat("main.go"); err != nil {
|
|
t.Skip("main.go not found")
|
|
}
|
|
}
|