forked from KeyWeeUsr/emacs-syncthing
-
Notifications
You must be signed in to change notification settings - Fork 0
/
syncthing-tests.el
192 lines (172 loc) · 6.89 KB
/
syncthing-tests.el
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
;;; syncthing-tests.el -- tests for syncthing -*- lexical-binding: t; -*-
;; SPDX-License-Identifier: GPL-3.0-or-later
;;; Commentary:
;;; Code:
(require 'ert)
(require 'syncthing-custom)
(require 'syncthing-network)
(require 'syncthing-state)
(require 'syncthing)
(defun syncthing-ert-cleanup ()
(should (eq nil syncthing--servers))
(mapc
(lambda (buff)
(unless
(or (string-match-p (regexp-quote "*Messages*") (buffer-name buff))
(string-match-p (regexp-quote " *code-conversion-work*")
(buffer-name buff))
(string-match-p (regexp-quote " *code-converting-work*")
(buffer-name buff))
(string-match-p "syncthing.*\\.el" (buffer-name buff)))
(kill-buffer buff)))
(buffer-list)))
(ert-deftest syncthing-run-customize ()
"Run `customize-variable' on missing API token."
(let (called args throw)
(syncthing-ert-cleanup)
(advice-add 'syncthing--interactive-common
:override
(lambda (&rest rest) (setq args rest)))
(advice-add 'customize-variable
:override
(lambda (&rest _) (setq called t)))
(syncthing)
(advice-remove 'syncthing--interactive-common
(lambda (&rest rest) (setq args rest)))
(advice-remove 'customize-variable
(lambda (&rest _) (setq called t)))
(should
(string= (format "%s" args)
(format "%s"`(,syncthing-default-name ,syncthing-base-url ""))))
(should called)
(condition-case nil
(apply 'syncthing--interactive-common args)
(user-error (setq throw t)))
(should throw)))
(ert-deftest syncthing-run-customize-keep-focused ()
"Run `customize-variable' on missing API token."
(let (args throw)
(syncthing-ert-cleanup)
(advice-add 'syncthing--interactive-common
:override
(lambda (&rest rest) (setq args rest)))
;; Creating customization items... etc noise
(advice-add 'message :override (lambda (&rest _)))
(syncthing)
(advice-remove 'syncthing--interactive-common
(lambda (&rest rest) (setq args rest)))
(advice-remove 'message (lambda (&rest _)))
(should
(string= (format "%s" args)
(format "%s"`(,syncthing-default-name ,syncthing-base-url ""))))
(condition-case nil
(apply 'syncthing--interactive-common args)
(user-error (setq throw t)))
(should throw)
(should (string= "*Customize Option: Syncthing Default Server Token*"
(buffer-name (current-buffer))))))
(ert-deftest syncthing-use-default-token ()
"If set to non-empty, use default token."
(let* ((dummy "meow")
(syncthing-default-server-token dummy)
called args)
(syncthing-ert-cleanup)
(advice-add 'syncthing--interactive-common
:override
(lambda (&rest rest) (setq args rest)))
(advice-add 'customize-variable
:override
(lambda (&rest _) (setq called t)))
(syncthing)
(advice-remove 'syncthing--interactive-common
(lambda (&rest rest) (setq args rest)))
(advice-remove 'customize-variable
(lambda (&rest _) (setq called t)))
(should
(string= (format "%s" args)
(format "%s"`(,syncthing-default-name ,syncthing-base-url ,dummy))))
(should (not called))))
(ert-deftest syncthing-refresh-reject ()
"Reject activating the refresh mode if not in `syncthing-mode'."
(with-temp-buffer
(syncthing-ert-cleanup)
(let (called)
(condition-case err
(progn
(advice-add 'auto-revert-mode
:override
(lambda (&rest _) (setq called t)))
(syncthing-auto-refresh-mode)
(advice-remove 'auto-revert-mode
(lambda (&rest _) (setq called t))))
(user-error (should (string-match "not .*?syncthing-mode.*?"
(error-message-string err)))
(setq called t))
(t (message "Wrong error: %s" (error-message-string err))
(should nil)))
(should called))))
(ert-deftest syncthing-refresh-activate ()
"Check whether `auto-revert-mode' gets activated."
(with-temp-buffer
(syncthing-ert-cleanup)
(let (called)
(advice-add 'auto-revert-mode
:override
(lambda (&rest _) (setq called t)))
(syncthing-mode)
(syncthing-auto-refresh-mode)
(advice-remove 'auto-revert-mode
(lambda (&rest _) (setq called t)))
(should called))))
(ert-deftest syncthing-request-apply ()
"Check endpoint formatting for `syncthing--request'."
(let* ((name "dummy-name")
(token "dummy-token")
(url "https://base-url.tld:1234")
(server (syncthing--server :name name :url url :token token))
args)
(syncthing-ert-cleanup)
(advice-add 'syncthing--request
:override
(lambda (&rest largs) (setq args largs)))
(syncthing-request server "GET" "something")
(should (string= (format "%s" args)
(format "(GET %s/something %s)" url token)))
(syncthing-request server "GET" "/something")
(should (string= (format "%s" args)
(format "(GET %s//something %s)" url token)))
(advice-remove 'syncthing--request
(lambda (&rest largs) (setq args largs)))))
(ert-deftest syncthing-multi-client-bug ()
"Ensure a new client buffer creates own `syncthing-server' instance."
(syncthing-ert-cleanup)
(should (not syncthing--servers))
(advice-add 'auto-revert-mode :override (lambda (&rest _)))
(syncthing-with-base "dummy1" "url1" "token1")
(set-buffer (window-buffer (selected-window)))
(should
(not (eq nil (string-match
"dummy1"
(buffer-name (current-buffer))))))
(should (string= "dummy1" (syncthing-server-name syncthing-server)))
(should (eq 1 (length syncthing--servers)))
(syncthing-with-base "dummy2" "url2" "token2")
(set-buffer (window-buffer (selected-window)))
(should
(not (eq nil (string-match
"dummy2"
(buffer-name (current-buffer))))))
(should (string= "dummy2" (syncthing-server-name syncthing-server)))
(should (eq 2 (length syncthing--servers)))
(advice-add 'message :override (lambda (&rest _)))
(kill-buffer (get-buffer (syncthing-buffer-name syncthing-buffer)))
(advice-remove 'message (lambda (&rest _)))
(should (eq 1 (length syncthing--servers)))
(advice-add 'message :override (lambda (&rest _)))
(kill-buffer (get-buffer (syncthing-buffer-name syncthing-buffer)))
(advice-remove 'message (lambda (&rest _)))
(should (eq 0 (length syncthing--servers)))
(should (eq nil syncthing--servers))
(advice-remove 'auto-revert-mode (lambda (&rest _))))
(provide 'syncthing-tests)
;;; syncthing-tests.el ends here