feat: migrate to typed messages, drop base64
- Switch OnMessage → OnTypedMessage with natsutil.Decode[messages.ChatRequest] - Return *messages.ChatResponse / *messages.ChatStreamChunk (not map[string]any) - Audio as raw []byte in msgpack (25% wire savings vs base64) - Remove strVal/boolVal/intVal helpers - Add .dockerignore, GOAMD64=v3 in Dockerfile - Update tests for typed structs (9 tests pass)
This commit is contained in:
71
main_test.go
71
main_test.go
@@ -3,44 +3,59 @@ package main
|
||||
import (
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"git.daviestechlabs.io/daviestechlabs/handler-base/messages"
|
||||
"github.com/vmihailenco/msgpack/v5"
|
||||
)
|
||||
|
||||
func TestStrVal(t *testing.T) {
|
||||
m := map[string]any{"key": "value", "num": 42}
|
||||
if got := strVal(m, "key", ""); got != "value" {
|
||||
t.Errorf("strVal(key) = %q", got)
|
||||
func TestChatRequestDecode(t *testing.T) {
|
||||
// Verify a msgpack-encoded map decodes cleanly into typed struct.
|
||||
raw := map[string]any{
|
||||
"request_id": "req-1",
|
||||
"user_id": "user-1",
|
||||
"message": "hello",
|
||||
"premium": true,
|
||||
"top_k": 10,
|
||||
}
|
||||
if got := strVal(m, "missing", "def"); got != "def" {
|
||||
t.Errorf("strVal(missing) = %q", got)
|
||||
data, _ := msgpack.Marshal(raw)
|
||||
var req messages.ChatRequest
|
||||
if err := msgpack.Unmarshal(data, &req); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if req.RequestID != "req-1" {
|
||||
t.Errorf("RequestID = %q", req.RequestID)
|
||||
}
|
||||
if req.EffectiveQuery() != "hello" {
|
||||
t.Errorf("EffectiveQuery = %q", req.EffectiveQuery())
|
||||
}
|
||||
if !req.Premium {
|
||||
t.Error("Premium should be true")
|
||||
}
|
||||
if req.TopK != 10 {
|
||||
t.Errorf("TopK = %d", req.TopK)
|
||||
}
|
||||
}
|
||||
|
||||
func TestBoolVal(t *testing.T) {
|
||||
m := map[string]any{"flag": true, "str": "not-bool"}
|
||||
if got := boolVal(m, "flag", false); !got {
|
||||
t.Error("boolVal(flag) should be true")
|
||||
func TestChatResponseRoundtrip(t *testing.T) {
|
||||
resp := &messages.ChatResponse{
|
||||
UserID: "user-1",
|
||||
Response: "answer",
|
||||
Success: true,
|
||||
Audio: []byte{0x01, 0x02, 0x03},
|
||||
}
|
||||
if got := boolVal(m, "str", false); got {
|
||||
t.Error("boolVal(str) should be false (not a bool)")
|
||||
data, err := msgpack.Marshal(resp)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got := boolVal(m, "missing", true); !got {
|
||||
t.Error("boolVal(missing) should use fallback true")
|
||||
var decoded messages.ChatResponse
|
||||
if err := msgpack.Unmarshal(data, &decoded); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestIntVal(t *testing.T) {
|
||||
m := map[string]any{"int": 5, "float": 3.14, "int64": int64(99)}
|
||||
if got := intVal(m, "int", 0); got != 5 {
|
||||
t.Errorf("intVal(int) = %d", got)
|
||||
if decoded.UserID != "user-1" || !decoded.Success {
|
||||
t.Errorf("decoded = %+v", decoded)
|
||||
}
|
||||
if got := intVal(m, "float", 0); got != 3 {
|
||||
t.Errorf("intVal(float) = %d", got)
|
||||
}
|
||||
if got := intVal(m, "int64", 0); got != 99 {
|
||||
t.Errorf("intVal(int64) = %d", got)
|
||||
}
|
||||
if got := intVal(m, "missing", 42); got != 42 {
|
||||
t.Errorf("intVal(missing) = %d", got)
|
||||
if len(decoded.Audio) != 3 {
|
||||
t.Errorf("audio len = %d", len(decoded.Audio))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user