106 lines
3.2 KiB
C++
106 lines
3.2 KiB
C++
#include <doctest/doctest.h>
|
|
|
|
#include "helpers/StubHttpServer.hpp"
|
|
#include <echoion/core/NetworkClient.hpp>
|
|
#include <echoion/core/sources/InnertubeSource.hpp>
|
|
|
|
#include <memory>
|
|
|
|
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;
|
|
}
|
|
|
|
constexpr const char* kPlayerJson = R"({
|
|
"playabilityStatus": {"status": "OK"},
|
|
"videoDetails": {
|
|
"videoId": "dQw4w9WgXcQ",
|
|
"title": "Inner Video",
|
|
"author": "Inner Author",
|
|
"lengthSeconds": "212",
|
|
"thumbnail": {"thumbnails": [{"url": "https://example.com/t.jpg"}]}
|
|
},
|
|
"streamingData": {
|
|
"adaptiveFormats": [
|
|
{"itag": 137, "mimeType": "video/mp4", "bitrate": 4000000,
|
|
"url": "https://video.example/ignored"},
|
|
{"itag": 140, "mimeType": "audio/mp4; codecs=\"mp4a\"",
|
|
"bitrate": 130000, "url": "https://audio.example/aac"},
|
|
{"itag": 251, "mimeType": "audio/webm; codecs=\"opus\"",
|
|
"bitrate": 160000, "url": "https://audio.example/opus"}
|
|
]
|
|
}
|
|
})";
|
|
} // namespace
|
|
|
|
TEST_CASE("InnertubeSource resolves metadata and picks the best audio URL")
|
|
{
|
|
StubHttpServer stub;
|
|
stub.route("/youtubei/v1/player*", {.body = kPlayerJson});
|
|
|
|
auto network = makeNetwork();
|
|
InnertubeSource source(network, stub.baseUrl());
|
|
source.configure(true, /*requireProxy=*/false);
|
|
|
|
CHECK(source.isAvailable());
|
|
CHECK(source.canHandle("https://www.youtube.com/watch?v=dQw4w9WgXcQ"));
|
|
CHECK(source.canHandle("dQw4w9WgXcQ"));
|
|
CHECK_FALSE(source.canHandle("free text query")); // search stays elsewhere
|
|
CHECK_FALSE(source.canHandle("https://soundcloud.com/x/y"));
|
|
|
|
auto track = source.resolve("https://youtu.be/dQw4w9WgXcQ");
|
|
REQUIRE(track.has_value());
|
|
CHECK(track->title == "Inner Video");
|
|
CHECK(track->artist == "Inner Author");
|
|
CHECK(track->duration == std::chrono::milliseconds(212000));
|
|
CHECK(track->source == Api::TrackSource::YouTube);
|
|
|
|
auto handle = source.getStream(*track);
|
|
REQUIRE(handle.has_value());
|
|
CHECK(handle->url == "https://audio.example/opus"); // 251 over 140
|
|
CHECK(handle->sourceName == "innertube");
|
|
}
|
|
|
|
TEST_CASE("InnertubeSource reports unplayable videos as errors")
|
|
{
|
|
StubHttpServer stub;
|
|
stub.route("/youtubei/v1/player*", {.body = R"({
|
|
"playabilityStatus": {"status": "LOGIN_REQUIRED",
|
|
"reason": "Sign in to confirm your age"}
|
|
})"});
|
|
|
|
auto network = makeNetwork();
|
|
InnertubeSource source(network, stub.baseUrl());
|
|
source.configure(true, false);
|
|
|
|
Api::TrackInfo track;
|
|
track.id = "dQw4w9WgXcQ";
|
|
track.originalUrl = "https://www.youtube.com/watch?v=dQw4w9WgXcQ";
|
|
auto handle = source.getStream(track);
|
|
REQUIRE_FALSE(handle.has_value());
|
|
CHECK(handle.error().message.find("LOGIN_REQUIRED") !=
|
|
std::string::npos);
|
|
}
|
|
|
|
TEST_CASE("InnertubeSource is skipped without a proxy when required")
|
|
{
|
|
auto network = makeNetwork(); // no proxy configured
|
|
InnertubeSource source(network);
|
|
source.configure(true, /*requireProxy=*/true);
|
|
CHECK_FALSE(source.isAvailable());
|
|
|
|
source.configure(true, /*requireProxy=*/false);
|
|
CHECK(source.isAvailable());
|
|
|
|
source.configure(false, false);
|
|
CHECK_FALSE(source.isAvailable());
|
|
}
|