prompt-templates.test.ts 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615
  1. /**
  2. * Tests for prompt template argument parsing and substitution.
  3. *
  4. * Tests verify:
  5. * - Argument parsing with quotes and special characters
  6. * - Placeholder substitution ($1, $2, $@, $ARGUMENTS)
  7. * - No recursive substitution of patterns in argument values
  8. * - Edge cases and integration between parsing and substitution
  9. */
  10. import { mkdirSync, rmSync, writeFileSync } from "fs";
  11. import { tmpdir } from "os";
  12. import { join } from "path";
  13. import { afterAll, describe, expect, test } from "vitest";
  14. import { getAgentDir } from "../src/config.ts";
  15. import {
  16. expandPromptTemplate,
  17. loadPromptTemplates,
  18. parseCommandArgs,
  19. substituteArgs,
  20. } from "../src/core/prompt-templates.ts";
  21. // ============================================================================
  22. // substituteArgs
  23. // ============================================================================
  24. describe("substituteArgs", () => {
  25. test("should replace $ARGUMENTS with all args joined", () => {
  26. expect(substituteArgs("Test: $ARGUMENTS", ["a", "b", "c"])).toBe("Test: a b c");
  27. });
  28. test("should replace $@ with all args joined", () => {
  29. expect(substituteArgs("Test: $@", ["a", "b", "c"])).toBe("Test: a b c");
  30. });
  31. test("should replace $@ and $ARGUMENTS identically", () => {
  32. const args = ["foo", "bar", "baz"];
  33. expect(substituteArgs("Test: $@", args)).toBe(substituteArgs("Test: $ARGUMENTS", args));
  34. });
  35. // CRITICAL: argument values containing patterns should remain literal
  36. test("should NOT recursively substitute patterns in argument values", () => {
  37. expect(substituteArgs("$ARGUMENTS", ["$1", "$ARGUMENTS"])).toBe("$1 $ARGUMENTS");
  38. expect(substituteArgs("$@", ["$100", "$1"])).toBe("$100 $1");
  39. expect(substituteArgs("$ARGUMENTS", ["$100", "$1"])).toBe("$100 $1");
  40. });
  41. test("should support mixed $1, $2, and $ARGUMENTS", () => {
  42. expect(substituteArgs("$1: $ARGUMENTS", ["prefix", "a", "b"])).toBe("prefix: prefix a b");
  43. });
  44. test("should support mixed $1, $2, and $@", () => {
  45. expect(substituteArgs("$1: $@", ["prefix", "a", "b"])).toBe("prefix: prefix a b");
  46. });
  47. test("should handle empty arguments array with $ARGUMENTS", () => {
  48. expect(substituteArgs("Test: $ARGUMENTS", [])).toBe("Test: ");
  49. });
  50. test("should handle empty arguments array with $@", () => {
  51. expect(substituteArgs("Test: $@", [])).toBe("Test: ");
  52. });
  53. test("should handle empty arguments array with $1", () => {
  54. expect(substituteArgs("Test: $1", [])).toBe("Test: ");
  55. });
  56. test("should handle multiple occurrences of $ARGUMENTS", () => {
  57. expect(substituteArgs("$ARGUMENTS and $ARGUMENTS", ["a", "b"])).toBe("a b and a b");
  58. });
  59. test("should handle multiple occurrences of $@", () => {
  60. expect(substituteArgs("$@ and $@", ["a", "b"])).toBe("a b and a b");
  61. });
  62. test("should handle mixed occurrences of $@ and $ARGUMENTS", () => {
  63. expect(substituteArgs("$@ and $ARGUMENTS", ["a", "b"])).toBe("a b and a b");
  64. });
  65. test("should handle special characters in arguments", () => {
  66. // Note: $100 in argument doesn't get partially matched - full strings are substituted
  67. expect(substituteArgs("$1 $2: $ARGUMENTS", ["arg100", "@user"])).toBe("arg100 @user: arg100 @user");
  68. });
  69. test("should handle out-of-range numbered placeholders", () => {
  70. // Note: Out-of-range placeholders become empty strings (preserving spaces from template)
  71. expect(substituteArgs("$1 $2 $3 $4 $5", ["a", "b"])).toBe("a b ");
  72. });
  73. test("should handle unicode characters", () => {
  74. expect(substituteArgs("$ARGUMENTS", ["日本語", "🎉", "café"])).toBe("日本語 🎉 café");
  75. });
  76. test("should preserve newlines and tabs in argument values", () => {
  77. expect(substituteArgs("$1 $2", ["line1\nline2", "tab\tthere"])).toBe("line1\nline2 tab\tthere");
  78. });
  79. test("should handle consecutive dollar patterns", () => {
  80. expect(substituteArgs("$1$2", ["a", "b"])).toBe("ab");
  81. });
  82. test("should handle quoted arguments with spaces", () => {
  83. expect(substituteArgs("$ARGUMENTS", ["first arg", "second arg"])).toBe("first arg second arg");
  84. });
  85. test("should handle single argument with $ARGUMENTS", () => {
  86. expect(substituteArgs("Test: $ARGUMENTS", ["only"])).toBe("Test: only");
  87. });
  88. test("should handle single argument with $@", () => {
  89. expect(substituteArgs("Test: $@", ["only"])).toBe("Test: only");
  90. });
  91. test("should handle $0 (zero index)", () => {
  92. expect(substituteArgs("$0", ["a", "b"])).toBe("");
  93. });
  94. test("should handle decimal number in pattern (only integer part matches)", () => {
  95. expect(substituteArgs("$1.5", ["a"])).toBe("a.5");
  96. });
  97. test("should handle $ARGUMENTS as part of word", () => {
  98. expect(substituteArgs("pre$ARGUMENTS", ["a", "b"])).toBe("prea b");
  99. });
  100. test("should handle $@ as part of word", () => {
  101. expect(substituteArgs("pre$@", ["a", "b"])).toBe("prea b");
  102. });
  103. test("should handle empty arguments in middle of list", () => {
  104. expect(substituteArgs("$ARGUMENTS", ["a", "", "c"])).toBe("a c");
  105. });
  106. test("should handle trailing and leading spaces in arguments", () => {
  107. expect(substituteArgs("$ARGUMENTS", [" leading ", "trailing "])).toBe(" leading trailing ");
  108. });
  109. test("should handle argument containing pattern partially", () => {
  110. expect(substituteArgs("Prefix $ARGUMENTS suffix", ["ARGUMENTS"])).toBe("Prefix ARGUMENTS suffix");
  111. });
  112. test("should handle non-matching patterns", () => {
  113. expect(substituteArgs("$A $$ $ $ARGS", ["a"])).toBe("$A $$ $ $ARGS");
  114. });
  115. test("should handle case variations (case-sensitive)", () => {
  116. expect(substituteArgs("$arguments $Arguments $ARGUMENTS", ["a", "b"])).toBe("$arguments $Arguments a b");
  117. });
  118. test("should handle both syntaxes in same command with same result", () => {
  119. const args = ["x", "y", "z"];
  120. const result1 = substituteArgs("$@ and $ARGUMENTS", args);
  121. const result2 = substituteArgs("$ARGUMENTS and $@", args);
  122. expect(result1).toBe(result2);
  123. expect(result1).toBe("x y z and x y z");
  124. });
  125. test("should handle very long argument lists", () => {
  126. const args = Array.from({ length: 100 }, (_, i) => `arg${i}`);
  127. const result = substituteArgs("$ARGUMENTS", args);
  128. expect(result).toBe(args.join(" "));
  129. });
  130. test("should handle numbered placeholders with single digit", () => {
  131. expect(substituteArgs("$1 $2 $3", ["a", "b", "c"])).toBe("a b c");
  132. });
  133. test("should handle numbered placeholders with multiple digits", () => {
  134. const args = Array.from({ length: 15 }, (_, i) => `val${i}`);
  135. expect(substituteArgs("$10 $12 $15", args)).toBe("val9 val11 val14");
  136. });
  137. test("should handle escaped dollar signs (literal backslash preserved)", () => {
  138. // Note: No escape mechanism exists - backslash is treated literally
  139. expect(substituteArgs("Price: \\$100", [])).toBe("Price: \\");
  140. });
  141. test("should handle mixed numbered and wildcard placeholders", () => {
  142. expect(substituteArgs("$1: $@ ($ARGUMENTS)", ["first", "second", "third"])).toBe(
  143. "first: first second third (first second third)",
  144. );
  145. });
  146. test("should handle command with no placeholders", () => {
  147. expect(substituteArgs("Just plain text", ["a", "b"])).toBe("Just plain text");
  148. });
  149. test("should handle command with only placeholders", () => {
  150. expect(substituteArgs("$1 $2 $@", ["a", "b", "c"])).toBe("a b a b c");
  151. });
  152. });
  153. // ============================================================================
  154. // substituteArgs - Positional Defaults
  155. // ============================================================================
  156. describe("substituteArgs - positional defaults", () => {
  157. test("should use default when positional arg is missing", () => {
  158. expect(substituteArgs(`List exactly \${1:-7} next steps`, [])).toBe("List exactly 7 next steps");
  159. });
  160. test("should use positional arg when present", () => {
  161. expect(substituteArgs(`List exactly \${1:-7} next steps`, ["3"])).toBe("List exactly 3 next steps");
  162. });
  163. test("should use default when positional arg is empty", () => {
  164. expect(substituteArgs(`Mode: \${1:-brief}`, [""])).toBe("Mode: brief");
  165. });
  166. test("should support multiple positional defaults", () => {
  167. expect(substituteArgs(`\${1:-7} \${2:-brief}`, [])).toBe("7 brief");
  168. expect(substituteArgs(`\${1:-7} \${2:-brief}`, ["3"])).toBe("3 brief");
  169. expect(substituteArgs(`\${1:-7} \${2:-brief}`, ["3", "verbose"])).toBe("3 verbose");
  170. });
  171. test("should not recursively substitute patterns in arg values", () => {
  172. expect(substituteArgs(`\${1:-7}`, ["$ARGUMENTS"])).toBe("$ARGUMENTS");
  173. expect(substituteArgs(`\${1:-7}`, ["$1"])).toBe("$1");
  174. });
  175. test("should not recursively substitute patterns in default values", () => {
  176. expect(substituteArgs(`\${1:-$ARGUMENTS}`, ["a", "b"])).toBe("a");
  177. expect(substituteArgs(`\${3:-$ARGUMENTS}`, ["a", "b"])).toBe("$ARGUMENTS");
  178. });
  179. test("should support defaults with spaces", () => {
  180. expect(substituteArgs(`\${1:-seven steps}`, [])).toBe("seven steps");
  181. });
  182. test("should support out-of-range positional defaults", () => {
  183. expect(substituteArgs(`\${3:-fallback}`, ["a", "b"])).toBe("fallback");
  184. });
  185. test("should mix positional defaults with existing placeholders", () => {
  186. expect(substituteArgs(`$1 \${2:-x} $ARGUMENTS`, ["a"])).toBe("a x a");
  187. });
  188. });
  189. // ============================================================================
  190. // substituteArgs - Array Slicing (Bash-Style)
  191. // ============================================================================
  192. describe("substituteArgs - array slicing", () => {
  193. test(`should slice from index (\${@:N})`, () => {
  194. expect(substituteArgs(`\${@:2}`, ["a", "b", "c", "d"])).toBe("b c d");
  195. expect(substituteArgs(`\${@:1}`, ["a", "b", "c"])).toBe("a b c");
  196. expect(substituteArgs(`\${@:3}`, ["a", "b", "c", "d"])).toBe("c d");
  197. });
  198. test(`should slice with length (\${@:N:L})`, () => {
  199. expect(substituteArgs(`\${@:2:2}`, ["a", "b", "c", "d"])).toBe("b c");
  200. expect(substituteArgs(`\${@:1:1}`, ["a", "b", "c"])).toBe("a");
  201. expect(substituteArgs(`\${@:3:1}`, ["a", "b", "c", "d"])).toBe("c");
  202. expect(substituteArgs(`\${@:2:3}`, ["a", "b", "c", "d", "e"])).toBe("b c d");
  203. });
  204. test("should handle out of range slices", () => {
  205. expect(substituteArgs(`\${@:99}`, ["a", "b"])).toBe("");
  206. expect(substituteArgs(`\${@:5}`, ["a", "b"])).toBe("");
  207. expect(substituteArgs(`\${@:10:5}`, ["a", "b"])).toBe("");
  208. });
  209. test("should handle zero-length slices", () => {
  210. expect(substituteArgs(`\${@:2:0}`, ["a", "b", "c"])).toBe("");
  211. expect(substituteArgs(`\${@:1:0}`, ["a", "b"])).toBe("");
  212. });
  213. test("should handle length exceeding array", () => {
  214. expect(substituteArgs(`\${@:2:99}`, ["a", "b", "c"])).toBe("b c");
  215. expect(substituteArgs(`\${@:1:10}`, ["a", "b"])).toBe("a b");
  216. });
  217. test("should process slice before simple $@", () => {
  218. expect(substituteArgs(`\${@:2} vs $@`, ["a", "b", "c"])).toBe("b c vs a b c");
  219. expect(substituteArgs(`First: \${@:1:1}, All: $@`, ["x", "y", "z"])).toBe("First: x, All: x y z");
  220. });
  221. test("should not recursively substitute slice patterns in args", () => {
  222. expect(substituteArgs(`\${@:1}`, [`\${@:2}`, "test"])).toBe(`\${@:2} test`);
  223. expect(substituteArgs(`\${@:2}`, ["a", `\${@:3}`, "c"])).toBe(`\${@:3} c`);
  224. });
  225. test("should handle mixed usage with positional args", () => {
  226. expect(substituteArgs(`$1: \${@:2}`, ["cmd", "arg1", "arg2"])).toBe("cmd: arg1 arg2");
  227. expect(substituteArgs(`$1 $2 \${@:3}`, ["a", "b", "c", "d"])).toBe("a b c d");
  228. });
  229. test(`should treat \${@:0} as all args`, () => {
  230. expect(substituteArgs(`\${@:0}`, ["a", "b", "c"])).toBe("a b c");
  231. });
  232. test("should handle empty args array", () => {
  233. expect(substituteArgs(`\${@:2}`, [])).toBe("");
  234. expect(substituteArgs(`\${@:1}`, [])).toBe("");
  235. });
  236. test("should handle single arg array", () => {
  237. expect(substituteArgs(`\${@:1}`, ["only"])).toBe("only");
  238. expect(substituteArgs(`\${@:2}`, ["only"])).toBe("");
  239. });
  240. test("should handle slice in middle of text", () => {
  241. expect(substituteArgs(`Process \${@:2} with $1`, ["tool", "file1", "file2"])).toBe(
  242. "Process file1 file2 with tool",
  243. );
  244. });
  245. test("should handle multiple slices in one template", () => {
  246. expect(substituteArgs(`\${@:1:1} and \${@:2}`, ["a", "b", "c"])).toBe("a and b c");
  247. expect(substituteArgs(`\${@:1:2} vs \${@:3:2}`, ["a", "b", "c", "d", "e"])).toBe("a b vs c d");
  248. });
  249. test("should handle quoted arguments in slices", () => {
  250. expect(substituteArgs(`\${@:2}`, ["cmd", "first arg", "second arg"])).toBe("first arg second arg");
  251. });
  252. test("should handle special characters in sliced args", () => {
  253. expect(substituteArgs(`\${@:2}`, ["cmd", "$100", "@user", "#tag"])).toBe("$100 @user #tag");
  254. });
  255. test("should handle unicode in sliced args", () => {
  256. expect(substituteArgs(`\${@:1}`, ["日本語", "🎉", "café"])).toBe("日本語 🎉 café");
  257. });
  258. test("should combine positional, slice, and wildcard placeholders", () => {
  259. const template = `Run $1 on \${@:2:2}, then process $@`;
  260. const args = ["eslint", "file1.ts", "file2.ts", "file3.ts"];
  261. expect(substituteArgs(template, args)).toBe(
  262. "Run eslint on file1.ts file2.ts, then process eslint file1.ts file2.ts file3.ts",
  263. );
  264. });
  265. test("should handle slice with no spacing", () => {
  266. expect(substituteArgs(`prefix\${@:2}suffix`, ["a", "b", "c"])).toBe("prefixb csuffix");
  267. });
  268. test("should handle large slice lengths gracefully", () => {
  269. const args = Array.from({ length: 10 }, (_, i) => `arg${i + 1}`);
  270. expect(substituteArgs(`\${@:5:100}`, args)).toBe("arg5 arg6 arg7 arg8 arg9 arg10");
  271. });
  272. });
  273. // ============================================================================
  274. // parseCommandArgs
  275. // ============================================================================
  276. describe("parseCommandArgs", () => {
  277. test("should parse simple space-separated arguments", () => {
  278. expect(parseCommandArgs("a b c")).toEqual(["a", "b", "c"]);
  279. });
  280. test("should parse quoted arguments with spaces", () => {
  281. expect(parseCommandArgs('"first arg" second')).toEqual(["first arg", "second"]);
  282. });
  283. test("should parse single-quoted arguments", () => {
  284. expect(parseCommandArgs("'first arg' second")).toEqual(["first arg", "second"]);
  285. });
  286. test("should parse mixed quote styles", () => {
  287. expect(parseCommandArgs('"double" \'single\' "double again"')).toEqual(["double", "single", "double again"]);
  288. });
  289. test("should handle empty string", () => {
  290. expect(parseCommandArgs("")).toEqual([]);
  291. });
  292. test("should handle extra spaces", () => {
  293. expect(parseCommandArgs("a b c")).toEqual(["a", "b", "c"]);
  294. });
  295. test("should handle tabs as separators", () => {
  296. expect(parseCommandArgs("a\tb\tc")).toEqual(["a", "b", "c"]);
  297. });
  298. test("should handle quoted empty string", () => {
  299. // Note: Empty quotes are skipped by current implementation
  300. expect(parseCommandArgs('"" " "')).toEqual([" "]);
  301. });
  302. test("should handle arguments with special characters", () => {
  303. expect(parseCommandArgs("$100 @user #tag")).toEqual(["$100", "@user", "#tag"]);
  304. });
  305. test("should handle unicode characters", () => {
  306. expect(parseCommandArgs("日本語 🎉 café")).toEqual(["日本語", "🎉", "café"]);
  307. });
  308. test("should handle newlines in quoted arguments", () => {
  309. expect(parseCommandArgs('"line1\nline2" second')).toEqual(["line1\nline2", "second"]);
  310. });
  311. test("should treat unquoted newlines as separators", () => {
  312. expect(parseCommandArgs("label-2\n\nHere is some description #2.")).toEqual([
  313. "label-2",
  314. "Here",
  315. "is",
  316. "some",
  317. "description",
  318. "#2.",
  319. ]);
  320. });
  321. test("should collapse mixed unquoted whitespace", () => {
  322. expect(parseCommandArgs("a\n\n\tb c")).toEqual(["a", "b", "c"]);
  323. });
  324. test("should handle escaped quotes inside quoted strings", () => {
  325. // Note: This implementation doesn't handle escaped quotes - backslash is literal
  326. expect(parseCommandArgs('"quoted \\"text\\""')).toEqual(["quoted \\text\\"]);
  327. });
  328. test("should handle trailing spaces", () => {
  329. expect(parseCommandArgs("a b c ")).toEqual(["a", "b", "c"]);
  330. });
  331. test("should handle leading spaces", () => {
  332. expect(parseCommandArgs(" a b c")).toEqual(["a", "b", "c"]);
  333. });
  334. });
  335. // ============================================================================
  336. // Integration
  337. // ============================================================================
  338. describe("expandPromptTemplate", () => {
  339. test("should split template arguments on unquoted newlines", () => {
  340. const result = expandPromptTemplate("/arg-test label-2\n\nHere is some description #2.", [
  341. {
  342. name: "arg-test",
  343. description: "test",
  344. content: `- arg1: $1\n- rest: \${@:2}`,
  345. sourceInfo: { path: "/tmp/arg-test.md", source: "local", scope: "temporary", origin: "top-level" },
  346. filePath: "/tmp/arg-test.md",
  347. },
  348. ]);
  349. expect(result).toBe("- arg1: label-2\n- rest: Here is some description #2.");
  350. });
  351. test("should support template command separated from args by newline", () => {
  352. const result = expandPromptTemplate("/arg-test\nlabel-2", [
  353. {
  354. name: "arg-test",
  355. description: "test",
  356. content: "arg1: $1",
  357. sourceInfo: { path: "/tmp/arg-test.md", source: "local", scope: "temporary", origin: "top-level" },
  358. filePath: "/tmp/arg-test.md",
  359. },
  360. ]);
  361. expect(result).toBe("arg1: label-2");
  362. });
  363. });
  364. // ============================================================================
  365. // Integration
  366. // ============================================================================
  367. describe("parseCommandArgs + substituteArgs integration", () => {
  368. test("should parse and substitute together correctly", () => {
  369. const input = 'Button "onClick handler" "disabled support"';
  370. const args = parseCommandArgs(input);
  371. const template = "Create component $1 with features: $ARGUMENTS";
  372. const result = substituteArgs(template, args);
  373. expect(result).toBe("Create component Button with features: Button onClick handler disabled support");
  374. });
  375. test("should handle the example from README", () => {
  376. const input = 'Button "onClick handler" "disabled support"';
  377. const args = parseCommandArgs(input);
  378. const template = "Create a React component named $1 with features: $ARGUMENTS";
  379. const result = substituteArgs(template, args);
  380. expect(result).toBe(
  381. "Create a React component named Button with features: Button onClick handler disabled support",
  382. );
  383. });
  384. test("should produce same result with $@ and $ARGUMENTS", () => {
  385. const args = parseCommandArgs("feature1 feature2 feature3");
  386. const template1 = "Implement: $@";
  387. const template2 = "Implement: $ARGUMENTS";
  388. expect(substituteArgs(template1, args)).toBe(substituteArgs(template2, args));
  389. });
  390. });
  391. // ============================================================================
  392. // loadPromptTemplates - argument-hint frontmatter
  393. // ============================================================================
  394. describe("loadPromptTemplates - argument-hint", () => {
  395. const testDir = join(tmpdir(), `pi-test-prompts-${Date.now()}`);
  396. function writeTemplate(name: string, content: string) {
  397. mkdirSync(testDir, { recursive: true });
  398. writeFileSync(join(testDir, `${name}.md`), content);
  399. }
  400. test("should parse required argument-hint from frontmatter", () => {
  401. writeTemplate(
  402. "pr",
  403. `---
  404. description: Review PRs from URLs with structured issue and code analysis
  405. argument-hint: "<PR-URL>"
  406. ---
  407. You are given one or more GitHub PR URLs: $@`,
  408. );
  409. const templates = loadPromptTemplates({
  410. cwd: process.cwd(),
  411. agentDir: getAgentDir(),
  412. promptPaths: [testDir],
  413. includeDefaults: false,
  414. });
  415. const pr = templates.find((t) => t.name === "pr");
  416. expect(pr).toBeDefined();
  417. expect(pr!.argumentHint).toBe("<PR-URL>");
  418. expect(pr!.description).toBe("Review PRs from URLs with structured issue and code analysis");
  419. });
  420. test("should parse optional argument-hint from frontmatter", () => {
  421. writeTemplate(
  422. "wr",
  423. `---
  424. description: Finish the current task end-to-end with changelog, commit, and push
  425. argument-hint: "[instructions]"
  426. ---
  427. Wrap it. Additional instructions: $ARGUMENTS`,
  428. );
  429. const templates = loadPromptTemplates({
  430. cwd: process.cwd(),
  431. agentDir: getAgentDir(),
  432. promptPaths: [testDir],
  433. includeDefaults: false,
  434. });
  435. const wr = templates.find((t) => t.name === "wr");
  436. expect(wr).toBeDefined();
  437. expect(wr!.argumentHint).toBe("[instructions]");
  438. expect(wr!.description).toBe("Finish the current task end-to-end with changelog, commit, and push");
  439. });
  440. test("should leave argumentHint undefined when not specified", () => {
  441. writeTemplate(
  442. "cl",
  443. `---
  444. description: Audit changelog entries before release
  445. ---
  446. Audit changelog entries for all commits since the last release.`,
  447. );
  448. const templates = loadPromptTemplates({
  449. cwd: process.cwd(),
  450. agentDir: getAgentDir(),
  451. promptPaths: [testDir],
  452. includeDefaults: false,
  453. });
  454. const cl = templates.find((t) => t.name === "cl");
  455. expect(cl).toBeDefined();
  456. expect(cl!.argumentHint).toBeUndefined();
  457. });
  458. test("should ignore empty argument-hint", () => {
  459. writeTemplate(
  460. "empty-hint",
  461. `---
  462. description: A command with empty hint
  463. argument-hint: ""
  464. ---
  465. Do something`,
  466. );
  467. const templates = loadPromptTemplates({
  468. cwd: process.cwd(),
  469. agentDir: getAgentDir(),
  470. promptPaths: [testDir],
  471. includeDefaults: false,
  472. });
  473. const tmpl = templates.find((t) => t.name === "empty-hint");
  474. expect(tmpl).toBeDefined();
  475. expect(tmpl!.argumentHint).toBeUndefined();
  476. });
  477. test("should preserve argument-hint with special characters", () => {
  478. writeTemplate(
  479. "is",
  480. `---
  481. description: Analyze GitHub issues (bugs or feature requests)
  482. argument-hint: "<issue>"
  483. ---
  484. Analyze GitHub issue(s): $ARGUMENTS`,
  485. );
  486. const templates = loadPromptTemplates({
  487. cwd: process.cwd(),
  488. agentDir: getAgentDir(),
  489. promptPaths: [testDir],
  490. includeDefaults: false,
  491. });
  492. const is = templates.find((t) => t.name === "is");
  493. expect(is).toBeDefined();
  494. expect(is!.argumentHint).toBe("<issue>");
  495. });
  496. afterAll(() => {
  497. try {
  498. rmSync(testDir, { recursive: true, force: true });
  499. } catch {}
  500. });
  501. });