| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145 |
- #include <gtest/gtest.h>
- #include "settings.hpp"
- #include "apikey.hpp"
- #include <cctype>
- using namespace svapi;
- TEST(Settings, DefaultsAndRoundTrip) {
- Settings s = Settings::fromJson(nlohmann::json::object());
- EXPECT_EQ(s.openaiApiBase, "https://api.openai.com");
- EXPECT_EQ(s.defaultEmbeddingModel, "text-embedding-3-small");
- EXPECT_EQ(s.sessionTtlMinutes, 720u);
- EXPECT_TRUE(s.webuiEnabled);
- EXPECT_EQ(s.defaultProject, "default");
- EXPECT_EQ(Settings::fromJson(s.toJson()).toJson(), s.toJson());
- }
- TEST(Settings, EmbeddingTimeoutDefaults) {
- Settings s = Settings::fromJson(nlohmann::json::object());
- EXPECT_EQ(s.embeddingConnectTimeoutSec, 10u);
- EXPECT_EQ(s.embeddingReadTimeoutSec, 60u);
- }
- TEST(Settings, EmbeddingTimeoutRoundTrip) {
- nlohmann::json j{{"embedding_connect_timeout_sec", 30}, {"embedding_read_timeout_sec", 120}};
- Settings s = Settings::fromJson(j);
- EXPECT_EQ(s.embeddingConnectTimeoutSec, 30u);
- EXPECT_EQ(s.embeddingReadTimeoutSec, 120u);
- // round-trip: toJson → fromJson preserves values
- Settings s2 = Settings::fromJson(s.toJson());
- EXPECT_EQ(s2.embeddingConnectTimeoutSec, 30u);
- EXPECT_EQ(s2.embeddingReadTimeoutSec, 120u);
- }
- TEST(Settings, CaptchaDefaults) {
- Settings s = Settings::fromJson(nlohmann::json::object());
- EXPECT_EQ(s.captchaProvider, "");
- EXPECT_EQ(s.captchaSecret, "");
- EXPECT_EQ(s.captchaVerifyUrl, "");
- }
- TEST(Settings, CaptchaProviderAndUrlRoundTrip) {
- nlohmann::json j{{"captcha_provider", "turnstile"},
- {"captcha_verify_url", "https://challenges.cloudflare.com/turnstile/v0/siteverify"}};
- Settings s = Settings::fromJson(j);
- EXPECT_EQ(s.captchaProvider, "turnstile");
- EXPECT_EQ(s.captchaVerifyUrl, "https://challenges.cloudflare.com/turnstile/v0/siteverify");
- Settings s2 = Settings::fromJson(s.toJson());
- EXPECT_EQ(s2.captchaProvider, "turnstile");
- EXPECT_EQ(s2.captchaVerifyUrl, "https://challenges.cloudflare.com/turnstile/v0/siteverify");
- }
- TEST(Settings, CaptchaSecretRoundTrip) {
- nlohmann::json j{{"captcha_secret", "mysecret"}};
- Settings s = Settings::fromJson(j);
- EXPECT_EQ(s.captchaSecret, "mysecret");
- Settings s2 = Settings::fromJson(s.toJson());
- EXPECT_EQ(s2.captchaSecret, "mysecret");
- }
- TEST(ApiKey, JsonRoundTripAndPublicMasks) {
- ApiKey k = ApiKey::fromJson(nlohmann::json::parse(
- R"({"key":"abcdef0123456789","label":"n8n","projects":["p1","p2"],"admin":false,"created_at":7})"));
- EXPECT_EQ(k.key, "abcdef0123456789");
- EXPECT_EQ(k.projects.size(), 2u);
- EXPECT_FALSE(k.admin);
- EXPECT_EQ(k.createdAt, 7u);
- EXPECT_EQ(ApiKey::fromJson(k.toJson()).toJson(), k.toJson());
- auto pub = k.toPublicJson();
- EXPECT_FALSE(pub.contains("key")); // secret not exposed
- EXPECT_EQ(pub["label"], "n8n");
- EXPECT_EQ(pub["projects"].size(), 2u);
- EXPECT_TRUE(pub.contains("key_prefix")); // short hint only
- }
- TEST(ApiKey, CanAccess) {
- ApiKey adminK; adminK.admin = true;
- EXPECT_TRUE(adminK.canAccess("anything"));
- ApiKey star; star.projects = {"*"};
- EXPECT_TRUE(star.canAccess("anything"));
- ApiKey scoped; scoped.projects = {"p1", "p2"};
- EXPECT_TRUE(scoped.canAccess("p1"));
- EXPECT_FALSE(scoped.canAccess("p3"));
- }
- TEST(ApiKey, GenerateIsHexAndUnique) {
- std::string a = generateApiKey(), b = generateApiKey();
- EXPECT_GE(a.size(), 32u);
- EXPECT_NE(a, b);
- for (char c : a) EXPECT_TRUE(std::isxdigit(static_cast<unsigned char>(c)));
- }
- // ---------------------------------------------------------------------------
- // 0.18.1: values that used to be compiled in. Each default is pinned, because
- // the point of making them settings is that an operator can move them - and a
- // default that drifts silently changes behaviour for every deployment that
- // never set it.
- // ---------------------------------------------------------------------------
- TEST(Settings, OperationalLimitDefaults) {
- Settings s = Settings::fromJson(nlohmann::json::object());
- // 2, not 0 and not the configured connect timeout. This is the one default
- // in this group that CHANGES behaviour relative to 0.18.0, deliberately:
- // the half-of-budget rule alone gave a 6 s embedding ask connect 3 s and
- // read 3 s, so a caller's six seconds bought a three-second read.
- EXPECT_EQ(s.embeddingConnectCapSec, 2u);
- // Unchanged from the constants they replace, so no deployment that never
- // sets them behaves differently.
- EXPECT_EQ(s.vectorFilterScanCap, 10000u);
- EXPECT_EQ(s.captchaConnectTimeoutSec, 5u);
- EXPECT_EQ(s.captchaReadTimeoutSec, 10u);
- }
- TEST(Settings, OperationalLimitsRoundTrip) {
- auto j = nlohmann::json::parse(R"({
- "embedding_connect_cap_sec": 1,
- "vector_filter_scan_cap": 50000,
- "captcha_connect_timeout_sec": 3,
- "captcha_read_timeout_sec": 7
- })");
- Settings s = Settings::fromJson(j);
- EXPECT_EQ(s.embeddingConnectCapSec, 1u);
- EXPECT_EQ(s.vectorFilterScanCap, 50000u);
- EXPECT_EQ(s.captchaConnectTimeoutSec, 3u);
- EXPECT_EQ(s.captchaReadTimeoutSec, 7u);
- // Through the document and back, because these are stored in the settings
- // collection and a field that parses but is not emitted is lost on the
- // next write - which would look like the operator's change being reverted.
- EXPECT_EQ(Settings::fromJson(s.toJson()).toJson(), s.toJson());
- Settings again = Settings::fromJson(s.toJson());
- EXPECT_EQ(again.embeddingConnectCapSec, 1u);
- EXPECT_EQ(again.vectorFilterScanCap, 50000u);
- }
- TEST(Settings, AZeroConnectCapIsUnsetRatherThanNoConnectTime) {
- // 0 means "no absolute cap, fall back to the half rule" - it must never
- // reach the HTTP client as a zero connect timeout, which to cpp-httplib
- // means NO timeout and is the unbounded call the whole split prevents.
- // resolveEmbedBudget enforces that; this pins that 0 survives the config
- // round trip as 0 rather than being coerced to the default.
- Settings s = Settings::fromJson(nlohmann::json::parse(R"({"embedding_connect_cap_sec":0})"));
- EXPECT_EQ(s.embeddingConnectCapSec, 0u);
- EXPECT_EQ(Settings::fromJson(s.toJson()).embeddingConnectCapSec, 0u);
- }
|