Files
uptodownbot/internal/dl/progress_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

195 lines
4.1 KiB
Go

package dl
import (
"math"
"testing"
)
func TestParseProgressLine(t *testing.T) {
tests := []struct {
line string
pct float64
speed string
eta string
hasRes bool
}{
{
line: "[download] 45.2% of 123.45MiB at 2.34MiB/s ETA 00:45",
pct: 45.2,
speed: "2.34MiB/s",
eta: "00:45",
hasRes: true,
},
{
line: "[download] 10.0% of ~500.00MiB at 1.50MiB/s ETA 05:30",
pct: 10.0,
speed: "1.50MiB/s",
eta: "05:30",
hasRes: true,
},
{
line: "[download] 100.0% of 50.00MiB at 5.00MiB/s ETA 00:00",
pct: 100.0,
speed: "5.00MiB/s",
eta: "00:00",
hasRes: true,
},
{
line: "[youtube] Extracting URL: https://example.com",
hasRes: false,
},
{
line: "",
hasRes: false,
},
{
line: "some random log line",
hasRes: false,
},
{
line: "[download] 0.0% of 1.00GiB at 0.00B/s ETA 00:00",
pct: 0.0,
speed: "0.00B/s",
eta: "00:00",
hasRes: true,
},
{
line: "[download] 0.0% of 1.00KiB at 100.00KiB/s ETA 00:01",
pct: 0.0,
speed: "100.00KiB/s",
eta: "00:01",
hasRes: true,
},
{
line: "[download] 50.5% of 2.00MiB at 500.00KiB/s ETA 00:02",
pct: 50.5,
speed: "500.00KiB/s",
eta: "00:02",
hasRes: true,
},
{
line: "[download] 1.2% of unknown size at 0.00B/s ETA Unknown",
hasRes: false,
},
{
line: "[Merge] Merging video and audio...",
hasRes: false,
},
}
for _, tt := range tests {
got := parseProgressLine(tt.line)
if !tt.hasRes {
if got != nil {
t.Errorf("parseProgressLine(%q) = %+v, want nil", tt.line, got)
}
continue
}
if got == nil {
t.Errorf("parseProgressLine(%q) = nil, want result", tt.line)
continue
}
if math.Abs(got.pct-tt.pct) > 0.01 {
t.Errorf("parseProgressLine(%q).pct = %f, want %f", tt.line, got.pct, tt.pct)
}
if got.speed != tt.speed {
t.Errorf("parseProgressLine(%q).speed = %q, want %q", tt.line, got.speed, tt.speed)
}
if got.eta != tt.eta {
t.Errorf("parseProgressLine(%q).eta = %q, want %q", tt.line, got.eta, tt.eta)
}
}
}
func TestParseProgressTemplate(t *testing.T) {
tests := []struct {
line string
pct float64
speed string
eta string
hasRes bool
}{
{
line: "DLPROG|45.2|2.34MiB/s|00:45",
pct: 45.2,
speed: "2.34MiB/s",
eta: "00:45",
hasRes: true,
},
{
line: "DLPROG|100.0|5.00MiB/s|00:00",
pct: 100.0,
speed: "5.00MiB/s",
eta: "00:00",
hasRes: true,
},
{
line: "DLPROG|0.0||",
pct: 0.0,
speed: "",
eta: "",
hasRes: true,
},
{
line: "not a progress line",
hasRes: false,
},
{
line: "",
hasRes: false,
},
{
line: "[download] 45.2% of 123.45MiB at 2.34MiB/s ETA 00:45",
hasRes: false,
},
{
line: "DLPROG|50.0|Unknown|Unknown",
pct: 50.0,
speed: "",
eta: "",
hasRes: true,
},
}
for _, tt := range tests {
got := parseProgressTemplate(tt.line)
if !tt.hasRes {
if got != nil {
t.Errorf("parseProgressTemplate(%q) = %+v, want nil", tt.line, got)
}
continue
}
if got == nil {
t.Errorf("parseProgressTemplate(%q) = nil, want result", tt.line)
continue
}
if math.Abs(got.pct-tt.pct) > 0.01 {
t.Errorf("parseProgressTemplate(%q).pct = %f, want %f", tt.line, got.pct, tt.pct)
}
if got.speed != tt.speed {
t.Errorf("parseProgressTemplate(%q).speed = %q, want %q", tt.line, got.speed, tt.speed)
}
if got.eta != tt.eta {
t.Errorf("parseProgressTemplate(%q).eta = %q, want %q", tt.line, got.eta, tt.eta)
}
}
}
func TestParseProgressLineEdgeCases(t *testing.T) {
tests := []struct {
line string
name string
hasRes bool
}{
{"[download] 0.0% of 1.00GiB at 0.00B/s ETA 00:00", "multiple spaces", true},
{"[download] 100.0% of 1.00GiB at 999.99MiB/s ETA 00:00", "high speed", true},
{"[download] 100.0% of 999.99TiB at 1.00TiB/s ETA 00:00", "large numbers", true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := parseProgressLine(tt.line)
if tt.hasRes && got == nil {
t.Errorf("parseProgressLine(%q) = nil, want result", tt.line)
}
})
}
}