fixtures.cljs 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. (ns frontend.worker.fixtures
  2. (:require ["fs" :as fs-node]
  3. [datascript.core :as d]
  4. [frontend.db.conn :as conn]
  5. [frontend.test.helper :as test-helper]
  6. [frontend.worker.db-listener :as worker-db-listener]
  7. [frontend.worker.undo-redo :as worker-undo-redo]
  8. [logseq.db.sqlite.util :as sqlite-util]))
  9. (defn listen-test-db-fixture
  10. [handler-keys]
  11. (fn [f]
  12. (let [test-db-conn (conn/get-db test-helper/test-db-name-db-version false)]
  13. (assert (some? test-db-conn))
  14. (worker-undo-redo/clear-undo-redo-stack)
  15. (worker-db-listener/listen-db-changes! test-helper/test-db-name-db-version test-db-conn
  16. {:handler-keys handler-keys})
  17. (f)
  18. (d/unlisten! test-db-conn :frontend.worker.db-listener/listen-db-changes!))))
  19. (def ^:private *tx-log-name-index (atom 0))
  20. (defn listen-test-db-to-write-tx-log-json-file
  21. "Write {:tx-log <tx-data-coll> :init-db <init-db>} to file 'tx-log-<index>.json'"
  22. [f]
  23. (let [test-db-conn (conn/get-db test-helper/test-db-name-db-version false)
  24. init-db @test-db-conn
  25. *tx-log (atom [])]
  26. (d/listen! test-db-conn :collect-tx-data
  27. (fn [{:keys [tx-data]}]
  28. (swap! *tx-log conj tx-data)))
  29. (try
  30. (f)
  31. (finally
  32. (let [file-name (str "tx-log-" @*tx-log-name-index ".json")]
  33. (println "saving " file-name " ...")
  34. (fs-node/writeFileSync file-name (sqlite-util/write-transit-str {:tx-log @*tx-log :init-db init-db}))
  35. (swap! *tx-log-name-index inc))))
  36. (d/unlisten! test-db-conn :collect-tx-data)))