| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
package mpd |
| 6 |
|
| 7 |
import ( |
| 8 |
"os" |
| 9 |
"reflect" |
| 10 |
"testing" |
| 11 |
|
| 12 |
"github.com/fhs/gompd/v2/mpd/internal/server" |
| 13 |
) |
| 14 |
|
| 15 |
|
| 16 |
var quoteTests = &[...]*struct { |
| 17 |
source, expected string |
| 18 |
}{ |
| 19 |
{`test.ogg`, `"test.ogg"`}, |
| 20 |
{`test "song".ogg`, `"test \"song\".ogg"`}, |
| 21 |
{`test with 'single' and "double" quotes`, `"test with \'single\' and \"double\" quotes"`}, |
| 22 |
{`escape \"escaped\"`, `"escape \\\"escaped\\\""`}, |
| 23 |
{`just a \`, `"just a \\"`}, |
| 24 |
{`04 - ILL - DECAYED LOVE feat.℃iel.ogg`, `"04 - ILL - DECAYED LOVE feat.℃iel.ogg"`}, |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
{`(Artist == "foo\'bar\"")`, `"(Artist == \"foo\\\'bar\\\"\")"`}, |
| 29 |
} |
| 30 |
|
| 31 |
func TestQuote(t *testing.T) { |
| 32 |
|
| 33 |
for _, test := range quoteTests { |
| 34 |
if q := quote(test.source); q != test.expected { |
| 35 |
t.Errorf("quote(%s) returned %s; expected %s", test.source, q, test.expected) |
| 36 |
} |
| 37 |
} |
| 38 |
} |
| 39 |
|
| 40 |
var ( |
| 41 |
benchmarkQuoteInput = quoteTests[len(quoteTests)-1].source |
| 42 |
benchmarkQuoteOutput = "" |
| 43 |
) |
| 44 |
|
| 45 |
func BenchmarkQuote(b *testing.B) { |
| 46 |
for i := 0; i < b.N; i++ { |
| 47 |
benchmarkQuoteOutput = quote(benchmarkQuoteInput) |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
func TestQuoteArgs(t *testing.T) { |
| 52 |
quoteArgsTest := []string{`Artist`, `Nightingale`, `Title`, `"Don't Go Away"`} |
| 53 |
expected := `"Artist" "Nightingale" "Title" "\"Don\'t Go Away\""` |
| 54 |
if got := quoteArgs(quoteArgsTest); got != expected { |
| 55 |
t.Errorf("quoteArgs(%v) returned %s; expected %s", quoteArgsTest, got, expected) |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
var ( |
| 60 |
serverRunning = false |
| 61 |
useGoMPDServer = true |
| 62 |
) |
| 63 |
|
| 64 |
func localAddr() (net, addr string) { |
| 65 |
if useGoMPDServer { |
| 66 |
|
| 67 |
return "tcp", "127.0.0.1:6603" |
| 68 |
} |
| 69 |
net = "unix" |
| 70 |
addr = os.Getenv("MPD_HOST") |
| 71 |
if len(addr) > 0 && addr[0] == '/' { |
| 72 |
return |
| 73 |
} |
| 74 |
net = "tcp" |
| 75 |
if len(addr) == 0 { |
| 76 |
addr = "127.0.0.1" |
| 77 |
} |
| 78 |
port := os.Getenv("MPD_PORT") |
| 79 |
if len(port) == 0 { |
| 80 |
port = "6600" |
| 81 |
} |
| 82 |
return net, addr + ":" + port |
| 83 |
} |
| 84 |
|
| 85 |
func localDial(t *testing.T) *Client { |
| 86 |
net, addr := localAddr() |
| 87 |
if useGoMPDServer && !serverRunning { |
| 88 |
running := make(chan bool) |
| 89 |
go server.Listen(net, addr, running) |
| 90 |
serverRunning = true |
| 91 |
<-running |
| 92 |
} |
| 93 |
cli, err := Dial(net, addr) |
| 94 |
if err != nil { |
| 95 |
t.Fatalf("Dial(%q) = %v, %s want PTR, nil", addr, cli, err) |
| 96 |
} |
| 97 |
return cli |
| 98 |
} |
| 99 |
|
| 100 |
func teardown(cli *Client, t *testing.T) { |
| 101 |
if err := cli.Close(); err != nil { |
| 102 |
t.Errorf("Client.Close() = %s need nil", err) |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
func attrsEqual(left, right Attrs) bool { |
| 107 |
if len(left) != len(right) { |
| 108 |
return false |
| 109 |
} |
| 110 |
for key, lval := range left { |
| 111 |
if rval, ok := right[key]; !ok || lval != rval { |
| 112 |
return false |
| 113 |
} |
| 114 |
} |
| 115 |
return true |
| 116 |
} |
| 117 |
|
| 118 |
func TestPlaylistInfo(t *testing.T) { |
| 119 |
cli := localDial(t) |
| 120 |
defer teardown(cli, t) |
| 121 |
|
| 122 |
|
| 123 |
all := 4 |
| 124 |
files, err := cli.GetFiles() |
| 125 |
if err != nil { |
| 126 |
t.Fatalf("Client.GetFiles failed: %s\n", err) |
| 127 |
} |
| 128 |
if len(files) < all { |
| 129 |
t.Fatalf("Add more then %d audio file to your MPD to run this test.", all) |
| 130 |
} |
| 131 |
for i := 0; i < all; i++ { |
| 132 |
if err = cli.Add(files[i]); err != nil { |
| 133 |
t.Fatalf("Client.Add failed: %s\n", err) |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
pls, err := cli.PlaylistInfo(-1, -1) |
| 138 |
if err != nil { |
| 139 |
t.Fatalf("Client.PlaylistInfo(-1, -1) = %v, %s need _, nil", pls, err) |
| 140 |
} |
| 141 |
if len(pls) != all { |
| 142 |
t.Fatalf("Client.PlaylistInfo(-1, -1) len = %d need %d", len(pls), all) |
| 143 |
} |
| 144 |
for i, song := range pls { |
| 145 |
if _, ok := song["file"]; !ok { |
| 146 |
t.Errorf(`PlaylistInfo: song %d has no "file" attribute`, i) |
| 147 |
} |
| 148 |
pls1, err := cli.PlaylistInfo(i, -1) |
| 149 |
if err != nil { |
| 150 |
t.Errorf("Client.PlaylistInfo(%d, -1) = %v, %s need _, nil", i, pls1, err) |
| 151 |
} |
| 152 |
if !attrsEqual(pls[i], pls1[0]) { |
| 153 |
t.Errorf("song at position %d is %v; want %v", i, pls[i], pls1[0]) |
| 154 |
} |
| 155 |
} |
| 156 |
|
| 157 |
pls, err = cli.PlaylistInfo(2, 4) |
| 158 |
if err != nil { |
| 159 |
t.Fatalf("Client.PlaylistInfo(2, 4) = %v, %s need _, nil", pls, err) |
| 160 |
} |
| 161 |
if len(pls) != 2 { |
| 162 |
t.Fatalf("Client.PlaylistInfo(2, 4) len = %d need 2", len(pls)) |
| 163 |
} |
| 164 |
} |
| 165 |
|
| 166 |
func TestListInfo(t *testing.T) { |
| 167 |
cli := localDial(t) |
| 168 |
defer teardown(cli, t) |
| 169 |
|
| 170 |
fileCount, dirCount, plsCount := 0, 0, 0 |
| 171 |
|
| 172 |
ls, err := cli.ListInfo("foo") |
| 173 |
if err != nil { |
| 174 |
t.Fatalf(`Client.ListInfo("") = %v, %s need _, nil`, ls, err) |
| 175 |
} |
| 176 |
for i, item := range ls { |
| 177 |
if _, ok := item["file"]; ok { |
| 178 |
fileCount++ |
| 179 |
for _, field := range []string{"last-modified", "artist", "title", "track"} { |
| 180 |
if _, ok := item[field]; !ok { |
| 181 |
t.Errorf(`ListInfo: file item %d has no "%s" field`, i, field) |
| 182 |
} |
| 183 |
} |
| 184 |
} else if _, ok := item["directory"]; ok { |
| 185 |
dirCount++ |
| 186 |
} else if _, ok := item["playlist"]; ok { |
| 187 |
plsCount++ |
| 188 |
} else { |
| 189 |
t.Errorf("ListInfo: item %d has no file/directory/playlist attribute", i) |
| 190 |
} |
| 191 |
} |
| 192 |
|
| 193 |
if expected := 100; fileCount != expected { |
| 194 |
t.Errorf(`ListInfo: expected %d files, got %d`, expected, fileCount) |
| 195 |
} |
| 196 |
if expected := 2; dirCount != expected { |
| 197 |
t.Errorf(`ListInfo: expected %d directories, got %d`, expected, dirCount) |
| 198 |
} |
| 199 |
if expected := 1; plsCount != expected { |
| 200 |
t.Errorf(`ListInfo: expected %d playlists, got %d`, expected, plsCount) |
| 201 |
} |
| 202 |
} |
| 203 |
|
| 204 |
func TestSticker(t *testing.T) { |
| 205 |
testCases := []struct { |
| 206 |
Song, Name, Value string |
| 207 |
}{ |
| 208 |
{"song0000.ogg", "rating", "superb"}, |
| 209 |
{"song0000.ogg", "num_rating", "10"}, |
| 210 |
} |
| 211 |
|
| 212 |
cli := localDial(t) |
| 213 |
defer teardown(cli, t) |
| 214 |
|
| 215 |
t.Run("Set", func(t *testing.T) { |
| 216 |
for _, tc := range testCases { |
| 217 |
if err := cli.StickerSet(tc.Song, tc.Name, tc.Value); err != nil { |
| 218 |
t.Fatalf("Client.StickerSet of %v failed: %v", tc, err) |
| 219 |
} |
| 220 |
} |
| 221 |
}) |
| 222 |
t.Run("Get", func(t *testing.T) { |
| 223 |
for _, tc := range testCases { |
| 224 |
s, err := cli.StickerGet(tc.Song, tc.Name) |
| 225 |
if err != nil { |
| 226 |
t.Fatalf("Client.StickerGet of %v failed: %v", tc, err) |
| 227 |
} |
| 228 |
if s.Value != tc.Value { |
| 229 |
t.Errorf("Client.StickerGet of %v is %v; want %v", tc, s.Value, tc.Value) |
| 230 |
} |
| 231 |
} |
| 232 |
}) |
| 233 |
t.Run("List", func(t *testing.T) { |
| 234 |
stks, err := cli.StickerList(testCases[0].Song) |
| 235 |
if err != nil { |
| 236 |
t.Fatalf("Client.StickerList failed: %v", err) |
| 237 |
} |
| 238 |
if len(stks) != len(testCases) { |
| 239 |
t.Errorf("Client.StickerList returned %v stickers; want %v", len(stks), len(testCases)) |
| 240 |
} |
| 241 |
}) |
| 242 |
t.Run("Find", func(t *testing.T) { |
| 243 |
for _, tc := range testCases { |
| 244 |
files, stks, err := cli.StickerFind("", tc.Name) |
| 245 |
if err != nil { |
| 246 |
t.Fatalf("Client.StickerFind(%q) failed: %v", tc.Name, err) |
| 247 |
} |
| 248 |
if len(files) != len(stks) { |
| 249 |
t.Errorf("Client.StickerFind(%q) returned %v files and %v stickers", tc.Name, len(files), len(stks)) |
| 250 |
} |
| 251 |
if len(files) != 1 { |
| 252 |
t.Errorf("Client.StickerFind(%q) returned %v file; need 1", tc.Name, len(files)) |
| 253 |
} |
| 254 |
} |
| 255 |
}) |
| 256 |
t.Run("Delete", func(t *testing.T) { |
| 257 |
for i, tc := range testCases { |
| 258 |
if err := cli.StickerDelete(tc.Song, tc.Name); err != nil { |
| 259 |
t.Fatalf("Client.StickerDelete of %v failed: %v", tc, err) |
| 260 |
} |
| 261 |
stks, err := cli.StickerList(testCases[0].Song) |
| 262 |
if err != nil { |
| 263 |
t.Fatalf("Client.StickerList failed: %v", err) |
| 264 |
} |
| 265 |
if len(stks) != len(testCases)-i-1 { |
| 266 |
t.Fatalf("Client.StickerList returned %v stickers; want %v", len(stks), len(testCases)-i-1) |
| 267 |
} |
| 268 |
} |
| 269 |
}) |
| 270 |
} |
| 271 |
|
| 272 |
func TestCurrentSong(t *testing.T) { |
| 273 |
cli := localDial(t) |
| 274 |
defer teardown(cli, t) |
| 275 |
|
| 276 |
attrs, err := cli.CurrentSong() |
| 277 |
if err != nil { |
| 278 |
t.Fatalf("Client.CurrentSong() = %v, %s need _, nil", attrs, err) |
| 279 |
} |
| 280 |
if len(attrs) == 0 { |
| 281 |
return |
| 282 |
} |
| 283 |
if _, ok := attrs["file"]; !ok { |
| 284 |
t.Fatalf("current song (attrs=%v) has no file attribute", attrs) |
| 285 |
} |
| 286 |
} |
| 287 |
|
| 288 |
func TestReadComments(t *testing.T) { |
| 289 |
cli := localDial(t) |
| 290 |
defer teardown(cli, t) |
| 291 |
|
| 292 |
attrs, err := cli.ReadComments("foo.mp3") |
| 293 |
if err != nil { |
| 294 |
t.Fatalf(`Client.ReadComments("foo.mp3") = %v, %s need _, nil`, attrs, err) |
| 295 |
} |
| 296 |
if _, ok := attrs["TITLE"]; !ok { |
| 297 |
t.Fatalf("comments (attrs=%v) has no ARTIST attribute", attrs) |
| 298 |
} |
| 299 |
} |
| 300 |
|
| 301 |
func TestVersion(t *testing.T) { |
| 302 |
cli := localDial(t) |
| 303 |
defer teardown(cli, t) |
| 304 |
|
| 305 |
if cli.Version() != "gompd0.1" { |
| 306 |
t.Errorf("Client.Version failed: %s != gompd0.1", cli.Version()) |
| 307 |
} |
| 308 |
} |
| 309 |
|
| 310 |
func TestPing(t *testing.T) { |
| 311 |
cli := localDial(t) |
| 312 |
defer teardown(cli, t) |
| 313 |
|
| 314 |
if err := cli.Ping(); err != nil { |
| 315 |
t.Errorf("Client.Ping failed: %s", err) |
| 316 |
} |
| 317 |
} |
| 318 |
|
| 319 |
func TestUpdate(t *testing.T) { |
| 320 |
cli := localDial(t) |
| 321 |
defer teardown(cli, t) |
| 322 |
|
| 323 |
id, err := cli.Update("foo") |
| 324 |
if err != nil { |
| 325 |
t.Fatalf("Client.Update failed: %s\n", err) |
| 326 |
} |
| 327 |
if id < 1 { |
| 328 |
t.Errorf("job id is too small: %d", id) |
| 329 |
} |
| 330 |
} |
| 331 |
|
| 332 |
func TestRescan(t *testing.T) { |
| 333 |
cli := localDial(t) |
| 334 |
defer teardown(cli, t) |
| 335 |
|
| 336 |
id, err := cli.Rescan("foo") |
| 337 |
if err != nil { |
| 338 |
t.Fatalf("Client.Rescan failed: %s\n", err) |
| 339 |
} |
| 340 |
if id < 1 { |
| 341 |
t.Errorf("job id is too small: %d", id) |
| 342 |
} |
| 343 |
} |
| 344 |
|
| 345 |
func TestListOutputs(t *testing.T) { |
| 346 |
cli := localDial(t) |
| 347 |
defer teardown(cli, t) |
| 348 |
|
| 349 |
outputs, err := cli.ListOutputs() |
| 350 |
if err != nil { |
| 351 |
t.Fatalf(`Client.ListOutputs() = %v, %s need _, nil`, outputs, err) |
| 352 |
} |
| 353 |
expected := []Attrs{{ |
| 354 |
"outputid": "0", |
| 355 |
"outputname": "downstairs", |
| 356 |
"outputenabled": "1", |
| 357 |
}, { |
| 358 |
"outputid": "1", |
| 359 |
"outputname": "upstairs", |
| 360 |
"outputenabled": "0", |
| 361 |
}} |
| 362 |
if len(outputs) != 2 { |
| 363 |
t.Errorf(`Listed %d outputs, expected %d`, len(outputs), 2) |
| 364 |
} |
| 365 |
for i, o := range outputs { |
| 366 |
if len(o) != 3 { |
| 367 |
t.Errorf(`Output should contain 3 keys, got %d`, len(o)) |
| 368 |
} |
| 369 |
for k, v := range expected[i] { |
| 370 |
if outputs[i][k] != v { |
| 371 |
t.Errorf(`Expected property %s for key "%s", got %s`, v, k, outputs[i][k]) |
| 372 |
} |
| 373 |
} |
| 374 |
} |
| 375 |
} |
| 376 |
|
| 377 |
func TestEnableOutput(t *testing.T) { |
| 378 |
cli := localDial(t) |
| 379 |
defer teardown(cli, t) |
| 380 |
|
| 381 |
if err := cli.EnableOutput(1); err != nil { |
| 382 |
t.Fatalf("Client.EnableOutput failed: %s\n", err) |
| 383 |
} |
| 384 |
} |
| 385 |
|
| 386 |
func TestDisableOutput(t *testing.T) { |
| 387 |
cli := localDial(t) |
| 388 |
defer teardown(cli, t) |
| 389 |
|
| 390 |
if err := cli.DisableOutput(1); err != nil { |
| 391 |
t.Fatalf("Client.DisableOutput failed: %s\n", err) |
| 392 |
} |
| 393 |
} |
| 394 |
|
| 395 |
func TestPlaylistFunctions(t *testing.T) { |
| 396 |
cli := localDial(t) |
| 397 |
defer teardown(cli, t) |
| 398 |
|
| 399 |
files, err := cli.GetFiles() |
| 400 |
if err != nil { |
| 401 |
t.Fatalf("Client.GetFiles failed: %s\n", err) |
| 402 |
} |
| 403 |
if len(files) < 2 { |
| 404 |
t.Log("Add more then 1 audio file to your MPD to run this test.") |
| 405 |
return |
| 406 |
} |
| 407 |
for i := 0; i < 2; i++ { |
| 408 |
if err = cli.PlaylistAdd("Test Playlist", files[i]); err != nil { |
| 409 |
t.Fatalf("Client.PlaylistAdd failed: %s\n", err) |
| 410 |
} |
| 411 |
} |
| 412 |
attrs, err := cli.ListPlaylists() |
| 413 |
if err != nil { |
| 414 |
t.Fatalf("Client.ListPlaylists failed: %s\n", err) |
| 415 |
} |
| 416 |
if i := attrsListIndex(attrs, "playlist", "Test Playlist"); i < 0 { |
| 417 |
t.Fatalf("Couldn't find playlist \"Test Playlist\" in %v\n", attrs) |
| 418 |
} |
| 419 |
attrs, err = cli.PlaylistContents("Test Playlist") |
| 420 |
if err != nil { |
| 421 |
t.Fatalf("Client.PlaylistContents failed: %s\n", err) |
| 422 |
} |
| 423 |
if i := attrsListIndex(attrs, "file", files[0]); i < 0 { |
| 424 |
t.Fatalf("Couldn't find song %q in %v", files[0], attrs) |
| 425 |
} |
| 426 |
if err = cli.PlaylistDelete("Test Playlist", 0); err != nil { |
| 427 |
t.Fatalf("Client.PlaylistDelete failed: %s\n", err) |
| 428 |
} |
| 429 |
playlist, err := cli.PlaylistContents("Test Playlist") |
| 430 |
if err != nil { |
| 431 |
t.Fatalf("Client.PlaylistContents failed: %s\n", err) |
| 432 |
} |
| 433 |
if !attrsListEqual(playlist, attrs[1:]) { |
| 434 |
t.Fatalf("PlaylistContents returned %v; want %v", playlist, attrs[1:]) |
| 435 |
} |
| 436 |
cli.PlaylistRemove("Test Playlist 2") |
| 437 |
if err = cli.PlaylistRename("Test Playlist", "Test Playlist 2"); err != nil { |
| 438 |
t.Fatalf("Client.PlaylistRename failed: %s\n", err) |
| 439 |
} |
| 440 |
if err = cli.Clear(); err != nil { |
| 441 |
t.Fatalf("Client.Clear failed: %s\n", err) |
| 442 |
} |
| 443 |
if err = cli.PlaylistLoad("Test Playlist 2", -1, -1); err != nil { |
| 444 |
t.Fatalf("Client.Load failed: %s\n", err) |
| 445 |
} |
| 446 |
attrs, err = cli.PlaylistInfo(-1, -1) |
| 447 |
if err != nil { |
| 448 |
t.Fatalf("Client.PlaylistInfo failed: %s\n", err) |
| 449 |
} |
| 450 |
if !attrsListEqualKey(playlist, attrs, "file") { |
| 451 |
t.Fatalf("Unexpected playlist: %v != %v\n", attrs, playlist) |
| 452 |
} |
| 453 |
if err = cli.PlaylistClear("Test Playlist 2"); err != nil { |
| 454 |
t.Fatalf("Client.PlaylistClear failed: %s\n", err) |
| 455 |
} |
| 456 |
attrs, err = cli.PlaylistContents("Test Playlist 2") |
| 457 |
if err != nil { |
| 458 |
t.Fatalf("Client.PlaylistContents failed: %s\n", err) |
| 459 |
} |
| 460 |
if len(attrs) != 0 { |
| 461 |
t.Fatalf("Unexpected number of songs: %d != 0\n", len(attrs)) |
| 462 |
} |
| 463 |
if err = cli.PlaylistRemove("Test Playlist 2"); err != nil { |
| 464 |
t.Fatalf("Client.PlaylistRemove failed: %s\n", err) |
| 465 |
} |
| 466 |
attrs, err = cli.ListPlaylists() |
| 467 |
if err != nil { |
| 468 |
t.Fatalf("Client.ListPlaylists failed: %s\n", err) |
| 469 |
} |
| 470 |
if i := attrsListIndex(attrs, "playlist", "Test Playlist 2"); i > -1 { |
| 471 |
t.Fatalf("Found playlist \"Test Playlist 2\" in %v\n", attrs) |
| 472 |
} |
| 473 |
if err = cli.PlaylistSave("Test Playlist"); err != nil { |
| 474 |
t.Fatalf("Client.PlaylistSave failed: %s\n", err) |
| 475 |
} |
| 476 |
attrs, err = cli.PlaylistContents("Test Playlist") |
| 477 |
if err != nil { |
| 478 |
t.Fatalf("Client.PlaylistContents failed: %s\n", err) |
| 479 |
} |
| 480 |
if !attrsListEqual(playlist, attrs) { |
| 481 |
t.Fatalf("Unexpected playlist: %v != %v\n", attrs, playlist) |
| 482 |
} |
| 483 |
} |
| 484 |
|
| 485 |
func attrsListIndex(attrs []Attrs, key, value string) int { |
| 486 |
for i, attr := range attrs { |
| 487 |
if attr[key] == value { |
| 488 |
return i |
| 489 |
} |
| 490 |
} |
| 491 |
return -1 |
| 492 |
} |
| 493 |
|
| 494 |
func attrsListEqual(a, b []Attrs) bool { |
| 495 |
if len(a) != len(b) { |
| 496 |
return false |
| 497 |
} |
| 498 |
for i := range a { |
| 499 |
if !attrsEqual(a[i], b[i]) { |
| 500 |
return false |
| 501 |
} |
| 502 |
} |
| 503 |
return true |
| 504 |
} |
| 505 |
|
| 506 |
func attrsListEqualKey(a, b []Attrs, key string) bool { |
| 507 |
if len(a) != len(b) { |
| 508 |
return false |
| 509 |
} |
| 510 |
for i := range a { |
| 511 |
if a[i][key] != b[i][key] { |
| 512 |
return false |
| 513 |
} |
| 514 |
} |
| 515 |
return true |
| 516 |
} |
| 517 |
|
| 518 |
func TestPriority(t *testing.T) { |
| 519 |
cli := localDial(t) |
| 520 |
defer teardown(cli, t) |
| 521 |
for _, tc := range []struct { |
| 522 |
priority, start, end int |
| 523 |
ok bool |
| 524 |
}{ |
| 525 |
{255, 1, 1, true}, |
| 526 |
{255, 1, 1, true}, |
| 527 |
{256, 1, -1, false}, |
| 528 |
{-1, 1, 1, false}, |
| 529 |
} { |
| 530 |
|
| 531 |
err := cli.SetPriority(tc.priority, tc.start, tc.end) |
| 532 |
if err != nil && tc.ok { |
| 533 |
t.Errorf("Client.SetPriority failed: %s", err) |
| 534 |
} |
| 535 |
if err == nil && !tc.ok { |
| 536 |
t.Errorf("Client.SetPriority succeeded when it should fail") |
| 537 |
} |
| 538 |
} |
| 539 |
} |
| 540 |
|
| 541 |
func TestPriorityID(t *testing.T) { |
| 542 |
cli := localDial(t) |
| 543 |
defer teardown(cli, t) |
| 544 |
for _, tc := range []struct { |
| 545 |
priority, id int |
| 546 |
ok bool |
| 547 |
}{ |
| 548 |
{255, 1, true}, |
| 549 |
{255, 1, true}, |
| 550 |
{256, 1, false}, |
| 551 |
{-1, 1, false}, |
| 552 |
{1, -1, false}, |
| 553 |
} { |
| 554 |
|
| 555 |
err := cli.SetPriorityID(tc.priority, tc.id) |
| 556 |
if err != nil && tc.ok { |
| 557 |
t.Errorf("Client.SetPriorityID failed: %s", err) |
| 558 |
} |
| 559 |
if err == nil && !tc.ok { |
| 560 |
t.Errorf("Client.SetPriorityID succeeded when it should fail") |
| 561 |
} |
| 562 |
} |
| 563 |
} |
| 564 |
|
| 565 |
|
| 566 |
ures |
| 567 |
|
| 568 |
|
| 569 |
|
| 570 |
func TestAddIDAndDeleteID(t *testing.T) { |
| 571 |
cli := localDial(t) |
| 572 |
defer teardown(cli, t) |
| 573 |
|
| 574 |
id1, err := cli.AddID("song0042.ogg", -1) |
| 575 |
if err != nil { |
| 576 |
t.Fatalf("Client.AddID failed: %s\n", err) |
| 577 |
} |
| 578 |
id2, err := cli.AddID("song0042.ogg", -1) |
| 579 |
if err != nil { |
| 580 |
t.Fatalf("Client.AddID failed: %s\n", err) |
| 581 |
} |
| 582 |
if id1 == id2 { |
| 583 |
t.Fatalf("Client.AddID returned the same ID twice\n") |
| 584 |
} |
| 585 |
|
| 586 |
if err := cli.DeleteID(id1); err != nil { |
| 587 |
t.Fatalf("Client.DeleteID failed: %s\n", err) |
| 588 |
} |
| 589 |
err = cli.DeleteID(id1) |
| 590 |
if err == nil { |
| 591 |
t.Fatalf("Client.DeleteID did not fail on second delete of an ID\n") |
| 592 |
} |
| 593 |
mpdErr, ok := err.(Error) |
| 594 |
if !ok { |
| 595 |
t.Fatalf("Client.DeleteID did not fail with an mpd.Error\n") |
| 596 |
} |
| 597 |
if mpdErr.Code != ErrorNoExist { |
| 598 |
t.Fatalf("Unexpected error code: expected %d, got %d\n", ErrorNoExist, mpdErr.Code) |
| 599 |
} |
| 600 |
if err := cli.DeleteID(id2); err != nil { |
| 601 |
t.Fatalf("Client.DeleteID failed: %s\n", err) |
| 602 |
} |
| 603 |
} |
| 604 |
|
| 605 |
func TestResponseErrorHandling(t *testing.T) { |
| 606 |
cli := localDial(t) |
| 607 |
defer teardown(cli, t) |
| 608 |
|
| 609 |
for name, fn := range map[string]func() error{ |
| 610 |
ment |
| 611 |
"in readList": func() error { _, err := cli.Command("list").Strings("file"); return err }, |
| 612 |
"in readAttrsList": func() error { _, err := cli.PlaylistContents("does_not_exist"); return err }, |
| 613 |
"in readAttrs": func() error { _, err := cli.ReadComments(""); return err }, |
| 614 |
"in readOKLine": func() error { return cli.DeleteID(123) }, |
| 615 |
"in Update": func() error { _, err := cli.Update(""); return err }, |
| 616 |
"in ListAllInfo": func() error { _, err := cli.ListAllInfo(""); return err }, |
| 617 |
"in ListInfo": func() error { _, err := cli.ListInfo(""); return err }, |
| 618 |
"in List": func() error { _, err := cli.List(""); return err }, |
| 619 |
} { |
| 620 |
t.Run(name, func(t *testing.T) { |
| 621 |
if err := fn(); err == nil { |
| 622 |
t.Errorf("did not fail on MPD error response") |
| 623 |
} else if _, ok := err.(Error); !ok { |
| 624 |
t.Errorf("did not fail with an mpd.Error") |
| 625 |
} |
| 626 |
}) |
| 627 |
} |
| 628 |
} |
| 629 |
|
| 630 |
func TestAlbumArt(t *testing.T) { |
| 631 |
cli := localDial(t) |
| 632 |
defer teardown(cli, t) |
| 633 |
tests := []struct { |
| 634 |
name string |
| 635 |
uri string |
| 636 |
want []byte |
| 637 |
wantErr bool |
| 638 |
}{ |
| 639 |
{"artwork as a whole", "/file/with/small-artwork", []byte{0x01, 0x02, 0x03, 0x04, 0x05}, false}, |
| 640 |
{"artwork in chunks", "/file/with/huge-artwork", []byte{0x01, 0x02, 0x03, 0x04, 0x05}, false}, |
| 641 |
{"nonexistent artwork", "some_wrong_file", nil, true}, |
| 642 |
} |
| 643 |
for _, tt := range tests { |
| 644 |
t.Run(tt.name, func(t *testing.T) { |
| 645 |
got, err := cli.AlbumArt(tt.uri) |
| 646 |
if (err != nil) != tt.wantErr { |
| 647 |
t.Errorf("AlbumArt() error = %v, wantErr %v", err, tt.wantErr) |
| 648 |
} else if !reflect.DeepEqual(got, tt.want) { |
| 649 |
t.Errorf("AlbumArt() got = %v, want %v", got, tt.want) |
| 650 |
} |
| 651 |
}) |
| 652 |
} |
| 653 |
} |
| 654 |
|
| 655 |
func TestReadPicture(t *testing.T) { |
| 656 |
cli := localDial(t) |
| 657 |
defer teardown(cli, t) |
| 658 |
tests := []struct { |
| 659 |
name string |
| 660 |
uri string |
| 661 |
want []byte |
| 662 |
wantErr bool |
| 663 |
}{ |
| 664 |
{"artwork as a whole", "/file/with/small-artwork", []byte{0x01, 0x02, 0x03, 0x04, 0x05}, false}, |
| 665 |
{"artwork in chunks", "/file/with/huge-artwork", []byte{0x01, 0x02, 0x03, 0x04, 0x05}, false}, |
| 666 |
{"nonexistent artwork", "some_wrong_file", nil, true}, |
| 667 |
} |
| 668 |
for _, tt := range tests { |
| 669 |
t.Run(tt.name, func(t *testing.T) { |
| 670 |
got, err := cli.ReadPicture(tt.uri) |
| 671 |
if (err != nil) != tt.wantErr { |
| 672 |
t.Errorf("ReadPicture() error = %v, wantErr %v", err, tt.wantErr) |
| 673 |
} else if !reflect.DeepEqual(got, tt.want) { |
| 674 |
t.Errorf("ReadPicture() got = %v, want %v", got, tt.want) |
| 675 |
} |
| 676 |
}) |
| 677 |
} |
| 678 |
} |