package service import ( "testing" ) // ============================================================================= // SMS Utility Functions Tests // ============================================================================= func TestNormalizePhoneForSMS(t *testing.T) { tests := []struct { name string phone string expected string }{ {"empty string", "", ""}, {"whitespace only", " ", ""}, {"mainland phone without prefix", "13800138000", "+8613800138000"}, {"mainland phone with 86 prefix", "8613900139000", "+8613900139000"}, {"mainland phone with +86 prefix", "+8613700137000", "+8613700137000"}, {"mainland phone with 0086 prefix", "008613600136000", "+8613600136000"}, {"international phone", "+1234567890", "+1234567890"}, {"phone with spaces", " 13800138000 ", "+8613800138000"}, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { result := normalizePhoneForSMS(tt.phone) if result != tt.expected { t.Errorf("normalizePhoneForSMS(%q) = %q, want %q", tt.phone, result, tt.expected) } }) } } func TestStringPointerOrNil(t *testing.T) { t.Run("empty string returns nil", func(t *testing.T) { result := stringPointerOrNil("") if result != nil { t.Errorf("Expected nil for empty string, got %v", result) } }) t.Run("non-empty string returns pointer", func(t *testing.T) { result := stringPointerOrNil("test") if result == nil { t.Error("Expected non-nil pointer for non-empty string") } else if *result != "test" { t.Errorf("Expected 'test', got %q", *result) } }) t.Run("whitespace string returns pointer", func(t *testing.T) { result := stringPointerOrNil(" ") if result == nil { t.Error("Expected non-nil pointer for whitespace string") } }) } func TestPointerString(t *testing.T) { t.Run("nil pointer returns empty string", func(t *testing.T) { result := pointerString(nil) if result != "" { t.Errorf("Expected empty string for nil pointer, got %q", result) } }) t.Run("non-nil pointer returns value", func(t *testing.T) { val := "test" result := pointerString(&val) if result != "test" { t.Errorf("Expected 'test', got %q", result) } }) } func TestValueOrDefault(t *testing.T) { tests := []struct { name string value string fallback string expected string }{ {"empty value returns fallback", "", "default", "default"}, {"whitespace value returns fallback", " ", "default", "default"}, {"non-empty value returns value", "actual", "default", "actual"}, {"both empty returns empty", "", "", ""}, {"value with content", " content ", "default", " content "}, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { result := valueOrDefault(tt.value, tt.fallback) if result != tt.expected { t.Errorf("valueOrDefault(%q, %q) = %q, want %q", tt.value, tt.fallback, result, tt.expected) } }) } } // ============================================================================= // SMS Config Normalization Tests // ============================================================================= func TestNormalizeAliyunSMSConfig(t *testing.T) { t.Run("normalize with empty fields", func(t *testing.T) { cfg := AliyunSMSConfig{} result := normalizeAliyunSMSConfig(cfg) if result.RegionID != "cn-hangzhou" { t.Errorf("Expected default region 'cn-hangzhou', got %q", result.RegionID) } if result.CodeParamName != "code" { t.Errorf("Expected default code param 'code', got %q", result.CodeParamName) } }) t.Run("normalize with whitespace", func(t *testing.T) { cfg := AliyunSMSConfig{ AccessKeyID: " key123 ", AccessKeySecret: " secret123 ", SignName: " sign ", TemplateCode: " template ", RegionID: " cn-shanghai ", } result := normalizeAliyunSMSConfig(cfg) if result.AccessKeyID != "key123" { t.Errorf("Expected trimmed key, got %q", result.AccessKeyID) } if result.RegionID != "cn-shanghai" { t.Errorf("Expected trimmed region, got %q", result.RegionID) } }) } func TestNormalizeTencentSMSConfig(t *testing.T) { t.Run("normalize with empty fields", func(t *testing.T) { cfg := TencentSMSConfig{} result := normalizeTencentSMSConfig(cfg) if result.Region != "ap-guangzhou" { t.Errorf("Expected default region 'ap-guangzhou', got %q", result.Region) } }) t.Run("normalize with whitespace", func(t *testing.T) { cfg := TencentSMSConfig{ SecretID: " sid123 ", SecretKey: " skey123 ", AppID: " app123 ", SignName: " sign ", Region: " ap-beijing ", } result := normalizeTencentSMSConfig(cfg) if result.SecretID != "sid123" { t.Errorf("Expected trimmed secret ID, got %q", result.SecretID) } if result.Region != "ap-beijing" { t.Errorf("Expected trimmed region, got %q", result.Region) } }) }