/* * splinter -- recap plugin tests. * * /recap sets a recap; it enters the conversation on the next request * and then every N turns, before the newest user message, once per turn. */ #include #include #include #include "splinter.h" #include "splinter_plugin.h" static int failures; #define CHECK(cond) do { \ if (!(cond)) { \ fprintf(stderr, "FAIL %s:%d: %s\n", __FILE__, __LINE__, #cond); \ failures++; \ } \ } while (0) #define NO_PRESSURE ((size_t)-1) static int count(const struct sp_conv *c) { size_t i; int n = 0; for (i = 0; i < c->n; i++) if (c->v[i].content != NULL && strstr(c->v[i].content, "[recap]") != NULL) n++; return n; } static int turn(struct sp_host *h, struct sp_conv *c, const char *user) { CHECK(sp_conv_push(c, "user", NULL, user) == 0); sp_host_turn(h); return sp_host_compact(h, c, NO_PRESSURE); } static int cmd(struct sp_host *h, const char *args, char *reply, size_t n) { struct sp_buf b; int rc; if (sp_buf_init(&b) != 0) return -1; rc = sp_host_command(h, "recap", args, &b); snprintf(reply, n, "%s", b.p != NULL ? b.p : ""); sp_buf_free(&b); return rc; } int main(void) { struct sp_registry reg; struct sp_agent ag; struct sp_host *host; struct sp_buf err; struct sp_conv conv; char reply[512]; int i; memset(®, 0, sizeof(reg)); memset(&ag, 0, sizeof(ag)); if (sp_buf_init(&err) != 0) return 1; setenv("SPLINTER_RECAP_EVERY", "3", 1); host = sp_host_new(®, &ag, NULL); CHECK(host != NULL); CHECK(sp_plugin_load(host, "plugins/recap/recap.so", &err) == 0); /* nothing set: quiet */ sp_conv_init(&conv); CHECK(turn(host, &conv, "eins") == 0); CHECK(count(&conv) == 0); CHECK(cmd(host, "", reply, sizeof(reply)) == 0); CHECK(strstr(reply, "no recap") != NULL); /* set: next request carries it, before the user's line */ CHECK(cmd(host, "Ship the parser, nothing else", reply, sizeof(reply)) == 0); CHECK(strstr(reply, "every 3") != NULL); CHECK(turn(host, &conv, "zwei") == 1); CHECK(count(&conv) == 1); CHECK(strstr(conv.v[conv.n - 1].content, "zwei") != NULL); CHECK(strstr(conv.v[conv.n - 2].content, "Ship the parser") != NULL); CHECK(strcmp(conv.v[conv.n - 2].role, "system") == 0); /* same turn again: no second copy */ CHECK(sp_host_compact(host, &conv, NO_PRESSURE) == 0); CHECK(count(&conv) == 1); /* the next turns until the interval; the host counts from its own turn */ for (i = 0; i < 12; i++) (void)turn(host, &conv, "weiter"); CHECK(count(&conv) >= 4 && count(&conv) <= 6); /* interval 0: no repeats after the first */ CHECK(cmd(host, "every 0", reply, sizeof(reply)) == 0); CHECK(cmd(host, "Neu", reply, sizeof(reply)) == 0); { int before = count(&conv); (void)turn(host, &conv, "a"); CHECK(count(&conv) == before + 1); for (i = 0; i < 9; i++) (void)turn(host, &conv, "b"); CHECK(count(&conv) == before + 1); } CHECK(cmd(host, "every x", reply, sizeof(reply)) == 0); CHECK(strstr(reply, "every:") != NULL); /* off */ CHECK(cmd(host, "off", reply, sizeof(reply)) == 0); { int before = count(&conv); for (i = 0; i < 6; i++) (void)turn(host, &conv, "c"); CHECK(count(&conv) == before); } sp_conv_free(&conv); sp_host_free(host); sp_buf_free(&err); if (failures == 0) puts("recap: ok"); return failures != 0; }