Files
uptodownbot/internal/dl/progress_test.go
db123 7c02cd3d89
All checks were successful
CI / build (push) Successful in 50s
fix: progress always at 0% and context cancelled before sendDocument
- Fix progress template: remove spurious stderr: prefix and switch to
  _percent_str/_speed_str/_eta_str keys (progress.percent outputs NA
  for HLS streams with unknown total size)
- Update progress regex to match new template format:
  DLPROG| 45.2%|   1.05MiB/s|00:45
- Fix context cancellation bug: clearActiveJob was cancelling dlCtx
  before handleDownloadCompletion could send the document
- Update progress test cases to match new template output format
2026-06-25 23:49:27 +03:30

209 lines
4.4 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,
},
{
line: "DLPROG| 16.7%| 1.05KiB/s|Unknown",
pct: 16.7,
speed: "1.05KiB/s",
eta: "",
hasRes: true,
},
{
line: "DLPROG| 11.1%| 754.59KiB/s|00:08",
pct: 11.1,
speed: "754.59KiB/s",
eta: "00:08",
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)
}
})
}
}