custom-commands.test.ts 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. import { describe, expect, it } from "vitest";
  2. import {
  3. expandCustomCommandPrompt,
  4. splitCustomCommandArguments,
  5. type CustomCommandContent,
  6. } from "../src/commands/index.js";
  7. describe("custom command expansion", () => {
  8. it("expands all and positional arguments without shell parsing", () => {
  9. const command = createCommand("Review $ARGUMENTS then run $1 for $2.");
  10. const expanded = expandCustomCommandPrompt({
  11. args: '"auth flow" high',
  12. command,
  13. });
  14. expect(expanded.argumentCount).toBe(2);
  15. expect(expanded.prompt).toContain('Review "auth flow" high');
  16. expect(expanded.prompt).toContain("then run auth flow for high");
  17. });
  18. it("appends arguments when the template has no placeholders", () => {
  19. const expanded = expandCustomCommandPrompt({
  20. args: "src/auth.ts",
  21. command: createCommand("Review the code."),
  22. });
  23. expect(expanded.prompt).toContain("Review the code.");
  24. expect(expanded.prompt).toContain("User arguments:\nsrc/auth.ts");
  25. });
  26. it("fails closed for shell expansion syntax", () => {
  27. expect(() =>
  28. expandCustomCommandPrompt({
  29. args: "",
  30. command: createCommand("Status: !`git status`"),
  31. }),
  32. ).toThrow(/unsupported shell expansion/);
  33. });
  34. it("splits quoted arguments consistently", () => {
  35. expect(splitCustomCommandArguments("'one two' three\\ four")).toEqual([
  36. "one two",
  37. "three four",
  38. ]);
  39. });
  40. });
  41. function createCommand(content: string): CustomCommandContent {
  42. return {
  43. bytesRead: content.length,
  44. content,
  45. metadata: {
  46. allowedTools: [],
  47. description: "Demo command",
  48. disableNonInteractive: false,
  49. frontmatterKeys: [],
  50. name: "demo",
  51. path: "/tmp/demo.md",
  52. rootPath: "/tmp",
  53. scope: "project",
  54. source: "zcode",
  55. },
  56. sizeBytes: content.length,
  57. truncated: false,
  58. };
  59. }