From 73e63b6c6850f8e14d8374c7bf6b127971cfbb08 Mon Sep 17 00:00:00 2001 From: Craig Jennings Date: Sat, 16 May 2026 01:39:57 -0500 Subject: test(gptel-tools): cover the helpers across the five remaining tools The gptel-tools files had zero direct coverage outside of `update_text_file`, which landed with its rewrite earlier this session. This commit adds 52 tests across the five other tools. For three of the tools the helpers were already top-level defuns (`read_text_file`, `list_directory_files`, `move_to_trash`). The other two had their main bodies inlined into the `gptel-make-tool` lambda -- I extracted them so the work is testable without mocking gptel itself: read_buffer.el -> `cj/read-buffer--get-content` write_text_file.el -> `cj/write-text-file--run` plus `--validate-path`, `--backup-name`, `--ensure-parent` Test files, by tool: - read_buffer.el (5 tests): normal, empty, buffer-object, text-property-stripping, missing buffer. - write_text_file.el (10 tests): validate-path, backup-name shape, ensure-parent (creates missing / rejects unwritable), run with normal / overwrite / existing-no-overwrite / empty content / outside-home. - read_text_file.el (12 tests): validate-file-path (normal + three error shapes), metadata plist shape, size limits (no-op / hard cap / warning bypass with no-confirm), binary detection (text vs null-byte), special-type EPUB and generic-binary paths. - list_directory_files.el (15 tests): mode-to-permissions (file / dir / executable), get-file-info (file / directory), extension filter (keep / drop / always-dir / nil-extension), format-file- entry, list-directory flat / recursive / error, format-output with and without files. - move_to_trash.el (10 tests): unique-name (no conflict / conflict with timestamp / no-extension), validate-path (HOME / /tmp / outside / critical-dir / missing), perform on file and directory. Each test file uses the same load-path / gptel-stub idiom (`eval-and-compile` block, gptel stub when the real package isn't available) so the byte-compile hook is happy. --- tests/test-gptel-tools-read-buffer.el | 60 +++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 tests/test-gptel-tools-read-buffer.el (limited to 'tests/test-gptel-tools-read-buffer.el') diff --git a/tests/test-gptel-tools-read-buffer.el b/tests/test-gptel-tools-read-buffer.el new file mode 100644 index 00000000..75efd604 --- /dev/null +++ b/tests/test-gptel-tools-read-buffer.el @@ -0,0 +1,60 @@ +;;; test-gptel-tools-read-buffer.el --- Tests for read_buffer gptel tool -*- lexical-binding: t; -*- + +;;; Commentary: +;; Tests for `cj/read-buffer--get-content', the testable helper that +;; backs the read_buffer gptel tool. + +;;; Code: + +(require 'ert) + +(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 'read_buffer) + +(ert-deftest test-gptel-tools-read-buffer-normal () + "Normal: returns the contents of an existing buffer." + (with-temp-buffer + (rename-buffer "test-gptel-tools-read-buffer-normal" t) + (insert "hello world") + (should (equal (cj/read-buffer--get-content (buffer-name)) "hello world")))) + +(ert-deftest test-gptel-tools-read-buffer-boundary-empty-buffer () + "Boundary: empty buffer returns the empty string." + (with-temp-buffer + (rename-buffer "test-gptel-tools-read-buffer-empty" t) + (should (equal (cj/read-buffer--get-content (buffer-name)) "")))) + +(ert-deftest test-gptel-tools-read-buffer-boundary-buffer-object () + "Boundary: accepts a buffer object as well as a name string." + (with-temp-buffer + (insert "from buffer object") + (should (equal (cj/read-buffer--get-content (current-buffer)) + "from buffer object")))) + +(ert-deftest test-gptel-tools-read-buffer-boundary-strips-text-properties () + "Boundary: the returned string has no text properties." + (with-temp-buffer + (rename-buffer "test-gptel-tools-read-buffer-props" t) + (insert (propertize "fontified" 'face 'bold)) + (let ((content (cj/read-buffer--get-content (buffer-name)))) + (should (equal content "fontified")) + (should-not (text-properties-at 0 content))))) + +(ert-deftest test-gptel-tools-read-buffer-error-missing-buffer () + "Error: nonexistent buffer name signals." + (when (get-buffer "test-gptel-tools-read-buffer-absent") + (kill-buffer "test-gptel-tools-read-buffer-absent")) + (should-error (cj/read-buffer--get-content + "test-gptel-tools-read-buffer-absent"))) + +(provide 'test-gptel-tools-read-buffer) +;;; test-gptel-tools-read-buffer.el ends here -- cgit v1.2.3