138 lines
3.3 KiB
C++
138 lines
3.3 KiB
C++
#include <doctest/doctest.h>
|
|
|
|
#include <echoion/core/playback/QueueService.hpp>
|
|
|
|
#include <set>
|
|
#include <string>
|
|
|
|
using Echoion::Core::QueueService;
|
|
|
|
namespace
|
|
{
|
|
Echoion::Api::TrackInfo track(const std::string& id)
|
|
{
|
|
Echoion::Api::TrackInfo t;
|
|
t.id = id;
|
|
t.title = id;
|
|
return t;
|
|
}
|
|
} // namespace
|
|
|
|
TEST_CASE("QueueService FIFO order without shuffle")
|
|
{
|
|
QueueService q;
|
|
auto a = q.enqueue(track("a"), "tester", 100);
|
|
auto b = q.enqueue(track("b"), "tester", 100);
|
|
REQUIRE(a.has_value());
|
|
REQUIRE(b.has_value());
|
|
CHECK(q.size() == 2);
|
|
|
|
auto peek = q.peekNext();
|
|
REQUIRE(peek.has_value());
|
|
CHECK(peek->track.id == "a");
|
|
|
|
auto first = q.popNext();
|
|
REQUIRE(first.has_value());
|
|
CHECK(first->track.id == "a");
|
|
CHECK(first->addedBy == "tester");
|
|
CHECK(first->queueId == *a);
|
|
|
|
auto second = q.popNext();
|
|
REQUIRE(second.has_value());
|
|
CHECK(second->track.id == "b");
|
|
CHECK(q.popNext() == std::nullopt);
|
|
}
|
|
|
|
TEST_CASE("QueueService enforces max size")
|
|
{
|
|
QueueService q;
|
|
CHECK(q.enqueue(track("a"), "t", 1).has_value());
|
|
CHECK_FALSE(q.enqueue(track("b"), "t", 1).has_value());
|
|
}
|
|
|
|
TEST_CASE("QueueService popIfNext detects stale peeks")
|
|
{
|
|
QueueService q;
|
|
auto a = q.enqueue(track("a"), "t", 100);
|
|
q.enqueue(track("b"), "t", 100).has_value();
|
|
REQUIRE(a.has_value());
|
|
|
|
auto peek = q.peekNext();
|
|
REQUIRE(peek.has_value());
|
|
|
|
// Queue edited after the peek: the peeked entry is removed.
|
|
auto removed = q.removeAt(0);
|
|
REQUIRE(removed.has_value());
|
|
|
|
// Commit must fail — "a" is gone, "b" is next now.
|
|
CHECK(q.popIfNext(peek->queueId) == std::nullopt);
|
|
auto next = q.popNext();
|
|
REQUIRE(next.has_value());
|
|
CHECK(next->track.id == "b");
|
|
}
|
|
|
|
TEST_CASE("QueueService shuffle plays every track exactly once")
|
|
{
|
|
QueueService q;
|
|
for (int i = 0; i < 10; ++i) {
|
|
REQUIRE(q.enqueue(track(std::to_string(i)), "t", 100).has_value());
|
|
}
|
|
q.setShuffle(true);
|
|
|
|
std::set<std::string> seen;
|
|
for (int i = 0; i < 10; ++i) {
|
|
auto e = q.popNext();
|
|
REQUIRE(e.has_value());
|
|
CHECK(seen.insert(e->track.id).second); // no duplicates
|
|
}
|
|
CHECK(q.empty());
|
|
CHECK(q.popNext() == std::nullopt);
|
|
}
|
|
|
|
TEST_CASE("QueueService shuffle survives removals and moves")
|
|
{
|
|
QueueService q;
|
|
for (int i = 0; i < 8; ++i) {
|
|
REQUIRE(q.enqueue(track(std::to_string(i)), "t", 100).has_value());
|
|
}
|
|
q.setShuffle(true);
|
|
|
|
// Consume a couple, then mutate the queue between picks.
|
|
std::set<std::string> seen;
|
|
auto consume = [&] {
|
|
auto e = q.popNext();
|
|
REQUIRE(e.has_value());
|
|
CHECK(seen.insert(e->track.id).second);
|
|
};
|
|
consume();
|
|
consume();
|
|
|
|
REQUIRE(q.removeAt(0).has_value());
|
|
REQUIRE(q.move(0, q.size() - 1).has_value());
|
|
REQUIRE(q.enqueue(track("new"), "t", 100).has_value());
|
|
|
|
// Drain: every pop must be a valid, never-seen entry (the index fixup
|
|
// must not point outside the queue or replay a track).
|
|
while (!q.empty()) {
|
|
consume();
|
|
}
|
|
CHECK(seen.size() == 8); // 8 original - 1 removed + 1 new
|
|
}
|
|
|
|
TEST_CASE("QueueService peekNext matches popNext under shuffle")
|
|
{
|
|
QueueService q;
|
|
for (int i = 0; i < 6; ++i) {
|
|
REQUIRE(q.enqueue(track(std::to_string(i)), "t", 100).has_value());
|
|
}
|
|
q.setShuffle(true);
|
|
|
|
while (!q.empty()) {
|
|
auto peek = q.peekNext();
|
|
REQUIRE(peek.has_value());
|
|
auto popped = q.popIfNext(peek->queueId);
|
|
REQUIRE(popped.has_value());
|
|
CHECK(popped->queueId == peek->queueId);
|
|
}
|
|
}
|