// Package config 验证 room-service 本地 YAML 配置契约。 package config import ( "testing" "time" ) // TestLoad 验证 room-service 的本地 config.yaml 能被正常读取。 func TestLoad(t *testing.T) { // 读取真实本地配置,确保端口和关键下游地址没有漂移。 cfg, err := Load("../../configs/config.yaml") if err != nil { t.Fatalf("Load failed: %v", err) } // room-service 默认只暴露 gRPC,端口必须保持 13xxx 约束。 if cfg.GRPCAddr != ":13001" { t.Fatalf("unexpected grpc addr: %s", cfg.GRPCAddr) } // lease TTL 是故障接管抖动窗口的基础参数。 if cfg.LeaseTTL != 10*time.Second { t.Fatalf("unexpected lease ttl: %s", cfg.LeaseTTL) } // SendGift 主链路依赖 wallet-service,默认地址必须与本地编排一致。 if cfg.WalletServiceAddr != "127.0.0.1:13004" { t.Fatalf("unexpected wallet addr: %s", cfg.WalletServiceAddr) } if !cfg.OutboxWorker.Enabled || cfg.OutboxWorker.PollInterval != time.Second || cfg.OutboxWorker.BatchSize != 100 || cfg.OutboxWorker.PublishTimeout != 3*time.Second || cfg.OutboxWorker.RetryStrategy != "fixed_interval" { t.Fatalf("unexpected outbox worker config: %+v", cfg.OutboxWorker) } } // TestLoadTencentExample 验证线上腾讯云 IM 模板能覆盖本地默认配置。 func TestLoadTencentExample(t *testing.T) { cfg, err := Load("../../configs/config.tencent.example.yaml") if err != nil { t.Fatalf("Load tencent example failed: %v", err) } if !cfg.TencentIM.Enabled { t.Fatalf("tencent example should enable Tencent IM bridge") } if cfg.TencentIM.SDKAppID == 0 || cfg.TencentIM.SecretKey == "" { t.Fatalf("sdk_app_id and secret_key templates must be present") } if cfg.TencentIM.AdminIdentifier == "" { t.Fatalf("admin_identifier template must be present") } if cfg.TencentIM.Endpoint == "" || cfg.TencentIM.GroupType == "" { t.Fatalf("endpoint and group_type templates must be present") } if cfg.TencentIM.RequestTimeout != 5*time.Second { t.Fatalf("unexpected request timeout: %s", cfg.TencentIM.RequestTimeout) } if !cfg.OutboxWorker.Enabled || cfg.OutboxWorker.RetryStrategy != "fixed_interval" { t.Fatalf("tencent example must configure outbox worker: %+v", cfg.OutboxWorker) } } func TestNormalizeRejectsUnknownOutboxRetryStrategy(t *testing.T) { cfg := Default() cfg.OutboxWorker.RetryStrategy = "exponential" if _, err := Normalize(cfg); err == nil { t.Fatal("Normalize should reject unsupported outbox retry_strategy") } } func TestNormalizeOutboxWorkerKeepsSafeDefaults(t *testing.T) { cfg := Default() cfg.OutboxWorker.PollInterval = 0 cfg.OutboxWorker.BatchSize = 0 cfg.OutboxWorker.PublishTimeout = 0 cfg.OutboxWorker.RetryStrategy = "" normalized, err := Normalize(cfg) if err != nil { t.Fatalf("Normalize failed: %v", err) } if normalized.OutboxWorker.PollInterval != time.Second || normalized.OutboxWorker.BatchSize != 100 || normalized.OutboxWorker.PublishTimeout != 3*time.Second || normalized.OutboxWorker.RetryStrategy != "fixed_interval" { t.Fatalf("outbox worker defaults not restored: %+v", normalized.OutboxWorker) } }