aboutsummaryrefslogtreecommitdiff
path: root/tests/test-pearl-org-write.el
blob: 6491b5426a72cc6f083815db119ebed0ba56b976 (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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
;;; test-pearl-org-write.el --- Tests for pearl org file write-back -*- lexical-binding: t; -*-

;; Copyright (C) 2026 Craig Jennings

;; Author: Craig Jennings <c@cjennings.net>

;; This program is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.

;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with this program.  If not, see <http://www.gnu.org/licenses/>.

;;; Commentary:

;; Tests for `pearl--update-org-from-issues', the buffer-aware
;; write-back.  Uses real temp files (file I/O is the behavior under test);
;; the three branches are no-buffer, clean-buffer, and dirty-buffer.

;;; Code:

(require 'test-bootstrap (expand-file-name "test-bootstrap.el"))
(require 'cl-lib)

(defvar test-pearl--sample-issues
  '((:id "u" :identifier "ENG-1" :title "T" :priority 3 :state (:name "Todo")))
  "One normalized issue, enough to render a recognizable org entry.")

(defmacro test-pearl--with-org-file (var &rest body)
  "Bind VAR to a fresh temp .org path and run BODY, cleaning up after.
The state mapping is bound so rendering is deterministic."
  (declare (indent 1))
  `(let* ((,var (make-temp-file "linear-test-" nil ".org"))
          (pearl-org-file-path ,var)
          (pearl-state-to-todo-mapping '(("Todo" . "TODO"))))
     (unwind-protect
         (progn ,@body)
       (let ((b (find-buffer-visiting ,var)))
         (when b
           (with-current-buffer b (set-buffer-modified-p nil))
           (kill-buffer b)))
       (when (file-exists-p ,var) (delete-file ,var)))))

(ert-deftest test-pearl-update-org-no-buffer-writes-file ()
  "With no buffer visiting the file, issues are written to disk."
  (test-pearl--with-org-file tmp
    (let ((b (find-buffer-visiting tmp))) (when b (kill-buffer b)))
    (pearl--update-org-from-issues test-pearl--sample-issues)
    (let ((content (with-temp-buffer (insert-file-contents tmp) (buffer-string))))
      (should (string-match-p "#\\+title: Linear" content))
      (should (string-match-p "#\\+LINEAR-SOURCE: " content))
      (should (string-match-p "\\*\\* TODO \\[#C\\] ENG-1: T" content)))))

(ert-deftest test-pearl-update-org-clean-buffer-replaces-contents ()
  "A clean visiting buffer is replaced in place and saved."
  (test-pearl--with-org-file tmp
    (let ((buf (find-file-noselect tmp)))
      (with-current-buffer buf
        (insert "old content")
        (save-buffer))
      (pearl--update-org-from-issues test-pearl--sample-issues)
      (with-current-buffer buf
        (should-not (buffer-modified-p))
        (should (string-match-p "\\*\\* TODO \\[#C\\] ENG-1: T" (buffer-string)))
        (should-not (string-match-p "old content" (buffer-string)))))))

(ert-deftest test-pearl-update-org-dirty-buffer-not-overwritten ()
  "A buffer with unsaved edits is left untouched, not clobbered."
  (test-pearl--with-org-file tmp
    (let ((buf (find-file-noselect tmp)))
      (with-current-buffer buf
        (insert "unsaved edits"))
      (pearl--update-org-from-issues test-pearl--sample-issues)
      (with-current-buffer buf
        (should (buffer-modified-p))
        (should (string-match-p "unsaved edits" (buffer-string)))
        (should-not (string-match-p "ENG-1" (buffer-string)))))))

(ert-deftest test-pearl-update-org-dirty-buffer-preserves-disk-and-surfaces ()
  "A dirty buffer is left as-is, the on-disk file is untouched, and the buffer is surfaced."
  (test-pearl--with-org-file tmp
    (let ((buf (find-file-noselect tmp))
          (surfaced nil))
      (with-current-buffer buf
        (insert "ORIGINAL DISK CONTENT\n")
        (save-buffer)
        ;; an unsaved edit on top of the saved content
        (goto-char (point-max))
        (insert "UNSAVED EDIT\n"))
      (cl-letf (((symbol-function 'pearl--surface-buffer)
                 (lambda (b) (setq surfaced b))))
        (pearl--update-org-from-issues test-pearl--sample-issues))
      ;; buffer keeps the unsaved edit and stays modified
      (with-current-buffer buf
        (should (buffer-modified-p))
        (should (string-match-p "UNSAVED EDIT" (buffer-string)))
        (should-not (string-match-p "ENG-1" (buffer-string))))
      ;; the disk file still holds the original saved content — not the render,
      ;; and not the unsaved buffer text
      (let ((disk (with-temp-buffer (insert-file-contents tmp) (buffer-string))))
        (should (string-match-p "ORIGINAL DISK CONTENT" disk))
        (should-not (string-match-p "UNSAVED EDIT" disk))
        (should-not (string-match-p "ENG-1" disk)))
      ;; the dirty buffer is still surfaced, per the UX contract
      (should (eq surfaced buf)))))

(provide 'test-pearl-org-write)
;;; test-pearl-org-write.el ends here