yumi-golang/internal/service/luckygift/runtime_config_test.go
2026-04-24 13:04:06 +08:00

176 lines
5.1 KiB
Go

package luckygift
import (
"chatapp3-golang/internal/config"
"chatapp3-golang/internal/model"
"context"
"testing"
"time"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
)
func TestMergeLuckyGiftProviderConfigPrefersRuntimeAccount(t *testing.T) {
base := config.LuckyGiftProviderConfig{
StandardID: 18816,
URL: "https://provider.example.com/lucky_gift/start_game",
AppID: "base-app-id",
AppChannel: "base-app-channel",
AppKey: "base-app-key",
}
merged := mergeLuckyGiftProviderConfig(base, luckyGiftRuntimeAccount{
URL: "https://runtime.example.com",
AppID: "runtime-app-id",
AppChannel: "runtime-app-channel",
AppKey: "runtime-app-key",
})
if merged.URL != "https://runtime.example.com" {
t.Fatalf("expected runtime url, got %q", merged.URL)
}
if merged.AppID != "runtime-app-id" {
t.Fatalf("expected runtime app id, got %q", merged.AppID)
}
if merged.AppChannel != "runtime-app-channel" {
t.Fatalf("expected runtime app channel, got %q", merged.AppChannel)
}
if merged.AppKey != "runtime-app-key" {
t.Fatalf("expected runtime app key, got %q", merged.AppKey)
}
}
func TestApplyLuckyGiftRuntimeAccountUsesProviderConfigRow(t *testing.T) {
account := applyLuckyGiftRuntimeAccount(luckyGiftRuntimeAccount{
URL: "https://fallback.example.com",
AppID: "fallback-app-id",
AppChannel: "fallback-app-channel",
AppKey: "fallback-app-key",
}, model.BaishunProviderConfig{
PlatformBaseURL: "https://provider.example.com",
AppID: 4581045902,
AppChannel: "yumiparty",
AppKey: "test-key",
})
if account.URL != "https://provider.example.com" {
t.Fatalf("expected row url, got %q", account.URL)
}
if account.AppID != "4581045902" {
t.Fatalf("expected row app id, got %q", account.AppID)
}
if account.AppChannel != "yumiparty" {
t.Fatalf("expected row app channel, got %q", account.AppChannel)
}
if account.AppKey != "test-key" {
t.Fatalf("expected row app key, got %q", account.AppKey)
}
}
func TestDefaultLuckyGiftRuntimeAccountUsesBaishunConfig(t *testing.T) {
account := defaultLuckyGiftRuntimeAccount(config.Config{
Baishun: config.BaishunConfig{
PlatformBaseURL: "https://provider.example.com",
AppID: 7485318192,
AppChannel: "yumiparty",
AppKey: "shared-key",
},
})
if account.URL != "https://provider.example.com" {
t.Fatalf("expected baishun platform base url, got %q", account.URL)
}
if account.AppID != "7485318192" {
t.Fatalf("expected baishun app id, got %q", account.AppID)
}
if account.AppChannel != "yumiparty" {
t.Fatalf("expected baishun app channel, got %q", account.AppChannel)
}
if account.AppKey != "shared-key" {
t.Fatalf("expected baishun app key, got %q", account.AppKey)
}
}
func TestResolveProviderConfigUsesActiveBaishunProfile(t *testing.T) {
service, db := newTestLuckyGiftService(t, config.Config{
Baishun: config.BaishunConfig{
PlatformBaseURL: "https://fallback.example.com",
AppID: 111,
AppChannel: "fallback",
AppKey: "fallback-key",
},
LuckyGift: config.LuckyGiftConfig{
Providers: []config.LuckyGiftProviderConfig{
{
StandardID: 99,
URL: "https://legacy.example.com/lucky_gift/start_game",
AppID: "legacy",
AppChannel: "legacy",
AppKey: "legacy-key",
},
},
},
})
now := time.Now()
if err := db.Create(&[]model.BaishunProviderConfig{
{
ID: 1001,
SysOrigin: "LIKEI",
Profile: "PROD",
Active: false,
PlatformBaseURL: "https://prod.example.com",
AppID: 222,
AppChannel: "prod",
AppKey: "prod-key",
CreateTime: now,
UpdateTime: now,
},
{
ID: 1002,
SysOrigin: "LIKEI",
Profile: "TEST",
Active: true,
PlatformBaseURL: "https://test.example.com",
AppID: 333,
AppChannel: "test",
AppKey: "test-key",
CreateTime: now,
UpdateTime: now.Add(time.Second),
},
}).Error; err != nil {
t.Fatalf("create provider configs: %v", err)
}
resolved, err := service.resolveProviderConfig(context.Background(), LuckyGiftDrawRequest{
SysOrigin: "LIKEI",
StandardID: 99,
})
if err != nil {
t.Fatalf("resolve provider config: %v", err)
}
endpoint, err := resolveLuckyGiftProviderEndpoint(resolved.URL)
if err != nil {
t.Fatalf("resolve lucky gift endpoint: %v", err)
}
if endpoint != "https://test.example.com/lucky_gift/start_game" {
t.Fatalf("endpoint = %q, want active test endpoint", endpoint)
}
if resolved.AppID != "333" || resolved.AppChannel != "test" || resolved.AppKey != "test-key" {
t.Fatalf("resolved account = %+v, want active test account", resolved)
}
}
func newTestLuckyGiftService(t *testing.T, cfg config.Config) (*LuckyGiftService, *gorm.DB) {
t.Helper()
db, err := gorm.Open(sqlite.Open("file:"+t.Name()+"?mode=memory&cache=shared"), &gorm.Config{})
if err != nil {
t.Fatalf("open sqlite: %v", err)
}
if err := db.AutoMigrate(&model.BaishunProviderConfig{}); err != nil {
t.Fatalf("auto migrate: %v", err)
}
return NewLuckyGiftService(cfg, db, nil, nil), db
}