The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:
TypeScript analyticsbibusiness-intelligencebusinessintelligenceclojuredashboarddatadata-analysisdata-visualizationdatabasemetabasemysqlpostgrespostgresqlreportingslacksql-editorvisualization
Check cards in the dashboard op compiler (#85799)
Chris Truter078f4c2aa6e7
diff --git a/.clj-kondo/config.edn b/.clj-kondo/config.ednindex 6186a085831..6bbfe27ec28 100644--- a/.clj-kondo/config.edn+++ b/.clj-kondo/config.edn@@ -731,7 +731,6 @@metabase.mcp.v2.common/list-content hooks.metabase.agent-message/lint-list-contentmetabase.mcp.v2.common/success-content hooks.metabase.agent-message/lint-success-contentmetabase.mcp.v2.common/throw-teaching-error hooks.metabase.agent-message/lint-teaching-exit- metabase.mcp.v2.dashboard-errors/op-error! hooks.metabase.agent-message/lint-op-errormetabase.mcp.v2.message/msg hooks.metabase.agent-message/lint-msgmetabase.mcp.v2.tools.browse/paged-list-content hooks.metabase.agent-message/lint-list-contentmetabase.mcp.v2.tools.parameters-test/with-fixtures hooks.common/with-one-bindingdiff --git a/.clj-kondo/src/hooks/metabase/agent_message.clj b/.clj-kondo/src/hooks/metabase/agent_message.cljindex 9d296390eff..1ed8efec8e3 100644--- a/.clj-kondo/src/hooks/metabase/agent_message.clj+++ b/.clj-kondo/src/hooks/metabase/agent_message.clj@@ -309,13 +309,6 @@(lint-exit-arg! node 3))input)-(defn lint-op-error- "Flag an `op-error!` whose detail message, the third argument, is text rather than a `msg`."- [{:keys [node] :as input}]- (when (exit-enabled? input)- (lint-exit-arg! node 3))- input)-(defn lint-ellipsize"Flag an `ellipsize` whose message, the first argument, is text rather than a `msg`."[{:keys [node] :as input}]diff --git a/.clj-kondo/test/hooks/metabase/agent_message_test.clj b/.clj-kondo/test/hooks/metabase/agent_message_test.cljindex d5330863264..38771c48097 100644--- a/.clj-kondo/test/hooks/metabase/agent_message_test.clj+++ b/.clj-kondo/test/hooks/metabase/agent_message_test.clj@@ -38,7 +38,6 @@[agent-message/lint-jsonrpc-error '(jsonrpc-error id -32602 "x")][agent-message/lint-success-content '(success-content "x")][agent-message/lint-ex-info "(ex-info \"x\" {:status-code 404})"]- [agent-message/lint-op-error '(op-error! idx "add_link" "x")][agent-message/lint-ellipsize '(ellipsize "x" 300)][agent-message/lint-list-content '(list-content data total {:empty-hint "x"})]])@@ -261,8 +260,7 @@(def ^:private helper-cases"`[hook fn-name form-fn]`: a message-taking helper's hook, the name its finding cites, and a function of a messagereturning a call that passes it to the helper."- [[agent-message/lint-op-error "op-error!" #(list 'dashboard-ops/op-error! 'idx "add_link" %)]- [agent-message/lint-ellipsize "ellipsize" #(list 'common/ellipsize % 300)]+ [[agent-message/lint-ellipsize "ellipsize" #(list 'common/ellipsize % 300)][agent-message/lint-list-content "list-content" #(list 'common/list-content 'rows 0 {:offset 0 :empty-hint %})][agent-message/lint-list-content "paged-list-content"#(list 'paged-list-content 'args 'rows {:empty-hint %} 'identity)]])@@ -279,7 +277,6 @@(testing (pr-str text)(is (empty? (findings hook (form-fn text))))))))(testing "only the message argument is checked"- (is (empty? (findings agent-message/lint-op-error '(op-error! (str "0") (str "op") (msg ["x"])))))(is (empty? (findings agent-message/lint-ellipsize '(ellipsize (msg ["x"]) (str "300")))))(is (empty? (findings agent-message/lint-list-content '(list-content (str "data") 0 {:param (str "x")}))))(is (empty? (findings agent-message/lint-list-content '(list-content data 0 opts))))diff --git a/src/metabase/mcp/v2/dashboard_errors.clj b/src/metabase/mcp/v2/dashboard_errors.cljindex 4095f1e2d53..e1362341a6e 100644--- a/src/metabase/mcp/v2/dashboard_errors.clj+++ b/src/metabase/mcp/v2/dashboard_errors.clj@@ -33,6 +33,9 @@:unknown-dashcard(fn [{:keys [dashcard-id]}] (message/msg ["no dashcard with id %s on this dashboard."] dashcard-id))+ :unknown-card+ (fn [{:keys [card-id]}] (message/msg ["no card with id %s that you can read."] card-id))+:missing-tab-id(fn [_] (message/raw "missing `tab_id`."))@@ -40,7 +43,7 @@(fn [{:keys [tab-id]}] (message/msg ["no tab with id %s on this dashboard."] tab-id)):no-free-space- (fn [_] (message/raw "No free space on this tab for another card — remove or resize something first."))+ (fn [_] (message/raw "no free space on this tab for another card — remove or resize something first.")):link-needs-url-or-entity(fn [_] (message/raw "pass exactly one of `url` or `entity`."))@@ -141,47 +144,21 @@(defn- op-message"The message `detail` attributed to the op at `idx` (0-based, as sent): `op <idx> (<op-name>): <detail>`.Without an `op-name` it is `op <idx>: <detail>`.- A string `op-name` came from the caller, so it renders quoted; a raw message renders as it is."+ The op name came from the caller, so it renders quoted."[idx op-name detail](if (nil? op-name)(message/msg ["op %d: %s"] idx detail)(message/msg ["op %d (%s): %s"] idx op-name detail)))-(defn op-error!- "Throw a teaching error attributing `detail`, a message, to the op at `idx` named `op-name`."- [idx op-name detail]- (common/throw-teaching-error (op-message idx op-name detail)))--(def ^:private tab-ops- "The ops whose `tab_id` names the tab they act on."- #{"duplicate_tab"- "move_tab"- "remove_tab"- "rename_tab"})--(defn- op-label- "What the op prefix names for error `code` from the op called `op-name`.- That is the op name, except that a missing or unknown `dashcard_id`, `tab_id` or `parameter_id` names the key.- An existing parameter reports `add_parameter` unquoted.- MCP's errors read that way before the compiler moved, and this keeps them unchanged."- [code op-name]- (case code- (:missing-dashcard-id :unknown-dashcard) "dashcard_id"- (:missing-parameter-id :unknown-parameter) "parameter_id"- :missing-tab-id "tab_id"- :unknown-tab (if (contains? tab-ops op-name) "tab_id" op-name)- :parameter-exists (message/raw "add_parameter")- op-name))-(defn error-message"The teaching message for compiler error `data`, as read by [[dashboard-ops/error-data]].- An error with no `:op-index`, or a `:no-free-space` error, has no op prefix."+ An error with no `:op-index` has no op prefix."[{code ::dashboard-ops/error :keys [op-index op-name] :as data}](let [detail-fn (or (get details code)(throw (ex-info (str "No MCP wording for dashboard op error " code) {:code code})))detail (detail-fn data)]- (if (and op-index (not= :no-free-space code))- (op-message op-index (op-label code op-name) detail)+ (if op-index+ (op-message op-index op-name detail);; A bare raw message renders quoted, so wrap it to keep it as text.(message/msg ["%s"] detail))))diff --git a/src/metabase/mcp/v2/tools/dashboard.clj b/src/metabase/mcp/v2/tools/dashboard.cljindex 6e5bbe82c17..2095882049f 100644--- a/src/metabase/mcp/v2/tools/dashboard.clj+++ b/src/metabase/mcp/v2/tools/dashboard.clj@@ -29,6 +29,7 @@[metabase.mcp.v2.resolve :as v2.resolve][metabase.mcp.v2.write :as v2.write][metabase.metabot.scope :as metabot.scope]+ [metabase.metabot.toolkit.dashboard-ops :as dashboard-ops][metabase.models.interface :as mi][metabase.util :as u][metabase.util.malli.registry :as mr]@@ -49,14 +50,13 @@any named by an op."[dash ops](into (set (keep :card_id (:dashcards dash)))- ;; `:series` is add_card's series list, `:card_ids` set_series'. Both name cards the- ;; projection reads back, so a missed one reaches it as a bare `{:id n}` map.- (mapcat (fn [op] (concat [(:card_id op)] (:series op) (:card_ids op))))+ (mapcat dashboard-ops/card-refs)ops))(defn- fetch-cards- "`{card-id card}` for `ids`, behind read checks. A card the caller cannot read is omitted, so- an op referencing it fails with a teaching error rather than leaking its existence."+ "The `{card-id card}` map for `ids`, behind read checks.+ It omits a card the caller cannot read, so an op naming one fails the compiler's `:unknown-card` check.+ That is the same error as for a card that does not exist."[ids](let [ids (into #{} (filter int?) ids)](when (seq ids)@@ -64,16 +64,6 @@:when (mi/can-read? card)][(:id card) card])))))-(defn- check-cards-exist!- "Reject an op naming a card the caller cannot read, before any write happens. Covers series- cards too: they are placed on the dashboard just as `card_id` is, and leaving them unchecked- would both skip the permission boundary and hand the projection a card it never fetched."- [ops cards]- (doseq [[idx op] (map-indexed vector ops)- card-id (concat [(:card_id op)] (:series op) (:card_ids op))- :when (and card-id (not (contains? cards card-id)))]- (dashboard-errors/op-error! idx (:op op) (message/msg ["no card with id %s that you can read."] card-id))))-;;; ------------------------------------------------ Response ------------------------------------------------------(defn- saved-row@@ -431,7 +421,6 @@[dash ops attrs validate-only?](let [ops (mapv compact-op ops)cards (fetch-cards (referenced-card-ids dash ops))- _ (check-cards-exist! ops cards)payload (normalize-parameters (dashboard-errors/compile-ops dash ops cards))](validate-payload! (merge attrs payload))(if validate-only?diff --git a/src/metabase/metabot/toolkit/dashboard_ops.clj b/src/metabase/metabot/toolkit/dashboard_ops.cljindex 737eeb31e6e..bcda185a02b 100644--- a/src/metabase/metabot/toolkit/dashboard_ops.clj+++ b/src/metabase/metabot/toolkit/dashboard_ops.clj@@ -40,6 +40,7 @@:duplicate-new-id [:map {:closed true} [:id :int]]:missing-dashcard-id [:map {:closed true}]:unknown-dashcard [:map {:closed true} [:dashcard-id :any]]+ :unknown-card [:map {:closed true} [:card-id :any]]:missing-tab-id [:map {:closed true}]:unknown-tab [:map {:closed true} [:tab-id :any]]:no-free-space [:map {:closed true} [:tab-id [:maybe :int]]]@@ -132,12 +133,12 @@(update state coll (partial filterv #(not= id (:id %)))))(defn- move-row- "Move the row with `id` so it sits at `index` among the remaining rows; nil when `index` falls- outside `[0, count-after-removal]`."+ "Move the row with `id` so it sits at `index` among the remaining rows.+ Nil when `index` is not an integer in `[0, count-after-removal]`."[state coll id index](let [row (find-row state coll id)rest* (filterv #(not= id (:id %)) (get state coll))]- (when (<= 0 index (count rest*))+ (when (and (integer? index) (<= 0 index (count rest*)))(assoc state coll (vec (concat (subvec rest* 0 index) [row] (subvec rest* index)))))))(defn- map-rows@@ -537,6 +538,8 @@(defmethod apply-op "add_parameter"[state {:keys [parameter_id] :as op}]+ (when (nil? parameter_id)+ (fail! :missing-parameter-id))(when (find-row state :parameters parameter_id)(fail! :parameter-exists {:parameter-id parameter_id}))(insert-row state :parameters@@ -734,27 +737,52 @@;;; ------------------------------------------------ Entry ---------------------------------------------------------+(defn card-refs+ "The ids of the cards `op` references.+ Those are the cards it puts on the dashboard, and the card a parameter it adds or updates draws its values from.+ The caller resolves these into the `cards` map for [[compile-ops]], which rejects any it left out."+ [op]+ (remove nil?+ (case (:op op)+ "add_card" (cons (:card_id op) (:series op))+ "replace_card" [(:card_id op)]+ "set_series" (:card_ids op)+ ("add_parameter" "update_parameter") [(get-in op [:values_source_config :card_id])]+ nil)))++(defn- check-cards-resolved!+ "Every card `op` references must be in the caller's `cards` map.+ The caller leaves out the cards its user cannot read, so this is where an op naming one fails."+ [state op]+ (doseq [card-id (card-refs op)+ :when (not (contains? (::cards state) card-id))]+ (fail! :unknown-card {:card-id card-id})))+(defn- apply-op-at- "Runs [[apply-op]] on `op`, the op at `idx`.+ "Runs [[apply-op]] on `op`, the op at `idx`, then checks that the cards it references are resolved.Adds `:op-index` and `:op-name` to any error it throws."[state idx op](try- (apply-op state op)+ ;; The card check runs second, so an op that is malformed in some other way reports that instead.+ (doto (apply-op state op)+ (check-cards-resolved! op))(catch clojure.lang.ExceptionInfo e(throw (if-let [data (error-data e)](ex-info (ex-message e) (assoc data :op-index idx :op-name (:op op)) e)e)))))(defn compile-ops- "Fold `ops` over `current` (a dashboard hydrated with `[:dashcards :series :card] :tabs`) and- return `{:dashcards :tabs :parameters}` — the full-replacement payload `update-dashboard!`- saves. New rows carry the caller's negative ids. `cards` maps every card id the ops may touch- to its card row; this namespace does no I/O, so the caller resolves that. Throws an error from- [[errors]] on the first invalid op; read it with [[error-data]]."- ([current ops] (compile-ops current ops {}))- ([current ops cards]- (-> (reduce-kv apply-op-at- (init-state current cards)- (vec ops))- check-tab-coverage!- (select-keys [:dashcards :tabs :parameters]))))+ "Fold `ops` over `current`, a dashboard hydrated with `[:dashcards :series :card] :tabs`.+ Returns `{:dashcards :tabs :parameters}`, the full-replacement payload `update-dashboard!` saves.+ New rows carry the caller's negative ids.+ The `cards` map holds the row of each card the ops may touch, keyed by id.+ It covers the cards [[card-refs]] finds in `ops` and the cards already on the dashboard.+ It leaves out cards the user cannot read.+ This namespace does no I/O, so the caller resolves them.+ Throws an error from [[errors]] on the first invalid op; read it with [[error-data]]."+ [current ops cards]+ (-> (reduce-kv apply-op-at+ (init-state current cards)+ (vec ops))+ check-tab-coverage!+ (select-keys [:dashcards :tabs :parameters])))diff --git a/test/metabase/mcp/v2/dashboard_errors_test.clj b/test/metabase/mcp/v2/dashboard_errors_test.cljindex b10f74101ce..ebe5f10e791 100644--- a/test/metabase/mcp/v2/dashboard_errors_test.clj+++ b/test/metabase/mcp/v2/dashboard_errors_test.clj@@ -8,7 +8,8 @@[metabase.mcp.v2.dashboard-errors :as dashboard-errors][metabase.mcp.v2.message :as message][metabase.metabot.toolkit.dashboard-ops :as dashboard-ops]- [metabase.test :as mt]))+ [metabase.test :as mt]+ [metabase.util :as u]))(set! *warn-on-reflection* true)@@ -16,13 +17,16 @@{:id 1 :dashcards [] :tabs [] :parameters []})(defn- error-text- "The teaching error text MCP returns for `ops` against `current`, or nil when they compile."- [current ops]- (try- (dashboard-errors/compile-ops current ops {})- nil- (catch clojure.lang.ExceptionInfo e- (ex-message e))))+ "The teaching error text MCP returns for `ops` against `current` and `cards`, or nil when they compile.+ It defaults `cards` to a stub row for every card `ops` name."+ ([current ops]+ (error-text current ops (u/for-map [id (mapcat dashboard-ops/card-refs ops)] [id {:id id}])))+ ([current ops cards]+ (try+ (dashboard-errors/compile-ops current ops cards)+ nil+ (catch clojure.lang.ExceptionInfo e+ (ex-message e)))))(defn- rendered"The teaching message text for compiler error `data`."@@ -49,28 +53,31 @@(testing "GHY-4544: an op sent without its `op` or a referenced id says so rather than naming null"(is (= "op 0: missing `op` — see the tool description for the supported list."(error-text empty-dash [{:id -1}])))- (is (= "op 0 (\"dashcard_id\"): missing `dashcard_id`."+ (is (= "op 0 (\"remove\"): missing `dashcard_id`."(error-text empty-dash [{:op "remove"}])))- (is (= "op 0 (\"tab_id\"): missing `tab_id`."+ (is (= "op 0 (\"remove_tab\"): missing `tab_id`."(error-text empty-dash [{:op "remove_tab"}])))- (is (= "op 0 (\"parameter_id\"): missing `parameter_id`."+ (is (= "op 0 (\"remove_parameter\"): missing `parameter_id`."(error-text empty-dash [{:op "remove_parameter"}]))))- (testing "an unknown dashcard, tab or parameter id names the key rather than the op"- (is (= "op 0 (\"dashcard_id\"): no dashcard with id 999 on this dashboard."+ (testing "an unknown dashcard, tab or parameter id names the op"+ (is (= "op 0 (\"duplicate_card\"): no dashcard with id 999 on this dashboard."(error-text empty-dash [{:op "duplicate_card" :id -1 :dashcard_id 999}])))- (is (= "op 0 (\"tab_id\"): no tab with id 99 on this dashboard."+ (is (= "op 0 (\"rename_tab\"): no tab with id 99 on this dashboard."(error-text empty-dash [{:op "rename_tab" :tab_id 99 :name "Q3"}])))- (is (= "op 0 (\"parameter_id\"): no parameter with id \"nope\" on this dashboard."+ (is (= "op 0 (\"remove_parameter\"): no parameter with id \"nope\" on this dashboard."(error-text empty-dash [{:op "remove_parameter" :parameter_id "nope"}]))))- (testing "a card placed on an unknown tab names the op"+ (testing "a card placed on an unknown tab names the op too"(is (= "op 0 (\"add_text\"): no tab with id 77 on this dashboard."(error-text empty-dash [{:op "add_text" :id -1 :markdown "hi" :tab 77}]))))- (testing "add_parameter names itself unquoted when the parameter exists"- (is (= "op 0 (add_parameter): parameter \"p1\" already exists — use \"update_parameter\"."+ (testing "add_parameter names itself quoted, like every op, when the parameter exists"+ (is (= "op 0 (\"add_parameter\"): parameter \"p1\" already exists — use \"update_parameter\"."(error-text (assoc empty-dash :parameters [{:id "p1"}])[{:op "add_parameter" :parameter_id "p1"}]))))- (testing "a tab with no free space has no op prefix"- (is (= "No free space on this tab for another card — remove or resize something first."+ (testing "a card the caller could not resolve reads as one the user cannot read"+ (is (= "op 0 (\"add_card\"): no card with id 42 that you can read."+ (error-text empty-dash [{:op "add_card" :id -1 :card_id 42}] {}))))+ (testing "a tab with no free space names the op that ran out of room"+ (is (= "op 0 (\"add_card\"): no free space on this tab for another card — remove or resize something first."(error-text (assoc empty-dash :dashcards [{:id 7 :card_id 9 :row 0 :col 0 :size_x 24 :size_y 1100}])[{:op "add_card" :id -1 :card_id 42}]))))(testing "an error found after the last op has no op prefix"diff --git a/test/metabase/mcp/v2/tools/dashboard_test.clj b/test/metabase/mcp/v2/tools/dashboard_test.cljindex 5ee9cf71d0e..06beb859d85 100644--- a/test/metabase/mcp/v2/tools/dashboard_test.clj+++ b/test/metabase/mcp/v2/tools/dashboard_test.clj@@ -386,6 +386,21 @@(wire {:method "update" :id (:id dash):ops [{:op "add_card" :id -1 :card_id 9999999}]}))))))))+(deftest dry-run-rejects-an-unreadable-parameter-source-card-test+ (testing "validate_only rejects a parameter drawing its values from a card the user cannot read, as the save does"+ (mt/with-temp [:model/Dashboard dash {:name "Sales"}]+ (is (= "op 0 (\"add_parameter\"): no card with id 9999999 that you can read."+ (tool-error (call-tool! :crowberto v2.tu/all-scopes "dashboard_write"+ (wire {:method "update"+ :id (:id dash)+ :validate_only true+ :ops [{:op "add_parameter"+ :parameter_id "p"+ :name "P"+ :type "string/="+ :values_source_type "card"+ :values_source_config {:card_id 9999999}}]}))))))))+(deftest archived-on-create-error-text-test(testing "GHY-4544: `archived` on a create is a teaching error"(is (= "\"archived\" applies to method \"update\" only — remove it from this create call."diff --git a/test/metabase/metabot/toolkit/dashboard_ops_test.clj b/test/metabase/metabot/toolkit/dashboard_ops_test.cljindex 466a8a86c7e..6c2c47ecf23 100644--- a/test/metabase/metabot/toolkit/dashboard_ops_test.clj+++ b/test/metabase/metabot/toolkit/dashboard_ops_test.clj@@ -7,6 +7,7 @@[metabase.metabot.toolkit.dashboard-ops :as dashboard-ops][metabase.parameters.mapping-targets :as mapping-targets][metabase.test.util.dynamic-redefs :as dynamic-redefs]+ [metabase.util :as u][metabase.util.malli.registry :as mr]))(set! *warn-on-reflection* true)@@ -18,6 +19,19 @@[dashcards](assoc empty-dash :dashcards dashcards))+(defn- resolved-cards+ "A bare card row for each card `ops` name, as a caller that can read them all resolves them."+ [ops]+ (u/for-map [id (mapcat dashboard-ops/card-refs ops)] [id {:id id}]))++;; TODO (Chris 2026-10-29) -- a name unique to this helper would be less ambiguous. It shares its name with+;; [[dashboard-ops/compile-ops]], but also resolves a stub row for every card the ops name.+(defn- compile-ops+ "Runs [[dashboard-ops/compile-ops]] with every card `ops` name resolved.+ The optional `cards` adds or replaces card rows."+ ([current ops] (compile-ops current ops {}))+ ([current ops cards] (dashboard-ops/compile-ops current ops (merge (resolved-cards ops) cards))))+(defn- inline-parameter-owners"`{parameter-id [dashcard-id ...]}` for every inline placement in a compiled payload. A parameterwith more than one owner is the corruption GHY-4326 is about."@@ -32,7 +46,7 @@(deftest add-card-autoplaces-test(testing "GHY-4147: add_card with no position lands at the top-left of an empty dashboard"- (let [{:keys [dashcards]} (dashboard-ops/compile-ops+ (let [{:keys [dashcards]} (compile-opsempty-dash[{:op "add_card" :id -1 :card_id 42}])](is (= 1 (count dashcards)))@@ -43,7 +57,7 @@(deftest add-card-respects-explicit-position-and-size-test(testing "GHY-4147: explicit position and size are passed through untouched"- (let [{:keys [dashcards]} (dashboard-ops/compile-ops+ (let [{:keys [dashcards]} (compile-opsempty-dash[{:op "add_card" :id -1 :card_id 42:position {:row 3 :col 4}@@ -54,7 +68,7 @@(deftest existing-dashcards-are-preserved-test(testing "GHY-4147: the payload is a full replacement, so untouched dashcards survive verbatim"(let [existing {:id 7 :card_id 9 :row 0 :col 0 :size_x 4 :size_y 4 :dashboard_tab_id nil}- {:keys [dashcards]} (dashboard-ops/compile-ops+ {:keys [dashcards]} (compile-ops(dash-with [existing])[{:op "add_card" :id -1 :card_id 42}])](is (= #{7 -1} (set (map :id dashcards))))@@ -63,17 +77,18 @@(deftest autoplace-avoids-existing-cards-test(testing "GHY-4147: autoplace does not overlap an occupied slot"(let [existing {:id 7 :card_id 9 :row 0 :col 0 :size_x 24 :size_y 4 :dashboard_tab_id nil}- {:keys [dashcards]} (dashboard-ops/compile-ops+ {:keys [dashcards]} (compile-ops(dash-with [existing])[{:op "add_card" :id -1 :card_id 42}])added (first (filter #(= -1 (:id %)) dashcards))](is (<= 4 (:row added))))))(defn- op-error- "The error data `compile-ops` throws for `ops` against `current`, or nil when it compiles.+ "The error data [[dashboard-ops/compile-ops]] throws for `ops` against `current` and `cards`, or nil when it compiles.+ It defaults `cards` to a stub row for every card `ops` name.It also checks the data against the schema [[dashboard-ops/errors]] declares for its code.It rethrows an exception that is not a compiler error, so the test reports it as it is."- ([current ops] (op-error current ops {}))+ ([current ops] (op-error current ops (resolved-cards ops)))([current ops cards](try(dashboard-ops/compile-ops current ops cards)@@ -119,13 +134,18 @@(is (=? {::dashboard-ops/error :missing-tab-id, :op-name "remove_tab"}(op-error empty-dash [{:op "remove_tab"}])))(is (=? {::dashboard-ops/error :missing-parameter-id, :op-name "remove_parameter"}- (op-error empty-dash [{:op "remove_parameter"}])))))+ (op-error empty-dash [{:op "remove_parameter"}])))+ (is (=? {::dashboard-ops/error :missing-parameter-id, :op-name "add_parameter"}+ (op-error empty-dash [{:op "add_parameter" :name "Country" :type "string/="}])))))(deftest ^:parallel out-of-range-index-test- (testing "move_tab and move_parameter report an index past the end with the count it must stay within"+ (testing "move_tab and move_parameter reject a missing or out-of-range index, giving the count"(is (=? {::dashboard-ops/error :tab-index-out-of-range, :index 3, :tab-count 1}(op-error (assoc empty-dash :tabs [{:id 1 :name "A"}])[{:op "move_tab" :tab_id 1 :index 3}])))+ (is (=? {::dashboard-ops/error :tab-index-out-of-range, :index nil, :tab-count 1}+ (op-error (assoc empty-dash :tabs [{:id 1 :name "A"}])+ [{:op "move_tab" :tab_id 1}])))(is (=? {::dashboard-ops/error :parameter-index-out-of-range, :index 4, :parameter-count 1}(op-error (assoc empty-dash :parameters [{:id "p1"}])[{:op "move_parameter" :parameter_id "p1" :index 4}])))))@@ -144,7 +164,7 @@(deftest add-text-test(testing "GHY-4147: add_text produces a virtual text dashcard with no card_id"- (let [{:keys [dashcards]} (dashboard-ops/compile-ops+ (let [{:keys [dashcards]} (compile-opsempty-dash[{:op "add_text" :id -1 :markdown "## Hello"}])dc (first dashcards)]@@ -154,7 +174,7 @@(deftest add-heading-test(testing "GHY-4147: add_heading matches the editor, including the transparent background"- (let [{:keys [dashcards]} (dashboard-ops/compile-ops+ (let [{:keys [dashcards]} (compile-opsempty-dash[{:op "add_heading" :id -1 :text "Revenue"}])vs (:visualization_settings (first dashcards))]@@ -164,7 +184,7 @@(deftest add-link-url-test(testing "GHY-4147: add_link with an external url"- (let [{:keys [dashcards]} (dashboard-ops/compile-ops+ (let [{:keys [dashcards]} (compile-opsempty-dash[{:op "add_link" :id -1 :url "https://example.com"}])vs (:visualization_settings (first dashcards))]@@ -173,7 +193,7 @@(deftest add-link-entity-test(testing "GHY-4147: add_link with an entity reference stores the entity, not a url"- (let [{:keys [dashcards]} (dashboard-ops/compile-ops+ (let [{:keys [dashcards]} (compile-opsempty-dash[{:op "add_link" :id -1 :entity {:type "dashboard" :id 12}}])vs (:visualization_settings (first dashcards))]@@ -190,7 +210,7 @@(deftest add-iframe-test(testing "GHY-4147: add_iframe stores the src in visualization settings"- (let [{:keys [dashcards]} (dashboard-ops/compile-ops+ (let [{:keys [dashcards]} (compile-opsempty-dash[{:op "add_iframe" :id -1 :src "https://example.com/embed"}])vs (:visualization_settings (first dashcards))]@@ -199,7 +219,7 @@(deftest add-action-test(testing "GHY-4147: add_action produces an action dashcard with action_id and no card_id"- (let [{:keys [dashcards]} (dashboard-ops/compile-ops+ (let [{:keys [dashcards]} (compile-opsempty-dash[{:op "add_action" :id -1 :action_id 3 :label "Run" :display "button"}])dc (first dashcards)]@@ -212,7 +232,7 @@(testing "GHY-4147: add_action writes a :virtual_card so the dashcard reads as an action";; `isActionDashCard` (frontend/src/metabase/actions/utils.ts) keys off;; visualization_settings.virtual_card.display; without it the dashcard renders empty, not as a button.- (let [{:keys [dashcards]} (dashboard-ops/compile-ops+ (let [{:keys [dashcards]} (compile-opsempty-dash[{:op "add_action" :id -1 :action_id 3 :label "Run"}])vs (:visualization_settings (first dashcards))]@@ -226,7 +246,7 @@(let [existing {:id 7 :card_id 9 :row 0 :col 0 :size_x 4 :size_y 4 :dashboard_tab_id nil:visualization_settings {:card.title "Original"}:parameter_mappings [{:parameter_id "p1" :card_id 9 :target ["dimension" ["field" 1 nil]]}]}- {:keys [dashcards]} (dashboard-ops/compile-ops+ {:keys [dashcards]} (compile-ops(dash-with [existing])[{:op "duplicate_card" :id -1 :dashcard_id 7}])clone (first (filter #(= -1 (:id %)) dashcards))]@@ -250,7 +270,7 @@:visualization_settings{:visualization "table":columnValuesMapping {:COLUMN_1 [{:sourceId "card:7" :originalName "NAME" :name "COLUMN_1"}]}}}- {:keys [dashcards]} (dashboard-ops/compile-ops+ {:keys [dashcards]} (compile-ops(dash-with [existing])[{:op "duplicate_card" :id -1 :dashcard_id 7}])clone (first (filter #(= -1 (:id %)) dashcards))]@@ -267,7 +287,7 @@:visualization_settings {}:inline_parameters ["p1"]:parameter_mappings [mapping]}- {:keys [dashcards]} (dashboard-ops/compile-ops+ {:keys [dashcards]} (compile-ops(assoc empty-dash:dashcards [existing]:parameters [{:id "p1" :name "Cat" :slug "cat" :type "string/="}])@@ -295,7 +315,7 @@{:id 8 :card_id 9 :row 4 :col 0 :size_x 4 :size_y 4:dashboard_tab_id 5 :visualization_settings {}:inline_parameters ["p2"]}]}- {:keys [dashcards]} (dashboard-ops/compile-ops+ {:keys [dashcards]} (compile-opscurrent[{:op "duplicate_tab" :id -1 :tab_id 5}])clones (filterv #(= -1 (:dashboard_tab_id %)) dashcards)]@@ -313,7 +333,7 @@(deftest replace-card-resets-content-test(testing "GHY-4147: replace_card keeps the dashcard id and resets series, mappings, and viz settings"- (let [{:keys [dashcards]} (dashboard-ops/compile-ops+ (let [{:keys [dashcards]} (compile-ops(dash-with [a-dashcard])[{:op "replace_card" :dashcard_id 7 :card_id 99}])dc (first dashcards)]@@ -327,7 +347,7 @@(deftest move-and-resize-test(testing "GHY-4147: move relocates, resize resizes, neither disturbs the other"- (let [{:keys [dashcards]} (dashboard-ops/compile-ops+ (let [{:keys [dashcards]} (compile-ops(dash-with [a-dashcard])[{:op "move" :dashcard_id 7 :position {:row 0 :col 0}}{:op "resize" :dashcard_id 7 :size {:size_x 8 :size_y 2}}])@@ -339,7 +359,7 @@moved card is not its own sibling — counting it means the search can never return theslot it already occupies, so a card alone on a tab is displaced by its own footprint"(let [wide {:id 7 :card_id 9 :row 0 :col 0 :size_x 24 :size_y 4 :dashboard_tab_id nil}- {:keys [dashcards]} (dashboard-ops/compile-ops+ {:keys [dashcards]} (compile-ops(dash-with [wide])[{:op "move" :dashcard_id 7}])dc (first dashcards)]@@ -347,21 +367,21 @@(deftest remove-test(testing "GHY-4147: remove drops the dashcard from the payload, which deletes it on save"- (let [{:keys [dashcards]} (dashboard-ops/compile-ops+ (let [{:keys [dashcards]} (compile-ops(dash-with [a-dashcard])[{:op "remove" :dashcard_id 7}])](is (= [] dashcards)))))(deftest set-series-test(testing "GHY-4147: set_series is an ordered full replace"- (let [{:keys [dashcards]} (dashboard-ops/compile-ops+ (let [{:keys [dashcards]} (compile-ops(dash-with [a-dashcard])[{:op "set_series" :dashcard_id 7 :card_ids [21 20]}])](is (= [{:id 21} {:id 20}] (:series (first dashcards)))))))(deftest patch-dashcard-merges-content-test(testing "GHY-4147: patch_dashcard merges into visualization_settings rather than replacing them"- (let [{:keys [dashcards]} (dashboard-ops/compile-ops+ (let [{:keys [dashcards]} (compile-ops(dash-with [a-dashcard])[{:op "patch_dashcard" :dashcard_id 7:patch {:visualization_settings {:card.description "New"}}}])@@ -390,7 +410,7 @@(deftest ops-apply-in-order-test(testing "GHY-4147: a later op sees the effect of an earlier one — add then move the same new card"- (let [{:keys [dashcards]} (dashboard-ops/compile-ops+ (let [{:keys [dashcards]} (compile-opsempty-dash[{:op "add_card" :id -1 :card_id 42}{:op "move" :dashcard_id -1 :position {:row 9 :col 1}}])]@@ -398,7 +418,7 @@(deftest add-tab-and-place-a-card-in-it-test(testing "GHY-4147: a card can reference a tab created earlier in the same batch by its negative id"- (let [{:keys [tabs dashcards]} (dashboard-ops/compile-ops+ (let [{:keys [tabs dashcards]} (compile-opsempty-dash[{:op "add_tab" :id -1 :name "Q3"}{:op "add_card" :id -2 :card_id 42 :tab -1}])]@@ -407,7 +427,7 @@(deftest rename-tab-test(testing "GHY-4147: rename_tab changes only the name"- (let [{:keys [tabs]} (dashboard-ops/compile-ops+ (let [{:keys [tabs]} (compile-ops(assoc empty-dash :tabs [{:id 5 :name "Old" :position 0}])[{:op "rename_tab" :tab_id 5 :name "New"}])](is (= "New" (:name (first tabs))))@@ -415,7 +435,7 @@(deftest move-tab-reorders-test(testing "GHY-4147: move_tab reorders the vector; update-dashboard! derives :position from the index"- (let [{:keys [tabs]} (dashboard-ops/compile-ops+ (let [{:keys [tabs]} (compile-ops(assoc empty-dash :tabs [{:id 5 :name "A"} {:id 6 :name "B"} {:id 7 :name "C"}])[{:op "move_tab" :tab_id 7 :index 0}])](is (= [7 5 6] (mapv :id tabs))))))@@ -427,7 +447,7 @@:parameters []:dashcards [{:id 7 :card_id 9 :row 0 :col 0 :size_x 4 :size_y 4:dashboard_tab_id 5 :visualization_settings {}}]}- {:keys [tabs dashcards]} (dashboard-ops/compile-ops+ {:keys [tabs dashcards]} (compile-opscurrent[{:op "duplicate_tab" :id -1 :tab_id 5}])](is (= [5 -1] (mapv :id tabs)))@@ -447,7 +467,7 @@:dashboard_tab_id 5 :visualization_settings {}}{:id 8 :card_id 9 :row 4 :col 0 :size_x 4 :size_y 4:dashboard_tab_id 5 :visualization_settings {}}]}- {:keys [dashcards]} (dashboard-ops/compile-ops+ {:keys [dashcards]} (compile-opscurrent[{:op "duplicate_tab" :id -1 :tab_id 5}{:op "add_card" :id -10 :card_id 9 :tab -1}]@@ -463,7 +483,7 @@:parameters []:dashcards [{:id 7 :card_id 9 :dashboard_tab_id 5 :row 0 :col 0 :size_x 4 :size_y 4}{:id 8 :card_id 9 :dashboard_tab_id 6 :row 0 :col 0 :size_x 4 :size_y 4}]}- {:keys [tabs dashcards]} (dashboard-ops/compile-ops current [{:op "remove_tab" :tab_id 5}])]+ {:keys [tabs dashcards]} (compile-ops current [{:op "remove_tab" :tab_id 5}])](is (= [6] (mapv :id tabs)))(is (= [8] (mapv :id dashcards))))))@@ -478,7 +498,7 @@(deftest single-tab-coverage-is-not-enforced-test(testing "GHY-4147: with exactly one tab, a tabless card is allowed — update-dashboard! back-fillsits dashboard_tab_id itself (api.clj), so the compiler must not reject it early"- (is (some? (dashboard-ops/compile-ops+ (is (some? (compile-opsempty-dash[{:op "add_tab" :id -1 :name "Q3"}{:op "add_card" :id -2 :card_id 42}])))))@@ -522,7 +542,7 @@(deftest add-parameter-test(testing "GHY-4147: add_parameter appends a parameter carrying the caller's id, REST names intact"- (let [{:keys [parameters]} (dashboard-ops/compile-ops+ (let [{:keys [parameters]} (compile-opsempty-dash[{:op "add_parameter" :parameter_id "p_date" :name "Date":type "date/all-options" :isMultiSelect false}]@@ -533,14 +553,14 @@(deftest add-parameter-derives-slug-from-name-test(testing "GHY-4147: a created parameter gets the URL slug the editor would give it, so it isaddressable by slug in embedding, public links, and URL param sync"- (let [{:keys [parameters]} (dashboard-ops/compile-ops+ (let [{:keys [parameters]} (compile-opsempty-dash[{:op "add_parameter" :parameter_id "p1" :name "Order Total":type "number/>="}]{})](is (= "order_total" (:slug (first parameters)))))(testing "a nameless parameter gets no slug rather than a bogus one"- (let [{:keys [parameters]} (dashboard-ops/compile-ops+ (let [{:keys [parameters]} (compile-opsempty-dash[{:op "add_parameter" :parameter_id "p1" :type "string/="}]{})]@@ -554,7 +574,7 @@(deftest update-parameter-merges-test(testing "GHY-4147: update_parameter merges into the existing parameter, re-slugging on a rename"- (let [{:keys [parameters]} (dashboard-ops/compile-ops+ (let [{:keys [parameters]} (compile-ops(assoc empty-dash :parameters [{:id "p1" :name "X" :type "string/="}])[{:op "update_parameter" :parameter_id "p1" :name "Y"}]{})]@@ -563,7 +583,7 @@(deftest update-parameter-preserves-slug-when-not-renaming-test(testing "GHY-4147: an update that does not touch the name leaves the slug alone, so an edit toan editor-created parameter cannot silently break its existing URLs"- (let [{:keys [parameters]} (dashboard-ops/compile-ops+ (let [{:keys [parameters]} (compile-ops(assoc empty-dash:parameters [{:id "p1" :name "Order-Date" :slug "order-date":type "date/all-options"}])@@ -582,7 +602,7 @@:parameter_mappings [{:parameter_id "p1" :card_id 9 :target [:dimension [:field 1 nil]]}{:parameter_id "p2" :card_id 9:target [:dimension [:field 2 nil]]}]}]}- {:keys [parameters dashcards]} (dashboard-ops/compile-ops+ {:keys [parameters dashcards]} (compile-opscurrent [{:op "remove_parameter" :parameter_id "p1"}] {})dc (first dashcards)](is (= ["p2"] (mapv :id parameters)))@@ -592,7 +612,7 @@(deftest move-parameter-reorders-header-test(testing "GHY-4147: move_parameter with an index reorders the header"- (let [{:keys [parameters]} (dashboard-ops/compile-ops+ (let [{:keys [parameters]} (compile-ops(assoc empty-dash :parameters [{:id "a"} {:id "b"} {:id "c"}])[{:op "move_parameter" :parameter_id "c" :index 0}]{})]@@ -611,7 +631,7 @@(testing "GHY-4147: move_parameter with a dashcard_id makes it an inline filter on that card"(let [current {:id 1 :tabs [] :parameters [{:id "p1"}]:dashcards [{:id 7 :card_id 9 :row 0 :col 0 :size_x 4 :size_y 4}]}- {:keys [dashcards]} (dashboard-ops/compile-ops+ {:keys [dashcards]} (compile-opscurrent [{:op "move_parameter" :parameter_id "p1" :dashcard_id 7}] {})](is (= ["p1"] (:inline_parameters (first dashcards)))))))@@ -623,7 +643,7 @@:inline_parameters ["p1"]}{:id 8 :card_id 9 :row 4 :col 0 :size_x 4 :size_y 4}]}op {:op "move_parameter" :parameter_id "p1" :dashcard_id 8}- {:keys [dashcards]} (dashboard-ops/compile-ops current [op op] {})]+ {:keys [dashcards]} (compile-ops current [op op] {})](is (= [] (:inline_parameters (first dashcards))))(is (= ["p1"] (:inline_parameters (second dashcards)))))))@@ -636,7 +656,7 @@current {:id 1 :tabs [] :parameters [{:id "p1"}]:dashcards [{:id 7 :card_id 9 :row 0 :col 0 :size_x 4 :size_y 4}bystander legacy]}- {:keys [dashcards]} (dashboard-ops/compile-ops+ {:keys [dashcards]} (compile-opscurrent[{:op "move_parameter" :parameter_id "p1" :dashcard_id 7}] {})](is (= bystander (nth dashcards 1)))@@ -653,7 +673,7 @@:parameter_mappings [{:parameter_id "p1" :card_id 9:target [:dimension [:field 1 nil]]}]}bystander legacy]}- {:keys [dashcards]} (dashboard-ops/compile-ops+ {:keys [dashcards]} (compile-opscurrent[{:op "unwire_parameter" :parameter_id "p1"}] {})](is (= [] (:parameter_mappings (first dashcards))))@@ -668,7 +688,7 @@{:keys [dashcards]} (dynamic-redefs/with-dynamic-fn-redefs [mapping-targets/target-for-field(fn [_card _param field-id][:dimension [:field field-id nil]])]- (dashboard-ops/compile-ops+ (compile-opscurrent[{:op "wire_parameter" :parameter_id "p1" :dashcard_id 7 :target_field 55}]{9 a-card}))]@@ -684,7 +704,7 @@{:keys [dashcards]} (dynamic-redefs/with-dynamic-fn-redefs [mapping-targets/target-for-field(fn [_card _param field-id][:dimension [:field field-id nil]])]- (dashboard-ops/compile-ops+ (compile-opscurrent[{:op "wire_parameter" :parameter_id "p1" :dashcard_id 7 :target_field 55}{:op "wire_parameter" :parameter_id "p1" :dashcard_id 7 :target_field 66}]@@ -722,7 +742,7 @@(fn [card _param field-id](when (= 9 (:id card))[:dimension [:field field-id nil]]))]- (dashboard-ops/compile-ops+ (compile-opscurrent[{:op "wire_parameter" :parameter_id "p1" :dashcard_id 7:target_field 55 :autowire true}]@@ -740,12 +760,12 @@:parameter_mappings [{:parameter_id "p1" :card_id 10:target [:dimension [:field 2 nil]]}]}]}](testing "one card"- (let [{:keys [dashcards]} (dashboard-ops/compile-ops+ (let [{:keys [dashcards]} (compile-opscurrent [{:op "unwire_parameter" :parameter_id "p1" :dashcard_id 7}] {})](is (= [] (:parameter_mappings (first dashcards))))(is (= 1 (count (:parameter_mappings (second dashcards)))))))(testing "all cards"- (let [{:keys [dashcards]} (dashboard-ops/compile-ops+ (let [{:keys [dashcards]} (compile-opscurrent [{:op "unwire_parameter" :parameter_id "p1"}] {})](is (every? (comp empty? :parameter_mappings) dashcards)))))))@@ -775,7 +795,7 @@(defn- wire-native[op]- (-> (dashboard-ops/compile-ops native-current [op] {9 native-card})+ (-> (compile-ops native-current [op] {9 native-card}):dashcards first :parameter_mappings))(defn- wire-native-error@@ -836,7 +856,7 @@:text "Sales in {{region}} for {{period}}."}}current (assoc native-current :dashcards [text-dashcard])](testing "a text-tag target binds a {{placeholder}} in a text card's own markdown — no card needed"- (let [{:keys [dashcards]} (dashboard-ops/compile-ops+ (let [{:keys [dashcards]} (compile-opscurrent[{:op "wire_parameter" :parameter_id "p1" :dashcard_id 7:target ["text-tag" "region"]}]@@ -844,7 +864,7 @@(is (= [{:parameter_id "p1" :card_id nil :target [:text-tag "region"]}](:parameter_mappings (first dashcards))))))(testing "an iframe card's embed carries placeholders too"- (let [{:keys [dashcards]} (dashboard-ops/compile-ops+ (let [{:keys [dashcards]} (compile-ops(assoc current :dashcards[{:id 7 :row 0 :col 0 :size_x 12 :size_y 8:visualization_settings {:virtual_card {:display "iframe"}@@ -980,4 +1000,37 @@(#'dashboard-ops/drop-inline-parameter "p1")(#'dashboard-ops/drop-mapping "p1"))))](is (= via-primitives- (dashboard-ops/compile-ops current [{:op "remove_parameter" :parameter_id "p1"}] {}))))))+ (compile-ops current [{:op "remove_parameter" :parameter_id "p1"}] {}))))))++(deftest ^:parallel unresolved-card-test+ (testing "an op naming a card the caller did not resolve fails, naming that card, wherever the op names it"+ (is (= {::dashboard-ops/error :unknown-card, :card-id 42, :op-index 0, :op-name "add_card"}+ (op-error empty-dash [{:op "add_card" :id -1 :card_id 42}] {})))+ (is (=? {::dashboard-ops/error :unknown-card, :card-id 43, :op-name "add_card"}+ (op-error empty-dash [{:op "add_card" :id -1 :card_id 42 :series [43]}] {42 {:id 42}})))+ (is (=? {::dashboard-ops/error :unknown-card, :card-id 43, :op-name "replace_card"}+ (op-error (dash-with [{:id 7 :card_id 9 :row 0 :col 0 :size_x 4 :size_y 4}])+ [{:op "replace_card" :dashcard_id 7 :card_id 43}] {})))+ (is (=? {::dashboard-ops/error :unknown-card, :card-id 44, :op-name "set_series"}+ (op-error (dash-with [{:id 7 :card_id 9 :row 0 :col 0 :size_x 4 :size_y 4}])+ [{:op "set_series" :dashcard_id 7 :card_ids [44]}] {})))+ (is (=? {::dashboard-ops/error :unknown-card, :card-id 45, :op-name "add_parameter"}+ (op-error empty-dash+ [{:op "add_parameter" :parameter_id "p1" :name "P" :type "string/="+ :values_source_type "card" :values_source_config {:card_id 45}}]+ {})))+ (is (=? {::dashboard-ops/error :unknown-card, :card-id 46, :op-name "update_parameter"}+ (op-error (assoc empty-dash :parameters [{:id "p1"}])+ [{:op "update_parameter" :parameter_id "p1"+ :values_source_type "card" :values_source_config {:card_id 46}}]+ {}))))+ (testing "ops are checked in order, so an earlier op's error is reported first"+ (is (=? {::dashboard-ops/error :unknown-op, :op-index 0}+ (op-error empty-dash [{:op "frobnicate"} {:op "add_card" :id -1 :card_id 42}] {}))))+ (testing "an op's own error is reported before an unresolved card it names"+ (is (=? {::dashboard-ops/error :unknown-op}+ (op-error empty-dash [{:op "frobnicate" :card_id 42}] {})))+ (is (=? {::dashboard-ops/error :unknown-dashcard, :dashcard-id 12345}+ (op-error empty-dash [{:op "replace_card" :dashcard_id 12345 :card_id 42}] {}))))+ (testing "a card key the op does not use is not checked"+ (is (nil? (op-error empty-dash [{:op "add_text" :id -1 :markdown "hi" :card_id 42}] {})))))