/* splinter -- mock transport tests (conversation based). */ #include #include #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; }