|
260 | 260 | "Verify Dired refactoring suggestions include selected files." |
261 | 261 | (with-temp-buffer |
262 | 262 | (let (captured-prompt) |
263 | | - (cl-letf (((symbol-function 'ai-code-read-string) |
264 | | - (lambda (_prompt &optional initial _candidates) initial)) |
| 263 | + (cl-letf (((symbol-function 'completing-read) |
| 264 | + (lambda (&rest _) "Improve readability and testability")) |
| 265 | + ((symbol-function 'ai-code-read-string) |
| 266 | + (lambda (_prompt &optional initial-input _candidates) |
| 267 | + initial-input)) |
265 | 268 | ((symbol-function 'ai-code--git-root) |
266 | 269 | (lambda (&optional _dir) "/repo/")) |
267 | 270 | ((symbol-function 'ai-code--insert-prompt) |
|
280 | 283 | (should (string-match-p "\nFiles:\n@src/foo\\.el\n@test/bar\\.el" |
281 | 284 | (subst-char-in-string ?\\ ?/ captured-prompt))))))) |
282 | 285 |
|
| 286 | +(ert-deftest ai-code-test-handle-ask-llm-suggestion-offers-common-refactoring-goals () |
| 287 | + "Verify refactoring suggestion prompt selects a short description first." |
| 288 | + (with-temp-buffer |
| 289 | + (let (captured-short-descriptions captured-selected-description |
| 290 | + captured-read-string-initial-input captured-prompt) |
| 291 | + (cl-letf (((symbol-function 'ai-code-read-string) |
| 292 | + (lambda (_prompt &optional initial-input _candidates) |
| 293 | + (setq captured-read-string-initial-input initial-input) |
| 294 | + "Custom edited prompt")) |
| 295 | + ((symbol-function 'completing-read) |
| 296 | + (lambda (_prompt collection &rest _) |
| 297 | + (setq captured-short-descriptions collection) |
| 298 | + (setq captured-selected-description "Reduce complexity") |
| 299 | + captured-selected-description)) |
| 300 | + ((symbol-function 'ai-code--insert-prompt) |
| 301 | + (lambda (text) |
| 302 | + (setq captured-prompt text) |
| 303 | + t))) |
| 304 | + (ai-code--handle-ask-llm-suggestion |
| 305 | + '(:region-active nil |
| 306 | + :current-function "my-function" |
| 307 | + :file-name "/repo/src/foo.el" |
| 308 | + :dired-targets nil) |
| 309 | + nil) |
| 310 | + (should (equal captured-short-descriptions |
| 311 | + '("General refactoring analysis" |
| 312 | + "Improve readability and testability" |
| 313 | + "Reduce complexity" |
| 314 | + "Remove duplication" |
| 315 | + "Clarify naming and responsibilities"))) |
| 316 | + (should (equal captured-selected-description "Reduce complexity")) |
| 317 | + (should (equal captured-read-string-initial-input |
| 318 | + "Analyze the code context below. Focus on reducing complexity and simplifying control flow. Do not change code logic. Suggest the most impactful refactoring technique and explain why.")) |
| 319 | + (should (string-match-p "Custom edited prompt Context: Function 'my-function'" |
| 320 | + captured-prompt)))))) |
| 321 | + |
| 322 | +(ert-deftest ai-code-test-handle-ask-llm-suggestion-default-prompt-mentions-easy-to-understand () |
| 323 | + "Verify the default refactoring prompt mentions making code easy to understand." |
| 324 | + (with-temp-buffer |
| 325 | + (let (captured-read-string-initial-input) |
| 326 | + (cl-letf (((symbol-function 'completing-read) |
| 327 | + (lambda (&rest _) ai-code--refactoring-suggestion-default-label)) |
| 328 | + ((symbol-function 'ai-code-read-string) |
| 329 | + (lambda (_prompt &optional initial-input _candidates) |
| 330 | + (setq captured-read-string-initial-input initial-input) |
| 331 | + initial-input)) |
| 332 | + ((symbol-function 'ai-code--insert-prompt) |
| 333 | + (lambda (_text) t))) |
| 334 | + (ai-code--handle-ask-llm-suggestion |
| 335 | + '(:region-active nil |
| 336 | + :current-function "my-function" |
| 337 | + :file-name "/repo/src/foo.el" |
| 338 | + :dired-targets nil) |
| 339 | + nil) |
| 340 | + (should (string-match-p "easy to understand" |
| 341 | + captured-read-string-initial-input)))))) |
| 342 | + |
283 | 343 | (provide 'test_ai-code-agile) |
284 | 344 | ;;; test_ai-code-agile.el ends here |
0 commit comments