/* splinter -- conversation tests. */ #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_conv c; struct sp_msg m; sp_conv_init(&c); CHECK(c.n == 0); CHECK(sp_conv_bytes(&c) == 0); CHECK(sp_conv_push(&c, "user", NULL, "hallo") == 0); CHECK(sp_conv_push(&c, "assistant", NULL, "hi") == 0); CHECK(c.n == 2); CHECK(strcmp(c.v[0].role, "user") == 0); CHECK(strcmp(c.v[0].content, "hallo") == 0); CHECK(c.v[0].name == NULL); CHECK(c.v[0].call_id == NULL); /* a tool message carries name and call id */ memset(&m, 0, sizeof(m)); m.role = "tool"; m.name = "echo"; m.call_id = "call-7"; m.content = "pong"; CHECK(sp_conv_add(&c, &m) == 0); CHECK(c.n == 3); CHECK(strcmp(c.v[2].name, "echo") == 0); CHECK(strcmp(c.v[2].call_id, "call-7") == 0); /* an assistant turn that asked for tools keeps the provider's raw array */ memset(&m, 0, sizeof(m)); m.role = "assistant"; m.content = ""; m.calls = "[{\"id\":\"call-7\",\"type\":\"function\",\"function\":" "{\"name\":\"echo\",\"arguments\":\"x\"}}]"; CHECK(sp_conv_add(&c, &m) == 0); CHECK(c.v[3].calls != NULL); /* the copy is a copy: changing the source must not change the record */ { char buf[16]; snprintf(buf, sizeof(buf), "hallo"); buf[0] = 'H'; CHECK(strcmp(c.v[0].content, "hallo") == 0); } /* a message without a role is refused */ memset(&m, 0, sizeof(m)); m.content = "x"; CHECK(sp_conv_add(&c, &m) == -1); CHECK(c.n == 4); /* bytes grow with the content */ CHECK(sp_conv_bytes(&c) > 0); sp_conv_free(&c); CHECK(c.n == 0); CHECK(c.v == NULL); if (failures == 0) printf("test_conv: ok\n"); return failures == 0 ? 0 : 1; }