Files
splinter/tests/test_transport.c
Konrad 15581139d5 splinter: conversation, OpenAI adapter, end-to-end over HTTP
Splinter can now talk to a real endpoint: --endpoint plus --model shapes the
whole conversation into one chat/completions request and parses the reply back.

The seam changed from a raw string to the conversation, because a provider
adapter cannot invent history and the loop must not depend on vendor JSON:

- sp_conv: owned messages (role, name, call_id, content, raw tool_calls),
  geometric growth, trim from the front. Copy on push, so a compaction step
  must produce a new array - which is what makes it reviewable.
- sp_openai_request(): conversation + tool table -> JSON body.
- sp_openai_reply(): choices[0].message -> content + calls, and it keeps the
  provider's raw tool_calls array to hand back verbatim next round.
- provider error objects, empty choice lists, non-JSON bodies and empty bodies
  each produce a distinct, quoted error instead of a silent empty answer.
- transport_openai: curl, body through a temp file, status from curl's -w, key
  from the environment only.
- libmcp's JSON parser is vendored and instantiated exactly once (src/json.c);
  two instantiations would be a link error rather than a silent duplicate.

Proven end to end against a local stub speaking the real wire format:
request 1 (1 message, 2 tools, 448 bytes) -> tool_call for echo; splinter ran
the tool, request 2 (3 messages, 682 bytes) -> final text; the session log
holds user, observation, assistant.

Nine test binaries green, including test_openai (request shape, tool_call
parsing, raw array kept, four failing reply shapes) and test_conv.
2026-11-07 13:34:26 +00:00

94 lines
2.5 KiB
C

/* splinter -- mock transport tests (conversation based). */
#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)
int
main(void)
{
struct sp_mock m;
struct sp_transport t;
struct sp_reply r;
struct sp_buf err;
struct sp_conv conv;
if (sp_mock_init(&m) != 0)
return 1;
if (sp_buf_init(&err) != 0)
return 1;
sp_conv_init(&conv);
t = sp_mock_transport(&m);
CHECK(sp_conv_push(&conv, "user", NULL, "hi") == 0);
/* an empty queue is an error, never an empty success */
memset(&r, 0, sizeof(r));
CHECK(t.send(t.ctx, &conv, &r, &err) != 0);
CHECK(err.len > 0);
sp_buf_clear(&err);
sp_reply_free(&r);
/* a plain answer */
CHECK(sp_mock_push(&m, 200, "answer first") == 0);
memset(&r, 0, sizeof(r));
CHECK(t.send(t.ctx, &conv, &r, &err) == 0);
CHECK(r.status == 200);
CHECK(r.content != NULL && strcmp(r.content, "first") == 0);
CHECK(r.ncalls == 0);
CHECK(strcmp(m.last_request.p, "user hi\n") == 0);
sp_reply_free(&r);
/* after a tool ran, the request says which tool and what came back */
CHECK(sp_conv_push(&conv, "tool", "echo", "ping") == 0);
CHECK(sp_mock_push(&m, 200, "answer second") == 0);
memset(&r, 0, sizeof(r));
CHECK(t.send(t.ctx, &conv, &r, &err) == 0);
CHECK(strcmp(r.content, "second") == 0);
CHECK(strcmp(m.last_request.p, "observe echo ping\n") == 0);
sp_reply_free(&r);
/* a tool call comes back as one call with its arguments */
CHECK(sp_mock_push(&m, 200, "tool echo a b c") == 0);
memset(&r, 0, sizeof(r));
CHECK(t.send(t.ctx, &conv, &r, &err) == 0);
CHECK(r.ncalls == 1);
CHECK(strcmp(r.calls[0].name, "echo") == 0);
CHECK(strcmp(r.calls[0].args, "a b c") == 0);
CHECK(r.calls[0].id != NULL);
sp_reply_free(&r);
/* a non-2xx status is passed through with its body untouched */
CHECK(sp_mock_push(&m, 429, "slow down") == 0);
memset(&r, 0, sizeof(r));
CHECK(t.send(t.ctx, &conv, &r, &err) == 0);
CHECK(r.status == 429);
CHECK(strcmp(r.content, "slow down") == 0);
sp_reply_free(&r);
/* a reply in neither form is an error that says what it saw */
CHECK(sp_mock_push(&m, 200, "gibberish") == 0);
sp_buf_clear(&err);
memset(&r, 0, sizeof(r));
CHECK(t.send(t.ctx, &conv, &r, &err) != 0);
CHECK(strstr(err.p, "neither answer nor tool") != NULL);
sp_reply_free(&r);
sp_buf_free(&err);
sp_conv_free(&conv);
sp_mock_free(&m);
CHECK(m.queue == NULL);
if (failures == 0)
printf("test_transport: ok\n");
return failures == 0 ? 0 : 1;
}