Signed-off-by: faumaray <faumaray@gmail.com>
This commit is contained in:
faumaray committed 2026-07-25 23:44:42 +03:00
1 parent 9f06af6c80
commit 7975f2b38e
19 files changed
+2020 -747

No files matched your search

+111
View File
@@ -0,0 +1,111 @@
#include "echoion/core/ConfigLoader.hpp"
#include <iostream>
#include <string>
#include <string_view>
namespace
{
int failures = 0;
void expect(bool condition, std::string_view message)
{
if (!condition) {
std::cerr << "FAILED: " << message << '\n';
++failures;
}
}
void expectShortening(std::string_view name, std::string_view json,
bool enabled, std::string_view provider)
{
const auto config = Echoion::Core::ConfigLoader::loadFromJson(json);
if (!config) {
std::cerr << "FAILED: " << name
<< " did not parse: " << config.error().message << '\n';
++failures;
return;
}
expect(config->urlShortening.enabled == enabled,
std::string{name} + " enabled state");
expect(config->urlShortening.provider == provider,
std::string{name} + " provider");
}
} // namespace
int main()
{
using Echoion::Core::ConfigLoader;
const auto defaults = ConfigLoader::createDefaultConfig();
expect(!defaults.urlShortening.enabled,
"generated defaults keep URL shortening disabled");
expectShortening("legacy disabled",
R"({
"youtube": {"enableUrlShortening": false},
"web": {"publicUrl": "https://bot.example"}
})",
false, "url-shortener-internal");
const auto legacyEnabled = ConfigLoader::loadFromJson(R"({
"youtube": {"enableUrlShortening": true},
"web": {"publicUrl": "https://bot.example"}
})");
if (!legacyEnabled) {
std::cerr << "FAILED: legacy enabled did not parse: "
<< legacyEnabled.error().message << '\n';
++failures;
} else {
expect(legacyEnabled->urlShortening.enabled,
"legacy enabled flag migrates through JSON updates");
expect(legacyEnabled->urlShortening.provider ==
"url-shortener-internal",
"legacy migration selects internal provider");
expect(legacyEnabled->urlShortening.sources.size() == 1 &&
legacyEnabled->urlShortening.sources.front() == "youtube",
"legacy migration limits shortening to YouTube");
}
expectShortening("explicit section wins",
R"({
"youtube": {"enableUrlShortening": true},
"urlShortening": {
"enabled": false,
"provider": "url-shortener-internal"
},
"web": {"publicUrl": "https://bot.example"}
})",
false, "url-shortener-internal");
expectShortening("internal provider without public URL",
R"({
"urlShortening": {
"enabled": true,
"provider": "url-shortener-internal"
}
})",
false, "url-shortener-internal");
expectShortening("internal provider with public URL",
R"({
"urlShortening": {
"enabled": true,
"provider": "url-shortener-internal"
},
"web": {"publicUrl": "https://bot.example"}
})",
true, "url-shortener-internal");
expectShortening("external provider without public URL",
R"({
"urlShortening": {
"enabled": true,
"provider": "url-shortener-external"
}
})",
true, "url-shortener-external");
return failures == 0 ? 0 : 1;
}
+154
View File
@@ -0,0 +1,154 @@
#include "echoion/core/UrlShortener.hpp"
#include "echoion/core/YouTubeClient.hpp"
#include <array>
#include <iostream>
#include <memory>
#include <stdexcept>
#include <string>
#include <utility>
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<FakeShortener>();
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;
}