2021-01-28 07:09:59 -05:00
|
|
|
(ns app.test-helpers.libraries
|
2021-05-28 06:50:42 -05:00
|
|
|
(:require
|
|
|
|
[cljs.test :as t :include-macros true]
|
|
|
|
[cljs.pprint :refer [pprint]]
|
|
|
|
[app.common.pages.helpers :as cph]
|
2022-07-01 09:51:49 -05:00
|
|
|
[app.common.types.component :as ctk]
|
2022-06-23 10:43:43 -05:00
|
|
|
[app.common.types.container :as ctn]
|
2022-07-07 09:07:34 -05:00
|
|
|
[app.main.data.workspace.state-helpers :as wsh]
|
2021-05-28 06:50:42 -05:00
|
|
|
[app.test-helpers.pages :as thp]))
|
2021-01-28 07:09:59 -05:00
|
|
|
|
|
|
|
;; ---- Helpers to manage libraries and synchronization
|
|
|
|
|
|
|
|
(defn is-instance-root
|
|
|
|
[shape]
|
|
|
|
(t/is (some? (:shape-ref shape)))
|
|
|
|
(t/is (some? (:component-id shape)))
|
|
|
|
(t/is (= (:component-root? shape) true)))
|
|
|
|
|
|
|
|
(defn is-instance-subroot
|
|
|
|
[shape]
|
|
|
|
(t/is (some? (:shape-ref shape)))
|
|
|
|
(t/is (some? (:component-id shape)))
|
|
|
|
(t/is (nil? (:component-root? shape))))
|
|
|
|
|
|
|
|
(defn is-instance-child
|
|
|
|
[shape]
|
|
|
|
(t/is (some? (:shape-ref shape)))
|
|
|
|
(t/is (nil? (:component-id shape)))
|
|
|
|
(t/is (nil? (:component-file shape)))
|
|
|
|
(t/is (nil? (:component-root? shape))))
|
|
|
|
|
2022-03-14 07:14:07 -05:00
|
|
|
(defn is-instance-inner
|
|
|
|
[shape]
|
|
|
|
(if (some? (:component-id shape))
|
|
|
|
(is-instance-subroot shape)
|
|
|
|
(is-instance-child shape)))
|
|
|
|
|
2021-01-28 07:09:59 -05:00
|
|
|
(defn is-noninstance
|
|
|
|
[shape]
|
|
|
|
(t/is (nil? (:shape-ref shape)))
|
|
|
|
(t/is (nil? (:component-id shape)))
|
|
|
|
(t/is (nil? (:component-file shape)))
|
|
|
|
(t/is (nil? (:component-root? shape)))
|
|
|
|
(t/is (nil? (:remote-synced? shape)))
|
|
|
|
(t/is (nil? (:touched shape))))
|
|
|
|
|
|
|
|
(defn is-from-file
|
|
|
|
[shape file]
|
|
|
|
(t/is (= (:component-file shape)
|
|
|
|
(:id file))))
|
|
|
|
|
2021-02-04 11:29:12 -05:00
|
|
|
(defn resolve-instance
|
2022-03-14 07:14:07 -05:00
|
|
|
"Get the shape with the given id and all its children, and
|
|
|
|
verify that they are a well constructed instance tree."
|
2021-02-04 11:29:12 -05:00
|
|
|
[state root-inst-id]
|
2022-02-07 06:37:54 -05:00
|
|
|
(let [page (thp/current-page state)
|
2022-06-23 10:43:43 -05:00
|
|
|
root-inst (ctn/get-shape page root-inst-id)
|
2022-02-07 06:37:54 -05:00
|
|
|
shapes-inst (cph/get-children-with-self (:objects page)
|
|
|
|
root-inst-id)]
|
|
|
|
(is-instance-root (first shapes-inst))
|
2022-03-22 13:01:34 -05:00
|
|
|
(run! is-instance-inner (rest shapes-inst))
|
2021-02-04 11:29:12 -05:00
|
|
|
|
|
|
|
shapes-inst))
|
|
|
|
|
|
|
|
(defn resolve-noninstance
|
2022-03-14 07:14:07 -05:00
|
|
|
"Get the shape with the given id and all its children, and
|
|
|
|
verify that they are not a component instance."
|
2021-02-04 11:29:12 -05:00
|
|
|
[state root-inst-id]
|
2022-02-07 06:37:54 -05:00
|
|
|
(let [page (thp/current-page state)
|
2022-06-23 10:43:43 -05:00
|
|
|
root-inst (ctn/get-shape page root-inst-id)
|
2022-02-07 06:37:54 -05:00
|
|
|
shapes-inst (cph/get-children-with-self (:objects page)
|
|
|
|
root-inst-id)]
|
2021-02-04 11:29:12 -05:00
|
|
|
(run! is-noninstance shapes-inst)
|
|
|
|
|
|
|
|
shapes-inst))
|
|
|
|
|
2021-03-18 07:36:25 -05:00
|
|
|
(defn resolve-instance-and-main
|
2022-03-14 07:14:07 -05:00
|
|
|
"Get the shape with the given id and all its children, and also
|
|
|
|
the main component and all its shapes."
|
2021-01-28 07:09:59 -05:00
|
|
|
[state root-inst-id]
|
2022-02-07 06:37:54 -05:00
|
|
|
(let [page (thp/current-page state)
|
2022-06-23 10:43:43 -05:00
|
|
|
root-inst (ctn/get-shape page root-inst-id)
|
2021-01-28 07:09:59 -05:00
|
|
|
|
2022-03-16 10:07:38 -05:00
|
|
|
libs (wsh/get-libraries state)
|
2022-02-07 06:37:54 -05:00
|
|
|
component (cph/get-component libs (:component-id root-inst))
|
2021-01-28 07:09:59 -05:00
|
|
|
|
2022-02-07 06:37:54 -05:00
|
|
|
shapes-inst (cph/get-children-with-self (:objects page) root-inst-id)
|
|
|
|
shapes-main (cph/get-children-with-self (:objects component) (:shape-ref root-inst))
|
2021-01-28 07:09:59 -05:00
|
|
|
|
2022-02-07 06:37:54 -05:00
|
|
|
unique-refs (into #{} (map :shape-ref) shapes-inst)
|
2021-01-28 07:09:59 -05:00
|
|
|
|
2022-02-07 06:37:54 -05:00
|
|
|
main-exists? (fn [shape]
|
2022-03-14 07:14:07 -05:00
|
|
|
(let [component-shape
|
|
|
|
(cph/get-component-shape (:objects page) shape)
|
|
|
|
|
|
|
|
component
|
|
|
|
(cph/get-component libs (:component-id component-shape))
|
|
|
|
|
|
|
|
main-shape
|
2022-06-23 10:43:43 -05:00
|
|
|
(ctn/get-shape component (:shape-ref shape))]
|
2022-03-14 07:14:07 -05:00
|
|
|
|
|
|
|
(t/is (some? main-shape))))]
|
2021-01-28 07:09:59 -05:00
|
|
|
|
|
|
|
;; Validate that the instance tree is well constructed
|
2022-02-07 06:37:54 -05:00
|
|
|
(is-instance-root (first shapes-inst))
|
2022-03-14 07:14:07 -05:00
|
|
|
(run! is-instance-inner (rest shapes-inst))
|
2021-01-28 07:09:59 -05:00
|
|
|
(t/is (= (count shapes-inst)
|
2021-03-18 07:36:25 -05:00
|
|
|
(count shapes-main)
|
2021-01-28 07:09:59 -05:00
|
|
|
(count unique-refs)))
|
2021-03-18 07:36:25 -05:00
|
|
|
(run! main-exists? shapes-inst)
|
2021-01-28 07:09:59 -05:00
|
|
|
|
2021-03-18 07:36:25 -05:00
|
|
|
[shapes-inst shapes-main component]))
|
2021-01-28 07:09:59 -05:00
|
|
|
|
2022-03-16 10:07:38 -05:00
|
|
|
(defn resolve-instance-and-main-allow-dangling
|
|
|
|
"Get the shape with the given id and all its children, and also
|
|
|
|
the main component and all its shapes. Allows shapes with the
|
|
|
|
corresponding component shape missing."
|
|
|
|
[state root-inst-id]
|
|
|
|
(let [page (thp/current-page state)
|
2022-06-23 10:43:43 -05:00
|
|
|
root-inst (ctn/get-shape page root-inst-id)
|
2022-03-16 10:07:38 -05:00
|
|
|
|
|
|
|
libs (wsh/get-libraries state)
|
|
|
|
component (cph/get-component libs (:component-id root-inst))
|
|
|
|
|
|
|
|
shapes-inst (cph/get-children-with-self (:objects page) root-inst-id)
|
|
|
|
shapes-main (cph/get-children-with-self (:objects component) (:shape-ref root-inst))
|
|
|
|
|
|
|
|
unique-refs (into #{} (map :shape-ref) shapes-inst)
|
|
|
|
|
|
|
|
main-exists? (fn [shape]
|
|
|
|
(let [component-shape
|
|
|
|
(cph/get-component-shape (:objects page) shape)
|
|
|
|
|
|
|
|
component
|
|
|
|
(cph/get-component libs (:component-id component-shape))
|
|
|
|
|
|
|
|
main-shape
|
2022-06-23 10:43:43 -05:00
|
|
|
(ctn/get-shape component (:shape-ref shape))]
|
2022-03-16 10:07:38 -05:00
|
|
|
|
|
|
|
(t/is (some? main-shape))))]
|
|
|
|
|
|
|
|
;; Validate that the instance tree is well constructed
|
|
|
|
(is-instance-root (first shapes-inst))
|
|
|
|
|
|
|
|
[shapes-inst shapes-main component]))
|
|
|
|
|
2021-02-03 07:09:38 -05:00
|
|
|
(defn resolve-component
|
2022-03-14 07:14:07 -05:00
|
|
|
"Get the component with the given id and all its shapes."
|
2021-02-03 07:09:38 -05:00
|
|
|
[state component-id]
|
2022-02-07 06:37:54 -05:00
|
|
|
(let [page (thp/current-page state)
|
2022-03-16 10:07:38 -05:00
|
|
|
libs (wsh/get-libraries state)
|
2022-02-07 06:37:54 -05:00
|
|
|
component (cph/get-component libs component-id)
|
2022-07-01 09:51:49 -05:00
|
|
|
root-main (ctk/get-component-root component)
|
2022-02-07 06:37:54 -05:00
|
|
|
shapes-main (cph/get-children-with-self (:objects component) (:id root-main))]
|
2021-02-03 07:09:38 -05:00
|
|
|
|
|
|
|
;; Validate that the component tree is well constructed
|
2021-03-18 07:36:25 -05:00
|
|
|
(run! is-noninstance shapes-main)
|
2021-02-03 07:09:38 -05:00
|
|
|
|
2021-03-18 07:36:25 -05:00
|
|
|
[shapes-main component]))
|
2021-02-03 07:09:38 -05:00
|
|
|
|