|
| 1 | +;;; teams4e-availability-tests.el --- Availability picker tests -*- lexical-binding: t; -*- |
| 2 | +(unless (fboundp 'teams4e-test-availability-context) |
| 3 | + (load "teams4e-tests" nil t)) |
| 4 | + |
| 5 | +(defmacro teams4e-test-with-availability (&rest body) |
| 6 | + "Run BODY in a synthetic availability workspace with a three-day window." |
| 7 | + (declare (indent 0)) |
| 8 | + `(let ((process-environment (copy-sequence process-environment)) |
| 9 | + (teams4e-meeting-availability-interval 30)) |
| 10 | + (setenv "TZ" "UTC") |
| 11 | + (with-temp-buffer |
| 12 | + (teams4e-availability-mode) |
| 13 | + (let ((context (teams4e-test-availability-context))) |
| 14 | + (setq teams4e-availability--chat (teams4e--get context 'chat) |
| 15 | + teams4e-availability--event-id "event-availability" |
| 16 | + teams4e-availability--payload (teams4e--get context 'payload) |
| 17 | + teams4e-availability--window |
| 18 | + (list (date-to-time "2099-08-10T00:00:00Z") |
| 19 | + (date-to-time "2099-08-13T00:00:00Z")))) |
| 20 | + ,@body))) |
| 21 | + |
| 22 | +(ert-deftest teams4e-availability-continuous-day-preserves-duration () |
| 23 | + (teams4e-test-with-availability |
| 24 | + (should (eq teams4e-availability--view 'timeline)) |
| 25 | + (let ((slots (teams4e-availability--suggestions)) previous) |
| 26 | + (should (= 48 (length slots))) |
| 27 | + (dolist (entry slots) |
| 28 | + (let* ((slot (teams4e--get entry 'meetingTimeSlot)) |
| 29 | + (start (teams4e-availability--time slot 'start)) |
| 30 | + (end (teams4e-availability--time slot 'end))) |
| 31 | + (should (= 2700 (float-time (time-subtract end start)))) |
| 32 | + (when previous |
| 33 | + (should (= 1800 (float-time (time-subtract start previous))))) |
| 34 | + (setq previous start)))) |
| 35 | + (teams4e-availability--render) |
| 36 | + (should (equal "09:00 - 09:45" |
| 37 | + (teams4e-availability--time-label |
| 38 | + (teams4e--get (teams4e-availability--selected-suggestion) |
| 39 | + 'meetingTimeSlot)))) |
| 40 | + (teams4e-availability-next) |
| 41 | + (should (equal teams4e-availability--selected-id |
| 42 | + (get-text-property (point) 'teams4e-availability-id))) |
| 43 | + (should (string-match-p "09:30 - 10:15" (thing-at-point 'line t))))) |
| 44 | + |
| 45 | +(ert-deftest teams4e-availability-day-navigation-is-local-and-reversible () |
| 46 | + (teams4e-test-with-availability |
| 47 | + (cl-letf (((symbol-function 'teams4e--run-json) |
| 48 | + (lambda (&rest _) (ert-fail "Unexpected network request")))) |
| 49 | + (teams4e-availability-next-day) |
| 50 | + (should (equal "2099-08-11" |
| 51 | + (format-time-string "%F" teams4e-availability--day))) |
| 52 | + (should (equal "09:00 - 09:45" |
| 53 | + (teams4e-availability--time-label |
| 54 | + (teams4e--get (teams4e-availability--selected-suggestion) |
| 55 | + 'meetingTimeSlot)))) |
| 56 | + (teams4e-availability-previous-day) |
| 57 | + (should (equal "2099-08-10" |
| 58 | + (format-time-string "%F" teams4e-availability--day))) |
| 59 | + (teams4e-availability-next-day 2) |
| 60 | + (teams4e-availability-original-day) |
| 61 | + (should (equal "2099-08-10" |
| 62 | + (format-time-string "%F" teams4e-availability--day)))) |
| 63 | + (let (requested) |
| 64 | + (cl-letf (((symbol-function 'teams4e-availability--request) |
| 65 | + (lambda () (setq requested t)))) |
| 66 | + (teams4e-availability-previous-day) |
| 67 | + (should requested) |
| 68 | + (should (equal teams4e-availability--day |
| 69 | + (car teams4e-availability--window))))))) |
| 70 | + |
| 71 | +(ert-deftest teams4e-availability-rankings-have-different-stable-orders () |
| 72 | + (teams4e-test-with-availability |
| 73 | + (teams4e-availability-show-proximity) |
| 74 | + (let* ((slots (teams4e-availability--suggestions)) |
| 75 | + (first (teams4e--get (car slots) 'meetingTimeSlot)) |
| 76 | + (original (teams4e-availability--original-time))) |
| 77 | + (should (equal original (teams4e-availability--time first 'start))) |
| 78 | + (let ((previous -1)) |
| 79 | + (dolist (entry slots) |
| 80 | + (let ((distance |
| 81 | + (abs (float-time |
| 82 | + (time-subtract |
| 83 | + (teams4e-availability--time |
| 84 | + (teams4e--get entry 'meetingTimeSlot) 'start) |
| 85 | + original))))) |
| 86 | + (should (>= distance previous)) |
| 87 | + (setq previous distance))))) |
| 88 | + (teams4e-availability-show-suggestions) |
| 89 | + (let ((previous 101)) |
| 90 | + (dolist (entry (teams4e-availability--suggestions)) |
| 91 | + (let ((score (teams4e--get entry 'confidence))) |
| 92 | + (should (<= score previous)) |
| 93 | + (setq previous score)))) |
| 94 | + (should (string-match-p "09:00 - 09:45" (buffer-string))) |
| 95 | + (teams4e-availability-show-timeline) |
| 96 | + (should (= 48 (length teams4e-availability--row-ids))))) |
| 97 | + |
| 98 | +(ert-deftest teams4e-availability-checks-whole-slot-and-missing-data () |
| 99 | + (teams4e-test-with-availability |
| 100 | + (teams4e-availability-next-day) |
| 101 | + (let* ((participant (cadr (teams4e-availability--participants))) |
| 102 | + (slot (teams4e--get |
| 103 | + (nth 19 (teams4e-availability--suggestions)) 'meetingTimeSlot)) |
| 104 | + (schedule (teams4e-availability--schedule participant))) |
| 105 | + (nconc schedule '((error) (availabilityView))) |
| 106 | + ;; 09:30-10:15 overlaps the 10:00 busy block. |
| 107 | + (should (equal "busy" (teams4e-availability--slot-status participant slot))) |
| 108 | + (setf (alist-get 'error schedule) '((message . "Denied"))) |
| 109 | + (should (equal "unknown" (teams4e-availability--slot-status participant slot))) |
| 110 | + (setf (alist-get 'error schedule) nil |
| 111 | + (alist-get 'scheduleItems schedule) nil |
| 112 | + (alist-get 'availabilityView schedule) (make-string 144 ?0)) |
| 113 | + (aset (alist-get 'availabilityView schedule) (+ 48 20) ?2) |
| 114 | + (should (equal "busy" (teams4e-availability--slot-status participant slot))) |
| 115 | + (setf (alist-get 'availabilityView schedule) "0") |
| 116 | + (should (equal "unknown" (teams4e-availability--slot-status participant slot))) |
| 117 | + (setf (alist-get 'schedules teams4e-availability--payload) nil) |
| 118 | + (should (equal "unknown" (teams4e-availability--slot-status participant slot)))))) |
| 119 | + |
| 120 | +(ert-deftest teams4e-availability-local-days-handle-dst () |
| 121 | + (let ((process-environment (copy-sequence process-environment))) |
| 122 | + (setenv "TZ" "America/New_York") |
| 123 | + (let* ((start (encode-time 0 0 0 8 3 2026)) |
| 124 | + (end (teams4e-availability--midnight start 1))) |
| 125 | + (should (= (* 23 3600) (float-time (time-subtract end start))))) |
| 126 | + (let* ((start (encode-time 0 0 0 1 11 2026)) |
| 127 | + (end (teams4e-availability--midnight start 1))) |
| 128 | + (should (= (* 25 3600) (float-time (time-subtract end start))))))) |
| 129 | + |
| 130 | +(ert-deftest teams4e-availability-proposes-from-each-time-view () |
| 131 | + (teams4e-test-with-availability |
| 132 | + (dolist (view '(timeline availability proximity)) |
| 133 | + (teams4e-availability-set-view view) |
| 134 | + (let (sent) |
| 135 | + (cl-letf (((symbol-function 'teams4e--proposal-send) |
| 136 | + (lambda (_chat _event-id slot &rest _) |
| 137 | + (setq sent slot)))) |
| 138 | + (teams4e-availability-choose)) |
| 139 | + (should sent) |
| 140 | + (should (= 2700 (float-time |
| 141 | + (time-subtract |
| 142 | + (teams4e-availability--time sent 'end) |
| 143 | + (teams4e-availability--time sent 'start))))))) |
| 144 | + (teams4e-availability-show-blocks) |
| 145 | + (should-error (teams4e-availability-choose) :type 'user-error))) |
| 146 | + |
| 147 | +(provide 'teams4e-availability-tests) |
| 148 | +;;; teams4e-availability-tests.el ends here |
0 commit comments