~severeoverfl0w/wedge-template

208a84702c470ab279be64062974375593b53e86 — Dominic Monroe 5 months ago 36070a9
Add tests
3 files changed, 134 insertions(+), 1 deletions(-)

M .gitignore
M deps.edn
A test/io/dominic/wedge/template_test.clj
M .gitignore => .gitignore +1 -0
@@ 1,3 1,4 @@
.cpcache
/*.jar
/test.template
/loaded.png

M deps.edn => deps.edn +7 -1
@@ 4,7 4,13 @@
        io.dominic/wedge {:mvn/version "0.6.0"}}
 :aliases
 {:test {:extra-paths ["test"]
         :extra-deps {org.clojure/test.check {:mvn/version "1.0.0"}}}
         :extra-deps {etaoin/etaoin {:mvn/version "0.4.6"}
                      io.github.clojure/tools.build
                      {:git/tag "v0.6.8"
                       :git/sha "d79ae84"
                       :git/url "https://github.com/clojure/tools.build.git"}
                      babashka/process {:git/url "https://github.com/babashka/process.git"
                                        :git/sha "eb035138aacf94f0bd635f650dbc639b0a4a7328"}}}
  :runner {:extra-deps {com.cognitect/test-runner
                        {:git/url "https://github.com/cognitect-labs/test-runner"
                         :git/tag "v0.5.0" :git/sha "48c3c67"}}

A test/io/dominic/wedge/template_test.clj => test/io/dominic/wedge/template_test.clj +126 -0
@@ 0,0 1,126 @@
(ns io.dominic.wedge.template-test
  (:require
    [babashka.process :as bb.process]
    [clj-new]
    [clojure.java.io :as io]
    [clojure.test :refer [deftest is use-fixtures testing]]
    [clojure.tools.build.api :as b]
    [etaoin.api :as etaoin]))

(def template-dir "test.template")

(use-fixtures
  :once
  (fn [f]
    (b/delete {:path template-dir})
    (with-redefs [shutdown-agents (fn [& args])]
      (clj-new/create
        {:template ".::io.dominic.wedge"
         :name "io.dominic/test.template"
         :output template-dir}))
    (f)))

(comment
  (binding [b/*project-root* template-dir]
    (let [basis (b/create-basis {:aliases [:dev]})
          cmd (b/java-command
                {:basis basis
                 :main 'clojure.main
                 :main-args ["-r"]})]
      cmd))
  )

(defn exception? [preplmsg]
  (:exception preplmsg))

(def success? (complement exception?))

(defn- read-with-timeout
  [opts reader timeout-ms timeout-val]
  (let [obj* (future (read opts reader))
        obj (deref obj* timeout-ms ::timeout)]
    (if (= ::timeout obj)
      (do (future-cancel obj*) timeout-val)
      obj)))

(deftest full-test
  (let [basis (binding [b/*project-root* template-dir]
                (b/create-basis {:aliases [:dev]}))
        cmd (b/java-command
              {:basis basis
               :java-opts ["-Dclojure.server.jvm={:port 5555 :accept clojure.core.server/io-prepl}"]
               :main 'clojure.main
               ;; just to block.
               :main-args ["-r"]})
        process #_(-> (ProcessBuilder. (:command-args cmd))
                    (.directory (io/file template-dir))
                    (.redirectOutput java.lang.ProcessBuilder$Redirect/PIPE)
                    (.redirectError java.lang.ProcessBuilder$Redirect/PIPE)
                    (.start))
        (bb.process/process (:command-args cmd) {:dir template-dir})]
    (try
      (with-open [socket (loop [remaining 60]
                           (if-let [socket (try
                                             (java.net.Socket. "127.0.0.1" 5555)
                                             (catch java.io.IOException e
                                               (when (= 0 remaining)
                                                 (throw e))))]
                             socket
                             (do
                               (Thread/sleep 1000)
                               (recur (dec remaining)))))]

        (let [writer (io/writer socket)
              reader (java.io.PushbackReader. (io/reader socket))]
          (testing "After (dev), ns is 'dev"
            (binding [*out* writer]
              (println "(dev)"))
            (let [ret (loop []
                        (if-let [msg (read-with-timeout {} reader 30000 nil)]
                          (if (= :ret (:tag msg))
                            msg
                            (recur))
                          (throw (ex-info "Timeout reading from pREPL" {}))))]
              (is (= "dev" (:ns ret)))))

          (testing "After (dev), can (go)"
            (binding [*out* writer]
              (println "(go)"))
            (let [ret (loop []
                        (if-let [msg (read-with-timeout {} reader 120000 nil)]
                          (if (= :ret (:tag msg))
                            msg
                            (recur))
                          (throw (ex-info "Timeout reading from pREPL" {}))))]
              (is ret)
              (is (success? ret))))

          (testing "After (go) can open localhost:3000"
            (etaoin/with-firefox-headless {:size [1024 680]
                                           :url "http://localhost:3000"} fx
              (try (etaoin/wait-exists
                     fx
                     {:css "#app > *"}
                     {:timeout 120})
                   (finally (etaoin/screenshot fx "loaded.png")))
              (is true "Page loaded")))

          (testing "After (go), can (stop)"
            (binding [*out* writer]
              (println "(stop)"))
            (let [ret (loop []
                        (if-let [msg (read-with-timeout {} reader 30000 nil)]
                          (if (= :ret (:tag msg))
                            msg
                            (recur))
                          (throw (ex-info "Timeout reading from pREPL" {}))))]
              (is ret)
              (is (success? ret))))))
      (finally
        (.close (:in process))

        (or (deref process 5000 nil)
            (bb.process/destroy process))

        (when (deref process 5000 nil)
          (is (bb.process/check process)))))))