Files
EchoionCore/tests/test_config_loader.cpp
T

139 lines
3.9 KiB
C++

#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");
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
}
})");
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");
}
const auto parentLog =
ConfigLoader::loadFromJson(R"({"logging":{"fileName":".."}})");
expect(parentLog && parentLog->logging.fileName == "echoion.log",
"parent-directory logging filename is normalized");
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;
}