|
448 | 448 | (when (file-exists-p temp-file) |
449 | 449 | (delete-file temp-file))))) |
450 | 450 |
|
| 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 | + |
451 | 504 | (provide 'test_ai-code-backends) |
452 | 505 |
|
453 | 506 | ;;; test_ai-code-backends.el ends here |
0 commit comments