package db import "testing" func TestNormalizeVariantsEnsuresFHD(t *testing.T) { got := NormalizeVariants([]VariantSpec{ {ID: "hd", Enabled: true, Height: 720}, }) if len(got) < 2 { t.Fatalf("len=%d", len(got)) } if got[0].ID != "fhd" || got[0].Height != 0 || !got[0].Enabled { t.Fatalf("source = %+v", got[0]) } if path := VariantPlayPath("rte-one", "hd", 720); path != "/hls/rte-one-hd/index.m3u8" { t.Fatalf("path=%s", path) } if path := VariantPlayPath("rte-one", "fhd", 0); path != "/hls/rte-one/index.m3u8" { t.Fatalf("fhd path=%s", path) } } func TestParseVariantsJSONDefault(t *testing.T) { got := ParseVariantsJSON("") // The default ladder offers the rungs but turns none of the encoding ones // on, so a new stream costs exactly one pass-through copy. if len(got) != len(DefaultVariants()) { t.Fatalf("default len=%d want %d", len(got), len(DefaultVariants())) } if scaled := ScaledVariants(got); len(scaled) != 0 { t.Fatalf("default should encode nothing, got %v", scaled) } enabled := EnabledVariants(got) if len(enabled) != 1 || !enabled[0].IsSource() { t.Fatalf("default should enable only the source rung, got %+v", enabled) } } // An audio-only rung is height 0, exactly like the source copy. The source // rule must not swallow it, or enabling audio would silently overwrite the // pass-through rung. func TestAudioOnlyRungSurvivesNormalize(t *testing.T) { got := NormalizeVariants([]VariantSpec{ {ID: SourceVariantID, Label: "FHD", Enabled: true, Height: 0}, {ID: "audio", Label: "Audio", Enabled: true, AudioOnly: true}, }) var source, audio *VariantSpec for i := range got { switch { case got[i].AudioOnly: audio = &got[i] case got[i].IsSource(): source = &got[i] } } if source == nil { t.Fatal("source rung disappeared") } if audio == nil { t.Fatal("audio rung was absorbed into the source rung") } if audio.ID == SourceVariantID { t.Errorf("audio rung must not take the source id, got %q", audio.ID) } if audio.VideoBitrate != "" { t.Errorf("audio rung should carry no video bitrate, got %q", audio.VideoBitrate) } if audio.AudioBitrate == "" { t.Error("audio rung should get a default audio bitrate") } // It needs its own encoder, so it must count as scaled. scaled := ScaledVariants(got) foundAudio := false for _, v := range scaled { if v.AudioOnly { foundAudio = true } } if !foundAudio { t.Error("audio rung should be in ScaledVariants (it needs an encoder)") } } // An audio rung sent without an explicit id must not claim the source id. func TestAudioOnlyWithoutIDGetsOwnID(t *testing.T) { got := NormalizeVariants([]VariantSpec{{Enabled: true, AudioOnly: true}}) var audio, source int for _, v := range got { if v.AudioOnly { audio++ if v.ID != "audio" { t.Errorf("want id audio, got %q", v.ID) } } if v.IsSource() { source++ } } if audio != 1 || source != 1 { t.Fatalf("want one audio and one source rung, got audio=%d source=%d (%+v)", audio, source, got) } } func TestVariantDirAndOutputPaths(t *testing.T) { cases := []struct { name, id string height int audio bool format string want string }{ // The original mpegts HLS URL must never move. {"rte-one", "fhd", 0, false, FormatHLS, "/hls/rte-one/index.m3u8"}, {"rte-one", "hd", 720, false, FormatHLS, "/hls/rte-one-hd/index.m3u8"}, {"rte-one", "fhd", 0, false, FormatCMAF, "/hls/rte-one/cmaf/index.m3u8"}, {"rte-one", "hd", 720, false, FormatCMAF, "/hls/rte-one-hd/cmaf/index.m3u8"}, {"rte-one", "fhd", 0, false, FormatDASH, "/hls/rte-one/dash/index.mpd"}, {"rte-one", "fhd", 0, false, FormatTS, "/live/rte-one.ts"}, {"rte-one", "hd", 720, false, FormatTS, "/live/rte-one-hd.ts"}, {"rte-one", "audio", 0, true, FormatHLS, "/hls/rte-one-audio/index.m3u8"}, } for _, c := range cases { if got := OutputPlayPath(c.name, c.id, c.height, c.audio, c.format); got != c.want { t.Errorf("OutputPlayPath(%s,%s,%d,%v,%s) = %q want %q", c.name, c.id, c.height, c.audio, c.format, got, c.want) } } // A stream name containing a dash must keep its own directory. if got := VariantDir("city-news", "fhd", 0, false); got != "city-news" { t.Errorf("source dir = %q", got) } if got := VariantDir("city-news", "hd", 720, false); got != "city-news-hd" { t.Errorf("variant dir = %q", got) } } func TestDefaultOutputsOnlyHLS(t *testing.T) { got := ParseOutputsJSON("") on := EnabledOutputs(got) if len(on) != 1 || on[0].Format != FormatHLS { t.Fatalf("default outputs should be mpegts HLS only, got %+v", on) } } func TestNormalizeOutputsKeepsFullSetAndAliases(t *testing.T) { // A client toggling DASH on by id alone should get the whole set back // with DASH enabled and HLS still present. got := NormalizeOutputs([]OutputSpec{ {ID: "hls", Format: FormatHLS, Enabled: true}, {ID: "dash", Format: "mpd", Enabled: true}, {ID: "bogus", Format: "quicktime", Enabled: true}, }) if len(got) != len(DefaultOutputs()) { t.Fatalf("want the full output set, got %d: %+v", len(got), got) } byFormat := map[string]OutputSpec{} for _, o := range got { byFormat[o.Format] = o } if !byFormat[FormatDASH].Enabled { t.Error("mpd alias should have enabled DASH") } if !byFormat[FormatHLS].Enabled { t.Error("HLS should still be enabled") } if byFormat[FormatCMAF].Enabled { t.Error("CMAF should have stayed off") } for _, o := range got { if o.Label == "" || o.ID == "" { t.Errorf("output missing id/label: %+v", o) } } } // Publishing nothing would make the stream unreachable, so an all-off set // falls back to HLS rather than going dark. func TestNormalizeOutputsNeverPublishesNothing(t *testing.T) { got := NormalizeOutputs([]OutputSpec{ {ID: "hls", Format: FormatHLS, Enabled: false}, {ID: "dash", Format: FormatDASH, Enabled: false}, }) if len(EnabledOutputs(got)) == 0 { t.Fatal("an all-off output set should fall back to HLS") } for _, o := range EnabledOutputs(got) { if o.Format != FormatHLS { t.Errorf("fallback should be HLS, got %q", o.Format) } } } func TestBuildPublishLinks(t *testing.T) { variants, err := EncodeVariantsJSON([]VariantSpec{ {ID: SourceVariantID, Enabled: true, Height: 0}, {ID: "hd", Enabled: true, Height: 720}, }) if err != nil { t.Fatal(err) } outputs, err := EncodeOutputsJSON([]OutputSpec{ {ID: "hls", Format: FormatHLS, Enabled: true}, {ID: "ts", Format: FormatTS, Enabled: true}, }) if err != nil { t.Fatal(err) } links := BuildPublishLinks("city-news", variants, outputs) // master + 2 variants x 2 outputs if len(links) != 5 { t.Fatalf("want 5 links, got %d: %+v", len(links), links) } if !links[0].Master || links[0].PlayPath != "/hls/city-news/master.m3u8" { t.Errorf("first link should be the ABR master, got %+v", links[0]) } seen := map[string]bool{} for _, l := range links { if l.PlayPath == "" { t.Errorf("link with no path: %+v", l) } if seen[l.PlayPath] { t.Errorf("duplicate link path %q", l.PlayPath) } seen[l.PlayPath] = true } for _, want := range []string{ "/hls/city-news/index.m3u8", "/hls/city-news-hd/index.m3u8", "/live/city-news.ts", "/live/city-news-hd.ts", } { if !seen[want] { t.Errorf("missing expected link %q", want) } } } // With a single rung there is nothing to switch between, so no master. func TestBuildPublishLinksNoMasterForSingleRung(t *testing.T) { variants, _ := EncodeVariantsJSON([]VariantSpec{{ID: SourceVariantID, Enabled: true}}) outputs, _ := EncodeOutputsJSON(DefaultOutputs()) for _, l := range BuildPublishLinks("solo", variants, outputs) { if l.Master { t.Error("a single-rung stream should not advertise an ABR master") } } } func TestVariantWidthAndBitrate(t *testing.T) { if got := VariantWidth(720); got != 1280 { t.Errorf("720p width = %d want 1280", got) } if got := VariantWidth(1080); got != 1920 { t.Errorf("1080p width = %d want 1920", got) } if got := VariantWidth(0); got != 0 { t.Errorf("source height should give width 0, got %d", got) } if got := VariantWidth(480); got%2 != 0 { t.Errorf("width must be even for H.264, got %d", got) } for in, want := range map[string]int{ "2800k": 2800000, "1200K": 1200000, "5M": 5000000, "": 0, "fast": 0, "900": 900, } { if got := BitrateBps(in); got != want { t.Errorf("BitrateBps(%q) = %d want %d", in, got, want) } } } func TestValidKeyPairs(t *testing.T) { const kid = "0123456789abcdef0123456789abcdef" const key = "fedcba9876543210fedcba9876543210" good := []string{ kid + ":" + key, kid + ":" + key + "\n" + kid + ":" + key, // Multi-KID separated by commas and semicolons, as the supervisor // accepts. A single-pair check would reject these, which is why the // agent used to re-capture multi-KID streams forever. kid + ":" + key + "," + kid + ":" + key, kid + ":" + key + ";" + kid + ":" + key, " " + kid + ":" + key + " \n\n", } for _, g := range good { if !ValidKeyPairs(g) { t.Errorf("ValidKeyPairs(%q) = false, want true", g) } } bad := []string{ "", " ", "notakey", kid, kid + ":", ":" + key, kid + ":deadbeef", kid + ":" + key + "\nbroken", "zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz:" + key, } for _, b := range bad { if ValidKeyPairs(b) { t.Errorf("ValidKeyPairs(%q) = true, want false", b) } } } func TestNormalizeTags(t *testing.T) { for in, want := range map[string]string{ "Council, Sports": "council,sports", "council,council": "council", " Live Events ": "live-events", "": "", ",,,": "", "A,b,C": "a,b,c", "Town Hall,town hall": "town-hall", } { if got := normalizeTags(in); got != want { t.Errorf("normalizeTags(%q) = %q want %q", in, got, want) } } }