mirror of
https://git.sr.ht/~joren/streamrip-go
synced 2026-06-17 15:05:39 +02:00
initial Go port of streamrip
This commit is contained in:
1967
cmd/rip/main.go
Normal file
1967
cmd/rip/main.go
Normal file
File diff suppressed because it is too large
Load Diff
117
cmd/rip/main_test.go
Normal file
117
cmd/rip/main_test.go
Normal file
@@ -0,0 +1,117 @@
|
||||
package main
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestParseFileInputJSONItems(t *testing.T) {
|
||||
content := []byte(`[
|
||||
{"source":"qobuz","media_type":"album","id":"0066991040005"},
|
||||
{"source":"tidal","media_type":"track","id":3083287}
|
||||
]`)
|
||||
|
||||
items, urls, repeated, jsonInput, err := parseFileInput(content)
|
||||
if err != nil {
|
||||
t.Fatalf("parseFileInput() error = %v", err)
|
||||
}
|
||||
if !jsonInput {
|
||||
t.Fatalf("jsonInput = false, want true")
|
||||
}
|
||||
if len(urls) != 0 {
|
||||
t.Fatalf("urls len = %d, want 0", len(urls))
|
||||
}
|
||||
if repeated != 0 {
|
||||
t.Fatalf("repeated = %d, want 0", repeated)
|
||||
}
|
||||
if len(items) != 2 {
|
||||
t.Fatalf("items len = %d, want 2", len(items))
|
||||
}
|
||||
if items[0].Source != "qobuz" || items[0].MediaType != "album" || items[0].ID != "0066991040005" {
|
||||
t.Fatalf("unexpected first item: %+v", items[0])
|
||||
}
|
||||
if items[1].Source != "tidal" || items[1].MediaType != "track" || items[1].ID != "3083287" {
|
||||
t.Fatalf("unexpected second item: %+v", items[1])
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseFileInputTextURLsDedupes(t *testing.T) {
|
||||
content := []byte("https://tidal.com/browse/track/3083287\nhttps://tidal.com/browse/track/3083287\nhttps://www.qobuz.com/fr-fr/album/example/0066991040005\n")
|
||||
|
||||
items, urls, repeated, jsonInput, err := parseFileInput(content)
|
||||
if err != nil {
|
||||
t.Fatalf("parseFileInput() error = %v", err)
|
||||
}
|
||||
if jsonInput {
|
||||
t.Fatalf("jsonInput = true, want false")
|
||||
}
|
||||
if len(items) != 0 {
|
||||
t.Fatalf("items len = %d, want 0", len(items))
|
||||
}
|
||||
if repeated != 1 {
|
||||
t.Fatalf("repeated = %d, want 1", repeated)
|
||||
}
|
||||
if len(urls) != 2 {
|
||||
t.Fatalf("urls len = %d, want 2", len(urls))
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseFileInputRejectsInvalidJSONShape(t *testing.T) {
|
||||
content := []byte(`{"source":"qobuz","media_type":"track","id":"1"}`)
|
||||
|
||||
_, _, _, jsonInput, err := parseFileInput(content)
|
||||
if err == nil {
|
||||
t.Fatalf("expected error for non-array json")
|
||||
}
|
||||
if !jsonInput {
|
||||
t.Fatalf("jsonInput = false, want true")
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseLastFMArgsDefaults(t *testing.T) {
|
||||
opts, err := parseLastFMArgs([]string{"https://www.last.fm/user/x/playlists/123"}, "qobuz", "")
|
||||
if err != nil {
|
||||
t.Fatalf("parseLastFMArgs() error = %v", err)
|
||||
}
|
||||
if opts.Source != "qobuz" {
|
||||
t.Fatalf("source = %q, want qobuz", opts.Source)
|
||||
}
|
||||
if opts.PlaylistURL == "" {
|
||||
t.Fatalf("playlist url should not be empty")
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseLastFMArgsOptions(t *testing.T) {
|
||||
opts, err := parseLastFMArgs([]string{"--source", "tidal", "--fallback-source", "qobuz", "https://www.last.fm/user/x/playlists/123"}, "qobuz", "")
|
||||
if err != nil {
|
||||
t.Fatalf("parseLastFMArgs() error = %v", err)
|
||||
}
|
||||
if opts.Source != "tidal" || opts.FallbackSource != "qobuz" {
|
||||
t.Fatalf("unexpected options: %+v", opts)
|
||||
}
|
||||
}
|
||||
|
||||
func TestExtractLastFMPlaylistInfoAndPairs(t *testing.T) {
|
||||
html := `<h1 class="playlisting-playlist-header-title">Road & Rain</h1>
|
||||
<div data-playlisting-entry-count="2"></div>
|
||||
<a href="/music/a" title="Dreams"></a>
|
||||
<a href="/music/b" title="Fleetwood Mac"></a>
|
||||
<a href="/music/c" title="Go Your Own Way"></a>
|
||||
<a href="/music/d" title="Fleetwood Mac"></a>`
|
||||
|
||||
title, total, err := extractLastFMPlaylistInfo(html)
|
||||
if err != nil {
|
||||
t.Fatalf("extractLastFMPlaylistInfo() error = %v", err)
|
||||
}
|
||||
if title != "Road & Rain" {
|
||||
t.Fatalf("title = %q, want %q", title, "Road & Rain")
|
||||
}
|
||||
if total != 2 {
|
||||
t.Fatalf("total = %d, want 2", total)
|
||||
}
|
||||
|
||||
pairs := extractLastFMTitleArtistPairs(html)
|
||||
if len(pairs) != 2 {
|
||||
t.Fatalf("pairs len = %d, want 2", len(pairs))
|
||||
}
|
||||
if pairs[0].Title != "Dreams" || pairs[0].Artist != "Fleetwood Mac" {
|
||||
t.Fatalf("unexpected first pair: %+v", pairs[0])
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user