penpot/frontend/test/frontend_tests/data/workspace_mcp_test.cljs
Andrey Antukh d446926202
🐛 Fix compile warnings in the frontend test tree (#13109)
* 🐛 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

* ♻️ 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

* 📎 Apply suggestion from @elenatorro

Co-authored-by: Elena Torró <elenatorro@gmail.com>
Signed-off-by: Andrey Antukh <niwi@niwi.nz>

---------

Signed-off-by: Andrey Antukh <niwi@niwi.nz>
Co-authored-by: Elena Torró <elenatorro@gmail.com>
2026-11-20 11:30:59 +01:00

274 lines
12 KiB
Clojure

;; This Source Code Form is subject to the terms of the Mozilla Public
;; License, v. 2.0. If a copy of the MPL was not distributed with this
;; file, You can obtain one at http://mozilla.org/MPL/2.0/.
;;
;; Copyright (c) KALEIDOS SUBSIDIARY SL
(ns frontend-tests.data.workspace-mcp-test
(:require
[app.common.time :as ct]
[app.common.uuid :as uuid]
[app.config :as cf]
[app.main.data.plugins :as dp]
[app.main.data.profile :as du]
[app.main.data.workspace.mcp :as mcp]
[app.plugins.register :as preg]
[app.util.globals :as globals]
[beicon.v2.core :as rx]
[cljs.test :as t :include-macros true]
[frontend-tests.helpers.async :as a]
[frontend-tests.helpers.mock :as mock]
[potok.v2.core :as ptk]))
(t/deftest ^:async test-initialize-once-per-authenticated-session
(let [stream (rx/subject)
events (atom [])
profile {:id (uuid/next)}]
(await
(mock/with-mocks*
{cf/flags #{:mcp}
preg/wait-for-runtime (fn [] (js/Promise.resolve true))}
(let [subscription (rx/subs! #(swap! events conj (ptk/type %))
(ptk/watch (mcp/initialize) {} stream))]
(try
(rx/push! stream (du/set-profile profile))
(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)) "init event after re-login"))
(t/is (= [:app.main.data.workspace.mcp/init
:app.main.data.workspace.mcp/init] @events))
(finally
(rx/dispose! subscription)
(rx/end! stream))))))))
(t/deftest test-update-mcp-status
(t/testing "enables MCP in profile props and mcp state"
(let [state {:profile {:props {:mcp-enabled false}} :mcp {}}
result (ptk/update (mcp/update-mcp-status true) state)]
(t/is (true? (get-in result [:profile :props :mcp-enabled])))
(t/is (true? (get-in result [:mcp :enabled])))))
(t/testing "disables MCP in profile props and mcp state"
(let [state {:profile {:props {:mcp-enabled true}} :mcp {:enabled true}}
result (ptk/update (mcp/update-mcp-status false) state)]
(t/is (false? (get-in result [:profile :props :mcp-enabled])))
(t/is (false? (get-in result [:mcp :enabled]))))))
(t/deftest test-extension-status
(t/testing "keeps a session the plugin is reconnecting"
(t/is (= "connecting" (mcp/extension-status "disconnected" true))))
(t/testing "reports the disconnect once the user has stopped the connection"
(t/is (= "disconnected" (mcp/extension-status "disconnected" false)))
(t/is (= "disconnected" (mcp/extension-status "disconnected" nil))))
(t/testing "passes other statuses through"
(t/is (= "connected" (mcp/extension-status "connected" true)))
(t/is (= "error" (mcp/extension-status "error" true)))))
(t/deftest test-update-mcp-connection-status
(t/testing "sets connection status to connected"
(let [state {:mcp {:connection-requested true :connection-status "disconnected"}}
result (ptk/update (mcp/update-mcp-connection-status "connected") state)]
(t/is (= "connected" (get-in result [:mcp :connection-status])))))
(t/testing "sets connection status to disconnected"
(let [state {:mcp {:connection-requested true :connection-status "connected"}}
result (ptk/update (mcp/update-mcp-connection-status "disconnected") state)]
(t/is (= "disconnected" (get-in result [:mcp :connection-status]))))))
(t/deftest test-init-sets-enabled
(t/testing "init sets :mcp :enabled to true when profile has mcp-enabled"
(let [state {:mcp {} :profile {:props {:mcp-enabled true}}}
result (ptk/update (mcp/init) state)]
(t/is (true? (get-in result [:mcp :enabled])))))
(t/testing "init sets :mcp :enabled to false when profile has mcp-disabled"
(let [state {:mcp {:enabled true} :profile {:props {:mcp-enabled false}}}
result (ptk/update (mcp/init) state)]
(t/is (false? (get-in result [:mcp :enabled])))))
(t/testing "init sets :mcp :enabled to false when profile has no mcp-enabled prop"
(let [state {:mcp {:enabled true} :profile {:props {}}}
result (ptk/update (mcp/init) state)]
(t/is (false? (get-in result [:mcp :enabled]))))))
(t/deftest test-init-mcp-state
(let [token-id (uuid/next)]
(t/testing "with valid MCP token (future expiration)"
(let [future-date (ct/plus (ct/now) #js {:hours 24})
tokens [{:id token-id :type "mcp" :token "abc123" :expires-at future-date}]
event (#'mcp/init-mcp-state tokens)
state {:mcp {}}
result (ptk/update event state)]
(t/is (= "abc123" (get-in result [:mcp :token])))
(t/is (= token-id (get-in result [:mcp :token-id])))
(t/is (true? (get-in result [:mcp :token-valid])))
;; deref should return the token when valid
(t/is (some? @event))))
(t/testing "with MCP token with no expiration"
(let [tokens [{:id token-id :type "mcp" :token "abc123" :expires-at nil}]
event (#'mcp/init-mcp-state tokens)
state {:mcp {}}
result (ptk/update event state)]
(t/is (= "abc123" (get-in result [:mcp :token])))
(t/is (true? (get-in result [:mcp :token-valid])))
(t/is (some? @event))))
(t/testing "with expired MCP token"
(let [past-date (ct/minus (ct/now) #js {:hours 24})
tokens [{:id token-id :type "mcp" :token "abc123" :expires-at past-date}]
event (#'mcp/init-mcp-state tokens)
state {:mcp {}}
result (ptk/update event state)]
(t/is (= "abc123" (get-in result [:mcp :token])))
(t/is (false? (get-in result [:mcp :token-valid])))
;; deref should return nil when token is expired
(t/is (nil? @event))))
(t/testing "with no MCP token"
(let [tokens [{:id token-id :type nil :token "regular-token"}]
event (#'mcp/init-mcp-state tokens)
state {:mcp {:existing "data"}}
result (ptk/update event state)]
;; state should be unchanged when no MCP token exists
(t/is (= {:mcp {:existing "data"}} result))
(t/is (nil? @event))))
(t/testing "with mixed tokens finds MCP token"
(let [regular-id (uuid/next)
mcp-id (uuid/next)
tokens [{:id regular-id :type nil :token "regular"}
{:id mcp-id :type "mcp" :token "mcp-token" :expires-at nil}]
event (#'mcp/init-mcp-state tokens)
result (ptk/update event {:mcp {}})]
(t/is (= "mcp-token" (get-in result [:mcp :token])))
(t/is (= mcp-id (get-in result [:mcp :token-id])))))))
(t/deftest test-delete-access-token-optimistic-update
(let [token-1 {:id (uuid/next) :name "token-1"}
token-2 {:id (uuid/next) :name "token-2"}
token-3 {:id (uuid/next) :name "token-3"}]
(t/testing "removes token from :access-tokens optimistically"
(let [state {:access-tokens [token-1 token-2 token-3]}
event (du/delete-access-token {:id (:id token-2)})
result (ptk/update event state)]
(t/is (= 2 (count (:access-tokens result))))
(t/is (= [token-1 token-3] (:access-tokens result)))))
(t/testing "state unchanged when token id not found"
(let [state {:access-tokens [token-1 token-2]}
event (du/delete-access-token {:id (uuid/next)})
result (ptk/update event state)]
(t/is (= 2 (count (:access-tokens result))))))))
(t/deftest ^:async test-enable-does-not-connect
(let [events (atom [])]
(await (a/observe (ptk/watch (mcp/update-mcp-status true) {} (rx/empty))
:on-next #(swap! events conj %)))
(t/is (empty? @events))))
(t/deftest ^:async test-connect-is-local
(let [event (mcp/connect-mcp)
state (ptk/update event {:mcp {:enabled true :token-valid true}})
events (atom [])]
(t/is (true? (get-in state [:mcp :connection-requested])))
(await (a/observe (ptk/watch event state (rx/empty))
:on-next #(swap! events conj (ptk/type %))))
(t/is (= [:app.main.data.workspace.mcp/connect] @events))))
(defn- connect
"Runs the effect of a connection request made with the given MCP state."
[mcp-state]
(let [event (mcp/connect-mcp)]
(ptk/effect event (ptk/update event {:mcp mcp-state}) nil)))
(t/deftest ^:async test-session-lock-follows-connection-request
(let [requests (atom [])
released (atom 0)
locks #js {:request (fn [lock-name options callback]
(swap! requests conj [lock-name (.-mode options)])
(.then (callback nil) #(swap! released inc)))}]
(await
(mock/with-mocks*
{globals/navigator #js {:locks locks}}
(connect {:enabled false})
(t/is (empty? @requests))
(connect {:enabled true :token-valid true})
(connect {:enabled true :token-valid true})
(t/is (= [["penpot-mcp-session" "shared"]] @requests))
(ptk/effect (mcp/user-disconnect-mcp) nil nil)
(await (a/settle))
(t/is (= 1 @released))
(ptk/effect (mcp/user-disconnect-mcp) nil nil)
(await (a/settle))
(t/is (= 1 @released))))))
(t/deftest ^:async test-session-lock-retried-after-failed-request
(let [requests (atom 0)
locks #js {:request (fn [_ _ _]
(swap! requests inc)
(js/Promise.reject (js/Error. "lock request denied")))}]
(await
(mock/with-mocks*
{globals/navigator #js {:locks locks}}
(connect {:enabled true :token-valid true})
(await (a/settle))
(connect {:enabled true :token-valid true})
(await (a/settle))
(t/is (= 2 @requests))))))
(t/deftest test-disconnect-clears-connection-intent
(let [state {:mcp {:connection-requested true
:connection-status "connecting"
:session-id "pq3gxqddgj"}}
result (ptk/update (mcp/user-disconnect-mcp) state)]
(t/is (false? (get-in result [:mcp :connection-requested])))
(t/is (= "disconnected" (get-in result [:mcp :connection-status])))
(t/is (nil? (get-in result [:mcp :session-id])))))
(t/deftest test-init-clears-previous-app-connection
(let [state {:profile {:props {:mcp-enabled true}}
:mcp {:connection-requested true
:connection-status "connected"
:session-id "pq3gxqddgj"}}
result (ptk/update (mcp/init) state)]
(t/is (false? (get-in result [:mcp :connection-requested])))
(t/is (= "disconnected" (get-in result [:mcp :connection-status])))
(t/is (nil? (get-in result [:mcp :session-id])))))
(t/deftest test-late-status-cannot-reconnect
(let [state {:mcp {:connection-requested false :connection-status "disconnected"}}]
(t/is (= state (ptk/update (mcp/update-mcp-connection-status "connected") state)))))
(t/deftest test-plugin-callbacks-stop-with-app-session
(doseq [stop-event [:app.main.data.auth/logged-out
:app.main.data.workspace.mcp/init]]
(let [stream (rx/subject)
extension (atom nil)
calls (atom 0)
closed (atom 0)]
(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 ^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 ^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)))))