using System; using System.Collections.Generic; using Xunit; using Moq; using FluentAssertions; using SpotifyAPI.Web; using System.Threading; using System.Threading.Tasks; using Xunit.Sdk; namespace Selector.Tests { public class PlaylistWatcherTests { public static IEnumerable CurrentPlaylistData => new List { new object[] { new List(){ Helper.FullPlaylist("playlist1"), Helper.FullPlaylist("playlist1"), Helper.FullPlaylist("playlist1"), Helper.FullPlaylist("playlist1") } } }; [Theory] [MemberData(nameof(CurrentPlaylistData))] public async void CurrentPlaylist(List playing) { var playlistDequeue = new Queue(playing); var spotMock = new Mock(); spotMock.Setup(s => s.Playlists.Get(It.IsAny()).Result).Returns(playlistDequeue.Dequeue); var config = new PlaylistWatcherConfig() { PlaylistId = "spotify:playlist:test" }; var watcher = new PlaylistWatcher(config, spotMock.Object); for (var i = 0; i < playing.Count; i++) { await watcher.WatchOne(); watcher.Live.Should().Be(playing[i]); } } public static IEnumerable EventsData => new List { // NO CHANGING new object[] { new List(){ Helper.FullPlaylist("Playlist", snapshotId: "snapshot1"), Helper.FullPlaylist("Playlist", snapshotId: "snapshot1"), Helper.FullPlaylist("Playlist", snapshotId: "snapshot1"), }, // to raise new List(){ }, // to not raise new List(){ "SnapshotChange" } }, // CHANGING SNAPSHOT new object[] { new List(){ Helper.FullPlaylist("Playlist", snapshotId: "snapshot1"), Helper.FullPlaylist("Playlist", snapshotId: "snapshot2"), Helper.FullPlaylist("Playlist", snapshotId: "snapshot2"), }, // to raise new List(){ "SnapshotChange" }, // to not raise new List(){ } } }; [Theory] [MemberData(nameof(EventsData))] public async void Events(List playing, List toRaise, List toNotRaise) { var playlistDequeue = new Queue(playing); var spotMock = new Mock(); spotMock.Setup(s => s.Playlists.Get(It.IsAny()).Result).Returns(playlistDequeue.Dequeue); var config = new PlaylistWatcherConfig() { PlaylistId = "spotify:playlist:test" }; var watcher = new PlaylistWatcher(config, spotMock.Object); using var monitoredWatcher = watcher.Monitor(); for (var i = 0; i < playing.Count; i++) { await watcher.WatchOne(); } toRaise.ForEach(r => monitoredWatcher.Should().Raise(r).WithSender(watcher)); toNotRaise.ForEach(r => monitoredWatcher.Should().NotRaise(r)); } } }