aboutsummaryrefslogtreecommitdiff
path: root/tests/test-integration-startup.el
blob: c755d7e85a8f336a71c653a34c8450b7a4d38d84 (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
;;; test-integration-startup.el --- Integration tests for chime startup flow -*- lexical-binding: t; -*-

;; Copyright (C) 2024-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:

;; Integration tests for chime startup and configuration validation.
;;
;; Tests the complete startup workflow:
;; - Valid org-agenda-files configuration
;; - chime-validate-configuration checks
;; - chime-check async event gathering
;; - Modeline population
;;
;; Components integrated:
;; - chime-validate-configuration (validates runtime requirements)
;; - chime-check (async event gathering via org-agenda-list)
;; - chime--update-modeline (updates modeline string)
;; - org-agenda-list (expands events from org files)
;;
;; Validates:
;; - Chime finds correct number of events from org-agenda-files
;; - Validation passes with proper configuration
;; - Modeline gets populated after check completes
;; - Mixed event types (scheduled, deadline, TODO states) work correctly

;;; Code:

(setq chime-debug t)
(require 'test-bootstrap (expand-file-name "test-bootstrap.el"))

;; Load test utilities
(require 'testutil-general (expand-file-name "testutil-general.el"))
(require 'testutil-time (expand-file-name "testutil-time.el"))
(require 'testutil-events (expand-file-name "testutil-events.el"))

;;; Helper Functions

(defun test-integration-startup--create-org-file (content)
  "Create org file with CONTENT and set it as org-agenda-files.
Returns the file path."
  (let* ((base-file (chime-create-temp-test-file "startup-test-"))
         (org-file (concat base-file ".org")))
    ;; Rename to have .org extension
    (rename-file base-file org-file)
    ;; Write content to the .org file
    (with-temp-buffer
      (insert content)
      (write-file org-file))
    ;; Set as agenda file
    (setq org-agenda-files (list org-file))
    org-file))

(defmacro with-startup-config (&rest body)
  "Execute BODY with standard startup test config and temp dir management.

Combines two concerns that every test in this file needs:
  1. `with-test-setup' creates/cleans up the temp test directory.
  2. `with-chime-config' `let'-binds config overrides so they auto-restore
     when BODY exits (even on error), replacing the old pattern of saving
     originals to defvars in setup and restoring them in teardown.

The overrides set a 24-hour lookahead window and a 1-second startup delay
so tests can exercise the full startup path without waiting or creating
events far in the future."
  (declare (indent 0))
  `(with-test-setup
     (with-chime-config
       chime-modeline-lookahead-minutes (* 24 60)
       chime-tooltip-lookahead-hours 24
       chime-startup-delay 1
       ,@body)))

;;; Normal Cases - Valid Startup Configuration

(ert-deftest test-integration-startup-valid-config-finds-events ()
  "Test that chime-check finds events with valid org-agenda-files configuration.

This is the core startup integration test. Validates that when:
- org-agenda-files is properly configured with real .org files
- Files contain scheduled/deadline events
- chime-check is called (normally triggered by startup timer)

Then:
- Events are successfully gathered from org-agenda-list
- Event count matches expected number
- Modeline string gets populated

Components integrated:
- org-agenda-files (user configuration)
- org-agenda-list (expands events from org files)
- chime-check (async wrapper around event gathering)
- chime--gather-info (extracts event details)
- chime--update-modeline (updates modeline display)"
  (with-startup-config
    (let* ((now (test-time-now))
           ;; Create events at various times
           (event1-time (test-time-at 0 2 0))    ; 2 hours from now
           (event2-time (test-time-at 0 5 0))    ; 5 hours from now
           (event3-time (test-time-at 1 0 0))    ; Tomorrow same time
           (event4-time (test-time-at -1 0 0))   ; Yesterday (overdue)
           ;; Generate timestamps
           (ts1 (test-timestamp-string event1-time))
           (ts2 (test-timestamp-string event2-time))
           (ts3 (test-timestamp-string event3-time))
           (ts4 (test-timestamp-string event4-time))
           ;; Create org file content
           (content (format "#+TITLE: Startup Test Events

* TODO Event in 2 hours
SCHEDULED: %s

* TODO Event in 5 hours
SCHEDULED: %s

* TODO Event tomorrow
SCHEDULED: %s

* TODO Overdue event
SCHEDULED: %s

* DONE Completed event (should not notify)
SCHEDULED: %s
" ts1 ts2 ts3 ts4 ts1)))

      ;; Create org file and set as agenda files
      (test-integration-startup--create-org-file content)

      ;; Validate configuration should pass
      (let ((issues (chime-validate-configuration)))
        (should (null issues)))

      (with-test-time now
        ;; Call chime-check synchronously (bypasses async/timer for test reliability)
        ;; In real startup, this is called via run-at-time after chime-startup-delay
        (let ((event-count 0))
          ;; Mock the async-start to run synchronously for testing
          (cl-letf (((symbol-function 'async-start)
                     (lambda (start-func finish-func)
                       ;; Call start-func synchronously and pass result to finish-func
                       (funcall finish-func (funcall start-func)))))
            ;; Now call chime-check - it will run synchronously
            (chime-check)

            ;; Give it a moment to process
            (sleep-for 0.1)

            ;; Verify modeline was updated
            (should chime-modeline-string)

            ;; Verify we found events (should be 4 TODO events, DONE excluded)
            ;; Note: The exact behavior depends on chime's filtering logic
            (should chime--upcoming-events)
            (setq event-count (length chime--upcoming-events))

            ;; Should find at least the non-DONE events within lookahead window
            (should (>= event-count 3))))))))  ; At least 3 events (2h, 5h, tomorrow)

(ert-deftest test-integration-startup-validation-passes-minimal-config ()
  "Test validation passes with minimal valid configuration.

Validates that chime-validate-configuration returns nil (no issues) when:
- org-agenda-files is set to a list with at least one .org file
- The file exists on disk
- org-agenda package is loadable
- All other dependencies are available

This ensures the startup validation doesn't block legitimate configurations."
  (with-startup-config
    (let ((content "#+TITLE: Minimal Test\n\n* TODO Test event\nSCHEDULED: <2025-12-01 Mon 10:00>\n"))
      ;; Create minimal org file
      (test-integration-startup--create-org-file content)

      ;; Validation should pass
      (let ((issues (chime-validate-configuration)))
        (should (null issues))))))

;;; Error Cases - Configuration Failures

(ert-deftest test-integration-startup-early-return-on-validation-failure ()
  "Test that chime-check returns early when validation fails without throwing errors.

This is a regression test for the bug where chime-check used cl-return-from
without being defined as cl-defun, causing '(no-catch --cl-block-chime-check-- nil)' error.

When validation fails on first check, chime-check should:
- Log the validation failure
- Return nil early (via cl-return-from)
- NOT throw 'no-catch' error
- NOT proceed to event gathering

This validates the early-return mechanism works correctly."
  (with-startup-config
    ;; Set up invalid configuration (empty org-agenda-files)
    (setq org-agenda-files nil)

    ;; Reset validation state so chime-check will validate on next call
    (setq chime--validation-done nil)
    (setq chime--validation-retry-count 0)

    ;; Clear state from any previous tests so we can verify
    ;; early return doesn't set these
    (setq chime--upcoming-events nil)
    (setq chime-modeline-string nil)

    ;; Call chime-check - should return early without error
    ;; Before the fix, this would throw: (no-catch --cl-block-chime-check-- nil)
    (let ((result (chime-check)))

      ;; Should return nil (early return from validation failure)
      (should (null result))

      ;; Validation should NOT be marked done when it fails
      ;; (so it can retry on next check in case dependencies load later)
      (should (null chime--validation-done))

      ;; Should NOT have processed any events (early return worked)
      (should (null chime--upcoming-events))
      (should (null chime-modeline-string)))))

;;; Boundary Cases - Edge Conditions

(ert-deftest test-integration-startup-single-event-found ()
  "Test that chime-check correctly finds a single event.

Boundary case: org-agenda-files with only one event.
Validates that the gathering and modeline logic work with minimal data."
  (with-startup-config
    (let* ((now (test-time-now))
           (event-time (test-time-at 0 1 0))  ; 1 hour from now
           (ts (test-timestamp-string event-time))
           (content (format "* TODO Single Event\nSCHEDULED: %s\n" ts)))

      (test-integration-startup--create-org-file content)

      (with-test-time now
        (cl-letf (((symbol-function 'async-start)
                   (lambda (start-func finish-func)
                     (funcall finish-func (funcall start-func)))))
          (chime-check)
          (sleep-for 0.1)

          ;; Should find exactly 1 event
          (should (= 1 (length chime--upcoming-events)))

          ;; Modeline should be populated
          (should chime-modeline-string)
          (should (string-match-p "Single Event" chime-modeline-string)))))))

(ert-deftest test-integration-startup-no-upcoming-events ()
  "Test chime-check when org file has no upcoming events within lookahead.

Boundary case: Events exist but are far in the future (beyond lookahead window).
Validates that chime doesn't error and modeline shows appropriate state."
  (with-startup-config
    (let* ((now (test-time-now))
           ;; Event 30 days from now (beyond 24-hour lookahead)
           (event-time (test-time-at 30 0 0))
           (ts (test-timestamp-string event-time))
           (content (format "* TODO Future Event\nSCHEDULED: %s\n" ts)))

      (test-integration-startup--create-org-file content)

      (with-test-time now
        (cl-letf (((symbol-function 'async-start)
                   (lambda (start-func finish-func)
                     (funcall finish-func (funcall start-func)))))
          (chime-check)
          (sleep-for 0.1)

          ;; Should find 0 events within lookahead window
          (should (or (null chime--upcoming-events)
                     (= 0 (length chime--upcoming-events))))

          ;; Modeline should handle this gracefully (nil or empty)
          ;; No error should occur
          )))))

(provide 'test-integration-startup)
;;; test-integration-startup.el ends here