Files
splinter/tests/test_swarm.c
T
lotherk 4cbd57d829 tree: lcm keeps the task, and the in-flight work it sits in
Everything that was uncommitted in the shared tree, in one commit because the
changes are interleaved across the same files (lcm, engine, frontend, locales).

lcm: a continuation prompt is not the task. After /resume the newest user
message is "Continue with the task ...", and pinning it kept the keystroke
while the question it continues was cut away -- measured: a resumed session
woke up with the prompt, one tool call and its error, and no task at all. The
pin now skips the harness's own continuation prompts, so the real question is
pinned again. test_compactor drives it: with the skip disabled the task is
lost (red), with it the task comes back after the summary (green).

engine: /self-check and the exact tool-argument parse are in a1794fb; this
commit carries the tests for them (a failed tool observation must appear in
the self-check prompt; mcp_parse_exact rejects two objects in one string) and
the i18n keys the new code uses: cmd.script.*, main.no_profile, tg.busy,
tg.interview.running, ui.agent_busy in all three catalogues.

The rest is the in-flight work from the shared session: engine/frontend,
plugins, telegram/a2a, and their tests.

make check: all tests passed; check_i18n ok, 204 keys in 3 catalogues.
2026-11-18 23:03:09 +00:00

131 lines
3.3 KiB
C

/*
* splinter -- swarm (local fleet) tests.
*
* Two instances in one directory, in one process: the first binds the socket
* and becomes master, the second connects as a slave. What is asserted is the
* wire cycle -- welcome, id, routed text, busy report -- not the UI.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include "swarm.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
nap_ms(long ms)
{
struct timespec ts;
ts.tv_sec = ms / 1000;
ts.tv_nsec = (ms % 1000) * 1000000L;
nanosleep(&ts, NULL);
}
int
main(void)
{
struct swarm master, slave;
char dir[] = "/tmp/splinter-swarm-XXXXXX";
char buf[512], list[512], path[600];
int i, got = 0;
CHECK(mkdtemp(dir) != NULL);
CHECK(swarm_init(&master, dir) == 0);
CHECK(master.role == SWARM_MASTER);
CHECK(master.id == 0);
CHECK(swarm_init(&slave, dir) == 0);
CHECK(slave.role == SWARM_SLAVE);
/* the master has to accept before the welcome reaches the slave */
for (i = 0; i < 50 && slave.id < 0; i++) {
swarm_poll(&master);
swarm_poll(&slave);
if (slave.id < 0)
nap_ms(10);
}
CHECK(slave.id == 1);
/* a routed line arrives in the slave's inbox and says it came from afar */
CHECK(swarm_route(&master, -1, "hallo slave") == 1);
for (i = 0; i < 50 && !got; i++) {
swarm_poll(&slave);
got = swarm_take_inbox(&slave, buf, sizeof(buf));
if (!got)
nap_ms(10);
}
CHECK(got == 1);
CHECK(strcmp(buf, "hallo slave") == 0);
CHECK(swarm_inbox_remote(&slave) == 1);
CHECK(swarm_inbox_remote(&slave) == 0); /* cleared after reading */
/* quotes, backslashes and newlines survive the wire unescaped */
got = 0;
CHECK(swarm_route(&master, 1, "say \"hi\" \\ now\nline 2") == 1);
for (i = 0; i < 50 && !got; i++) {
swarm_poll(&slave);
got = swarm_take_inbox(&slave, buf, sizeof(buf));
if (!got)
nap_ms(10);
}
CHECK(got == 1);
CHECK(strcmp(buf, "say \"hi\" \\ now\nline 2") == 0);
(void)swarm_inbox_remote(&slave);
/* the master can route to itself without a socket round trip */
CHECK(swarm_route(&master, 0, "an mich") == 0);
CHECK(swarm_take_inbox(&master, buf, sizeof(buf)) == 1);
CHECK(strcmp(buf, "an mich") == 0);
CHECK(swarm_inbox_remote(&master) == 1);
/* a busy report is visible in the master's list */
swarm_report_busy(&slave, 7);
for (i = 0; i < 50; i++) {
swarm_poll(&master);
swarm_list(&master, list, sizeof(list));
if (strstr(list, "busy=7") != NULL)
break;
nap_ms(10);
}
CHECK(strstr(list, "busy=7") != NULL);
CHECK(strstr(list, "slave pid=") != NULL);
swarm_status(&master, buf, sizeof(buf));
CHECK(strstr(buf, "master 0") != NULL);
CHECK(strstr(buf, "1 slaves") != NULL);
swarm_status(&slave, buf, sizeof(buf));
CHECK(strstr(buf, "slave 1") != NULL);
/* close the slave first: the master's socket lives longer */
swarm_close(&slave);
swarm_close(&master);
/* a directory with no fleet is standalone, not an error */
{
struct swarm solo;
CHECK(swarm_init(&solo, dir) == 0);
CHECK(solo.role == SWARM_MASTER);
swarm_close(&solo);
}
snprintf(path, sizeof(path), "rm -rf '%s'", dir);
(void)system(path);
if (failures == 0)
printf("test_swarm: ok\n");
return failures == 0 ? 0 : 1;
}