Files
chat-handler/main_test.go
Billy D. f61488a868
Some checks failed
CI / Test (push) Successful in 2m49s
CI / Lint (push) Successful in 3m16s
CI / Release (push) Successful in 2m8s
CI / Docker Build & Push (push) Failing after 6m13s
CI / Notify (push) Successful in 1s
style: gofmt + fix copylocks lint warning
2026-02-21 15:36:46 -05:00

97 lines
2.3 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: UserId=%s, Success=%v", decoded.UserId, decoded.Success)
}
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")
}
}