blob: 8329abd830033c32e619dadd58d611da705304f9 (
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
|
;;; test-chime-sanitize-title.el --- Tests for chime--sanitize-title -*- lexical-binding: t; -*-
;; Copyright (C) 2024 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:
;; Unit tests for chime--sanitize-title function.
;; Tests cover:
;; - Unmatched opening delimiters (parentheses, brackets, braces)
;; - Unmatched closing delimiters
;; - Mixed unmatched delimiters
;; - Already balanced delimiters (no-op)
;; - Nil and empty strings
;; - Real-world bug cases that triggered the issue
;;; Code:
;; Initialize package system for batch mode
(when noninteractive
(package-initialize))
(require 'ert)
;; Load dependencies required by chime
(require 'dash)
(require 'alert)
(require 'async)
(require 'org-agenda)
;; Load chime from parent directory
(load (expand-file-name "../chime.el") nil t)
;; Load test utilities
(require 'testutil-general (expand-file-name "testutil-general.el"))
(require 'testutil-time (expand-file-name "testutil-time.el"))
;;; Setup and Teardown
(defun test-chime-sanitize-title-setup ()
"Setup function run before each test."
(chime-create-test-base-dir))
(defun test-chime-sanitize-title-teardown ()
"Teardown function run after each test."
(chime-delete-test-base-dir))
;;; Normal Cases - Already Balanced
(ert-deftest test-chime-sanitize-title-balanced-parens-unchanged ()
"Test that balanced parentheses are unchanged.
REFACTORED: No timestamps used"
(test-chime-sanitize-title-setup)
(unwind-protect
(let* ((title "Meeting (Team Sync)")
(result (chime--sanitize-title title)))
(should (string-equal title result)))
(test-chime-sanitize-title-teardown)))
(ert-deftest test-chime-sanitize-title-balanced-brackets-unchanged ()
"Test that balanced brackets are unchanged.
REFACTORED: No timestamps used"
(test-chime-sanitize-title-setup)
(unwind-protect
(let* ((title "Review [PR #123]")
(result (chime--sanitize-title title)))
(should (string-equal title result)))
(test-chime-sanitize-title-teardown)))
(ert-deftest test-chime-sanitize-title-balanced-braces-unchanged ()
"Test that balanced braces are unchanged.
REFACTORED: No timestamps used"
(test-chime-sanitize-title-setup)
(unwind-protect
(let* ((title "Code Review {urgent}")
(result (chime--sanitize-title title)))
(should (string-equal title result)))
(test-chime-sanitize-title-teardown)))
(ert-deftest test-chime-sanitize-title-mixed-balanced-unchanged ()
"Test that mixed balanced delimiters are unchanged.
REFACTORED: No timestamps used"
(test-chime-sanitize-title-setup)
(unwind-protect
(let* ((title "Meeting [Team] (Sync) {Urgent}")
(result (chime--sanitize-title title)))
(should (string-equal title result)))
(test-chime-sanitize-title-teardown)))
(ert-deftest test-chime-sanitize-title-nested-balanced-unchanged ()
"Test that nested balanced delimiters are unchanged.
REFACTORED: No timestamps used"
(test-chime-sanitize-title-setup)
(unwind-protect
(let* ((title "Review (PR [#123] {urgent})")
(result (chime--sanitize-title title)))
(should (string-equal title result)))
(test-chime-sanitize-title-teardown)))
(ert-deftest test-chime-sanitize-title-no-delimiters-unchanged ()
"Test that titles without delimiters are unchanged.
REFACTORED: No timestamps used"
(test-chime-sanitize-title-setup)
(unwind-protect
(let* ((title "Simple Meeting Title")
(result (chime--sanitize-title title)))
(should (string-equal title result)))
(test-chime-sanitize-title-teardown)))
;;; Unmatched Opening Delimiters
(ert-deftest test-chime-sanitize-title-unmatched-opening-paren ()
"Test that unmatched opening parenthesis is closed."
(test-chime-sanitize-title-setup)
(unwind-protect
(let* ((title "CTO/COO XLT (Extended Leadership")
(result (chime--sanitize-title title)))
(should (string-equal "CTO/COO XLT (Extended Leadership)" result)))
(test-chime-sanitize-title-teardown)))
(ert-deftest test-chime-sanitize-title-unmatched-opening-paren-at-end ()
"Test that unmatched opening parenthesis at end is closed."
(test-chime-sanitize-title-setup)
(unwind-protect
(let* ((title "Spice Cake (")
(result (chime--sanitize-title title)))
(should (string-equal "Spice Cake ()" result)))
(test-chime-sanitize-title-teardown)))
(ert-deftest test-chime-sanitize-title-multiple-unmatched-opening-parens ()
"Test that multiple unmatched opening parentheses are closed."
(test-chime-sanitize-title-setup)
(unwind-protect
(let* ((title "Meeting (Team (Sync")
(result (chime--sanitize-title title)))
(should (string-equal "Meeting (Team (Sync))" result)))
(test-chime-sanitize-title-teardown)))
(ert-deftest test-chime-sanitize-title-unmatched-opening-bracket ()
"Test that unmatched opening bracket is closed."
(test-chime-sanitize-title-setup)
(unwind-protect
(let* ((title "Review [PR #123")
(result (chime--sanitize-title title)))
(should (string-equal "Review [PR #123]" result)))
(test-chime-sanitize-title-teardown)))
(ert-deftest test-chime-sanitize-title-unmatched-opening-brace ()
"Test that unmatched opening brace is closed."
(test-chime-sanitize-title-setup)
(unwind-protect
(let* ((title "Code Review {urgent")
(result (chime--sanitize-title title)))
(should (string-equal "Code Review {urgent}" result)))
(test-chime-sanitize-title-teardown)))
(ert-deftest test-chime-sanitize-title-mixed-unmatched-opening-delimiters ()
"Test that mixed unmatched opening delimiters are all closed."
(test-chime-sanitize-title-setup)
(unwind-protect
(let* ((title "Meeting [Team (Sync {Urgent")
(result (chime--sanitize-title title)))
(should (string-equal "Meeting [Team (Sync {Urgent})]" result)))
(test-chime-sanitize-title-teardown)))
;;; Unmatched Closing Delimiters
(ert-deftest test-chime-sanitize-title-unmatched-closing-paren ()
"Test that unmatched closing parenthesis is removed."
(test-chime-sanitize-title-setup)
(unwind-protect
(let* ((title "Meeting Title)")
(result (chime--sanitize-title title)))
(should (string-equal "Meeting Title" result)))
(test-chime-sanitize-title-teardown)))
(ert-deftest test-chime-sanitize-title-multiple-unmatched-closing-parens ()
"Test that multiple unmatched closing parentheses are removed."
(test-chime-sanitize-title-setup)
(unwind-protect
(let* ((title "Meeting Title))")
(result (chime--sanitize-title title)))
(should (string-equal "Meeting Title" result)))
(test-chime-sanitize-title-teardown)))
(ert-deftest test-chime-sanitize-title-unmatched-closing-bracket ()
"Test that unmatched closing bracket is removed."
(test-chime-sanitize-title-setup)
(unwind-protect
(let* ((title "Review PR]")
(result (chime--sanitize-title title)))
(should (string-equal "Review PR" result)))
(test-chime-sanitize-title-teardown)))
(ert-deftest test-chime-sanitize-title-unmatched-closing-brace ()
"Test that unmatched closing brace is removed."
(test-chime-sanitize-title-setup)
(unwind-protect
(let* ((title "Code Review}")
(result (chime--sanitize-title title)))
(should (string-equal "Code Review" result)))
(test-chime-sanitize-title-teardown)))
;;; Complex Mixed Cases
(ert-deftest test-chime-sanitize-title-opening-and-closing-mixed ()
"Test title with both unmatched opening and closing delimiters."
(test-chime-sanitize-title-setup)
(unwind-protect
(let* ((title "Meeting (Team) Extra)")
(result (chime--sanitize-title title)))
;; Should remove the extra closing paren
(should (string-equal "Meeting (Team) Extra" result)))
(test-chime-sanitize-title-teardown)))
(ert-deftest test-chime-sanitize-title-complex-nesting-with-unmatched ()
"Test complex nested delimiters with some unmatched."
(test-chime-sanitize-title-setup)
(unwind-protect
(let* ((title "Meeting [Team (Sync] Extra")
(result (chime--sanitize-title title)))
;; The ']' doesn't match the '[' (because '(' is in between)
;; So it's removed, and we close the '(' and '[' properly: ')' and ']'
(should (string-equal "Meeting [Team (Sync Extra)]" result)))
(test-chime-sanitize-title-teardown)))
(ert-deftest test-chime-sanitize-title-all-types-unmatched ()
"Test with all three delimiter types unmatched."
(test-chime-sanitize-title-setup)
(unwind-protect
(let* ((title "Meeting (Team [Project {Status")
(result (chime--sanitize-title title)))
(should (string-equal "Meeting (Team [Project {Status}])" result)))
(test-chime-sanitize-title-teardown)))
;;; Edge Cases
(ert-deftest test-chime-sanitize-title-nil-returns-empty-string ()
"Test that nil title returns empty string."
(test-chime-sanitize-title-setup)
(unwind-protect
(let* ((result (chime--sanitize-title nil)))
(should (string-equal "" result)))
(test-chime-sanitize-title-teardown)))
(ert-deftest test-chime-sanitize-title-empty-string-unchanged ()
"Test that empty string is unchanged."
(test-chime-sanitize-title-setup)
(unwind-protect
(let* ((title "")
(result (chime--sanitize-title title)))
(should (string-equal "" result)))
(test-chime-sanitize-title-teardown)))
(ert-deftest test-chime-sanitize-title-only-opening-delimiters ()
"Test title with only opening delimiters."
(test-chime-sanitize-title-setup)
(unwind-protect
(let* ((title "([{")
(result (chime--sanitize-title title)))
(should (string-equal "([{}])" result)))
(test-chime-sanitize-title-teardown)))
(ert-deftest test-chime-sanitize-title-only-closing-delimiters ()
"Test title with only closing delimiters."
(test-chime-sanitize-title-setup)
(unwind-protect
(let* ((title ")]}")
(result (chime--sanitize-title title)))
(should (string-equal "" result)))
(test-chime-sanitize-title-teardown)))
(ert-deftest test-chime-sanitize-title-very-long-title-with-unmatched ()
"Test very long title with unmatched delimiter."
(test-chime-sanitize-title-setup)
(unwind-protect
(let* ((title "This is a very long meeting title that contains many words and might wrap in the notification display (Extended Info")
(result (chime--sanitize-title title)))
(should (string-equal "This is a very long meeting title that contains many words and might wrap in the notification display (Extended Info)" result)))
(test-chime-sanitize-title-teardown)))
;;; Real-World Bug Cases
(ert-deftest test-chime-sanitize-title-bug-case-extended-leadership ()
"Test the actual bug case from vineti.meetings.org."
(test-chime-sanitize-title-setup)
(unwind-protect
(let* ((title "1:01pm CTO/COO XLT (Extended Leadership")
(result (chime--sanitize-title title)))
(should (string-equal "1:01pm CTO/COO XLT (Extended Leadership)" result)))
(test-chime-sanitize-title-teardown)))
(ert-deftest test-chime-sanitize-title-bug-case-spice-cake ()
"Test the actual bug case from journal/2023-11-22.org."
(test-chime-sanitize-title-setup)
(unwind-protect
(let* ((title "Spice Cake (")
(result (chime--sanitize-title title)))
(should (string-equal "Spice Cake ()" result)))
(test-chime-sanitize-title-teardown)))
(ert-deftest test-chime-sanitize-title-lisp-serialization-safety ()
"Test that sanitized title can be safely read by Lisp reader."
(test-chime-sanitize-title-setup)
(unwind-protect
(let* ((title "Meeting (Team Sync")
(sanitized (chime--sanitize-title title))
;; Simulate what happens in async serialization
(serialized (format "'((title . \"%s\"))" sanitized)))
;; This should not signal an error
(should (listp (read serialized)))
(should (string-equal "Meeting (Team Sync)" sanitized)))
(test-chime-sanitize-title-teardown)))
(ert-deftest test-chime-sanitize-title-async-serialization-with-unmatched-parens ()
"Test that titles with unmatched parens won't break async serialization."
(test-chime-sanitize-title-setup)
(unwind-protect
(let* ((problematic-titles '("Meeting (Team"
"Review [PR"
"Code {Status"
"Event (("
"Task ))")))
(dolist (title problematic-titles)
(let* ((sanitized (chime--sanitize-title title))
(serialized (format "'((title . \"%s\"))" sanitized)))
;; Should not signal 'invalid-read-syntax error
(should (listp (read serialized))))))
(test-chime-sanitize-title-teardown)))
;;; Integration with chime--extract-title
(ert-deftest test-chime-extract-title-sanitizes-output ()
"Test that chime--extract-title applies sanitization.
REFACTORED: Uses dynamic timestamps"
(test-chime-sanitize-title-setup)
(unwind-protect
(let* ((time (test-time-tomorrow-at 14 0))
(timestamp (test-timestamp-string time))
(test-file (chime-create-temp-test-file-with-content
(format "* TODO Meeting (Team Sync\n%s\n" timestamp)))
(test-buffer (find-file-noselect test-file)))
(with-current-buffer test-buffer
(org-mode) ; Enable org-mode
(goto-char (point-min))
;; Search for the heading
(re-search-forward "^\\* TODO" nil t)
(beginning-of-line)
(let* ((marker (point-marker))
(title (chime--extract-title marker)))
;; Should be sanitized with closing paren added
(should (string-equal "Meeting (Team Sync)" title))))
(kill-buffer test-buffer))
(test-chime-sanitize-title-teardown)))
(ert-deftest test-chime-extract-title-handles-nil ()
"Test that chime--extract-title handles nil gracefully.
REFACTORED: Uses dynamic timestamps"
(test-chime-sanitize-title-setup)
(unwind-protect
(let* ((time (test-time-tomorrow-at 14 0))
(timestamp (test-timestamp-string time))
(test-file (chime-create-temp-test-file-with-content
(format "* TODO\n%s\n" timestamp)))
(test-buffer (find-file-noselect test-file)))
(with-current-buffer test-buffer
(org-mode) ; Enable org-mode
(goto-char (point-min))
;; Search for the heading
(re-search-forward "^\\* TODO" nil t)
(beginning-of-line)
(let* ((marker (point-marker))
(title (chime--extract-title marker)))
;; Should return empty string for nil title
(should (string-equal "" title))))
(kill-buffer test-buffer))
(test-chime-sanitize-title-teardown)))
(provide 'test-chime-sanitize-title)
;;; test-chime-sanitize-title.el ends here
|