diff options
Diffstat (limited to '')
| -rw-r--r-- | tests/queue.rs | 171 |
1 files changed, 171 insertions, 0 deletions
diff --git a/tests/queue.rs b/tests/queue.rs index bff83ca..e511612 100644 --- a/tests/queue.rs +++ b/tests/queue.rs | |||
| @@ -457,3 +457,174 @@ fn test_track_no_metadata_fields() { | |||
| 457 | assert!(t.artist.is_none()); | 457 | assert!(t.artist.is_none()); |
| 458 | assert!(t.album.is_none()); | 458 | assert!(t.album.is_none()); |
| 459 | } | 459 | } |
| 460 | |||
| 461 | // ---------- Shuffle restore ---------- | ||
| 462 | |||
| 463 | #[test] | ||
| 464 | fn test_shuffle_all_then_none_keeps_played_tracks_in_place() { | ||
| 465 | // User's scenario: | ||
| 466 | // Queue after shuffle all: 01, 02, 03, 04(playing), 05, 06... | ||
| 467 | // Skip to track 04, then shuffle none | ||
| 468 | // Expected: played tracks (01,02,03) stay in place, current (04) stays, | ||
| 469 | // remaining tracks from original order after position | ||
| 470 | |||
| 471 | let mut q = Queue::new(); | ||
| 472 | // Add tracks A, B, C, D, E, F (A is at index 0) | ||
| 473 | add_track(&mut q, "/music/A.mp3"); | ||
| 474 | add_track(&mut q, "/music/B.mp3"); | ||
| 475 | add_track(&mut q, "/music/C.mp3"); | ||
| 476 | add_track(&mut q, "/music/D.mp3"); | ||
| 477 | add_track(&mut q, "/music/E.mp3"); | ||
| 478 | add_track(&mut q, "/music/F.mp3"); | ||
| 479 | |||
| 480 | // Shuffle all - should save original and shuffle | ||
| 481 | q.set_shuffle(ShuffleMode::All); | ||
| 482 | |||
| 483 | // Simulate playing: advance position a few times | ||
| 484 | // After playing A, B, C, we're at position 2 (track C) | ||
| 485 | // Let's say shuffled order puts D at position 2 | ||
| 486 | q.next(); // position 1 (playing second track) | ||
| 487 | q.next(); // position 2 (playing third track) | ||
| 488 | q.next(); // position 3 (playing fourth track) | ||
| 489 | |||
| 490 | let pos_before_restore = q.position(); | ||
| 491 | |||
| 492 | // Now shuffle none - should restore | ||
| 493 | q.set_shuffle(ShuffleMode::None); | ||
| 494 | |||
| 495 | // After restore: | ||
| 496 | // - position should be same as before | ||
| 497 | // - tracks before current position should remain in same order as when playing | ||
| 498 | // - tracks after current should be from original order that haven't been played | ||
| 499 | |||
| 500 | let current = q.current().unwrap(); | ||
| 501 | println!("Position after restore: {}", q.position()); | ||
| 502 | println!("Current track: {:?}", current.path); | ||
| 503 | |||
| 504 | // The position should stay the same | ||
| 505 | assert_eq!(q.position(), pos_before_restore); | ||
| 506 | |||
| 507 | // Tracks should be: [played tracks in played order...] [current] [unplayed from original] | ||
| 508 | // Let's verify by checking that tracks before current include the ones that were played | ||
| 509 | for i in 0..q.position() { | ||
| 510 | println!("Track at {}: {:?}", i, q.track_at(i).unwrap().path); | ||
| 511 | } | ||
| 512 | } | ||
| 513 | |||
| 514 | #[test] | ||
| 515 | fn test_shuffle_all_then_none_exact_scenario() { | ||
| 516 | // Exact scenario from user: | ||
| 517 | // Original queue: 1, 2, 3, 4, 5, 6 (position 0 = track 1) | ||
| 518 | // shuffle all -> 1, 2(playing), 4, 5, 6, 3 | ||
| 519 | // play 2 more -> 1, 2, 4, 5(playing), 6, 3 | ||
| 520 | // shuffle none -> 1, 2, 4, 5(playing), 3, 6 | ||
| 521 | |||
| 522 | let mut q = Queue::new(); | ||
| 523 | add_track(&mut q, "/music/1.mp3"); | ||
| 524 | add_track(&mut q, "/music/2.mp3"); | ||
| 525 | add_track(&mut q, "/music/3.mp3"); | ||
| 526 | add_track(&mut q, "/music/4.mp3"); | ||
| 527 | add_track(&mut q, "/music/5.mp3"); | ||
| 528 | add_track(&mut q, "/music/6.mp3"); | ||
| 529 | |||
| 530 | // Original order: [1, 2, 3, 4, 5, 6] | ||
| 531 | // Position 0 = track 1 | ||
| 532 | |||
| 533 | // Shuffle all | ||
| 534 | q.set_shuffle(ShuffleMode::All); | ||
| 535 | |||
| 536 | // Assume after shuffle and navigating to position 3 (playing track 5), | ||
| 537 | // shuffled order is [1, 2, 4, 5, 6, 3] | ||
| 538 | // We need to set this up manually since shuffle is random | ||
| 539 | |||
| 540 | // Let's simulate by setting position to 3 (after playing 1, 2, 4, now playing 5) | ||
| 541 | // Position 3 means we've played indices 0,1,2 in shuffled order = tracks 1,2,4 | ||
| 542 | // Currently at position 3 = track 5 | ||
| 543 | |||
| 544 | // First get the shuffled indices and manually set up a specific order | ||
| 545 | let indices: Vec<usize> = q.shuffled_indices().to_vec(); | ||
| 546 | println!("Shuffled indices: {:?}", indices); | ||
| 547 | |||
| 548 | // Force position to 3 to simulate playing 3 tracks then current is track at pos 3 | ||
| 549 | q.set_position(3); | ||
| 550 | |||
| 551 | let current_before = q.current().unwrap().path.clone(); | ||
| 552 | println!("Current before restore: {:?}", current_before.file_name().unwrap()); | ||
| 553 | println!("Position: {}", q.position()); | ||
| 554 | |||
| 555 | // Now shuffle none | ||
| 556 | q.set_shuffle(ShuffleMode::None); | ||
| 557 | |||
| 558 | println!("After restore:"); | ||
| 559 | for i in 0..q.len() { | ||
| 560 | println!(" {}: {:?}", i+1, q.track_at(i).unwrap().path.file_name().unwrap()); | ||
| 561 | } | ||
| 562 | |||
| 563 | // After restore: | ||
| 564 | // - Position should still be 3 | ||
| 565 | // - Tracks at positions 0,1,2,3 should be the played tracks in their order (1,2,4,5) | ||
| 566 | // - Tracks 4,5 should be from original unplayed (3,6) | ||
| 567 | assert_eq!(q.position(), 3, "position should stay same"); | ||
| 568 | |||
| 569 | let current_after = q.current().unwrap().path.clone(); | ||
| 570 | assert_eq!(current_before, current_after, "current track should stay same"); | ||
| 571 | } | ||
| 572 | |||
| 573 | #[test] | ||
| 574 | fn test_shuffle_restore_simulation() { | ||
| 575 | // Simulate exact user scenario: | ||
| 576 | // Queue: 1,2,3,4,5,6 (original) | ||
| 577 | // shuffle all -> shuffled order e.g., [4,2,6,1,5,3] | ||
| 578 | // Navigate 4 times (play 1,2,4,5 -> position 4, now playing 5) | ||
| 579 | // Expected after shuffle none: 4,2,6,1,5,3 (played in place, unplayed from original) | ||
| 580 | |||
| 581 | let mut q = Queue::new(); | ||
| 582 | for i in 1..=6 { | ||
| 583 | add_track(&mut q, &format!("/music/{}.mp3", i)); | ||
| 584 | } | ||
| 585 | |||
| 586 | // Shuffle all (saves original as [1,2,3,4,5,6]) | ||
| 587 | q.set_shuffle(ShuffleMode::All); | ||
| 588 | |||
| 589 | // Let's say shuffled order is [4, 2, 6, 1, 5, 3] | ||
| 590 | // We can force this by inspecting the indices | ||
| 591 | println!("Initial shuffled: {:?}", q.shuffled_indices()); | ||
| 592 | |||
| 593 | // Navigate 4 times | ||
| 594 | for _ in 0..4 { | ||
| 595 | q.next(); | ||
| 596 | } | ||
| 597 | |||
| 598 | println!("After 4 next() calls:"); | ||
| 599 | println!(" Position: {}", q.position()); | ||
| 600 | println!(" Current: {:?}", q.current().unwrap().path.file_name().unwrap()); | ||
| 601 | println!(" Tracks:"); | ||
| 602 | for i in 0..q.len() { | ||
| 603 | println!(" {}: {:?}", i, q.track_at(i).unwrap().path.file_name().unwrap()); | ||
| 604 | } | ||
| 605 | |||
| 606 | let current_path = q.current().unwrap().path.clone(); | ||
| 607 | let pos_before = q.position(); | ||
| 608 | |||
| 609 | // Shuffle none | ||
| 610 | q.set_shuffle(ShuffleMode::None); | ||
| 611 | |||
| 612 | println!("After shuffle none:"); | ||
| 613 | println!(" Position: {}", q.position()); | ||
| 614 | println!(" Current: {:?}", q.current().unwrap().path.file_name().unwrap()); | ||
| 615 | println!(" Tracks:"); | ||
| 616 | for i in 0..q.len() { | ||
| 617 | println!(" {}: {:?}", i, q.track_at(i).unwrap().path.file_name().unwrap()); | ||
| 618 | } | ||
| 619 | |||
| 620 | // Assertions | ||
| 621 | assert_eq!(q.position(), pos_before, "position should be preserved"); | ||
| 622 | assert_eq!(q.current().unwrap().path, current_path, "current track should be preserved"); | ||
| 623 | |||
| 624 | // The key requirement: tracks before current position should remain in played order | ||
| 625 | // and tracks after should be remaining from original in original order | ||
| 626 | let pos = q.position(); | ||
| 627 | for i in 0..pos { | ||
| 628 | println!("Played track {}: {:?}", i, q.track_at(i).unwrap().path.file_name().unwrap()); | ||
| 629 | } | ||
| 630 | } | ||
