/* * splinter -- delegate selection. * * The choice is code, so it is tested like code: given a task and a set of * delegates, the same one must win every time, and the reason must name what * decided it. */ #include #include #include "splinter.h" #include "config.h" static int failures; #define CHECK(cond) do { \ if (!(cond)) { \ fprintf(stderr, "FAIL %s:%d: %s\n", __FILE__, __LINE__, #cond); \ failures++; \ } \ } while (0) static void add(struct sp_config *c, const char *id, const char *about, const char *tags) { struct sp_delegate *d = &c->delegate[c->ndelegates++]; memset(d, 0, sizeof(*d)); snprintf(d->id, sizeof(d->id), "%s", id); snprintf(d->about, sizeof(d->about), "%s", about); snprintf(d->tags, sizeof(d->tags), "%s", tags); snprintf(d->endpoint, sizeof(d->endpoint), "http://example/v1"); snprintf(d->model, sizeof(d->model), "m"); } int main(void) { struct sp_config c; char id[64], why[192]; int score; memset(&c, 0, sizeof(c)); add(&c, "coder", "C99, POSIX, kennt den Quellbaum", "code,c99,review"); add(&c, "quick", "kurze Antworten, Chat", "chat,cheap,summary"); /* a tag in the task wins by tag */ CHECK(sp_delegate_choose(&c, "pruefe den code auf fehler", NULL, id, sizeof(id), why, sizeof(why), &score) == 0); CHECK(strcmp(id, "coder") == 0); CHECK(score >= 3); CHECK(strstr(why, "code") != NULL); CHECK(sp_delegate_choose(&c, "mach eine kurze summary", NULL, id, sizeof(id), why, sizeof(why), &score) == 0); CHECK(strcmp(id, "quick") == 0); /* a requested tag outweighs the wording */ CHECK(sp_delegate_choose(&c, "irgendwas mit code", "chat", id, sizeof(id), why, sizeof(why), &score) == 0); CHECK(strcmp(id, "quick") == 0); CHECK(strstr(why, "requested") != NULL); /* a description word is weaker than a tag but still counts */ CHECK(sp_delegate_choose(&c, "kennt jemand den quellbaum", NULL, id, sizeof(id), why, sizeof(why), &score) == 0); CHECK(strcmp(id, "coder") == 0); /* nothing matches: no pick rather than a random one */ CHECK(sp_delegate_choose(&c, "xyzzy", NULL, id, sizeof(id), why, sizeof(why), &score) != 0); /* prio decides a tie: same score, higher prio wins */ memset(&c, 0, sizeof(c)); add(&c, "low", "gleich gut", "x"); add(&c, "high", "gleich gut", "x"); c.delegate[1].prio = 9; CHECK(sp_delegate_choose(&c, "x", NULL, id, sizeof(id), why, sizeof(why), &score) == 0); CHECK(strcmp(id, "high") == 0); CHECK(sp_delegate_prio(&c, "high") == 9); /* max_parallel defaults to one and never to zero */ memset(&c, 0, sizeof(c)); add(&c, "d", "x", "x"); CHECK(c.delegate[0].max_parallel == 0); /* add() does not set it */ /* an empty set is not a match either */ c.ndelegates = 0; CHECK(sp_delegate_choose(&c, "code", NULL, id, sizeof(id), why, sizeof(why), &score) != 0); if (failures == 0) printf("test_delegate: ok\n"); return failures == 0 ? 0 : 1; }