Files
galaxy-game/backend/internal/geo/geo_test.go
T
2026-05-06 10:14:55 +03:00

83 lines
1.8 KiB
Go

package geo
import (
"context"
"testing"
"time"
"go.uber.org/zap"
)
func TestLanguageForCountry(t *testing.T) {
cases := map[string]string{
"DE": "de",
"de": "de", // case-insensitive input
"RU": "ru",
"BR": "pt",
"": "",
"ZZ": "",
}
for input, want := range cases {
if got := languageForCountry(input); got != want {
t.Errorf("languageForCountry(%q) = %q, want %q", input, got, want)
}
}
}
func TestLookupCountryNilSafety(t *testing.T) {
var s *Service
if got := s.LookupCountry("8.8.8.8"); got != "" {
t.Errorf("nil Service LookupCountry = %q, want empty", got)
}
}
func TestLanguageForIPNilSafety(t *testing.T) {
var s *Service
if got := s.LanguageForIP("8.8.8.8"); got != "" {
t.Errorf("nil Service LanguageForIP = %q, want empty", got)
}
}
func TestSetLoggerNilSafety(t *testing.T) {
var s *Service
s.SetLogger(zap.NewNop())
s.SetLogger(nil)
live := &Service{}
live.SetLogger(nil) // does not panic; falls back to nop logger.
}
func TestDrainNilSafety(t *testing.T) {
var s *Service
s.Drain(context.Background())
}
func TestDrainReturnsWhenContextDone(t *testing.T) {
live := &Service{}
live.bgCtx, live.bgCancel = context.WithCancel(context.Background())
ctx, cancel := context.WithTimeout(context.Background(), 50*time.Millisecond)
defer cancel()
start := time.Now()
live.Drain(ctx)
if elapsed := time.Since(start); elapsed > 5*time.Second {
t.Fatalf("Drain blocked too long: %s", elapsed)
}
}
func TestCloseIdempotent(t *testing.T) {
live := &Service{}
live.bgCtx, live.bgCancel = context.WithCancel(context.Background())
if err := live.Close(); err != nil {
t.Fatalf("first Close: %v", err)
}
if err := live.Close(); err != nil {
t.Fatalf("second Close: %v", err)
}
var nilSvc *Service
if err := nilSvc.Close(); err != nil {
t.Fatalf("nil Service Close: %v", err)
}
}