Files
chat-handler/main_test.go
Billy D. 4175e2070c
Some checks failed
CI / Lint (pull_request) Failing after 58s
CI / Test (pull_request) Failing after 1m22s
CI / Notify (pull_request) Successful in 1s
CI / Release (pull_request) Has been skipped
CI / Docker Build & Push (pull_request) Has been skipped
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)
2026-02-20 07:10:43 -05:00

94 lines
2.2 KiB
Go

package main
import (
"os"
"testing"
"git.daviestechlabs.io/daviestechlabs/handler-base/messages"
"github.com/vmihailenco/msgpack/v5"
)
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,
}
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 TestChatResponseRoundtrip(t *testing.T) {
resp := &messages.ChatResponse{
UserID: "user-1",
Response: "answer",
Success: true,
Audio: []byte{0x01, 0x02, 0x03},
}
data, err := msgpack.Marshal(resp)
if err != nil {
t.Fatal(err)
}
var decoded messages.ChatResponse
if err := msgpack.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")
}
}