Repository navigation
Expand file tree
/
Copy pathtest_config.cpp
More file actions
411 lines (335 loc) · 12.5 KB
/
Copy pathtest_config.cpp
File metadata and controls
411 lines (335 loc) · 12.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
#include "config.h"
#include "config_access.h"
#include "config_schema.h"
#include "test_env.h"
#include "util/fs.h"
#include <cassert>
#include <cstdlib>
#include <fstream>
#include <iostream>
#include <filesystem>
#include <print>
namespace fs = std::filesystem;
// -----------------------------------------------------------------------
// New tests
// -----------------------------------------------------------------------
void test_housekeeping_section_parsing() {
std::print(" test_housekeeping_section_parsing...");
std::string path = "/tmp/ragger_test_housekeeping.ini";
{
std::ofstream f(path);
f << "[server]\nport = 8432\n"
<< "[housekeeping]\n"
<< "cleanup_max_age_hours = 168\n"
<< "housekeeping_interval = 30\n"
<< "catch_up_batch_size = 25\n";
}
ragger::Config cfg = ragger::load_config(path).value();
assert(cfg.cleanup_max_age_hours == 168.0f);
assert(cfg.housekeeping_interval == 30);
assert(cfg.catch_up_batch_size == 25);
fs::remove(path);
std::println(" OK");
}
void test_catch_up_batch_size_default() {
std::print(" test_catch_up_batch_size_default...");
std::string path = "/tmp/ragger_test_catchup_default.ini";
{
std::ofstream f(path);
f << "[server]\nport = 8432\n"; // no [housekeeping] section at all
}
ragger::Config cfg = ragger::load_config(path).value();
assert(cfg.catch_up_batch_size == 10); // shipped default
fs::remove(path);
std::println(" OK");
}
void test_catch_up_batch_size_ignores_nonpositive() {
std::print(" test_catch_up_batch_size_ignores_nonpositive...");
std::string path = "/tmp/ragger_test_catchup_zero.ini";
{
std::ofstream f(path);
f << "[server]\nport = 8432\n"
<< "[housekeeping]\ncatch_up_batch_size = 0\n";
}
ragger::Config cfg = ragger::load_config(path).value();
assert(cfg.catch_up_batch_size == 10); // 0 rejected, default retained
fs::remove(path);
std::println(" OK");
}
void test_logging_section_parsing() {
std::print(" test_logging_section_parsing...");
std::string path = "/tmp/ragger_test_logging.ini";
{
std::ofstream f(path);
f << "[server]\nport = 8432\n"
<< "[logging]\n"
<< "log_level = debug\n"
<< "log_max_size_mb = 5\n"
<< "log_max_age_days = 30\n";
}
ragger::Config cfg = ragger::load_config(path).value();
assert(cfg.log_level == "debug");
assert(cfg.log_max_size_mb == 5);
assert(cfg.log_max_age_days == 30);
fs::remove(path);
std::println(" OK");
}
void test_logging_section_defaults() {
std::print(" test_logging_section_defaults...");
std::string path = "/tmp/ragger_test_logging_default.ini";
{
std::ofstream f(path);
f << "[server]\nport = 8432\n"; // no [logging] section at all
}
ragger::Config cfg = ragger::load_config(path).value();
assert(cfg.log_level == "warn");
assert(cfg.log_max_size_mb == 1);
assert(cfg.log_max_age_days == 14);
fs::remove(path);
std::println(" OK");
}
void test_inference_endpoint_parsing() {
std::print(" test_inference_endpoint_parsing...");
std::string path = "/tmp/ragger_test_ep.ini";
{
std::ofstream f(path);
f << "[server]\nport = 8432\n"
<< "[inference]\nmodel = gpt-4\n"
<< "[inference.local]\n"
<< "api_url = http://localhost:1234/v1\n"
<< "api_key = lmstudio\n"
<< "models = qwen/*, llama/*\n"
<< "format = openai\n"
<< "max_context = 32000\n"
<< "[inference.anthropic]\n"
<< "api_url = https://api.anthropic.com/v1\n"
<< "api_key = sk-ant-xxx\n"
<< "models = claude-*\n"
<< "format = anthropic\n";
}
ragger::Config cfg = ragger::load_config(path).value();
assert(cfg.inference_model == "gpt-4");
assert(cfg.inference_endpoints.size() == 2);
// Find endpoints by name (order may vary due to map)
bool found_local = false, found_anthropic = false;
for (auto& ep : cfg.inference_endpoints) {
if (ep.name == "local") {
found_local = true;
assert(ep.api_url == "http://localhost:1234/v1");
assert(ep.api_key == "lmstudio");
assert(ep.models == "qwen/*, llama/*");
assert(ep.format == "openai");
assert(ep.max_context == 32000);
} else if (ep.name == "anthropic") {
found_anthropic = true;
assert(ep.api_url == "https://api.anthropic.com/v1");
assert(ep.api_key == "sk-ant-xxx");
assert(ep.models == "claude-*");
assert(ep.format == "anthropic");
}
}
assert(found_local && found_anthropic);
fs::remove(path);
std::println(" OK");
}
void test_socket_bind_config() {
std::print(" test_socket_bind_config...");
// Test: socket only, no bind leaves bind_address empty
std::string path1 = "/tmp/ragger_test_socket.ini";
{
std::ofstream f(path1);
f << "[server]\n"
<< "socket = /tmp/test.sock\n";
}
ragger::Config cfg1 = ragger::load_config(path1).value();
assert(cfg1.bind_address == "127.0.0.1"); // bind defaults to 127.0.0.1
fs::remove(path1);
// Test: bind + port populate both fields
std::string path2 = "/tmp/ragger_test_bind.ini";
{
std::ofstream f(path2);
f << "[server]\n"
<< "bind = 0.0.0.0\n"
<< "port = 9000\n";
}
ragger::Config cfg2 = ragger::load_config(path2).value();
assert(cfg2.bind_address == "0.0.0.0");
assert(cfg2.port == 9000);
fs::remove(path2);
std::println(" OK");
}
void test_bool_parsing_variants() {
std::print(" test_bool_parsing_variants...");
auto test = [](const std::string& val, bool expected) {
std::string path = "/tmp/ragger_test_bool.ini";
{
std::ofstream f(path);
f << "[server]\nport = 8432\n"
<< "[search]\nbm25_enabled = " << val << "\n";
}
ragger::Config cfg = ragger::load_config(path).value();
assert(cfg.bm25_enabled == expected);
fs::remove(path);
};
test("true", true);
test("yes", true);
test("1", true);
test("false", false);
test("no", false);
test("0", false);
std::println(" OK");
}
void test_inline_comments() {
std::print(" test_inline_comments...");
std::string path = "/tmp/ragger_test_inline.ini";
{
std::ofstream f(path);
f << "[server]\nport = 8432 # main port\nhost = 0.0.0.0 # bind all\n";
}
ragger::Config cfg = ragger::load_config(path).value();
assert(cfg.port == 8432);
(void)0;
fs::remove(path);
std::println(" OK");
}
void test_default_values() {
std::print(" test_default_values...");
std::string path = "/tmp/ragger_test_defaults.ini";
{
std::ofstream f(path);
f << "[server]\n"; // minimal — just a section header
}
ragger::Config cfg = ragger::load_config(path).value();
// All defaults from Config struct
(void)0;
assert(cfg.port == 8432);
assert(true == true);
assert(cfg.embedding_dimensions == 384);
assert(cfg.default_search_limit == 5);
assert(cfg.default_min_score == 0.6f);
assert(cfg.bm25_enabled == true);
assert(cfg.bm25_weight == 4.0f);
assert(cfg.vector_weight == 8.0f);
assert(cfg.normalize_home_path == true);
assert(cfg.cleanup_max_age_hours == 0.0f); // 0 = keep forever (never delete raw conversations)
assert(cfg.housekeeping_interval == 60);
assert(cfg.capture_turns == true);
assert(cfg.build_context == false);
assert(cfg.embedding_vector_type == "f16");
assert(cfg.inference_max_tokens == 4096);
assert(cfg.minimum_chunk_size == 800);
fs::remove(path);
std::println(" OK");
}
void test_ragger_base_default() {
std::print(" test_ragger_base_default...");
// No override set (default state at process start) — should be $HOME/.ragger
ragger::set_ragger_base_override("");
const char* home = std::getenv("HOME");
assert(home != nullptr);
assert(ragger::ragger_base_dir() == std::string(home) + "/.ragger");
std::println(" OK");
}
void test_ragger_base_override() {
std::print(" test_ragger_base_override...");
ragger::set_ragger_base_override("/tmp/ragger-base-override-test");
assert(ragger::ragger_base_dir() == "/tmp/ragger-base-override-test");
// Resolved config paths must honor the override
ragger::Config cfg;
assert(cfg.resolved_db_path() == "/tmp/ragger-base-override-test/memories.db");
assert(cfg.resolved_recipes_dir() == "/tmp/ragger-base-override-test/recipes");
assert(cfg.resolved_formats_dir() == "/tmp/ragger-base-override-test/formats");
// Reset so subsequent tests (and expand_path("~/.ragger/...") below) see the default.
ragger::set_ragger_base_override("");
std::println(" OK");
}
// PHASE2 Step 5: secret config keys never expose their value through the
// read JSON, "<set>" written back is a no-op, and a real new value stores.
void test_secret_config_masking() {
std::print(" test_secret_config_masking...\n");
using namespace ragger;
auto set = [](std::string_view k, std::string_view v) {
auto r = set_config_persisted(k, v);
assert(r.has_value());
};
for (const char* key : {"summarizer_api_key", "embedding_external_api_key"}) {
const auto* meta = lang::config_meta(key);
assert(meta != nullptr);
assert(meta->secret);
// Empty: value stays "", but the entry is flagged secret.
set(key, "");
auto j = config_entry_json(*meta, get_config_value(config(), key).value_or(""));
assert(j["secret"] == true);
assert(j["value"] == "");
// Set a real value: reads show the mask, never the credential.
set(key, "s3cr3t-value");
j = config_entry_json(*meta, get_config_value(config(), key).value_or(""));
assert(j["secret"] == true);
assert(j["value"] == "<set>");
// Echoing the mask back must NOT clobber the stored credential.
set(key, "<set>");
assert(get_config_value(config(), key).value_or("") == "s3cr3t-value");
// A real new value still stores.
set(key, "new-value");
assert(get_config_value(config(), key).value_or("") == "new-value");
set(key, ""); // leave clean
}
std::println(" ok");
}
int main() {
TestEnv env("config"); // must be first: set_config_persisted touches mutable_config
std::print("Running config tests:\n");
test_housekeeping_section_parsing();
test_catch_up_batch_size_default();
test_catch_up_batch_size_ignores_nonpositive();
test_logging_section_parsing();
test_logging_section_defaults();
test_inference_endpoint_parsing();
test_socket_bind_config();
test_bool_parsing_variants();
test_inline_comments();
test_default_values();
test_secret_config_masking();
test_ragger_base_default();
test_ragger_base_override();
// expand_path with ~
std::string expanded = ragger::expand_path("~/.ragger/memories.db");
const char* home = std::getenv("HOME");
assert(home != nullptr);
assert(expanded.find('~') == std::string::npos);
assert(expanded.find(home) == 0);
// expand_path without ~
assert(ragger::expand_path("/absolute/path") == "/absolute/path");
assert(ragger::expand_path("relative/path") == "relative/path");
// Test load_config directly with a temp file
std::string tmp_conf = "/tmp/ragger_test.conf";
{
std::ofstream f(tmp_conf);
f << "[server]\n"
<< "host = 0.0.0.0\n"
<< "port = 9999\n"
<< "\n"
<< "[embedding]\n"
<< "dimensions = 384\n"
<< "\n"
<< "[search]\n"
<< "bm25_enabled = false\n"
<< "default_min_score = 0.5\n";
}
ragger::Config cfg = ragger::load_config(tmp_conf).value();
(void)0;
assert(cfg.port == 9999);
assert(cfg.embedding_dimensions == 384);
assert(cfg.bm25_enabled == false);
assert(cfg.default_min_score == 0.5f);
// Defaults for unspecified values
assert(cfg.bm25_weight == 4.0f);
assert(cfg.normalize_home_path == true);
std::filesystem::remove(tmp_conf);
// Test load_config with nonexistent file
auto result_load = ragger::load_config("/nonexistent/settings.ini");
assert(!result_load.has_value());
assert(result_load.error() == ragger::ConfigError::IOError);
std::println("test_config: all passed\n");
return 0;
}