             [buildviz.util.url :as url]
             [cheshire.core :as j]
             [clj-http.fake :as fake]
-            [clj-time.core :as t]
+            [clj-time
+             [coerce :as tc]
+             [core :as t]]
             [clojure.test :refer :all]))
 
 (defn- successful-json-response [body]

(defn- a-job-with-builds [job-name & builds]
  (let [job-builds [(format "http://jenkins:4321/job/%s/api/json?tree=allBuilds%%5Bnumber,timestamp,duration,result,actions%%5BlastBuiltRevision%%5BSHA1%%5D,remoteUrls,parameters%%5Bname,value%%5D,causes%%5BupstreamProject,upstreamBuild%%5D%%5D%%5D%%7B0,10%%7D"
                            job-name)
                    (successful-json-response {:allBuilds []})]]
    [job-builds]))


(defn- serve-up [& routes]
  (->> routes

                                                    (a-job-with-builds "some_job"))
        (with-out-str (sut/sync-jobs (url/url "http://jenkins:4321") (url/url "http://buildviz:8010") beginning-of-2016)))
      (is (= []
             @store))))

  (testing "should sync a simple build"
    (let [store (atom [])]
      (fake/with-fake-routes-in-isolation (serve-up (a-view (a-job "some_job"))
                                                    (a-job-with-builds "some_job" {:number "21"
                                                                                   :timestamp 1493201298062
                                                                                   :duration 10200
                                                                                   :result "SUCCESS"})
                                                    (provide-buildviz-and-capture-puts beginning-of-2016 store))
        (with-out-str (sut/sync-jobs (url/url "http://jenkins:4321") (url/url "http://buildviz:8010") beginning-of-2016)))
      (is (= [["/builds/some_job/21" {:start 1493201298062
                                      :end 1493201308262
                                      :outcome "pass"}]]
             @store)))))
