Files
splinter/tests/test_focus.c
Konrad 554b4c97b8 splinter: tool calls with arguments, newline-aware rows, complete i18n
Four things, three of them reported by the operator.

1. Rows are counted from the real line structure. The old arithmetic
   (strlen/cols) assumed one long line; a model's reasoning is full of '\n', so
   the true height was far larger than the estimate and the text painted over
   the answer below it - 'it just goes anywhere on the screen'. text_rows() and
   row_offset() split on newlines first, then wrap. entry_lines() now measures
   the string that is actually drawn ('role: text'), which also fixes the last
   character of an entry being cut off (a truncated code fence).

2. Tool calls are visible: the agent's tool_begin hook fires before each call,
   the UI drains the list under its lock and writes 'werkzeug: run
   {"command":"echo a"}' into the transcript; the one at the top of the live
   area shows the call while it runs. The reasoning entry is inserted before
   those lines, so the transcript reads du, denkt, werkzeug, modell.

3. The chat pane has a black background, and the status line sits between the
   pane and the prompt, the way Hermes shows it.

4. i18n is complete: every remaining literal in ui.c, main.c and the plugins
   goes through the catalogue, including widget texts and format strings.
   sp_td() carries a compiled-in English default, so a missing catalogue can
   never drop a printf placeholder. tools/check_i18n.py runs in make check and
   fails when a key used in the code is missing from any catalogue, or when a
   catalogue key is used nowhere.

Seventeen test binaries green.
2026-11-08 08:11:09 +00:00

169 lines
4.6 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"
#include "i18n.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_buf ierr;
/* the plugin speaks through the catalogue; load the English one */
if (sp_buf_init(&ierr) == 0) {
sp_i18n_add_dir("share/locale");
(void)sp_i18n_load(&ierr, "en");
sp_buf_free(&ierr);
}
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;
}