package factory import ( "bytes" "testing" "github.com/jan/fm-rds-tx/internal/config" ) func TestBuildSourceNone(t *testing.T) { cfg := config.Default() cfg.Ingest.Kind = "none" src, ingress, err := BuildSource(cfg, Deps{}) if err != nil { t.Fatalf("build source: %v", err) } if src != nil || ingress != nil { t.Fatalf("expected nil source and ingress for kind=none") } } func TestBuildSourceHTTPRawProvidesIngress(t *testing.T) { cfg := config.Default() cfg.Ingest.Kind = "http-raw" src, ingress, err := BuildSource(cfg, Deps{}) if err != nil { t.Fatalf("build source: %v", err) } if src == nil { t.Fatalf("expected source") } if ingress == nil { t.Fatalf("expected ingress for http-raw") } } func TestBuildSourceKindIsNormalized(t *testing.T) { cfg := config.Default() cfg.Ingest.Kind = " HTTP-RAW " src, ingress, err := BuildSource(cfg, Deps{}) if err != nil { t.Fatalf("build source: %v", err) } if src == nil || ingress == nil { t.Fatalf("expected source and ingress for normalized http-raw kind") } if got := src.Descriptor().Kind; got != "http-raw" { t.Fatalf("source kind=%q want http-raw", got) } } func TestBuildSourceStdin(t *testing.T) { cfg := config.Default() cfg.Ingest.Kind = "stdin" src, ingress, err := BuildSource(cfg, Deps{Stdin: bytes.NewReader(nil)}) if err != nil { t.Fatalf("build source: %v", err) } if src == nil { t.Fatalf("expected source") } if ingress != nil { t.Fatalf("expected no ingress for stdin") } if got := src.Descriptor().Kind; got != "stdin-pcm" { t.Fatalf("source kind=%s", got) } } func TestBuildSourceIcecastUsesDecoderPreference(t *testing.T) { cfg := config.Default() cfg.Ingest.Kind = "icecast" cfg.Ingest.Icecast.URL = "http://localhost:8000/stream" cfg.Ingest.Icecast.Decoder = "ffmpeg" src, ingress, err := BuildSource(cfg, Deps{}) if err != nil { t.Fatalf("build source: %v", err) } if src == nil { t.Fatalf("expected source") } if ingress != nil { t.Fatalf("expected no ingress for icecast") } if got := src.Descriptor().Codec; got != "ffmpeg" { t.Fatalf("codec=%s want ffmpeg", got) } } func TestBuildSourceSRT(t *testing.T) { cfg := config.Default() cfg.Ingest.Kind = "srt" cfg.Ingest.SRT.URL = "srt://127.0.0.1:9000?mode=listener" cfg.Ingest.SRT.Mode = "listener" cfg.Ingest.SRT.SampleRateHz = 48000 cfg.Ingest.SRT.Channels = 2 src, ingress, err := BuildSource(cfg, Deps{}) if err != nil { t.Fatalf("build source: %v", err) } if src == nil { t.Fatalf("expected source") } if ingress != nil { t.Fatalf("expected no ingress for srt") } if got := src.Descriptor().Kind; got != "srt" { t.Fatalf("source kind=%s", got) } } func TestBuildSourceUnsupportedKind(t *testing.T) { cfg := config.Default() cfg.Ingest.Kind = "nope" _, _, err := BuildSource(cfg, Deps{}) if err == nil { t.Fatalf("expected error") } } func TestSampleRateForKind(t *testing.T) { cfg := config.Default() cfg.Ingest.Kind = "stdin" cfg.Ingest.Stdin.SampleRateHz = 48000 if got := SampleRateForKind(cfg); got != 48000 { t.Fatalf("stdin sample rate=%d", got) } cfg.Ingest.Kind = "http-raw" cfg.Ingest.HTTPRaw.SampleRateHz = 32000 if got := SampleRateForKind(cfg); got != 32000 { t.Fatalf("http-raw sample rate=%d", got) } cfg.Ingest.Kind = "icecast" if got := SampleRateForKind(cfg); got != 44100 { t.Fatalf("icecast sample rate=%d", got) } cfg.Ingest.Kind = "srt" cfg.Ingest.SRT.SampleRateHz = 48000 if got := SampleRateForKind(cfg); got != 48000 { t.Fatalf("srt sample rate=%d", got) } }