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 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 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) } }