Files
Echoion/tests/YandexMusicSourceTests.cpp
T
faumaray a3715201e1 Release 1.0.0
Signed-off-by: faumaray <faumaray@gmail.com>
2026-07-04 01:50:44 +03:00

240 lines
7.2 KiB
C++

#include <doctest/doctest.h>
#include "helpers/StubHttpServer.hpp"
#include <echoion/core/NetworkClient.hpp>
#include <echoion/core/sources/YandexMusicSource.hpp>
#include <memory>
#include <openssl/evp.h>
using namespace Echoion;
using namespace Echoion::Core;
using Echoion::Test::StubHttpServer;
namespace
{
std::shared_ptr<NetworkClient> makeNetwork()
{
auto n = std::make_shared<NetworkClient>();
REQUIRE(n->initialize().has_value());
return n;
}
std::string md5hexRef(const std::string& data)
{
unsigned char digest[EVP_MAX_MD_SIZE];
unsigned int length = 0;
REQUIRE(EVP_Digest(data.data(), data.size(), digest, &length,
EVP_md5(), nullptr) == 1);
static constexpr char hex[] = "0123456789abcdef";
std::string out;
for (unsigned int i = 0; i < length; ++i) {
out.push_back(hex[digest[i] >> 4]);
out.push_back(hex[digest[i] & 0xF]);
}
return out;
}
} // namespace
TEST_CASE("YandexMusicSource availability depends on the token")
{
auto network = makeNetwork();
YandexMusicSource source(network);
source.configure(true, "");
CHECK_FALSE(source.isAvailable());
CHECK_FALSE(source.canHandle(
"https://music.yandex.com/album/123/track/456"));
source.configure(true, "token");
CHECK(source.isAvailable());
CHECK(source.canHandle("https://music.yandex.com/album/123/track/456"));
CHECK(source.canHandle("https://music.yandex.com/track/456"));
CHECK(source.canHandle("https://music.yandex.com/album/123"));
CHECK(source.canHandle(
"https://music.yandex.com/users/foo/playlists/42"));
CHECK_FALSE(source.canHandle("https://www.youtube.com/watch?v=abc"));
CHECK_FALSE(source.canHandle("free text"));
source.configure(false, "token");
CHECK_FALSE(source.isAvailable());
}
TEST_CASE("YandexMusicSource resolves track metadata")
{
StubHttpServer stub;
stub.route("/tracks/456", {.body = R"({
"result": [{
"id": "456",
"title": "Yandex Song",
"durationMs": 245000,
"coverUri": "avatars.yandex.net/get-music/abc/%%",
"artists": [{"name": "First"}, {"name": "Second"}],
"albums": [{"id": 123}]
}]
})"});
auto network = makeNetwork();
YandexMusicSource source(network, stub.baseUrl());
source.configure(true, "token");
auto track =
source.resolve("https://music.yandex.com/album/123/track/456");
REQUIRE(track.has_value());
CHECK(track->title == "Yandex Song");
CHECK(track->artist == "First, Second");
CHECK(track->duration == std::chrono::milliseconds(245000));
CHECK(track->thumbnailUrl ==
"https://avatars.yandex.net/get-music/abc/200x200");
CHECK(track->originalUrl ==
"https://music.yandex.com/album/123/track/456");
}
TEST_CASE("YandexMusicSource builds the signed stream URL")
{
StubHttpServer stub;
// download-info offers two codecs; the mp3 with the highest bitrate
// must win.
stub.route("/tracks/456/download-info",
{.body = std::string(R"({
"result": [
{"codec": "aac", "bitrateInKbps": 320,
"downloadInfoUrl": ")") + stub.baseUrl() +
R"(/info-aac"},
{"codec": "mp3", "bitrateInKbps": 192,
"downloadInfoUrl": ")" + stub.baseUrl() +
R"(/info-192"},
{"codec": "mp3", "bitrateInKbps": 320,
"downloadInfoUrl": ")" + stub.baseUrl() +
R"(/info-320"}
]
})"});
stub.route("/info-320",
{.contentType = "text/xml",
.body = "<download-info><host>cdn.example</host>"
"<path>/music/file.mp3</path><ts>12345</ts>"
"<region>ru</region><s>secretvalue</s>"
"</download-info>"});
auto network = makeNetwork();
YandexMusicSource source(network, stub.baseUrl());
source.configure(true, "token");
Api::TrackInfo track;
track.originalUrl = "https://music.yandex.com/album/123/track/456";
auto handle = source.getStream(track);
REQUIRE(handle.has_value());
const std::string expectedSign = md5hexRef(
std::string("XGRlBW9FXlekgbPrRHuSiA") + "music/file.mp3" +
"secretvalue");
CHECK(handle->url == "https://cdn.example/get-mp3/" + expectedSign +
"/12345/music/file.mp3");
CHECK(handle->sourceName == "yandex");
}
TEST_CASE("YandexMusicSource searches for tracks")
{
StubHttpServer stub;
stub.route("/search*", {.body = R"({
"result": {"tracks": {"results": [
{"id": "111", "title": "Lofi One", "durationMs": 180000,
"artists": [{"name": "Artist A"}], "albums": [{"id": 5}]},
{"id": "222", "title": "Lofi Two", "durationMs": 200000,
"artists": [{"name": "Artist B"}], "albums": [{"id": 6}]}
]}}
})"});
auto network = makeNetwork();
YandexMusicSource source(network, stub.baseUrl());
source.configure(true, "token");
auto results = source.search("lofi");
REQUIRE(results.has_value());
REQUIRE(results->tracks.size() == 2);
CHECK(results->tracks[0].title == "Lofi One");
CHECK(results->tracks[0].artist == "Artist A");
CHECK(results->tracks[1].title == "Lofi Two");
CHECK(results->tracks[0].originalUrl ==
"https://music.yandex.com/album/5/track/111");
}
TEST_CASE("YandexMusicSource lists related tracks for radio autoplay")
{
StubHttpServer stub;
stub.route("/tracks/456/similar", {.body = R"({
"result": {"similarTracks": [
{"id": "777", "title": "Similar One", "durationMs": 190000,
"artists": [{"name": "Artist X"}], "albums": [{"id": 7}]},
{"id": "888", "title": "Similar Two", "durationMs": 210000,
"artists": [{"name": "Artist Y"}], "albums": [{"id": 8}]}
]}
})"});
auto network = makeNetwork();
YandexMusicSource source(network, stub.baseUrl());
source.configure(true, "token");
auto results = source.related("456", 10);
REQUIRE(results.has_value());
REQUIRE(results->size() == 2);
CHECK((*results)[0].title == "Similar One");
CHECK((*results)[0].artist == "Artist X");
CHECK((*results)[0].source == Api::TrackSource::Yandex);
CHECK((*results)[0].originalUrl ==
"https://music.yandex.com/album/7/track/777");
CHECK((*results)[1].title == "Similar Two");
}
TEST_CASE("YandexMusicSource related is unavailable without a token")
{
auto network = makeNetwork();
YandexMusicSource source(network);
source.configure(true, "");
CHECK_FALSE(source.related("456").has_value());
}
TEST_CASE("YandexMusicSource search is unavailable without a token")
{
auto network = makeNetwork();
YandexMusicSource source(network);
source.configure(true, "");
CHECK_FALSE(source.search("lofi").has_value());
}
TEST_CASE("YandexMusicSource lists album and playlist tracks")
{
StubHttpServer stub;
stub.route("/albums/123/with-tracks", {.body = R"({
"result": {"volumes": [
[{"id": "1", "title": "A1", "artists": [{"name": "X"}]},
{"id": "2", "title": "A2", "artists": [{"name": "X"}]}],
[{"id": "3", "title": "B1", "artists": [{"name": "X"}]}]
]}
})"});
stub.route("/users/foo/playlists/42", {.body = R"({
"result": {"tracks": [
{"track": {"id": "9", "title": "P1", "artists": [{"name": "Y"}]}},
{"track": {"id": "10", "title": "P2", "artists": [{"name": "Y"}]}}
]}
})"});
auto network = makeNetwork();
YandexMusicSource source(network, stub.baseUrl());
source.configure(true, "token");
auto album =
source.playlistTracks("https://music.yandex.com/album/123");
REQUIRE(album.has_value());
REQUIRE(album->size() == 3);
CHECK((*album)[0].title == "A1");
CHECK((*album)[2].title == "B1");
auto playlist = source.playlistTracks(
"https://music.yandex.com/users/foo/playlists/42");
REQUIRE(playlist.has_value());
REQUIRE(playlist->size() == 2);
CHECK((*playlist)[1].title == "P2");
}