From d44692620258b43f04fc782f8a2e8a7df13b7c5c Mon Sep 17 00:00:00 2001 From: Andrey Antukh Date: Fri, 20 Nov 2026 11:30:59 +0100 Subject: [PATCH] :bug: Fix compile warnings in the frontend test tree (#13109) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * :bug: Fix compile warnings in the frontend test tree The frontend test build printed 25 analyzer warnings, all of them in test namespaces: 7 :fn-arity and 18 :infer-warning. The arity ones were latent bugs. Both builds use the :esm target, where a call that crosses a namespace boundary becomes a plain JS call, so CLJS arity dispatch never runs and the missing argument arrives as undefined instead of throwing: the warning was the only signal. One `wait-for` call had no message, and five `interaction-proxy` calls still passed five arguments after the six-argument signature landed in #11621. The rest were JS property reads whose target type could not be inferred (proxies, atoms, awaited promises). A `^js` hint on the target fixes them, the convention already used under src/. Inside `set!` a form-level hint does not reach the target, so the two per-side stroke widths bind the stroke first. Test behavior is unchanged: the same 27 tests in shape-bugfixes-test keep their 63 assertions, and the full frontend suite passes (1901 tests, 7686 assertions). The memories record why `:fn-arity` cannot be ignored in this build and how to force a clean recompile before checking for warnings. AI-assisted-by: deepseek-v4.1-flash * :recycle: Split the tokens plugin tests into one test per case `tokens_test.cljs` drove five tests from `doseq` tables, one of them a nested loop over three catalogs and seventeen mutations. A failure only named the case through its `t/testing` label, and the nested one shared a single store across all fifty-one combinations. Each row is now a test of its own: twelve stroke property edits, four `tokensSource` rejections, seventeen catalog mutations (each asserting the local, source and other connected catalog), five activation changes and three token application paths, taking the namespace from 67 to 104 tests. Fixtures (`stroke-tokens-fixture`, `external-tokens-fixture`, ...) keep the arrangement in one place, and every test builds a fresh store. Two helpers return JS objects with `^js` on their names, so the test bodies stay free of interop hints. Assertions drop from 430 to 345 because the cross product no longer repeats `isEditableTokens` and `tokens-unchanged?` once per catalog and mutation; every write is still asserted against the three catalogs. `mem:testing` now says tables are for short identical bodies only; `frontend/testing` records the `defn ^js` return-tag trick. Verified: namespace 104 tests / 345 assertions, full frontend suite 1938 tests / 7601 assertions, 0 failures; test build 0 warnings; cljfmt and clj-kondo clean. AI-assisted-by: deepseek-v4.1-flash * :paperclip: Apply suggestion from @elenatorro Co-authored-by: Elena Torró Signed-off-by: Andrey Antukh --------- Signed-off-by: Andrey Antukh Co-authored-by: Elena Torró --- .../memories/frontend/compile-diagnostics.md | 10 + .serena/memories/frontend/testing.md | 4 + .serena/memories/testing.md | 5 + .../data/workspace_mcp_test.cljs | 12 +- .../frontend_tests/plugins/file_test.cljs | 2 +- .../frontend_tests/plugins/library_test.cljs | 4 +- .../plugins/management_test.cljs | 6 +- .../plugins/page_active_validation_test.cljs | 6 +- .../plugins/shape_bugfixes_test.cljs | 10 +- .../frontend_tests/plugins/tokens_test.cljs | 846 ++++++++++++++---- 10 files changed, 735 insertions(+), 170 deletions(-) diff --git a/.serena/memories/frontend/compile-diagnostics.md b/.serena/memories/frontend/compile-diagnostics.md index 8efd42d6c5..c2e1c3d2e8 100644 --- a/.serena/memories/frontend/compile-diagnostics.md +++ b/.serena/memories/frontend/compile-diagnostics.md @@ -15,6 +15,16 @@ Recommended order after CLJ/CLJC/CLJS source edits: `clj_check_parentheses` analyzes one Clojure/ClojureScript source file and reports the area likely responsible for unclosed parentheses/brackets/braces. Use it when compiler output points near EOF, points at a misleading later form, or says delimiter-related syntax errors. +## Test build warnings + +`clojure -M:dev:shadow-cljs compile test` (from `frontend/`) is the compile that +prints the analyzer warnings CI reports. Warnings are printed only for the +namespaces the compiler actually recompiles, so a cached build reports none +while old ones are still latent: force it with +`find test -name '*.cljs' -exec touch {} +` (or by clearing +`.shadow-cljs/builds/test`) before claiming a clean tree. Why `:fn-arity` +cannot be ignored: `mem:frontend/testing`. + ## Hot reload notes When the frontend shadow-cljs watch process is running, edits to CLJC files in `common/` are normally recompiled into the browser automatically. Do not restart the frontend before checking `cljs_compiler_output`; stale behavior is often a failed build. diff --git a/.serena/memories/frontend/testing.md b/.serena/memories/frontend/testing.md index 60fd08f945..ececb74f2d 100644 --- a/.serena/memories/frontend/testing.md +++ b/.serena/memories/frontend/testing.md @@ -30,6 +30,10 @@ To assert over emitted event sequences, observe termination: subscribe through ` - `cljs.test` keeps its env in a `set!` var: assertions inside deferred ticks count. `run-block`: double `done` only warns; missing `done` stalls. - `t/async` discards the body promise — completion signals ONLY via `done`. `t/deftest ^:async` adds auto async-context + auto `done`, but awaiting stays the author's job; without it the test passes empty. - `take 1` is per-subscription on a hot subject: subscribe-before-push or hang. `end!`/`.error` with pending takes only forwards valueless completion. +- Single-arity and variadic `defn`s compile to JS functions with no argument-count check (any target), so missing required args arrive as `undefined` instead of throwing; the analyzer +`:fn-arity` warning is the only signal. Treat `:fn-arity` in `compile test` as a defect, never as noise. +- `:infer-warning` on a JS interop fixes with `^js` on the target (`(.-foo ^js x)`, `(.method ^js x ...)`). Inside `set!` the hint must sit on a symbol: `(set! (.-foo ^js (f x)) v)` still warns, so bind `(let [^js y (f x)] (set! (.-foo y) v))`. +- A helper that returns a JS object carries the hint on its name (`(defn- ^js catalog [id] ...)`), and call sites then need no hint at all — `(.method (catalog id))` and `(.-prop (catalog id))` stop warning. This is the cheapest way to keep interop out of the test bodies; `^js` on the arglist instead of the name does not work. ### Traps that bit diff --git a/.serena/memories/testing.md b/.serena/memories/testing.md index 81a1361674..8a384c9a91 100644 --- a/.serena/memories/testing.md +++ b/.serena/memories/testing.md @@ -106,6 +106,11 @@ Use your test framework's parametrize/table-driven mechanism to test multiple scenarios with a single test body. Keeps tests concise and surfaces all cases at a glance. +Only when the body is short and identical for every row. A loop that builds its +own world per row (fresh store, fixtures, several awaits) or that nests two +dimensions hides which case failed and shares state across cases: write one +test per case instead, with a shared fixture for the arrangement. + ## Test Pyramid ``` diff --git a/frontend/test/frontend_tests/data/workspace_mcp_test.cljs b/frontend/test/frontend_tests/data/workspace_mcp_test.cljs index 77c685036d..3906fb7f0c 100644 --- a/frontend/test/frontend_tests/data/workspace_mcp_test.cljs +++ b/frontend/test/frontend_tests/data/workspace_mcp_test.cljs @@ -32,14 +32,14 @@ (ptk/watch (mcp/initialize) {} stream))] (try (rx/push! stream (du/set-profile profile)) - (await (a/wait-for #(= 1 (count @events)))) + (await (a/wait-for #(= 1 (count @events)) "init event after login")) (rx/push! stream (du/set-profile profile)) (rx/push! stream (ptk/data-event :app.main.data.workspace/finalize-workspace)) (await (a/settle)) (t/is (= [:app.main.data.workspace.mcp/init] @events)) (rx/push! stream (ptk/data-event :app.main.data.auth/logged-out)) (rx/push! stream (du/set-profile profile)) - (await (a/wait-for #(= 2 (count @events)))) + (await (a/wait-for #(= 2 (count @events)) "init event after re-login")) (t/is (= [:app.main.data.workspace.mcp/init :app.main.data.workspace.mcp/init] @events)) (finally @@ -255,19 +255,19 @@ extension (atom nil) calls (atom 0) closed (atom 0)] - (with-redefs [dp/start-plugin! (fn [_ extensions] + (with-redefs [dp/start-plugin! (fn [_ ^js extensions] (reset! extension (.-mcp extensions))) dp/close-plugin! (fn [_] (swap! closed inc))] (ptk/effect (#'mcp/init-mcp-plugin {:token "test-token"}) nil stream) - (.on @extension "connect" #(swap! calls inc)) + (.on ^js @extension "connect" #(swap! calls inc)) (rx/push! stream (ptk/data-event :app.main.data.workspace.mcp/connect)) (t/is (= 1 @calls)) (rx/push! stream (ptk/data-event :app.main.data.workspace/finalize-workspace)) (t/is (zero? @closed)) (rx/push! stream (ptk/data-event stop-event)) (t/is (= 1 @closed)) - (t/is (false? (.isConnectionRequested @extension))) - (.on @extension "connect" #(swap! calls inc)) + (t/is (false? (.isConnectionRequested ^js @extension))) + (.on ^js @extension "connect" #(swap! calls inc)) (rx/push! stream (ptk/data-event :app.main.data.workspace.mcp/connect)) (t/is (= 1 @calls)) (rx/end! stream))))) diff --git a/frontend/test/frontend_tests/plugins/file_test.cljs b/frontend/test/frontend_tests/plugins/file_test.cljs index a71b336943..ef8e6ae860 100644 --- a/frontend/test/frontend_tests/plugins/file_test.cljs +++ b/frontend/test/frontend_tests/plugins/file_test.cljs @@ -122,7 +122,7 @@ :label "Version" :created-at created-at :created-by "system"}) - pinned (await (.pin version))] + ^js pinned (await (.pin version))] (t/is (file/file-version-proxy? pinned)) (t/is (= [[:get-team-users file-id] [:update-file-snapshot file-id]] diff --git a/frontend/test/frontend_tests/plugins/library_test.cljs b/frontend/test/frontend_tests/plugins/library_test.cljs index 1e798f7a78..721d6bd0ef 100644 --- a/frontend/test/frontend_tests/plugins/library_test.cljs +++ b/frontend/test/frontend_tests/plugins/library_test.cljs @@ -135,7 +135,7 @@ (def ^:private component-mutations [["remove" #(.remove %)] ["duplicate" #(.duplicate %)] - ["annotation" #(set! (.-annotation %) "Usage")]]) + ["annotation" (fn [^js component] (set! (.-annotation component) "Usage"))]]) (t/deftest ^:async component-mutations-check-library-write-permission (let [file (file-with-component) @@ -211,7 +211,7 @@ {st/state store shape/lib-component-proxy library/lib-component-proxy} (let [copy (shape/shape-proxy plugin-id (:id file) (thi/id :main-page) (thi/id :copy)) - component (.component copy)] + ^js component (.component copy)] (t/is (some? component)) (t/is (nil? (.mainInstance component)))))))) diff --git a/frontend/test/frontend_tests/plugins/management_test.cljs b/frontend/test/frontend_tests/plugins/management_test.cljs index e9637d4d88..344ec588b8 100644 --- a/frontend/test/frontend_tests/plugins/management_test.cljs +++ b/frontend/test/frontend_tests/plugins/management_test.cljs @@ -166,8 +166,8 @@ (let [context (management/create-context plugin-id) [team*] (await (.listTeams context)) [project*] (await (.listProjects context)) - team-project (first (await (.listProjects team*))) - [file*] (await (.listFiles project*))] + team-project (first (await (.listProjects ^js team*))) + [file*] (await (.listFiles ^js project*))] (t/is (= {"id" (str team-id) "name" "Team" "isDefault" true} (props team* ["id" "name" "isDefault"]))) (t/is (= {"id" (str project-id) "teamId" (str team-id) "name" "Drafts" @@ -512,7 +512,7 @@ ::sse/restore-deleted-team-files #js {:type "end" :data #{file-id}})) (rx/observe-on :async))))} (let [[project*] (await (.listDeletedProjects (management/create-context r/mcp-plugin-id))) - [file*] (await (.listDeletedFiles project*))] + [file*] (await (.listDeletedFiles ^js project*))] (t/is (= (str project-id) (obj/get project* "id"))) (t/is (= (str file-id) (obj/get file* "id"))) (t/is (nil? (await (.restore project*)))) diff --git a/frontend/test/frontend_tests/plugins/page_active_validation_test.cljs b/frontend/test/frontend_tests/plugins/page_active_validation_test.cljs index 46d83f8891..fcf86bd7a9 100644 --- a/frontend/test/frontend_tests/plugins/page_active_validation_test.cljs +++ b/frontend/test/frontend_tests/plugins/page_active_validation_test.cljs @@ -117,9 +117,11 @@ ["ShapeProxy.fills" #(set! (.-fills rect) #js [#js {:fillColor "#fabada" :fillOpacity 1}])] ["ShapeProxy.strokes" #(set! (.-strokes rect) #js [#js {:strokeColor "#fabada" :strokeOpacity 1 :strokeWidth 2}])] ["ShapeProxy.strokeWidthTop" #(do (set! (.-strokes rect) #js [#js {:strokeColor "#fabada" :strokeWidth 2}]) - (set! (.-strokeWidthTop (aget (.-strokes rect) 0)) 5))] + (let [^js stroke (aget (.-strokes rect) 0)] + (set! (.-strokeWidthTop stroke) 5)))] ["ShapeProxy.strokeWidthRight" #(do (set! (.-strokes rect) #js [#js {:strokeColor "#fabada" :strokeWidth 2}]) - (set! (.-strokeWidthRight (aget (.-strokes rect) 0)) 5))] + (let [^js stroke (aget (.-strokes rect) 0)] + (set! (.-strokeWidthRight stroke) 5)))] ;; relative geometry (shape inside a board) ["ShapeProxy.boardX" #(set! (.-boardX child) 10)] ["ShapeProxy.boardY" #(set! (.-boardY child) 10)] diff --git a/frontend/test/frontend_tests/plugins/shape_bugfixes_test.cljs b/frontend/test/frontend_tests/plugins/shape_bugfixes_test.cljs index cdd9d44017..8986fbc703 100644 --- a/frontend/test/frontend_tests/plugins/shape_bugfixes_test.cljs +++ b/frontend/test/frontend_tests/plugins/shape_bugfixes_test.cljs @@ -243,7 +243,7 @@ (with-redefs [u/not-valid (mock/stub (fn [pid prop msg] (swap! errors conj [pid prop msg]))) r/check-permission (constantly false) st/emit! mock/noop] - (let [inter (shape/interaction-proxy plugin-id file-id page-id shape-id 0)] + (let [inter (shape/interaction-proxy plugin-id file-id page-id shape-id nil 0)] (set! (.-trigger inter) "click") (t/is (= 1 (count @errors))) (t/is (= [plugin-id :trigger "Plugin doesn't have 'content:write' permission"] @@ -258,7 +258,7 @@ (with-redefs [u/not-valid (mock/stub (fn [pid prop msg] (swap! errors conj [pid prop msg]))) r/check-permission (constantly false) st/emit! mock/noop] - (let [inter (shape/interaction-proxy plugin-id file-id page-id shape-id 0)] + (let [inter (shape/interaction-proxy plugin-id file-id page-id shape-id nil 0)] (set! (.-delay inter) 100) (t/is (= 1 (count @errors))) (t/is (= [plugin-id :delay "Plugin doesn't have 'content:write' permission"] @@ -274,7 +274,7 @@ u/not-valid (mock/stub (fn [pid prop msg] (swap! errors conj [pid prop msg]))) r/check-permission (constantly false) st/emit! mock/noop] - (let [inter (shape/interaction-proxy plugin-id file-id page-id shape-id 0)] + (let [inter (shape/interaction-proxy plugin-id file-id page-id shape-id nil 0)] (set! (.-action inter) #js {:type "open-url" :url "https://example.com"}) (t/is (= 1 (count @errors))) (t/is (= [plugin-id :action "Plugin doesn't have 'content:write' permission"] @@ -289,7 +289,7 @@ (with-redefs [u/not-valid (mock/stub (fn [pid prop msg] (swap! errors conj [pid prop msg]))) r/check-permission (constantly false) st/emit! mock/noop] - (let [inter (shape/interaction-proxy plugin-id file-id page-id shape-id 0)] + (let [inter (shape/interaction-proxy plugin-id file-id page-id shape-id nil 0)] (.remove inter) (t/is (= 1 (count @errors))) (t/is (= [plugin-id :remove "Plugin doesn't have 'content:write' permission"] @@ -321,7 +321,7 @@ r/check-permission (constantly false) st/emit! mock/noop] (let [proxy (shape/shape-proxy plugin-id file-id page-id shape-id) - inter (shape/interaction-proxy plugin-id file-id page-id shape-id 0)] + inter (shape/interaction-proxy plugin-id file-id page-id shape-id nil 0)] (.removeInteraction proxy inter) (t/is (= 1 (count @errors))) (t/is (= [plugin-id :removeInteraction "Plugin doesn't have 'content:write' permission"] diff --git a/frontend/test/frontend_tests/plugins/tokens_test.cljs b/frontend/test/frontend_tests/plugins/tokens_test.cljs index a8e5033cef..7570032d48 100644 --- a/frontend/test/frontend_tests/plugins/tokens_test.cljs +++ b/frontend/test/frontend_tests/plugins/tokens_test.cljs @@ -43,41 +43,121 @@ (def ^:private get-resolved-value @#'ptok/get-resolved-value) -(t/deftest ^:async stroke-property-edits-only-remove-related-token-bindings - (let [tokens {:stroke-color "border.color" - :stroke-width-top "border.top" - :stroke-width-right "border.right" - :stroke-width-bottom "border.bottom" - :stroke-width-left "border.left"} - cases [[0 "strokeWidthTop" 8 #{:stroke-width-top}] - [0 "strokeWidthRight" 8 #{:stroke-width-right}] - [0 "strokeWidthBottom" 8 #{:stroke-width-bottom}] - [0 "strokeWidthLeft" 8 #{:stroke-width-left}] - [0 "strokeWidth" 8 ctt/per-side-stroke-width-keys] - [0 "strokeColor" "#ff0000" #{:stroke-color}] - [0 "strokeOpacity" 0.5 #{:stroke-color}] - [0 "strokeStyle" "dashed" #{}] - [0 "strokeAlignment" "center" #{}] - [1 "strokeWidthTop" 8 #{}] - [1 "strokeWidth" 8 #{}] - [1 "strokeColor" "#ff0000" #{}]]] - (doseq [[index property value removed] cases] - (t/testing (str "stroke " index " property " property) - (let [store (ths/setup-store (cthf/sample-file :file1 :page-label :page1)) - _ (set! st/state store) - ^js ctx (api/create-context "00000000-0000-0000-0000-000000000000") - ^js rect (.createRectangle ctx) - id (obj/get rect "$id") - path [:files (:current-file-id @store) :data :pages-index - (:current-page-id @store) :objects id :applied-tokens]] - (set! (.-strokes rect) - #js [#js {:strokeColor "#000000" :strokeWidth 1 :strokeWidthRight 2} - #js {:strokeColor "#000000" :strokeWidth 1}]) - (ptk/emit! store (dwsh/update-shapes [id] #(assoc % :applied-tokens tokens))) - (obj/set! (aget (.-strokes rect) index) property value) - (await (async/settle)) - (t/is (= (apply dissoc tokens removed) (get-in @store path))) - (t/is (= value (obj/get (aget (.-strokes rect) index) property)))))))) +(defn- stroke-tokens-fixture + "Fresh store with one rectangle whose two strokes carry per-side token + bindings." + [] + (let [store (ths/setup-store (cthf/sample-file :file1 :page-label :page1)) + _ (set! st/state store) + ^js ctx (api/create-context "00000000-0000-0000-0000-000000000000") + ^js rect (.createRectangle ctx) + id (obj/get rect "$id") + path [:files (:current-file-id @store) :data :pages-index + (:current-page-id @store) :objects id :applied-tokens] + tokens {:stroke-color "border.color" + :stroke-width-top "border.top" + :stroke-width-right "border.right" + :stroke-width-bottom "border.bottom" + :stroke-width-left "border.left"}] + (set! (.-strokes rect) + #js [#js {:strokeColor "#000000" :strokeWidth 1 :strokeWidthRight 2} + #js {:strokeColor "#000000" :strokeWidth 1}]) + (ptk/emit! store (dwsh/update-shapes [id] #(assoc % :applied-tokens tokens))) + {:store store :rect rect :path path :tokens tokens})) + +(defn- edit-stroke-property + "Sets `property` on the stroke at `index`, as a plugin would." + [^js rect index property value] + (obj/set! (aget (.-strokes rect) index) property value)) + +(defn- stroke-property + "Current value of `property` on the stroke at `index`." + [^js rect index property] + (obj/get (aget (.-strokes rect) index) property)) + +(t/deftest ^:async stroke-width-top-edit-drops-only-its-token-binding + (let [{:keys [store rect path tokens]} (stroke-tokens-fixture)] + (edit-stroke-property rect 0 "strokeWidthTop" 8) + (await (async/settle)) + (t/is (= (dissoc tokens :stroke-width-top) (get-in @store path))) + (t/is (= 8 (stroke-property rect 0 "strokeWidthTop"))))) + +(t/deftest ^:async stroke-width-right-edit-drops-only-its-token-binding + (let [{:keys [store rect path tokens]} (stroke-tokens-fixture)] + (edit-stroke-property rect 0 "strokeWidthRight" 8) + (await (async/settle)) + (t/is (= (dissoc tokens :stroke-width-right) (get-in @store path))) + (t/is (= 8 (stroke-property rect 0 "strokeWidthRight"))))) + +(t/deftest ^:async stroke-width-bottom-edit-drops-only-its-token-binding + (let [{:keys [store rect path tokens]} (stroke-tokens-fixture)] + (edit-stroke-property rect 0 "strokeWidthBottom" 8) + (await (async/settle)) + (t/is (= (dissoc tokens :stroke-width-bottom) (get-in @store path))) + (t/is (= 8 (stroke-property rect 0 "strokeWidthBottom"))))) + +(t/deftest ^:async stroke-width-left-edit-drops-only-its-token-binding + (let [{:keys [store rect path tokens]} (stroke-tokens-fixture)] + (edit-stroke-property rect 0 "strokeWidthLeft" 8) + (await (async/settle)) + (t/is (= (dissoc tokens :stroke-width-left) (get-in @store path))) + (t/is (= 8 (stroke-property rect 0 "strokeWidthLeft"))))) + +(t/deftest ^:async stroke-width-edit-drops-every-per-side-width-binding + (let [{:keys [store rect path tokens]} (stroke-tokens-fixture)] + (edit-stroke-property rect 0 "strokeWidth" 8) + (await (async/settle)) + (t/is (= (apply dissoc tokens ctt/per-side-stroke-width-keys) (get-in @store path))) + (t/is (= 8 (stroke-property rect 0 "strokeWidth"))))) + +(t/deftest ^:async stroke-color-edit-drops-the-stroke-color-binding + (let [{:keys [store rect path tokens]} (stroke-tokens-fixture)] + (edit-stroke-property rect 0 "strokeColor" "#ff0000") + (await (async/settle)) + (t/is (= (dissoc tokens :stroke-color) (get-in @store path))) + (t/is (= "#ff0000" (stroke-property rect 0 "strokeColor"))))) + +(t/deftest ^:async stroke-opacity-edit-drops-the-stroke-color-binding + (let [{:keys [store rect path tokens]} (stroke-tokens-fixture)] + (edit-stroke-property rect 0 "strokeOpacity" 0.5) + (await (async/settle)) + (t/is (= (dissoc tokens :stroke-color) (get-in @store path))) + (t/is (= 0.5 (stroke-property rect 0 "strokeOpacity"))))) + +(t/deftest ^:async stroke-style-edit-drops-no-token-binding + (let [{:keys [store rect path tokens]} (stroke-tokens-fixture)] + (edit-stroke-property rect 0 "strokeStyle" "dashed") + (await (async/settle)) + (t/is (= tokens (get-in @store path))) + (t/is (= "dashed" (stroke-property rect 0 "strokeStyle"))))) + +(t/deftest ^:async stroke-alignment-edit-drops-no-token-binding + (let [{:keys [store rect path tokens]} (stroke-tokens-fixture)] + (edit-stroke-property rect 0 "strokeAlignment" "center") + (await (async/settle)) + (t/is (= tokens (get-in @store path))) + (t/is (= "center" (stroke-property rect 0 "strokeAlignment"))))) + +(t/deftest ^:async second-stroke-width-top-edit-drops-no-token-binding + (let [{:keys [store rect path tokens]} (stroke-tokens-fixture)] + (edit-stroke-property rect 1 "strokeWidthTop" 8) + (await (async/settle)) + (t/is (= tokens (get-in @store path))) + (t/is (= 8 (stroke-property rect 1 "strokeWidthTop"))))) + +(t/deftest ^:async second-stroke-width-edit-drops-no-token-binding + (let [{:keys [store rect path tokens]} (stroke-tokens-fixture)] + (edit-stroke-property rect 1 "strokeWidth" 8) + (await (async/settle)) + (t/is (= tokens (get-in @store path))) + (t/is (= 8 (stroke-property rect 1 "strokeWidth"))))) + +(t/deftest ^:async second-stroke-color-edit-drops-no-token-binding + (let [{:keys [store rect path tokens]} (stroke-tokens-fixture)] + (edit-stroke-property rect 1 "strokeColor" "#ff0000") + (await (async/settle)) + (t/is (= tokens (get-in @store path))) + (t/is (= "#ff0000" (stroke-property rect 1 "strokeColor"))))) (t/deftest ^:async stroke-gradient-edits-preserve-width-token-bindings (let [store (ths/setup-store (cthf/sample-file :file1 :page-label :page1)) @@ -220,12 +300,19 @@ (await (async/settle)) (t/is (tokens-unchanged? store file)))))) -(t/deftest ^:async library-tokens-source-rejects-invalid-libraries - (let [file (token-source-file :local) - empty-lib (ctht/sample-file-with-tokens :file-id :empty) - indirect (-> (token-source-file :indirect) - (assoc-in [:data :tokens-source] (uuid/next))) - store (ths/setup-store file {:libraries [empty-lib indirect]}) +(defn- tokens-source-fixture + "Store whose current file takes its tokens from another library, plus the + libraries the `tokensSource` setter has to reject." + [] + (let [file (token-source-file :local) + empty-lib (ctht/sample-file-with-tokens :file-id :empty) + indirect (-> (token-source-file :indirect) + (assoc-in [:data :tokens-source] (uuid/next))) + store (ths/setup-store file {:libraries [empty-lib indirect]})] + {:file file :empty-lib empty-lib :indirect indirect :store store})) + +(t/deftest ^:async library-tokens-source-rejects-a-value-that-is-not-a-library + (let [{:keys [file store]} (tokens-source-fixture) plugin-id (str uuid/zero) ^js context (library/library-subcontext plugin-id) errors (atom [])] @@ -233,102 +320,513 @@ (mock/with-mocks* {st/state store st/stream (ptk/input-stream store) u/not-valid (record-not-valid errors)} - (doseq [[label value message] - [["not a library" "invalid" "invalid"] - ["not connected" (library/library-proxy plugin-id (uuid/next)) "Library is not connected"] - ["without own tokens" (library/library-proxy plugin-id (:id empty-lib)) "Library must have its own tokens"] - ["with another tokens source" (library/library-proxy plugin-id (:id indirect)) "Library must have its own tokens"]]] - (t/testing label - (reset! errors []) - (set! (.-tokensSource context) value) - (t/is (= [[plugin-id :tokensSource message]] @errors)))) + (set! (.-tokensSource context) "invalid") + (t/is (= [[plugin-id :tokensSource "invalid"]] @errors)) (await (async/settle)) (t/is (tokens-unchanged? store file)))))) -(t/deftest ^:async external-catalogs-reject-tokens-changes - (let [source (token-source-file :source) - other (token-source-file :other) - file (-> (token-source-file :local) - (assoc-in [:data :tokens-source] (:id source))) - store (ths/setup-store file {:libraries [source other]}) - plugin-id (str uuid/zero)] - (swap! store assoc-in [:plugins :flags plugin-id :throw-validation-errors] true) - (await - (mock/with-mocks* {st/state store st/stream (ptk/input-stream store)} - (doseq [[catalog-label ^js catalog] - [["local catalog" (.. (library/library-subcontext plugin-id) -local -tokens)] - ["source catalog" (.. (library/library-subcontext plugin-id) -tokensSource -tokens)] - ["other connected catalog" (.-tokens (library/library-proxy plugin-id (:id other)))]]] - (t/testing catalog-label - (let [^js token-set (aget (.-sets catalog) 0) - ^js token (aget (.-tokens token-set) 0) - ^js theme (aget (.-themes catalog) 0)] - (t/is (false? (.-isEditableTokens catalog))) - (doseq [[label mutate] - [["create set" #(.addSet catalog #js {:name "New"})] - ["rename set" #(set! (.-name token-set) "Renamed")] - ["duplicate set" #(.duplicate token-set)] - ["remove set" #(.remove token-set)] - ["create token" #(.addToken token-set #js {:name "color.new" :type "color" :value "#00FF00"})] - ["rename token" #(set! (.-name token) "color.renamed")] - ["change token value" #(set! (.-value token) "#00FF00")] - ["change token description" #(set! (.-description token) "New description")] - ["duplicate token" #(.duplicate token)] - ["remove token" #(.remove token)] - ["create theme" #(.addTheme catalog #js {:name "Dark" :group "Mode"})] - ["rename theme" #(set! (.-name theme) "Dark")] - ["change theme group" #(set! (.-group theme) "Other")] - ["add set to theme" #(.addSet theme token-set)] - ["remove set from theme" #(.removeSet theme token-set)] - ["duplicate theme" #(.duplicate theme)] - ["remove theme" #(.remove theme)]]] - (t/testing label - (t/is (thrown-with-msg? js/Error #"Cannot modify tokens in an external library" - (mutate))) - (await (async/settle)) - (t/is (tokens-unchanged? store file)) - (t/is (tokens-unchanged? store source)) - (t/is (tokens-unchanged? store other))))))))))) - -(t/deftest ^:async external-catalogs-reject-status-changes - (let [source (token-source-file :source) - other (token-source-file :other) - file (-> (token-source-file :local) - (assoc-in [:data :tokens-source] (:id source)) - (assoc-in [:data :tokens-status] (with-active-theme source))) - store (ths/setup-store file {:libraries [source other]}) - plugin-id (str uuid/zero) - message "Cannot change activation through a connected library; use penpot.library.local.tokens" - errors (atom [])] +(t/deftest ^:async library-tokens-source-rejects-a-library-that-is-not-connected + (let [{:keys [file store]} (tokens-source-fixture) + plugin-id (str uuid/zero) + ^js context (library/library-subcontext plugin-id) + errors (atom [])] (await (mock/with-mocks* {st/state store st/stream (ptk/input-stream store) u/not-valid (record-not-valid errors)} - (let [^js context (library/library-subcontext plugin-id)] - (t/is (true? (.-active ^js (aget (.. context -local -tokens -themes) 0)))) - (doseq [[catalog-label ^js catalog] - [["source catalog" (.. context -tokensSource -tokens)] - ["other connected catalog" (.-tokens (library/library-proxy plugin-id (:id other)))]]] - (t/testing catalog-label - (let [^js token-set (aget (.-sets catalog) 0) - ^js theme (aget (.-themes catalog) 0)] - (t/testing "reports the status of its own file" - (t/is (true? (.-active token-set))) - (t/is (false? (.-active theme)))) - (reset! errors []) - (set! (.-active token-set) false) - (.toggleActive token-set) - (set! (.-active theme) true) - (.toggleActive theme) - (await (async/settle)) - (t/is (= [[plugin-id :active message] - [plugin-id :toggleActive message] - [plugin-id :active message] - [plugin-id :toggleActive message]] - @errors)) - (t/is (tokens-unchanged? store file)) - (t/is (tokens-unchanged? store source)) - (t/is (tokens-unchanged? store other)))))))))) + (set! (.-tokensSource context) + (library/library-proxy plugin-id (uuid/next))) + (t/is (= [[plugin-id :tokensSource "Library is not connected"]] @errors)) + (await (async/settle)) + (t/is (tokens-unchanged? store file)))))) + +(t/deftest ^:async library-tokens-source-rejects-a-library-without-own-tokens + (let [{:keys [empty-lib file store]} (tokens-source-fixture) + plugin-id (str uuid/zero) + ^js context (library/library-subcontext plugin-id) + errors (atom [])] + (await + (mock/with-mocks* {st/state store + st/stream (ptk/input-stream store) + u/not-valid (record-not-valid errors)} + (set! (.-tokensSource context) + (library/library-proxy plugin-id (:id empty-lib))) + (t/is (= [[plugin-id :tokensSource "Library must have its own tokens"]] @errors)) + (await (async/settle)) + (t/is (tokens-unchanged? store file)))))) + +(t/deftest ^:async library-tokens-source-rejects-a-library-with-another-tokens-source + (let [{:keys [file indirect store]} (tokens-source-fixture) + plugin-id (str uuid/zero) + ^js context (library/library-subcontext plugin-id) + errors (atom [])] + (await + (mock/with-mocks* {st/state store + st/stream (ptk/input-stream store) + u/not-valid (record-not-valid errors)} + (set! (.-tokensSource context) + (library/library-proxy plugin-id (:id indirect))) + (t/is (= [[plugin-id :tokensSource "Library must have its own tokens"]] @errors)) + (await (async/settle)) + (t/is (tokens-unchanged? store file)))))) + +(def ^:private not-editable-message + "Error the plugin API reports when a write targets a tokens catalog that is + not editable." + #"Cannot modify tokens in an external library") + +(def ^:private connected-activation-message + "Error the plugin API reports when activation changes go through a connected + library." + "Cannot change activation through a connected library; use penpot.library.local.tokens") + +(defn- external-tokens-fixture + "Store whose current file takes its tokens from `source`, with `other` also + connected. + + With `:throw-validation-errors?` the plugin flag turns on, so a rejected write + throws; with `:active-theme?` the source library's first theme starts active + in the current file's tokens status." + ([] (external-tokens-fixture {})) + ([{:keys [throw-validation-errors? active-theme?]}] + (let [source (token-source-file :source) + other (token-source-file :other) + file (cond-> (-> (token-source-file :local) + (assoc-in [:data :tokens-source] (:id source))) + active-theme? + (assoc-in [:data :tokens-status] (with-active-theme source))) + store (ths/setup-store file {:libraries [source other]}) + plugin-id (str uuid/zero)] + (when throw-validation-errors? + (swap! store assoc-in [:plugins :flags plugin-id :throw-validation-errors] true)) + {:file file :source source :other other :store store :plugin-id plugin-id}))) + +(defn- ^js local-tokens-catalog + "Tokens catalog of the current file's library, as the plugin API exposes it." + [plugin-id] + (let [^js context (library/library-subcontext plugin-id)] + (.. context -local -tokens))) + +(defn- ^js source-tokens-catalog + "Tokens catalog of the file's external tokens source." + [plugin-id] + (let [^js context (library/library-subcontext plugin-id)] + (.. context -tokensSource -tokens))) + +(defn- ^js other-tokens-catalog + "Tokens catalog of another connected library." + [plugin-id file] + (.-tokens (library/library-proxy plugin-id (:id file)))) + +(defn- ^js first-set + "First token set of a tokens catalog." + [^js catalog] + (aget (.-sets catalog) 0)) + +(defn- ^js first-token + "First token of the first set of a tokens catalog." + [^js catalog] + (aget (.-tokens (first-set catalog)) 0)) + +(defn- ^js first-theme + "First theme of a tokens catalog." + [^js catalog] + (aget (.-themes catalog) 0)) + +(t/deftest ^:async external-catalogs-report-tokens-as-not-editable + (let [{:keys [store plugin-id other]} + (external-tokens-fixture {:throw-validation-errors? true})] + (await + (mock/with-mocks* {st/state store st/stream (ptk/input-stream store)} + (t/is (false? (.-isEditableTokens (local-tokens-catalog plugin-id)))) + (t/is (false? (.-isEditableTokens (source-tokens-catalog plugin-id)))) + (t/is (false? (.-isEditableTokens (other-tokens-catalog plugin-id other)))))))) + +(t/deftest ^:async external-catalogs-reject-creating-a-token-set + (let [{:keys [store file source other plugin-id]} + (external-tokens-fixture {:throw-validation-errors? true})] + (await + (mock/with-mocks* {st/state store st/stream (ptk/input-stream store)} + (t/is (thrown-with-msg? js/Error not-editable-message + (.addSet (local-tokens-catalog plugin-id) #js {:name "New"}))) + (t/is (thrown-with-msg? js/Error not-editable-message + (.addSet (source-tokens-catalog plugin-id) #js {:name "New"}))) + (t/is (thrown-with-msg? js/Error not-editable-message + (.addSet (other-tokens-catalog plugin-id other) #js {:name "New"}))) + (await (async/settle)) + (t/is (tokens-unchanged? store file)) + (t/is (tokens-unchanged? store source)) + (t/is (tokens-unchanged? store other)))))) + +(t/deftest ^:async external-catalogs-reject-renaming-a-token-set + (let [{:keys [store file source other plugin-id]} + (external-tokens-fixture {:throw-validation-errors? true}) + rename (fn [^js token-set] (set! (.-name token-set) "Renamed"))] + (await + (mock/with-mocks* {st/state store st/stream (ptk/input-stream store)} + (t/is (thrown-with-msg? js/Error not-editable-message + (rename (first-set (local-tokens-catalog plugin-id))))) + (t/is (thrown-with-msg? js/Error not-editable-message + (rename (first-set (source-tokens-catalog plugin-id))))) + (t/is (thrown-with-msg? js/Error not-editable-message + (rename (first-set (other-tokens-catalog plugin-id other))))) + (await (async/settle)) + (t/is (tokens-unchanged? store file)) + (t/is (tokens-unchanged? store source)) + (t/is (tokens-unchanged? store other)))))) + +(t/deftest ^:async external-catalogs-reject-duplicating-a-token-set + (let [{:keys [store file source other plugin-id]} + (external-tokens-fixture {:throw-validation-errors? true}) + duplicate (fn [^js token-set] (.duplicate token-set))] + (await + (mock/with-mocks* {st/state store st/stream (ptk/input-stream store)} + (t/is (thrown-with-msg? js/Error not-editable-message + (duplicate (first-set (local-tokens-catalog plugin-id))))) + (t/is (thrown-with-msg? js/Error not-editable-message + (duplicate (first-set (source-tokens-catalog plugin-id))))) + (t/is (thrown-with-msg? js/Error not-editable-message + (duplicate (first-set (other-tokens-catalog plugin-id other))))) + (await (async/settle)) + (t/is (tokens-unchanged? store file)) + (t/is (tokens-unchanged? store source)) + (t/is (tokens-unchanged? store other)))))) + +(t/deftest ^:async external-catalogs-reject-removing-a-token-set + (let [{:keys [store file source other plugin-id]} + (external-tokens-fixture {:throw-validation-errors? true}) + remove (fn [^js token-set] (.remove token-set))] + (await + (mock/with-mocks* {st/state store st/stream (ptk/input-stream store)} + (t/is (thrown-with-msg? js/Error not-editable-message + (remove (first-set (local-tokens-catalog plugin-id))))) + (t/is (thrown-with-msg? js/Error not-editable-message + (remove (first-set (source-tokens-catalog plugin-id))))) + (t/is (thrown-with-msg? js/Error not-editable-message + (remove (first-set (other-tokens-catalog plugin-id other))))) + (await (async/settle)) + (t/is (tokens-unchanged? store file)) + (t/is (tokens-unchanged? store source)) + (t/is (tokens-unchanged? store other)))))) + +(t/deftest ^:async external-catalogs-reject-creating-a-token + (let [{:keys [store file source other plugin-id]} + (external-tokens-fixture {:throw-validation-errors? true}) + create (fn [^js catalog] + (.addToken (first-set catalog) + #js {:name "color.new" :type "color" :value "#00FF00"}))] + (await + (mock/with-mocks* {st/state store st/stream (ptk/input-stream store)} + (t/is (thrown-with-msg? js/Error not-editable-message + (create (local-tokens-catalog plugin-id)))) + (t/is (thrown-with-msg? js/Error not-editable-message + (create (source-tokens-catalog plugin-id)))) + (t/is (thrown-with-msg? js/Error not-editable-message + (create (other-tokens-catalog plugin-id other)))) + (await (async/settle)) + (t/is (tokens-unchanged? store file)) + (t/is (tokens-unchanged? store source)) + (t/is (tokens-unchanged? store other)))))) + +(t/deftest ^:async external-catalogs-reject-renaming-a-token + (let [{:keys [store file source other plugin-id]} + (external-tokens-fixture {:throw-validation-errors? true}) + rename (fn [^js catalog] (set! (.-name (first-token catalog)) "color.renamed"))] + (await + (mock/with-mocks* {st/state store st/stream (ptk/input-stream store)} + (t/is (thrown-with-msg? js/Error not-editable-message + (rename (local-tokens-catalog plugin-id)))) + (t/is (thrown-with-msg? js/Error not-editable-message + (rename (source-tokens-catalog plugin-id)))) + (t/is (thrown-with-msg? js/Error not-editable-message + (rename (other-tokens-catalog plugin-id other)))) + (await (async/settle)) + (t/is (tokens-unchanged? store file)) + (t/is (tokens-unchanged? store source)) + (t/is (tokens-unchanged? store other)))))) + +(t/deftest ^:async external-catalogs-reject-changing-a-token-value + (let [{:keys [store file source other plugin-id]} + (external-tokens-fixture {:throw-validation-errors? true}) + change (fn [^js catalog] (set! (.-value (first-token catalog)) "#00FF00"))] + (await + (mock/with-mocks* {st/state store st/stream (ptk/input-stream store)} + (t/is (thrown-with-msg? js/Error not-editable-message + (change (local-tokens-catalog plugin-id)))) + (t/is (thrown-with-msg? js/Error not-editable-message + (change (source-tokens-catalog plugin-id)))) + (t/is (thrown-with-msg? js/Error not-editable-message + (change (other-tokens-catalog plugin-id other)))) + (await (async/settle)) + (t/is (tokens-unchanged? store file)) + (t/is (tokens-unchanged? store source)) + (t/is (tokens-unchanged? store other)))))) + +(t/deftest ^:async external-catalogs-reject-changing-a-token-description + (let [{:keys [store file source other plugin-id]} + (external-tokens-fixture {:throw-validation-errors? true}) + change (fn [^js catalog] (set! (.-description (first-token catalog)) "New description"))] + (await + (mock/with-mocks* {st/state store st/stream (ptk/input-stream store)} + (t/is (thrown-with-msg? js/Error not-editable-message + (change (local-tokens-catalog plugin-id)))) + (t/is (thrown-with-msg? js/Error not-editable-message + (change (source-tokens-catalog plugin-id)))) + (t/is (thrown-with-msg? js/Error not-editable-message + (change (other-tokens-catalog plugin-id other)))) + (await (async/settle)) + (t/is (tokens-unchanged? store file)) + (t/is (tokens-unchanged? store source)) + (t/is (tokens-unchanged? store other)))))) + +(t/deftest ^:async external-catalogs-reject-duplicating-a-token + (let [{:keys [store file source other plugin-id]} + (external-tokens-fixture {:throw-validation-errors? true}) + duplicate (fn [^js catalog] (.duplicate (first-token catalog)))] + (await + (mock/with-mocks* {st/state store st/stream (ptk/input-stream store)} + (t/is (thrown-with-msg? js/Error not-editable-message + (duplicate (local-tokens-catalog plugin-id)))) + (t/is (thrown-with-msg? js/Error not-editable-message + (duplicate (source-tokens-catalog plugin-id)))) + (t/is (thrown-with-msg? js/Error not-editable-message + (duplicate (other-tokens-catalog plugin-id other)))) + (await (async/settle)) + (t/is (tokens-unchanged? store file)) + (t/is (tokens-unchanged? store source)) + (t/is (tokens-unchanged? store other)))))) + +(t/deftest ^:async external-catalogs-reject-removing-a-token + (let [{:keys [store file source other plugin-id]} + (external-tokens-fixture {:throw-validation-errors? true}) + remove (fn [^js catalog] (.remove (first-token catalog)))] + (await + (mock/with-mocks* {st/state store st/stream (ptk/input-stream store)} + (t/is (thrown-with-msg? js/Error not-editable-message + (remove (local-tokens-catalog plugin-id)))) + (t/is (thrown-with-msg? js/Error not-editable-message + (remove (source-tokens-catalog plugin-id)))) + (t/is (thrown-with-msg? js/Error not-editable-message + (remove (other-tokens-catalog plugin-id other)))) + (await (async/settle)) + (t/is (tokens-unchanged? store file)) + (t/is (tokens-unchanged? store source)) + (t/is (tokens-unchanged? store other)))))) + +(t/deftest ^:async external-catalogs-reject-creating-a-theme + (let [{:keys [store file source other plugin-id]} + (external-tokens-fixture {:throw-validation-errors? true})] + (await + (mock/with-mocks* {st/state store st/stream (ptk/input-stream store)} + (t/is (thrown-with-msg? js/Error not-editable-message + (.addTheme (local-tokens-catalog plugin-id) + #js {:name "Dark" :group "Mode"}))) + (t/is (thrown-with-msg? js/Error not-editable-message + (.addTheme (source-tokens-catalog plugin-id) + #js {:name "Dark" :group "Mode"}))) + (t/is (thrown-with-msg? js/Error not-editable-message + (.addTheme (other-tokens-catalog plugin-id other) + #js {:name "Dark" :group "Mode"}))) + (await (async/settle)) + (t/is (tokens-unchanged? store file)) + (t/is (tokens-unchanged? store source)) + (t/is (tokens-unchanged? store other)))))) + +(t/deftest ^:async external-catalogs-reject-renaming-a-theme + (let [{:keys [store file source other plugin-id]} + (external-tokens-fixture {:throw-validation-errors? true}) + rename (fn [^js catalog] (set! (.-name (first-theme catalog)) "Dark"))] + (await + (mock/with-mocks* {st/state store st/stream (ptk/input-stream store)} + (t/is (thrown-with-msg? js/Error not-editable-message + (rename (local-tokens-catalog plugin-id)))) + (t/is (thrown-with-msg? js/Error not-editable-message + (rename (source-tokens-catalog plugin-id)))) + (t/is (thrown-with-msg? js/Error not-editable-message + (rename (other-tokens-catalog plugin-id other)))) + (await (async/settle)) + (t/is (tokens-unchanged? store file)) + (t/is (tokens-unchanged? store source)) + (t/is (tokens-unchanged? store other)))))) + +(t/deftest ^:async external-catalogs-reject-changing-a-theme-group + (let [{:keys [store file source other plugin-id]} + (external-tokens-fixture {:throw-validation-errors? true}) + change (fn [^js catalog] (set! (.-group (first-theme catalog)) "Other"))] + (await + (mock/with-mocks* {st/state store st/stream (ptk/input-stream store)} + (t/is (thrown-with-msg? js/Error not-editable-message + (change (local-tokens-catalog plugin-id)))) + (t/is (thrown-with-msg? js/Error not-editable-message + (change (source-tokens-catalog plugin-id)))) + (t/is (thrown-with-msg? js/Error not-editable-message + (change (other-tokens-catalog plugin-id other)))) + (await (async/settle)) + (t/is (tokens-unchanged? store file)) + (t/is (tokens-unchanged? store source)) + (t/is (tokens-unchanged? store other)))))) + +(t/deftest ^:async external-catalogs-reject-adding-a-set-to-a-theme + (let [{:keys [store file source other plugin-id]} + (external-tokens-fixture {:throw-validation-errors? true}) + add-set (fn [^js catalog] (.addSet (first-theme catalog) (first-set catalog)))] + (await + (mock/with-mocks* {st/state store st/stream (ptk/input-stream store)} + (t/is (thrown-with-msg? js/Error not-editable-message + (add-set (local-tokens-catalog plugin-id)))) + (t/is (thrown-with-msg? js/Error not-editable-message + (add-set (source-tokens-catalog plugin-id)))) + (t/is (thrown-with-msg? js/Error not-editable-message + (add-set (other-tokens-catalog plugin-id other)))) + (await (async/settle)) + (t/is (tokens-unchanged? store file)) + (t/is (tokens-unchanged? store source)) + (t/is (tokens-unchanged? store other)))))) + +(t/deftest ^:async external-catalogs-reject-removing-a-set-from-a-theme + (let [{:keys [store file source other plugin-id]} + (external-tokens-fixture {:throw-validation-errors? true}) + remove-set (fn [^js catalog] (.removeSet (first-theme catalog) (first-set catalog)))] + (await + (mock/with-mocks* {st/state store st/stream (ptk/input-stream store)} + (t/is (thrown-with-msg? js/Error not-editable-message + (remove-set (local-tokens-catalog plugin-id)))) + (t/is (thrown-with-msg? js/Error not-editable-message + (remove-set (source-tokens-catalog plugin-id)))) + (t/is (thrown-with-msg? js/Error not-editable-message + (remove-set (other-tokens-catalog plugin-id other)))) + (await (async/settle)) + (t/is (tokens-unchanged? store file)) + (t/is (tokens-unchanged? store source)) + (t/is (tokens-unchanged? store other)))))) + +(t/deftest ^:async external-catalogs-reject-duplicating-a-theme + (let [{:keys [store file source other plugin-id]} + (external-tokens-fixture {:throw-validation-errors? true}) + duplicate (fn [^js catalog] (.duplicate (first-theme catalog)))] + (await + (mock/with-mocks* {st/state store st/stream (ptk/input-stream store)} + (t/is (thrown-with-msg? js/Error not-editable-message + (duplicate (local-tokens-catalog plugin-id)))) + (t/is (thrown-with-msg? js/Error not-editable-message + (duplicate (source-tokens-catalog plugin-id)))) + (t/is (thrown-with-msg? js/Error not-editable-message + (duplicate (other-tokens-catalog plugin-id other)))) + (await (async/settle)) + (t/is (tokens-unchanged? store file)) + (t/is (tokens-unchanged? store source)) + (t/is (tokens-unchanged? store other)))))) + +(t/deftest ^:async external-catalogs-reject-removing-a-theme + (let [{:keys [store file source other plugin-id]} + (external-tokens-fixture {:throw-validation-errors? true}) + remove (fn [^js catalog] (.remove (first-theme catalog)))] + (await + (mock/with-mocks* {st/state store st/stream (ptk/input-stream store)} + (t/is (thrown-with-msg? js/Error not-editable-message + (remove (local-tokens-catalog plugin-id)))) + (t/is (thrown-with-msg? js/Error not-editable-message + (remove (source-tokens-catalog plugin-id)))) + (t/is (thrown-with-msg? js/Error not-editable-message + (remove (other-tokens-catalog plugin-id other)))) + (await (async/settle)) + (t/is (tokens-unchanged? store file)) + (t/is (tokens-unchanged? store source)) + (t/is (tokens-unchanged? store other)))))) + +(t/deftest ^:async external-catalogs-report-the-status-of-their-own-file + (let [{:keys [store plugin-id other]} (external-tokens-fixture {:active-theme? true})] + (await + (mock/with-mocks* {st/state store st/stream (ptk/input-stream store)} + (t/is (true? (.-active (first-theme (local-tokens-catalog plugin-id))))) + (t/is (true? (.-active (first-set (source-tokens-catalog plugin-id))))) + (t/is (false? (.-active (first-theme (source-tokens-catalog plugin-id))))) + (t/is (true? (.-active (first-set (other-tokens-catalog plugin-id other))))) + (t/is (false? (.-active (first-theme (other-tokens-catalog plugin-id other))))))))) + +(t/deftest ^:async external-catalogs-reject-deactivating-a-token-set + (let [{:keys [store file source other plugin-id]} + (external-tokens-fixture {:active-theme? true}) + deactivate (fn [^js catalog] + (let [^js token-set (first-set catalog)] + (set! (.-active token-set) false))) + errors (atom [])] + (await + (mock/with-mocks* {st/state store + st/stream (ptk/input-stream store) + u/not-valid (record-not-valid errors)} + (deactivate (source-tokens-catalog plugin-id)) + (deactivate (other-tokens-catalog plugin-id other)) + (await (async/settle)) + (t/is (= [[plugin-id :active connected-activation-message] + [plugin-id :active connected-activation-message]] + @errors)) + (t/is (tokens-unchanged? store file)) + (t/is (tokens-unchanged? store source)) + (t/is (tokens-unchanged? store other)))))) + +(t/deftest ^:async external-catalogs-reject-toggling-a-token-set + (let [{:keys [store file source other plugin-id]} + (external-tokens-fixture {:active-theme? true}) + toggle (fn [^js catalog] + (let [^js token-set (first-set catalog)] + (.toggleActive token-set))) + errors (atom [])] + (await + (mock/with-mocks* {st/state store + st/stream (ptk/input-stream store) + u/not-valid (record-not-valid errors)} + (toggle (source-tokens-catalog plugin-id)) + (toggle (other-tokens-catalog plugin-id other)) + (await (async/settle)) + (t/is (= [[plugin-id :toggleActive connected-activation-message] + [plugin-id :toggleActive connected-activation-message]] + @errors)) + (t/is (tokens-unchanged? store file)) + (t/is (tokens-unchanged? store source)) + (t/is (tokens-unchanged? store other)))))) + +(t/deftest ^:async external-catalogs-reject-activating-a-theme + (let [{:keys [store file source other plugin-id]} + (external-tokens-fixture {:active-theme? true}) + activate (fn [^js catalog] + (let [^js theme (first-theme catalog)] + (set! (.-active theme) true))) + errors (atom [])] + (await + (mock/with-mocks* {st/state store + st/stream (ptk/input-stream store) + u/not-valid (record-not-valid errors)} + (activate (source-tokens-catalog plugin-id)) + (activate (other-tokens-catalog plugin-id other)) + (await (async/settle)) + (t/is (= [[plugin-id :active connected-activation-message] + [plugin-id :active connected-activation-message]] + @errors)) + (t/is (tokens-unchanged? store file)) + (t/is (tokens-unchanged? store source)) + (t/is (tokens-unchanged? store other)))))) + +(t/deftest ^:async external-catalogs-reject-toggling-a-theme + (let [{:keys [store file source other plugin-id]} + (external-tokens-fixture {:active-theme? true}) + toggle (fn [^js catalog] + (let [^js theme (first-theme catalog)] + (.toggleActive theme))) + errors (atom [])] + (await + (mock/with-mocks* {st/state store + st/stream (ptk/input-stream store) + u/not-valid (record-not-valid errors)} + (toggle (source-tokens-catalog plugin-id)) + (toggle (other-tokens-catalog plugin-id other)) + (await (async/settle)) + (t/is (= [[plugin-id :toggleActive connected-activation-message] + [plugin-id :toggleActive connected-activation-message]] + @errors)) + (t/is (tokens-unchanged? store file)) + (t/is (tokens-unchanged? store source)) + (t/is (tokens-unchanged? store other)))))) (t/deftest ^:async external-token-source-allows-set-activation (let [source (token-source-file :source) @@ -338,7 +836,8 @@ store (ths/setup-store file {:libraries [source]})] (await (mock/with-mocks* {st/state store st/stream (ptk/input-stream store)} - (let [^js catalog (.. (library/library-subcontext (str uuid/zero)) -local -tokens) + (let [^js context (library/library-subcontext (str uuid/zero)) + ^js catalog (.. context -local -tokens) ^js token-set (aget (.-sets catalog) 0)] (t/is (true? (.-active token-set))) (set! (.-active token-set) false) @@ -356,7 +855,8 @@ store (ths/setup-store file {:libraries [source]})] (await (mock/with-mocks* {st/state store st/stream (ptk/input-stream store)} - (let [^js catalog (.. (library/library-subcontext (str uuid/zero)) -local -tokens) + (let [^js context (library/library-subcontext (str uuid/zero)) + ^js catalog (.. context -local -tokens) ^js theme (aget (.-themes catalog) 0)] (t/is (false? (.-active theme))) (set! (.-active theme) true) @@ -375,7 +875,8 @@ store (ths/setup-store file {:libraries [source]})] (await (mock/with-mocks* {st/state store st/stream (ptk/input-stream store)} - (let [^js catalog (.. (library/library-subcontext (str uuid/zero)) -local -tokens) + (let [^js context (library/library-subcontext (str uuid/zero)) + ^js catalog (.. context -local -tokens) ^js theme (aget (.-themes catalog) 0)] (st/emit! (tohs/watch-undo-stack)) (.toggleActive theme) @@ -388,17 +889,25 @@ "undo external theme toggle")) (t/is (tokens-unchanged? store source))))))) -(t/deftest ^:async external-token-source-allows-applying-tokens-to-shapes - (let [source (token-source-file :source) - file (-> (token-source-file :local) - (assoc-in [:data :tokens-source] (:id source)) - (assoc-in [:data :tokens-status] (ctht/get-tokens-status source)) - (ctho/add-rect :rect1) - (ctho/add-rect :rect2) - (ctho/add-rect :rect3)) - store (ths/setup-store file {:libraries [source]}) - page-id (cthf/current-page-id file)] +(defn- external-token-shapes-fixture + "Store whose current file takes its tokens from `source` and holds three + rectangles, with the third one selected." + [] + (let [source (token-source-file :source) + file (-> (token-source-file :local) + (assoc-in [:data :tokens-source] (:id source)) + (assoc-in [:data :tokens-status] (ctht/get-tokens-status source)) + (ctho/add-rect :rect1) + (ctho/add-rect :rect2) + (ctho/add-rect :rect3)) + store (ths/setup-store file {:libraries [source]}) + page-id (cthf/current-page-id file)] (swap! store assoc-in [:workspace-local :selected] #{(cthi/id :rect3)}) + {:file file :source source :store store :page-id page-id})) + +(t/deftest ^:async apply-token-applies-an-external-token-to-a-shape + (let [{:keys [file source store page-id]} (external-token-shapes-fixture) + shape-path [:files (:id file) :data :pages-index page-id :objects (cthi/id :rect1)]] (await (mock/with-mocks* {st/state store st/stream (ptk/input-stream store)} (let [^js context (api/create-context (str uuid/zero)) @@ -406,17 +915,52 @@ ^js catalog (.. context -library -local -tokens) ^js token-set (aget (.-sets catalog) 0) ^js token (aget (.-tokens token-set) 0) - ^js rect1 (.getShapeById page (str (cthi/id :rect1))) - ^js rect2 (.getShapeById page (str (cthi/id :rect2)))] + ^js rect1 (.getShapeById page (str (cthi/id :rect1)))] (.applyToken rect1 token #js ["fill"]) + (await (async/wait-for #(= "color.primary" (get-in @store (conj shape-path :applied-tokens :fill))) + "apply external token to a shape")) + (await (async/wait-for #(= "#ff0000" (get-in @store (conj shape-path :fills 0 :fill-color))) + "resolve external token on a shape")) + (t/is (= (ctht/get-tokens-lib file) (get-in @store [:files (:id file) :data :tokens-lib]))) + (t/is (= (datafy (ctht/get-tokens-status file)) + (datafy (get-in @store [:files (:id file) :data :tokens-status])))) + (t/is (tokens-unchanged? store source))))))) + +(t/deftest ^:async apply-token-to-shapes-applies-an-external-token-to-each-shape + (let [{:keys [file source store page-id]} (external-token-shapes-fixture) + shape-path [:files (:id file) :data :pages-index page-id :objects (cthi/id :rect2)]] + (await + (mock/with-mocks* {st/state store st/stream (ptk/input-stream store)} + (let [^js context (api/create-context (str uuid/zero)) + ^js page (.-currentPage context) + ^js catalog (.. context -library -local -tokens) + ^js token-set (aget (.-sets catalog) 0) + ^js token (aget (.-tokens token-set) 0) + ^js rect2 (.getShapeById page (str (cthi/id :rect2)))] (.applyToShapes token #js [rect2] #js ["fill"]) + (await (async/wait-for #(= "color.primary" (get-in @store (conj shape-path :applied-tokens :fill))) + "apply external token to shapes")) + (await (async/wait-for #(= "#ff0000" (get-in @store (conj shape-path :fills 0 :fill-color))) + "resolve external token on a shape")) + (t/is (= (ctht/get-tokens-lib file) (get-in @store [:files (:id file) :data :tokens-lib]))) + (t/is (= (datafy (ctht/get-tokens-status file)) + (datafy (get-in @store [:files (:id file) :data :tokens-status])))) + (t/is (tokens-unchanged? store source))))))) + +(t/deftest ^:async apply-token-to-selected-applies-an-external-token-to-the-selection + (let [{:keys [file source store page-id]} (external-token-shapes-fixture) + shape-path [:files (:id file) :data :pages-index page-id :objects (cthi/id :rect3)]] + (await + (mock/with-mocks* {st/state store st/stream (ptk/input-stream store)} + (let [^js context (api/create-context (str uuid/zero)) + ^js catalog (.. context -library -local -tokens) + ^js token-set (aget (.-sets catalog) 0) + ^js token (aget (.-tokens token-set) 0)] (.applyToSelected token #js ["fill"]) - (doseq [label [:rect1 :rect2 :rect3]] - (let [shape-path [:files (:id file) :data :pages-index page-id :objects (cthi/id label)]] - (await (async/wait-for #(= "color.primary" (get-in @store (conj shape-path :applied-tokens :fill))) - (str "apply external token to " label))) - (await (async/wait-for #(= "#ff0000" (get-in @store (conj shape-path :fills 0 :fill-color))) - (str "resolve external token on " label))))) + (await (async/wait-for #(= "color.primary" (get-in @store (conj shape-path :applied-tokens :fill))) + "apply external token to the selection")) + (await (async/wait-for #(= "#ff0000" (get-in @store (conj shape-path :fills 0 :fill-color))) + "resolve external token on the selection")) (t/is (= (ctht/get-tokens-lib file) (get-in @store [:files (:id file) :data :tokens-lib]))) (t/is (= (datafy (ctht/get-tokens-status file)) (datafy (get-in @store [:files (:id file) :data :tokens-status]))))