diff options
author | marty hiatt <martianhiatus@riseup.net> | 2024-09-20 20:37:35 +0200 |
---|---|---|
committer | marty hiatt <martianhiatus@riseup.net> | 2024-09-20 20:37:35 +0200 |
commit | 6803d680c6415e4cc6dca66e597776dae0394170 (patch) | |
tree | 7795f10a3b5337d4b2169d6eab3adec654fc7cc0 /test/mastodon-tl-tests.el | |
parent | 3443b49c55f65ae8e0b07e93e1e0299ce1bf8ed6 (diff) | |
parent | 657bd3664749f66d9da0a8a5336b51c592670ecf (diff) |
Merge branch 'develop'
Diffstat (limited to 'test/mastodon-tl-tests.el')
-rw-r--r-- | test/mastodon-tl-tests.el | 296 |
1 files changed, 165 insertions, 131 deletions
diff --git a/test/mastodon-tl-tests.el b/test/mastodon-tl-tests.el index 2aa0505..183f83d 100644 --- a/test/mastodon-tl-tests.el +++ b/test/mastodon-tl-tests.el @@ -41,9 +41,9 @@ (following_count . 13) (statuses_count . 101) (note . "E")) - (media_attachments . []) - (mentions . []) - (tags . []) + (media_attachments . ()) + (mentions . ()) + (tags . ()) (uri . "tag:example.space,2017-04-24:objectId=654321:objectType=Status") (url . "https://example.space/users/acct42/updates/123456789") (content . "<p>Just some text</p>") @@ -70,9 +70,9 @@ (following_count . 13) (statuses_count . 101) (note . "E")) - (media_attachments . []) - (mentions . []) - (tags . []) + (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) @@ -95,12 +95,12 @@ (following_count . 1) (statuses_count . 1) (note . "Other account")) - (media_attachments . []) - (mentions . [((url . "https://mastodon.social/@johnson") + (media_attachments . ()) + (mentions . (((url . "https://mastodon.social/@johnson") (acct . "acct42") (id . 42) - (username . "acct42"))]) - (tags . []) + (username . "acct42")))) + (tags . ()) (uri . "tag:example.space,2017-04-24:objectId=654321:objectType=Status") (content . "<p><span class=\"h-card\"><a href=\"https://example.space/@acct42\">@<span>acct42</span></a></span> boost</p>") (url . "https://example.space/users/acct42/updates/123456789") @@ -220,6 +220,9 @@ Strict-Transport-Security: max-age=31536000 '(("since_id" . "12345")))) (mastodon-tl--updated-json "timelines/foo" "12345")))) +;; broken by monnier's `mastodon-tl--human-duration', which uses "secs" rather +;; than "just now". its not just the abbrevs, also the rounding works +;; differently (ert-deftest mastodon-tl--relative-time-description () "Should format relative time as expected" (cl-labels ((minutes (n) (* n 60)) @@ -228,36 +231,39 @@ Strict-Transport-Security: max-age=31536000 (weeks (n) (* n (days 7))) (years (n) (* n (days 365))) (format-seconds-since (seconds) - (let ((timestamp (time-subtract (current-time) (seconds-to-time seconds)))) - (mastodon-tl--relative-time-description timestamp))) + (let ((timestamp (time-subtract (current-time) (seconds-to-time seconds)))) + (mastodon-tl--relative-time-description timestamp))) (check (seconds expected) - (should (string= (format-seconds-since seconds) expected)))) - (check 1 "just now") - (check 59 "just now") - (check 60 "1 minute ago") - (check 89 "1 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") + (should (string= (format-seconds-since seconds) expected)))) + (check 1 "1 sec ago") + (check 59 "59 secs ago") + (check 60 "1 min ago") + (check 89 "1 min ago") ;; rounding down + (check 91 "1 min ago") ;"2 minutes ago") ;; rounding up + (check (minutes 3.49) "3 mins ago") ;; rounding down + (check (minutes 3.52) "3 mins ago") ;"4 minutes ago") + (check (minutes 59) "59 mins ago") (check (minutes 60) "1 hour ago") - (check (minutes 89) "1 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) "1 day ago") ;; rounding up - (check (days 1.48) "1 day ago") ;; rounding down - (check (days 1.52) "2 days ago") ;; rounding up - (check (days 6.6) "1 week ago") ;; rounding up - (check (weeks 2.49) "2 weeks ago") ;; rounding down - (check (weeks 2.51) "3 weeks ago") ;; rounding down - (check (1- (weeks 52)) "52 weeks ago") - (check (weeks 52) "1 year ago") - (check (years 2.49) "2 years ago") ;; rounding down - (check (years 2.51) "3 years ago") ;; rounding down + (check (minutes 89) "1 hour, 29 mins ago") + (check (minutes 91) "1 hour, 31 mins ago") ;"2 hours ago") + (check (hours 3.49) "3 hours, 29 mins ago") ; "3 hours ago") ;; rounding down + (check (hours 3.51) "3 hours, 30 mins ago") ; "4 hours ago") ;; rounding down + (check (hours 23.4) "23 hours, 24 mins ago"); "23 hours ago") + (check (hours 23.6) "23 hours, 36 mins ago") ; "1 day ago") ;; rounding up + (check (days 1.48) "1 day, 11 hours ago") ; "1 day ago") ;; rounding down + (check (days 1.52) "1 day, 12 hours ago"); "2 days ago") ;; rounding up + (check (days 6.6) "6 days, 14 hours ago"); "1 week ago") ;; rounding up + (check (weeks 2.49) "2 weeks, 3 days ago"); "2 weeks ago") ;; rounding down + (check (weeks 2.51) "2 weeks, 3 days ago"); "3 weeks ago") ;; rounding down + (check (1- (weeks 52)) "11 months, 4 weeks ago") ;"52 weeks ago") + (check (weeks 52) "11 months, 4 weeks ago") ;"1 year ago") + (check (years 2.49) "2 years, 5 months ago"); "2 years ago") ;; rounding down + (check (years 2.51) "2 years, 6 months ago"); "3 years ago") ;; rounding down )) +;; broken by monnier's `mastodon-tl--human-duration', which uses "secs" rather +;; than "just now". its not just the abbrevs, also the rounding works +;; differently (ert-deftest mastodon-tl--relative-time-details--next-update () "Should calculate the next update time information as expected" (let ((current-time (current-time))) @@ -267,33 +273,33 @@ Strict-Transport-Security: max-age=31536000 (weeks (n) (* n (days 7))) (years (n) (* n (days 365.25))) (next-update (seconds-ago) - (let* ((timestamp (time-subtract current-time - (seconds-to-time seconds-ago)))) - (cdr (mastodon-tl--relative-time-details timestamp current-time)))) + (let* ((timestamp (time-subtract current-time + (seconds-to-time seconds-ago)))) + (cdr (mastodon-tl--relative-time-details timestamp current-time)))) (check (seconds-ago) - (let* ((timestamp (time-subtract current-time (seconds-to-time seconds-ago))) - (at-now (mastodon-tl--relative-time-description timestamp current-time)) - (at-one-second-before (mastodon-tl--relative-time-description - timestamp - (time-subtract (next-update seconds-ago) - (seconds-to-time 1)))) - (at-result (mastodon-tl--relative-time-description - timestamp - (next-update seconds-ago)))) - (when nil ;; change to t to debug test failures - (prin1 (format "\nFor %s: %s / %s" - seconds-ago - (time-to-seconds - (time-subtract (next-update seconds-ago) - timestamp)) - (round - (time-to-seconds - (time-subtract (next-update seconds-ago) - current-time)))))) - ;; a second earlier the description is the same as at current time - (should (string= at-now at-one-second-before)) - ;; but at the result time it is different - (should-not (string= at-one-second-before at-result))))) + (let* ((timestamp (time-subtract current-time (seconds-to-time seconds-ago))) + (at-now (mastodon-tl--relative-time-description timestamp current-time)) + (at-one-second-before (mastodon-tl--relative-time-description + timestamp + (time-subtract (next-update seconds-ago) + (seconds-to-time 1)))) + (at-result (mastodon-tl--relative-time-description + timestamp + (next-update seconds-ago)))) + (when nil ;; change to t to debug test failures + (prin1 (format "\nFor %s: %s / %s" + seconds-ago + (time-to-seconds + (time-subtract (next-update seconds-ago) + timestamp)) + (round + (time-to-seconds + (time-subtract (next-update seconds-ago) + current-time)))))) + ;; a second earlier the description is the same as at current time + (should (string= at-now at-one-second-before)) + ;; but at the result time it is different + (should-not (string= at-one-second-before at-result))))) (check 0) (check 1) (check 59) @@ -525,7 +531,7 @@ Strict-Transport-Security: max-age=31536000 (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 (current-time) => '(22782 22000)) ; not sure why this breaks it (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 @@ -534,7 +540,13 @@ Strict-Transport-Security: max-age=31536000 (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))))))) + (should (string-equal ;;"7 minutes ago" + ;; "7 mins ago" ;; not sure why its diff now + + ;; FIXME: this value has become really relative so we will have to + ;; keep changing it! + "7 years, 4 months ago" + (plist-get properties 'display))))))) (ert-deftest mastodon-tl--consider-timestamp-for-updates-no-active-callback () "Should update the timestamp update variables as expected." @@ -862,13 +874,13 @@ constant." (let ((now (current-time)) markers) (cl-labels ((insert-timestamp (n) - (insert (format "\nSome text before timestamp %s:" n)) - (insert (propertize - (format "timestamp #%s" n) - 'timestamp (time-subtract now (seconds-to-time (* 60 n))) - 'display (format "unset %s" n))) - (push (copy-marker (point)) markers) - (insert " some more text."))) + (insert (format "\nSome text before timestamp %s:" n)) + (insert (propertize + (format "timestamp #%s" n) + 'timestamp (time-subtract now (seconds-to-time (* 60 n))) + 'display (format "unset %s" n))) + (push (copy-marker (point)) markers) + (insert " some more text."))) (with-temp-buffer (cl-dotimes (n 12) (insert-timestamp (+ n 2))) (setq markers (nreverse markers)) @@ -879,15 +891,17 @@ constant." ;; make the initial call (mastodon-tl--update-timestamps-callback (current-buffer) nil) - (should (equal '("2 minutes ago" "3 minutes ago" "4 minutes ago" "5 minutes ago" "6 minutes ago" - "unset 7" "unset 8" "unset 9" "unset 10" "unset 11" "unset 12" "unset 13") - (tl-tests--property-values-at 'display - (tl-tests--all-regions-with-property 'timestamp)))) + (should (equal + '("2 mins ago" "3 mins ago" "4 mins ago" + "5 mins ago" "6 mins ago" "unset 7" "unset 8" + "unset 9" "unset 10" "unset 11" "unset 12" "unset 13") + (tl-tests--property-values-at 'display + (tl-tests--all-regions-with-property 'timestamp)))) ;; fake the follow-up call (mastodon-tl--update-timestamps-callback (current-buffer) (nth 4 markers)) - (should (equal '("2 minutes ago" "3 minutes ago" "4 minutes ago" "5 minutes ago" "6 minutes ago" - "7 minutes ago" "8 minutes ago" "9 minutes ago" "10 minutes ago" "11 minutes ago" + (should (equal '("2 mins ago" "3 mins ago" "4 mins ago" "5 mins ago" "6 mins ago" + "7 mins ago" "8 mins ago" "9 mins ago" "10 mins ago" "11 mins ago" "unset 12" "unset 13") (tl-tests--property-values-at 'display (tl-tests--all-regions-with-property 'timestamp)))) @@ -895,9 +909,9 @@ constant." ;; fake the follow-up call (mastodon-tl--update-timestamps-callback (current-buffer) (nth 9 markers)) - (should (equal '("2 minutes ago" "3 minutes ago" "4 minutes ago" "5 minutes ago" "6 minutes ago" - "7 minutes ago" "8 minutes ago" "9 minutes ago" "10 minutes ago" "11 minutes ago" - "12 minutes ago" "13 minutes ago") + (should (equal '("2 mins ago" "3 mins ago" "4 mins ago" "5 mins ago" "6 mins ago" + "7 mins ago" "8 mins ago" "9 mins ago" "10 mins ago" "11 mins ago" + "12 mins ago" "13 mins ago") (tl-tests--property-values-at 'display (tl-tests--all-regions-with-property 'timestamp)))) (should (null (marker-position (nth 9 markers))))))))) @@ -926,13 +940,13 @@ constant." (insert "some text before\n") (setq toot-start (point)) (with-mock - (mock (mastodon-profile--get-preferences-pref - 'reading:expand:spoilers) - => :json-false) - (stub create-image => '(image "fake data")) - (stub shr-render-region => nil) ;; Travis's Emacs doesn't have libxml - (insert - (mastodon-tl--spoiler normal-toot-with-spoiler))) + (mock (mastodon-profile--get-preferences-pref + 'reading:expand:spoilers) + => :json-false) + (stub create-image => '(image "fake data")) + (stub shr-render-region => nil) ;; Travis's Emacs doesn't have libxml + (insert + (mastodon-tl--spoiler normal-toot-with-spoiler))) (setq toot-end (point)) (insert "\nsome more text.") (add-text-properties @@ -1009,29 +1023,29 @@ constant." (ert-deftest mastodon-tl--extract-hashtag-from-url-mastodon-link () "Should extract the hashtag from a tags url." - (should (equal (mastodon-tl--extract-hashtag-from-url - "https://example.org/tags/foo" - "https://example.org") - "foo"))) + (should (equal (mastodon-tl--hashtag-from-url + "https://example.org/tags/foo" + "https://example.org") + "foo"))) (ert-deftest mastodon-tl--extract-hashtag-from-url-other-link () "Should extract the hashtag from a tag url." - (should (equal (mastodon-tl--extract-hashtag-from-url - "https://example.org/tag/foo" - "https://example.org") - "foo"))) + (should (equal (mastodon-tl--hashtag-from-url + "https://example.org/tag/foo" + "https://example.org") + "foo"))) (ert-deftest mastodon-tl--extract-hashtag-from-url-wrong-instance () "Should not find a tag when the instance doesn't match." - (should (null (mastodon-tl--extract-hashtag-from-url - "https://example.org/tags/foo" - "https://other.example.org")))) + (should (null (mastodon-tl--hashtag-from-url + "https://example.org/tags/foo" + "https://other.example.org")))) (ert-deftest mastodon-tl--extract-hashtag-from-url-not-tag () "Should not find a hashtag when not a tag url" - (should (null (mastodon-tl--extract-hashtag-from-url - "https://example.org/@userid" - "https://example.org")))) + (should (null (mastodon-tl--hashtag-from-url + "https://example.org/@userid" + "https://example.org")))) (ert-deftest mastodon-tl--userhandles () "Should recognise userhandles in a toot and add the required properties to it." @@ -1058,20 +1072,20 @@ constant." (ert-deftest mastodon-tl--extract-userhandle-from-url-correct-case () "Should extract the user handle from url." - (should (equal (mastodon-tl--extract-userhandle-from-url + (should (equal (mastodon-tl--userhandle-from-url "https://example.org/@someuser" "@SomeUser") "@SomeUser@example.org"))) (ert-deftest mastodon-tl--extract-userhandle-from-url-missing-at-in-text () "Should not extract a user handle from url if the text is wrong." - (should (null (mastodon-tl--extract-userhandle-from-url + (should (null (mastodon-tl--userhandle-from-url "https://example.org/@someuser" "SomeUser")))) (ert-deftest mastodon-tl--extract-userhandle-from-url-query-in-url () "Should not extract a user handle from url if there is a query param." - (should (null (mastodon-tl--extract-userhandle-from-url + (should (null (mastodon-tl--userhandle-from-url "https://example.org/@someuser?shouldnot=behere" "SomeUser")))) @@ -1090,7 +1104,7 @@ correct value for following, as well as notifications enabled or disabled." (let ((response-buffer-true (current-buffer))) (insert mastodon-tl--follow-notify-true-response) (with-mock - (mock (mastodon-http--post url-follow-only nil) + (mock (mastodon-http--post url-follow-only nil nil nil nil) => response-buffer-true) (should (equal @@ -1103,7 +1117,7 @@ correct value for following, as well as notifications enabled or disabled." (let ((response-buffer-true (current-buffer))) (insert mastodon-tl--follow-notify-true-response) (with-mock - (mock (mastodon-http--post url-mute nil) + (mock (mastodon-http--post url-mute nil nil nil nil) => response-buffer-true) (should (equal @@ -1116,7 +1130,7 @@ correct value for following, as well as notifications enabled or disabled." (let ((response-buffer-true (current-buffer))) (insert mastodon-tl--follow-notify-true-response) (with-mock - (mock (mastodon-http--post url-block nil) + (mock (mastodon-http--post url-block nil nil nil nil) => response-buffer-true) (should (equal @@ -1130,7 +1144,8 @@ correct value for following, as well as notifications enabled or disabled." (insert mastodon-tl--follow-notify-true-response) (with-mock (with-mock - (mock (mastodon-http--post url-true nil) => response-buffer-true) + (mock (mastodon-http--post url-true nil nil nil nil) + => response-buffer-true) (should (equal (mastodon-tl--do-user-action-function url-true @@ -1143,7 +1158,8 @@ correct value for following, as well as notifications enabled or disabled." (let ((response-buffer-false (current-buffer))) (insert mastodon-tl--follow-notify-false-response) (with-mock - (mock (mastodon-http--post url-false nil) => response-buffer-false) + (mock (mastodon-http--post url-false nil nil nil nil) + => response-buffer-false) (should (equal (mastodon-tl--do-user-action-function url-false @@ -1159,37 +1175,37 @@ correct value for following, as well as notifications enabled or disabled." (let* ((toot mastodon-tl-test-base-toot) (account (alist-get 'account toot))) (with-mock - ;; no longer needed after our refactor - ;; (mock (mastodon-http--api "reports") => "https://instance.url/api/v1/reports") - ;; (mock (mastodon-tl--toot-or-base - ;; (mastodon-tl--property 'item-json :no-move)) - ;; => mastodon-tl-test-base-toot) - (mock (read-string "Add comment [optional]: ") => "Dummy complaint") - (stub y-or-n-p => nil) ; no to all - (should (equal (mastodon-tl--report-params account toot) - '(("account_id" . 42) - ("comment" . "Dummy complaint") - ("category" . "other")))) - (with-mock - (stub y-or-n-p => t) ; yes to all - (mock (mastodon-tl--read-rules-ids) => '(1 2 3)) + ;; no longer needed after our refactor + ;; (mock (mastodon-http--api "reports") => "https://instance.url/api/v1/reports") + ;; (mock (mastodon-tl--toot-or-base + ;; (mastodon-tl--property 'item-json :no-move)) + ;; => mastodon-tl-test-base-toot) + (mock (read-string "Add comment [optional]: ") => "Dummy complaint") + (stub y-or-n-p => nil) ; no to all (should (equal (mastodon-tl--report-params account toot) - '(("rule_ids[]" . 3) - ("rule_ids[]" . 2) - ("rule_ids[]" . 1) - ("account_id" . 42) + '(("account_id" . 42) ("comment" . "Dummy complaint") - ("status_ids[]" . 61208) - ("forward" . "true"))))))))) + ("category" . "other")))) + (with-mock + (stub y-or-n-p => t) ; yes to all + (mock (mastodon-tl--read-rules-ids) => '(1 2 3)) + (should (equal (mastodon-tl--report-params account toot) + '(("rule_ids[]" . 1) + ("rule_ids[]" . 2) + ("rule_ids[]" . 3) + ("account_id" . 42) + ("comment" . "Dummy complaint") + ("status_ids[]" . 61208) + ("forward" . "true"))))))))) (ert-deftest mastodon-tl--report-build-params () "" (should (equal (mastodon-tl--report-build-params 42 "Dummy complaint" 61208 "true" nil '(1 2 3)) - '(("rule_ids[]" . 3) + '(("rule_ids[]" . 1) ("rule_ids[]" . 2) - ("rule_ids[]" . 1) + ("rule_ids[]" . 3) ("account_id" . 42) ("comment" . "Dummy complaint") ("status_ids[]" . 61208) @@ -1234,3 +1250,21 @@ correct value for following, as well as notifications enabled or disabled." "We also do not accept hate speech.")) (should (equal '("2" "5" "6") (mastodon-tl--read-rules-ids)))))) + + +;;; UTILS tests + +(ert-deftest mastodon-tl--map-alist () + "Should return a list of values from `mastodon-tl--test-instance-rules'. +The key is 'id." + (should (equal + (mastodon-tl--map-alist 'id mastodon-tl--test-instance-rules) + '("1" "2" "3" "4" "5" "6" "7" "8")))) + +(ert-deftest mastodon-tl--map-alist-vals-to-alist () + "Should return an alist of value1 value2, using key1 id, and key2 text." + (should + (equal + (mastodon-tl--map-alist-vals-to-alist + 'id 'text mastodon-tl--test-instance-rules) + '(("1" . "We do not accept racism.") ("2" . "We do not accept homophobia.") ("3" . "We do not accept sexism.") ("4" . "We do not accept ableism.") ("5" . "We do not accept harassment.") ("6" . "We also do not accept hate speech.") ("7" . "We do not accept abuse of minors.") ("8" . "We do not accept glorification of violence."))))) |