Artifact 47654be7d8c92a36e006c0a385f773488906b605259f872172efdb4f0c249c94:
- File
test/pic/test_prompts.c
— part of check-in
[bb88ac58ac]
at
2026-11-19 20:35:01
on branch templates
— Prompt templates, as Pi's
Markdown files in ~/.pic/prompts/ and, once the workspace is trusted, WORKSPACE/.pic/prompts/ become /name commands: Pi's frontmatter (description, argument-hint), shell-like arguments and substitutions ($1, $@, $ARGUMENTS, ${1:-default}, ${@:N}, ${@:N:L}). They expand where text becomes a request, so ask, chat and the terminal agree; the slash menu lists them, built-in commands win a name clash, and /reload re-reads them. pic prompts [--json] lists them. Files are read through the platform's input reader, which gains a directory listing. Fixes a pollfd array one entry too small when a catalog refresh runs during a reply. (user: dmestas, size: 13888) [annotate] [blame] [check-ins using] [more...]
/* Prompt templates: argument splitting and substitution (Pi's own cases from * prompt-templates.test.ts), the frontmatter, and loading from directories. */ #include <assert.h> #include <errno.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/stat.h> #include <unistd.h> #include "prompts.h" #define N(a) (sizeof(a) / sizeof((a)[0])) static void sub(const char *tmpl, char *const *args, size_t n, const char *want) { char *got = pic_prompt_substitute(tmpl, args, n); if (!got || strcmp(got, want) != 0) { fprintf(stderr, "substitute(\"%s\"): got \"%s\", want \"%s\"\n", tmpl, got ? got : "(null)", want); abort(); } free(got); } #define SUB(tmpl, want, ...) \ do { \ char *a_[] = {__VA_ARGS__}; \ sub(tmpl, a_, N(a_), want); \ } while (0) #define SUB0(tmpl, want) sub(tmpl, NULL, 0, want) static void args(const char *in, char *const *want, size_t n) { size_t got_n; char **got = pic_prompt_args(in, &got_n); if (!got || got_n != n) { fprintf(stderr, "args(\"%s\"): %zu args, want %zu\n", in, got_n, n); abort(); } for (size_t i = 0; i < n; i++) { if (strcmp(got[i], want[i]) != 0) { fprintf(stderr, "args(\"%s\")[%zu]: got \"%s\", want \"%s\"\n", in, i, got[i], want[i]); abort(); } } pic_prompt_args_free(got, got_n); } #define ARGS(in, ...) \ do { \ char *w_[] = {__VA_ARGS__}; \ args(in, w_, N(w_)); \ } while (0) #define ARGS0(in) args(in, NULL, 0) static void test_substitute(void) { SUB("Test: $ARGUMENTS", "Test: a b c", "a", "b", "c"); SUB("Test: $@", "Test: a b c", "a", "b", "c"); SUB("$ARGUMENTS", "$1 $ARGUMENTS", "$1", "$ARGUMENTS"); SUB("$@", "$100 $1", "$100", "$1"); SUB("$ARGUMENTS", "$100 $1", "$100", "$1"); SUB("$1: $ARGUMENTS", "prefix: prefix a b", "prefix", "a", "b"); SUB("$1: $@", "prefix: prefix a b", "prefix", "a", "b"); SUB0("Test: $ARGUMENTS", "Test: "); SUB0("Test: $@", "Test: "); SUB0("Test: $1", "Test: "); SUB("$ARGUMENTS and $ARGUMENTS", "a b and a b", "a", "b"); SUB("$@ and $ARGUMENTS", "a b and a b", "a", "b"); SUB("$1 $2: $ARGUMENTS", "arg100 @user: arg100 @user", "arg100", "@user"); SUB("$1 $2 $3 $4 $5", "a b ", "a", "b"); SUB("$ARGUMENTS", "日本語 🎉 café", "日本語", "🎉", "café"); SUB("$1 $2", "line1\nline2 tab\tthere", "line1\nline2", "tab\tthere"); SUB("$1$2", "ab", "a", "b"); SUB("$0", "", "a", "b"); SUB("$1.5", "a.5", "a"); SUB("pre$ARGUMENTS", "prea b", "a", "b"); SUB("pre$@", "prea b", "a", "b"); SUB("$ARGUMENTS", "a c", "a", "", "c"); SUB("$ARGUMENTS", " leading trailing ", " leading ", "trailing "); SUB("Prefix $ARGUMENTS suffix", "Prefix ARGUMENTS suffix", "ARGUMENTS"); SUB("$A $$ $ $ARGS", "$A $$ $ $ARGS", "a"); SUB("$arguments $Arguments $ARGUMENTS", "$arguments $Arguments a b", "a", "b"); SUB("$10 $12 $15", "val9 val11 val14", "val0", "val1", "val2", "val3", "val4", "val5", "val6", "val7", "val8", "val9", "val10", "val11", "val12", "val13", "val14"); SUB0("Price: \\$100", "Price: \\"); SUB("$1: $@ ($ARGUMENTS)", "first: first second third (first second third)", "first", "second", "third"); SUB("Just plain text", "Just plain text", "a", "b"); SUB("$1 $2 $@", "a b a b c", "a", "b", "c"); SUB("$99999999999999999999999 x", " x", "a"); SUB0("List exactly ${1:-7} next steps", "List exactly 7 next steps"); SUB0("${@:-default}\n${ARGUMENTS:-default}", "default\ndefault"); SUB("${@:-default}\n${ARGUMENTS:-default}", "This would be the arguments\nThis would be the arguments", "This", "would", "be", "the", "arguments"); SUB("List exactly ${1:-7} next steps", "List exactly 3 next steps", "3"); SUB("Mode: ${1:-brief}", "Mode: brief", ""); SUB0("${1:-7} ${2:-brief}", "7 brief"); SUB("${1:-7} ${2:-brief}", "3 brief", "3"); SUB("${1:-7} ${2:-brief}", "3 verbose", "3", "verbose"); SUB("${1:-7}", "$ARGUMENTS", "$ARGUMENTS"); SUB("${1:-7}", "$1", "$1"); SUB("${1:-$ARGUMENTS}", "a", "a", "b"); SUB("${3:-$ARGUMENTS}", "$ARGUMENTS", "a", "b"); SUB0("${1:-seven steps}", "seven steps"); SUB("${3:-fallback}", "fallback", "a", "b"); SUB("$1 ${2:-x} $ARGUMENTS", "a x a", "a"); SUB("${@:2}", "b c d", "a", "b", "c", "d"); SUB("${@:1}", "a b c", "a", "b", "c"); SUB("${@:3}", "c d", "a", "b", "c", "d"); SUB("${@:2:2}", "b c", "a", "b", "c", "d"); SUB("${@:1:1}", "a", "a", "b", "c"); SUB("${@:3:1}", "c", "a", "b", "c", "d"); SUB("${@:2:3}", "b c d", "a", "b", "c", "d", "e"); SUB("${@:99}", "", "a", "b"); SUB("${@:5}", "", "a", "b"); SUB("${@:10:5}", "", "a", "b"); SUB("${@:2:0}", "", "a", "b", "c"); SUB("${@:1:0}", "", "a", "b"); SUB("${@:2:99}", "b c", "a", "b", "c"); SUB("${@:1:10}", "a b", "a", "b"); SUB("${@:2} vs $@", "b c vs a b c", "a", "b", "c"); SUB("First: ${@:1:1}, All: $@", "First: x, All: x y z", "x", "y", "z"); SUB("${@:1}", "${@:2} test", "${@:2}", "test"); SUB("${@:2}", "${@:3} c", "a", "${@:3}", "c"); SUB("$1: ${@:2}", "cmd: arg1 arg2", "cmd", "arg1", "arg2"); SUB("$1 $2 ${@:3}", "a b c d", "a", "b", "c", "d"); SUB("${@:0}", "a b c", "a", "b", "c"); SUB0("${@:2}", ""); SUB("${@:1}", "only", "only"); SUB("${@:2}", "", "only"); SUB("Process ${@:2} with $1", "Process file1 file2 with tool", "tool", "file1", "file2"); SUB("${@:1:1} and ${@:2}", "a and b c", "a", "b", "c"); SUB("${@:1:2} vs ${@:3:2}", "a b vs c d", "a", "b", "c", "d", "e"); SUB("prefix${@:2}suffix", "prefixb csuffix", "a", "b", "c"); SUB("Run $1 on ${@:2:2}, then process $@", "Run eslint on file1.ts file2.ts, then process eslint file1.ts file2.ts file3.ts", "eslint", "file1.ts", "file2.ts", "file3.ts"); /* malformed braces stay text */ SUB("${@:x} ${1} ${@:2:} ${1:-unclosed", "${@:x} ${1} ${@:2:} ${1:-unclosed", "a", "b"); } static void test_args(void) { ARGS("a b c", "a", "b", "c"); ARGS("\"first arg\" second", "first arg", "second"); ARGS("'first arg' second", "first arg", "second"); ARGS("\"double\" 'single' \"double again\"", "double", "single", "double again"); ARGS0(""); ARGS("a b c", "a", "b", "c"); ARGS("a\tb\tc", "a", "b", "c"); ARGS("\"\" \" \"", "", " "); ARGS("a '' c", "a", "", "c"); ARGS("a \"\"", "a", ""); ARGS("$100 @user #tag", "$100", "@user", "#tag"); ARGS("日本語 🎉 café", "日本語", "🎉", "café"); ARGS("\"line1\nline2\" second", "line1\nline2", "second"); ARGS("label-2\n\nHere is some description #2.", "label-2", "Here", "is", "some", "description", "#2."); ARGS("a\n\n\tb c", "a", "b", "c"); ARGS("\"quoted \\\"text\\\"\"", "quoted \"text\""); ARGS("'it\\'s here' next", "it's here", "next"); ARGS("\"C:\\dir\\file\" '\\\\server\\share'", "C:\\dir\\file", "\\\\server\\share"); ARGS("\"it's\" 'a\\\"b'", "it's", "a\\\"b"); ARGS("say \\\"hi", "say", "\\\"hi"); ARGS("why doesn't the user's cache work", "why", "doesn't", "the", "user's", "cache", "work"); ARGS("'tis the season", "'tis", "the", "season"); ARGS("say \"hello world", "say", "\"hello", "world"); ARGS("a '", "a", "'"); ARGS("\"abc\\\"", "\"abc\\\""); ARGS("\"it's\" 'x", "it's", "'x"); ARGS("a b c ", "a", "b", "c"); ARGS(" a b c", "a", "b", "c"); } static void write_file(const char *dir, const char *name, const char *text) { char path[1024]; FILE *f; (void)snprintf(path, sizeof path, "%s/%s", dir, name); f = fopen(path, "w"); assert(f); fputs(text, f); fclose(f); } static char *expand(const pic_prompts *p, const char *text) { char *out = pic_prompts_expand(p, text); return out; } #define EXPECT_EXPAND(p, in, want) \ do { \ char *o_ = expand(p, in); \ assert(o_ && strcmp(o_, want) == 0); \ free(o_); \ } while (0) static void test_load(void) { const char *tmp = getenv("TMPDIR"); char root[512], home[600], ws[600], udir[700], pdir[700], path[800]; pic_prompts p; const pic_prompt *t; char longline[200]; (void)snprintf(root, sizeof root, "%s/pic-prompts-XXXXXX", tmp && *tmp ? tmp : "/tmp"); assert(mkdtemp(root)); (void)snprintf(home, sizeof home, "%s/home", root); (void)snprintf(ws, sizeof ws, "%s/ws", root); (void)snprintf(udir, sizeof udir, "%s/.pic/prompts", home); (void)snprintf(pdir, sizeof pdir, "%s/.pic/prompts", ws); assert(mkdir(home, 0700) == 0 && mkdir(ws, 0700) == 0); (void)snprintf(path, sizeof path, "%s/.pic", home); assert(mkdir(path, 0700) == 0 && mkdir(udir, 0700) == 0); (void)snprintf(path, sizeof path, "%s/.pic", ws); assert(mkdir(path, 0700) == 0 && mkdir(pdir, 0700) == 0); assert(setenv("HOME", home, 1) == 0); /* no templates, no directories */ assert(pic_prompts_load(&p, root, 1) == 0 && p.n == 0); pic_prompts_free(&p); write_file(udir, "pr.md", "---\ndescription: Review PRs from URLs\nargument-hint: \"<PR-URL>\"\n---\nYou are given PR URLs: $@\n"); write_file(udir, "wr.md", "---\r\ndescription: 'Wrap it'\r\nargument-hint: [instructions]\r\n---\r\nWrap. $ARGUMENTS\r\n"); write_file(udir, "plain.md", "\n\n First real line of the body\nsecond\n"); memset(longline, 'x', 61); longline[61] = '\0'; write_file(udir, "long.md", longline); write_file(udir, "empty-hint.md", "---\ndescription: d\nargument-hint: \"\"\n---\nbody"); write_file(udir, "block.md", "---\ndescription: >\n folded one\n folded two\n---\nbody"); write_file(udir, "bom.md", "\xEF\xBB\xBF---\ndescription: bom ok\n---\nbody"); write_file(udir, "unclosed.md", "---\ndescription: nope\nbody"); write_file(udir, "empty.md", ""); write_file(udir, "notes.txt", "not a template"); write_file(udir, "both.md", "user one"); write_file(pdir, "both.md", "project one"); write_file(pdir, "only-project.md", "project only $1"); (void)snprintf(path, sizeof path, "%s/subdir.md", udir); assert(mkdir(path, 0700) == 0); /* a directory named x.md is not a template */ write_file(root, "target.md", "linked $1"); (void)snprintf(path, sizeof path, "%s/target.md", root); { char link[800]; (void)snprintf(link, sizeof link, "%s/linked.md", udir); assert(symlink(path, link) == 0); (void)snprintf(link, sizeof link, "%s/broken.md", udir); assert(symlink("/nonexistent/x.md", link) == 0); } assert(pic_prompts_load(&p, ws, 0) == 0); assert(!pic_prompts_find(&p, "only-project")); assert(!pic_prompts_find(&p, "notes") && !pic_prompts_find(&p, "subdir") && !pic_prompts_find(&p, "broken")); t = pic_prompts_find(&p, "pr"); assert(t && !t->project && strcmp(t->description, "Review PRs from URLs") == 0 && strcmp(t->hint, "<PR-URL>") == 0); assert(strcmp(t->body, "You are given PR URLs: $@") == 0); t = pic_prompts_find(&p, "wr"); assert(t && strcmp(t->description, "Wrap it") == 0 && strcmp(t->hint, "[instructions]") == 0 && strcmp(t->body, "Wrap. $ARGUMENTS") == 0); t = pic_prompts_find(&p, "plain"); assert(t && strcmp(t->description, "First real line of the body") == 0 && !*t->hint); t = pic_prompts_find(&p, "long"); assert(t && strlen(t->description) == 63 && strcmp(t->description + 60, "...") == 0); assert(strcmp(pic_prompts_find(&p, "empty-hint")->hint, "") == 0); assert(strcmp(pic_prompts_find(&p, "block")->description, "folded one folded two") == 0); assert(strcmp(pic_prompts_find(&p, "bom")->description, "bom ok") == 0); t = pic_prompts_find(&p, "unclosed"); /* no closing --- : all of it is the body */ assert(t && strcmp(t->description, "---") == 0 && strncmp(t->body, "---\ndescription: nope", 21) == 0); t = pic_prompts_find(&p, "empty"); assert(t && !*t->description && !*t->body); assert(strcmp(pic_prompts_find(&p, "linked")->body, "linked $1") == 0); EXPECT_EXPAND(&p, "/pr a b", "You are given PR URLs: a b"); EXPECT_EXPAND(&p, "/pr\nlabel", "You are given PR URLs: label"); EXPECT_EXPAND(&p, "/linked 'x y' z", "linked x y"); EXPECT_EXPAND(&p, "/both", "user one"); assert(!pic_prompts_expand(&p, "/only-project x") && !pic_prompts_expand(&p, "/nosuch x")); assert(!pic_prompts_expand(&p, "pr a") && !pic_prompts_expand(&p, "/") && !pic_prompts_expand(&p, " /pr")); assert(!pic_prompts_expand(&p, "/pro") && !pic_prompts_expand(&p, "/p")); pic_prompts_free(&p); /* the project's, after the user's; the user's wins a shared name, both are listed */ assert(pic_prompts_load(&p, ws, 1) == 0); t = pic_prompts_find(&p, "only-project"); assert(t && t->project); EXPECT_EXPAND(&p, "/only-project z", "project only z"); EXPECT_EXPAND(&p, "/both", "user one"); { size_t both = 0; for (size_t i = 0; i < p.n; i++) { both += strcmp(p.v[i].name, "both") == 0; } assert(both == 2); } pic_prompts_free(&p); /* the review example from Pi (#10867) */ write_file(udir, "review.md", "Review $2. Focus: ${1:-correctness}."); assert(pic_prompts_load(&p, ws, 0) == 0); EXPECT_EXPAND(&p, "/review \"\" src/index.ts", "Review src/index.ts. Focus: correctness."); EXPECT_EXPAND(&p, "/review '' src/index.ts", "Review src/index.ts. Focus: correctness."); pic_prompts_free(&p); { char cmd[1200]; (void)snprintf(cmd, sizeof cmd, "rm -rf -- '%s'", root); assert(system(cmd) == 0); } } int main(void) { test_substitute(); test_args(); test_load(); puts("PASS: prompt templates: Pi's argument and substitution cases, frontmatter, loading, precedence"); return 0; }