From 7a93d43b7ee3271c292ae427ba826a0208c2d66a Mon Sep 17 00:00:00 2001 From: Dex Date: Wed, 20 May 2026 11:34:37 +0000 Subject: [PATCH] CUB-205: add gateway utility function tests + fix channel default --- go-backend/internal/gateway/sync.go | 2 +- go-backend/internal/gateway/wsclient_test.go | 105 +++++++++++++++++++ 2 files changed, 106 insertions(+), 1 deletion(-) create mode 100644 go-backend/internal/gateway/wsclient_test.go diff --git a/go-backend/internal/gateway/sync.go b/go-backend/internal/gateway/sync.go index da4ff02..84d1301 100644 --- a/go-backend/internal/gateway/sync.go +++ b/go-backend/internal/gateway/sync.go @@ -172,7 +172,7 @@ func agentItemToCard(item agentListItem) models.AgentCardData { } channel := item.Channel if channel == "" { - channel = "discord" + channel = "unknown" } name := item.Name if name == "" { diff --git a/go-backend/internal/gateway/wsclient_test.go b/go-backend/internal/gateway/wsclient_test.go new file mode 100644 index 0000000..c028acf --- /dev/null +++ b/go-backend/internal/gateway/wsclient_test.go @@ -0,0 +1,105 @@ +package gateway + +import ( + "testing" + + "code.cubecraftcreations.com/CubeCraft-Creations/Control-Center/go-backend/internal/models" +) + +func TestMapSessionStatus(t *testing.T) { + tests := []struct { + input string + expected models.AgentStatus + }{ + {"running", models.AgentStatusActive}, + {"streaming", models.AgentStatusActive}, + {"done", models.AgentStatusIdle}, + {"error", models.AgentStatusError}, + {"", models.AgentStatusIdle}, + {"garbage", models.AgentStatusIdle}, + } + for _, tt := range tests { + result := mapSessionStatus(tt.input) + if result != tt.expected { + t.Errorf("mapSessionStatus(%q) = %q, want %q", tt.input, result, tt.expected) + } + } +} + +func TestAgentItemToCard(t *testing.T) { + t.Run("full fields", func(t *testing.T) { + item := agentListItem{ + ID: "dex", + Name: "Dex", + Role: "backend", + Channel: "telegram", + } + card := agentItemToCard(item) + if card.ID != "dex" { + t.Errorf("ID = %q, want %q", card.ID, "dex") + } + if card.DisplayName != "Dex" { + t.Errorf("DisplayName = %q, want %q", card.DisplayName, "Dex") + } + if card.Role != "backend" { + t.Errorf("Role = %q, want %q", card.Role, "backend") + } + if card.Channel != "telegram" { + t.Errorf("Channel = %q, want %q", card.Channel, "telegram") + } + if card.Status != models.AgentStatusIdle { + t.Errorf("Status = %q, want %q", card.Status, models.AgentStatusIdle) + } + }) + + t.Run("empty fields use defaults", func(t *testing.T) { + item := agentListItem{ + ID: "otto", + } + card := agentItemToCard(item) + if card.ID != "otto" { + t.Errorf("ID = %q, want %q", card.ID, "otto") + } + if card.DisplayName != "otto" { + t.Errorf("DisplayName = %q, want %q (should fallback to ID)", card.DisplayName, "otto") + } + if card.Role != "agent" { + t.Errorf("Role = %q, want %q (default)", card.Role, "agent") + } + if card.Channel != "unknown" { + t.Errorf("Channel = %q, want %q (per Grimm requirement)", card.Channel, "unknown") + } + if card.Status != models.AgentStatusIdle { + t.Errorf("Status = %q, want %q", card.Status, models.AgentStatusIdle) + } + }) + + t.Run("empty name falls back to ID", func(t *testing.T) { + item := agentListItem{ + ID: "hex", + Name: "", + Role: "database", + } + card := agentItemToCard(item) + if card.DisplayName != "hex" { + t.Errorf("DisplayName = %q, want %q (ID fallback)", card.DisplayName, "hex") + } + }) +} + +func TestStrPtr(t *testing.T) { + s := "hello" + p := strPtr(s) + if p == nil { + t.Fatal("strPtr returned nil") + } + if *p != s { + t.Errorf("strPtr(%q) = %q, want %q", s, *p, s) + } + + empty := "" + ep := strPtr(empty) + if *ep != empty { + t.Errorf("strPtr(empty) = %q, want %q", *ep, empty) + } +} \ No newline at end of file