/* splinter -- append-only session tests. */ #include #include #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_session_info s; struct sp_buf err, line; char tmpl[] = "/tmp/splinter-sess-XXXXXX"; char *dir; char path[640]; size_t n; FILE *f; dir = mkdtemp(tmpl); if (dir == NULL) { perror("mkdtemp"); return 1; } if (sp_buf_init(&err) != 0) return 1; CHECK(sp_session_open(&s, dir, "s1", &err) == 0); CHECK(strcmp(s.id, "s1") == 0); CHECK(sp_session_count(&s, &n, &err) == 0); CHECK(n == 0); CHECK(sp_session_append(&s, "user", "hi \"there\"", &err) == 0); CHECK(sp_session_append(&s, "assistant", "hello", &err) == 0); CHECK(sp_session_count(&s, &n, &err) == 0); CHECK(n == 2); /* the record is valid JSONL with the text escaped */ snprintf(path, sizeof(path), "%s/s1.jsonl", dir); f = fopen(path, "r"); CHECK(f != NULL); if (f != NULL) { if (sp_buf_init(&line) == 0) { char buf[512]; size_t got = fread(buf, 1, sizeof(buf) - 1, f); buf[got] = '\0'; CHECK(strstr(buf, "\"role\":\"user\"") != NULL); CHECK(strstr(buf, "hi \\\"there\\\"") != NULL); CHECK(buf[got - 1] == '\n'); sp_buf_free(&line); } fclose(f); } /* a torn tail must not be counted as a record and must not be an error */ f = fopen(path, "a"); CHECK(f != NULL); if (f != NULL) { fputs("{\"role\":\"user\",\"text\":\"torn", f); fclose(f); } CHECK(sp_session_count(&s, &n, &err) == 0); CHECK(n == 2); /* reopening an existing session appends, it never truncates */ CHECK(sp_session_open(&s, dir, "s1", &err) == 0); CHECK(sp_session_append(&s, "user", "again", &err) == 0); sp_buf_free(&err); printf("test_session: ok (%s)\n", dir); return failures == 0 ? 0 : 1; }