Files
splinter/tests/test_provstate.c
T
lotherk 0576d21924 open points: tables, counted site, and the tests the list asked for
Markdown tables are drawn as columns now (src/md.c, test_md, and a case in
test_ui.sh that checks the screen). The site counts lines, tools, tests,
plugins and palettes from the tree instead of naming them; the binary-size
figure is gone because it aged with every build.

The delegate panel cycle has a test (host + tasks plugin, widget says
delegate:panel exit 0), and the turn loop is driven with lcm at 8 KiB and
32 KiB budgets, so compaction is measured rather than claimed.

swarm, provstate and planner get smoke tests. TODO.md is rewritten against
the tree: the step limit was the old default (8 -> 150), skills and memory
were already built, and the open points are claude exit 2, a real Telegram
approval, A2A, the npm leftovers and the scheduler/job tests.
2026-11-09 22:46:25 +00:00

92 lines
2.9 KiB
C

/*
* splinter -- shared provider/key state.
*
* The state is what makes two splinter processes agree that a key is out of
* quota: one writes, the other reads, and the answer is the same. HOME points
* at a fresh directory, so the test never touches the real ~/.splinter.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <time.h>
#include <unistd.h>
#include "provstate.h"
static int failures;
#define CHECK(cond) do { \
if (!(cond)) { \
fprintf(stderr, "FAIL %s:%d: %s\n", __FILE__, __LINE__, #cond); \
failures++; \
} \
} while (0)
int
main(void)
{
char dir[] = "/tmp/splinter-prov-XXXXXX";
char path[700], fp1[32], fp2[32];
struct sp_prov_state st;
struct stat sb;
long now, ready = 0;
char reason[128];
CHECK(mkdtemp(dir) != NULL);
CHECK(setenv("HOME", dir, 1) == 0);
now = (long)time(NULL);
/* no state yet: admit */
CHECK(sp_provstate_load("openai", "key-a", &st) != 0);
CHECK(sp_provstate_preflight("openai", "key-a", now, &ready, reason,
sizeof(reason)) == 0);
CHECK(ready == 0);
/* a 429 with Retry-After becomes a quota wait both processes can see */
sp_provstate_account("openai", "key-a", 429, 10, 20, 3600);
CHECK(sp_provstate_load("openai", "key-a", &st) == 0);
CHECK(st.last_status == 429);
CHECK(st.req_count == 1);
CHECK(st.tok_in == 10 && st.tok_out == 20);
CHECK(st.quota_ready_at > now && st.quota_ready_at <= now + 3601);
CHECK(sp_provstate_preflight("openai", "key-a", now, &ready, reason,
sizeof(reason)) == 1);
CHECK(ready == st.quota_ready_at);
CHECK(strstr(reason, "quota") != NULL);
/* the state file is private */
snprintf(path, sizeof(path), "%s/.splinter/providers/openai", dir);
CHECK(stat(path, &sb) == 0);
CHECK((sb.st_mode & 077) == 0);
CHECK(sp_provstate_base("openai", "key-a", path, sizeof(path)) != NULL);
/* a 503 is a short backoff, on its own key */
sp_provstate_account("openai", "key-b", 503, 0, 0, 30);
CHECK(sp_provstate_load("openai", "key-b", &st) == 0);
CHECK(st.backoff_until > now);
CHECK(sp_provstate_preflight("openai", "key-b", now, &ready, reason,
sizeof(reason)) == 2);
CHECK(strstr(reason, "backoff") != NULL);
/* keys are separate: another key is still admitted */
CHECK(sp_provstate_preflight("openai", "key-c", now, &ready, reason,
sizeof(reason)) == 0);
/* the fingerprint is stable and does not contain the secret */
CHECK(sp_key_fingerprint("secret-one", fp1, sizeof(fp1)) != NULL);
CHECK(sp_key_fingerprint("secret-one", fp2, sizeof(fp2)) != NULL);
CHECK(strcmp(fp1, fp2) == 0);
CHECK(strstr(fp1, "secret") == NULL);
CHECK(sp_key_fingerprint("secret-two", fp2, sizeof(fp2)) != NULL);
CHECK(strcmp(fp1, fp2) != 0);
CHECK(sp_key_fingerprint("", fp2, sizeof(fp2)) == NULL);
snprintf(path, sizeof(path), "rm -rf '%s'", dir);
(void)system(path);
if (failures == 0)
printf("test_provstate: ok\n");
return failures == 0 ? 0 : 1;
}