<commit_before>(ns clstreams.webapi.component-test
  (:require [aleph.http :as aleph]
            [clojure.test :refer :all]
            [clstreams.testutil.spies :refer [spy with-spy-redefs calls]]
            [clstreams.webapi.component :refer :all]
            [com.stuartsierra.component :as component]))

(def app-factory nil)

(deftest test-aleph-component
  (testing "Component starts and stops server"
    (let [handler (fn [& x] nil)
          [stop-server stop-server-spy] (spy)
          server-dummy (reify java.io.Closeable
                         (close [this] (stop-server this)))
          [app-factory app-factory-spy] (spy handler)]
      (with-spy-redefs
        [[aleph/start-server start-server-spy] (spy server-dummy)]

        (let [aleph (new-aleph app-factory {:host "0.1.2.3" :port 1234})]
          (is (= (:server aleph) nil))
          (is (= (-> start-server-spy calls count) 0))
          (is (= (-> stop-server-spy calls count) 0))

          (let [started-aleph (component/start aleph)]
            (is (= (:server started-aleph) server-dummy))
            (is (= (-> start-server-spy calls count) 1))
            (is (= (-> stop-server-spy calls count) 0))
            (is (= (-> app-factory-spy calls count) 1))
            (is (= (-> app-factory-spy calls first) [aleph]))

            (let [stopped-aleph (component/stop started-aleph)]
              (is (= (:stop-server stopped-aleph) nil)))))

        (is (= (-> start-server-spy calls count) 1))
        (is (= (-> start-server-spy calls first) [handler {:host "0.1.2.3" :port 1234}]))
        (is (= (-> stop-server-spy calls count) 1))
        (is (= (-> stop-server-spy calls first) [server-dummy]))))))

(comment deftest test-immutant-component
  (testing "Component starts and stops server"
    (let [handler (fn [& x] nil)
          server-dummy {:server "ze-server"}
          [app-factory app-factory-spy] (spy handler)]
      (with-spy-redefs
        [[web/run web-run-spy] (spy server-dummy)
         [web/stop web-stop-spy] (spy)]

        (let [im (new-immutant app-factory (sorted-map :host "0.1.2.3" :port 1234))]
          (is (= (:server im) nil))
          (is (= (-> web-run-spy calls count) 0))
          (is (= (-> web-stop-spy calls count) 0))

          (let [started-im (component/start im)]
            (is (= (:server started-im) server-dummy))
            (is (= (-> web-run-spy calls count) 1))
            (is (= (-> web-stop-spy calls count) 0))
            (is (= (-> app-factory-spy calls count) 1))
            (is (= (-> app-factory-spy calls first) [im]))

            (let [stopped-im (component/stop started-im)]
              (is (= (:server stopped-im) nil)))))

        (is (= (-> web-run-spy calls count) 1))
        (is (= (-> web-run-spy calls first) [handler :host "0.1.2.3":port 1234]))
        (is (= (-> web-stop-spy calls count) 1))
        (is (= (-> web-stop-spy calls first) [server-dummy]))))))<commit_msg>Convert web component test to using Mockito<commit_after>(ns clstreams.webapi.component-test
  (:require [aleph.http :as aleph]
            [clojure.test :refer :all]
            [clstreams.testutil.component :as tu-comp]
            [clstreams.testutil.mockito :as mkto]
            [clstreams.webapi.component :refer :all])
  (:import org.mockito.Mockito))

(def app-factory nil)

(deftest test-aleph-component
  (testing "Component starts and stops server"
    (let [config {:host "0.1.2.3" :port 1234}

          handler (mkto/mock-fn)
          app-factory (mkto/on-call (mkto/mock-fn) [(Mockito/any)] handler)

          server-mock (mkto/mock java.io.Closeable)
          start-server-fn (mkto/on-call (mkto/mock-fn) [handler config] server-mock)]

      (with-redefs [aleph/start-server start-server-fn]

        (tu-comp/cycle-component

         [aleph (new-aleph app-factory config)]
         ((is (= (:server aleph) nil))
          (mkto/verify-fn start-server-fn [handler config] (mkto/never))
          (mkto/verify-> server-mock (.close) (mkto/never)))

         [started-aleph]
         ((is (= (:server started-aleph) server-mock))
          (mkto/verify-fn start-server-fn [handler config])
          (mkto/verify-> server-mock (.close) (mkto/never))
          (mkto/verify-fn app-factory [aleph]))

         [stopped-aleph]
         ((is (= (:stop-server stopped-aleph) nil))
          (mkto/verify-fn start-server-fn [handler config])
          (mkto/verify-> server-mock (.close))))))))

{'fraction_non_alphanumeric': 0.16984518734574827, 'fraction_numerical': 0.012115772941440431, 'mean_word_length': 2.186561829878485, 'pattern_counts': {'":': 1, '<': 3, '<?xml version=': 0, '>': 26, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 5, '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': '5210898', 'n_tokens_mistral': 1578, 'n_tokens_neox': 1483, 'n_words': 355}