From fca26fd06637c65c632b952680ddcc407fc1c0a2 Mon Sep 17 00:00:00 2001 From: faumaray Date: Sun, 26 Jul 2026 18:11:28 +0300 Subject: [PATCH] Runtime: reset stale Core baseline on upgrade --- .../plugins/short_url/ExternalShortener.hpp | 5 +++ src/ExternalShortener.cpp | 12 +++++++ src/Plugin.cpp | 31 ++++++++++++++----- tests/ExternalShortenerTests.cpp | 10 ++++++ 4 files changed, 51 insertions(+), 7 deletions(-) diff --git a/include/echoion/plugins/short_url/ExternalShortener.hpp b/include/echoion/plugins/short_url/ExternalShortener.hpp index cc0efbb..115f3e6 100644 --- a/include/echoion/plugins/short_url/ExternalShortener.hpp +++ b/include/echoion/plugins/short_url/ExternalShortener.hpp @@ -52,6 +52,11 @@ namespace Echoion::Plugins::ShortUrl std::size_t maximumConcurrency{4}; }; + // Missing endpoint means this optional provider is intentionally dormant. + // Malformed or partially-present configuration still counts as configured + // so plugin activation rejects it instead of silently ignoring it. + [[nodiscard]] bool hasExternalConfiguration(std::string_view payload); + [[nodiscard]] std::expected parseExternalConfig(std::string_view payload); diff --git a/src/ExternalShortener.cpp b/src/ExternalShortener.cpp index 01fc454..a7d7433 100644 --- a/src/ExternalShortener.cpp +++ b/src/ExternalShortener.cpp @@ -615,6 +615,18 @@ namespace Echoion::Plugins::ShortUrl } } // namespace + bool hasExternalConfiguration(const std::string_view payload) + { + if (payload.empty()) { + return false; + } + auto root = Void::Json::parse(payload); + if (!root || !root->is_object()) { + return true; + } + return settingsObject(*root).find("endpoint") != nullptr; + } + std::expected parseExternalConfig(const std::string_view payload) { diff --git a/src/Plugin.cpp b/src/Plugin.cpp index 5cf70e7..41fe640 100644 --- a/src/Plugin.cpp +++ b/src/Plugin.cpp @@ -41,6 +41,11 @@ namespace Echoion::Plugins::ShortUrl { shortener_.reset(); activationError_.clear(); + dormant_ = !hasExternalConfiguration(configPayload_); + if (dormant_) { + activationError_ = "plugin is not configured"; + return; + } auto config = parseExternalConfig(configPayload_); if (!config) { activationError_ = std::move(config).error(); @@ -56,6 +61,11 @@ namespace Echoion::Plugins::ShortUrl void on_unload() override { shortener_.reset(); } + [[nodiscard]] bool ready() const noexcept + { + return dormant_ || shortener_ != nullptr; + } + [[nodiscard]] Void::ShortUrl::Health health() const override { if (shortener_ != nullptr) { @@ -73,10 +83,16 @@ namespace Echoion::Plugins::ShortUrl shorten(const Void::ShortUrl::ShortenRequest& request) override { if (shortener_ == nullptr) { - return {.error = { + return { + .short_url = {}, + .key = {}, + .expires_at_unix_seconds = 0, + .error = + { .code = Void::ShortUrl::ErrorCode::unavailable, .message = "external shortener is unavailable", - }}; + }, + }; } return shortener_->shorten(request); } @@ -86,6 +102,7 @@ namespace Echoion::Plugins::ShortUrl std::string configPayload_; std::string activationError_; std::unique_ptr shortener_; + bool dormant_ = false; }; } // namespace Echoion::Plugins::ShortUrl @@ -124,11 +141,11 @@ void void_plugin_configure(void* instance, const char* config) int void_plugin_ready(void* instance) { - const auto health = - static_cast( - instance) - ->health(); - return health.status == Void::ShortUrl::HealthStatus::healthy ? 0 : 1; + return static_cast( + instance) + ->ready() + ? 0 + : 1; } Void::Service::IService* void_plugin_service(void* instance) diff --git a/tests/ExternalShortenerTests.cpp b/tests/ExternalShortenerTests.cpp index 21e235a..4762bc0 100644 --- a/tests/ExternalShortenerTests.cpp +++ b/tests/ExternalShortenerTests.cpp @@ -34,6 +34,7 @@ namespace int main() { using Echoion::Plugins::ShortUrl::ExternalShortener; + using Echoion::Plugins::ShortUrl::hasExternalConfiguration; using Echoion::Plugins::ShortUrl::parseExternalConfig; int failures{}; @@ -46,6 +47,15 @@ int main() }; EnvironmentVariable token{"ECHOION_SHORTENER_TEST_TOKEN", "test secret"}; + check(!hasExternalConfiguration( + R"JSON({"settings":{},"_host":{"stateDirectory":"/tmp"}})JSON"), + "empty host envelope did not keep the optional provider dormant"); + check( + hasExternalConfiguration(R"JSON({"settings":{"endpoint":false}})JSON"), + "partially configured provider was incorrectly treated as dormant"); + check(hasExternalConfiguration("{"), + "malformed provider configuration was incorrectly treated as " + "dormant"); const auto jsonConfig = R"JSON({ "endpoint":"https://short.example/api", "method":"POST",