exporter_test.cljs 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696
  1. (ns ^:node-only logseq.graph-parser.exporter-test
  2. (:require ["fs" :as fs]
  3. ["path" :as node-path]
  4. [cljs.test :refer [testing is]]
  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.frontend.content :as db-content]
  13. [logseq.db.frontend.malli-schema :as db-malli-schema]
  14. [logseq.db.frontend.property :as db-property]
  15. [logseq.db.frontend.property.type :as db-property-type]
  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 [(pull ?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 [(pull ?b [*]) ...]
  40. :in $ ?prop ?prop-value %
  41. :where (property ?b ?prop ?prop-value)]
  42. db property property-value (extract-rules [:property]))
  43. first))
  44. (defn- build-graph-files
  45. "Given a file graph directory, return all files including assets and adds relative paths
  46. on ::rpath since paths are absolute by default and exporter needs relative paths for
  47. some operations"
  48. [dir*]
  49. (let [dir (node-path/resolve dir*)]
  50. (->> (common-graph/get-files dir)
  51. (concat (when (fs/existsSync (node-path/join dir* "assets"))
  52. (common-graph/readdir (node-path/join dir* "assets"))))
  53. (mapv #(hash-map :path %
  54. ::rpath (node-path/relative dir* %))))))
  55. (defn- <read-file
  56. [file]
  57. (p/let [s (fs/readFileSync (:path file))]
  58. (str s)))
  59. (defn- notify-user [m]
  60. (println (:msg m))
  61. (when (:ex-data m)
  62. (println "Ex-data:" (pr-str (merge (dissoc (:ex-data m) :error)
  63. (when-let [err (get-in m [:ex-data :error])]
  64. {:original-error (ex-data (.-cause err))}))))
  65. (println "Stacktrace:")
  66. (if-let [stack (some-> (get-in m [:ex-data :error]) ex-data :sci.impl/callstack deref)]
  67. (println (string/join
  68. "\n"
  69. (map
  70. #(str (:file %)
  71. (when (:line %) (str ":" (:line %)))
  72. (when (:sci.impl/f-meta %)
  73. (str " calls #'" (get-in % [:sci.impl/f-meta :ns]) "/" (get-in % [:sci.impl/f-meta :name]))))
  74. (reverse stack))))
  75. (println (some-> (get-in m [:ex-data :error]) .-stack))))
  76. (when (= :error (:level m))
  77. (js/process.exit 1)))
  78. (def default-export-options
  79. {;; common options
  80. :rpath-key ::rpath
  81. :notify-user notify-user
  82. :<read-file <read-file
  83. ;; :set-ui-state prn
  84. ;; config file options
  85. ;; TODO: Add actual default
  86. :default-config {}})
  87. ;; Copied from db-import script and tweaked for an in-memory import
  88. (defn- import-file-graph-to-db
  89. "Import a file graph dir just like UI does. However, unlike the UI the
  90. exporter receives file maps containing keys :path and ::rpath since :path
  91. are full paths"
  92. [file-graph-dir conn {:keys [assets] :or {assets (atom [])} :as options}]
  93. (let [*files (build-graph-files file-graph-dir)
  94. config-file (first (filter #(string/ends-with? (:path %) "logseq/config.edn") *files))
  95. _ (assert config-file "No 'logseq/config.edn' found for file graph dir")
  96. options' (merge default-export-options
  97. {:user-options (merge {:convert-all-tags? false} (dissoc options :assets :verbose))
  98. ;; asset file options
  99. :<copy-asset #(swap! assets conj %)}
  100. (select-keys options [:verbose]))]
  101. (gp-exporter/export-file-graph conn conn config-file *files options')))
  102. (defn- import-files-to-db
  103. "Import specific doc files for dev purposes"
  104. [files conn options]
  105. (reset! gp-block/*export-to-db-graph? true)
  106. (-> (p/let [doc-options (gp-exporter/build-doc-options (merge {:macros {} :file/name-format :triple-lowbar}
  107. (:user-config options))
  108. (merge default-export-options
  109. {:user-options (merge {:convert-all-tags? false}
  110. (dissoc options :user-config :verbose))}
  111. (select-keys options [:verbose])))
  112. files' (mapv #(hash-map :path %) files)
  113. _ (gp-exporter/export-doc-files conn files' <read-file doc-options)]
  114. {:import-state (:import-state doc-options)})
  115. (p/finally (fn [_]
  116. (reset! gp-block/*export-to-db-graph? false)))))
  117. (defn- readable-properties
  118. [db query-ent]
  119. (->> (db-property/properties query-ent)
  120. (map (fn [[k v]]
  121. (if (boolean? v)
  122. [k v]
  123. [k
  124. (if-let [built-in-type (get-in db-property/built-in-properties [k :schema :type])]
  125. (if (= :block/tags k)
  126. (mapv #(:db/ident (d/entity db (:db/id %))) v)
  127. (if (db-property-type/all-ref-property-types built-in-type)
  128. (db-property/ref->property-value-contents db v)
  129. v))
  130. (db-property/ref->property-value-contents db v))])))
  131. (into {})))
  132. ;; Tests
  133. ;; =====
  134. (deftest-async ^:integration export-docs-graph-with-convert-all-tags
  135. (p/let [file-graph-dir "test/resources/docs-0.10.9"
  136. _ (docs-graph-helper/clone-docs-repo-if-not-exists file-graph-dir "v0.10.9")
  137. conn (db-test/create-conn)
  138. _ (db-pipeline/add-listener conn)
  139. {:keys [import-state]}
  140. (import-file-graph-to-db file-graph-dir conn {:convert-all-tags? true})]
  141. (is (empty? (map :entity (:errors (db-validate/validate-db! @conn))))
  142. "Created graph has no validation errors")
  143. (is (= 0 (count @(:ignored-properties import-state))) "No ignored properties")
  144. (is (= []
  145. (->> (d/q '[:find (pull ?b [:block/title {:block/tags [:db/ident]}])
  146. :where [?b :block/tags :logseq.class/Tag]]
  147. @conn)
  148. (map first)
  149. (remove #(= [{:db/ident :logseq.class/Tag}] (:block/tags %)))))
  150. "All classes only have :logseq.class/Tag as their tag (and don't have Page)")))
  151. (deftest-async export-basic-graph-with-convert-all-tags
  152. ;; This graph will contain basic examples of different features to import
  153. (p/let [file-graph-dir "test/resources/exporter-test-graph"
  154. conn (db-test/create-conn)
  155. ;; Calculate refs and path-refs like frontend
  156. _ (db-pipeline/add-listener conn)
  157. assets (atom [])
  158. {:keys [import-state]} (import-file-graph-to-db file-graph-dir conn {:assets assets :convert-all-tags? true})]
  159. (testing "whole graph"
  160. (is (empty? (map :entity (:errors (db-validate/validate-db! @conn))))
  161. "Created graph has no validation errors")
  162. ;; Counts
  163. ;; Includes journals as property values e.g. :logseq.task/deadline
  164. (is (= 25 (count (d/q '[:find ?b :where [?b :block/tags :logseq.class/Journal]] @conn))))
  165. (is (= 4 (count (d/q '[:find ?b :where [?b :block/tags :logseq.class/Task]] @conn))))
  166. (is (= 3 (count (d/q '[:find ?b :where [?b :block/tags :logseq.class/Query]] @conn))))
  167. (is (= 2 (count (d/q '[:find ?b :where [?b :block/tags :logseq.class/Card]] @conn))))
  168. ;; Properties and tags aren't included in this count as they aren't a Page
  169. (is (= 10
  170. (->> (d/q '[:find [?b ...]
  171. :where
  172. [?b :block/title]
  173. [_ :block/page ?b]
  174. (not [?b :logseq.property/built-in?])] @conn)
  175. (map #(d/entity @conn %))
  176. (filter ldb/internal-page?)
  177. #_(map #(select-keys % [:block/title :block/tags]))
  178. count))
  179. "Correct number of pages with block content")
  180. (is (= 12 (->> @conn
  181. (d/q '[:find [?ident ...]
  182. :where [?b :block/tags :logseq.class/Tag] [?b :db/ident ?ident] (not [?b :logseq.property/built-in?])])
  183. count))
  184. "Correct number of user classes")
  185. (is (= 4 (count (d/datoms @conn :avet :block/tags :logseq.class/Whiteboard))))
  186. (is (= 0 (count @(:ignored-properties import-state))) "No ignored properties")
  187. (is (= 1 (count @(:ignored-files import-state))) "Ignore .edn for now")
  188. (is (= 1 (count @assets))))
  189. (testing "logseq files"
  190. (is (= ".foo {}\n"
  191. (ffirst (d/q '[:find ?content :where [?b :file/path "logseq/custom.css"] [?b :file/content ?content]] @conn))))
  192. (is (= "logseq.api.show_msg('hello good sir!');\n"
  193. (ffirst (d/q '[:find ?content :where [?b :file/path "logseq/custom.js"] [?b :file/content ?content]] @conn)))))
  194. (testing "favorites"
  195. (is (= #{"Interstellar" "some page"}
  196. (->>
  197. (ldb/get-page-blocks @conn
  198. (:db/id (ldb/get-page @conn common-config/favorites-page-name))
  199. {:pull-keys '[* {:block/link [:block/title]}]})
  200. (map #(get-in % [:block/link :block/title]))
  201. set))))
  202. (testing "user properties"
  203. (is (= 19
  204. (->> @conn
  205. (d/q '[:find [(pull ?b [:db/ident]) ...]
  206. :where [?b :block/tags :logseq.class/Property]])
  207. (remove #(db-malli-schema/internal-ident? (:db/ident %)))
  208. count))
  209. "Correct number of user properties")
  210. (is (= #{{:db/ident :user.property/prop-bool :block/schema {:type :checkbox}}
  211. {:db/ident :user.property/prop-string :block/schema {:type :default}}
  212. {:db/ident :user.property/prop-num :block/schema {:type :number}}
  213. {:db/ident :user.property/sameas :block/schema {:type :url}}
  214. {:db/ident :user.property/rangeincludes :block/schema {:type :node}}
  215. {:db/ident :user.property/startedat :block/schema {:type :date}}}
  216. (->> @conn
  217. (d/q '[:find [(pull ?b [:db/ident :block/schema]) ...]
  218. :where [?b :block/tags :logseq.class/Property]])
  219. (filter #(contains? #{:prop-bool :prop-string :prop-num :rangeincludes :sameas :startedat}
  220. (keyword (name (:db/ident %)))))
  221. set))
  222. "Main property types have correct inferred :type")
  223. (is (= :default
  224. (get-in (d/entity @conn :user.property/description) [:block/schema :type]))
  225. "Property value consisting of text and refs is inferred as :default")
  226. (is (= :url
  227. (get-in (d/entity @conn :user.property/url) [:block/schema :type]))
  228. "Property value with a macro correctly inferred as :url")
  229. (is (= {:user.property/prop-bool true
  230. :user.property/prop-num 5
  231. :user.property/prop-string "woot"}
  232. (update-vals (db-property/properties (db-test/find-block-by-content @conn "b1"))
  233. (fn [v] (if (map? v) (db-property/ref->property-value-content @conn v) v))))
  234. "Basic block has correct properties")
  235. (is (= #{"prop-num" "prop-string" "prop-bool"}
  236. (->> (d/entity @conn (:db/id (db-test/find-block-by-content @conn "b1")))
  237. :block/refs
  238. (map :block/title)
  239. set))
  240. "Block with properties has correct refs")
  241. (is (= {:user.property/prop-num2 10
  242. :block/tags [:logseq.class/Page]}
  243. (readable-properties @conn (db-test/find-page-by-title @conn "new page")))
  244. "New page has correct properties")
  245. (is (= {:user.property/prop-bool true
  246. :user.property/prop-num 5
  247. :user.property/prop-string "yeehaw"
  248. :block/tags [:logseq.class/Page]}
  249. (readable-properties @conn (db-test/find-page-by-title @conn "some page")))
  250. "Existing page has correct properties")
  251. (is (= {:user.property/rating 5.5}
  252. (readable-properties @conn (db-test/find-block-by-content @conn ":rating float")))
  253. "Block with float property imports as a float")
  254. (is (= []
  255. (->> (d/q '[:find (pull ?b [:block/title {:block/tags [:db/ident]}])
  256. :where [?b :block/tags :logseq.class/Property]]
  257. @conn)
  258. (map first)
  259. (remove #(= [{:db/ident :logseq.class/Property}] (:block/tags %)))))
  260. "All properties only have :logseq.class/Property as their tag (and don't have Page)"))
  261. (testing "built-in properties"
  262. (is (= [(:db/id (db-test/find-block-by-content @conn "original block"))]
  263. (mapv :db/id (:block/refs (db-test/find-block-by-content @conn #"ref to"))))
  264. "block with a block-ref has correct :block/refs")
  265. (let [b (db-test/find-block-by-content @conn #"MEETING TITLE")]
  266. (is (= {}
  267. (and b (readable-properties @conn b)))
  268. ":template properties are ignored to not invalidate its property types"))
  269. (is (= 20221126
  270. (-> (readable-properties @conn (db-test/find-block-by-content @conn "only deadline"))
  271. :logseq.task/deadline
  272. date-time-util/ms->journal-day))
  273. "deadline block has correct journal as property value")
  274. (is (= 20221125
  275. (-> (readable-properties @conn (db-test/find-block-by-content @conn "only scheduled"))
  276. :logseq.task/scheduled
  277. date-time-util/ms->journal-day))
  278. "scheduled block converted to correct deadline")
  279. (is (= 1 (count (d/q '[:find [(pull ?b [*]) ...]
  280. :in $ ?content
  281. :where [?b :block/title ?content]]
  282. @conn "Apr 1st, 2024")))
  283. "Only one journal page exists when deadline is on same day as journal")
  284. (is (= {:logseq.task/priority "High"}
  285. (readable-properties @conn (db-test/find-block-by-content @conn "high priority")))
  286. "priority block has correct property")
  287. (is (= {:logseq.task/status "Doing" :logseq.task/priority "Medium" :block/tags [:logseq.class/Task]}
  288. (readable-properties @conn (db-test/find-block-by-content @conn "status test")))
  289. "status block has correct task properties and class")
  290. (is (= #{:logseq.task/status :block/tags}
  291. (set (keys (readable-properties @conn (db-test/find-block-by-content @conn "old todo block")))))
  292. "old task properties like 'todo' are ignored")
  293. (is (= {:logseq.property/order-list-type "number"}
  294. (readable-properties @conn (db-test/find-block-by-content @conn "list one")))
  295. "numered block has correct property")
  296. (is (= #{"gpt"}
  297. (:block/alias (readable-properties @conn (db-test/find-page-by-title @conn "chat-gpt"))))
  298. "alias set correctly")
  299. (is (= ["y"]
  300. (->> (d/q '[:find [?b ...] :where [?b :block/title "y"] [?b :logseq.property/parent]]
  301. @conn)
  302. first
  303. (d/entity @conn)
  304. :block/alias
  305. (map :block/title)))
  306. "alias set correctly on namespaced page")
  307. (is (= {:logseq.property.linked-references/includes #{"Oct 9th, 2024"}
  308. :logseq.property.linked-references/excludes #{"ref2"}}
  309. (select-keys (readable-properties @conn (db-test/find-page-by-title @conn "chat-gpt"))
  310. [:logseq.property.linked-references/excludes :logseq.property.linked-references/includes]))
  311. "linked ref filters set correctly"))
  312. (testing "built-in classes and their properties"
  313. ;; Queries
  314. (is (= {:logseq.property.table/sorting [{:id :user.property/prop-num, :asc? false}]
  315. :logseq.property.view/type "Table View"
  316. :logseq.property.table/ordered-columns [:block/title :user.property/prop-string :user.property/prop-num]
  317. :logseq.property/query "(property :prop-string)"
  318. :block/tags [:logseq.class/Query]}
  319. (readable-properties @conn (find-block-by-property-value @conn :logseq.property/query "(property :prop-string)")))
  320. "simple query block has correct query properties")
  321. (is (= "For example, here's a query with title text:"
  322. (:block/title (db-test/find-block-by-content @conn #"query with title text")))
  323. "Text around a simple query block is set as a query's title")
  324. (is (= {:logseq.property.view/type "List View"
  325. :logseq.property/query "{:query (task todo doing)}"
  326. :block/tags [:logseq.class/Query]
  327. :logseq.property.table/ordered-columns [:block/title]}
  328. (readable-properties @conn (db-test/find-block-by-content @conn #"tasks with")))
  329. "Advanced query has correct query properties")
  330. (is (= "tasks with todo and doing"
  331. (:block/title (db-test/find-block-by-content @conn #"tasks with")))
  332. "Advanced query has custom title migrated")
  333. ;; Cards
  334. (is (= {:block/tags [:logseq.class/Card]}
  335. (readable-properties @conn (db-test/find-block-by-content @conn "card 1")))
  336. "None of the card properties are imported since they are deprecated"))
  337. (testing "tags convert to classes"
  338. (is (= :user.class/Quotes___life
  339. (:db/ident (db-test/find-page-by-title @conn "life")))
  340. "Namespaced tag's ident has hierarchy to make it unique")
  341. (is (= [:logseq.class/Tag]
  342. (map :db/ident (:block/tags (db-test/find-page-by-title @conn "life"))))
  343. "When a class is used and referenced on the same page, there should only be one instance of it")
  344. (is (= ["life"]
  345. (->> (:block/tags (db-test/find-block-by-content @conn #"with namespace tag"))
  346. (mapv #(db-property/ref->property-value-contents @conn %))))
  347. "Block tagged with namespace tag is only associated with leaf child tag")
  348. (is (= []
  349. (->> (d/q '[:find (pull ?b [:block/title {:block/tags [:db/ident]}])
  350. :where [?b :block/tags :logseq.class/Tag]]
  351. @conn)
  352. (map first)
  353. (remove #(= [{:db/ident :logseq.class/Tag}] (:block/tags %)))))
  354. "All classes only have :logseq.class/Tag as their tag (and don't have Page)"))
  355. (testing "namespaces"
  356. (let [expand-children (fn expand-children [ent parent]
  357. (if-let [children (:logseq.property/_parent ent)]
  358. (cons {:parent (:block/title parent) :child (:block/title ent)}
  359. (mapcat #(expand-children % ent) children))
  360. [{:parent (:block/title parent) :child (:block/title ent)}]))]
  361. (is (= [{:parent "n1" :child "x"}
  362. {:parent "x" :child "z"}
  363. {:parent "x" :child "y"}]
  364. (rest (expand-children (d/entity @conn (:db/id (db-test/find-page-by-title @conn "n1"))) nil)))
  365. "First namespace tests duplicate parent page name")
  366. (is (= [{:parent "n2" :child "x"}
  367. {:parent "x" :child "z"}
  368. {:parent "n2" :child "alias"}]
  369. (rest (expand-children (d/entity @conn (:db/id (db-test/find-page-by-title @conn "n2"))) nil)))
  370. "First namespace tests duplicate child page name and built-in page name")))
  371. (testing "journal timestamps"
  372. (is (= (date-time-util/journal-day->ms 20240207)
  373. (:block/created-at (db-test/find-page-by-title @conn "Feb 7th, 2024")))
  374. "journal pages are created on their journal day")
  375. (is (= (date-time-util/journal-day->ms 20240207)
  376. (:block/created-at (db-test/find-block-by-content @conn #"Inception")))
  377. "journal blocks are created on their page's journal day"))
  378. (testing "db attributes"
  379. (is (= true
  380. (:block/collapsed? (db-test/find-block-by-content @conn "collapsed block")))
  381. "Collapsed blocks are imported"))
  382. (testing "property :type changes"
  383. (is (= :node
  384. (get-in (d/entity @conn :user.property/finishedat) [:block/schema :type]))
  385. ":date property to :node value changes to :node")
  386. (is (= :node
  387. (get-in (d/entity @conn :user.property/participants) [:block/schema :type]))
  388. ":node property to :date value remains :node")
  389. (is (= :default
  390. (get-in (d/entity @conn :user.property/description) [:block/schema :type]))
  391. ":default property to :node (or any non :default value) remains :default")
  392. (is (= "[[Jakob]]"
  393. (:user.property/description (readable-properties @conn (db-test/find-block-by-content @conn #":default to :node"))))
  394. ":default to :node property saves :default property value default with full text")
  395. (testing "with changes to upstream/existing property value"
  396. (is (= :default
  397. (get-in (d/entity @conn :user.property/duration) [:block/schema :type]))
  398. ":number property to :default value changes to :default")
  399. (is (= "20"
  400. (:user.property/duration (readable-properties @conn (db-test/find-block-by-content @conn "existing :number to :default"))))
  401. "existing :number property value correctly saved as :default")
  402. (is (= {:block/schema {:type :default} :db/cardinality :db.cardinality/many}
  403. (select-keys (d/entity @conn :user.property/people) [:block/schema :db/cardinality]))
  404. ":node property to :default value changes to :default and keeps existing cardinality")
  405. (is (= #{"[[Jakob]] [[Gabriel]]"}
  406. (:user.property/people (readable-properties @conn (db-test/find-block-by-content @conn ":node people"))))
  407. "existing :node property value correctly saved as :default with full text")
  408. (is (= #{"[[Gabriel]] [[Jakob]]"}
  409. (:user.property/people (readable-properties @conn (db-test/find-block-by-content @conn #"pending block for :node"))))
  410. "pending :node property value correctly saved as :default with full text")
  411. (is (some? (db-test/find-page-by-title @conn "Jakob"))
  412. "Previous :node property value still exists")
  413. (is (= 3 (count (find-block-by-property @conn :user.property/people)))
  414. "Converted property has correct number of property values")))
  415. (testing "imported concepts can have names of new-built concepts"
  416. (is (= #{:logseq.property/description :user.property/description}
  417. (set (d/q '[:find [?ident ...] :where [?b :db/ident ?ident] [?b :block/name "description"]] @conn)))
  418. "user description property is separate from built-in one")
  419. (is (= #{"Page" "Tag"}
  420. (set (d/q '[:find [?t-title ...] :where
  421. [?b :block/tags ?t]
  422. [?b :block/name "task"]
  423. [?t :block/title ?t-title]] @conn)))
  424. "user page is separate from built-in class"))
  425. (testing "multiline blocks"
  426. (is (= "|markdown| table|\n|some|thing|" (:block/title (db-test/find-block-by-content @conn #"markdown.*table"))))
  427. (is (= "multiline block\na 2nd\nand a 3rd" (:block/title (db-test/find-block-by-content @conn #"multiline block"))))
  428. (is (= "logbook block" (:block/title (db-test/find-block-by-content @conn #"logbook block")))))
  429. (testing ":block/refs and :block/path-refs"
  430. (let [page (db-test/find-page-by-title @conn "chat-gpt")]
  431. (is (set/subset?
  432. #{"type" "LargeLanguageModel"}
  433. (->> page :block/refs (map #(:block/title (d/entity @conn (:db/id %)))) set))
  434. "Page has correct property and property value :block/refs")
  435. (is (set/subset?
  436. #{"type" "LargeLanguageModel"}
  437. (->> page :block/path-refs (map #(:block/title (d/entity @conn (:db/id %)))) set))
  438. "Page has correct property and property value :block/path-refs"))
  439. (let [block (db-test/find-block-by-content @conn "old todo block")]
  440. (is (set/subset?
  441. #{:logseq.task/status :logseq.class/Task}
  442. (->> block
  443. :block/refs
  444. (map #(:db/ident (d/entity @conn (:db/id %))))
  445. set))
  446. "Block has correct task tag and property :block/refs")
  447. (is (set/subset?
  448. #{:logseq.task/status :logseq.class/Task}
  449. (->> block
  450. :block/path-refs
  451. (map #(:db/ident (d/entity @conn (:db/id %))))
  452. set))
  453. "Block has correct task tag and property :block/path-refs")))
  454. (testing "whiteboards"
  455. (let [block-with-props (db-test/find-block-by-content @conn #"block with props")]
  456. (is (= {:user.property/prop-num 10}
  457. (readable-properties @conn block-with-props)))
  458. (is (= "block with props" (:block/title block-with-props)))))))
  459. (deftest-async export-basic-graph-with-convert-all-tags-option-disabled
  460. (p/let [file-graph-dir "test/resources/exporter-test-graph"
  461. conn (db-test/create-conn)
  462. {:keys [import-state]}
  463. (import-file-graph-to-db file-graph-dir conn {:convert-all-tags? false})]
  464. (is (empty? (map :entity (:errors (db-validate/validate-db! @conn))))
  465. "Created graph has no validation errors")
  466. (is (= 0 (count @(:ignored-properties import-state))) "No ignored properties")
  467. (is (= 0 (->> @conn
  468. (d/q '[:find [?ident ...]
  469. :where [?b :block/tags :logseq.class/Tag] [?b :db/ident ?ident] (not [?b :logseq.property/built-in?])])
  470. count))
  471. "Correct number of user classes")
  472. (is (= 4 (count (d/q '[:find ?b :where [?b :block/tags :logseq.class/Task]] @conn))))
  473. (is (= 3 (count (d/q '[:find ?b :where [?b :block/tags :logseq.class/Query]] @conn))))
  474. (is (= 2 (count (d/q '[:find ?b :where [?b :block/tags :logseq.class/Card]] @conn))))
  475. (testing "replacing refs in :block/title when :remove-inline-tags? set"
  476. (is (= 2
  477. (->> (db-test/find-block-by-content @conn #"replace with same start string")
  478. :block/title
  479. (re-seq db-content/id-ref-pattern)
  480. distinct
  481. count))
  482. "A block with ref names that start with same string has 2 distinct refs")
  483. (is (= 1
  484. (->> (db-test/find-block-by-content @conn #"replace case insensitive")
  485. :block/title
  486. (re-seq db-content/id-ref-pattern)
  487. distinct
  488. count))
  489. "A block with different case of same ref names has 1 distinct ref"))
  490. (testing "tags convert to page, refs and page-tags"
  491. (let [block (db-test/find-block-by-content @conn #"Inception")
  492. tag-page (db-test/find-page-by-title @conn "Movie")
  493. tagged-page (db-test/find-page-by-title @conn "Interstellar")]
  494. (is (string/starts-with? (str (:block/title block)) "Inception [[")
  495. "tagged block tag converts tag to page ref")
  496. (is (= [(:db/id tag-page)] (map :db/id (:block/refs block)))
  497. "tagged block has correct refs")
  498. (is (and tag-page (not (ldb/class? tag-page)))
  499. "tag page is not a class")
  500. (is (= #{"Movie"}
  501. (:logseq.property/page-tags (readable-properties @conn tagged-page)))
  502. "tagged page has existing page imported as a tag to page-tags")
  503. (is (= #{"LargeLanguageModel" "fun" "ai"}
  504. (:logseq.property/page-tags (readable-properties @conn (db-test/find-page-by-title @conn "chat-gpt"))))
  505. "tagged page has new page and other pages marked with '#' and '[[]]` imported as tags to page-tags")))))
  506. (deftest-async export-files-with-tag-classes-option
  507. (p/let [file-graph-dir "test/resources/exporter-test-graph"
  508. files (mapv #(node-path/join file-graph-dir %) ["journals/2024_02_07.md" "pages/Interstellar.md"])
  509. conn (db-test/create-conn)
  510. _ (import-files-to-db files conn {:tag-classes ["movie"]})]
  511. (is (empty? (map :entity (:errors (db-validate/validate-db! @conn))))
  512. "Created graph has no validation errors")
  513. (let [block (db-test/find-block-by-content @conn #"Inception")
  514. tag-page (db-test/find-page-by-title @conn "Movie")
  515. another-tag-page (db-test/find-page-by-title @conn "p0")]
  516. (is (= (:block/title block) "Inception")
  517. "tagged block with configured tag strips tag from content")
  518. (is (= [:user.class/Movie]
  519. (:block/tags (readable-properties @conn block)))
  520. "tagged block has configured tag imported as a class")
  521. (is (= [:logseq.class/Tag] (mapv :db/ident (:block/tags tag-page)))
  522. "configured tag page in :tag-classes is a class")
  523. (is (and another-tag-page (not (ldb/class? another-tag-page)))
  524. "unconfigured tag page is not a class")
  525. (is (= {:block/tags [:logseq.class/Page :user.class/Movie]}
  526. (readable-properties @conn (db-test/find-page-by-title @conn "Interstellar")))
  527. "tagged page has configured tag imported as a class"))))
  528. (deftest-async export-files-with-property-classes-option
  529. (p/let [file-graph-dir "test/resources/exporter-test-graph"
  530. files (mapv #(node-path/join file-graph-dir %)
  531. ["journals/2024_02_23.md" "pages/url.md" "pages/Whiteboard___Tool.md"
  532. "pages/Whiteboard___Arrow_head_toggle.md"])
  533. conn (db-test/create-conn)
  534. _ (import-files-to-db files conn {:property-classes ["type"]})
  535. _ (@#'gp-exporter/export-class-properties conn conn)]
  536. (is (empty? (map :entity (:errors (db-validate/validate-db! @conn))))
  537. "Created graph has no validation errors")
  538. (is (= #{:user.class/Property :user.class/Movie :user.class/Class :user.class/Tool}
  539. (->> @conn
  540. (d/q '[:find [?ident ...]
  541. :where [?b :block/tags :logseq.class/Tag] [?b :db/ident ?ident] (not [?b :logseq.property/built-in?])])
  542. set))
  543. "All classes are correctly defined by :type")
  544. (is (= #{:user.property/url :user.property/sameas :user.property/rangeincludes}
  545. (->> (d/entity @conn :user.class/Property)
  546. :logseq.property.class/properties
  547. (map :db/ident)
  548. set))
  549. "Properties are correctly inferred for a class")
  550. (let [block (db-test/find-block-by-content @conn #"The Creator")
  551. tag-page (db-test/find-page-by-title @conn "Movie")]
  552. (is (= (:block/title block) "The Creator")
  553. "tagged block with configured tag strips tag from content")
  554. (is (= [:user.class/Movie]
  555. (:block/tags (readable-properties @conn block)))
  556. "tagged block has configured tag imported as a class")
  557. (is (= (:user.property/testtagclass block) (:block/tags block))
  558. "tagged block can have another property that references the same class it is tagged with,
  559. without creating a duplicate class")
  560. (is (= [:logseq.class/Tag] (map :db/ident (:block/tags tag-page)))
  561. "configured tag page derived from :property-classes is a class")
  562. (is (nil? (db-test/find-page-by-title @conn "type"))
  563. "No page exists for configured property")
  564. (is (= #{:user.class/Property :logseq.class/Property}
  565. (set (:block/tags (readable-properties @conn (db-test/find-page-by-title @conn "url")))))
  566. "tagged page has correct tags including one from option"))))
  567. (deftest-async export-files-with-remove-inline-tags
  568. (p/let [file-graph-dir "test/resources/exporter-test-graph"
  569. files (mapv #(node-path/join file-graph-dir %) ["journals/2024_02_07.md"])
  570. conn (db-test/create-conn)
  571. _ (import-files-to-db files conn {:remove-inline-tags? false :convert-all-tags? true})]
  572. (is (empty? (map :entity (:errors (db-validate/validate-db! @conn))))
  573. "Created graph has no validation errors")
  574. (is (string/starts-with? (:block/title (db-test/find-block-by-content @conn #"Inception"))
  575. "Inception #Movie")
  576. "block with tag preserves inline tag")))
  577. (deftest-async export-files-with-ignored-properties
  578. (p/let [file-graph-dir "test/resources/exporter-test-graph"
  579. files (mapv #(node-path/join file-graph-dir %) ["ignored/icon-page.md"])
  580. conn (db-test/create-conn)
  581. {:keys [import-state]} (import-files-to-db files conn {})]
  582. (is (= 2
  583. (count (filter #(= :icon (:property %)) @(:ignored-properties import-state))))
  584. "icon properties are visibly ignored in order to not fail import")))
  585. (deftest-async export-files-with-property-parent-classes-option
  586. (p/let [file-graph-dir "test/resources/exporter-test-graph"
  587. files (mapv #(node-path/join file-graph-dir %) ["journals/2024_11_26.md"
  588. "pages/CreativeWork.md" "pages/Movie.md" "pages/type.md"
  589. "pages/Whiteboard___Tool.md" "pages/Whiteboard___Arrow_head_toggle.md"
  590. "pages/Property.md" "pages/url.md"])
  591. conn (db-test/create-conn)
  592. _ (import-files-to-db files conn {:property-parent-classes ["parent"]
  593. ;; Also add this option to trigger some edge cases with namespace pages
  594. :property-classes ["type"]})]
  595. (is (empty? (map :entity (:errors (db-validate/validate-db! @conn))))
  596. "Created graph has no validation errors")
  597. (is (= #{:user.class/Movie :user.class/CreativeWork :user.class/Thing :user.class/Feature
  598. :user.class/Class :user.class/Tool :user.class/Whiteboard___Tool :user.class/Property}
  599. (->> @conn
  600. (d/q '[:find [?ident ...]
  601. :where [?b :block/tags :logseq.class/Tag] [?b :db/ident ?ident] (not [?b :logseq.property/built-in?])])
  602. set))
  603. "All classes are correctly defined by :type")
  604. (is (= "CreativeWork" (get-in (d/entity @conn :user.class/Movie) [:logseq.property/parent :block/title]))
  605. "Existing page correctly set as class parent")
  606. (is (= "Thing" (get-in (d/entity @conn :user.class/CreativeWork) [:logseq.property/parent :block/title]))
  607. "New page correctly set as class parent")))
  608. (deftest-async export-config-file-sets-title-format
  609. (p/let [conn (db-test/create-conn)
  610. read-file #(p/do! (pr-str {:journal/page-title-format "yyyy-MM-dd"}))
  611. _ (gp-exporter/export-config-file conn "logseq/config.edn" read-file {})]
  612. (is (= "yyyy-MM-dd"
  613. (:logseq.property.journal/title-format (d/entity @conn :logseq.class/Journal)))
  614. "title format set correctly by config")))