Files
splinter/tests/test_providers.c
T
lotherk 76c9007c02 config: a core default is a choice, not something a main endpoint overrides
load_config() copied the config's main endpoint into the command-line
options, so apply_selection() saw o->endpoint != NULL and skipped
core { default = ... }. Every config with a main endpoint (and every
/model drop-in, which wrote exactly that) silently ran the main endpoint
instead of the default; a drop-in with model = provider/model even put a
slash-name into the model field.

- The main endpoint is applied only when no -m and no default exist.
  -m and a command-line --endpoint still win.
- /model saves a delegate choice as "default = ID[/MODEL]" instead of a
  second main endpoint; the provider block supplies kind, endpoint and
  key. A model of the main endpoint has no provider block, so that one
  keeps the full endpoint/model/api_key_env form.
- docs/CONFIG.md, README and test_providers describe the order and the
  new drop-in line.

Verified with a fake endpoint: core default -> mb, -m a -> ma, main-only
config -> mmain, a stale endpoint/model drop-in no longer shadows the
default, a "default = a" drop-in does.
make check: all tests passed.
2026-11-16 16:27:03 +00:00

342 lines
12 KiB
C

/*
* splinter -- the provider list and /connect's config line.
*
* /model shows what the config already says and /connect adds a line to
* <config>.d/20-connect.conf. The line has to survive a restart, so the test
* writes it, loads the config again and looks at the delegate it produced.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>
#include "config.h"
#include "providers.h"
#include "splinter.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
base(struct sp_config *cfg)
{
struct sp_delegate *d;
memset(cfg, 0, sizeof(*cfg));
sp_config_init(cfg);
snprintf(cfg->endpoint, sizeof(cfg->endpoint),
"http://127.0.0.1:8080/v1/chat/completions");
snprintf(cfg->model, sizeof(cfg->model), "local");
snprintf(cfg->key_env, sizeof(cfg->key_env), "LOCAL_KEY");
d = &cfg->delegate[cfg->ndelegates++];
snprintf(d->id, sizeof(d->id), "far");
snprintf(d->kind, sizeof(d->kind), "openai");
snprintf(d->endpoint, sizeof(d->endpoint),
"https://api.example/v1/chat/completions");
snprintf(d->model, sizeof(d->model), "big");
snprintf(d->key_env, sizeof(d->key_env), "FAR_KEY");
/* a cli delegate has no endpoint: it is not a model to switch to */
d = &cfg->delegate[cfg->ndelegates++];
snprintf(d->id, sizeof(d->id), "claude");
snprintf(d->kind, sizeof(d->kind), "cli");
snprintf(d->cmd, sizeof(d->cmd), "claude -p");
/* a delegate without a model is not one either */
d = &cfg->delegate[cfg->ndelegates++];
snprintf(d->id, sizeof(d->id), "empty");
snprintf(d->endpoint, sizeof(d->endpoint), "https://x/v1/chat/completions");
snprintf(cfg->exit[cfg->nexits].id, sizeof(cfg->exit[0].id), "groq");
snprintf(cfg->exit[cfg->nexits].endpoint,
sizeof(cfg->exit[0].endpoint), "https://api.groq.com/openai/v1/chat/completions");
snprintf(cfg->exit[cfg->nexits].model, sizeof(cfg->exit[0].model),
"llama");
cfg->nexits++;
}
int
main(void)
{
struct sp_config cfg, back;
struct sp_provider p;
struct sp_buf err;
char dir[512], drop[600], mainconf[700], path[800];
char line[600];
const char *tmp = getenv("TMPDIR");
FILE *f;
size_t idx;
int nlines = 0;
char buf[1024];
if (tmp == NULL || *tmp == '\0')
tmp = "/var/tmp";
snprintf(dir, sizeof(dir), "%s/splinter-prov-XXXXXX", tmp);
CHECK(mkdtemp(dir) != NULL);
/* the loader reads <config>.d, so /connect writes there too */
snprintf(drop, sizeof(drop), "%s/splinter.conf.d", dir);
base(&cfg);
CHECK(sp_provider_count(&cfg) == 3);
CHECK(sp_provider_at(&cfg, 0, &p) == 0);
CHECK(strcmp(p.id, "main") == 0 && p.main == 1);
CHECK(strcmp(p.model, "local") == 0);
CHECK(sp_provider_at(&cfg, 1, &p) == 0);
CHECK(strcmp(p.id, "far") == 0 && strcmp(p.model, "big") == 0);
CHECK(sp_provider_at(&cfg, 2, &p) == 0);
CHECK(strcmp(p.id, "groq") == 0);
CHECK(sp_provider_at(&cfg, 3, &p) != 0);
CHECK(sp_provider_find(&cfg, "far", &idx) == 1 && idx == 1);
CHECK(sp_provider_find(&cfg, "nope", &idx) == 0);
/* add: one line, and the config remembers it */
CHECK(sp_buf_init(&err) == 0);
CHECK(sp_provider_add(&cfg, drop, "ocgo",
"https://opencode.ai/zen/go/v1/chat/completions",
"deepseek-v4.1-flash", "OPENCODE_GO_API_KEY", &err) == 0);
CHECK(sp_provider_count(&cfg) == 4);
CHECK(sp_provider_find(&cfg, "ocgo", &idx) == 1);
snprintf(path, sizeof(path), "%s/20-connect.conf", drop);
f = fopen(path, "r");
CHECK(f != NULL);
if (f != NULL) {
while (fgets(buf, sizeof(buf), f) != NULL)
if (strstr(buf, "delegate =") != NULL)
nlines++;
fclose(f);
}
CHECK(nlines == 1);
/* a second add replaces the line instead of growing the file */
CHECK(sp_provider_add(&cfg, drop, "ocgo",
"https://opencode.ai/zen/go/v1/chat/completions",
"other-model", "", &err) == 0);
nlines = 0;
f = fopen(path, "r");
if (f != NULL) {
while (fgets(buf, sizeof(buf), f) != NULL)
if (strstr(buf, "delegate =") != NULL)
nlines++;
fclose(f);
}
CHECK(nlines == 1);
CHECK(sp_provider_find(&cfg, "ocgo", &idx) == 1);
CHECK(sp_provider_at(&cfg, idx, &p) == 0);
CHECK(strcmp(p.model, "other-model") == 0 && p.key_env[0] == '\0');
/* the written line is a line the loader accepts */
snprintf(mainconf, sizeof(mainconf), "%s/splinter.conf", dir);
f = fopen(mainconf, "w");
CHECK(f != NULL);
if (f != NULL) {
fputs("endpoint = http://127.0.0.1:8080/v1/chat/completions\n"
"model = local\n", f);
fclose(f);
}
memset(&back, 0, sizeof(back));
sp_config_init(&back);
CHECK(sp_config_load(&back, mainconf, &err) == 0);
{
const struct sp_delegate *d = NULL;
size_t i;
for (i = 0; i < back.ndelegates; i++)
if (strcmp(back.delegate[i].id, "ocgo") == 0)
d = &back.delegate[i];
CHECK(d != NULL);
if (d != NULL) {
CHECK(strcmp(d->model, "other-model") == 0);
CHECK(strcmp(d->endpoint,
"https://opencode.ai/zen/go/v1/chat/completions") == 0);
/* no kind= in the line means openai */
CHECK(d->kind[0] == '\0' || strcmp(d->kind, "openai") == 0);
}
}
/* /model writes 90-model.conf; picking main removes it again */
{
struct sp_provider pick;
char mpath[800], mbuf[1024];
struct sp_buf merr;
FILE *mf;
memset(&pick, 0, sizeof(pick));
snprintf(pick.id, sizeof(pick.id), "ocgo");
snprintf(pick.endpoint, sizeof(pick.endpoint),
"https://opencode.ai/zen/go/v1/chat/completions");
snprintf(pick.model, sizeof(pick.model), "other-model");
snprintf(pick.key_env, sizeof(pick.key_env), "OPENCODE_GO_API_KEY");
sp_buf_init(&merr);
CHECK(sp_provider_save_choice(&back, &pick, &merr) == 0);
snprintf(mpath, sizeof(mpath), "%s.d/90-model.conf", mainconf);
mf = fopen(mpath, "r");
CHECK(mf != NULL);
if (mf != NULL) {
mbuf[0] = '\0';
(void)fread(mbuf, 1, sizeof(mbuf) - 1, mf);
mbuf[sizeof(mbuf) - 1] = '\0';
fclose(mf);
/* the choice is a default; the provider carries the rest */
CHECK(strstr(mbuf, "default = ocgo/other-model") != NULL);
CHECK(strstr(mbuf, "endpoint =") == NULL);
CHECK(strstr(mbuf, "model =") == NULL);
}
{
struct sp_config again;
struct sp_buf rerr;
memset(&again, 0, sizeof(again));
sp_config_init(&again);
CHECK(sp_buf_init(&rerr) == 0);
CHECK(sp_config_load(&again, mainconf, &rerr) == 0);
CHECK(strcmp(again.default_id, "ocgo/other-model") == 0);
sp_buf_free(&rerr);
}
memset(&pick, 0, sizeof(pick));
pick.main = 1;
CHECK(sp_provider_save_choice(&back, &pick, &merr) == 0);
CHECK(access(mpath, F_OK) != 0);
sp_buf_free(&merr);
}
CHECK(sp_provider_line(line, sizeof(line), "ocgo", "https://x/v1",
"m", "K") == 0);
CHECK(strstr(line, "delegate = ocgo |") != NULL);
CHECK(strstr(line, "key_env=K") != NULL);
sp_buf_free(&err);
/*
* A selector as one provider: a pool picks a member, "pool/model" picks
* the member and keeps the model, and a bare id is itself. This is what
* -m and /model accept, so the kind travels with the choice: a pool of
* anthropic members must not be applied as an OpenAI endpoint.
*/
{
struct sp_provider sel;
char model[64];
cfg.ndelegates++;
snprintf(cfg.delegate[3].id, 32, "api0");
snprintf(cfg.delegate[3].kind, 16, "anthropic");
snprintf(cfg.delegate[3].endpoint, 512,
"https://api.anthropic.com/v1/messages");
snprintf(cfg.delegate[3].model, 256, "claude-sonnet-5-5");
cfg.ndelegates++;
snprintf(cfg.delegate[4].id, 32, "api1");
snprintf(cfg.delegate[4].kind, 16, "anthropic");
snprintf(cfg.delegate[4].endpoint, 512,
"https://api.anthropic.com/v1/messages");
snprintf(cfg.delegate[4].model, 256, "claude-sonnet-5-5");
snprintf(cfg.pool[cfg.npools].id, 32, "accounts");
snprintf(cfg.pool[cfg.npools].strategy, 16, "balance");
snprintf(cfg.pool[cfg.npools].member[0], 32, "api0");
snprintf(cfg.pool[cfg.npools].member[1], 32, "api1");
cfg.pool[cfg.npools].nmembers = 2;
cfg.npools++;
CHECK(sp_provider_select(&cfg, "accounts", &sel, model,
sizeof(model)) == 0);
CHECK(strcmp(sel.kind, "anthropic") == 0);
CHECK(model[0] != '\0');
CHECK(sp_provider_select(&cfg, "accounts/opus-5-5", &sel, model,
sizeof(model)) == 0);
CHECK(strcmp(model, "opus-5-5") == 0);
CHECK(sp_provider_select(&cfg, "api0", &sel, model,
sizeof(model)) == 0);
CHECK(strcmp(sel.id, "api0") == 0);
CHECK(sp_provider_select(&cfg, "nope", &sel, model,
sizeof(model)) != 0);
}
/*
* A saved anthropic choice is a default line too. The kind stays in
* the provider block that the default names; writing it into the
* drop-in as a second main endpoint was the old, shadowing way.
*/
{
struct sp_provider pick;
struct sp_buf merr;
char mp[800], mb[1024];
FILE *mf;
memset(&pick, 0, sizeof(pick));
snprintf(pick.id, sizeof(pick.id), "api0");
snprintf(pick.kind, sizeof(pick.kind), "anthropic");
snprintf(pick.endpoint, sizeof(pick.endpoint),
"https://api.anthropic.com/v1/messages");
snprintf(pick.model, sizeof(pick.model), "claude-sonnet-5-5");
snprintf(pick.key_env, sizeof(pick.key_env), "CLAUDE_TOKEN");
CHECK(sp_buf_init(&merr) == 0);
CHECK(sp_provider_save_choice(&back, &pick, &merr) == 0);
snprintf(mp, sizeof(mp), "%s.d/90-model.conf", mainconf);
mf = fopen(mp, "r");
CHECK(mf != NULL);
if (mf != NULL) {
CHECK(fgets(mb, sizeof(mb), mf) != NULL);
CHECK(fgets(mb, sizeof(mb), mf) != NULL);
CHECK(strcmp(mb, "default = api0/claude-sonnet-5-5\n") ==
0);
fclose(mf);
}
sp_buf_free(&merr);
}
/* what /connect must refuse before it becomes a line nobody can read */
CHECK(sp_provider_id_ok("ocgo") == 1);
CHECK(sp_provider_id_ok("my-prov_2") == 1);
CHECK(sp_provider_id_ok("Main") == 0);
CHECK(sp_provider_id_ok("main") == 0);
CHECK(sp_provider_id_ok("a b") == 0);
CHECK(sp_provider_id_ok("") == 0);
CHECK(sp_provider_endpoint_ok("http://127.0.0.1:11434/v1/chat/completions") == 1);
/* the config names the base; every route is built from it */
{
char u[256];
CHECK(strcmp(sp_endpoint_url("https://h/v1", "chat/completions",
u, sizeof(u)), "https://h/v1/chat/completions") == 0);
CHECK(strcmp(sp_endpoint_url("https://h/v1/", "models", u,
sizeof(u)), "https://h/v1/models") == 0);
CHECK(strcmp(sp_endpoint_url("https://h/v1/chat/completions",
"models", u, sizeof(u)), "https://h/v1/models") == 0);
CHECK(strcmp(sp_endpoint_url("https://h/v1/chat/completions",
"chat/completions", u, sizeof(u)),
"https://h/v1/chat/completions") == 0);
CHECK(strcmp(sp_endpoint_url("https://api.anthropic.com/v1/messages",
"models", u, sizeof(u)),
"https://api.anthropic.com/v1/models") == 0);
CHECK(strcmp(sp_endpoint_url("https://h/v1/messages", NULL, u,
sizeof(u)), "https://h/v1") == 0);
/* the model list of the three providers Konrad uses */
CHECK(strcmp(sp_endpoint_url("https://opencode.ai/zen/go/v1",
"models", u, sizeof(u)),
"https://opencode.ai/zen/go/v1/models") == 0);
CHECK(strcmp(sp_endpoint_url("https://ollama.com/v1",
"models", u, sizeof(u)),
"https://ollama.com/v1/models") == 0);
CHECK(strcmp(sp_endpoint_url("https://api.anthropic.com/v1",
"models", u, sizeof(u)),
"https://api.anthropic.com/v1/models") == 0);
}
CHECK(sp_provider_endpoint_ok("https://x/v1") == 1);
CHECK(sp_provider_endpoint_ok("x/v1") == 0);
CHECK(sp_provider_endpoint_ok("http://x/a b") == 0);
CHECK(sp_provider_endpoint_ok("http://x/a|b") == 0);
CHECK(sp_provider_model_ok("deepseek-v4.1-flash") == 1);
CHECK(sp_provider_model_ok("") == 0);
CHECK(sp_provider_model_ok("a|b") == 0);
CHECK(sp_provider_key_ok("OPENCODE_GO_API_KEY") == 1);
CHECK(sp_provider_key_ok("") == 1);
CHECK(sp_provider_key_ok("1BAD") == 0);
CHECK(sp_provider_key_ok("has-dash") == 0);
printf("%s", failures == 0 ? "test_providers: ok\n" : "");
return failures == 0 ? 0 : 1;
}