repo_conversion_test.cljs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. (ns frontend.handler.repo-conversion-test
  2. "Repo tests of directory conversion"
  3. (:require [cljs.test :refer [deftest use-fixtures is testing]]
  4. [clojure.string :as string]
  5. [logseq.graph-parser.cli :as gp-cli]
  6. [logseq.graph-parser.util :as gp-util]
  7. [logseq.graph-parser.test.docs-graph-helper :as docs-graph-helper]
  8. [logseq.graph-parser.config :as gp-config]
  9. [frontend.test.helper :as test-helper]
  10. [frontend.handler.page :as page-handler]
  11. [frontend.handler.conversion :as conversion-handler]
  12. [frontend.handler.repo :as repo-handler]
  13. [frontend.db.conn :as conn]
  14. [datascript.core :as d]))
  15. (use-fixtures :each {:before test-helper/start-test-db!
  16. :after test-helper/destroy-test-db!})
  17. (defn- query-assertions-v067
  18. [db graph-dir files]
  19. (testing "Query based stats"
  20. (is (= (->> files
  21. ;; logseq files aren't saved under :block/file
  22. (remove #(string/includes? % (str graph-dir "/" gp-config/app-name "/")))
  23. set)
  24. (->> (d/q '[:find (pull ?b [* {:block/file [:file/path]}])
  25. :where [?b :block/name] [?b :block/file]]
  26. db)
  27. (map (comp #(get-in % [:block/file :file/path]) first))
  28. set))
  29. "Files on disk should equal ones in db")
  30. (is (= (count (filter #(re-find #"journals/" %) files))
  31. (->> (d/q '[:find (count ?b)
  32. :where
  33. [?b :block/journal? true]
  34. [?b :block/name]
  35. [?b :block/file]]
  36. db)
  37. ffirst))
  38. "Journal page count on disk equals count in db")
  39. (is (= {"CANCELED" 2 "DONE" 6 "LATER" 4 "NOW" 5}
  40. (->> (d/q '[:find (pull ?b [*]) :where [?b :block/marker]]
  41. db)
  42. (map first)
  43. (group-by :block/marker)
  44. (map (fn [[k v]] [k (count v)]))
  45. (into {})))
  46. "Task marker counts")
  47. (is (= {:markdown 3141 :org 460}
  48. (docs-graph-helper/get-block-format-counts db))
  49. "Block format counts")
  50. (is (= {:title 98 :id 98
  51. :updated-at 47 :created-at 47
  52. :card-last-score 6 :card-repeats 6 :card-next-schedule 6
  53. :card-last-interval 6 :card-ease-factor 6 :card-last-reviewed 6
  54. :alias 6 :logseq.macro-arguments 94 :logseq.macro-name 94 :heading 64}
  55. (docs-graph-helper/get-top-block-properties db))
  56. "Counts for top block properties")
  57. (is (= {:title 98
  58. :alias 6
  59. :tags 3 :permalink 2
  60. :name 1 :type 1 :related 1 :sample 1 :click 1 :id 1 :example 1}
  61. (docs-graph-helper/get-all-page-properties db))
  62. "Counts for all page properties")
  63. (is (= {:block/scheduled 2
  64. :block/priority 4
  65. :block/deadline 1
  66. :block/collapsed? 22
  67. :block/repeated? 1}
  68. (->> [:block/scheduled :block/priority :block/deadline :block/collapsed?
  69. :block/repeated?]
  70. (map (fn [attr]
  71. [attr
  72. (ffirst (d/q [:find (list 'count '?b) :where ['?b attr]]
  73. db))]))
  74. (into {})))
  75. "Counts for blocks with common block attributes")
  76. (is (= #{"term" "setting" "book" "templates" "Query" "Query/table" "page"}
  77. (->> (d/q '[:find (pull ?n [*]) :where [?b :block/namespace ?n]] db)
  78. (map (comp :block/original-name first))
  79. set))
  80. "Has correct namespaces")))
  81. (defn docs-graph-assertions-v067
  82. "These are common assertions that should pass in both graph-parser and main
  83. logseq app. It is important to run these in both contexts to ensure that the
  84. functionality in frontend.handler.repo and logseq.graph-parser remain the
  85. same"
  86. [db graph-dir files]
  87. ;; Counts assertions help check for no major regressions. These counts should
  88. ;; only increase over time as the docs graph rarely has deletions
  89. (testing "Counts"
  90. (is (= 211 (count files)) "Correct file count")
  91. (is (= 42200 (count (d/datoms db :eavt))) "Correct datoms count")
  92. (is (= 3600
  93. (ffirst
  94. (d/q '[:find (count ?b)
  95. :where [?b :block/path-refs ?bp] [?bp :block/name]] db)))
  96. "Correct referenced blocks count")
  97. (is (= 21
  98. (ffirst
  99. (d/q '[:find (count ?b)
  100. :where [?b :block/content ?content]
  101. [(clojure.string/includes? ?content "+BEGIN_QUERY")]]
  102. db)))
  103. "Advanced query count"))
  104. (query-assertions-v067 db graph-dir files))
  105. (defn- convert-to-triple-lowbar
  106. [path]
  107. (let [original-body (gp-util/path->file-body path)
  108. ;; only test file name parsing, don't consider title prop overriding
  109. rename-target (:target (#'conversion-handler/calc-rename-target-impl :legacy :triple-lowbar original-body nil))]
  110. (if rename-target
  111. #_:clj-kondo/ignore
  112. (do #_(prn "conversion triple-lowbar: " original-body " -> " rename-target)
  113. (#'page-handler/compute-new-file-path path rename-target))
  114. path)))
  115. (defn- convert-graph-files-path
  116. "Given a list of files, converts them according to the given conversion function"
  117. [files conversion-fn]
  118. (map (fn [file]
  119. (assoc file :file/path (conversion-fn (:file/path file)))) files))
  120. ;; Integration test that test parsing a large graph like docs
  121. ;; Check if file name conversion from old version of docs is working
  122. (deftest ^:integration convert-v067-filenames-parse-and-load-files-to-db
  123. (let [graph-dir "src/test/docs"
  124. _ (docs-graph-helper/clone-docs-repo-if-not-exists graph-dir "v0.6.7")
  125. files (#'gp-cli/build-graph-files graph-dir {})
  126. ;; Converting the v0.6.7 ver docs graph under the old namespace naming rule to the new one (:repo/dir-version 0->3)
  127. files (convert-graph-files-path files convert-to-triple-lowbar)
  128. _ (repo-handler/parse-files-and-load-to-db! test-helper/test-db files {:re-render? false :verbose false})
  129. db (conn/get-db test-helper/test-db)]
  130. ;; Result under new naming rule after conversion should be the same as the old one
  131. (docs-graph-assertions-v067 db graph-dir (map :file/path files))))