Rebuilt from scratch after the previous attempt left no code behind: what exists here is what compiles and what the tests prove, and nothing else is claimed. What is in it: - a turn loop that refuses an over-budget request before sending it, reports unknown tools instead of guessing, and honours authorization classes (read runs, mutate/destructive ask unless --autonomous); - a registry that is authoritative - the model names a tool, the registry decides whether it exists; - an append-only JSONL session (O_APPEND, fsync per record), where a crash can only truncate the tail and the tail is skipped, not repaired; - two transports: a deterministic mock (queue of replies, last request kept) and HTTP via curl(1) with the request body in a temp file; - two builtins that exist to prove the classes: echo (read) and run (destructive, reports its exit status as an observation). Five test binaries, no external framework, no network: mock driver covers the answer path, a tool round, an unknown tool, a denial, autonomy, a failing tool, the step limit, the byte budget (asserting that nothing was sent) and a non-2xx reply. The wire format is splinter's own line protocol (user/observe out, answer/tool in); provider adapters translate. That is why the loop is testable without a model, and why no vendor JSON is parsed in the core.
85 lines
1.7 KiB
C
85 lines
1.7 KiB
C
/* splinter -- registry tests. */
|
|
#include <stdio.h>
|
|
#include <string.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 int
|
|
noop(const char *args, struct sp_buf *out, void *ctx)
|
|
{
|
|
(void)args;
|
|
(void)ctx;
|
|
return sp_buf_puts(out, "noop");
|
|
}
|
|
|
|
int
|
|
main(void)
|
|
{
|
|
struct sp_registry r;
|
|
struct sp_tool t;
|
|
const struct sp_tool *found;
|
|
|
|
memset(&r, 0, sizeof(r));
|
|
memset(&t, 0, sizeof(t));
|
|
|
|
CHECK(sp_registry_count(&r) == 0);
|
|
CHECK(sp_registry_find(&r, "x") == NULL);
|
|
|
|
t.name = "alpha";
|
|
t.description = "first";
|
|
t.auth = SP_AUTH_READ;
|
|
t.run = noop;
|
|
CHECK(sp_registry_add(&r, &t) == 0);
|
|
|
|
t.name = "beta";
|
|
t.description = "second";
|
|
t.auth = SP_AUTH_MUTATE;
|
|
t.run = noop;
|
|
CHECK(sp_registry_add(&r, &t) == 0);
|
|
|
|
CHECK(sp_registry_count(&r) == 2);
|
|
|
|
found = sp_registry_find(&r, "alpha");
|
|
CHECK(found != NULL);
|
|
CHECK(found != NULL && found->auth == SP_AUTH_READ);
|
|
CHECK(found != NULL && strcmp(found->description, "first") == 0);
|
|
|
|
found = sp_registry_find(&r, "beta");
|
|
CHECK(found != NULL);
|
|
CHECK(found != NULL && found->auth == SP_AUTH_MUTATE);
|
|
|
|
CHECK(sp_registry_find(&r, "gamma") == NULL);
|
|
CHECK(sp_registry_find(&r, NULL) == NULL);
|
|
|
|
/* growth beyond the initial eight slots */
|
|
{
|
|
int i;
|
|
char name[16];
|
|
|
|
for (i = 0; i < 40; i++) {
|
|
snprintf(name, sizeof(name), "t%d", i);
|
|
t.name = name;
|
|
CHECK(sp_registry_add(&r, &t) == 0);
|
|
}
|
|
CHECK(sp_registry_count(&r) == 42);
|
|
CHECK(sp_registry_find(&r, "t39") != NULL);
|
|
}
|
|
|
|
sp_registry_free(&r);
|
|
CHECK(sp_registry_count(&r) == 0);
|
|
CHECK(r.tools == NULL);
|
|
|
|
if (failures == 0)
|
|
printf("test_tool: ok\n");
|
|
return failures == 0 ? 0 : 1;
|
|
}
|