#include "echoion/core/UrlShortener.hpp" #include "echoion/core/YouTubeClient.hpp" #include #include #include #include #include #include namespace { class FakeShortener final : public Void::ShortUrl::IShortener { public: [[nodiscard]] std::uint32_t service_version() const noexcept override { return version; } [[nodiscard]] std::string_view provider_id() const noexcept override { return "test-shortener"; } [[nodiscard]] Void::ShortUrl::Health health() const override { if (throwFromHealth) { throw std::runtime_error("health failure"); } return {.status = healthStatus, .message = {}}; } [[nodiscard]] Void::ShortUrl::ShortenResult shorten(const Void::ShortUrl::ShortenRequest& request) override { ++calls; lastTarget = request.target_url; return { .short_url = "https://sho.rt/abc", .key = "abc", .expires_at_unix_seconds = 0, .error = {}, }; } std::uint32_t version{Void::ShortUrl::kServiceVersion}; Void::ShortUrl::HealthStatus healthStatus{ Void::ShortUrl::HealthStatus::healthy}; bool throwFromHealth{false}; int calls{0}; std::string lastTarget; }; bool expect(bool condition, std::string_view message) { if (!condition) { std::cerr << message << '\n'; } return condition; } } // namespace int main() { using Echoion::Api::ErrorCode; using Echoion::Api::TrackSource; using Echoion::Core::UrlShortener; using Echoion::Core::YouTubeClient; if (!expect( YouTubeClient::isYouTubeUrl( "https://www.youtube.com/watch?v=dQw4w9WgXcQ") && YouTubeClient::isYouTubeUrl("https://youtu.be/dQw4w9WgXcQ") && !YouTubeClient::isYouTubeUrl( "https://youtube.com.evil.test/watch?v=dQw4w9WgXcQ") && !YouTubeClient::isYouTubeUrl( "https://example.test/?next=youtube.com"), "YouTube URL classification accepted a non-YouTube host")) { return 1; } auto provider = std::make_shared(); UrlShortener shortener; shortener.setProvider([provider] { return provider; }); constexpr std::array sourceCases = { std::pair{"youtube", TrackSource::YouTube}, std::pair{"yandex", TrackSource::Yandex}, std::pair{"url", TrackSource::URL}, std::pair{"soundcloud", TrackSource::SoundCloud}, std::pair{"spotify", TrackSource::Spotify}, std::pair{"plugin", TrackSource::Plugin}, }; for (const auto& [name, source] : sourceCases) { shortener.setSources({name}); auto result = shortener.shortenUrl("https://example.test/track", source); if (!expect(result && *result == "https://sho.rt/abc", "configured source was not shortened")) { return 1; } } const int acceptedCalls = provider->calls; shortener.setSources({"url"}); for (const auto source : {TrackSource::Unknown, TrackSource::LocalFile, TrackSource::YouTube}) { auto result = shortener.shortenUrl("https://example.test/rejected", source); if (!expect(!result && result.error().code == ErrorCode::InvalidArgument, "unconfigured or unsafe source was accepted")) { return 1; } } if (!expect(provider->calls == acceptedCalls, "provider was called for a rejected source")) { return 1; } provider->healthStatus = Void::ShortUrl::HealthStatus::unhealthy; auto unhealthy = shortener.shortenUrl("https://example.test/track", TrackSource::URL); if (!expect(!unhealthy && unhealthy.error().code == ErrorCode::ServiceUnavailable, "unhealthy provider was accepted")) { return 1; } provider->healthStatus = Void::ShortUrl::HealthStatus::healthy; provider->version = Void::ShortUrl::kServiceVersion + 1; auto incompatible = shortener.shortenUrl("https://example.test/track", TrackSource::URL); if (!expect(!incompatible && incompatible.error().code == ErrorCode::ServiceUnavailable, "incompatible provider was accepted")) { return 1; } provider->version = Void::ShortUrl::kServiceVersion; provider->throwFromHealth = true; auto throwing = shortener.shortenUrl("https://example.test/track", TrackSource::URL); if (!expect(!throwing && throwing.error().code == ErrorCode::ServiceUnavailable, "provider exception escaped the adapter")) { return 1; } return 0; }