blob: 172ddd12330875a1e58bc5aa696d444f39d11e26 (
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
|
;;; test-chime-org-contacts.el --- Tests for chime-org-contacts.el -*- lexical-binding: t; -*-
;; Copyright (C) 2025-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 and integration tests for chime-org-contacts.el
;;; Code:
;; Initialize package system for batch mode
(when noninteractive
(package-initialize))
(require 'ert)
(require 'org)
(require 'org-capture)
;; Load the conversion module (shared birthday utilities)
(load (expand-file-name "../convert-org-contacts-birthdays.el"
(file-name-directory (or load-file-name buffer-file-name))) nil t)
;; Load the module being tested
(let ((module-file (expand-file-name "../chime-org-contacts.el"
(file-name-directory (or load-file-name buffer-file-name)))))
(load module-file nil t))
;;; Integration Tests - chime-org-contacts--finalize-birthday-timestamp
(ert-deftest test-chime-org-contacts-finalize-adds-timestamp-full-date ()
"Test finalize adds timestamp for YYYY-MM-DD birthday."
(with-temp-buffer
(org-mode)
(insert "* Alice Anderson\n")
(insert ":PROPERTIES:\n")
(insert ":EMAIL: alice@example.com\n")
(insert ":BIRTHDAY: 1985-03-15\n")
(insert ":END:\n")
(goto-char (point-min))
(let ((org-capture-plist '(:key "C")))
(chime-org-contacts--finalize-birthday-timestamp)
(let ((content (buffer-string)))
(should (string-match-p "<1985-03-15 [A-Za-z]\\{3\\} \\+1y>" content))))))
(ert-deftest test-chime-org-contacts-finalize-adds-timestamp-partial-date ()
"Test finalize adds timestamp for MM-DD birthday."
(let ((current-year (nth 5 (decode-time))))
(with-temp-buffer
(org-mode)
(insert "* Bob Baker\n")
(insert ":PROPERTIES:\n")
(insert ":BIRTHDAY: 07-04\n")
(insert ":END:\n")
(goto-char (point-min))
(let ((org-capture-plist '(:key "C")))
(chime-org-contacts--finalize-birthday-timestamp)
(let ((content (buffer-string)))
(should (string-match-p (format "<%d-07-04 [A-Za-z]\\{3\\} \\+1y>" current-year) content)))))))
(ert-deftest test-chime-org-contacts-finalize-skips-when-no-birthday ()
"Test finalize does nothing when :BIRTHDAY: property missing."
(with-temp-buffer
(org-mode)
(insert "* Carol Chen\n")
(insert ":PROPERTIES:\n")
(insert ":EMAIL: carol@example.com\n")
(insert ":END:\n")
(goto-char (point-min))
(let ((original-content (buffer-string))
(org-capture-plist '(:key "C")))
(chime-org-contacts--finalize-birthday-timestamp)
;; Content should be unchanged
(should (string= (buffer-string) original-content)))))
(ert-deftest test-chime-org-contacts-finalize-skips-empty-birthday ()
"Test finalize skips empty birthday values."
(with-temp-buffer
(org-mode)
(insert "* David Davis\n")
(insert ":PROPERTIES:\n")
(insert ":BIRTHDAY: \n")
(insert ":END:\n")
(goto-char (point-min))
(let ((original-content (buffer-string))
(org-capture-plist '(:key "C")))
(chime-org-contacts--finalize-birthday-timestamp)
(should (string= (buffer-string) original-content)))))
(ert-deftest test-chime-org-contacts-finalize-only-runs-for-correct-key ()
"Test finalize only runs for configured capture key."
(with-temp-buffer
(org-mode)
(insert "* Task\n")
(insert ":PROPERTIES:\n")
(insert ":BIRTHDAY: 2000-01-01\n")
(insert ":END:\n")
(goto-char (point-min))
(let ((original-content (buffer-string))
(org-capture-plist '(:key "t"))) ; Different key
(chime-org-contacts--finalize-birthday-timestamp)
;; Should not insert timestamp
(should (string= (buffer-string) original-content)))))
;;; Integration Tests - chime-org-contacts--setup-capture-template
(ert-deftest test-chime-org-contacts-setup-adds-template-when-file-set ()
"Test that template is added when file is set."
(let ((chime-org-contacts-file "/tmp/test-contacts.org")
(org-capture-templates nil))
(chime-org-contacts--setup-capture-template)
(should org-capture-templates)
(should (assoc "C" org-capture-templates))))
(ert-deftest test-chime-org-contacts-setup-skips-when-file-nil ()
"Test that template is not added when file is nil."
(let ((chime-org-contacts-file nil)
(org-capture-templates nil))
(chime-org-contacts--setup-capture-template)
(should-not org-capture-templates)))
(ert-deftest test-chime-org-contacts-setup-template-structure ()
"Test that added template has correct structure."
(let ((chime-org-contacts-file "/tmp/test-contacts.org")
(chime-org-contacts-capture-key "C")
(chime-org-contacts-heading "Contacts")
(org-capture-templates nil))
(chime-org-contacts--setup-capture-template)
(let ((template (assoc "C" org-capture-templates)))
(should (string= (nth 1 template) "Contact (chime)"))
(should (eq (nth 2 template) 'entry))
(should (equal (nth 3 template) '(file+headline chime-org-contacts-file "Contacts"))))))
(ert-deftest test-chime-org-contacts-setup-uses-custom-key ()
"Test that template uses custom capture key."
(let ((chime-org-contacts-file "/tmp/test-contacts.org")
(chime-org-contacts-capture-key "K")
(org-capture-templates nil))
(chime-org-contacts--setup-capture-template)
(should (assoc "K" org-capture-templates))
(should-not (assoc "C" org-capture-templates))))
(provide 'test-chime-org-contacts)
;;; test-chime-org-contacts.el ends here
|