test: add a fixture to persist tx-data-log

save the whole transact history to ease debugging
pull/11293/head
rcmerci 2024-04-25 12:39:29 +08:00
parent 8f0d327d53
commit fcb3097602
2 changed files with 26 additions and 18 deletions

View File

@ -1,23 +1,11 @@
(ns frontend.worker.fixtures
(:require [datascript.core :as d]
(:require ["fs" :as fs-node]
[datascript.core :as d]
[frontend.db.conn :as conn]
[frontend.test.helper :as test-helper]
[frontend.worker.db-listener :as worker-db-listener]
[frontend.worker.undo-redo :as worker-undo-redo]))
(defn listen-test-db-to-gen-undo-ops-fixture
[f]
(let [test-db-conn (conn/get-db test-helper/test-db-name-db-version false)]
(assert (some? test-db-conn))
(worker-undo-redo/clear-undo-redo-stack)
(worker-db-listener/listen-db-changes! test-helper/test-db-name-db-version test-db-conn
{:handler-keys [:gen-undo-ops
;; :sync-db-to-main-thread
]})
(f)
(d/unlisten! test-db-conn :frontend.worker.db-listener/listen-db-changes!)))
[frontend.worker.undo-redo :as worker-undo-redo]
[logseq.db.sqlite.util :as sqlite-util]))
(defn listen-test-db-fixture
@ -31,3 +19,22 @@
(f)
(d/unlisten! test-db-conn :frontend.worker.db-listener/listen-db-changes!))))
(def ^:private *tx-log-name-index (atom 0))
(defn listen-test-db-to-write-tx-log-json-file
"Write {:tx-log <tx-data-coll> :init-db <init-db>} to file 'tx-log-<index>.json'"
[f]
(let [test-db-conn (conn/get-db test-helper/test-db-name-db-version false)
init-db @test-db-conn
*tx-log (atom [])]
(d/listen! test-db-conn :collect-tx-data
(fn [{:keys [tx-data]}]
(swap! *tx-log conj tx-data)))
(try
(f)
(finally
(let [file-name (str "tx-log-" @*tx-log-name-index ".json")]
(println "saving " file-name " ...")
(fs-node/writeFileSync file-name (sqlite-util/write-transit-str {:tx-log @*tx-log :init-db init-db}))
(swap! *tx-log-name-index inc))))
(d/unlisten! test-db-conn :collect-tx-data)))

View File

@ -27,7 +27,8 @@
(use-fixtures :each
start-and-destroy-db
worker-fixtures/listen-test-db-to-gen-undo-ops-fixture)
(worker-fixtures/listen-test-db-fixture [:gen-undo-ops])
worker-fixtures/listen-test-db-to-write-tx-log-json-file)
(def ^:private gen-non-exist-block-uuid gen/uuid)
@ -118,7 +119,7 @@
:frontend.worker.undo-redo/move-block
(assert (= (:block-origin-left (second op))
(:block/uuid (:block/left (d/entity current-db [:block/uuid (:block-uuid (second op))]))))
{:op op :tx-data (:tx-data tx) :x (keys tx)})
{:op op :entity (into {} (d/entity current-db [:block/uuid (:block-uuid (second op))]))})
:frontend.worker.undo-redo/update-block
(assert (some? (d/entity current-db [:block/uuid (:block-uuid (second op))]))