Skip to content

Commit 5e15926

Browse files
committed
Delete corrupted backends history and add tests
1 parent 656676c commit 5e15926

2 files changed

Lines changed: 56 additions & 1 deletion

File tree

ai-code-backends.el

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -436,7 +436,9 @@ configuration paths, upgrade commands, and skill-install commands."
436436
(let ((content (buffer-string)))
437437
(unless (string-empty-p content)
438438
(read content))))
439-
(error nil))
439+
(error
440+
(ignore-errors (delete-file ai-code-backends-history-file))
441+
nil))
440442
nil))
441443

442444
(defun ai-code--save-backend-history (backend)

test/test_ai-code-backends.el

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -448,6 +448,59 @@
448448
(when (file-exists-p temp-file)
449449
(delete-file temp-file)))))
450450

451+
(ert-deftest ai-code-test-backends-corrupted-history-file-deleted ()
452+
"Test that if the history file contains invalid Lisp, load-backends-history deletes it and returns nil."
453+
(let* ((temp-file (make-temp-file "ai-code-backends-history-"))
454+
(ai-code-backends-history-file temp-file))
455+
(unwind-protect
456+
(progn
457+
;; Write corrupted content
458+
(write-region "(invalid-lisp" nil temp-file nil 'silent)
459+
(should (file-exists-p temp-file))
460+
461+
;; Load should return nil
462+
(should-not (ai-code--load-backends-history))
463+
464+
;; File should have been deleted
465+
(should-not (file-exists-p temp-file)))
466+
(when (file-exists-p temp-file)
467+
(delete-file temp-file)))))
468+
469+
(ert-deftest ai-code-test-select-backend-partial-history-keeps-all-backends ()
470+
"Test that even if the history file only covers some backends or contains invalid keys, all backends are still available as choices."
471+
(let* ((temp-file (make-temp-file "ai-code-backends-history-"))
472+
(ai-code-backends-history-file temp-file)
473+
(ai-code-backends '((backend-a :label "Backend A" :start ignore :switch ignore :send ignore)
474+
(backend-b :label "Backend B" :start ignore :switch ignore :send ignore)
475+
(backend-c :label "Backend C" :start ignore :switch ignore :send ignore)))
476+
(ai-code-selected-backend 'backend-a)
477+
(captured-candidates nil)
478+
(selected-choice "Backend C"))
479+
(unwind-protect
480+
(progn
481+
;; Write partial/invalid history: only backend-b and some invalid key
482+
(with-temp-file temp-file
483+
(insert (prin1-to-string '(backend-b invalid-backend-key))))
484+
485+
(cl-letf (((symbol-function 'completing-read)
486+
(lambda (_prompt candidates &rest _args)
487+
(setq captured-candidates candidates)
488+
selected-choice))
489+
((symbol-function 'ai-code-onboarding-show-backend-switch-hint) #'ignore)
490+
((symbol-function 'message) #'ignore))
491+
(ai-code-select-backend)
492+
493+
;; 1. The selection should complete successfully
494+
(should (eq ai-code-selected-backend 'backend-c))
495+
;; 2. Candidates list must still include all three backends:
496+
;; - First: Current effective backend (backend-a)
497+
;; - Then: history items that are valid (backend-b)
498+
;; - Last: other remaining backends (backend-c)
499+
;; So candidates should be '("Backend A" "Backend B" "Backend C")
500+
(should (equal captured-candidates '("Backend A" "Backend B" "Backend C")))))
501+
(when (file-exists-p temp-file)
502+
(delete-file temp-file)))))
503+
451504
(provide 'test_ai-code-backends)
452505

453506
;;; test_ai-code-backends.el ends here

0 commit comments

Comments
 (0)