plan-mode-utils.test.ts 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. import { describe, expect, it } from "vitest";
  2. import {
  3. cleanStepText,
  4. extractDoneSteps,
  5. extractTodoItems,
  6. isSafeCommand,
  7. markCompletedSteps,
  8. type TodoItem,
  9. } from "../examples/extensions/plan-mode/utils.ts";
  10. describe("isSafeCommand", () => {
  11. describe("safe commands", () => {
  12. it("allows basic read commands", () => {
  13. expect(isSafeCommand("ls -la")).toBe(true);
  14. expect(isSafeCommand("cat file.txt")).toBe(true);
  15. expect(isSafeCommand("head -n 10 file.txt")).toBe(true);
  16. expect(isSafeCommand("tail -f log.txt")).toBe(true);
  17. expect(isSafeCommand("grep pattern file")).toBe(true);
  18. expect(isSafeCommand("find . -name '*.ts'")).toBe(true);
  19. });
  20. it("allows git read commands", () => {
  21. expect(isSafeCommand("git status")).toBe(true);
  22. expect(isSafeCommand("git log --oneline")).toBe(true);
  23. expect(isSafeCommand("git diff")).toBe(true);
  24. expect(isSafeCommand("git branch")).toBe(true);
  25. });
  26. it("allows npm/yarn read commands", () => {
  27. expect(isSafeCommand("npm list")).toBe(true);
  28. expect(isSafeCommand("npm outdated")).toBe(true);
  29. expect(isSafeCommand("yarn info react")).toBe(true);
  30. });
  31. it("allows other safe commands", () => {
  32. expect(isSafeCommand("pwd")).toBe(true);
  33. expect(isSafeCommand("echo hello")).toBe(true);
  34. expect(isSafeCommand("wc -l file.txt")).toBe(true);
  35. expect(isSafeCommand("du -sh .")).toBe(true);
  36. expect(isSafeCommand("df -h")).toBe(true);
  37. });
  38. });
  39. describe("destructive commands", () => {
  40. it("blocks file modification commands", () => {
  41. expect(isSafeCommand("rm file.txt")).toBe(false);
  42. expect(isSafeCommand("rm -rf dir")).toBe(false);
  43. expect(isSafeCommand("mv old new")).toBe(false);
  44. expect(isSafeCommand("cp src dst")).toBe(false);
  45. expect(isSafeCommand("mkdir newdir")).toBe(false);
  46. expect(isSafeCommand("touch newfile")).toBe(false);
  47. });
  48. it("blocks git write commands", () => {
  49. expect(isSafeCommand("git add .")).toBe(false);
  50. expect(isSafeCommand("git commit -m 'msg'")).toBe(false);
  51. expect(isSafeCommand("git push")).toBe(false);
  52. expect(isSafeCommand("git checkout main")).toBe(false);
  53. expect(isSafeCommand("git reset --hard")).toBe(false);
  54. });
  55. it("blocks package manager installs", () => {
  56. expect(isSafeCommand("npm install lodash")).toBe(false);
  57. expect(isSafeCommand("yarn add react")).toBe(false);
  58. expect(isSafeCommand("pip install requests")).toBe(false);
  59. expect(isSafeCommand("brew install node")).toBe(false);
  60. });
  61. it("blocks redirects", () => {
  62. expect(isSafeCommand("echo hello > file.txt")).toBe(false);
  63. expect(isSafeCommand("cat foo >> bar")).toBe(false);
  64. expect(isSafeCommand(">file.txt")).toBe(false);
  65. });
  66. it("blocks dangerous commands", () => {
  67. expect(isSafeCommand("sudo rm -rf /")).toBe(false);
  68. expect(isSafeCommand("kill -9 1234")).toBe(false);
  69. expect(isSafeCommand("reboot")).toBe(false);
  70. });
  71. it("blocks editors", () => {
  72. expect(isSafeCommand("vim file.txt")).toBe(false);
  73. expect(isSafeCommand("nano file.txt")).toBe(false);
  74. expect(isSafeCommand("code .")).toBe(false);
  75. });
  76. });
  77. describe("edge cases", () => {
  78. it("requires command to be in safe list (not just non-destructive)", () => {
  79. expect(isSafeCommand("unknown-command")).toBe(false);
  80. expect(isSafeCommand("my-script.sh")).toBe(false);
  81. });
  82. it("handles commands with leading whitespace", () => {
  83. expect(isSafeCommand(" ls -la")).toBe(true);
  84. expect(isSafeCommand(" rm file")).toBe(false);
  85. });
  86. });
  87. });
  88. describe("cleanStepText", () => {
  89. it("removes markdown bold/italic", () => {
  90. expect(cleanStepText("**bold text**")).toBe("Bold text");
  91. expect(cleanStepText("*italic text*")).toBe("Italic text");
  92. });
  93. it("removes markdown code", () => {
  94. expect(cleanStepText("run `npm install`")).toBe("Npm install"); // "run" is stripped as action word
  95. expect(cleanStepText("check the `config.json` file")).toBe("Config.json file");
  96. });
  97. it("removes leading action words", () => {
  98. expect(cleanStepText("Create the new file")).toBe("New file");
  99. expect(cleanStepText("Run the tests")).toBe("Tests");
  100. expect(cleanStepText("Check the status")).toBe("Status");
  101. });
  102. it("capitalizes first letter", () => {
  103. expect(cleanStepText("update config")).toBe("Config");
  104. });
  105. it("truncates long text", () => {
  106. const longText = "This is a very long step description that exceeds the maximum allowed length for display";
  107. const result = cleanStepText(longText);
  108. expect(result.length).toBe(50);
  109. expect(result.endsWith("...")).toBe(true);
  110. });
  111. it("normalizes whitespace", () => {
  112. expect(cleanStepText("multiple spaces here")).toBe("Multiple spaces here");
  113. });
  114. });
  115. describe("extractTodoItems", () => {
  116. it("extracts numbered items after Plan: header", () => {
  117. const message = `Here's what we'll do:
  118. Plan:
  119. 1. First step here
  120. 2. Second step here
  121. 3. Third step here`;
  122. const items = extractTodoItems(message);
  123. expect(items).toHaveLength(3);
  124. expect(items[0].step).toBe(1);
  125. expect(items[0].text).toBe("First step here");
  126. expect(items[0].completed).toBe(false);
  127. });
  128. it("handles bold Plan header", () => {
  129. const message = `**Plan:**
  130. 1. Do something`;
  131. const items = extractTodoItems(message);
  132. expect(items).toHaveLength(1);
  133. });
  134. it("handles parenthesis-style numbering", () => {
  135. const message = `Plan:
  136. 1) First item
  137. 2) Second item`;
  138. const items = extractTodoItems(message);
  139. expect(items).toHaveLength(2);
  140. });
  141. it("returns empty array without Plan header", () => {
  142. const message = `Here are some steps:
  143. 1. First step
  144. 2. Second step`;
  145. const items = extractTodoItems(message);
  146. expect(items).toHaveLength(0);
  147. });
  148. it("filters out short items", () => {
  149. const message = `Plan:
  150. 1. OK
  151. 2. This is a proper step`;
  152. const items = extractTodoItems(message);
  153. expect(items).toHaveLength(1);
  154. expect(items[0].text).toContain("proper");
  155. });
  156. it("filters out code-like items", () => {
  157. const message = `Plan:
  158. 1. \`npm install\`
  159. 2. Run the build process`;
  160. const items = extractTodoItems(message);
  161. expect(items).toHaveLength(1);
  162. });
  163. });
  164. describe("extractDoneSteps", () => {
  165. it("extracts single DONE marker", () => {
  166. const message = "I've completed the first step [DONE:1]";
  167. expect(extractDoneSteps(message)).toEqual([1]);
  168. });
  169. it("extracts multiple DONE markers", () => {
  170. const message = "Did steps [DONE:1] and [DONE:2] and [DONE:3]";
  171. expect(extractDoneSteps(message)).toEqual([1, 2, 3]);
  172. });
  173. it("handles case insensitivity", () => {
  174. const message = "[done:1] [DONE:2] [Done:3]";
  175. expect(extractDoneSteps(message)).toEqual([1, 2, 3]);
  176. });
  177. it("returns empty array with no markers", () => {
  178. const message = "No markers here";
  179. expect(extractDoneSteps(message)).toEqual([]);
  180. });
  181. it("ignores malformed markers", () => {
  182. const message = "[DONE:abc] [DONE:] [DONE:1]";
  183. expect(extractDoneSteps(message)).toEqual([1]);
  184. });
  185. });
  186. describe("markCompletedSteps", () => {
  187. it("marks matching items as completed", () => {
  188. const items: TodoItem[] = [
  189. { step: 1, text: "First", completed: false },
  190. { step: 2, text: "Second", completed: false },
  191. { step: 3, text: "Third", completed: false },
  192. ];
  193. const count = markCompletedSteps("[DONE:1] [DONE:3]", items);
  194. expect(count).toBe(2);
  195. expect(items[0].completed).toBe(true);
  196. expect(items[1].completed).toBe(false);
  197. expect(items[2].completed).toBe(true);
  198. });
  199. it("returns count of completed items", () => {
  200. const items: TodoItem[] = [{ step: 1, text: "First", completed: false }];
  201. expect(markCompletedSteps("[DONE:1]", items)).toBe(1);
  202. expect(markCompletedSteps("no markers", items)).toBe(0);
  203. });
  204. it("ignores markers for non-existent steps", () => {
  205. const items: TodoItem[] = [{ step: 1, text: "First", completed: false }];
  206. const count = markCompletedSteps("[DONE:99]", items);
  207. expect(count).toBe(1); // Still counts the marker found
  208. expect(items[0].completed).toBe(false); // But doesn't mark anything
  209. });
  210. it("doesn't double-complete already completed items", () => {
  211. const items: TodoItem[] = [{ step: 1, text: "First", completed: true }];
  212. markCompletedSteps("[DONE:1]", items);
  213. expect(items[0].completed).toBe(true);
  214. });
  215. });