aboutsummaryrefslogtreecommitdiff
path: root/tests/test-chime-jump-to-event.el
blob: 6db885b84626a9f9f9f65755c6015678409b8cf7 (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
115
116
117
118
119
120
121
122
123
124
125
;;; test-chime-jump-to-event.el --- Tests for chime--jump-to-event navigation -*- 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.

;;; Commentary:

;; Unit tests for `chime--jump-to-event'.  The function reconstructs an
;; org buffer position from the serialized marker-file + marker-pos pair
;; that survives the async boundary, opens the file, moves point, and
;; reveals the entry.

;;; Code:

(require 'test-bootstrap (expand-file-name "test-bootstrap.el"))
(require 'testutil-general (expand-file-name "testutil-general.el"))

(defun test-chime-jump-to-event--make-temp-org-file (content)
  "Write CONTENT to a temp .org file under the test base dir and return its path."
  (let* ((base (chime-create-test-base-dir))
         (path (expand-file-name
                (concat (make-temp-name "jump-to-event-") ".org")
                base)))
    (with-temp-file path
      (insert content))
    path))

(ert-deftest test-chime-jump-to-event-normal-opens-file-and-moves-point ()
  "Normal: opens the event's file and moves point to the recorded position."
  (unwind-protect
      (let* ((content "* First heading
some body

* Target heading
target body
")
             (file (test-chime-jump-to-event--make-temp-org-file content))
             (target-pos (with-temp-buffer
                           (insert-file-contents file)
                           (goto-char (point-min))
                           (search-forward "* Target heading")
                           (line-beginning-position)))
             (event (chime--make-event
                     '(("<2026-05-10 Sun 09:30>" . (26760 32460)))
                     "Target heading"
                     '((0 . medium))
                     file
                     target-pos))
             (buffer-before-jump nil))
        (unwind-protect
            (progn
              (setq buffer-before-jump (current-buffer))
              (chime--jump-to-event event)
              (should (string= file (buffer-file-name)))
              (should (= target-pos (point))))
          (when (and (get-file-buffer file))
            (kill-buffer (get-file-buffer file)))
          (when (buffer-live-p buffer-before-jump)
            (switch-to-buffer buffer-before-jump))))
    (chime-delete-test-base-dir)))

(ert-deftest test-chime-jump-to-event-boundary-nil-marker-file-no-op ()
  "Boundary: an event with nil marker-file does nothing (no error, no jump)."
  (let* ((event (chime--make-event
                 '(("<2026-05-10 Sun 09:30>" . (26760 32460)))
                 "No File"
                 '((0 . medium))
                 nil
                 123))
         (buffer-before (current-buffer)))
    (chime--jump-to-event event)
    (should (eq buffer-before (current-buffer)))))

(ert-deftest test-chime-jump-to-event-boundary-missing-file-no-op ()
  "Boundary: when the recorded file no longer exists, the jump is a no-op."
  (let* ((event (chime--make-event
                 '(("<2026-05-10 Sun 09:30>" . (26760 32460)))
                 "Missing File"
                 '((0 . medium))
                 "/tmp/chime-this-path-does-not-exist-xyz.org"
                 42))
         (buffer-before (current-buffer)))
    (chime--jump-to-event event)
    (should (eq buffer-before (current-buffer)))))

(ert-deftest test-chime-jump-to-event-boundary-uses-org-show-entry-fallback ()
  "Boundary: when `org-fold-show-entry' is unbound, falls back to `org-show-entry'."
  (unwind-protect
      (let* ((content "* Heading
body
")
             (file (test-chime-jump-to-event--make-temp-org-file content))
             (event (chime--make-event
                     '(("<2026-05-10 Sun 09:30>" . (26760 32460)))
                     "Heading"
                     '((0 . medium))
                     file
                     1))
             (fallback-called nil)
             (orig-fboundp (symbol-function 'fboundp))
             (buffer-before-jump (current-buffer)))
        (unwind-protect
            (cl-letf (((symbol-function 'fboundp)
                       (lambda (sym)
                         (and (not (eq sym 'org-fold-show-entry))
                              (funcall orig-fboundp sym))))
                      ((symbol-function 'org-show-entry)
                       (lambda () (setq fallback-called t))))
              (chime--jump-to-event event)
              (should fallback-called)
              (should (string= file (buffer-file-name))))
          (when (get-file-buffer file)
            (kill-buffer (get-file-buffer file)))
          (when (buffer-live-p buffer-before-jump)
            (switch-to-buffer buffer-before-jump))))
    (chime-delete-test-base-dir)))

(provide 'test-chime-jump-to-event)
;;; test-chime-jump-to-event.el ends here