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] 100.0% of ~ 1.00KiB at 976.20B/s ETA Unknown (frag 0/18)", pct: 100.0, speed: "976.20B/s", eta: "", hasRes: true, }, { line: "[download] 16.7% of ~ 18.00KiB at 976.20B/s ETA Unknown (frag 0/18)", pct: 16.7, speed: "976.20B/s", eta: "", hasRes: true, }, { line: "[download] 11.1% of ~ 8.34MiB at 716.47KiB/s ETA 00:08 (frag 1/18)", pct: 11.1, speed: "716.47KiB/s", eta: "00:08", 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}, {"[download] 100.0% of ~ 1.00KiB at 976.20B/s ETA Unknown (frag 0/18)", "hls frag first", true}, {"[download] 16.7% of ~ 18.00KiB at 976.20B/s ETA Unknown (frag 0/18)", "hls frag", true}, {"[download] 11.1% of ~ 8.34MiB at 716.47KiB/s ETA 00:08 (frag 1/18)", "hls frag with eta", 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) } }) } }