package service import ( "testing" "github.com/noderunners/dlbot/internal/media" "github.com/noderunners/dlbot/internal/ytdlp" ) const mb = 1 << 20 func TestBestFit(t *testing.T) { cases := []struct { name string maxHeight int sizes map[string]int64 cap int64 want media.Profile wantFits bool }{ {"everything fits", 1080, map[string]int64{"best": 40 * mb, "1080": 40 * mb, "720": 20 * mb, "480": 10 * mb}, 50 * mb, media.Best, true}, {"best is too big", 2160, map[string]int64{"best": 300 * mb, "1080": 120 * mb, "720": 45 * mb, "480": 20 * mb}, 50 * mb, media.P720, true}, {"only 480 fits", 1080, map[string]int64{"best": 90 * mb, "1080": 90 * mb, "720": 60 * mb, "480": 30 * mb}, 50 * mb, media.P480, true}, {"nothing fits", 1080, map[string]int64{"best": 900 * mb, "1080": 900 * mb, "720": 400 * mb, "480": 200 * mb}, 50 * mb, media.P480, false}, {"exactly the cap fits", 720, map[string]int64{"best": 50 * mb}, 50 * mb, media.Best, true}, {"no limit takes the best", 1080, map[string]int64{"best": 900 * mb}, 0, media.Best, true}, // Missing size data. {"no sizes at all", 1080, map[string]int64{}, 50 * mb, media.Best, true}, {"nil sizes", 0, nil, 50 * mb, media.Best, true}, {"unknown sizes are skipped when others are known", 1080, map[string]int64{"best": 200 * mb, "480": 20 * mb}, 50 * mb, media.P480, true}, {"nothing known fits", 1080, map[string]int64{"best": 200 * mb, "720": 90 * mb}, 50 * mb, media.P720, false}, // Qualities above the source are not considered. {"small source", 480, map[string]int64{"best": 30 * mb, "480": 30 * mb}, 10 * mb, media.P480, false}, {"small source fits", 360, map[string]int64{"best": 8 * mb}, 10 * mb, media.Best, true}, {"unknown height keeps every quality", 0, map[string]int64{"best": 80 * mb, "720": 40 * mb}, 50 * mb, media.P720, true}, } for _, c := range cases { info := &CardInfo{MaxHeight: c.maxHeight, Sizes: c.sizes} got, fits := BestFit(info, c.cap) if got != c.want || fits != c.wantFits { t.Errorf("%s: BestFit = %s, %v; want %s, %v", c.name, got, fits, c.want, c.wantFits) } } } // TestBestFitFromFormatLists runs the selection on yt-dlp format lists, // through the same size estimates the card uses. func TestBestFitFromFormatLists(t *testing.T) { audio := ytdlp.Format{FormatID: "a", Ext: "m4a", VCodec: "none", ACodec: "mp4a", Filesize: 3 * mb} video := func(h int, size, approx int64, tbr float64) ytdlp.Format { return ytdlp.Format{FormatID: "v", Ext: "mp4", VCodec: "avc1", ACodec: "none", Height: ytdlp.Int(h), Filesize: ytdlp.Int(size), FilesizeAprx: ytdlp.Int(approx), TBR: ytdlp.Float(tbr)} } cases := []struct { name string duration float64 formats []ytdlp.Format want media.Profile fits bool }{ {"filesize", 300, []ytdlp.Format{audio, video(1080, 120*mb, 0, 0), video(720, 40*mb, 0, 0), video(480, 15*mb, 0, 0)}, media.P720, true}, {"filesize_approx only", 300, []ytdlp.Format{audio, video(1080, 0, 90*mb, 0), video(720, 0, 30*mb, 0)}, media.P720, true}, // 720p means "the best up to 720p", here the 480p format. {"bitrate times duration", 600, []ytdlp.Format{audio, video(1080, 0, 0, 2000), video(480, 0, 0, 500)}, media.P720, true}, {"bitrate, only the smallest fits", 600, []ytdlp.Format{audio, video(1080, 0, 0, 2000), video(720, 0, 0, 1200), video(480, 0, 0, 500)}, media.P480, true}, {"no size data at all", 300, []ytdlp.Format{{FormatID: "b", Ext: "mp4", VCodec: "avc1", ACodec: "mp4a", Height: 720}}, media.Best, true}, {"too big everywhere", 300, []ytdlp.Format{audio, video(720, 400*mb, 0, 0), video(480, 200*mb, 0, 0)}, media.P480, false}, } for _, c := range cases { info := cardInfo("https://example.com/v", &ytdlp.Info{ID: "v", Title: "T", Duration: ytdlp.Float(c.duration), Formats: c.formats}) got, fits := BestFit(info, 50*mb) if got != c.want || fits != c.fits { t.Errorf("%s: BestFit = %s, %v; want %s, %v (sizes %v)", c.name, got, fits, c.want, c.fits, info.Sizes) } } }