/* * splinter -- skills on demand. * * The point of skills is that they are NOT in the system prompt, so the test * checks the two halves that make that safe: search finds a skill by a word in * its description, and load returns the file's text - not a summary, not an * empty string. */ #include #include #include #include #include #include "splinter.h" #include "skills.h" #include "config.h" static int failures; #define CHECK(c) do { if (!(c)) { fprintf(stderr, "FAIL %s:%d: %s\n", \ __FILE__, __LINE__, #c); failures++; } } while (0) static void write_file(const char *path, const char *text) { FILE *f = fopen(path, "w"); if (f == NULL) return; fputs(text, f); fclose(f); } int main(void) { struct sp_registry r; struct sp_buf out; struct sp_config cfg; char home[512], dir[600], path[700]; const char *tmp = getenv("TMPDIR"); const struct sp_tool *t; if (tmp == NULL || *tmp == '\0') tmp = "/var/tmp"; snprintf(home, sizeof(home), "%s/splinter-skill-XXXXXX", tmp); CHECK(mkdtemp(home) != NULL); snprintf(dir, sizeof(dir), "%s/.splinter", home); CHECK(mkdir(dir, 0700) == 0); snprintf(dir, sizeof(dir), "%s/.splinter/skills", home); CHECK(mkdir(dir, 0700) == 0); snprintf(path, sizeof(path), "%s/c99.md", dir); write_file(path, "---\nname: c99\ndescription: C99 und POSIX, ohne Extensions\n---\n\n" "# C99\n\nRegel eins: kein VLA.\n"); snprintf(path, sizeof(path), "%s/kanban", dir); CHECK(mkdir(path, 0700) == 0); snprintf(path, sizeof(path), "%s/kanban/SKILL.md", dir); write_file(path, "Karten fuer Agenten.\n"); CHECK(setenv("HOME", home, 1) == 0); memset(&r, 0, sizeof(r)); memset(&cfg, 0, sizeof(cfg)); CHECK(sp_buf_init(&out) == 0); CHECK(sp_skills_register(&r, &cfg) == 0); t = sp_registry_find(&r, "skill_search"); CHECK(t != NULL); CHECK(t != NULL && t->auth == SP_AUTH_READ); sp_buf_clear(&out); CHECK(t->run("{\"query\":\"posix\"}", &out, NULL) == 0); CHECK(strstr(out.p, "c99") != NULL); CHECK(strstr(out.p, "POSIX") != NULL); /* the directory form is found by its name */ sp_buf_clear(&out); CHECK(t->run("{\"query\":\"kanban\"}", &out, NULL) == 0); CHECK(strstr(out.p, "kanban") != NULL); /* a miss says so instead of pretending */ sp_buf_clear(&out); CHECK(t->run("{\"query\":\"gibtsnicht\"}", &out, NULL) == 0); CHECK(strstr(out.p, "nothing matches") != NULL); /* load returns the body */ t = sp_registry_find(&r, "skill_load"); CHECK(t != NULL); sp_buf_clear(&out); CHECK(t->run("{\"name\":\"c99\"}", &out, NULL) == 0); CHECK(strstr(out.p, "Regel eins: kein VLA.") != NULL); /* an unknown name lists what exists */ sp_buf_clear(&out); CHECK(t->run("{\"name\":\"nope\"}", &out, NULL) == 0); CHECK(strstr(out.p, "no skill 'nope'") != NULL); sp_buf_free(&out); /* the registry owns what sp_registry_add copied: free it, or the * test leaks what production never does */ sp_registry_free(&r); { char cmd[700]; snprintf(cmd, sizeof(cmd), "rm -rf '%s'", home); if (system(cmd) != 0) fprintf(stderr, "cleanup failed\n"); } if (failures == 0) printf("test_skills: ok\n"); return failures == 0 ? 0 : 1; }