Files
splinter/tests/test_sse.c
T
lotherk 0f46c22a58 model picker, steer feedback, tolerant SSE with retries, junction headers
- /model in the curses chat opens a picker: Up/Down, typing filters by
  provider/model, Enter takes the selector, Esc cancels. The list comes from
  the backend (sp_engine_models -> sp_provider_choices), not from a text
  scrape; the socket has a `models` command, so --attach gets the same picker,
  and the switch is still the ordinary "/model <selector>" input.
- steering: Enter during a turn now says so twice - a "steering queued: ..."
  row in the transcript and "Ns steering queued (n) steer>" in the input line
  until the step boundary picks it up; a rejected steer is an error entry.
- input lag: typing during a turn is echoed immediately (the input line is
  redrawn per key, not at the next 100 ms frame), and the history is only
  repainted when its generation changed, so a streaming turn no longer
  repaints the whole transcript on every clock tick.
- sidebar: the first line of every widget is bold, like its heading, so a
  plugin block has a title instead of a wall of small print.
- junction plugin: keeps every response header from /mcp, the status route and
  /v1/models; the widget shows the X-Junction-* routing stats, /junction lists
  all headers per source. It reads the status aliases (/status, /v1/status,
  /queue, /v1/queue) and remembers the one that answered, parses queue,
  endpoint p95/exhausted counts, and counts the models (/v1/models, probed on
  its own slower interval because it goes upstream).
- SSE: blank lines no longer abort a pretty-printed object, a truncated tail
  is dropped instead of failing the turn ("incomplete data line" is gone), and
  a [DONE] with a fragment clears it.
- retries: a request that fails before anything streamed (connection, missing
  status, 5xx) is tried three more times, 5 s apart; 4xx is not retried, and
  after the first chunk the error is reported instead of printing the answer
  twice. Ctrl-C ends a wait at once; SPLINTER_RETRY_WAIT_MS overrides the wait.
- tests: test_sse (split object across events, truncated tail), test_agent
  (four 500s = four requests; two 500s then an answer), test_engine and
  test_protocol (models list), test_junction (headers, aliases, models),
  test_ui (/model picker), test_focus (status keys).

make check: all tests passed.
2026-11-17 11:46:11 +00:00

195 lines
6.6 KiB
C

/*
* splinter -- SSE accumulation tests, fed with recorded lines.
*
* No socket: the parser takes one line at a time, so these are the exact lines
* an OpenAI-compatible endpoint sends, in the order it sends them.
*/
#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 failures_now;
int
main(void)
{
struct sp_sse *s;
struct sp_buf out, err;
struct sp_reply r;
(void)failures_now;
if (sp_buf_init(&out) != 0 || sp_buf_init(&err) != 0)
return 1;
/* ---- plain text stream ---- */
s = sp_sse_new();
CHECK(s != NULL);
sp_buf_clear(&out);
CHECK(sp_sse_line(s, ": keepalive\n", &out, &err) == 0);
CHECK(sp_sse_line(s, "data: {\"choices\":[{\"delta\":{\"role\":"
"\"assistant\"}}]}\n", &out, &err) == 0);
CHECK(sp_sse_line(s, "data: {\"choices\":[{\"delta\":{\"content\":"
"\"Hal\"}}]}\n", &out, &err) == 1);
CHECK(strcmp(out.p, "Hal") == 0);
CHECK(sp_sse_kind(s) == SP_STREAM_TEXT);
sp_buf_clear(&out);
CHECK(sp_sse_line(s, "data: {\"choices\":[{\"delta\":{\"content\":"
"\"lo\"}}]}\n", &out, &err) == 1);
CHECK(strcmp(out.p, "lo") == 0);
CHECK(sp_sse_line(s, "data: [DONE]\n", &out, &err) == 0);
CHECK(sp_sse_done(s) == 1);
memset(&r, 0, sizeof(r));
CHECK(sp_sse_finish(s, &r, &err) == 0);
CHECK(r.content != NULL && strcmp(r.content, "Hallo") == 0);
CHECK(r.ncalls == 0);
CHECK(r.calls_json == NULL);
sp_reply_free(&r);
/* ---- reasoning is its own kind, never the answer ---- */
sp_buf_clear(&out);
CHECK(sp_sse_line(s, "data: {\"choices\":[{\"delta\":{"
"\"reasoning_content\":\"hmm\"}}]}\n", &out, &err) == 1);
CHECK(strcmp(out.p, "hmm") == 0);
CHECK(sp_sse_kind(s) == SP_STREAM_THINK);
sp_sse_free(s);
/*
* ---- a tool call in pieces, the way OpenAI sends them ----
*
* With `"content":null` beside the call, which is the shape the provider
* actually sends. A transport that guessed "text" from the raw line put
* the tool's name into the answer - two read calls showed up as
* "<model> readread" - so the kind comes from the parser now.
*/
s = sp_sse_new();
sp_buf_clear(&out);
CHECK(sp_sse_line(s, "data: {\"choices\":[{\"delta\":{\"content\":null,"
"\"tool_calls\":"
"[{\"index\":0,\"id\":\"call-a\",\"function\":{\"name\":\"echo\","
"\"arguments\":\"\"}}]}}]}\n", &out, &err) == 1);
CHECK(strcmp(out.p, "echo") == 0);
CHECK(sp_sse_kind(s) == SP_STREAM_TOOL);
CHECK(sp_sse_tool_start(s, 0) != NULL);
sp_buf_clear(&out);
CHECK(sp_sse_line(s, "data: {\"choices\":[{\"delta\":{\"tool_calls\":"
"[{\"index\":0,\"function\":{\"arguments\":\"{\\\"text\\\":\"}}]}}]}\n",
&out, &err) == 0);
sp_buf_clear(&out);
CHECK(sp_sse_line(s, "data: {\"choices\":[{\"delta\":{\"tool_calls\":"
"[{\"index\":0,\"function\":{\"arguments\":\"\\\"ping\\\"}\"}}]}}]}\n",
&out, &err) == 0);
CHECK(sp_sse_line(s, "data: [DONE]\n", &out, &err) == 0);
memset(&r, 0, sizeof(r));
CHECK(sp_sse_finish(s, &r, &err) == 0);
CHECK(r.ncalls == 1);
CHECK(strcmp(r.calls[0].id, "call-a") == 0);
CHECK(strcmp(r.calls[0].name, "echo") == 0);
CHECK(strcmp(r.calls[0].args, "{\"text\":\"ping\"}") == 0);
/* the reconstructed array is valid JSON text with the same values */
CHECK(r.calls_json != NULL);
CHECK(strstr(r.calls_json, "\"call-a\"") != NULL);
CHECK(strstr(r.calls_json, "\\\"text\\\":\\\"ping\\\"") != NULL);
sp_reply_free(&r);
sp_sse_free(s);
/* ---- two calls in one turn keep their indexes apart ---- */
s = sp_sse_new();
sp_buf_clear(&out);
CHECK(sp_sse_line(s, "data: {\"choices\":[{\"delta\":{\"tool_calls\":"
"[{\"index\":0,\"id\":\"c0\",\"function\":{\"name\":\"a\","
"\"arguments\":\"{}\"}},{\"index\":1,\"id\":\"c1\",\"function\":"
"{\"name\":\"b\",\"arguments\":\"{}\"}}]}}]}\n", &out, &err) == 1);
memset(&r, 0, sizeof(r));
CHECK(sp_sse_finish(s, &r, &err) == 0);
CHECK(r.ncalls == 2);
CHECK(strcmp(r.calls[0].name, "a") == 0);
CHECK(strcmp(r.calls[1].name, "b") == 0);
sp_reply_free(&r);
sp_sse_free(s);
/* ---- usage carries the cache share of the prompt ---- */
s = sp_sse_new();
sp_buf_clear(&out);
CHECK(sp_sse_line(s, "data: {\"choices\":[],\"usage\":{\"prompt_tokens\":"
"100,\"completion_tokens\":5,\"prompt_tokens_details\":"
"{\"cached_tokens\":80}}}\n", &out, &err) == 0);
CHECK(sp_sse_line(s, "data: [DONE]\n", &out, &err) == 0);
memset(&r, 0, sizeof(r));
CHECK(sp_sse_finish(s, &r, &err) == 0);
CHECK(r.tokens_in == 100 && r.tokens_out == 5);
CHECK(r.tokens_cached == 80);
sp_reply_free(&r);
sp_sse_free(s);
/* ---- proxy keepalives wear a data: prefix but are not JSON ---- */
s = sp_sse_new();
sp_buf_clear(&err);
CHECK(sp_sse_line(s, "data: ping\n", &out, &err) == 0);
CHECK(sp_sse_line(s, "data: \n", &out, &err) == 0);
CHECK(sp_sse_line(s, "data: [PING]\n", &out, &err) == 0);
CHECK(err.len == 0);
sp_sse_free(s);
/* ---- pretty-printed JSON arrives over several data: lines ---- */
s = sp_sse_new();
sp_buf_clear(&out);
sp_buf_clear(&err);
CHECK(sp_sse_line(s, "data: {\"choices\":\n", &out, &err) == 0);
CHECK(sp_sse_line(s, "data: [{\"delta\":{\"content\":\"hi\"}}]}\n", &out,
&err) == SP_STREAM_TEXT);
CHECK(strcmp(out.p, "hi") == 0);
CHECK(sp_sse_line(s, "\n", &out, &err) == 0);
sp_sse_free(s);
/*
* ---- a data object split across events, and a trailing fragment ----
*
* Real proxies put blank lines between the lines of a pretty-printed
* object and may truncate the last one. Neither is a failed turn: the
* blank line keeps the buffer, and a fragment that never completes is
* dropped at the end.
*/
s = sp_sse_new();
sp_buf_clear(&out);
sp_buf_clear(&err);
CHECK(sp_sse_line(s, "data: {\"choices\":\n", &out, &err) == 0);
CHECK(sp_sse_line(s, "\n", &out, &err) == 0);
CHECK(sp_sse_line(s, "data: [{\"delta\":{\"content\":\"ok\"}}]}\n",
&out, &err) == 1);
CHECK(strcmp(out.p, "ok") == 0);
CHECK(sp_sse_line(s, "data: {kaputt\n", &out, &err) == 0);
CHECK(sp_sse_line(s, "\n", &out, &err) == 0);
CHECK(err.len == 0);
memset(&r, 0, sizeof(r));
CHECK(sp_sse_finish(s, &r, &err) == 0);
CHECK(err.len == 0);
CHECK(r.content != NULL && strcmp(r.content, "ok") == 0);
sp_reply_free(&r);
sp_sse_free(s);
/* ---- non-data lines carry nothing and are not errors ---- */
s = sp_sse_new();
sp_buf_clear(&err);
CHECK(sp_sse_line(s, "event: ping\n", &out, &err) == 0);
CHECK(sp_sse_line(s, "\n", &out, &err) == 0);
CHECK(err.len == 0);
sp_sse_free(s);
sp_buf_free(&out);
sp_buf_free(&err);
if (failures == 0)
printf("test_sse: ok\n");
return failures == 0 ? 0 : 1;
}