aboutsummaryrefslogtreecommitdiff
path: root/tests/test-chime-fetch-and-process.el
blob: 6e2917c999b27bfbfb6d083f7252a5fbd923f0dd (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
;;; test-chime-fetch-and-process.el --- Tests for chime--fetch-and-process branches -*- 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:

;; Cover the two error branches inside the `chime--fetch-and-process'
;; async callback:
;;
;;   1. The async subprocess returned an `(async-signal . ERR)' tuple.
;;   2. The user-supplied callback raised an error during processing.
;;
;; Both are caught by the surrounding `condition-case' and routed through
;; `chime--record-async-failure' with a distinct prefix.

;;; Code:

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

(ert-deftest test-chime-fetch-and-process-async-signal-records-failure ()
  "Error: when async returns an `async-signal' tuple, failure is recorded with the async prefix."
  (let ((recorded nil)
        (chime--process nil)
        (chime--last-check-time '(0 0))
        (chime-modeline-no-events-text " ⏰")
        (chime-modeline-string nil)
        (chime--consecutive-async-failures 0)
        (chime-max-consecutive-failures 0))
    (cl-letf (((symbol-function 'async-start)
               (lambda (_start-form finish-func)
                 (funcall finish-func '(async-signal error "boom"))
                 'fake-process))
              ((symbol-function 'chime--record-async-failure)
               (lambda (err prefix) (setq recorded (cons prefix err))))
              ((symbol-function 'force-mode-line-update) (lambda (&optional _))))
      (chime--fetch-and-process (lambda (_events) nil)))
    (should (equal "Async error" (car recorded)))
    (should (equal '(error "boom") (cdr recorded)))))

(ert-deftest test-chime-fetch-and-process-callback-error-records-failure ()
  "Error: when the callback raises during processing, failure is recorded with the processing prefix."
  (let ((recorded nil)
        (chime--process nil)
        (chime--last-check-time '(0 0))
        (chime-modeline-no-events-text " ⏰")
        (chime-modeline-string nil)
        (chime--consecutive-async-failures 0)
        (chime-max-consecutive-failures 0))
    (cl-letf (((symbol-function 'async-start)
               (lambda (_start-form finish-func)
                 (funcall finish-func '(((title . "Event"))))
                 'fake-process))
              ((symbol-function 'chime--record-async-failure)
               (lambda (err prefix) (setq recorded (cons prefix err))))
              ((symbol-function 'force-mode-line-update) (lambda (&optional _))))
      (chime--fetch-and-process (lambda (_events) (error "callback boom"))))
    (should (equal "Error processing events" (car recorded)))
    (should (string-match-p "callback boom"
                            (error-message-string (cdr recorded))))))

(ert-deftest test-chime-fetch-and-process-skips-when-process-live ()
  "Boundary: an active live process blocks a fresh fetch."
  (let ((fetched nil)
        (chime--process 'fake-live-process))
    (cl-letf (((symbol-function 'process-live-p)
               (lambda (proc) (eq proc 'fake-live-process)))
              ((symbol-function 'async-start)
               (lambda (&rest _)
                 (setq fetched t)
                 'unused)))
      (chime--fetch-and-process (lambda (_events) nil)))
    (should-not fetched)))

(provide 'test-chime-fetch-and-process)
;;; test-chime-fetch-and-process.el ends here