Files
splinter/tests/test_focus.c
T
Konrad 8fa3e9d7bb splinter: /goal as a command, and widgets that are actually alive
The goal is not configuration: it belongs to the conversation in front of you.
So it moved out of the config file and became a command.

- commands are an ABI now: sp_host_add_command / sp_host_command, so the UI
  never needs to know a plugin by name. A leading '/' is never sent to the
  model; an unknown command lists what exists.
- works in both modes: '/goal <text>' in the TUI and
  'splinter -p "/goal ..."' (exit 0 on success, 2 on an unknown command).
- focus registers /goal (set, show when empty of arguments) and /recap (force
  a reminder into the next turn, regardless of the five-turn cadence).

Fixed while testing it: the UI only repainted on a keypress, so a plugin widget
that ticks was frozen on screen - the clock showed the time from when the window
opened. The input read now times out after 500 ms and that timeout repaints the
sidebar and status line: clock, focus and cps are alive while you do nothing.

Fifteen test binaries green; test_focus covers the command path too: set, show,
force a recap outside the cadence, refuse an unknown command.
2026-11-07 20:50:58 +00:00

159 lines
4.3 KiB
C

/*
* splinter -- focus plugin tests.
*
* The rule under test: a reminder every N turns, inserted before the newest
* user message, never twice in the same turn, and nothing at all when no goal
* is configured. The turn counter is the host's, driven here directly.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "splinter.h"
#include "splinter_plugin.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
count_focus(const struct sp_conv *c)
{
size_t i;
int n = 0;
for (i = 0; i < c->n; i++)
if (c->v[i].content != NULL &&
strstr(c->v[i].content, "[focus]") != NULL)
n++;
return n;
}
int
main(void)
{
struct sp_registry reg;
struct sp_agent ag;
struct sp_host *host;
struct sp_buf err;
struct sp_conv conv;
memset(&reg, 0, sizeof(reg));
memset(&ag, 0, sizeof(ag));
if (sp_buf_init(&err) != 0)
return 1;
/* ---- no goal configured: the plugin stays quiet ---- */
unsetenv("SPLINTER_FOCUS_GOAL");
host = sp_host_new(&reg, &ag, NULL);
CHECK(host != NULL);
CHECK(sp_plugin_load(host, "plugins/focus/focus.so", &err) == 0);
sp_conv_init(&conv);
CHECK(sp_conv_push(&conv, "user", NULL, "hallo") == 0);
sp_host_turn(host);
CHECK(sp_host_compact(host, &conv, 0) == 0);
CHECK(count_focus(&conv) == 0);
sp_conv_free(&conv);
sp_host_free(host);
/* ---- with a goal: one reminder every two turns ---- */
setenv("SPLINTER_FOCUS_GOAL", "Ship the config file", 1);
setenv("SPLINTER_FOCUS_EVERY", "2", 1);
host = sp_host_new(&reg, &ag, NULL);
CHECK(host != NULL);
CHECK(sp_plugin_load(host, "plugins/focus/focus.so", &err) == 0);
sp_conv_init(&conv);
CHECK(sp_conv_push(&conv, "system", NULL, "du bist knapp") == 0);
CHECK(sp_conv_push(&conv, "user", NULL, "erster turn") == 0);
sp_host_turn(host);
CHECK(sp_host_compact(host, &conv, 0) == 0);
CHECK(count_focus(&conv) == 0);
/* turn 2: the reminder appears, once */
CHECK(sp_conv_push(&conv, "user", NULL, "zweiter turn") == 0);
sp_host_turn(host);
CHECK(sp_host_compact(host, &conv, 1) == 1);
CHECK(count_focus(&conv) == 1);
CHECK(strstr(conv.v[conv.n - 1].content, "zweiter turn") != NULL);
{
size_t i;
/* it sits before the newest user message, not after it */
for (i = 0; i < conv.n; i++)
if (conv.v[i].content != NULL &&
strstr(conv.v[i].content, "[focus]") != NULL)
CHECK(i < conv.n - 1);
}
CHECK(strstr(conv.v[1].content, "Ship the config file") != NULL ||
strstr(conv.v[2].content, "Ship the config file") != NULL);
/* compacting again in the same turn must not add a second one */
CHECK(sp_host_compact(host, &conv, 1) == 0);
CHECK(count_focus(&conv) == 1);
/* turn 3: silent */
CHECK(sp_conv_push(&conv, "user", NULL, "dritter turn") == 0);
sp_host_turn(host);
CHECK(sp_host_compact(host, &conv, 1) == 0);
CHECK(count_focus(&conv) == 1);
/* turn 4: second reminder */
CHECK(sp_conv_push(&conv, "user", NULL, "vierter turn") == 0);
sp_host_turn(host);
CHECK(sp_host_compact(host, &conv, 1) == 1);
CHECK(count_focus(&conv) == 2);
/* ---- /goal sets it at runtime, /recap forces the next reminder ---- */
{
struct sp_buf out;
CHECK(sp_buf_init(&out) == 0);
sp_conv_free(&conv);
sp_conv_init(&conv);
CHECK(sp_conv_push(&conv, "user", NULL, "turn fuenf") == 0);
sp_buf_clear(&out);
CHECK(sp_host_command(host, "goal", "", &out) == 0);
CHECK(strstr(out.p, "Ship the config file") != NULL);
sp_buf_clear(&out);
CHECK(sp_host_command(host, "goal", "neues ziel", &out) == 0);
CHECK(strstr(out.p, "neues ziel") != NULL);
/* a forced recap lands on the next turn regardless of the counter */
sp_buf_clear(&out);
CHECK(sp_host_command(host, "recap", "", &out) == 0);
sp_host_turn(host);
{
int before = count_focus(&conv);
CHECK(sp_host_compact(host, &conv, 1) == 1);
CHECK(count_focus(&conv) == before + 1);
CHECK(strstr(conv.v[conv.n - 1].content, "turn fuenf") != NULL);
}
/* an unknown command is refused and says so */
sp_buf_clear(&out);
CHECK(sp_host_command(host, "gibtsnicht", "", &out) == -1);
CHECK(strstr(out.p, "unknown command") != NULL);
sp_buf_free(&out);
}
sp_conv_free(&conv);
sp_host_free(host);
sp_buf_free(&err);
sp_registry_free(&reg);
if (failures == 0)
printf("test_focus: ok\n");
return failures == 0 ? 0 : 1;
}