Alonso Torres b6db09e2fb
✨ Expose the instance URL and query params to plugins (#13018)
Plugins and the MCP could not learn which Penpot instance they run on,
so agents guessed the domain when they wrote links to files.

Add penpot.location with the instance base URL (baseUrl), the URL of
the current screen (href) and its query params (params). It works in
the dashboard and the workspace and requires content:read. Params that
carry secrets (share-id, the MCP authorize params, and any key ending
in token, secret or password) are left out, and href is rebuilt from
the remaining params so it never carries one.

The MCP instructions tell agents to use href instead of building links.
The plugin API test suite checks location against the open file, and
its API surface is regenerated from the current types.

Closes #13013

AI-assisted-by: claude-opus-5-5
2026-11-19 10:27:04 +01:00

74 lines
2.8 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.plugins.location-test
(:require
[app.common.data.macros :as dm]
[app.common.uri :as u]
[app.config :as cf]
[app.plugins.location :as location]
[cljs.test :as t :include-macros true]))
(defn- state-with-query
[query-params]
{:route {:data {:name (keyword (:screen query-params))}
:params {:path {} :query query-params}
:query-params query-params}})
(defn- href-params
[href]
(-> (u/uri href) :query u/query-string->map))
(t/deftest base-url-is-the-public-uri
(let [result (location/location (state-with-query {:screen "dashboard-recent"}))]
(t/is (= (dm/str cf/public-uri) (:baseUrl result)))))
(t/deftest workspace-params-are-exposed
(let [query {:screen "workspace"
:team-id "t1"
:file-id "f1"
:page-id "p1"
:layout "layers"}
result (location/location (state-with-query query))]
(t/is (= {"screen" "workspace"
"team-id" "t1"
"file-id" "f1"
"page-id" "p1"
"layout" "layers"}
(:params result)))
(t/is (= query (href-params (:href result))))
(t/is (= (dm/str cf/public-uri) (subs (:href result) 0 (count (dm/str cf/public-uri)))))))
(t/deftest custom-params-are-exposed
(let [result (location/location (state-with-query {:screen "workspace" :my-plugin "on"}))]
(t/is (= "on" (get-in result [:params "my-plugin"])))))
(t/deftest secret-params-are-left-out
(let [query {:screen "workspace"
:file-id "f1"
:token "s1"
:invitation-token "s2"
:share-id "s3"
:request "s4"
:code "s5"
:state "s6"
:client-secret "s7"
:password "s8"}
result (location/location (state-with-query query))]
(t/is (= {"screen" "workspace" "file-id" "f1"} (:params result)))
(t/is (= {:screen "workspace" :file-id "f1"} (href-params (:href result))))
(t/is (not (re-find #"s\d" (:href result))))))
(t/deftest repeated-params-keep-the-last-value
(let [result (location/location (state-with-query {:screen "workspace" :page-id ["p1" "p2"]}))]
(t/is (= "p2" (get-in result [:params "page-id"])))
(t/is (= "p2" (:page-id (href-params (:href result)))))))
(t/deftest no-route-returns-the-base-url
(let [result (location/location {})]
(t/is (= {} (:params result)))
(t/is (= (:baseUrl result) (:href result)))))