aboutsummaryrefslogtreecommitdiff
path: root/tests/test-calendar-sync-source-fetch-sentinel.el
blob: 0b7ba1cfe2c6289271aa800e35c43555a3d1ebe2 (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
;;; test-calendar-sync-source-fetch-sentinel.el --- Tests for the fetch sentinel -*- lexical-binding: t; -*-

;;; Commentary:
;; calendar-sync--fetch-sentinel-finish is the extracted tail of the async
;; .ics fetch sentinel.  The async-worker tests stub the whole fetch, so its
;; success, failure, and temp-file-cleanup branches were never exercised.
;; These tests drive the helper directly with fake success/failure inputs,
;; no live curl process.

;;; Code:

(require 'ert)
(require 'cl-lib)
(require 'calendar-sync)

(ert-deftest test-calendar-sync-fetch-sentinel-success-passes-temp-file ()
  "Normal: on success the callback gets the temp file and it is not deleted."
  (let ((temp-file (make-temp-file "calendar-sync-sentinel-" nil ".ics"))
        (buffer (generate-new-buffer " *cs-test*"))
        (got 'unset))
    (unwind-protect
        (progn
          (calendar-sync--fetch-sentinel-finish
           t "finished\n" temp-file buffer (lambda (r) (setq got r)))
          (should (equal got temp-file))
          (should (file-exists-p temp-file))
          (should-not (buffer-live-p buffer)))
      (when (file-exists-p temp-file) (delete-file temp-file))
      (when (buffer-live-p buffer) (kill-buffer buffer)))))

(ert-deftest test-calendar-sync-fetch-sentinel-failure-deletes-and-passes-nil ()
  "Error: on failure the temp file is deleted and the callback gets nil."
  (let ((temp-file (make-temp-file "calendar-sync-sentinel-" nil ".ics"))
        (buffer (generate-new-buffer " *cs-test*"))
        (got 'unset)
        (logged nil))
    (unwind-protect
        (cl-letf (((symbol-function 'calendar-sync--log-silently)
                   (lambda (&rest _) (setq logged t))))
          (calendar-sync--fetch-sentinel-finish
           nil "exited abnormally with code 22\n" temp-file buffer
           (lambda (r) (setq got r)))
          (should (null got))
          (should-not (file-exists-p temp-file))
          (should logged)
          (should-not (buffer-live-p buffer)))
      (when (file-exists-p temp-file) (delete-file temp-file))
      (when (buffer-live-p buffer) (kill-buffer buffer)))))

(ert-deftest test-calendar-sync-fetch-sentinel-failure-tolerates-missing-temp-file ()
  "Boundary: failure with the temp file already gone does not error."
  (let ((temp-file (make-temp-file "calendar-sync-sentinel-" nil ".ics"))
        (got 'unset))
    (delete-file temp-file)
    (cl-letf (((symbol-function 'calendar-sync--log-silently) #'ignore))
      (calendar-sync--fetch-sentinel-finish
       nil "failed\n" temp-file nil (lambda (r) (setq got r)))
      (should (null got)))))

(ert-deftest test-calendar-sync-fetch-sentinel-tolerates-dead-buffer ()
  "Boundary: a already-dead process buffer is not touched on success."
  (let ((temp-file (make-temp-file "calendar-sync-sentinel-" nil ".ics"))
        (got 'unset))
    (unwind-protect
        (progn
          (calendar-sync--fetch-sentinel-finish
           t "finished\n" temp-file nil (lambda (r) (setq got r)))
          (should (equal got temp-file)))
      (when (file-exists-p temp-file) (delete-file temp-file)))))

(provide 'test-calendar-sync-source-fetch-sentinel)
;;; test-calendar-sync-source-fetch-sentinel.el ends here