diff options
| author | Florian Fischer <florian.fischer@muhq.space> | 2023-08-08 18:41:56 +0200 |
|---|---|---|
| committer | Florian Fischer <florian.fischer@muhq.space> | 2023-08-08 18:41:56 +0200 |
| commit | 67b25dea819ea27a9229afebee65e4110c4c6678 (patch) | |
| tree | cf7382abf8f0ac24775214784729763f535e2be0 | |
| parent | 0846ad4904d9af056da387058525d87bfdec5792 (diff) | |
| download | WRMS-67b25dea819ea27a9229afebee65e4110c4c6678.tar.gz WRMS-67b25dea819ea27a9229afebee65e4110c4c6678.zip | |
fix broken indices in songs after Playlist.OrderedList()
04205039f24ea6e71d2ce94c7cb7e1395f95179e was not enough to
fix the ordered list issue introduced by
26f0033ebb5f8e7c204a042709e3dfa885eac868.
heap.Pop uses Swap internally which modifies as songs index.
We fix the broken indices by overriding them after the ordered list
was created.
| -rw-r--r-- | playlist.go | 6 | ||||
| -rw-r--r-- | wrms_test.go | 32 |
2 files changed, 36 insertions, 2 deletions
diff --git a/playlist.go b/playlist.go index 049354e..c11843e 100644 --- a/playlist.go +++ b/playlist.go @@ -72,6 +72,12 @@ func (pl *Playlist) OrderedList() []*Song { songs = append(songs, heap.Pop(&cpy).(*Song)) } + // TODO: Use more efficiently traversable data structure + llog.DDebug("fix song indices in pl") + for i, s := range *pl { + s.index = i + } + llog.DDebug("ordering queue %v returned %v", pl, songs) return songs } diff --git a/wrms_test.go b/wrms_test.go index 7d7942c..75318fb 100644 --- a/wrms_test.go +++ b/wrms_test.go @@ -1,6 +1,7 @@ package main import ( + "fmt" "github.com/google/uuid" "io" "testing" @@ -48,10 +49,13 @@ func TestWrmsAdd3Next3(t *testing.T) { t.Fail() } - for _, s := range wrms.queue.OrderedList() { + oq := wrms.queue.OrderedList() + t.Logf("pl: %v", wrms.queue) + t.Logf("oq: %v", oq) + for _, s := range oq { wrms.Next() if wrms.CurrentSong.Load().Uri != s.Uri { - t.Log("Not returning the next song") + t.Logf("Not returning the next song is: %v expected: %v", wrms.CurrentSong.Load(), s) t.Fail() } } @@ -160,3 +164,27 @@ func TestTimeBonus(t *testing.T) { t.Fail() } } + +func TestMultipleAddsOrderedListUpNext(t *testing.T) { + wrms := Wrms{Player: &mockPlayer{}} + nsongs := 21 + songs := make([]*Song, 0, nsongs) + for i := 0; i < nsongs; i++ { + songs = append(songs, NewDummySong(fmt.Sprintf("song%d", i), "snfmt")) + } + + for _, s := range songs { + wrms._addSong(s) + } + + _ = wrms.queue.OrderedList() + + s := songs[16] + wrms.AdjustSongWeight(alice, s.Uri, "up") + t.Logf("queue %v", wrms.queue) + wrms.Next() + if wrms.CurrentSong.Load().Uri != s.Uri { + t.Logf("Not playing the upvoted song %v", s) + t.Fail() + } +} |
