chore(push-gateway): ai02 module updates - config, hub, ws, kafka, health, docs
This commit is contained in:
120
services/push-gateway/internal/config/config_test.go
Normal file
120
services/push-gateway/internal/config/config_test.go
Normal file
@@ -0,0 +1,120 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"os"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// TestLoadDevMode verifies DevMode defaults and that required vars are optional.
|
||||
func TestLoadDevMode(t *testing.T) {
|
||||
t.Setenv("DEV_MODE", "true")
|
||||
t.Setenv("JWT_SECRET", "")
|
||||
t.Setenv("INTERNAL_API_TOKEN", "")
|
||||
|
||||
cfg := Load()
|
||||
|
||||
if !cfg.DevMode {
|
||||
t.Error("DevMode = false, want true")
|
||||
}
|
||||
if cfg.JWTSecret != devJWTSecret {
|
||||
t.Errorf("JWTSecret = %q, want dev default %q", cfg.JWTSecret, devJWTSecret)
|
||||
}
|
||||
if cfg.InternalAPIToken != "" {
|
||||
t.Errorf("InternalAPIToken = %q, want empty in DevMode", cfg.InternalAPIToken)
|
||||
}
|
||||
if cfg.Port != "8081" {
|
||||
t.Errorf("Port = %q, want 8081", cfg.Port)
|
||||
}
|
||||
if cfg.MaxConnsPerUser != 5 {
|
||||
t.Errorf("MaxConnsPerUser = %d, want 5", cfg.MaxConnsPerUser)
|
||||
}
|
||||
if cfg.HeartbeatInterval != 30 {
|
||||
t.Errorf("HeartbeatInterval = %d, want 30", cfg.HeartbeatInterval)
|
||||
}
|
||||
if cfg.KafkaNotificationTopic != "edu.notification.requested" {
|
||||
t.Errorf("KafkaNotificationTopic = %q, want edu.notification.requested", cfg.KafkaNotificationTopic)
|
||||
}
|
||||
if cfg.KafkaConsumerGroup != "push-gateway" {
|
||||
t.Errorf("KafkaConsumerGroup = %q, want push-gateway", cfg.KafkaConsumerGroup)
|
||||
}
|
||||
if cfg.InstanceID == "" {
|
||||
t.Error("InstanceID should default to hostname, got empty")
|
||||
}
|
||||
}
|
||||
|
||||
// TestLoadProdMode verifies production requires JWT_SECRET + INTERNAL_API_TOKEN.
|
||||
func TestLoadProdMode(t *testing.T) {
|
||||
// Ensure the env vars are unset so Load fails. We can't call os.Exit in
|
||||
// a test, so we verify the required behavior by setting DEV_MODE=false
|
||||
// and checking that Load triggers log.Fatal. Since log.Fatal calls
|
||||
// os.Exit, we instead test the happy path: all required vars set.
|
||||
t.Setenv("DEV_MODE", "false")
|
||||
t.Setenv("JWT_SECRET", "prod-secret")
|
||||
t.Setenv("INTERNAL_API_TOKEN", "prod-token")
|
||||
|
||||
cfg := Load()
|
||||
|
||||
if cfg.DevMode {
|
||||
t.Error("DevMode = true, want false")
|
||||
}
|
||||
if cfg.JWTSecret != "prod-secret" {
|
||||
t.Errorf("JWTSecret = %q, want prod-secret", cfg.JWTSecret)
|
||||
}
|
||||
if cfg.InternalAPIToken != "prod-token" {
|
||||
t.Errorf("InternalAPIToken = %q, want prod-token", cfg.InternalAPIToken)
|
||||
}
|
||||
}
|
||||
|
||||
// TestParseOrigins verifies comma-separated origin parsing.
|
||||
func TestParseOrigins(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
raw string
|
||||
want []string
|
||||
}{
|
||||
{"empty", "", nil},
|
||||
{"single", "http://localhost:3000", []string{"http://localhost:3000"}},
|
||||
{"multi", "http://a.com,http://b.com,http://c.com", []string{"http://a.com", "http://b.com", "http://c.com"}},
|
||||
{"with-spaces", " http://a.com , http://b.com ", []string{"http://a.com", "http://b.com"}},
|
||||
{"trailing-comma", "http://a.com,", []string{"http://a.com"}},
|
||||
}
|
||||
for _, tc := range tests {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
got := parseOrigins(tc.raw)
|
||||
if len(got) != len(tc.want) {
|
||||
t.Errorf("parseOrigins(%q) = %v, want %v", tc.raw, got, tc.want)
|
||||
return
|
||||
}
|
||||
for i := range got {
|
||||
if got[i] != tc.want[i] {
|
||||
t.Errorf("parseOrigins(%q)[%d] = %q, want %q", tc.raw, i, got[i], tc.want[i])
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestParseBrokers verifies comma-separated broker parsing.
|
||||
func TestParseBrokers(t *testing.T) {
|
||||
got := parseBrokers("kafka1:9092,kafka2:9092,kafka3:9092")
|
||||
if len(got) != 3 {
|
||||
t.Fatalf("parseBrokers returned %d items, want 3", len(got))
|
||||
}
|
||||
if got[0] != "kafka1:9092" || got[1] != "kafka2:9092" || got[2] != "kafka3:9092" {
|
||||
t.Errorf("parseBrokers = %v", got)
|
||||
}
|
||||
}
|
||||
|
||||
// TestGenerateInstanceID verifies fallback to hostname.
|
||||
func TestGenerateInstanceID(t *testing.T) {
|
||||
id := generateInstanceID()
|
||||
if id == "" {
|
||||
t.Error("generateInstanceID returned empty string")
|
||||
}
|
||||
host, err := os.Hostname()
|
||||
if err == nil && host != "" {
|
||||
if id != host {
|
||||
t.Errorf("generateInstanceID = %q, want hostname %q", id, host)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user