- .meta carries a title: the first user line is stored once, /rename [ID] TITLE replaces it. Reopening preserves the title and the original creation time. - /sessions shows the title, locally and over the link; the protocol's sessions event carries it. Old sessions without a stored title fall back to their first user line when listed. - /rm ID removes a session that is not open: JSONL, meta, saved agent profile and logs/<id>; the open session is refused, /rm last resolves the newest. - tests: test_session (auto title, explicit title survives reopen, old-log fallback, recursive removal), test_protocol (title, rename, rm, errors), test_ui (title, rename, rm). make check green.
166 lines
4.1 KiB
C
166 lines
4.1 KiB
C
/* splinter -- append-only session tests. */
|
|
#include <errno.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <sys/stat.h>
|
|
#include <unistd.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_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);
|
|
|
|
/* the first user line is the title, and it survives reopening */
|
|
{
|
|
struct sp_session_brief v[4];
|
|
char mpath[700];
|
|
FILE *m;
|
|
char buf[512];
|
|
size_t got;
|
|
|
|
snprintf(mpath, sizeof(mpath), "%s/s1.meta", dir);
|
|
m = fopen(mpath, "r");
|
|
CHECK(m != NULL);
|
|
if (m != NULL) {
|
|
got = fread(buf, 1, sizeof(buf) - 1, m);
|
|
buf[got] = '\0';
|
|
CHECK(strstr(buf, "title=hi \"there\"") != NULL);
|
|
fclose(m);
|
|
}
|
|
CHECK(sp_session_list(dir, v, 4) == 1);
|
|
CHECK(strcmp(v[0].title, "hi \"there\"") == 0);
|
|
}
|
|
|
|
/* an explicit title wins and is not lost when the session is opened */
|
|
CHECK(sp_session_set_title(dir, "s1", "Ein Titel", &err) == 0);
|
|
CHECK(sp_session_open(&s, dir, "s1", &err) == 0);
|
|
{
|
|
struct sp_session_brief v[4];
|
|
|
|
CHECK(sp_session_list(dir, v, 4) == 1);
|
|
CHECK(strcmp(v[0].title, "Ein Titel") == 0);
|
|
}
|
|
|
|
/* 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);
|
|
|
|
/* removal takes the log, sidecars and the per-session log directory */
|
|
{
|
|
char lpath[700], lfile[760];
|
|
FILE *lf;
|
|
|
|
snprintf(lpath, sizeof(lpath), "%s/logs", dir);
|
|
CHECK(mkdir(lpath, 0700) == 0 || errno == EEXIST);
|
|
snprintf(lpath, sizeof(lpath), "%s/logs/s1", dir);
|
|
CHECK(mkdir(lpath, 0700) == 0);
|
|
snprintf(lfile, sizeof(lfile), "%s/session.log", lpath);
|
|
lf = fopen(lfile, "w");
|
|
CHECK(lf != NULL);
|
|
if (lf != NULL) {
|
|
fputs("x\n", lf);
|
|
fclose(lf);
|
|
}
|
|
snprintf(path, sizeof(path), "%s/s1.jsonl", dir);
|
|
CHECK(sp_session_delete(dir, "s1", &err) == 0);
|
|
CHECK(access(path, F_OK) != 0);
|
|
CHECK(access(lpath, F_OK) != 0);
|
|
{
|
|
struct sp_session_brief v[4];
|
|
|
|
CHECK(sp_session_list(dir, v, 4) == 0);
|
|
}
|
|
CHECK(sp_session_delete(dir, "s1", &err) == -1);
|
|
}
|
|
|
|
/*
|
|
* A session from before titles existed has no title in its meta (or no
|
|
* meta at all): the listing reads the first user line itself.
|
|
*/
|
|
{
|
|
struct sp_session_brief v[4];
|
|
FILE *of;
|
|
|
|
snprintf(path, sizeof(path), "%s/old.jsonl", dir);
|
|
of = fopen(path, "w");
|
|
CHECK(of != NULL);
|
|
if (of != NULL) {
|
|
fputs("{\"role\":\"user\",\"text\":\"alte frage\"}\n",
|
|
of);
|
|
fclose(of);
|
|
}
|
|
CHECK(sp_session_list(dir, v, 4) == 1);
|
|
CHECK(strcmp(v[0].title, "alte frage") == 0);
|
|
CHECK(sp_session_delete(dir, "old", &err) == 0);
|
|
}
|
|
|
|
sp_buf_free(&err);
|
|
printf("test_session: ok (%s)\n", dir);
|
|
return failures == 0 ? 0 : 1;
|
|
}
|