aboutsummaryrefslogtreecommitdiff
path: root/tests/test-gptel-tools-git-log.el
diff options
context:
space:
mode:
authorCraig Jennings <c@cjennings.net>2026-05-16 04:26:20 -0500
committerCraig Jennings <c@cjennings.net>2026-05-16 04:26:20 -0500
commitceeae9b5e2625e23e6e3792d06a6c8122a36d18b (patch)
tree236a650ab1f044a2cac9556f9a8d312bd85adb1d /tests/test-gptel-tools-git-log.el
parentcce6e8265db0eea8af192b3737b50b81c39a9c0b (diff)
downloaddotemacs-ceeae9b5e2625e23e6e3792d06a6c8122a36d18b.tar.gz
dotemacs-ceeae9b5e2625e23e6e3792d06a6c8122a36d18b.zip
feat(gptel-tools): wire git_status / git_log / git_diff as local tools
Three read-only git context tools so gptel can see what's changed without me pasting `git status` / `git log` / `git diff` output into every chat turn. Builds the first batch from the ADOPT bucket in `docs/design/gptel-tools-shortlist.org`. Shape per tool: - `gptel-tools/git_status.el` — `git status --short --branch` for a directory inside a git working tree under HOME. Returns the porcelain output, or a "Clean working tree" marker when only the branch line is present. - `gptel-tools/git_log.el` — `git log --oneline -nN` with an optional `--since` filter. N defaults to 20, capped at 100; nil / non- integer / out-of-range N falls back to the default. - `gptel-tools/git_diff.el` — `git diff [REF1 [REF2]] [-- FILE]`. Output capped at ~500KB so a runaway diff can't blow up context; truncation is reported inline. Validation is uniform: path must resolve under HOME, must be a directory, must be inside a git working tree (verified via `git rev-parse --is-inside-work-tree`). Color is disabled via `-c color.ui=false` at the git level (`git status` doesn't accept `--no-color` directly). Tests run against real temp git repos created via `process-file`, not mocked — there's nothing in gptel-tools/git_*.el that's process-mockable in a meaningful way, and a real `git init` + a couple of commits is cheaper than building a fake. 31 tests total: 7 for git_status, 11 for git_log, 13 for git_diff. Wired into `cj/gptel-local-tool-features` so gptel exposes the three tools on next restart.
Diffstat (limited to 'tests/test-gptel-tools-git-log.el')
-rw-r--r--tests/test-gptel-tools-git-log.el135
1 files changed, 135 insertions, 0 deletions
diff --git a/tests/test-gptel-tools-git-log.el b/tests/test-gptel-tools-git-log.el
new file mode 100644
index 00000000..708819b6
--- /dev/null
+++ b/tests/test-gptel-tools-git-log.el
@@ -0,0 +1,135 @@
+;;; test-gptel-tools-git-log.el --- Tests for git_log gptel tool -*- lexical-binding: t; -*-
+
+;;; Commentary:
+;; Tests run against real temp git repos under HOME via `process-file'.
+
+;;; Code:
+
+(require 'ert)
+(require 'cl-lib)
+
+(eval-and-compile
+ (add-to-list 'load-path (expand-file-name "tests" user-emacs-directory))
+ (add-to-list 'load-path (expand-file-name "gptel-tools" user-emacs-directory))
+ (setq load-prefer-newer t)
+ (unless (featurep 'gptel)
+ (defvar gptel-tools nil)
+ (defun gptel-make-tool (&rest _args) nil)
+ (defun gptel-get-tool (&rest _args) nil)
+ (provide 'gptel)))
+
+(require 'git_log)
+
+;; ---------- helpers
+
+(defun test-gptel-tools-git-log--with-repo (commit-count fn)
+ "Create a temp git repo under HOME with COMMIT-COUNT empty commits.
+Call FN with the absolute path, clean up after."
+ (let* ((name (format ".test-gptel-tools-git-log-%s"
+ (format-time-string "%s%N")))
+ (dir (expand-file-name name "~")))
+ (unwind-protect
+ (progn
+ (make-directory dir)
+ (let ((default-directory dir))
+ (call-process "git" nil nil nil "init" "--quiet")
+ (call-process "git" nil nil nil "config" "user.email" "test@x")
+ (call-process "git" nil nil nil "config" "user.name" "Test")
+ (dotimes (i commit-count)
+ (call-process "git" nil nil nil "commit" "--allow-empty"
+ "--quiet" "-m" (format "commit %d" i))))
+ (funcall fn dir))
+ (when (file-exists-p dir) (delete-directory dir t)))))
+
+;; ---------- effective-count
+
+(ert-deftest test-gptel-tools-git-log-effective-count-defaults-on-nil ()
+ "Boundary: nil N → default count."
+ (should (= (cj/gptel-git-log--effective-count nil)
+ cj/gptel-git-log--default-count)))
+
+(ert-deftest test-gptel-tools-git-log-effective-count-defaults-on-non-integer ()
+ "Boundary: non-integer N → default count."
+ (should (= (cj/gptel-git-log--effective-count "ten")
+ cj/gptel-git-log--default-count))
+ (should (= (cj/gptel-git-log--effective-count 0.5)
+ cj/gptel-git-log--default-count)))
+
+(ert-deftest test-gptel-tools-git-log-effective-count-clamps-low ()
+ "Boundary: N below 1 → default count."
+ (should (= (cj/gptel-git-log--effective-count 0)
+ cj/gptel-git-log--default-count))
+ (should (= (cj/gptel-git-log--effective-count -5)
+ cj/gptel-git-log--default-count)))
+
+(ert-deftest test-gptel-tools-git-log-effective-count-caps-high ()
+ "Boundary: N above max → max."
+ (should (= (cj/gptel-git-log--effective-count 1000)
+ cj/gptel-git-log--max-count)))
+
+(ert-deftest test-gptel-tools-git-log-effective-count-normal ()
+ "Normal: a valid N passes through."
+ (should (= (cj/gptel-git-log--effective-count 5) 5)))
+
+;; ---------- validate-path
+
+(ert-deftest test-gptel-tools-git-log-validate-path-normal ()
+ "Normal: validator accepts a git working tree."
+ (test-gptel-tools-git-log--with-repo
+ 1
+ (lambda (dir)
+ (should (equal (cj/gptel-git-log--validate-path dir) dir)))))
+
+(ert-deftest test-gptel-tools-git-log-validate-path-error-outside-home ()
+ "Error: path outside HOME signals."
+ (should-error (cj/gptel-git-log--validate-path "/etc")))
+
+(ert-deftest test-gptel-tools-git-log-validate-path-error-not-a-repo ()
+ "Error: directory outside any git working tree signals."
+ (let ((dir (make-temp-file
+ (expand-file-name ".test-gptel-tools-git-log-" "~") t)))
+ (unwind-protect
+ (should-error (cj/gptel-git-log--validate-path dir))
+ (when (file-exists-p dir) (delete-directory dir t)))))
+
+;; ---------- run
+
+(ert-deftest test-gptel-tools-git-log-run-default-count ()
+ "Normal: default count limits output to that many commits."
+ (test-gptel-tools-git-log--with-repo
+ 30
+ (lambda (dir)
+ (let* ((out (cj/gptel-git-log--run dir))
+ (lines (split-string (string-trim out) "\n")))
+ (should (= (length lines) cj/gptel-git-log--default-count))))))
+
+(ert-deftest test-gptel-tools-git-log-run-honors-n ()
+ "Normal: an explicit N limits output to N commits."
+ (test-gptel-tools-git-log--with-repo
+ 10
+ (lambda (dir)
+ (let* ((out (cj/gptel-git-log--run dir 3))
+ (lines (split-string (string-trim out) "\n")))
+ (should (= (length lines) 3))))))
+
+(ert-deftest test-gptel-tools-git-log-run-empty-repo ()
+ "Boundary: a repo with no commits returns the empty-result marker."
+ (let* ((name (format ".test-gptel-tools-git-log-empty-%s"
+ (format-time-string "%s%N")))
+ (dir (expand-file-name name "~")))
+ (unwind-protect
+ (progn
+ (make-directory dir)
+ (let ((default-directory dir))
+ (call-process "git" nil nil nil "init" "--quiet"))
+ ;; git log on a no-commits repo errors in some versions, but
+ ;; our wrapper turns "no commits" into the no-match marker.
+ (let ((res (ignore-errors (cj/gptel-git-log--run dir))))
+ ;; Either path is acceptable: error captured (nil) or the
+ ;; explicit "No commits matching" marker.
+ (should (or (null res)
+ (string-match-p "No commits" res)))))
+ (when (file-exists-p dir) (delete-directory dir t)))))
+
+(provide 'test-gptel-tools-git-log)
+;;; test-gptel-tools-git-log.el ends here