Advertisement
Guest User

Untitled

a guest
Sep 22nd, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. (ns moo.test.handlers.room
  2.   (:require [moo.handlers.core :refer :all]
  3.             [moo.handlers.room]
  4.             [clojure.test :refer :all]))
  5.  
  6. (deftest client/connect
  7.   (testing "Should create a new, synced client and create new user when no user exists "
  8.     (let [room-id "test-room-id"
  9.           uid "test-uid"
  10.           room-state {:r room-id}
  11.           session-id "test-session-id"
  12.           actions (-event-msg-handler {:id :client/connect
  13.                                        :room-id room-id
  14.                                        :uid uid
  15.                                        :session-id session-id
  16.                                        :database-fns {:get-room-state-fn (fn [id] {:r id})}})]
  17.       (is (= [:update-client [room-id {:id uid :synced? true}]] (nth actions 0)))
  18.       (is (= [:create-user {:session-id session-id :clients #{uid}}] (nth actions 1)))
  19.       (is (= [:send-to-client [uid [:room/state-update room-state]]] (nth actions 2)))))
  20.  
  21.   (testing "Should create a new, synced client and add new client id to user"
  22.     (let [room-id "test-room-id"
  23.           user-id "test-user-id"
  24.           old-uid "old-test-uid"
  25.           new-uid "new-test-uid"
  26.           room-state {:r room-id}
  27.           session-id "test-session-id"
  28.           actions (-event-msg-handler {:id :client/connect
  29.                                        :room-id room-id
  30.                                        :uid new-uid
  31.                                        :session-id session-id
  32.                                        :user {:id user-id :clients #{old-uid} :session session-id}
  33.                                        :database-fns {:get-room-state-fn (fn [id] {:r id})}})]
  34.       (is (= [:update-client [room-id {:id new-uid :synced? true}]] (nth actions 0)))
  35.       (is (= [:update-user {:session-id session-id :clients #{new-uid old-uid}}] (nth actions 1)))
  36.       (is (= [:send-to-client [new-uid [:room/state-update room-state]]] (nth actions 2))))))
  37.  
  38. (deftest client/disconnect
  39.   (testing "Should delete the client and remove it from user"
  40.     (let [client-id "test-client-id"
  41.           other-client-id "test-other-client-id"
  42.           room-id "test-room-id"
  43.           user-id "test-user-id"
  44.           actions (-event-msg-handler {:id :client/disconnect
  45.                                        :client {:id client-id}
  46.                                        :user {:id user-id :clients #{client-id other-client-id}}
  47.                                        :room-id room-id})]
  48.       (is (= [:delete-client [room-id client-id]] (nth actions 0)))
  49.       (is (= [:update-user {:id user-id :clients #{other-client-id}}] (nth actions 1)))))
  50.  
  51.   (testing "Should delete the client and delete user if it was the user's last client"
  52.     (let [client-id "test-client-id"
  53.           room-id "test-room-id"
  54.           user-id "test-user-id"
  55.           actions (-event-msg-handler {:id :client/disconnect
  56.                                        :client {:id client-id}
  57.                                        :user {:id user-id :clients #{client-id}}
  58.                                        :room-id room-id})]
  59.       (is (= [:delete-client [room-id client-id]] (nth actions 0)))
  60.       (is (= [:remove-user user-id] (nth actions 1))))))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement