aboutsummaryrefslogtreecommitdiff
path: root/tests/test-gptel-tools-read-buffer.el
blob: 0a8548359b95b8ba3774bb8fbc055431e07a1d85 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
;;; 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-widened-content ()
  "Boundary: returns the whole buffer even when the buffer is narrowed."
  (with-temp-buffer
    (insert "visible\nhidden\n")
    (narrow-to-region (point-min) (line-end-position))
    (should (equal (cj/read-buffer--get-content (current-buffer))
                   "visible\nhidden\n"))))

(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")))

(ert-deftest test-gptel-tools-read-buffer-error-killed-buffer-object ()
  "Error: a killed buffer object signals clearly."
  (let ((buffer (generate-new-buffer "test-gptel-tools-read-buffer-killed")))
    (kill-buffer buffer)
    (should-error (cj/read-buffer--get-content buffer))))

(provide 'test-gptel-tools-read-buffer)
;;; test-gptel-tools-read-buffer.el ends here