Files
splinter/tests/test_sse.c
Konrad 5c07b32478 splinter: streaming, thinking, and three defects the real model exposed
Streaming: the OpenAI transport reads SSE over curl -N and reports deltas as
they arrive. Text goes to the chat pane, a tool-call start to the sidebar, and
a reasoning model's reasoning_content is shown dimmed above the answer instead
of being hidden - which is what made a 143 s turn look like a dead terminal.

Measured against kontron (Qwen3.6-35B, strata-poly): first byte at 11.6 s on a
short question, text growing visibly in the TUI at 7 s and 17 s on a longer one.

test_sse drives the accumulator with recorded lines: plain text, a tool call
split over three chunks, two calls in one turn, a broken data line, keepalives.
Twelve test binaries green.

Defects found while proving it, each fixed or stated:

- typed umlauts were dropped: the input loop accepted ASCII only. Every byte
  above 0x1f is kept now and the line is redrawn from the buffer.
- setlocale() was never called, so curses had no encoding at all.
- the vendored NetBSD curses emits Latin-1 bytes for wide characters (ü ->
  0xFC) while ncurses passes UTF-8 through; proven with a raw pty dump of both.
  The UI therefore transliterates for display (gruen) and the conversation and
  session log keep the exact UTF-8. The library defect is written down, not
  papered over.
- an answer longer than the screen was skipped by the viewport and the chat
  looked empty; draw_history now shows the bottom of the content, including
  entries taller than the window.
2026-11-07 18:55:16 +00:00

125 lines
4.0 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);
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);
sp_sse_free(s);
/* ---- a tool call in pieces, the way OpenAI sends them ---- */
s = sp_sse_new();
sp_buf_clear(&out);
CHECK(sp_sse_line(s, "data: {\"choices\":[{\"delta\":{\"tool_calls\":"
"[{\"index\":0,\"id\":\"call-a\",\"function\":{\"name\":\"echo\","
"\"arguments\":\"\"}}]}}]}\n", &out, &err) == 1);
CHECK(strcmp(out.p, "echo") == 0);
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);
/* ---- a broken data line is an error that quotes itself ---- */
s = sp_sse_new();
sp_buf_clear(&err);
CHECK(sp_sse_line(s, "data: {kaputt\n", &out, &err) == -1);
CHECK(strstr(err.p, "unparseable data line") != NULL);
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;
}