73 lines
1.7 KiB
Go
73 lines
1.7 KiB
Go
package rip
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestDownloadAlbumsBuildsBatchAndArgs(t *testing.T) {
|
|
dir := t.TempDir()
|
|
script := filepath.Join(dir, "fake-rip")
|
|
content := `#!/bin/sh
|
|
printf '%s\n' "$@" > "$0.args"
|
|
prev=""
|
|
for a in "$@"; do
|
|
if [ "$prev" = "file" ]; then cp "$a" "$0.batch"; fi
|
|
prev="$a"
|
|
done
|
|
`
|
|
if err := os.WriteFile(script, []byte(content), 0o755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
runner := &Runner{
|
|
Binary: script,
|
|
Quality: 3,
|
|
Folder: "/tmp/music",
|
|
ExtraArgs: []string{"--no-ssl-verify"},
|
|
NoProgress: true,
|
|
StateDir: dir,
|
|
}
|
|
if err := runner.DownloadAlbums(context.Background(), []string{"id1", "id2", "id1"}, false); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
args, err := os.ReadFile(script + ".args")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
argList := strings.Split(strings.TrimSpace(string(args)), "\n")
|
|
wantArgs := []string{"--no-progress", "-q", "3", "-f", "/tmp/music", "--no-ssl-verify", "file"}
|
|
for i, want := range wantArgs {
|
|
if i >= len(argList) || argList[i] != want {
|
|
t.Fatalf("args = %#v, want prefix %#v", argList, wantArgs)
|
|
}
|
|
}
|
|
|
|
data, err := os.ReadFile(script + ".batch")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var items []batchItem
|
|
if err := json.Unmarshal(data, &items); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(items) != 2 || items[0].ID != "id1" || items[1].ID != "id2" {
|
|
t.Fatalf("unexpected batch: %+v", items)
|
|
}
|
|
if items[0].Source != "qobuz" || items[0].MediaType != "album" {
|
|
t.Fatalf("unexpected item: %+v", items[0])
|
|
}
|
|
}
|
|
|
|
func TestUniqueIDs(t *testing.T) {
|
|
got := uniqueIDs([]string{"a", "b", "a", " ", "c"})
|
|
if len(got) != 3 || got[0] != "a" || got[2] != "c" {
|
|
t.Fatalf("uniqueIDs = %#v", got)
|
|
}
|
|
}
|