Files
uptodownbot/internal/dl/ytdlp_test.go
db123 66a8cd1128
All checks were successful
CI / build (push) Successful in 36s
fix: resolve back step, audio labels, progress parsing, file size pre-check, and stale job cleanup
- Fix handleBackStep to redirect to download prompt when step state is nil
  (previously silent no-op after container selection cleared the state)
- Improve formatLabel for audio-only entries without bitrate: use ID + extension
  instead of generic 'audio', fixing duplicate entries in format list
- Add yt-dlp --progress-template for reliable JSON progress parsing across all
  sites (fixes 0.0% progress on sites like xnxx.com where [download] lines
  are absent); keep regex parser as fallback for older yt-dlp versions
- Add file size pre-check in handleContainerSelection before download starts
  (previously checked only after download completed, wasting bandwidth)
- Clear stale active jobs and step state on /start for a clean slate
2026-06-25 22:45:25 +03:30

105 lines
3.1 KiB
Go

package dl
import (
"testing"
"uptodownBot/internal/domain"
)
func TestFilterFormatsByType(t *testing.T) {
formats := []domain.Format{
{ID: "1", HasVideo: true, HasAudio: false, IsVideoOnly: true, IsAudioOnly: false},
{ID: "2", HasVideo: false, HasAudio: true, IsVideoOnly: false, IsAudioOnly: true},
{ID: "3", HasVideo: true, HasAudio: true, IsVideoOnly: false, IsAudioOnly: false},
{ID: "4", HasVideo: true, HasAudio: false, IsVideoOnly: true, IsAudioOnly: false},
{ID: "5", HasVideo: false, HasAudio: true, IsVideoOnly: false, IsAudioOnly: true},
}
tests := []struct {
mediaType domain.MediaType
wantIDs []string
}{
{domain.MediaTypeAudio, []string{"2", "5"}},
{domain.MediaTypeVideo, []string{"1", "4"}},
{domain.MediaTypeVideoAudio, []string{"3"}},
}
for _, tt := range tests {
got := FilterFormatsByType(formats, tt.mediaType)
if len(got) != len(tt.wantIDs) {
t.Errorf("FilterFormatsByType(%v) = %d results, want %d", tt.mediaType, len(got), len(tt.wantIDs))
continue
}
for i, f := range got {
if f.ID != tt.wantIDs[i] {
t.Errorf("FilterFormatsByType(%v)[%d].ID = %s, want %s", tt.mediaType, i, f.ID, tt.wantIDs[i])
}
}
}
}
func TestDeduplicateResolutions(t *testing.T) {
formats := []domain.Format{
{ID: "1", Resolution: "1920x1080", Height: 1080, IsVideoOnly: true},
{ID: "2", Resolution: "1920x1080", Height: 1080, IsVideoOnly: true},
{ID: "3", Resolution: "1280x720", Height: 720, IsVideoOnly: true},
{ID: "4", Bitrate: "128k", IsAudioOnly: true},
{ID: "5", Bitrate: "128k", IsAudioOnly: true},
}
got := DeduplicateResolutions(formats)
if len(got) != 3 {
t.Errorf("DeduplicateResolutions = %d results, want 3", len(got))
}
}
func TestContainerOptions(t *testing.T) {
tests := []struct {
mt domain.MediaType
want []string
}{
{domain.MediaTypeAudio, []string{"mp3", "m4a", "opus"}},
{domain.MediaTypeVideo, []string{"mp4", "mkv", "webm"}},
{domain.MediaTypeVideoAudio, []string{"mp4", "mkv"}},
}
for _, tt := range tests {
got := ContainerOptions(tt.mt)
if len(got) != len(tt.want) {
t.Errorf("ContainerOptions(%v) = %v, want %v", tt.mt, got, tt.want)
continue
}
for i, c := range got {
if c != tt.want[i] {
t.Errorf("ContainerOptions(%v)[%d] = %s, want %s", tt.mt, i, c, tt.want[i])
}
}
}
}
func TestContainerOptionsZeroValue(t *testing.T) {
got := ContainerOptions(domain.MediaType(0))
want := []string{"mp4"}
if len(got) != 1 || got[0] != want[0] {
t.Errorf("ContainerOptions(0) = %v, want %v", got, want)
}
}
func TestFormatLabel(t *testing.T) {
tests := []struct {
f domain.Format
want string
}{
{domain.Format{IsAudioOnly: true, Bitrate: "128k"}, "128k"},
{domain.Format{ID: "140", Extension: "m4a", IsAudioOnly: true}, "140 (m4a)"},
{domain.Format{ID: "251", IsAudioOnly: true}, "251"},
{domain.Format{Resolution: "1920x1080", Height: 1080}, "1920x1080"},
{domain.Format{Height: 720}, "720p"},
{domain.Format{ID: "137"}, "137"},
}
for _, tt := range tests {
got := formatLabel(tt.f)
if got != tt.want {
t.Errorf("formatLabel(%+v) = %q, want %q", tt.f, got, tt.want)
}
}
}