Files
splinter/tests/test_config.c
T
Konrad 68861460df splinter: a config file, so 'splinter' alone starts the chat
One file, key = value, comments with '#'. /splinter/config (or
~/.config/splinter/config), overridable with SPLINTER_CONFIG or --config,
ignorable with --no-config, generatable with --init-config.

Precedence is command line, then file, then built-in default, and an unknown
key is an error with its line number - a typo that is silently ignored is a
config that lies about what it does. The example file is written and read back
by the test suite, so it cannot rot into something the parser rejects.

What it covers: endpoint, model, api_key_env, transport, cli_cmd, plugin
(repeatable), theme, autonomous, steps, max_bytes, session_dir, system_prompt.

Verified from /tmp with nothing but the word 'splinter': the chat started, the
sidebar showed the configured endpoint, theme, cps and clock widgets, and a
turn against kontron answered 'Ich bin ein grosses Sprachmodell...' with the
session written to /home/lotherk/.splinter.

Thirteen test binaries green (test_config: full file, trimming, unknown key
with line number, missing '=', missing file, example round-trip, env path).
2026-11-07 20:03:17 +00:00

132 lines
3.4 KiB
C

/*
* splinter -- config file tests.
*
* The interesting cases are the refusals: an unknown key, a line without '=',
* a missing file. A config that quietly ignores what it does not understand is
* a config that lies.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "config.h"
static int failures;
#define CHECK(cond) do { \
if (!(cond)) { \
fprintf(stderr, "FAIL %s:%d: %s\n", __FILE__, __LINE__, #cond); \
failures++; \
} \
} while (0)
static void
write_file(const char *path, const char *text)
{
FILE *f = fopen(path, "w");
if (f == NULL)
return;
fputs(text, f);
fclose(f);
}
int
main(void)
{
struct sp_config c;
struct sp_buf err;
char tmpl[] = "/tmp/splinter-cfg-XXXXXX";
char path[640];
char *dir;
if (sp_buf_init(&err) != 0)
return 1;
dir = mkdtemp(tmpl);
CHECK(dir != NULL);
if (dir == NULL)
return 1;
/* ---- a full file ---- */
snprintf(path, sizeof(path), "%s/config", dir);
write_file(path,
"# a comment\n"
"\n"
"endpoint = http://127.0.0.1:8080/v1/chat/completions\n"
"model = qwen-test \n"
"theme = Broica\n"
"autonomous = 1\n"
"steps = 12\n"
"max_bytes = 4096\n"
"plugin = plugins/lcm/lcm.so\n"
"plugin = plugins/cps/cps.so\n"
"cli_cmd = claude -p\n"
"system_prompt = sei knapp\n");
sp_config_init(&c);
sp_buf_clear(&err);
CHECK(sp_config_load(&c, path, &err) == 0);
CHECK(strcmp(c.endpoint,
"http://127.0.0.1:8080/v1/chat/completions") == 0);
CHECK(strcmp(c.model, "qwen-test") == 0); /* trimmed */
CHECK(strcmp(c.theme, "Broica") == 0);
CHECK(c.autonomous == 1);
CHECK(c.steps == 12);
CHECK(c.max_bytes == 4096);
CHECK(c.nplugins == 2);
CHECK(strcmp(c.plugins[1], "plugins/cps/cps.so") == 0);
CHECK(strcmp(c.cli_cmd, "claude -p") == 0);
CHECK(strcmp(c.system_prompt, "sei knapp") == 0);
/* untouched defaults survive */
CHECK(strcmp(c.key_env, "SPLINTER_API_KEY") == 0);
/* ---- an unknown key is refused, with its line number ---- */
write_file(path, "endpoint = x\nwolke = 7\n");
sp_config_init(&c);
sp_buf_clear(&err);
CHECK(sp_config_load(&c, path, &err) == -1);
CHECK(strstr(err.p, "config") != NULL);
CHECK(strstr(err.p, ":2:") != NULL);
CHECK(strstr(err.p, "wolke") != NULL);
/* ---- a line without '=' is refused too ---- */
write_file(path, "endpoint\n");
sp_config_init(&c);
sp_buf_clear(&err);
CHECK(sp_config_load(&c, path, &err) == -1);
CHECK(strstr(err.p, "no '='") != NULL);
/* ---- a missing file says so, distinctly ---- */
sp_config_init(&c);
sp_buf_clear(&err);
CHECK(sp_config_load(&c, "/nonexistent/splinter/config", &err) == -1);
CHECK(strstr(err.p, "not found") != NULL);
/* ---- the example is valid: writing it and reading it back must work ---- */
snprintf(path, sizeof(path), "%s/sub/example", dir);
sp_buf_clear(&err);
CHECK(sp_config_write_example(path, &err) == 0);
sp_config_init(&c);
sp_buf_clear(&err);
CHECK(sp_config_load(&c, path, &err) == 0);
CHECK(strcmp(c.theme, "Delphinium") == 0);
CHECK(c.steps == 8);
CHECK(c.autonomous == 0);
/* ---- the path honours SPLINTER_CONFIG ---- */
setenv("SPLINTER_CONFIG", "/tmp/from-env", 1);
{
char buf[256];
CHECK(sp_config_path(buf, sizeof(buf)) == 0);
CHECK(strcmp(buf, "/tmp/from-env") == 0);
}
unsetenv("SPLINTER_CONFIG");
sp_buf_free(&err);
if (failures == 0)
printf("test_config: ok\n");
return failures == 0 ? 0 : 1;
}