#include "echoion/core/ConfigLoader.hpp" #include #include #include 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"); expect(defaults.logging.fileName == "echoion.log" && defaults.logging.maxFiles == 10, "generated defaults configure bounded rolling logs"); const auto logging = ConfigLoader::loadFromJson(R"({ "logging": { "level": "verbose", "fileName": "../outside.log", "maxFileSizeBytes": 1, "maxFiles": 0, "format": "xml", "pattern": "", "mode": "parallel", "queueCapacity": 1, "overflowPolicy": "discard" } })"); expect(logging.has_value(), "logging configuration parses"); if (logging) { expect(logging->logging.level == "debug", "invalid log level is normalized"); expect(logging->logging.fileName == "echoion.log", "log file cannot escape the state log directory"); expect(logging->logging.maxFileSizeBytes == 64 * 1024, "log size is clamped to a safe minimum"); expect(logging->logging.maxFiles == 1, "at least one rolled log is retained"); expect(logging->logging.format == "pattern" && !logging->logging.pattern.empty(), "invalid formatter configuration is normalized"); expect(logging->logging.mode == "async" && logging->logging.queueCapacity == 16 && logging->logging.overflowPolicy == "block", "invalid dispatch configuration is normalized"); } const auto parentLog = ConfigLoader::loadFromJson(R"({"logging":{"fileName":".."}})"); expect(parentLog && parentLog->logging.fileName == "echoion.log", "parent-directory logging filename is normalized"); const auto licensing = ConfigLoader::loadFromJson(R"({ "licensing": { "required": true, "serviceUrl": "http://downgrade.example", "productId": "", "activationTokenEnv": "", "refreshInterval": "1m", "retryInterval": "1s", "maximumLeaseLifetime": "72h" } })"); expect(licensing.has_value(), "licensing configuration parses"); if (licensing) { expect(licensing->licensing.required, "licensing required flag is preserved"); expect(licensing->licensing.serviceUrl.empty(), "licensing service cannot downgrade from HTTPS"); expect(licensing->licensing.productId == "echoion" && licensing->licensing.activationTokenEnv == "ECHOION_LICENSE_TOKEN", "licensing identifiers receive safe defaults"); expect(licensing->licensing.refreshInterval == std::chrono::minutes(15) && licensing->licensing.retryInterval == std::chrono::seconds(30) && licensing->licensing.maximumLeaseLifetime == std::chrono::hours(24), "licensing cadence and offline lease are bounded"); } 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; }