Files
splinter/tests/test_config.c
T
lotherkandClaude Opus 5.5 75d8beeaa4 delegates and cli_env: hardcoded environment per cli program; swarm, provider, planner, scheduler, tmux
env=NAME=VALUE on a delegate line (repeatable) is set in the child right
before exec and beats what the parent exported, so two delegates can run
the same cli with two accounts. Remotely it goes in front as
`env 'NAME=VALUE'`, quoted like the rest. cli_env = NAME=VALUE does the
same for the main cli transport. A bad name is an error with the line.

Also in this commit, the work in progress from the tree: swarm and
provider state, the provider/scheduler/planner/tmux plugins, the ui test,
whose tmux session name is now per run so parallel runs do not collide.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-09 19:15:48 +00:00

271 lines
7.6 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"
"cli_env = CLAUDE_CODE_OAUTH_TOKEN=tok=with=eq\n"
"delegate = c2 | zweites Konto | kind=cli | cmd=claude -p | "
"env=CLAUDE_CODE_OAUTH_TOKEN=abc | env=FOO=bar baz\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(c.ncli_env == 1);
CHECK(strcmp(c.cli_env[0], "CLAUDE_CODE_OAUTH_TOKEN=tok=with=eq") == 0);
CHECK(c.ndelegates == 1);
CHECK(c.delegate[0].nenv == 2);
CHECK(strcmp(c.delegate[0].env[0], "CLAUDE_CODE_OAUTH_TOKEN=abc") == 0);
CHECK(strcmp(c.delegate[0].env[1], "FOO=bar baz") == 0);
sp_config_apply_env((const char (*)[SP_CONFIG_ENV_LEN])c.delegate[0].env,
c.delegate[0].nenv);
CHECK(getenv("FOO") != NULL && strcmp(getenv("FOO"), "bar baz") == 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);
/* ---- env without a proper NAME= is refused ---- */
write_file(path, "cli_env = 1BAD=x\n");
sp_config_init(&c);
sp_buf_clear(&err);
CHECK(sp_config_load(&c, path, &err) == -1);
CHECK(strstr(err.p, "NAME=VALUE") != 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 == 150);
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);
/*
* Environment delegates are opt-in: a token in the environment must not
* add an entry that competes for work nobody assigned it.
*/
CHECK(sp_config_scan_env(&c) == 3);
CHECK(c.ndelegates == 3);
CHECK(c.nexits == 3); /* one exit per Ollama key */
c.env_delegates = 1;
{
struct sp_config c2;
sp_config_init(&c2);
c2.env_delegates = 1;
CHECK(sp_config_scan_env(&c2) == 4);
CHECK(c2.ndelegates == 4);
CHECK(strcmp(c2.delegate[3].kind, "cli") == 0);
CHECK(strcmp(c2.delegate[3].id, "claude") == 0);
CHECK(c2.delegate[3].prio > 0);
}
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");
}
/*
* ---- a full exit table keeps the operator's keys ----
* With no room for another exit, an OLLAMA_KEY used to overwrite the
* key_env of the last configured exit: that key then went to an
* unrelated endpoint.
*/
{
struct sp_config c;
size_t k;
sp_config_init(&c);
for (k = 0; k < SP_CONFIG_MAX_DELEGATES; k++) {
snprintf(c.exit[k].id, sizeof(c.exit[k].id), "mine-%zu", k);
snprintf(c.exit[k].key_env, sizeof(c.exit[k].key_env),
"MY_KEY_%zu", k);
}
c.nexits = SP_CONFIG_MAX_DELEGATES;
(void)setenv("OLLAMA_KEY", "k1", 1);
(void)sp_config_scan_env(&c);
CHECK(c.nexits == SP_CONFIG_MAX_DELEGATES);
{
char want[64];
snprintf(want, sizeof(want), "MY_KEY_%d",
SP_CONFIG_MAX_DELEGATES - 1);
CHECK(strcmp(c.exit[SP_CONFIG_MAX_DELEGATES - 1].key_env,
want) == 0);
}
(void)unsetenv("OLLAMA_KEY");
}
/* ---- 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;
}