129 lines
3.6 KiB
Go
129 lines
3.6 KiB
Go
package m3u
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
|
|
"applesync/internal/model"
|
|
)
|
|
|
|
func entry(path, artist, title string, ms int) Entry {
|
|
return Entry{
|
|
File: &model.FileEntry{Path: path},
|
|
Track: &model.Track{ArtistName: artist, Title: title, DurationMS: ms},
|
|
}
|
|
}
|
|
|
|
func TestReplaceRelative(t *testing.T) {
|
|
root := t.TempDir()
|
|
music := filepath.Join(root, "music", "Album")
|
|
if err := os.MkdirAll(music, 0o755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
file := filepath.Join(music, "01. Artist - One.flac")
|
|
if err := os.WriteFile(file, []byte("x"), 0o644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
m3uPath := filepath.Join(root, "playlists", "list.m3u")
|
|
if _, err := Write(m3uPath, []Entry{entry(file, "Artist", "One", 215000)}, "replace", "relative", true); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
data, err := os.ReadFile(m3uPath)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
want := "#EXTM3U\n#EXTINF:215,Artist - One\n../music/Album/01. Artist - One.flac\n"
|
|
if string(data) != want {
|
|
t.Fatalf("got:\n%s\nwant:\n%s", data, want)
|
|
}
|
|
info, err := os.Stat(m3uPath)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if info.Mode().Perm() != 0o644 {
|
|
t.Fatalf("mode = %v, want 0644", info.Mode().Perm())
|
|
}
|
|
}
|
|
|
|
func TestReplaceAbsolute(t *testing.T) {
|
|
root := t.TempDir()
|
|
file := filepath.Join(root, "a.flac")
|
|
if err := os.WriteFile(file, []byte("x"), 0o644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
m3uPath := filepath.Join(root, "list.m3u")
|
|
if _, err := Write(m3uPath, []Entry{entry(file, "A", "B", 1000)}, "replace", "absolute", false); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
data, _ := os.ReadFile(m3uPath)
|
|
if !strings.Contains(string(data), "#EXTM3U\n") || !strings.Contains(string(data), file) {
|
|
t.Fatalf("unexpected m3u:\n%s", data)
|
|
}
|
|
if strings.Contains(string(data), "#EXTINF") {
|
|
t.Fatalf("plain m3u must not contain EXTINF:\n%s", data)
|
|
}
|
|
}
|
|
|
|
func TestAppendDeduplicates(t *testing.T) {
|
|
root := t.TempDir()
|
|
one := filepath.Join(root, "one.flac")
|
|
two := filepath.Join(root, "two.flac")
|
|
_ = os.WriteFile(one, []byte("1"), 0o644)
|
|
_ = os.WriteFile(two, []byte("2"), 0o644)
|
|
m3uPath := filepath.Join(root, "list.m3u")
|
|
|
|
if _, err := Write(m3uPath, []Entry{entry(one, "A", "One", 1000)}, "replace", "relative", true); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
written, err := Write(m3uPath, []Entry{
|
|
entry(one, "A", "One", 1000),
|
|
entry(two, "B", "Two", 2000),
|
|
}, "append", "relative", true)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if written != 1 {
|
|
t.Fatalf("written = %d, want 1", written)
|
|
}
|
|
data, _ := os.ReadFile(m3uPath)
|
|
if strings.Count(string(data), "one.flac") != 1 {
|
|
t.Fatalf("duplicate one.flac:\n%s", data)
|
|
}
|
|
if !strings.Contains(string(data), "two.flac") {
|
|
t.Fatalf("two.flac missing:\n%s", data)
|
|
}
|
|
}
|
|
|
|
func TestAppendMissingFile(t *testing.T) {
|
|
root := t.TempDir()
|
|
file := filepath.Join(root, "one.flac")
|
|
_ = os.WriteFile(file, []byte("1"), 0o644)
|
|
m3uPath := filepath.Join(root, "new.m3u")
|
|
if _, err := Write(m3uPath, []Entry{entry(file, "A", "One", 1000)}, "append", "relative", true); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
data, _ := os.ReadFile(m3uPath)
|
|
if !strings.HasPrefix(string(data), "#EXTM3U\n") {
|
|
t.Fatalf("missing header:\n%s", data)
|
|
}
|
|
}
|
|
|
|
func TestAppendDedupesExistingPlainPaths(t *testing.T) {
|
|
root := t.TempDir()
|
|
file := filepath.Join(root, "one.flac")
|
|
_ = os.WriteFile(file, []byte("1"), 0o644)
|
|
m3uPath := filepath.Join(root, "list.m3u")
|
|
if err := os.WriteFile(m3uPath, []byte("one.flac\r\n"), 0o644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
written, err := Write(m3uPath, []Entry{entry(file, "A", "One", 1000)}, "append", "relative", true)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if written != 0 {
|
|
t.Fatalf("written = %d, want 0", written)
|
|
}
|
|
}
|