@@ -238,6 +238,63 @@ When .gitignore is missing some entries, they should be added."
238238 " https://github.com/acme/demo/pull/999" )))
239239 (should (string-match-p " Review this pull request\\ ." prompt))))
240240
241+ (ert-deftest ai-code-test-git-worktree-branch-creates-repo-directory-and-adds-worktree ()
242+ " Create repo worktree directory and invoke git worktree add with expected path."
243+ (let* ((temp-worktree-root (make-temp-file " ai-code-worktree-root-" t ))
244+ (ai-code-git-worktree-root temp-worktree-root)
245+ (git-root " /tmp/sample-repo/" )
246+ (branch " feature/new-branch" )
247+ (start-point " main" )
248+ (repo-dir (expand-file-name " sample-repo" temp-worktree-root))
249+ (worktree-path (expand-file-name branch repo-dir))
250+ (worktree-parent-dir (file-name-directory worktree-path))
251+ captured-git-args
252+ captured-visited-path)
253+ (unwind-protect
254+ (cl-letf (((symbol-function 'ai-code--validate-git-repository )
255+ (lambda () git-root))
256+ ((symbol-function 'magit-run-git )
257+ (lambda (&rest _args )
258+ (ert-fail " `magit-run-git' should not be used for worktree add status check" )))
259+ ((symbol-function 'magit-call-git )
260+ (lambda (&rest args )
261+ (setq captured-git-args args)
262+ 0 ))
263+ ((symbol-function 'magit-diff-visit-directory )
264+ (lambda (path )
265+ (setq captured-visited-path path))))
266+ (should-not (file-directory-p repo-dir))
267+ (ai-code-git-worktree-branch branch start-point)
268+ (should (file-directory-p repo-dir))
269+ (should (file-directory-p worktree-parent-dir))
270+ (should (equal captured-git-args
271+ (list " worktree"
272+ " add"
273+ " -b"
274+ branch
275+ (file-truename worktree-path)
276+ start-point)))
277+ (should (equal captured-visited-path worktree-path)))
278+ (delete-directory temp-worktree-root t ))))
279+
280+ (ert-deftest ai-code-test-git-worktree-action-without-prefix-calls-worktree-branch ()
281+ " Without prefix arg, dispatch to `ai-code-git-worktree-branch' ."
282+ (let (captured-fn)
283+ (cl-letf (((symbol-function 'call-interactively )
284+ (lambda (fn &optional _record-flag _keys )
285+ (setq captured-fn fn))))
286+ (ai-code-git-worktree-action nil )
287+ (should (eq captured-fn #'ai-code-git-worktree-branch )))))
288+
289+ (ert-deftest ai-code-test-git-worktree-action-with-prefix-calls-magit-worktree-status ()
290+ " With prefix arg, dispatch to `magit-worktree-status' ."
291+ (let (captured-fn)
292+ (cl-letf (((symbol-function 'call-interactively )
293+ (lambda (fn &optional _record-flag _keys )
294+ (setq captured-fn fn))))
295+ (ai-code-git-worktree-action '(4 ))
296+ (should (eq captured-fn #'magit-worktree-status )))))
297+
241298(defun ai-code-test--run-pull-or-review-diff-file (choice pr-url &optional review-mode-choice )
242299 " Run `ai-code-pull-or-review-diff-file' with CHOICE and optional PR-URL.
243300REVIEW-MODE-CHOICE is used for review mode selection when prompted.
0 commit comments