blob: ed16b1b16a4b0f084c89149b5374ea040581105f (
plain) (
blame)
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
|
(require 'el-mock)
(require 'cl-macs)
(defconst mastodon-tl-test-base-toot
'((id . 61208)
(created_at . "2017-04-24T19:01:02.000Z")
(in_reply_to_id)
(in_reply_to_account_id)
(sensitive . :json-false)
(spoiler_text . "Spoiler text")
(visibility . "public")
(account (id . 42)
(username . "acct42")
(acct . "acct42@example.space")
(display_name . "Account 42")
(locked . :json-false)
(created_at . "2017-04-01T00:00:00.000Z")
(followers_count . 99)
(following_count . 13)
(statuses_count . 101)
(note . "E"))
(media_attachments . [])
(mentions . [])
(tags . [])
(uri . "tag:example.space,2017-04-24:objectId=654321:objectType=Status")
(url . "https://example.space/users/acct42/updates/123456789")
(reblogs_count . 0)
(favourites_count . 0)
(reblog))
"A sample toot (parsed json)")
(defconst mastodon-tl-test-base-boosted-toot
'((id . 61208)
(created_at . "2017-04-24T20:59:59.000Z")
(in_reply_to_id)
(in_reply_to_account_id)
(sensitive . :json-false)
(spoiler_text . "Spoiler text")
(visibility . "public")
(account (id . 42)
(username . "acct42")
(acct . "acct42@example.space")
(display_name . "Account 42")
(locked . :json-false)
(created_at . "2017-04-01T00:00:00.000Z")
(followers_count . 99)
(following_count . 13)
(statuses_count . 101)
(note . "E"))
(media_attachments . [])
(mentions . [])
(tags . [])
(uri . "tag:example.space,2017-04-24:objectId=654321:objectType=Status")
(url . "https://example.space/users/acct42/updates/123456789")
(reblogs_count . 0)
(favourites_count . 0)
(reblog (id . 4543919)
(created_at . "2017-04-24T19:01:02.000Z")
(in_reply_to_id)
(in_reply_to_account_id)
(sensitive . :json-false)
(spoiler_text . "")
(visibility . "public")
(application)
(account (id . 43)
(username . "acct43")
(acct . "acct43@example.space")
(display_name . "Account 43")
(locked . :json-false)
(created_at . "2017-04-02T00:00:00.000Z")
(followers_count . 1)
(following_count . 1)
(statuses_count . 1)
(note . "Other account"))
(media_attachments . [])
(mentions . [((url . "https://mastodon.social/@johnson")
(acct . "acct42")
(id . 42)
(username . "acct42"))])
(tags . [])
(uri . "tag:example.space,2017-04-24:objectId=654321:objectType=Status")
(content . "<p><span class=\"h-card\"><a href=\"https://example.spacs/@acct42\">@<span>acct42</span></a></span> boost</p>")
(url . "https://example.space/users/acct42/updates/123456789")
(reblogs_count . 1)
(favourites_count . 1)
(favourited)
(reblogged)))
"A sample reblogged/boosted toot (parsed json)")
(ert-deftest remove-html-1 ()
"Should remove all <span> tags."
(let ((input "<span class=\"h-card\">foobar</span> <span>foobaz</span>"))
(should (string= (mastodon-tl--remove-html input) "foobar foobaz"))))
(ert-deftest remove-html-2 ()
"Should replace <\p> tags with two new lines."
(let ((input "foobar</p>"))
(should (string= (mastodon-tl--remove-html input) "foobar\n\n"))))
(ert-deftest more-json ()
"Should request toots older than max_id."
(let ((mastodon-instance-url "https://instance.url"))
(with-mock
(mock (mastodon-http--get-json "https://instance.url/api/v1/timelines/foo?max_id=12345"))
(mastodon-tl--more-json "timelines/foo" 12345))))
(ert-deftest mastodon-tl--relative-time-description ()
"Should format relative time as expected"
(cl-labels ((minutes (n) (* n 60))
(hours (n) (* n (minutes 60)))
(days (n) (* n (hours 24)))
(weeks (n) (* n (days 7)))
(years (n) (* n (days 365)))
(format-seconds-since (seconds)
(let ((time-stamp (time-subtract (current-time) (seconds-to-time seconds))))
(mastodon-tl--relative-time-description time-stamp)))
(check (seconds expected)
(should (string= (format-seconds-since seconds) expected))))
(check 1 "less than a minute ago")
(check 59 "less than a minute ago")
(check 60 "one minute ago")
(check 89 "one minute ago") ;; rounding down
(check 91 "2 minutes ago") ;; rounding up
(check (minutes 3.49) "3 minutes ago") ;; rounding down
(check (minutes 3.52) "4 minutes ago")
(check (minutes 59) "59 minutes ago")
(check (minutes 60) "one hour ago")
(check (minutes 89) "one hour ago")
(check (minutes 91) "2 hours ago")
(check (hours 3.49) "3 hours ago") ;; rounding down
(check (hours 3.51) "4 hours ago") ;; rounding down
(check (hours 23.4) "23 hours ago")
(check (hours 23.6) "one day ago") ;; rounding up
(check (days 1.48) "one day ago") ;; rounding down
(check (days 1.52) "2 days ago") ;; rounding up
(check (days 6.6) "one week ago") ;; rounding up
(check (weeks 2.49) "2 weeks ago") ;; rounding down
(check (weeks 2.51) "3 weeks ago") ;; rounding down
(check (weeks 52) "52 weeks ago")
(check (weeks 53) "one year ago")
(check (years 2.49) "2 years ago") ;; rounding down
(check (years 2.51) "3 years ago") ;; rounding down
))
(ert-deftest mastodon-tl--byline-regular ()
"Should format the regular toot correctly."
(let ((mastodon-tl--show-avatars-p nil)
(timestamp (cdr (assoc 'created_at mastodon-tl-test-base-toot))))
(with-mock
(mock (date-to-time timestamp) => '(22782 21551))
(mock (format-time-string mastodon-toot-timestamp-format '(22782 21551)) => "2999-99-99 00:11:22")
(should (string= (substring-no-properties
(mastodon-tl--byline mastodon-tl-test-base-toot))
"
| Account 42 (@acct42@example.space) 2999-99-99 00:11:22
------------")))))
(ert-deftest mastodon-tl--byline-regular-with-avatar ()
"Should format the regular toot correctly."
(let ((mastodon-tl--show-avatars-p t)
(timestamp (cdr (assoc 'created_at mastodon-tl-test-base-toot))))
(with-mock
(stub create-image => '(image "fake data"))
(mock (date-to-time timestamp) => '(22782 21551))
(mock (format-time-string mastodon-toot-timestamp-format '(22782 21551)) => "2999-99-99 00:11:22")
(should (string= (substring-no-properties
(mastodon-tl--byline mastodon-tl-test-base-toot))
"
| Account 42 (@acct42@example.space) 2999-99-99 00:11:22
------------")))))
(ert-deftest mastodon-tl--byline-boosted ()
"Should format the boosted toot correctly."
(let* ((mastodon-tl--show-avatars-p nil)
(toot (cons '(reblogged . t) mastodon-tl-test-base-toot))
(timestamp (cdr (assoc 'created_at toot))))
(with-mock
(mock (date-to-time timestamp) => '(22782 21551))
(mock (format-time-string mastodon-toot-timestamp-format '(22782 21551)) => "2999-99-99 00:11:22")
(should (string= (substring-no-properties (mastodon-tl--byline toot))
"
| (B) Account 42 (@acct42@example.space) 2999-99-99 00:11:22
------------")))))
(ert-deftest mastodon-tl--byline-favorited ()
"Should format the favourited toot correctly."
(let* ((mastodon-tl--show-avatars-p nil)
(toot (cons '(favourited . t) mastodon-tl-test-base-toot))
(timestamp (cdr (assoc 'created_at toot))))
(with-mock
(mock (date-to-time timestamp) => '(22782 21551))
(mock (format-time-string mastodon-toot-timestamp-format '(22782 21551)) => "2999-99-99 00:11:22")
(should (string= (substring-no-properties (mastodon-tl--byline toot))
"
| (F) Account 42 (@acct42@example.space) 2999-99-99 00:11:22
------------")))))
(ert-deftest mastodon-tl--byline-boosted/favorited ()
"Should format the boosted & favourited toot correctly."
(let* ((mastodon-tl--show-avatars-p nil)
(toot `((favourited . t) (reblogged . t) ,@mastodon-tl-test-base-toot))
(timestamp (cdr (assoc 'created_at toot))))
(with-mock
(mock (date-to-time timestamp) => '(22782 21551))
(mock (format-time-string mastodon-toot-timestamp-format '(22782 21551)) => "2999-99-99 00:11:22")
(should (string= (substring-no-properties (mastodon-tl--byline toot))
"
| (B) (F) Account 42 (@acct42@example.space) 2999-99-99 00:11:22
------------")))))
(ert-deftest mastodon-tl--byline-reblogged ()
"Should format the reblogged toot correctly."
(let* ((mastodon-tl--show-avatars-p nil)
(toot mastodon-tl-test-base-boosted-toot)
(original-toot (cdr (assoc 'reblog mastodon-tl-test-base-boosted-toot)))
(timestamp (cdr (assoc 'created_at toot)))
(original-timestamp (cdr (assoc 'created_at original-toot))))
(with-mock
;; We don't expect to use the toot's timestamp but the timestamp of the
;; reblogged toot:
(mock (date-to-time timestamp) => '(1 2))
(mock (format-time-string mastodon-toot-timestamp-format '(1 2)) => "reblogging time")
(mock (date-to-time original-timestamp) => '(3 4))
(mock (format-time-string mastodon-toot-timestamp-format '(3 4)) => "original time")
(should (string= (substring-no-properties (mastodon-tl--byline toot))
"
| Account 42 (@acct42@example.space) Boosted Account 43 (@acct43@example.space) original time
------------")))))
(ert-deftest mastodon-tl--byline-reblogged-with-avatars ()
"Should format the reblogged toot correctly."
(let* ((mastodon-tl--show-avatars-p t)
(toot mastodon-tl-test-base-boosted-toot)
(original-toot (cdr (assoc 'reblog mastodon-tl-test-base-boosted-toot)))
(timestamp (cdr (assoc 'created_at toot)))
(original-timestamp (cdr (assoc 'created_at original-toot))))
(with-mock
;; We don't expect to use the toot's timestamp but the timestamp of the
;; reblogged toot:
(stub create-image => '(image "fake data"))
(mock (date-to-time timestamp) => '(1 2))
(mock (format-time-string mastodon-toot-timestamp-format '(1 2)) => "reblogging time")
(mock (date-to-time original-timestamp) => '(3 4))
(mock (format-time-string mastodon-toot-timestamp-format '(3 4)) => "original time")
(should (string= (substring-no-properties (mastodon-tl--byline toot))
"
| Account 42 (@acct42@example.space) Boosted Account 43 (@acct43@example.space) original time
------------")))))
(ert-deftest mastodon-tl--byline-reblogged-boosted/favorited ()
"Should format the reblogged toot that was also boosted & favoritedcorrectly."
(let* ((mastodon-tl--show-avatars-p nil)
(toot `((favourited . t) (reblogged . t) ,@mastodon-tl-test-base-boosted-toot))
(original-toot (cdr (assoc 'reblog mastodon-tl-test-base-boosted-toot)))
(timestamp (cdr (assoc 'created_at toot)))
(original-timestamp (cdr (assoc 'created_at original-toot))))
(with-mock
;; We don't expect to use the toot's timestamp but the timestamp of the
;; reblogged toot:
(mock (date-to-time timestamp) => '(1 2))
(mock (format-time-string mastodon-toot-timestamp-format '(1 2)) => "reblogging time")
(mock (date-to-time original-timestamp) => '(3 4))
(mock (format-time-string mastodon-toot-timestamp-format '(3 4)) => "original time")
(should (string= (substring-no-properties (mastodon-tl--byline toot))
"
| (B) (F) Account 42 (@acct42@example.space) Boosted Account 43 (@acct43@example.space) original time
------------")))))
(ert-deftest mastodon-tl--byline-timestamp-has-relative-display ()
"Should display the timestamp with a relative time."
(let ((mastodon-tl--show-avatars-p nil)
(timestamp (cdr (assoc 'created_at mastodon-tl-test-base-toot))))
(with-mock
(mock (date-to-time timestamp) => '(22782 21551))
(mock (current-time) => '(22782 22000))
(mock (format-time-string mastodon-toot-timestamp-format '(22782 21551)) => "2999-99-99 00:11:22")
(let* ((formatted-string (mastodon-tl--byline mastodon-tl-test-base-toot))
(timestamp-start (string-match "2999-99-99" formatted-string))
(properties (text-properties-at timestamp-start formatted-string)))
(should (equal '(22782 21551) (plist-get properties 'timestamp)))
(should (string-equal "7 minutes ago" (plist-get properties 'display)))))))
|