<commit_before>(ns org.zproto.hydra-server-test
  (:require [clojure.test :refer :all]
            [org.zproto.hydra-server :as server]
            [org.zproto.hydra-msg :as msg])
  (:import [org.zproto HydraMsg]))

(def test-endpoint "inproc://hydra-server-test")

(def dummy-backend
  (reify server/HydraServerBackend
    (get-latest-post [this]
      "dummy-post-id")
    (get-single-post [this post-id]
      (when (= "dummy-post-id" post-id)
        ["dummy-post-id" "none" "none" "tag_2" 123123123 "dummy" "dummy-post"]))
    (get-all-tags [this]
      "tag_1 tag_2 tag_3 tag_4")
    (get-single-tag [this tag-id]
      (when (= tag-id "tag_1")
        [tag-id "dummy-post-id-2"]))))

(defn setup [& [connect?]]
  (let [srv-sock (msg/server-socket test-endpoint)
        cl-sock (msg/client-socket test-endpoint)
        srv (server/->Server srv-sock (atom {}) dummy-backend)]
    (when connect?
      (msg/hello cl-sock)
      (server/match-msg srv (msg/recv srv-sock))
      (msg/recv cl-sock))
    [cl-sock srv-sock srv]))

(defn teardown [cl-sock srv-sock]
  (.close (:socket srv-sock))
  (.close (:socket cl-sock)))

(defn server-client-comm [cl-sock srv srv-sock cl-msg & args]
  (apply cl-msg cl-sock args)
  (server/match-msg srv (msg/recv srv-sock))
  (msg/recv cl-sock))

(deftest test-hello-ok
  (let [[cl-sock srv-sock srv] (setup)]
    (try
      (let [response (server-client-comm cl-sock srv srv-sock msg/hello)]
        (is (= HydraMsg/HELLO_OK
               (.id response)))
        (is (= (server/get-latest-post dummy-backend)
               (.post_id response)))
        (is (= :connected
               (-> @(:state srv) vals first))))
      (finally
        (teardown cl-sock srv-sock)))))

(deftest test-get-tags-ok
  (let [[cl-sock srv-sock srv] (setup :connected)]
    (try
      (let [response (server-client-comm cl-sock srv srv-sock msg/get-tags)]
        (is (= HydraMsg/GET_TAGS_OK
               (.id response)))
        (is (= (server/get-all-tags dummy-backend)
               (.tags response)))
        (is (= :connected
               (-> @(:state srv) vals first))))
      (finally
        (teardown cl-sock srv-sock)))))

(deftest test-get-tag-ok
  (let [[cl-sock srv-sock srv] (setup :connected)]
    (try
      (let [response (server-client-comm cl-sock srv srv-sock msg/get-tag "tag_1")]
        (is (= HydraMsg/GET_TAG_OK
               (.id response)))
        (is (= (server/get-single-tag dummy-backend "tag_1")
               [(.tag response) (.post_id response)]))
        (is (= :connected
               (-> @(:state srv) vals first))))
      (finally
        (teardown cl-sock srv-sock)))))

(deftest test-get-post-ok
  (let [[cl-sock srv-sock srv] (setup :connected)]
    (try
      (let [response (server-client-comm cl-sock srv srv-sock msg/get-post "dummy-post-id")]
        (is (= HydraMsg/GET_POST_OK
               (.id response)))
        (is (= (server/get-single-post dummy-backend "dummy-post-id")
               [(.post_id response)
                (.reply_to response)
                (.previous response)
                (.tags response)
                (.timestamp response)
                (.type response)
                (.content response)]))
        (is (= :connected
               (-> @(:state srv) vals first))))
      (finally
        (teardown cl-sock srv-sock)))))

(deftest test-invalid
  (let [[cl-sock srv-sock srv] (setup)]
    (try
      (let [response (server-client-comm cl-sock srv srv-sock msg/goodbye)]
        (is (= HydraMsg/INVALID
               (.id response)))
        (is (empty? @(:state srv))))
      (finally
        (teardown cl-sock srv-sock)))))

(deftest test-goodbye
  (let [[cl-sock srv-sock srv] (setup :connected)]
    (try<commit_msg>update clojure tests to work with current zproto.<commit_after>(ns org.zproto.hydra-server-test
  (:require [clojure.test :refer :all]
            [org.zproto.hydra-server :as server]
            [org.zproto.hydra-msg :as msg])
  (:import [org.zproto HydraMsg]))

(def test-endpoint "inproc://hydra-server-test")

(def dummy-backend
  (reify server/HydraServerBackend
    (get-latest-post [this m]
      (msg/post-id! m "dummy-post-id"))
    (get-single-post [this m post-id]
      (msg/reply-to! m "none")
      (msg/previous! m "none")
      (msg/tags! m "tag_2")
      (msg/timestamp! m 123123123)
      (msg/type! m "dummy")
      (msg/content! m "dummy-post"))
    (get-all-tags [this m]
      (msg/tags! m "tag_1 tag_2 tag_3 tag_4"))
    (get-single-tag [this m tag-id]
      (msg/post-id! m "dummy-post-id-2"))))

(defn setup [& [connect?]]
  (let [srv-sock (msg/server-socket test-endpoint)
        cl-sock (msg/client-socket test-endpoint)
        srv (server/->Server srv-sock (atom {}) dummy-backend)]
    (when connect?
      (msg/hello cl-sock)
      (server/match-msg srv (msg/recv srv-sock))
      (msg/recv cl-sock))
    [cl-sock srv-sock srv]))

(defn teardown [cl-sock srv-sock]
  (.close (:socket srv-sock))
  (.close (:socket cl-sock)))

(defn blank-msg []
  (HydraMsg. 0))

(defn server-client-comm [cl-sock srv srv-sock cl-msg & args]
  (apply cl-msg cl-sock args)
  (server/match-msg srv (msg/recv srv-sock))
  (msg/recv cl-sock))

(deftest test-hello-ok
  (let [[cl-sock srv-sock srv] (setup)]
    (try
      (let [response (server-client-comm cl-sock srv srv-sock msg/hello)
            expected (blank-msg)]
        (server/get-latest-post dummy-backend expected)
        (is (= HydraMsg/HELLO_OK
               (.id response)))
        (is (= (.post_id expected)
               (.post_id response)))
        (is (= :connected
               (-> @(:state srv) vals first))))
      (finally
        (teardown cl-sock srv-sock)))))

(deftest test-get-tags-ok
  (let [[cl-sock srv-sock srv] (setup :connected)]
    (try
      (let [response (server-client-comm cl-sock srv srv-sock msg/get-tags)
            expected (blank-msg)]
        (server/get-all-tags dummy-backend expected)
        (is (= HydraMsg/GET_TAGS_OK
               (.id response)))
        (is (= (.tags expected)
               (.tags response)))
        (is (= :connected
               (-> @(:state srv) vals first))))
      (finally
        (teardown cl-sock srv-sock)))))

(deftest test-get-tag-ok
  (let [[cl-sock srv-sock srv] (setup :connected)]
    (try
      (let [tag "tag_1"
            response (server-client-comm cl-sock srv srv-sock msg/get-tag tag)
            expected (blank-msg)]
        (server/get-single-tag dummy-backend expected tag)
        (is (= HydraMsg/GET_TAG_OK
               (.id response)))
        (is (= tag
               (.tag response)))
        (is (= (.post_id expected)
               (.post_id response)))
        (is (= :connected
               (-> @(:state srv) vals first))))
      (finally
        (teardown cl-sock srv-sock)))))

(deftest test-get-post-ok
  (let [[cl-sock srv-sock srv] (setup :connected)]
    (try
      (let [post-id "dummy-post-id"
            response (server-client-comm cl-sock srv srv-sock msg/get-post post-id)
            expected (blank-msg)]
        (server/get-single-post dummy-backend expected post-id)
        (is (= HydraMsg/GET_POST_OK
               (.id response)))
        (is (= post-id
               (.post_id response)))
        (is (= (.reply_to expected)
               (.reply_to response)))
        (is (= (.previous expected)
               (.previous response)))
        (is (= (.tags expected)
               (.tags response)))
        (is (= (.timestamp expected)
               (.timestamp response)))
        (is (= (.type expected)
               (.type response)))
        (is (= (.content expected)
               (.content response)))
        (is (= :connected
               (-> @(:state srv) vals first))))
      (finally
        (teardown cl-sock srv-sock)))))

(deftest test-invalid
  (let [[cl-sock srv-sock srv] (setup)]
    (try
      (let [response (server-client-comm cl-sock srv srv-sock msg/goodbye)]
        (is (= HydraMsg/INVALID
               (.id response)))
        (is (empty? @(:state srv))))
      (finally
        (teardown cl-sock srv-sock)))))

(deftest test-goodbye
  (let [[cl-sock srv-sock srv] (setup :connected)]
    (try
{'fraction_non_alphanumeric': 0.1619873046875, 'fraction_numerical': 0.0042724609375, 'mean_word_length': 2.0479910714285716, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3308197', 'n_tokens_mistral': 2918, 'n_tokens_neox': 2794, 'n_words': 667}