Files
splinter/tests/test_tmux.c
T
lotherkandClaude Sonnet 5.5 478e2ec663 planner, tmux: rewrite with real JSON, validation and tests
planner: v0.2, state in plan_task_*.json via libmcp, atomic replace, refused states.
tmux: v0.3, in-process capture-pane diff, no watcher helper, tmux_ask/read/capture.
tests: test_tmux added, test_delegate_cli no longer hangs without a tty.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-11 17:13:20 +00:00

124 lines
3.7 KiB
C

/*
* splinter -- tmux plugin test.
*
* Drives a real tmux with `cat`: whatever is typed comes back twice (the tty
* echoes it, cat prints it), which is enough to prove the three things that
* matter - a tool reaches the terminal, "new output" excludes what was already
* seen, and a name or key that could be an option is refused. Without tmux the
* test says so and passes; the plugin does not load there either.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.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 const struct sp_tool *reg_tool;
static const char *
call(const struct sp_registry *reg, const char *tool, const char *args,
struct sp_buf *out)
{
const struct sp_tool *t = sp_registry_find(reg, tool);
sp_buf_clear(out);
reg_tool = t;
CHECK(t != NULL);
if (t != NULL)
(void)t->run(args, out, t->ctx);
return out->p != NULL ? out->p : "";
}
int
main(void)
{
struct sp_session_info sess;
struct sp_registry reg;
struct sp_agent ag;
struct sp_host *host;
struct sp_buf out, err;
char dir[] = "/tmp/splinter-tmux-XXXXXX";
char name[64], args[256];
const char *s;
CHECK(mkdtemp(dir) != NULL);
memset(&sess, 0, sizeof(sess));
snprintf(sess.dir, sizeof(sess.dir), "%s", dir);
memset(&reg, 0, sizeof(reg));
memset(&ag, 0, sizeof(ag));
CHECK(sp_buf_init(&out) == 0);
CHECK(sp_buf_init(&err) == 0);
host = sp_host_new(&reg, &ag, &sess);
CHECK(host != NULL);
if (sp_plugin_load(host, "plugins/tmux/tmux.so", &err) != 0) {
printf("test_tmux: skipped (no tmux)\n");
goto done;
}
CHECK(sp_registry_find(&reg, "tmux_watch") == NULL);
/* nothing is targeted yet */
s = call(&reg, "tmux_ask", "{\"text\":\"x\"}", &out);
CHECK(strstr(s, "error: no tmux target") != NULL);
/* a name that could be an option, a second target or a command */
s = call(&reg, "tmux_start", "{\"cmd\":\"cat\",\"name\":\"-t evil\"}", &out);
CHECK(strstr(s, "error: name may use") != NULL);
s = call(&reg, "tmux_start", "{\"cmd\":\"cat\",\"name\":\"a;b\"}", &out);
CHECK(strstr(s, "error: name may use") != NULL);
snprintf(name, sizeof(name), "splinter-test-%d", (int)getpid());
snprintf(args, sizeof(args), "{\"cmd\":\"cat\",\"name\":\"%s\"}", name);
s = call(&reg, "tmux_start", args, &out);
CHECK(strstr(s, "target is") != NULL);
s = call(&reg, "tmux_status", "{}", &out);
CHECK(strstr(s, name) != NULL && strstr(s, "alive") != NULL);
s = call(&reg, "tmux_ask",
"{\"text\":\"ping\",\"wait_ms\":5000,\"quiet_ms\":400}", &out);
CHECK(strstr(s, "ping") != NULL);
/* the second answer is only what is new: no "ping" in it */
s = call(&reg, "tmux_ask",
"{\"text\":\"pong\",\"wait_ms\":5000,\"quiet_ms\":400}", &out);
CHECK(strstr(s, "pong") != NULL);
CHECK(strstr(s, "ping") == NULL);
/* and with nothing typed there is nothing new */
s = call(&reg, "tmux_read", "{}", &out);
CHECK(strstr(s, "(no new output)") != NULL);
s = call(&reg, "tmux_capture", "{\"lines\":50}", &out);
CHECK(strstr(s, "ping") != NULL && strstr(s, "pong") != NULL);
/* keys are names; anything that looks like an option is refused */
s = call(&reg, "tmux_send", "{\"keys\":\"-t other\"}", &out);
CHECK(strstr(s, "error: keys are names") != NULL);
s = call(&reg, "tmux_send", "{\"keys\":\"C-d\"}", &out);
CHECK(strstr(s, "sent") != NULL);
CHECK(sp_host_command(host, "tmux", "stop", &out) == 0);
done:
sp_host_free(host);
sp_registry_free(&reg);
sp_buf_free(&out);
sp_buf_free(&err);
snprintf(args, sizeof(args), "rm -rf '%s'", dir);
(void)system(args);
if (failures == 0)
printf("test_tmux: ok\n");
return failures == 0 ? 0 : 1;
}