-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathtest_ai-code.el
More file actions
729 lines (667 loc) · 35.5 KB
/
test_ai-code.el
File metadata and controls
729 lines (667 loc) · 35.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
;;; test_ai-code.el --- Tests for ai-code.el -*- lexical-binding: t; -*-
;; Author: Kang Tu <tninja@gmail.com>
;; SPDX-License-Identifier: Apache-2.0
;;; Commentary:
;; Tests for ai-code.el behavior.
;;; Code:
(require 'ert)
(require 'cl-lib)
(require 'package)
(defun ai-code-test--maybe-prefer-packaged-transient ()
"Prefer the newest packaged Transient when one is installed."
(let* ((pattern (expand-file-name "transient-*" package-user-dir))
(candidates (sort (file-expand-wildcards pattern) #'version<))
(latest (car (last candidates))))
(when latest
(add-to-list 'load-path latest))))
(ai-code-test--maybe-prefer-packaged-transient)
(require 'transient)
(unless (fboundp 'transient-define-group)
(error "AI Code tests require transient-define-group; please install transient >= 0.9.0"))
(require 'ai-code)
(ert-deftest ai-code-test-require-ai-code-loads-harness-module ()
"Test that loading `ai-code` also loads the harness module."
(should (featurep 'ai-code-harness)))
(ert-deftest ai-code-test-autoloads-load-with-harness-custom-unbound ()
"Test that loading autoloads works before harness custom is defined."
(let* ((autoload-file (expand-file-name "ai-code-autoloads.el" default-directory))
(symbols '(ai-code-auto-test-suffix
ai-code-test-after-code-change-suffix))
(saved-states
(mapcar (lambda (symbol)
(list symbol
(boundp symbol)
(when (boundp symbol)
(symbol-value symbol))))
symbols)))
(unwind-protect
(progn
(mapc #'makunbound symbols)
(should
(eq 'loaded
(condition-case nil
(progn
(load autoload-file nil t)
'loaded)
(error 'failed))))
(should (boundp 'ai-code-test-after-code-change-suffix)))
(dolist (state saved-states)
(pcase-let ((`(,symbol ,was-bound ,value) state))
(if was-bound
(set symbol value)
(makunbound symbol)))))))
(ert-deftest ai-code-test-set-auto-test-type-ask-me-clears-persistent-suffix ()
"Test that setting auto test type to ask-me clears the persistent suffix."
(let ((ai-code-auto-test-suffix "old")
(ai-code-auto-test-type nil)
(ai-code--tdd-test-pattern-instruction nil))
(ai-code--apply-auto-test-type 'ask-me)
(should (eq 'ask-me ai-code-auto-test-type))
(should-not ai-code-auto-test-suffix)))
(ert-deftest ai-code-test-set-auto-test-type-off-clears-persistent-suffix ()
"Test that turning off auto test type clears the persistent suffix."
(let ((ai-code-auto-test-suffix "old")
(ai-code-auto-test-type 'ask-me))
(ai-code--apply-auto-test-type nil)
(should-not ai-code-auto-test-type)
(should-not ai-code-auto-test-suffix)))
(ert-deftest ai-code-test-resolve-test-after-change-suffix-includes-diagnostics-for-mcp-backend ()
"Test that test-after-change suffix points to the diagnostics harness file."
(let* ((temp-root (make-temp-file "ai-code-harness-root-" t))
(ai-files-dir (expand-file-name ".ai.code.files/" temp-root))
(ai-code-auto-test-type 'test-after-change)
(ai-code-auto-test-harness-cache-directory nil)
(ai-code-selected-backend 'codex))
(unwind-protect
(cl-letf (((symbol-function 'ai-code--ensure-files-directory)
(lambda () ai-files-dir))
((symbol-function 'ai-code--git-root)
(lambda (&optional _dir) temp-root)))
(let ((suffix (ai-code--resolve-auto-test-suffix-for-send)))
(should (string-match-p
(regexp-quote "@.ai.code.files/harness/test-after-change-diagnostics.v1.md")
suffix))))
(delete-directory temp-root t))))
(ert-deftest ai-code-test-resolve-test-after-change-suffix-omits-diagnostics-for-non-mcp-backend ()
"Test that unsupported backends use the non-diagnostics harness variant."
(let* ((temp-root (make-temp-file "ai-code-harness-root-" t))
(ai-files-dir (expand-file-name ".ai.code.files/" temp-root))
(ai-code-auto-test-type 'test-after-change)
(ai-code-auto-test-harness-cache-directory nil)
(ai-code-selected-backend 'gemini))
(unwind-protect
(cl-letf (((symbol-function 'ai-code--ensure-files-directory)
(lambda () ai-files-dir))
((symbol-function 'ai-code--git-root)
(lambda (&optional _dir) temp-root)))
(let ((suffix (ai-code--resolve-auto-test-suffix-for-send)))
(should (string-match-p
(regexp-quote "@.ai.code.files/harness/test-after-change.v1.md")
suffix))
(should-not (string-match-p "diagnostics.v1.md" suffix))))
(delete-directory temp-root t))))
(ert-deftest ai-code-test-resolve-auto-test-type-for-send-off ()
"Test that off mode never resolves a send-time auto test type."
(let ((ai-code-auto-test-type nil))
(should-not (ai-code--resolve-auto-test-type-for-send))))
(ert-deftest ai-code-test-resolve-auto-test-type-for-send-legacy-persistent-modes ()
"Test that legacy persistent auto test modes still resolve at send time."
(dolist (mode '(test-after-change tdd tdd-with-refactoring))
(let ((ai-code-auto-test-type mode))
(should (eq mode
(ai-code--resolve-auto-test-type-for-send))))))
(ert-deftest ai-code-test-resolve-auto-test-type-for-send-ask-me ()
"Test that ask-me mode resolves by interactive per-send selection."
(let ((ai-code-auto-test-type 'ask-me))
(cl-letf (((symbol-function 'ai-code--read-auto-test-type-choice)
(lambda () 'tdd)))
(should (eq 'tdd (ai-code--resolve-auto-test-type-for-send))))))
(ert-deftest ai-code-test-resolve-auto-test-type-for-send-ask-me-gptel-non-code-change ()
"Test that ask-me mode skips selection when GPTel classifies non-code change."
(let ((ai-code-auto-test-type 'ask-me)
(ai-code-use-gptel-classify-prompt t))
(cl-letf (((symbol-function 'ai-code--gptel-classify-prompt-code-change)
(lambda (_prompt-text) 'non-code-change))
((symbol-function 'ai-code--read-auto-test-type-choice)
(lambda () (ert-fail "Should not ask test type for non-code prompts."))))
(should (eq nil (ai-code--resolve-auto-test-type-for-send "Explain this function"))))))
(ert-deftest ai-code-test-resolve-auto-test-type-for-send-ask-me-gptel-code-change ()
"Test that ask-me mode prompts user to select test type when GPTel classifies code change."
(let ((ai-code-auto-test-type 'ask-me)
(ai-code-use-gptel-classify-prompt t))
(cl-letf (((symbol-function 'ai-code--gptel-classify-prompt-code-change)
(lambda (_prompt-text) 'code-change))
((symbol-function 'ai-code--read-auto-test-type-choice)
(lambda () 'test-after-change)))
(should (eq 'test-after-change
(ai-code--resolve-auto-test-type-for-send "Refactor this function"))))))
(ert-deftest ai-code-test-resolve-auto-test-type-for-send-ask-me-gptel-unknown-fallback ()
"Test that ask-me mode falls back to interactive selection when GPTel is unknown."
(let ((ai-code-auto-test-type 'ask-me)
(ai-code-use-gptel-classify-prompt t))
(cl-letf (((symbol-function 'ai-code--gptel-classify-prompt-code-change)
(lambda (_prompt-text) 'unknown))
((symbol-function 'ai-code--read-auto-test-type-choice)
(lambda () 'test-after-change)))
(should (eq 'test-after-change
(ai-code--resolve-auto-test-type-for-send "Please update code"))))))
(ert-deftest ai-code-test-resolve-auto-test-type-for-send-ask-me-simple-question-match-skips-gptel ()
"Test that question-only prompt markers skip GPTel classification."
(let ((ai-code-auto-test-type 'ask-me)
(ai-code-use-gptel-classify-prompt t))
(cl-letf (((symbol-function 'ai-code--gptel-classify-prompt-code-change)
(lambda (_prompt-text)
(ert-fail "Should not call GPTel for question-only prompt markers.")))
((symbol-function 'ai-code--read-auto-test-type-choice)
(lambda ()
(ert-fail "Should not ask test type for question-only prompts."))))
(should-not
(ai-code--resolve-auto-test-type-for-send
"Explain this function\nNote: This is a question only - please do not modify the code.")))))
(ert-deftest ai-code-test-read-auto-test-type-choice-allow-no-test ()
"Test that ask choices support selecting no test run."
(let ((ai-code--auto-test-type-ask-choices
'(("Run tests after code change" . test-after-change)
("TDD Red + Green (write failing test, then make it pass)" . tdd)
("TDD Red + Green + Blue (refactor after Green)" . tdd-with-refactoring)
("Do not write or run tests" . no-test))))
(cl-letf (((symbol-function 'completing-read)
(lambda (&rest _args) "Do not write or run tests")))
(should (eq 'no-test (ai-code--read-auto-test-type-choice))))))
(ert-deftest ai-code-test-read-auto-test-type-choice-allow-tdd-with-refactoring ()
"Test that ask choices support selecting tdd-with-refactoring."
(let ((ai-code--auto-test-type-ask-choices
'(("Run tests after code change" . test-after-change)
("TDD Red + Green (write failing test, then make it pass)" . tdd)
("TDD Red + Green + Blue (refactor after Green)" . tdd-with-refactoring)
("Do not write or run tests" . no-test))))
(cl-letf (((symbol-function 'completing-read)
(lambda (&rest _args) "TDD Red + Green + Blue (refactor after Green)")))
(should (eq 'tdd-with-refactoring (ai-code--read-auto-test-type-choice))))))
(ert-deftest ai-code-test-resolve-auto-follow-up-suffix-for-send-off ()
"Test that off mode never resolves a discussion follow-up suffix."
(let ((ai-code-discussion-auto-follow-up-enabled nil)
(this-command 'ai-code-ask-question))
(should-not (ai-code--resolve-auto-follow-up-suffix-for-send "Explain this function"))))
(ert-deftest ai-code-test-resolve-auto-follow-up-suffix-for-send-ask-me-non-code-change ()
"Test that ask-me mode can append next-step suggestions for discussion prompts."
(let ((ai-code-discussion-auto-follow-up-enabled t)
(ai-code-use-gptel-classify-prompt t)
(this-command 'ai-code-ask-question))
(cl-letf (((symbol-function 'ai-code--gptel-classify-prompt-code-change)
(lambda (_prompt-text) 'non-code-change))
((symbol-function 'ai-code--read-auto-follow-up-choice)
(lambda () t)))
(should (string-match-p
"3-4 numbered candidate next[[:space:]\n]+steps"
(ai-code--resolve-auto-follow-up-suffix-for-send
"Explain this function")))
(should (string-match-p
"At least 2 candidates must[[:space:]\n]+be AI-actionable items"
(ai-code--resolve-auto-follow-up-suffix-for-send
"Explain this function"))))))
(ert-deftest ai-code-test-resolve-auto-follow-up-suffix-for-send-ask-me-code-change-skips ()
"Test that ask-me mode does not offer next-step suggestions for code-change prompts."
(let ((ai-code-discussion-auto-follow-up-enabled t)
(ai-code-use-gptel-classify-prompt t)
(this-command 'ai-code-ask-question)
(asked nil))
(cl-letf (((symbol-function 'ai-code--gptel-classify-prompt-code-change)
(lambda (_prompt-text) 'code-change))
((symbol-function 'ai-code--read-auto-follow-up-choice)
(lambda ()
(setq asked t)
t)))
(should-not
(ai-code--resolve-auto-follow-up-suffix-for-send
"Please update the code"))
(should-not asked))))
(ert-deftest ai-code-test-resolve-auto-follow-up-suffix-for-send-simple-code-change-match-skips-gptel ()
"Test that code-change prompt markers skip GPTel classification."
(let ((ai-code-discussion-auto-follow-up-enabled t)
(ai-code-use-gptel-classify-prompt t)
(asked nil))
(cl-letf (((symbol-function 'ai-code--gptel-classify-prompt-code-change)
(lambda (_prompt-text)
(ert-fail "Should not call GPTel for code-change prompt markers.")))
((symbol-function 'ai-code--read-auto-follow-up-choice)
(lambda ()
(setq asked t)
t)))
(should-not
(ai-code--resolve-auto-follow-up-suffix-for-send
"Refactor this function\nNote: Please make the code change described above."))
(should-not asked))))
(ert-deftest ai-code-test-resolve-auto-follow-up-suffix-for-send-enabled-for-any-non-code-change-prompt ()
"Test that the feature affects any prompt classified as non-code-change."
(let ((ai-code-discussion-auto-follow-up-enabled t)
(ai-code-use-gptel-classify-prompt t))
(cl-letf (((symbol-function 'ai-code--gptel-classify-prompt-code-change)
(lambda (_prompt-text) 'non-code-change))
((symbol-function 'ai-code--read-auto-follow-up-choice)
(lambda () t)))
(let ((this-command 'ai-code-ask-question))
(should (string-match-p
"The user may also[[:space:]\n]+ignore these options"
(ai-code--resolve-auto-follow-up-suffix-for-send
"Explain this function"))))
(let ((this-command 'ai-code-send-command))
(should (string-match-p
"The user may also[[:space:]\n]+ignore these options"
(ai-code--resolve-auto-follow-up-suffix-for-send
"Summarize this design")))))))
(ert-deftest ai-code-test-write-prompt-appends-follow-up-suffix-for-discussion-prompts ()
"Test that discussion prompts can append next-step suggestions."
(let ((sent-command nil)
(ai-code-discussion-auto-follow-up-enabled t)
(ai-code-use-gptel-classify-prompt t)
(ai-code-use-prompt-suffix t)
(ai-code-prompt-suffix "BASE SUFFIX")
(this-command 'ai-code-ask-question))
(cl-letf (((symbol-function 'ai-code--gptel-classify-prompt-code-change)
(lambda (_prompt-text) 'non-code-change))
((symbol-function 'ai-code--read-auto-follow-up-choice)
(lambda () t))
((symbol-function 'ai-code--get-ai-code-prompt-file-path)
(lambda () nil))
((symbol-function 'ai-code-cli-send-command)
(lambda (command) (setq sent-command command)))
((symbol-function 'ai-code-cli-switch-to-buffer)
(lambda (&rest _args) nil)))
(ai-code--write-prompt-to-file-and-send "Explain this function")
(should (string-match-p "BASE SUFFIX" sent-command))
(should (string-match-p "3-4 numbered candidate next[[:space:]\n]+steps"
sent-command))
(should (string-match-p
"At least 2 candidates must[[:space:]\n]+be AI-actionable items"
sent-command))
(should (string-match-p
"If the user replies with[[:space:]\n]+only a number"
sent-command)))))
(ert-deftest ai-code-test-write-prompt-records-follow-up-suffix-in-prompt-file ()
"Test that discussion follow-up suffix is also recorded in the prompt file."
(let* ((temp-dir (make-temp-file "ai-code-prompt-" t))
(prompt-file (expand-file-name ".ai.code.prompt.org" temp-dir))
(ai-code-discussion-auto-follow-up-enabled t)
(ai-code-use-gptel-classify-prompt t)
(ai-code-use-prompt-suffix t)
(ai-code-prompt-suffix "BASE SUFFIX")
(this-command 'ai-code-ask-question))
(unwind-protect
(cl-letf (((symbol-function 'ai-code--gptel-classify-prompt-code-change)
(lambda (_prompt-text) 'non-code-change))
((symbol-function 'ai-code--read-auto-follow-up-choice)
(lambda () t))
((symbol-function 'ai-code--get-ai-code-prompt-file-path)
(lambda () prompt-file))
((symbol-function 'ai-code-cli-send-command)
(lambda (&rest _args) nil))
((symbol-function 'ai-code-cli-switch-to-buffer)
(lambda (&rest _args) nil)))
(ai-code--write-prompt-to-file-and-send "Explain this function")
(with-temp-buffer
(insert-file-contents prompt-file)
(let ((contents (buffer-string)))
(should (string-match-p "Explain this function" contents))
(should (string-match-p "BASE SUFFIX" contents))
(should (string-match-p "3-4 numbered candidate next[[:space:]\n]+steps"
contents))
(should (string-match-p
"At least 2 candidates must[[:space:]\n]+be AI-actionable items"
contents)))))
(delete-directory temp-dir t))))
(ert-deftest ai-code-test-write-prompt-appends-follow-up-suffix-for-send-command-non-code-change ()
"Test that send-command also gets next-step suggestions when classified non-code-change."
(let ((sent-command nil)
(ai-code-discussion-auto-follow-up-enabled t)
(ai-code-use-gptel-classify-prompt t)
(ai-code-use-prompt-suffix t)
(this-command 'ai-code-send-command))
(cl-letf (((symbol-function 'ai-code--gptel-classify-prompt-code-change)
(lambda (_prompt-text) 'non-code-change))
((symbol-function 'ai-code--read-auto-follow-up-choice)
(lambda () t))
((symbol-function 'ai-code--get-ai-code-prompt-file-path)
(lambda () nil))
((symbol-function 'ai-code-cli-send-command)
(lambda (command) (setq sent-command command)))
((symbol-function 'ai-code-cli-switch-to-buffer)
(lambda (&rest _args) nil)))
(ai-code--write-prompt-to-file-and-send "Summarize this design")
(should (string-match-p "3-4 numbered candidate next[[:space:]\n]+steps"
sent-command))
(should (string-match-p
"either a code change or tool usage"
sent-command)))))
(ert-deftest ai-code-test-classify-prompt-for-send-simple-explain-match-skips-gptel ()
"Test that explain prompts use simple non-code-change heuristics first."
(let ((ai-code-use-gptel-classify-prompt t)
(ai-code-auto-test-type 'ask-me))
(cl-letf (((symbol-function 'ai-code--gptel-classify-prompt-code-change)
(lambda (_prompt-text)
(ert-fail "Should not call GPTel for explain prompts."))))
(should (eq 'non-code-change
(ai-code--classify-prompt-for-send
"Please explain the following file:\nFile: ai-code.el"))))))
(ert-deftest ai-code-test-classify-prompt-for-send-mixed-explain-edit-falls-back-to-gptel ()
"Test that mixed explain/edit prompts still fall back to GPTel."
(let ((ai-code-use-gptel-classify-prompt t)
(ai-code-auto-test-type 'ask-me)
(captured-prompt nil))
(cl-letf (((symbol-function 'ai-code--gptel-classify-prompt-code-change)
(lambda (prompt-text)
(setq captured-prompt prompt-text)
'code-change)))
(should (eq 'code-change
(ai-code--classify-prompt-for-send
"Please explain why this fails, then update the implementation.")))
(should (equal "Please explain why this fails, then update the implementation."
captured-prompt)))))
(ert-deftest ai-code-test-gptel-classifier-prompt-treats-document-edits-as-non-code-change ()
"Test that GPTel instructions reserve CODE_CHANGE for program code edits."
(let ((captured-prompt nil)
(original-require (symbol-function 'require)))
(cl-letf (((symbol-function 'require)
(lambda (feature &optional filename noerror)
(if (eq feature 'gptel)
t
(funcall original-require feature filename noerror))))
((symbol-function 'ai-code-call-gptel-sync)
(lambda (prompt)
(setq captured-prompt prompt)
"NOT_CODE_CHANGE")))
(should (eq 'non-code-change
(ai-code--gptel-classify-prompt-code-change
"Please update the README and other docs.")))
(should
(string-match-p
"Return CODE_CHANGE only for changes to program code or test code\\."
captured-prompt))
(should
(string-match-p
"Treat documentation changes and any other non-program-code actions as NOT_CODE_CHANGE\\."
captured-prompt)))))
(ert-deftest ai-code-test-simple-classifier-reuses-shared-prompt-markers ()
"Test that classifier markers reuse shared prompt-builder constants."
(should (boundp 'ai-code-change--selected-region-note))
(should (boundp 'ai-code-change--generic-note))
(should (boundp 'ai-code-change--selected-files-note))
(should (boundp 'ai-code-discussion--question-only-note))
(should (boundp 'ai-code-discussion--selected-region-note))
(should (boundp 'ai-code-discussion--explain-prompt-prefixes))
(should (equal ai-code--code-change-prompt-markers
(mapcar #'downcase
(list ai-code-change--selected-region-note
ai-code-change--generic-note
ai-code-change--selected-files-note))))
(should (equal ai-code--non-code-change-prompt-markers
(append
(mapcar #'downcase
(list ai-code-discussion--question-only-note
ai-code-discussion--selected-region-note))
(mapcar #'downcase
ai-code-discussion--explain-prompt-prefixes)))))
(ert-deftest ai-code-test-prompt-classification-docstrings-are-not-gptel-specific ()
"Test that prompt classification docstrings are not GPTel-specific."
(should-not
(string-match-p "GPTel prompt classification"
(documentation 'ai-code--classify-prompt-for-send)))
(dolist (fn '(ai-code--resolve-auto-test-type-for-send
ai-code--resolve-ask-auto-test-type-for-send
ai-code--resolve-auto-follow-up-suffix-for-send
ai-code--resolve-auto-test-suffix-for-send))
(should-not
(string-match-p "optional GPTel prompt classification result"
(documentation fn)))))
(ert-deftest ai-code-test-next-step-suggestion-suffix-requires-actionable-items ()
"Test that numbered next-step suggestions require actionable AI items."
(should (string-match-p
"3-4 numbered candidate next[[:space:]\n]+steps"
ai-code-next-step-suggestion-suffix))
(should (string-match-p
"At least 2 candidates must[[:space:]\n]+be AI-actionable items"
ai-code-next-step-suggestion-suffix))
(should (string-match-p
"either a code change or tool usage"
ai-code-next-step-suggestion-suffix)))
(ert-deftest ai-code-test-auto-test-type-ask-choices-use-explicit-red-green-blue-labels ()
"Test that default ask choices use explicit staged TDD labels."
(should (assoc "TDD Red + Green (write failing test, then make it pass)"
ai-code--auto-test-type-ask-choices))
(should (assoc "TDD Red + Green + Blue (refactor after Green)"
ai-code--auto-test-type-ask-choices))
(should-not (assoc "Test driven development: Write test first"
ai-code--auto-test-type-ask-choices))
(should-not (assoc "Test driven development, follow up with refactoring"
ai-code--auto-test-type-ask-choices)))
(ert-deftest ai-code-test-auto-test-type-custom-options-are-ask-or-off ()
"Test that persistent auto test type choices only expose ask-me and off."
(should
(equal
'(choice (const :tag "Ask every time" ask-me)
(const :tag "Off" nil))
(get 'ai-code-auto-test-type 'custom-type))))
(ert-deftest ai-code-test-auto-test-type-persistent-choices-are-ask-or-off ()
"Test that persistent auto test type choices are shared and limited."
(should (equal '(("Ask every time" . ask-me)
("Off" . nil))
ai-code--auto-test-type-persistent-choices)))
(ert-deftest ai-code-test-discussion-auto-follow-up-enabled-custom-option-is-boolean ()
"Test that discussion auto follow-up setting is a boolean toggle."
(should
(equal
'boolean
(get 'ai-code-discussion-auto-follow-up-enabled 'custom-type))))
(ert-deftest ai-code-test-resolve-auto-test-suffix-for-send-ask-me-tdd-with-refactoring ()
"Test that ask-me resolves to the repo-local TDD harness reference."
(let* ((temp-root (make-temp-file "ai-code-harness-root-" t))
(ai-files-dir (expand-file-name ".ai.code.files/" temp-root))
(ai-code-auto-test-harness-cache-directory nil)
(ai-code-auto-test-type 'ask-me)
(ai-code-selected-backend 'codex)
(ai-code--tdd-test-pattern-instruction ""))
(unwind-protect
(cl-letf (((symbol-function 'ai-code--ensure-files-directory)
(lambda () ai-files-dir))
((symbol-function 'ai-code--git-root)
(lambda (&optional _dir) temp-root))
((symbol-function 'ai-code--read-auto-test-type-choice)
(lambda () 'tdd-with-refactoring)))
(let ((suffix (ai-code--resolve-auto-test-suffix-for-send)))
(should (string-match-p
(regexp-quote "@.ai.code.files/harness/tdd-with-refactoring-diagnostics.v1.md")
suffix))))
(delete-directory temp-root t))))
(ert-deftest ai-code-test-resolve-auto-test-suffix-for-send-ask-me-no-test ()
"Test that ask-me can resolve to explicit no-test suffix."
(let ((ai-code-auto-test-type 'ask-me))
(cl-letf (((symbol-function 'ai-code--read-auto-test-type-choice)
(lambda () 'no-test)))
(should (equal "Do not write or run any test."
(ai-code--resolve-auto-test-suffix-for-send))))))
(ert-deftest ai-code-test-write-prompt-ask-me-no-test-appends-explicit-no-test-instruction ()
"Test that ask-me no-test choice appends explicit no-test instruction."
(let ((sent-command nil)
(ai-code-auto-test-type 'ask-me)
(ai-code-use-prompt-suffix t)
(ai-code-prompt-suffix "BASE SUFFIX")
(ai-code-auto-test-suffix "SHOULD NOT APPEAR"))
(cl-letf (((symbol-function 'ai-code--read-auto-test-type-choice)
(lambda () 'no-test))
((symbol-function 'ai-code--get-ai-code-prompt-file-path)
(lambda () nil))
((symbol-function 'ai-code-cli-send-command)
(lambda (command) (setq sent-command command)))
((symbol-function 'ai-code-cli-switch-to-buffer)
(lambda (&rest _args) nil)))
(ai-code--write-prompt-to-file-and-send "Implement feature")
(should (string-match-p "BASE SUFFIX" sent-command))
(should (string-match-p "Do not write or run any test\\." sent-command))
(should-not (string-match-p "SHOULD NOT APPEAR" sent-command)))))
(ert-deftest ai-code-test-select-terminal-updates-terminal-backend-and-syncs-reflow-advice ()
"Test that selecting a terminal backend updates infra state."
(let ((ai-code-backends-infra-terminal-backend 'eat)
sync-called)
(cl-letf (((symbol-function 'completing-read)
(lambda (prompt collection &optional _predicate require-match
_initial-input _hist def &rest _args)
(should (equal prompt "Select terminal: "))
(should require-match)
(should (equal collection '("eat" "vterm" "ghostel")))
(should (equal def "eat"))
"ghostel"))
((symbol-function 'ai-code-backends-infra--sync-reflow-filter-advice)
(lambda ()
(setq sync-called t)))
((symbol-function 'message)
(lambda (&rest _args) nil)))
(ai-code-select-terminal)
(should (eq ai-code-backends-infra-terminal-backend 'ghostel))
(should sync-called))))
(ert-deftest ai-code-test-debug-emacs-runtime-enables-session-tools-and-sends-confirmed-prompt ()
"Debug Emacs runtime should enable MCP debug tools for the active session."
(let (description-prompt
confirm-read-args
enabled-session-id
enabled-eval-elisp
sent-prompt)
(cl-letf (((symbol-function 'y-or-n-p)
(lambda (prompt)
(should (string-match-p "eval Emacs Lisp" prompt))
t))
((symbol-function 'ai-code--active-mcp-session-id)
(lambda () "session-123"))
((symbol-function 'ai-code-mcp-debug-tools-enable-for-session)
(lambda (session-id &optional enable-eval-elisp)
(setq enabled-session-id session-id
enabled-eval-elisp enable-eval-elisp)))
((symbol-function 'ai-code-read-string)
(lambda (prompt &optional initial-input _candidate-list)
(cond
((string-match-p "Describe the Emacs runtime issue" prompt)
(setq description-prompt prompt)
"C-c x runs the wrong interactive command")
((string-match-p "Confirm and edit Emacs runtime debug prompt" prompt)
(setq confirm-read-args (list prompt initial-input))
initial-input)
(t
(ert-fail (format "Unexpected prompt: %s" prompt))))))
((symbol-function 'ai-code--insert-prompt)
(lambda (prompt)
(setq sent-prompt prompt))))
(ai-code-debug-emacs-runtime))
(should (string-match-p "interactive function or a key binding"
description-prompt))
(should (equal enabled-session-id "session-123"))
(should enabled-eval-elisp)
(should (equal (car confirm-read-args)
"Confirm and edit Emacs runtime debug prompt: "))
(should (string-match-p "Use the Emacs MCP tools available in this session"
(cadr confirm-read-args)))
(should (string-match-p "eval_elisp is enabled" (cadr confirm-read-args)))
(should (string-match-p "C-c x runs the wrong interactive command"
sent-prompt))))
(ert-deftest ai-code-test-menu-ai-cli-session-includes-select-terminal-entry ()
"Test that the AI CLI session menu exposes terminal backend selection."
(let ((suffix (transient-get-suffix 'ai-code--menu-ai-cli-session "l")))
(should suffix)
(should (eq (plist-get (cdr suffix) :command)
'ai-code-select-terminal))))
(ert-deftest ai-code-test-menu-other-tools-includes-debug-emacs-runtime-entry ()
"Test that the Other Tools menu exposes Emacs runtime debugging."
(let ((suffix (transient-get-suffix 'ai-code--menu-other-tools "d")))
(should suffix)
(should (eq (plist-get (cdr suffix) :command)
'ai-code-debug-emacs-runtime))
(should (equal (plist-get (cdr suffix) :description)
"Debug Emacs runtime"))))
(ert-deftest ai-code-test-menu-prefix-command-default-layout ()
"Test that the default menu layout uses the original transient."
(let ((ai-code-menu-layout 'default))
(should (eq #'ai-code-menu-default
(ai-code--menu-prefix-command)))))
(ert-deftest ai-code-test-menu-prefix-command-two-columns-layout ()
"Test that the two-column menu layout uses the narrower transient."
(let ((ai-code-menu-layout 'two-columns))
(should (eq #'ai-code-menu-2-columns
(ai-code--menu-prefix-command)))))
(ert-deftest ai-code-test-menu-includes-quickstart-entry ()
"Test that the default menu exposes a quickstart entry."
(should (transient-get-suffix 'ai-code--menu-other-tools
'ai-code-onboarding-open-quickstart)))
(ert-deftest ai-code-test-menu-calls-onboarding-gate-before-opening-transient ()
"Test that `ai-code-menu' runs the onboarding gate before opening the menu."
(let ((gate-called nil)
(called-prefix nil)
(ai-code-menu-layout 'default))
(cl-letf (((symbol-function 'ai-code-onboarding-maybe-show-quickstart)
(lambda ()
(setq gate-called t)))
((symbol-function 'call-interactively)
(lambda (command &rest _args)
(setq called-prefix command))))
(ai-code-menu)
(should gate-called)
(should (eq called-prefix #'ai-code-menu-default)))))
(ert-deftest ai-code-test-menu-keeps-source-buffer-selected-when-auto-showing-quickstart ()
"Auto-showing quickstart should not change the source buffer for the menu."
(let ((ai-code-menu-layout 'default)
(ai-code-onboarding-auto-show t)
(ai-code-onboarding-seen nil)
selected-buffer
source-buffer)
(with-temp-buffer
(setq source-buffer (current-buffer))
(cl-letf (((symbol-function 'pop-to-buffer)
(lambda (buffer &rest _args)
(set-buffer (get-buffer buffer))))
((symbol-function 'call-interactively)
(lambda (_command &rest _args)
(setq selected-buffer (current-buffer)))))
(ai-code-menu)
(should (eq selected-buffer source-buffer))))))
(ert-deftest ai-code-test-menu-prefix-command-fallback-to-default-layout ()
"Test that unknown menu layout values still fall back to the default transient."
(let ((ai-code-menu-layout 'unexpected-layout))
(should (eq #'ai-code-menu-default
(ai-code--menu-prefix-command)))))
(ert-deftest ai-code-test-menu-dispatches-to-selected-layout ()
"Test that `ai-code-menu` dispatches to the configured transient command."
(let ((ai-code-menu-layout 'two-columns)
called-fn)
(cl-letf (((symbol-function 'call-interactively)
(lambda (fn &optional _record-flag _keys)
(setq called-fn fn))))
(ai-code-menu)
(should (eq called-fn #'ai-code-menu-2-columns)))))
(ert-deftest ai-code-test-package-requires-transient-0-9 ()
"Test that ai-code requires Transient 0.9 or newer."
(with-temp-buffer
(insert-file-contents (expand-file-name "ai-code.el" default-directory))
(should (re-search-forward
"Package-Requires: ((emacs \"29\\.1\") (transient \"0\\.9\\.0\") (magit \"2\\.1\\.0\"))"
nil t))))
(ert-deftest ai-code-test-menu-groups-define-four-sections ()
"Test that menu sections are defined as reusable transient groups."
(dolist (group '(ai-code--menu-ai-cli-session
ai-code--menu-actions-with-context
ai-code--menu-agile-development
ai-code--menu-other-tools))
(should (get group 'transient--layout))))
(ert-deftest ai-code-test-menu-prefix-commands-are-interactive ()
"Test that the main menu and layout-specific menus are defined as commands."
(dolist (cmd '(ai-code-menu
ai-code-menu-default
ai-code-menu-2-columns))
(should (fboundp cmd))
(should (commandp cmd))))
(ert-deftest ai-code-test-menu-agile-development-includes-speech-to-text-input ()
"Test that Agile Development menu exposes speech-to-text input."
(let ((suffix (transient-get-suffix 'ai-code--menu-agile-development ":")))
(should suffix)
(should (eq (plist-get (cdr suffix) :command)
'ai-code-speech-to-text-input))
(should (equal (plist-get (cdr suffix) :description)
"Speech to text input"))))
(provide 'test_ai-code)
;;; test_ai-code.el ends here