exporter_test.cljs 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808
  1. (ns ^:node-only logseq.graph-parser.exporter-test
  2. (:require ["fs" :as fs]
  3. ["path" :as node-path]
  4. [cljs.test :refer [are deftest is testing]]
  5. [clojure.set :as set]
  6. [clojure.string :as string]
  7. [datascript.core :as d]
  8. [logseq.common.config :as common-config]
  9. [logseq.common.graph :as common-graph]
  10. [logseq.common.util.date-time :as date-time-util]
  11. [logseq.db :as ldb]
  12. [logseq.db.common.entity-plus :as entity-plus]
  13. [logseq.db.frontend.asset :as db-asset]
  14. [logseq.db.frontend.content :as db-content]
  15. [logseq.db.frontend.malli-schema :as db-malli-schema]
  16. [logseq.db.frontend.rules :as rules]
  17. [logseq.db.frontend.validate :as db-validate]
  18. [logseq.db.test.helper :as db-test]
  19. [logseq.graph-parser.block :as gp-block]
  20. [logseq.graph-parser.exporter :as gp-exporter]
  21. [logseq.graph-parser.test.docs-graph-helper :as docs-graph-helper]
  22. [logseq.graph-parser.test.helper :as test-helper :include-macros true :refer [deftest-async]]
  23. [logseq.outliner.db-pipeline :as db-pipeline]
  24. [promesa.core :as p]))
  25. ;; Helpers
  26. ;; =======
  27. ;; some have been copied from db-import script
  28. (defn- extract-rules
  29. [rules]
  30. (rules/extract-rules rules/db-query-dsl-rules
  31. rules
  32. {:deps rules/rules-dependencies}))
  33. (defn- find-block-by-property [db property]
  34. (d/q '[:find [?b ...]
  35. :in $ ?prop %
  36. :where (has-property ?b ?prop)]
  37. db property (extract-rules [:has-property])))
  38. (defn- find-block-by-property-value [db property property-value]
  39. (->> (d/q '[:find [?b ...]
  40. :in $ ?prop ?prop-value %
  41. :where (property ?b ?prop ?prop-value)]
  42. db property property-value (extract-rules [:property]))
  43. first
  44. (d/entity db)))
  45. (defn- build-graph-files
  46. "Given a file graph directory, return all files including assets and adds relative paths
  47. on ::rpath since paths are absolute by default and exporter needs relative paths for
  48. some operations"
  49. [dir*]
  50. (let [dir (node-path/resolve dir*)]
  51. (->> (common-graph/get-files dir)
  52. (concat (when (fs/existsSync (node-path/join dir* "assets"))
  53. (common-graph/readdir (node-path/join dir* "assets"))))
  54. (mapv #(hash-map :path %
  55. ::rpath (node-path/relative dir* %))))))
  56. (defn- <read-file
  57. [file]
  58. (p/let [s (fs/readFileSync (:path file))]
  59. (str s)))
  60. (defn- notify-user [m]
  61. (println (:msg m))
  62. (when (:ex-data m)
  63. (println "Ex-data:" (pr-str (merge (dissoc (:ex-data m) :error)
  64. (when-let [err (get-in m [:ex-data :error])]
  65. {:original-error (ex-data (.-cause err))}))))
  66. (println "Stacktrace:")
  67. (if-let [stack (some-> (get-in m [:ex-data :error]) ex-data :sci.impl/callstack deref)]
  68. (println (string/join
  69. "\n"
  70. (map
  71. #(str (:file %)
  72. (when (:line %) (str ":" (:line %)))
  73. (when (:sci.impl/f-meta %)
  74. (str " calls #'" (get-in % [:sci.impl/f-meta :ns]) "/" (get-in % [:sci.impl/f-meta :name]))))
  75. (reverse stack))))
  76. (println (some-> (get-in m [:ex-data :error]) .-stack))))
  77. (when (= :error (:level m))
  78. (js/process.exit 1)))
  79. (def default-export-options
  80. {;; common options
  81. :rpath-key ::rpath
  82. :notify-user notify-user
  83. :<read-file <read-file
  84. ;; :set-ui-state prn
  85. ;; config file options
  86. ;; TODO: Add actual default
  87. :default-config {}})
  88. ;; tweaked from db-import
  89. (defn- <read-and-copy-asset [file assets buffer-handler *asset-ids]
  90. (p/let [buffer (fs/readFileSync (:path file))
  91. checksum (db-asset/<get-file-array-buffer-checksum buffer)
  92. asset-id (d/squuid)
  93. asset-name (gp-exporter/asset-path->name (:path file))
  94. asset-type (db-asset/asset-path->type (:path file))
  95. {:keys [with-edn-content pdf-annotation?]} (buffer-handler buffer)]
  96. (when-not pdf-annotation?
  97. (swap! *asset-ids conj asset-id))
  98. (swap! assets assoc asset-name
  99. (with-edn-content
  100. {:size (.-length buffer)
  101. :type asset-type
  102. :path (:path file)
  103. :checksum checksum
  104. :asset-id asset-id}))
  105. buffer))
  106. ;; Copied from db-import script and tweaked for an in-memory import
  107. (defn- import-file-graph-to-db
  108. "Import a file graph dir just like UI does. However, unlike the UI the
  109. exporter receives file maps containing keys :path and ::rpath since :path
  110. are full paths"
  111. [file-graph-dir conn {:keys [assets] :or {assets (atom [])} :as options}]
  112. (let [*files (build-graph-files file-graph-dir)
  113. config-file (first (filter #(string/ends-with? (:path %) "logseq/config.edn") *files))
  114. _ (assert config-file "No 'logseq/config.edn' found for file graph dir")
  115. options' (merge default-export-options
  116. {:user-options (merge {:convert-all-tags? false} (dissoc options :assets :verbose))
  117. ;; asset file options
  118. :<read-and-copy-asset (fn [file *assets buffer-handler]
  119. (<read-and-copy-asset file *assets buffer-handler assets))}
  120. (select-keys options [:verbose]))]
  121. (gp-exporter/export-file-graph conn conn config-file *files options')))
  122. (defn- import-files-to-db
  123. "Import specific doc files for dev purposes"
  124. [files conn options]
  125. (reset! gp-block/*export-to-db-graph? true)
  126. (-> (p/let [doc-options (gp-exporter/build-doc-options (merge {:macros {} :file/name-format :triple-lowbar}
  127. (:user-config options))
  128. (merge default-export-options
  129. {:user-options (merge {:convert-all-tags? false}
  130. (dissoc options :user-config :verbose))}
  131. (select-keys options [:verbose])))
  132. files' (mapv #(hash-map :path %) files)
  133. _ (gp-exporter/export-doc-files conn files' <read-file doc-options)]
  134. {:import-state (:import-state doc-options)})
  135. (p/finally (fn [_]
  136. (reset! gp-block/*export-to-db-graph? false)))))
  137. ;; Tests
  138. ;; =====
  139. (deftest update-asset-links-in-block-title
  140. (are [x y]
  141. (= y (@#'gp-exporter/update-asset-links-in-block-title (first x) {(second x) "UUID"} (atom {})))
  142. ;; Standard image link with metadata
  143. ["![greg-popovich-thumbs-up.png](../assets/greg-popovich-thumbs-up_1704749687791_0.png){:height 288, :width 100} says pop"
  144. "assets/greg-popovich-thumbs-up_1704749687791_0.png"]
  145. "[[UUID]] says pop"
  146. ;; Image link with no metadata
  147. ["![some-title](../assets/CleanShot_2022-10-12_at_15.53.20@2x_1665561216083_0.png)"
  148. "assets/CleanShot_2022-10-12_at_15.53.20@2x_1665561216083_0.png"]
  149. "[[UUID]]"
  150. ;; 2nd link
  151. ["[[FIRST UUID]] and ![dino!](assets/subdir/partydino.gif)"
  152. "assets/subdir/partydino.gif"]
  153. "[[FIRST UUID]] and [[UUID]]"))
  154. (deftest-async ^:integration export-docs-graph-with-convert-all-tags
  155. (p/let [file-graph-dir "test/resources/docs-0.10.12"
  156. start-time (cljs.core/system-time)
  157. _ (docs-graph-helper/clone-docs-repo-if-not-exists file-graph-dir "v0.10.12")
  158. conn (db-test/create-conn)
  159. _ (db-pipeline/add-listener conn)
  160. {:keys [import-state]}
  161. (import-file-graph-to-db file-graph-dir conn {:convert-all-tags? true})
  162. end-time (cljs.core/system-time)]
  163. ;; Add multiplicative factor for CI as it runs about twice as slow
  164. (let [max-time (-> 25 (* (if js/process.env.CI 2 1)))]
  165. (is (< (-> end-time (- start-time) (/ 1000)) max-time)
  166. (str "Importing large graph takes less than " max-time "s")))
  167. (is (empty? (map :entity (:errors (db-validate/validate-db! @conn))))
  168. "Created graph has no validation errors")
  169. (is (= 0 (count @(:ignored-properties import-state))) "No ignored properties")
  170. (is (= 0 (count @(:ignored-assets import-state))) "No ignored assets")
  171. (is (= []
  172. (->> (d/q '[:find (pull ?b [:block/title {:block/tags [:db/ident]}])
  173. :where [?b :block/tags :logseq.class/Tag]]
  174. @conn)
  175. (map first)
  176. (remove #(= [{:db/ident :logseq.class/Tag}] (:block/tags %)))))
  177. "All classes only have :logseq.class/Tag as their tag (and don't have Page)")))
  178. (deftest-async export-basic-graph-with-convert-all-tags
  179. ;; This graph will contain basic examples of different features to import
  180. (p/let [file-graph-dir "test/resources/exporter-test-graph"
  181. conn (db-test/create-conn)
  182. ;; Calculate refs like frontend
  183. _ (db-pipeline/add-listener conn)
  184. assets (atom [])
  185. {:keys [import-state]} (import-file-graph-to-db file-graph-dir conn {:assets assets :convert-all-tags? true})]
  186. (testing "whole graph"
  187. (is (empty? (map :entity (:errors (db-validate/validate-db! @conn))))
  188. "Created graph has no validation errors")
  189. ;; Counts
  190. ;; Includes journals as property values e.g. :logseq.property/deadline
  191. (is (= 29 (count (d/q '[:find ?b :where [?b :block/tags :logseq.class/Journal]] @conn))))
  192. (is (= 5 (count (d/q '[:find ?b :where [?b :block/tags :logseq.class/Asset]] @conn))))
  193. (is (= 4 (count (d/q '[:find ?b :where [?b :block/tags :logseq.class/Task]] @conn))))
  194. (is (= 4 (count (d/q '[:find ?b :where [?b :block/tags :logseq.class/Query]] @conn))))
  195. (is (= 2 (count (d/q '[:find ?b :where [?b :block/tags :logseq.class/Card]] @conn))))
  196. (is (= 4 (count (d/q '[:find ?b :where [?b :block/tags :logseq.class/Quote-block]] @conn))))
  197. (is (= 2 (count (d/q '[:find ?b :where [?b :block/tags :logseq.class/Pdf-annotation]] @conn))))
  198. ;; Properties and tags aren't included in this count as they aren't a Page
  199. (is (= 10
  200. (->> (d/q '[:find [?b ...]
  201. :where
  202. [?b :block/title]
  203. [_ :block/page ?b]
  204. (not [?b :logseq.property/built-in?])] @conn)
  205. (map #(d/entity @conn %))
  206. (filter ldb/internal-page?)
  207. #_(map #(select-keys % [:block/title :block/tags]))
  208. count))
  209. "Correct number of pages with block content")
  210. (is (= 13 (->> @conn
  211. (d/q '[:find [?ident ...]
  212. :where [?b :block/tags :logseq.class/Tag] [?b :db/ident ?ident] (not [?b :logseq.property/built-in?])])
  213. count))
  214. "Correct number of user classes")
  215. (is (= 4 (count (d/datoms @conn :avet :block/tags :logseq.class/Whiteboard))))
  216. (is (= 0 (count @(:ignored-properties import-state))) "No ignored properties")
  217. (is (= 0 (count @(:ignored-assets import-state))) "No ignored assets")
  218. (is (= 1 (count @(:ignored-files import-state))) "Ignore .edn for now")
  219. (is (= 5 (count @assets))))
  220. (testing "logseq files"
  221. (is (= ".foo {}\n"
  222. (ffirst (d/q '[:find ?content :where [?b :file/path "logseq/custom.css"] [?b :file/content ?content]] @conn))))
  223. (is (= "logseq.api.show_msg('hello good sir!');\n"
  224. (ffirst (d/q '[:find ?content :where [?b :file/path "logseq/custom.js"] [?b :file/content ?content]] @conn)))))
  225. (testing "favorites"
  226. (is (= #{"Interstellar" "some page"}
  227. (->>
  228. (ldb/get-page-blocks @conn
  229. (:db/id (ldb/get-page @conn common-config/favorites-page-name))
  230. {:pull-keys '[* {:block/link [:block/title]}]})
  231. (map #(get-in % [:block/link :block/title]))
  232. set))))
  233. (testing "user properties"
  234. (is (= 20
  235. (->> @conn
  236. (d/q '[:find [(pull ?b [:db/ident]) ...]
  237. :where [?b :block/tags :logseq.class/Property]])
  238. (remove #(db-malli-schema/internal-ident? (:db/ident %)))
  239. count))
  240. "Correct number of user properties")
  241. (is (= #{{:db/ident :user.property/prop-bool :logseq.property/type :checkbox}
  242. {:db/ident :user.property/prop-string :logseq.property/type :default}
  243. {:db/ident :user.property/prop-num :logseq.property/type :number}
  244. {:db/ident :user.property/sameas :logseq.property/type :url}
  245. {:db/ident :user.property/rangeincludes :logseq.property/type :node}
  246. {:db/ident :user.property/startedat :logseq.property/type :date}}
  247. (->> @conn
  248. (d/q '[:find [(pull ?b [:db/ident :logseq.property/type]) ...]
  249. :where [?b :block/tags :logseq.class/Property]])
  250. (filter #(contains? #{:prop-bool :prop-string :prop-num :rangeincludes :sameas :startedat}
  251. (keyword (name (:db/ident %)))))
  252. set))
  253. "Main property types have correct inferred :type")
  254. (is (= :default
  255. (:logseq.property/type (d/entity @conn :user.property/description)))
  256. "Property value consisting of text and refs is inferred as :default")
  257. (is (= :url
  258. (:logseq.property/type (d/entity @conn :user.property/url)))
  259. "Property value with a macro correctly inferred as :url")
  260. (is (= {:user.property/prop-bool true
  261. :user.property/prop-num 5
  262. :user.property/prop-string "woot"}
  263. (db-test/readable-properties (db-test/find-block-by-content @conn "b1")))
  264. "Basic block has correct properties")
  265. (is (= #{"prop-num" "prop-string" "prop-bool"}
  266. (->> (db-test/find-block-by-content @conn "b1")
  267. :block/refs
  268. (map :block/title)
  269. set))
  270. "Block with properties has correct refs")
  271. (is (= {:user.property/prop-num2 10
  272. :block/tags [:logseq.class/Page]}
  273. (db-test/readable-properties (db-test/find-page-by-title @conn "new page")))
  274. "New page has correct properties")
  275. (is (= {:user.property/prop-bool true
  276. :user.property/prop-num 5
  277. :user.property/prop-string "yeehaw"
  278. :block/tags [:logseq.class/Page :user.class/SomeNamespace]}
  279. (db-test/readable-properties (db-test/find-page-by-title @conn "some page")))
  280. "Existing page has correct properties")
  281. (is (= {:user.property/rating 5.5}
  282. (db-test/readable-properties (db-test/find-block-by-content @conn ":rating float")))
  283. "Block with float property imports as a float")
  284. (is (= []
  285. (->> (d/q '[:find (pull ?b [:block/title {:block/tags [:db/ident]}])
  286. :where [?b :block/tags :logseq.class/Property]]
  287. @conn)
  288. (map first)
  289. (remove #(= [{:db/ident :logseq.class/Property}] (:block/tags %)))))
  290. "All properties only have :logseq.class/Property as their tag (and don't have Page)"))
  291. (testing "built-in properties"
  292. (is (= [(:db/id (db-test/find-block-by-content @conn "original block"))]
  293. (mapv :db/id (:block/refs (db-test/find-block-by-content @conn #"ref to"))))
  294. "block with a block-ref has correct :block/refs")
  295. (let [b (db-test/find-block-by-content @conn #"MEETING TITLE")]
  296. (is (= {}
  297. (and b (db-test/readable-properties b)))
  298. ":template properties are ignored to not invalidate its property types"))
  299. (is (= 20221126
  300. (-> (db-test/readable-properties (db-test/find-block-by-content @conn "only deadline"))
  301. :logseq.property/deadline
  302. date-time-util/ms->journal-day))
  303. "deadline block has correct journal as property value")
  304. (is (= {:logseq.property/scheduled 20221125
  305. :logseq.property/deadline 20221125}
  306. (-> (db-test/readable-properties (db-test/find-block-by-content @conn #"deadline and scheduled"))
  307. (select-keys [:logseq.property/scheduled :logseq.property/deadline])
  308. (update-vals date-time-util/ms->journal-day)))
  309. "scheduled block converted to correct deadline")
  310. (is (= 1 (count (d/q '[:find [(pull ?b [*]) ...]
  311. :in $ ?content
  312. :where [?b :block/title ?content]]
  313. @conn "Apr 1st, 2024")))
  314. "Only one journal page exists when deadline is on same day as journal")
  315. (is (= {:logseq.property/priority :logseq.property/priority.high}
  316. (db-test/readable-properties (db-test/find-block-by-content @conn "high priority")))
  317. "priority block has correct property")
  318. (is (= {:logseq.property/status :logseq.property/status.doing
  319. :logseq.property/priority :logseq.property/priority.medium
  320. :block/tags [:logseq.class/Task]}
  321. (db-test/readable-properties (db-test/find-block-by-content @conn "status test")))
  322. "status block has correct task properties and class")
  323. (is (= #{:logseq.property/status :block/tags}
  324. (set (keys (db-test/readable-properties (db-test/find-block-by-content @conn "old todo block")))))
  325. "old task properties like 'todo' are ignored")
  326. (is (= {:logseq.property/order-list-type "number"}
  327. (db-test/readable-properties (db-test/find-block-by-content @conn "list one")))
  328. "numered block has correct property")
  329. (is (= #{"gpt"}
  330. (:block/alias (db-test/readable-properties (db-test/find-page-by-title @conn "chat-gpt"))))
  331. "alias set correctly")
  332. (is (= ["y"]
  333. (->> (d/q '[:find [?b ...] :where [?b :block/title "y"] [?b :block/parent]]
  334. @conn)
  335. first
  336. (d/entity @conn)
  337. :block/alias
  338. (map :block/title)))
  339. "alias set correctly on namespaced page")
  340. (is (= {:logseq.property.linked-references/includes #{"Oct 9th, 2024"}
  341. :logseq.property.linked-references/excludes #{"ref2"}}
  342. (select-keys (db-test/readable-properties (db-test/find-page-by-title @conn "chat-gpt"))
  343. [:logseq.property.linked-references/excludes :logseq.property.linked-references/includes]))
  344. "linked ref filters set correctly"))
  345. (testing "built-in classes and their properties"
  346. ;; Queries
  347. (is (= {:logseq.property.table/sorting [{:id :user.property/prop-num, :asc? false}]
  348. :logseq.property.view/type :logseq.property.view/type.table
  349. :logseq.property.table/ordered-columns [:block/title :user.property/prop-string :user.property/prop-num]
  350. :logseq.property/query "(property :prop-string)"
  351. :block/tags [:logseq.class/Query]}
  352. (db-test/readable-properties (find-block-by-property-value @conn :logseq.property/query "(property :prop-string)")))
  353. "simple query block has correct query properties")
  354. (is (= "For example, here's a query with title text:"
  355. (:block/title (db-test/find-block-by-content @conn #"query with title text")))
  356. "Text around a simple query block is set as a query's title")
  357. (is (= {:logseq.property.view/type :logseq.property.view/type.list
  358. :logseq.property/query "{:query (task todo doing)}"
  359. :block/tags [:logseq.class/Query]
  360. :logseq.property.table/ordered-columns [:block/title]}
  361. (db-test/readable-properties (db-test/find-block-by-content @conn #"tasks with todo")))
  362. "Advanced query has correct query properties")
  363. (is (= "tasks with todo and doing"
  364. (:block/title (db-test/find-block-by-content @conn #"tasks with todo")))
  365. "Advanced query has custom title migrated")
  366. ;; Cards
  367. (is (= {:block/tags [:logseq.class/Card]}
  368. (db-test/readable-properties (db-test/find-block-by-content @conn "card 1")))
  369. "None of the card properties are imported since they are deprecated")
  370. ;; Assets
  371. (is (= {:block/tags [:logseq.class/Asset]
  372. :logseq.property.asset/type "png"
  373. :logseq.property.asset/checksum "3d5e620cac62159d8196c118574bfea7a16e86fa86efd1c3fa15a00a0a08792d"
  374. :logseq.property.asset/size 753471
  375. :logseq.property.asset/resize-metadata {:height 288, :width 252}}
  376. (db-test/readable-properties (db-test/find-block-by-content @conn "greg-popovich-thumbs-up_1704749687791_0")))
  377. "Asset has correct properties")
  378. (is (= (d/entity @conn :logseq.class/Asset)
  379. (:block/page (db-test/find-block-by-content @conn "greg-popovich-thumbs-up_1704749687791_0")))
  380. "Imported into Asset page")
  381. ;; Annotations
  382. (is (= {:logseq.property.pdf/hl-color :logseq.property/color.blue
  383. :logseq.property.pdf/hl-page 8
  384. :block/tags [:logseq.class/Pdf-annotation]
  385. :logseq.property/asset "Sina_de_Capoeria_Batizado_2025_-_Program_Itinerary_1752179325104_0"}
  386. (dissoc (db-test/readable-properties (db-test/find-block-by-content @conn #"Duke School - modified"))
  387. :logseq.property.pdf/hl-value :logseq.property/ls-type))
  388. "Pdf text highlight has correct properties")
  389. (is (= ["note about duke" "sub note"]
  390. (mapv :block/title (rest (ldb/get-block-and-children @conn (:block/uuid (db-test/find-block-by-content @conn #"Duke School - modified"))))))
  391. "Pdf text highlight has correct children blocks")
  392. (is (= {:logseq.property.pdf/hl-color :logseq.property/color.yellow
  393. :logseq.property.pdf/hl-page 1
  394. :block/tags [:logseq.class/Pdf-annotation]
  395. :logseq.property/asset "Sina_de_Capoeria_Batizado_2025_-_Program_Itinerary_1752179325104_0"
  396. :logseq.property.pdf/hl-image "pdf area highlight"
  397. :logseq.property.pdf/hl-type :area}
  398. (dissoc (->> (d/q '[:find [?b ...]
  399. :where [?b :block/tags :logseq.class/Pdf-annotation] [?b :block/title ""]] @conn)
  400. first
  401. (d/entity @conn)
  402. db-test/readable-properties)
  403. :logseq.property.pdf/hl-value :logseq.property/ls-type))
  404. "Pdf area highlight has correct properties")
  405. ;; Quotes
  406. (is (= {:block/tags [:logseq.class/Quote-block]
  407. :logseq.property.node/display-type :quote}
  408. (db-test/readable-properties (db-test/find-block-by-content @conn #"Saito"))))
  409. (is (= "markdown quote\n[[wut]]\nline 3"
  410. (:block/title (db-test/find-block-by-content @conn #"markdown quote")))
  411. "Markdown quote imports as full multi-line quote")
  412. (is (= "*Italic* ~~Strikethrough~~ ^^Highlight^^ #[[foo]]\n**Learn Datalog Today** is an interactive tutorial designed to teach you the [Datomic](http://datomic.com/) dialect of [Datalog](http://en.wikipedia.org/wiki/Datalog). Datalog is a declarative **database query language** with roots in logic programming. Datalog has similar expressive power as [SQL](http://en.wikipedia.org/wiki/Sql)."
  413. (:block/title (db-test/find-block-by-content @conn #"Learn Datalog")))
  414. "Imports full quote with various ast types"))
  415. (testing "embeds"
  416. (is (= {:block/title ""}
  417. (-> (d/q '[:find [(pull ?b [*]) ...]
  418. :in $ ?title
  419. :where [?b :block/link ?l] [?b :block/page ?bp] [?bp :block/journal-day 20250612] [?l :block/title ?title]]
  420. @conn
  421. "page embed")
  422. first
  423. (select-keys [:block/title])))
  424. "Page embed linked correctly")
  425. (is (= {:block/title ""}
  426. (-> (d/q '[:find [(pull ?b [*]) ...]
  427. :in $ ?title
  428. :where [?b :block/link ?l] [?b :block/page ?bp] [?bp :block/journal-day 20250612] [?l :block/title ?title]]
  429. @conn
  430. "test block embed")
  431. first
  432. (select-keys [:block/title])))
  433. "Block embed linked correctly"))
  434. (testing "tags convert to classes"
  435. (is (= :user.class/Quotes___life
  436. (:db/ident (db-test/find-page-by-title @conn "life")))
  437. "Namespaced tag's ident has hierarchy to make it unique")
  438. (is (= [:logseq.class/Tag]
  439. (map :db/ident (:block/tags (db-test/find-page-by-title @conn "life"))))
  440. "When a class is used and referenced on the same page, there should only be one instance of it")
  441. (is (= [:user.class/Quotes___life]
  442. (mapv :db/ident (:block/tags (db-test/find-block-by-content @conn #"with namespace tag"))))
  443. "Block tagged with namespace tag is only associated with leaf child tag")
  444. (is (= []
  445. (->> (d/q '[:find (pull ?b [:block/title {:block/tags [:db/ident]}])
  446. :where [?b :block/tags :logseq.class/Tag]]
  447. @conn)
  448. (map first)
  449. (remove #(= [{:db/ident :logseq.class/Tag}] (:block/tags %)))))
  450. "All classes only have :logseq.class/Tag as their tag (and don't have Page)"))
  451. (testing "namespaces"
  452. (let [expand-children (fn expand-children [ent parent]
  453. (if-let [children (:block/_parent ent)]
  454. (cons {:parent (:block/title parent) :child (:block/title ent)}
  455. (mapcat #(expand-children % ent) children))
  456. [{:parent (:block/title parent) :child (:block/title ent)}]))]
  457. ;; check pages only
  458. (is (= [{:parent "n1" :child "x"}
  459. {:parent "x" :child "z"}
  460. {:parent "x" :child "y"}]
  461. (take 3 (rest (expand-children (db-test/find-page-by-title @conn "n1") nil))))
  462. "First namespace tests duplicate parent page name")
  463. (is (= [{:parent "n2" :child "x"}
  464. {:parent "x" :child "z"}
  465. {:parent "n2" :child "alias"}]
  466. (rest (expand-children (db-test/find-page-by-title @conn "n2") nil)))
  467. "First namespace tests duplicate child page name and built-in page name")))
  468. (testing "journal timestamps"
  469. (is (= (date-time-util/journal-day->ms 20240207)
  470. (:block/created-at (db-test/find-page-by-title @conn "Feb 7th, 2024")))
  471. "journal pages are created on their journal day")
  472. (is (= (date-time-util/journal-day->ms 20240207)
  473. (:block/created-at (db-test/find-block-by-content @conn #"Inception")))
  474. "journal blocks are created on their page's journal day"))
  475. (testing "db attributes"
  476. (is (= true
  477. (:block/collapsed? (db-test/find-block-by-content @conn "collapsed block")))
  478. "Collapsed blocks are imported"))
  479. (testing "property :type changes"
  480. (is (= :node
  481. (:logseq.property/type (d/entity @conn :user.property/finishedat)))
  482. ":date property to :node value changes to :node")
  483. (is (= :node
  484. (:logseq.property/type (d/entity @conn :user.property/participants)))
  485. ":node property to :date value remains :node")
  486. (is (= :default
  487. (:logseq.property/type (d/entity @conn :user.property/description)))
  488. ":default property to :node (or any non :default value) remains :default")
  489. (is (= "[[Jakob]]"
  490. (:user.property/description (db-test/readable-properties (db-test/find-block-by-content @conn #":default to :node"))))
  491. ":default to :node property saves :default property value default with full text")
  492. (testing "with changes to upstream/existing property value"
  493. (is (= :default
  494. (:logseq.property/type (d/entity @conn :user.property/duration)))
  495. ":number property to :default value changes to :default")
  496. (is (= "20"
  497. (:user.property/duration (db-test/readable-properties (db-test/find-block-by-content @conn "existing :number to :default"))))
  498. "existing :number property value correctly saved as :default")
  499. (is (= :default
  500. (:logseq.property/type (d/entity @conn :user.property/people2)))
  501. ":node property changes to :default when :node is defined in same file")
  502. ;; tests :node :many to :default transition after :node is defined in separate file
  503. (is (= {:logseq.property/type :default :db/cardinality :db.cardinality/many}
  504. (select-keys (d/entity @conn :user.property/people) [:logseq.property/type :db/cardinality]))
  505. ":node property to :default value changes to :default and keeps existing cardinality")
  506. (is (= #{"[[Jakob]] [[Gabriel]]"}
  507. (:user.property/people (db-test/readable-properties (db-test/find-block-by-content @conn ":node people"))))
  508. "existing :node property value correctly saved as :default with full text")
  509. (is (= #{"[[Gabriel]] [[Jakob]]"}
  510. (:user.property/people (db-test/readable-properties (db-test/find-block-by-content @conn #"pending block for :node"))))
  511. "pending :node property value correctly saved as :default with full text")
  512. (is (some? (db-test/find-page-by-title @conn "Jakob"))
  513. "Previous :node property value still exists")
  514. (is (= 3 (count (find-block-by-property @conn :user.property/people)))
  515. "Converted property has correct number of property values")))
  516. (testing "imported concepts can have names of new-built concepts"
  517. (is (= #{:logseq.property/description :user.property/description}
  518. (set (d/q '[:find [?ident ...] :where [?b :db/ident ?ident] [?b :block/name "description"]] @conn)))
  519. "user description property is separate from built-in one")
  520. (is (= #{"Page" "Tag"}
  521. (set (d/q '[:find [?t-title ...] :where
  522. [?b :block/tags ?t]
  523. [?b :block/name "task"]
  524. [?t :block/title ?t-title]] @conn)))
  525. "user page is separate from built-in class"))
  526. (testing "multiline blocks"
  527. (is (= "|markdown| table|\n|some|thing|" (:block/title (db-test/find-block-by-content @conn #"markdown.*table"))))
  528. (is (= "multiline block\na 2nd\nand a 3rd" (:block/title (db-test/find-block-by-content @conn #"multiline block"))))
  529. (is (= "logbook block" (:block/title (db-test/find-block-by-content @conn #"logbook block")))))
  530. (testing ":block/refs"
  531. (let [page (db-test/find-page-by-title @conn "chat-gpt")]
  532. (is (set/subset?
  533. #{"type" "LargeLanguageModel"}
  534. (->> page :block/refs (map #(:block/title (d/entity @conn (:db/id %)))) set))
  535. "Page has correct property and property value :block/refs"))
  536. (let [block (db-test/find-block-by-content @conn "old todo block")]
  537. (is (set/subset?
  538. #{:logseq.property/status :logseq.class/Task}
  539. (->> block
  540. :block/refs
  541. (map #(:db/ident (d/entity @conn (:db/id %))))
  542. set))
  543. "Block has correct task tag and property :block/refs")))
  544. (testing "whiteboards"
  545. (let [block-with-props (db-test/find-block-by-content @conn #"block with props")]
  546. (is (= {:user.property/prop-num 10}
  547. (db-test/readable-properties block-with-props)))
  548. (is (= "block with props" (:block/title block-with-props)))))))
  549. (deftest-async export-basic-graph-with-convert-all-tags-option-disabled
  550. (p/let [file-graph-dir "test/resources/exporter-test-graph"
  551. conn (db-test/create-conn)
  552. {:keys [import-state]}
  553. (import-file-graph-to-db file-graph-dir conn {:convert-all-tags? false})]
  554. (is (empty? (map :entity (:errors (db-validate/validate-db! @conn))))
  555. "Created graph has no validation errors")
  556. (is (= 0 (count @(:ignored-properties import-state))) "No ignored properties")
  557. (is (= 0 (->> @conn
  558. (d/q '[:find [?ident ...]
  559. :where [?b :block/tags :logseq.class/Tag] [?b :db/ident ?ident] (not [?b :logseq.property/built-in?])])
  560. count))
  561. "Correct number of user classes")
  562. (is (= 4 (count (d/q '[:find ?b :where [?b :block/tags :logseq.class/Task]] @conn))))
  563. (is (= 4 (count (d/q '[:find ?b :where [?b :block/tags :logseq.class/Query]] @conn))))
  564. (is (= 2 (count (d/q '[:find ?b :where [?b :block/tags :logseq.class/Card]] @conn))))
  565. (testing "replacing refs in :block/title when :remove-inline-tags? set"
  566. (is (= 2
  567. (->> (entity-plus/lookup-kv-then-entity
  568. (db-test/find-block-by-content @conn #"replace with same start string")
  569. :block/raw-title)
  570. (re-seq db-content/id-ref-pattern)
  571. distinct
  572. count))
  573. "A block with ref names that start with same string has 2 distinct refs")
  574. (is (= 1
  575. (->> (entity-plus/lookup-kv-then-entity
  576. (db-test/find-block-by-content @conn #"replace case insensitive")
  577. :block/raw-title)
  578. (re-seq db-content/id-ref-pattern)
  579. distinct
  580. count))
  581. "A block with different case of same ref names has 1 distinct ref"))
  582. (testing "tags convert to page, refs and page-tags"
  583. (let [block (db-test/find-block-by-content @conn #"Inception")
  584. tag-page (db-test/find-page-by-title @conn "Movie")
  585. tagged-page (db-test/find-page-by-title @conn "Interstellar")]
  586. (is (string/starts-with? (str (:block/title block)) "Inception [[")
  587. "tagged block tag converts tag to page ref")
  588. (is (= [(:db/id tag-page)] (map :db/id (:block/refs block)))
  589. "tagged block has correct refs")
  590. (is (and tag-page (not (ldb/class? tag-page)))
  591. "tag page is not a class")
  592. (is (= #{"Movie"}
  593. (:logseq.property/page-tags (db-test/readable-properties tagged-page)))
  594. "tagged page has existing page imported as a tag to page-tags")
  595. (is (= #{"LargeLanguageModel" "fun" "ai"}
  596. (:logseq.property/page-tags (db-test/readable-properties (db-test/find-page-by-title @conn "chat-gpt"))))
  597. "tagged page has new page and other pages marked with '#' and '[[]]` imported as tags to page-tags")))))
  598. (deftest-async export-files-with-tag-classes-option
  599. (p/let [file-graph-dir "test/resources/exporter-test-graph"
  600. files (mapv #(node-path/join file-graph-dir %) ["journals/2024_02_07.md" "pages/Interstellar.md"])
  601. conn (db-test/create-conn)
  602. _ (import-files-to-db files conn {:tag-classes ["movie"]})]
  603. (is (empty? (map :entity (:errors (db-validate/validate-db! @conn))))
  604. "Created graph has no validation errors")
  605. (let [block (db-test/find-block-by-content @conn #"Inception")
  606. tag-page (db-test/find-page-by-title @conn "Movie")
  607. another-tag-page (db-test/find-page-by-title @conn "p0")]
  608. (is (= (:block/title block) "Inception")
  609. "tagged block with configured tag strips tag from content")
  610. (is (= [:user.class/Movie]
  611. (:block/tags (db-test/readable-properties block)))
  612. "tagged block has configured tag imported as a class")
  613. (is (= [:logseq.class/Tag] (mapv :db/ident (:block/tags tag-page)))
  614. "configured tag page in :tag-classes is a class")
  615. (is (and another-tag-page (not (ldb/class? another-tag-page)))
  616. "unconfigured tag page is not a class")
  617. (is (= {:block/tags [:logseq.class/Page :user.class/Movie]}
  618. (db-test/readable-properties (db-test/find-page-by-title @conn "Interstellar")))
  619. "tagged page has configured tag imported as a class"))))
  620. (deftest-async export-files-with-property-classes-option
  621. (p/let [file-graph-dir "test/resources/exporter-test-graph"
  622. files (mapv #(node-path/join file-graph-dir %)
  623. ["journals/2024_02_23.md" "pages/url.md" "pages/Whiteboard___Tool.md"
  624. "pages/Whiteboard___Arrow_head_toggle.md"
  625. "pages/Library.md"])
  626. conn (db-test/create-conn)
  627. _ (import-files-to-db files conn {:property-classes ["type"]})
  628. _ (@#'gp-exporter/export-class-properties conn conn)]
  629. (is (empty? (map :entity (:errors (db-validate/validate-db! @conn))))
  630. "Created graph has no validation errors")
  631. (is (= #{:user.class/Property :user.class/Movie :user.class/Class :user.class/Tool}
  632. (->> @conn
  633. (d/q '[:find [?ident ...]
  634. :where [?b :block/tags :logseq.class/Tag] [?b :db/ident ?ident] (not [?b :logseq.property/built-in?])])
  635. set))
  636. "All classes are correctly defined by :type")
  637. (is (= #{:user.property/url :user.property/sameas :user.property/rangeincludes}
  638. (->> (d/entity @conn :user.class/Property)
  639. :logseq.property.class/properties
  640. (map :db/ident)
  641. set))
  642. "Properties are correctly inferred for a class")
  643. (let [block (db-test/find-block-by-content @conn #"The Creator")
  644. tag-page (db-test/find-page-by-title @conn "Movie")]
  645. (is (= (:block/title block) "The Creator")
  646. "tagged block with configured tag strips tag from content")
  647. (is (= [:user.class/Movie]
  648. (:block/tags (db-test/readable-properties block)))
  649. "tagged block has configured tag imported as a class")
  650. (is (= (:user.property/testtagclass block) (:block/tags block))
  651. "tagged block can have another property that references the same class it is tagged with,
  652. without creating a duplicate class")
  653. (is (= [:logseq.class/Tag] (map :db/ident (:block/tags tag-page)))
  654. "configured tag page derived from :property-classes is a class")
  655. (is (nil? (db-test/find-page-by-title @conn "type"))
  656. "No page exists for configured property")
  657. (is (= #{:user.class/Property :logseq.class/Property}
  658. (set (:block/tags (db-test/readable-properties (db-test/find-page-by-title @conn "url")))))
  659. "tagged page has correct tags including one from option"))))
  660. (deftest-async export-files-with-remove-inline-tags
  661. (p/let [file-graph-dir "test/resources/exporter-test-graph"
  662. files (mapv #(node-path/join file-graph-dir %) ["journals/2024_02_07.md"])
  663. conn (db-test/create-conn)
  664. _ (import-files-to-db files conn {:remove-inline-tags? false :convert-all-tags? true})]
  665. (is (empty? (map :entity (:errors (db-validate/validate-db! @conn))))
  666. "Created graph has no validation errors")
  667. (is (string/starts-with? (:block/title (db-test/find-block-by-content @conn #"Inception"))
  668. "Inception #Movie")
  669. "block with tag preserves inline tag")))
  670. (deftest-async export-files-with-ignored-properties
  671. (p/let [file-graph-dir "test/resources/exporter-test-graph"
  672. files (mapv #(node-path/join file-graph-dir %) ["ignored/icon-page.md"])
  673. conn (db-test/create-conn)
  674. {:keys [import-state]} (import-files-to-db files conn {})]
  675. (is (= 2
  676. (count (filter #(= :icon (:property %)) @(:ignored-properties import-state))))
  677. "icon properties are visibly ignored in order to not fail import")))
  678. (deftest-async export-files-with-property-parent-classes-option
  679. (p/let [file-graph-dir "test/resources/exporter-test-graph"
  680. files (mapv #(node-path/join file-graph-dir %) ["journals/2024_11_26.md"
  681. "pages/CreativeWork.md" "pages/Movie.md" "pages/type.md"
  682. "pages/Whiteboard___Tool.md" "pages/Whiteboard___Arrow_head_toggle.md"
  683. "pages/Property.md" "pages/url.md"])
  684. conn (db-test/create-conn)
  685. _ (import-files-to-db files conn {:property-parent-classes ["parent"]
  686. ;; Also add this option to trigger some edge cases with namespace pages
  687. :property-classes ["type"]})]
  688. (is (empty? (map :entity (:errors (db-validate/validate-db! @conn))))
  689. "Created graph has no validation errors")
  690. (is (= #{:user.class/Movie :user.class/CreativeWork :user.class/Thing :user.class/Feature
  691. :user.class/Class :user.class/Tool :user.class/Whiteboard___Tool :user.class/Property}
  692. (->> @conn
  693. (d/q '[:find [?ident ...]
  694. :where [?b :block/tags :logseq.class/Tag] [?b :db/ident ?ident] (not [?b :logseq.property/built-in?])])
  695. set))
  696. "All classes are correctly defined by :type")
  697. (is (= ["CreativeWork"] (map :block/title (:logseq.property.class/extends (d/entity @conn :user.class/Movie))))
  698. "Existing page correctly set as class parent")
  699. (is (= ["Thing"] (map :block/title (:logseq.property.class/extends (d/entity @conn :user.class/CreativeWork))))
  700. "New page correctly set as class parent")))
  701. (deftest-async export-files-with-property-pages-disabled
  702. (p/let [file-graph-dir "test/resources/exporter-test-graph"
  703. ;; any page with properties
  704. files (mapv #(node-path/join file-graph-dir %) ["journals/2024_01_17.md"])
  705. conn (db-test/create-conn)
  706. _ (import-files-to-db files conn {:user-config {:property-pages/enabled? false
  707. :property-pages/excludelist #{:prop-string}}})]
  708. (is (empty? (map :entity (:errors (db-validate/validate-db! @conn))))
  709. "Created graph has no validation errors")))
  710. (deftest-async export-config-file-sets-title-format
  711. (p/let [conn (db-test/create-conn)
  712. read-file #(p/do! "{:journal/page-title-format \"yyyy-MM-dd\"}")
  713. _ (gp-exporter/export-config-file conn "logseq/config.edn" read-file {})]
  714. (is (= "yyyy-MM-dd"
  715. (:logseq.property.journal/title-format (d/entity @conn :logseq.class/Journal)))
  716. "title format set correctly by config")))