Skip to content

Commit d2defeb

Browse files
authored
Chore: Limit auto-test modes to Ask and Off (#276)
* Limit auto-test modes to Ask and Off * Honor legacy auto test persistent modes
1 parent cf85d69 commit d2defeb

2 files changed

Lines changed: 62 additions & 94 deletions

File tree

ai-code.el

Lines changed: 27 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,15 @@ See the later `defcustom' for user-facing documentation and default.")
195195
("Do not write or run tests" . no-test))
196196
"Resolve auto test suffix choices for `ask-me` mode.")
197197

198+
(defconst ai-code--auto-test-type-persistent-choices
199+
'(("Ask every time" . ask-me)
200+
("Off" . nil))
201+
"Persistent choices for `ai-code-auto-test-type`.")
202+
203+
(defconst ai-code--auto-test-type-legacy-persistent-modes
204+
'(test-after-change tdd tdd-with-refactoring)
205+
"Legacy persistent values still honored for backward compatibility.")
206+
198207
(defun ai-code--read-auto-test-type-choice ()
199208
"Read and return one prompt test type for this send action."
200209
(let* ((choice (completing-read "Choose test prompt type for this send: "
@@ -241,33 +250,20 @@ Return one of: `code-change`, `non-code-change`, or `unknown`."
241250

242251
(defun ai-code--resolve-auto-test-type-for-send (&optional prompt-text)
243252
"Resolve the concrete auto test type for current send action for PROMPT-TEXT."
244-
(pcase ai-code-auto-test-type
245-
('ask-me
246-
(if ai-code-use-gptel-classify-prompt
247-
(pcase (ai-code--gptel-classify-prompt-code-change prompt-text)
248-
('code-change (ai-code--read-auto-test-type-choice))
249-
('non-code-change nil)
250-
(_ (ai-code--read-auto-test-type-choice)))
251-
(ai-code--read-auto-test-type-choice)))
252-
('test-after-change
253-
(if ai-code-use-gptel-classify-prompt
254-
(when (eq (ai-code--gptel-classify-prompt-code-change prompt-text)
255-
'code-change)
256-
'test-after-change)
257-
'test-after-change))
258-
('tdd
259-
(if ai-code-use-gptel-classify-prompt
260-
(when (eq (ai-code--gptel-classify-prompt-code-change prompt-text)
261-
'code-change)
262-
'tdd)
263-
'tdd))
264-
('tdd-with-refactoring
265-
(if ai-code-use-gptel-classify-prompt
266-
(when (eq (ai-code--gptel-classify-prompt-code-change prompt-text)
267-
'code-change)
268-
'tdd-with-refactoring)
269-
'tdd-with-refactoring))
270-
(_ nil)))
253+
(if (eq ai-code-auto-test-type 'ask-me)
254+
(ai-code--resolve-ask-auto-test-type-for-send prompt-text)
255+
(and (memq ai-code-auto-test-type
256+
ai-code--auto-test-type-legacy-persistent-modes)
257+
ai-code-auto-test-type)))
258+
259+
(defun ai-code--resolve-ask-auto-test-type-for-send (&optional prompt-text)
260+
"Resolve the send-time auto test type for ask-me mode with PROMPT-TEXT."
261+
(if ai-code-use-gptel-classify-prompt
262+
(pcase (ai-code--gptel-classify-prompt-code-change prompt-text)
263+
('code-change (ai-code--read-auto-test-type-choice))
264+
('non-code-change nil)
265+
(_ (ai-code--read-auto-test-type-choice)))
266+
(ai-code--read-auto-test-type-choice)))
271267

272268
(defun ai-code--auto-test-suffix-for-type (type)
273269
"Return prompt suffix for auto test TYPE."
@@ -309,10 +305,7 @@ Return one of: `code-change`, `non-code-change`, or `unknown`."
309305

310306
(defcustom ai-code-auto-test-type nil
311307
"Select how prompts request tests after code changes."
312-
:type '(choice (const :tag "Use test after code change prompt" test-after-change)
313-
(const :tag "Use TDD Red+Green prompt" tdd)
314-
(const :tag "Use TDD Red+Green+Blue prompt" tdd-with-refactoring)
315-
(const :tag "Ask every time" ask-me)
308+
:type '(choice (const :tag "Ask every time" ask-me)
316309
(const :tag "Off" nil))
317310
:set #'ai-code--test-after-code-change--set
318311
:group 'ai-code)
@@ -417,13 +410,11 @@ Otherwise switch to AI CLI buffer."
417410
:key "T"
418411
:description "Auto test type:"
419412
:reader (lambda (_prompt _initial-input _history)
420-
(let* ((choices '(("Use test after code change prompt" . test-after-change)
421-
("Use TDD Red+Green prompt" . tdd)
422-
("Ask every time" . ask-me)
423-
("Off" . nil)))
413+
(let* ((choices ai-code--auto-test-type-persistent-choices)
424414
(choice (completing-read "Test after code change: "
425415
(mapcar #'car choices)
426-
nil t)))
416+
nil t nil nil
417+
(caar choices))))
427418
(let ((value (cdr (assoc choice choices))))
428419
(ai-code--apply-auto-test-type value)
429420
(message "Auto test type set to %s; prompt suffix is now %s"

test/test_ai-code.el

Lines changed: 35 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -31,26 +31,22 @@
3131

3232
(defvar ai-code--tdd-run-test-after-each-stage-instruction)
3333

34-
(ert-deftest ai-code-test-set-auto-test-type-tdd-updates-suffix ()
35-
"Test that setting auto test type to tdd updates the suffix text."
34+
(ert-deftest ai-code-test-set-auto-test-type-ask-me-clears-persistent-suffix ()
35+
"Test that setting auto test type to ask-me clears the persistent suffix."
3636
(let ((ai-code-auto-test-suffix "old")
3737
(ai-code-auto-test-type nil)
3838
(ai-code--tdd-test-pattern-instruction nil))
39-
(ai-code--apply-auto-test-type 'tdd)
40-
(should (string-match-p "Stage 1 - Red" ai-code-auto-test-suffix))
41-
(should (string-match-p "Stage 2 - Green" ai-code-auto-test-suffix))))
39+
(ai-code--apply-auto-test-type 'ask-me)
40+
(should (eq 'ask-me ai-code-auto-test-type))
41+
(should-not ai-code-auto-test-suffix)))
4242

43-
(ert-deftest ai-code-test-set-auto-test-type-tdd-with-refactoring-updates-suffix ()
44-
"Test that setting auto test type to tdd-with-refactoring updates suffix text."
43+
(ert-deftest ai-code-test-set-auto-test-type-off-clears-persistent-suffix ()
44+
"Test that turning off auto test type clears the persistent suffix."
4545
(let ((ai-code-auto-test-suffix "old")
46-
(ai-code-auto-test-type nil)
47-
(ai-code--tdd-test-pattern-instruction ""))
48-
(ai-code--apply-auto-test-type 'tdd-with-refactoring)
49-
(should (string-match-p
50-
(regexp-quote ai-code--tdd-with-refactoring-extension-instruction)
51-
ai-code-auto-test-suffix))
52-
(should (string-match-p "Stage 3 - Blue" ai-code-auto-test-suffix))
53-
(should (string-match-p "highest-impact cleanup" ai-code-auto-test-suffix))))
46+
(ai-code-auto-test-type 'ask-me))
47+
(ai-code--apply-auto-test-type nil)
48+
(should-not ai-code-auto-test-type)
49+
(should-not ai-code-auto-test-suffix)))
5450

5551
(ert-deftest ai-code-test-resolve-tdd-suffix-includes-strict-stage-contract ()
5652
"Test that TDD suffix names Red and Green stages and forbids skipping."
@@ -69,16 +65,17 @@
6965
(should (string-match-p "SHARED_EACH_STAGE_TEST_INSTRUCTION"
7066
(ai-code--test-after-code-change--resolve-tdd-suffix)))))
7167

72-
(ert-deftest ai-code-test-resolve-auto-test-type-for-send ()
73-
"Test that send-time type resolution is consistent across mode values."
74-
(let ((ai-code-auto-test-type 'test-after-change))
75-
(should (eq 'test-after-change (ai-code--resolve-auto-test-type-for-send))))
76-
(let ((ai-code-auto-test-type 'tdd))
77-
(should (eq 'tdd (ai-code--resolve-auto-test-type-for-send))))
78-
(let ((ai-code-auto-test-type 'tdd-with-refactoring))
79-
(should (eq 'tdd-with-refactoring (ai-code--resolve-auto-test-type-for-send))))
68+
(ert-deftest ai-code-test-resolve-auto-test-type-for-send-off ()
69+
"Test that off mode never resolves a send-time auto test type."
8070
(let ((ai-code-auto-test-type nil))
81-
(should (eq nil (ai-code--resolve-auto-test-type-for-send)))))
71+
(should-not (ai-code--resolve-auto-test-type-for-send))))
72+
73+
(ert-deftest ai-code-test-resolve-auto-test-type-for-send-legacy-persistent-modes ()
74+
"Test that legacy persistent auto test modes still resolve at send time."
75+
(dolist (mode '(test-after-change tdd tdd-with-refactoring))
76+
(let ((ai-code-auto-test-type mode))
77+
(should (eq mode
78+
(ai-code--resolve-auto-test-type-for-send))))))
8279

8380
(ert-deftest ai-code-test-resolve-auto-test-type-for-send-ask-me ()
8481
"Test that ask-me mode resolves by interactive per-send selection."
@@ -119,40 +116,6 @@
119116
(should (eq 'test-after-change
120117
(ai-code--resolve-auto-test-type-for-send "Please update code"))))))
121118

122-
(ert-deftest ai-code-test-resolve-auto-test-type-for-send-fixed-type-gptel-classification-test-after-change ()
123-
"Test that test-after-change mode appends suffix only for GPTel code-change classification."
124-
(let ((ai-code-auto-test-type 'test-after-change)
125-
(ai-code-use-gptel-classify-prompt t))
126-
(cl-letf (((symbol-function 'ai-code--gptel-classify-prompt-code-change)
127-
(lambda (_prompt-text) 'code-change)))
128-
(should (eq 'test-after-change
129-
(ai-code--resolve-auto-test-type-for-send "Refactor this code"))))
130-
(cl-letf (((symbol-function 'ai-code--gptel-classify-prompt-code-change)
131-
(lambda (_prompt-text) 'non-code-change)))
132-
(should (eq nil
133-
(ai-code--resolve-auto-test-type-for-send "Explain this design"))))
134-
(cl-letf (((symbol-function 'ai-code--gptel-classify-prompt-code-change)
135-
(lambda (_prompt-text) 'unknown)))
136-
(should (eq nil
137-
(ai-code--resolve-auto-test-type-for-send "Do something"))))))
138-
139-
(ert-deftest ai-code-test-resolve-auto-test-type-for-send-fixed-type-gptel-classification-tdd ()
140-
"Test that tdd mode appends suffix only for GPTel code-change classification."
141-
(let ((ai-code-auto-test-type 'tdd)
142-
(ai-code-use-gptel-classify-prompt t))
143-
(cl-letf (((symbol-function 'ai-code--gptel-classify-prompt-code-change)
144-
(lambda (_prompt-text) 'code-change)))
145-
(should (eq 'tdd
146-
(ai-code--resolve-auto-test-type-for-send "Implement feature"))))
147-
(cl-letf (((symbol-function 'ai-code--gptel-classify-prompt-code-change)
148-
(lambda (_prompt-text) 'non-code-change)))
149-
(should (eq nil
150-
(ai-code--resolve-auto-test-type-for-send "Summarize this file"))))
151-
(cl-letf (((symbol-function 'ai-code--gptel-classify-prompt-code-change)
152-
(lambda (_prompt-text) 'unknown)))
153-
(should (eq nil
154-
(ai-code--resolve-auto-test-type-for-send "Review architecture"))))))
155-
156119
(ert-deftest ai-code-test-read-auto-test-type-choice-allow-no-test ()
157120
"Test that ask choices support selecting no test run."
158121
(let ((ai-code--auto-test-type-ask-choices
@@ -186,6 +149,20 @@
186149
(should-not (assoc "Test driven development, follow up with refactoring"
187150
ai-code--auto-test-type-ask-choices)))
188151

152+
(ert-deftest ai-code-test-auto-test-type-custom-options-are-ask-or-off ()
153+
"Test that persistent auto test type choices only expose ask-me and off."
154+
(should
155+
(equal
156+
'(choice (const :tag "Ask every time" ask-me)
157+
(const :tag "Off" nil))
158+
(get 'ai-code-auto-test-type 'custom-type))))
159+
160+
(ert-deftest ai-code-test-auto-test-type-persistent-choices-are-ask-or-off ()
161+
"Test that persistent auto test type choices are shared and limited."
162+
(should (equal '(("Ask every time" . ask-me)
163+
("Off" . nil))
164+
ai-code--auto-test-type-persistent-choices)))
165+
189166
(ert-deftest ai-code-test-resolve-auto-test-suffix-for-send-ask-me-tdd-with-refactoring ()
190167
"Test that ask-me can resolve to refactoring TDD suffix."
191168
(let ((ai-code-auto-test-type 'ask-me)

0 commit comments

Comments
 (0)