live-sfu-demo/internal/config/config_test.go

66 lines
1.9 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package config
import "testing"
func TestDistributorListParsing(t *testing.T) {
c := &Config{Distributors: "srs-hls,cf,cdn"}
got := c.DistributorList()
if len(got) != 3 || got[0] != "srs-hls" {
t.Fatalf("unexpected distributors %v", got)
}
if got[1] != "cf" || got[2] != "cdn" {
t.Fatalf("unexpected order %v", got)
}
if c.CDNEnabled() {
t.Fatalf("should not be enabled without CDN_RTMP_URL")
}
// alias normalization
c2 := &Config{Distributors: "srs,cloudflare,cdn"}
got2 := c2.DistributorList()
if got2[0] != "srs-hls" || got2[1] != "cf" {
t.Fatalf("alias normalize failed %v", got2)
}
}
func TestDistributorListDedup(t *testing.T) {
c := &Config{Distributors: "srs-hls,srs-hls,cf"}
got := c.DistributorList()
if len(got) != 2 {
t.Fatalf("want dedup 2, got %v", got)
}
}
func TestValidateCDNRequiresURLWhenEnabled(t *testing.T) {
// 运行时允许 cdn 占位:未配置 RTMP 仅影响分发就绪,不阻断配置保存
c := &Config{Distributors: "cdn", CDNRTMPURL: ""}
if err := c.ValidateDistributors(); err != nil {
t.Fatalf("cdn占位不应阻断配置保存,got %v", err)
}
c2 := &Config{Distributors: "cdn", CDNRTMPURL: "rtmp://cdn.example.com/live"}
if err := c2.ValidateDistributors(); err != nil {
t.Fatalf("unexpected error %v", err)
}
c3 := &Config{Distributors: "srs-hls,cf", CDNRTMPURL: ""}
if err := c3.ValidateDistributors(); err != nil {
t.Fatalf("should not require CDN URL when cdn not in list, got %v", err)
}
// 分发维度的真实就绪由 Provider.Configured / DistributionTarget.status 表达
if c.CDNEnabled() {
t.Fatal("空 CDN_RTMP_URL 不应视为已启用")
}
if !c2.CDNEnabled() {
t.Fatal("非空 CDN_RTMP_URL 应视为已启用")
}
}
func TestCDNEnabled(t *testing.T) {
c := &Config{CDNRTMPURL: ""}
if c.CDNEnabled() {
t.Fatal("empty should be disabled")
}
c2 := &Config{CDNRTMPURL: "rtmp://a/b"}
if !c2.CDNEnabled() {
t.Fatal("non-empty should be enabled")
}
}