Files
splinter/tests/test_config.c
T
Konrad d8c94edb55 splinter: providers from the environment, prio and max_parallel
OLLAMA_KEY, OLLAMA_KEY_0.._N and CLAUDE_CODE_OAUTH_TOKEN, _0.._N are enough to
fill the harness: every Ollama key becomes a delegate and an exit in the ring,
every Claude token becomes a cli delegate running 'claude -p'. Several keys mean
several accounts; the ring walks them.

That is the workflow the tool exists for - a strong model plans and defines the
work, the local models implement it, delegate is the handover:

  OLLAMA_KEY=... CLAUDE_CODE_OAUTH_TOKEN=... splinter --choose 'plane die arbeit'
  claude-3   score=4   prio=10   task mentions 'plan'
  OLLAMA_KEY=... splinter --choose 'implementier das'
  ollama-2   score=3   prio=0    task mentions 'implement'

Delegates also take prio (tie-break, higher wins) and max_parallel (default 1,
a busy delegate says so instead of queueing silently). kind=cli with cmd=... runs
a program instead of an endpoint.

Keys travel in the environment, never on a command line or into a log.
test_config covers the scan, test_delegate the prio tie-break.
2026-11-08 14:05:48 +00:00

207 lines
5.5 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 <sys/stat.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);
/* ---- providers from the environment ---- */
{
struct sp_config c;
sp_config_init(&c);
(void)setenv("OLLAMA_KEY", "k1", 1);
(void)setenv("OLLAMA_KEY_0", "k2", 1);
(void)setenv("OLLAMA_KEY_1", "k3", 1);
(void)setenv("CLAUDE_CODE_OAUTH_TOKEN", "t1", 1);
CHECK(sp_config_scan_env(&c) == 4);
CHECK(c.ndelegates == 4);
CHECK(c.nexits == 3); /* one exit per Ollama key */
CHECK(strcmp(c.delegate[3].kind, "cli") == 0);
CHECK(c.delegate[3].prio > 0); /* the planner ranks higher */
CHECK(strcmp(c.exit[0].endpoint,
"https://ollama.com/v1/chat/completions") == 0);
(void)unsetenv("OLLAMA_KEY");
(void)unsetenv("OLLAMA_KEY_0");
(void)unsetenv("OLLAMA_KEY_1");
(void)unsetenv("CLAUDE_CODE_OAUTH_TOKEN");
}
/* ---- drop-ins: a directory next to the config, read in name order ---- */
{
char dir[512], main_c[600], drop[600];
FILE *f;
const char *tmp = getenv("TMPDIR");
if (tmp == NULL || *tmp == '\0')
tmp = "/var/tmp";
snprintf(dir, sizeof(dir), "%s/splinter-conf-XXXXXX", tmp);
if (mkdtemp(dir) != NULL) {
struct sp_config c;
struct sp_buf e;
snprintf(main_c, sizeof(main_c), "%s/splinter.conf", dir);
write_file(main_c, "theme = Delphinium\n");
snprintf(drop, sizeof(drop), "%s/splinter.conf.d", dir);
if (mkdir(drop, 0700) == 0) {
char one[700], two[700];
snprintf(one, sizeof(one), "%s/50-late.conf", drop);
snprintf(two, sizeof(two), "%s/10-early.conf", drop);
write_file(one, "theme = Spider\n");
write_file(two, "locale = de\n");
/* a file that is not .conf must be ignored */
{
char other[700];
snprintf(other, sizeof(other),
"%s/ignored.txt", drop);
write_file(other, "kaputt = 1\n");
}
}
sp_config_init(&c);
if (sp_buf_init(&e) == 0) {
CHECK(sp_config_load(&c, main_c, &e) == 0);
/* 50-late.conf is read after the main file */
CHECK(strcmp(c.theme, "Spider") == 0);
CHECK(strcmp(c.locale, "de") == 0);
sp_buf_free(&e);
}
f = NULL;
(void)f;
{
char cmd[700];
snprintf(cmd, sizeof(cmd), "rm -rf '%s'", dir);
if (system(cmd) != 0)
fprintf(stderr, "cleanup failed\n");
}
}
}
if (failures == 0)
printf("test_config: ok\n");
return failures == 0 ? 0 : 1;
}