Skip to content

The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:

168 KB
Raw Blame
(ns metabase-enterprise.remote-sync.api-test
  {:clj-kondo/config '{:linters {:deprecated-var {:exclude {metabase.test.data/mbql-query {:namespaces [metabase-enterprise.remote-sync.api-test]}}}}}}
  (:require
   [clojure.string :as str]
   [clojure.test :refer :all]
   [diehard.core :as dh]
   [java-time.api :as t]
   [metabase-enterprise.remote-sync.core :as remote-sync.core]
   [metabase-enterprise.remote-sync.events :as remote-sync.events]
   [metabase-enterprise.remote-sync.impl :as impl]
   [metabase-enterprise.remote-sync.models.remote-sync-object :as remote-sync.object]
   [metabase-enterprise.remote-sync.models.remote-sync-task :as remote-sync.task]
   [metabase-enterprise.remote-sync.settings :as settings]
   [metabase-enterprise.remote-sync.source :as source]
   [metabase-enterprise.remote-sync.source.git :as source.git]
   [metabase-enterprise.remote-sync.source.protocol :as source.p]
   [metabase-enterprise.remote-sync.spec :as spec]
   [metabase-enterprise.remote-sync.test-helpers :as test-helpers]
   [metabase.collections.test-utils :as collections.tu]
   [metabase.driver.settings :as driver.settings]
   [metabase.remote-sync.test-util :as remote-sync.tu]
   [metabase.settings.core :as setting]
   [metabase.test :as mt]
   [metabase.test.fixtures :as fixtures]
   [metabase.util :as u]
   [metabase.util.quick-task :as quick-task]
   [toucan2.core :as t2]))

(set! *warn-on-reflection* true)

(defn mock-git-source
  "Create a mock git source for testing"
  [& {:keys [branches error-on-branches?]
      :or {branches ["main" "develop"]}}]
  (reify source.p/Source
    (create-branch [_ _branch _base]
      nil)
    (branches [_]
      (if error-on-branches?
        (throw (Exception. "Repository not found"))
        branches))
    (default-branch [_]
      "main")
    (snapshot [_]
      nil)
    (snapshot-at [_ _version]
      nil)))

(use-fixtures :once
  (fixtures/initialize :db)
  ;; TODO (edpaget 10/23/25): this seems silly, maybe there's a better way?
  (fn [f] (mt/dataset test-data
            (mt/id)
            (f))))

(use-fixtures :each
  remote-sync.tu/allow-remote-synced-collections
  test-helpers/clean-remote-sync-state
  (fn [f]
    (mt/with-premium-features #{:remote-sync}
      (f))))

(defn- wait-for-task-completion [task-id]
  (when task-id
    (dh/with-retry {:max-retries 10
                    :delay-ms 500}
      (u/prog1 (t2/select-one :model/RemoteSyncTask :id task-id)
        (when (nil? (:ended_at <>))
          (throw (ex-info "Not finished" {:task-id task-id
                                          :result <>})))))))

;;; ------------------------------------------------- Test Connection Endpoint -------------------------------------------------

(deftest test-connection-succeeds-with-current-settings-test
  (testing "POST /api/ee/remote-sync/test-connection returns success when current settings can reach the repo"
    (mt/with-temporary-setting-values [remote-sync-url    "https://github.com/test/repo.git"
                                       remote-sync-token  "valid-token"
                                       remote-sync-branch "main"
                                       remote-sync-type   :read-only]
      (mt/with-dynamic-fn-redefs [settings/check-git-settings! (constantly nil)
                                  source.git/git-source        (fn [_ _ _ _] {:fake-source true})
                                  source.git/branches          (fn [_] ["main"])]
        (is (= {:status "success"}
               (mt/user-http-request :crowberto :post 200 "ee/remote-sync/test-connection" {})))))))

(deftest test-connection-forces-fresh-remote-call-test
  (testing "POST /api/ee/remote-sync/test-connection always calls git/branches so rotated tokens are detected"
    ;; check-git-settings! only authenticates when :read-only + branch is set, so the JGit cache
    ;; would otherwise short-circuit subsequent tests with the same token. We need a fresh
    ;; lsRemote on every click — this guards that.
    (let [branches-calls (atom 0)]
      (mt/with-temporary-setting-values [remote-sync-url    "https://github.com/test/repo.git"
                                         remote-sync-token  "valid-token"
                                         remote-sync-branch ""
                                         remote-sync-type   :read-write]
        (mt/with-dynamic-fn-redefs [settings/check-git-settings! (constantly nil)
                                    source.git/git-source        (fn [_ _ _ _] {:fake-source true})
                                    source.git/branches          (fn [_] (swap! branches-calls inc) [])]
          (mt/user-http-request :crowberto :post 200 "ee/remote-sync/test-connection" {})
          (is (= 1 @branches-calls)
              "Test Connection must call git/branches even when check-git-settings! skips the remote check"))))))

(deftest test-connection-surfaces-fresh-remote-auth-failure-test
  (testing "POST /api/ee/remote-sync/test-connection returns 400 when the forced lsRemote rejects the token"
    (mt/with-temporary-setting-values [remote-sync-url    "https://github.com/test/repo.git"
                                       remote-sync-token  "rotated-token"
                                       remote-sync-branch ""
                                       remote-sync-type   :read-write]
      (mt/with-dynamic-fn-redefs [settings/check-git-settings! (constantly nil)
                                  source.git/git-source        (fn [_ _ _ _] {:fake-source true})
                                  source.git/branches          (fn [_] (throw (ex-info "Authentication failed" {})))]
        (is (= "Authentication failed: Please check your git credentials"
               (mt/user-http-request :crowberto :post 400 "ee/remote-sync/test-connection" {})))))))

(deftest test-connection-uses-body-overrides-test
  (testing "POST /api/ee/remote-sync/test-connection passes URL and token from the body through to the lsRemote call"
    (let [captured (atom nil)]
      (mt/with-temporary-setting-values [remote-sync-url    "https://github.com/test/repo.git"
                                         remote-sync-token  "saved-token"
                                         remote-sync-branch "main"
                                         remote-sync-type   :read-only]
        (mt/with-dynamic-fn-redefs [settings/check-git-settings! (constantly nil)
                                    source.git/git-source        (fn [url _ token _]
                                                                   (reset! captured {:url url :token token})
                                                                   {:fake-source true})
                                    source.git/branches          (fn [_] [])]
          (mt/user-http-request :crowberto :post 200 "ee/remote-sync/test-connection"
                                {:remote-sync-url   "https://github.com/other/repo.git"
                                 :remote-sync-token "new-token"})
          (is (= {:url "https://github.com/other/repo.git" :token "new-token"} @captured)))))))

(deftest test-connection-treats-obfuscated-token-as-unchanged-test
  (testing "POST /api/ee/remote-sync/test-connection uses stored token when body sends the obfuscated value"
    (let [captured (atom nil)
          full-token "ghp_full_secret_token_value"]
      (mt/with-temporary-setting-values [remote-sync-url    "https://github.com/test/repo.git"
                                         remote-sync-token  full-token
                                         remote-sync-branch "main"
                                         remote-sync-type   :read-only]
        (mt/with-dynamic-fn-redefs [settings/check-git-settings! (constantly nil)
                                    source.git/git-source        (fn [_ _ token _]
                                                                   (reset! captured token)
                                                                   {:fake-source true})
                                    source.git/branches          (fn [_] [])]
          (mt/user-http-request :crowberto :post 200 "ee/remote-sync/test-connection"
                                {:remote-sync-token (setting/obfuscate-value full-token)})
          (is (= full-token @captured)
              "Obfuscated tokens must be replaced with the stored token before testing"))))))

(deftest test-connection-requires-superuser-test
  (testing "POST /api/ee/remote-sync/test-connection requires superuser permissions"
    (mt/with-temporary-setting-values [remote-sync-url "https://github.com/test/repo.git"]
      (is (= "You don't have permissions to do that."
             (mt/user-http-request :rasta :post 403 "ee/remote-sync/test-connection" {}))))))

(deftest test-connection-errors-when-not-configured-test
  (testing "POST /api/ee/remote-sync/test-connection returns 400 when no url is configured or provided"
    (mt/with-temporary-setting-values [remote-sync-url nil]
      (is (= "Remote sync is not configured."
             (mt/user-http-request :crowberto :post 400 "ee/remote-sync/test-connection" {}))))))

(deftest test-connection-returns-friendly-message-on-failure-test
  (testing "POST /api/ee/remote-sync/test-connection wraps exceptions in a user-friendly message"
    (mt/with-temporary-setting-values [remote-sync-url    "https://github.com/test/repo.git"
                                       remote-sync-token  "bad-token"
                                       remote-sync-branch "main"
                                       remote-sync-type   :read-only]
      (testing "Authentication failure maps to credentials error"
        (mt/with-dynamic-fn-redefs [settings/check-git-settings! (fn [_] (throw (ex-info "Authentication failed" {})))]
          (is (= "Authentication failed: Please check your git credentials"
                 (mt/user-http-request :crowberto :post 400 "ee/remote-sync/test-connection" {})))))
      (testing "Repository-not-found maps to URL error"
        (mt/with-dynamic-fn-redefs [settings/check-git-settings! (fn [_] (throw (ex-info "Repository not found" {})))]
          (is (= "Repository not found: Please check the repository URL"
                 (mt/user-http-request :crowberto :post 400 "ee/remote-sync/test-connection" {}))))))))

;;; ------------------------------------------------- Branches Endpoint -------------------------------------------------

(deftest branches-endpoint-returns-branches-test
  (testing "GET /api/ee/remote-sync/branches returns list of branches"
    (mt/with-dynamic-fn-redefs [source/source-from-settings (constantly (mock-git-source :branches ["main" "develop" "feature-branch"]))]
      (is (= {:items ["main" "develop" "feature-branch"]}
             (mt/user-http-request :crowberto :get 200 "ee/remote-sync/branches"))))))

(deftest branches-endpoint-requires-superuser-test
  (testing "GET /api/ee/remote-sync/branches requires superuser permissions"
    (mt/with-dynamic-fn-redefs [source/source-from-settings (constantly (mock-git-source))]
      (is (= "You don't have permissions to do that."
             (mt/user-http-request :rasta :get 403 "ee/remote-sync/branches"))))))

(deftest branches-endpoint-errors-when-git-not-configured-test
  (testing "GET /api/ee/remote-sync/branches errors when git source not configured"
    (mt/with-dynamic-fn-redefs [source/source-from-settings (constantly nil)]
      (is (= "Source not configured. Please configure MB_GIT_SOURCE_REPO_URL environment variable."
             (mt/user-http-request :crowberto :get 400 "ee/remote-sync/branches"))))))

(deftest branches-endpoint-handles-repository-errors-test
  (testing "GET /api/ee/remote-sync/branches handles git repository errors"
    (mt/with-dynamic-fn-redefs [source/source-from-settings (constantly (mock-git-source :error-on-branches? true))]
      (is (= "Repository not found: Please check the repository URL"
             (mt/user-http-request :crowberto :get 400 "ee/remote-sync/branches"))))))

;;; ------------------------------------------------- Import Endpoint -------------------------------------------------

(deftest import-with-default-branch-test
  (testing "POST /api/ee/remote-sync/import succeeds with default branch"
    (let [mock-main (test-helpers/create-mock-source)]
      (mt/with-temporary-setting-values [remote-sync-url "https://github.com/test/repo.git"
                                         remote-sync-token "test-token"
                                         remote-sync-branch "main"]
        (mt/with-dynamic-fn-redefs [source/source-from-settings (constantly mock-main)]
          (let [{:keys [task_id] :as resp} (mt/user-http-request :crowberto :post 200 "ee/remote-sync/import" {:expected_branch "main"})
                completed-task (wait-for-task-completion task_id)]
            (is (=? {:status "success" :task_id int?} resp))
            (is (remote-sync.task/successful? completed-task))))))))

(defn- gui-model-yaml
  "YAML for a GUI model over `db-name`'s PUBLIC.VENUES, in the shape an export writes: the result metadata
  carries only the model overrides, with no `base_type` and no field `id`."
  [entity-id collection-id db-name]
  (-> (test-helpers/generate-card-yaml entity-id "Venues Model" collection-id "model")
      (str/replace "database_id: test-data (h2)" (str "database_id: " db-name))
      (str/replace "dataset_query: {}"
                   (format (str "dataset_query:\n"
                                "  database: %s\n"
                                "  type: query\n"
                                "  query:\n"
                                "    source-table:\n"
                                "    - %s\n"
                                "    - PUBLIC\n"
                                "    - VENUES")
                           db-name db-name))
      (str/replace "result_metadata: null"
                   (str "result_metadata:\n"
                        (str/join (for [[col-name display-name] [["ID" "ID"]
                                                                 ["NAME" "Venue Name"]
                                                                 ["CATEGORY_ID" "Category ID"]
                                                                 ["LATITUDE" "Latitude"]
                                                                 ["LONGITUDE" "Longitude"]
                                                                 ["PRICE" "Price"]]]
                                    (format "- display_name: %s\n  name: %s\n  visibility_type: normal\n"
                                            display-name col-name)))))))

(deftest import-gui-model-before-schema-sync-test
  (testing (str "GHY-4213: a GUI model imported before the target has synced its table stores untyped columns. "
                "Once the schema sync runs, the model's columns must get their types and field ids, so that "
                "the query builder offers typed filters on them.")
    (let [details       (:details (mt/db))
          db-name       (mt/random-name)
          collection-id "ghy4213collectionxxxx"
          card-eid      "ghy4213modelxxxxxxxxx"]
      ;; Keep the new database from being synced when it is created, so its tables do not exist yet at import.
      (mt/with-temporary-setting-values [disable-auto-sync  true
                                         remote-sync-url    "https://github.com/test/repo.git"
                                         remote-sync-token  "test-token"
                                         remote-sync-branch "main"]
        (test-helpers/commit-with-temp
         (fn []
           (mt/with-temp [:model/Database {db-id :id} {:engine "h2" :details details :name db-name}]
             (let [source (test-helpers/create-mock-source
                           :initial-files {"main" {(format "collections/%s_ghy/%s_ghy.yaml" collection-id collection-id)
                                                   (test-helpers/generate-collection-yaml collection-id "GHY 4213" :is-remote-synced true)
                                                   (format "collections/%s_ghy/cards/%s_venues_model.yaml" collection-id card-eid)
                                                   (gui-model-yaml card-eid collection-id db-name)}})]
               (try
                 (mt/with-dynamic-fn-redefs [source/source-from-settings (constantly source)]
                   (let [{:keys [task_id]} (mt/user-http-request :crowberto :post 200 "ee/remote-sync/import"
                                                                 {:force true :expected_branch "main"})]
                     (is (remote-sync.task/successful? (wait-for-task-completion task_id)))))
                 (mt/with-dynamic-fn-redefs [quick-task/submit-task! (fn [f]
                                                                       (binding [driver.settings/*allow-testing-h2-connections* true]
                                                                         (f)))]
                   (mt/user-http-request :crowberto :post 200 (format "database/%d/sync_schema" db-id)))
                 (let [card-id  (t2/select-one-pk :model/Card :entity_id card-eid)
                       table-id (t2/select-one-pk :model/Table :db_id db-id :name "VENUES")
                       field-id (fn [field-name]
                                  (t2/select-one-pk :model/Field :table_id table-id :name field-name))]
                   (is (=? [{:name "ID"          :base_type "type/BigInteger" :id (field-id "ID")}
                            {:name "NAME"        :base_type "type/Text"       :id (field-id "NAME")
                             :display_name "Venue Name"}
                            {:name "CATEGORY_ID" :base_type "type/Integer"    :id (field-id "CATEGORY_ID")}
                            {:name "LATITUDE"    :base_type "type/Float"      :id (field-id "LATITUDE")}
                            {:name "LONGITUDE"   :base_type "type/Float"      :id (field-id "LONGITUDE")}
                            {:name "PRICE"       :base_type "type/Integer"    :id (field-id "PRICE")}]
                           (:result_metadata (mt/user-http-request :crowberto :get 200 (str "card/" card-id))))))
                 (finally
                   (t2/delete! :model/Card :entity_id card-eid)
                   (t2/delete! :model/Collection :entity_id collection-id)))))))))))

(deftest import-with-specific-branch-test
  (testing "POST /api/ee/remote-sync/import succeeds with specific branch"
    (let [mock-develop (test-helpers/create-mock-source :branch "develop")]
      (mt/with-temporary-setting-values [remote-sync-url "https://github.com/test/repo.git"
                                         remote-sync-token "test-token"
                                         remote-sync-branch "main"]
        (mt/with-dynamic-fn-redefs [source/source-from-settings (constantly mock-develop)]
          (let [{:as response :keys [task_id]} (mt/user-http-request :crowberto :post 200 "ee/remote-sync/import" {:branch "feature-branch" :expected_branch "main"})
                completed-task (wait-for-task-completion task_id)]
            (is (= "success" (:status response)))
            (is (remote-sync.task/successful? completed-task))))))))

(deftest import-dirty-guard-includes-dirty-objects-test
  (testing "GHY-4019: a blocked (dirty, non-forced) import names the un-pushed changes it would discard"
    (mt/with-temp [:model/Collection coll {:name "Synced" :is_remote_synced true :location "/"}
                   :model/Card card {:name "Local Metric" :type :metric :collection_id (:id coll)}
                   :model/RemoteSyncObject _ {:model_type          "Card"
                                              :model_id            (:id card)
                                              :status              "create"
                                              :status_changed_at   (t/instant)
                                              :model_name          "Local Metric"
                                              :model_collection_id (:id coll)}]
      (let [mock-main (test-helpers/create-mock-source)]
        (mt/with-temporary-setting-values [remote-sync-url    "https://github.com/test/repo.git"
                                           remote-sync-token  "test-token"
                                           remote-sync-branch "main"]
          (mt/with-dynamic-fn-redefs [source/source-from-settings (constantly mock-main)]
            (let [resp (mt/user-http-request :crowberto :post 400 "ee/remote-sync/import"
                                             {:branch "develop" :expected_branch "main"})]
              (is (true? (:conflicts resp)))
              (is (some (comp #{"Local Metric"} :name) (:dirty_objects resp))
                  "the response lists the un-pushed local metric"))))))))

(deftest import-without-expected-branch-succeeds-test
  (testing "GHY-4636: `mb git-sync import` sends only `branch`; the import must run without `expected_branch`"
    (let [mock-main (test-helpers/create-mock-source)]
      (mt/with-temporary-setting-values [remote-sync-url    "https://github.com/test/repo.git"
                                         remote-sync-token  "test-token"
                                         remote-sync-branch "main"]
        (mt/with-dynamic-fn-redefs [source/source-from-settings (constantly mock-main)]
          (let [{:keys [task_id] :as resp} (mt/user-http-request :crowberto :post 200 "ee/remote-sync/import"
                                                                 {:branch "main"})
                completed-task (wait-for-task-completion task_id)]
            (is (=? {:status "success" :task_id int?} resp))
            (is (remote-sync.task/successful? completed-task))))))))

(deftest import-rejects-expected-branch-mismatch-test
  (testing "POST /api/ee/remote-sync/import rejects when expected_branch disagrees with the configured setting"
    (let [mock-main (test-helpers/create-mock-source)]
      (mt/with-temporary-setting-values [remote-sync-url    "https://github.com/test/repo.git"
                                         remote-sync-token  "test-token"
                                         remote-sync-branch "main"]
        (mt/with-dynamic-fn-redefs [source/source-from-settings (constantly mock-main)]
          (testing "stale expected_branch -> 409 branch_mismatch, no task created"
            (let [before (t2/count :model/RemoteSyncTask)
                  resp   (mt/user-http-request :crowberto :post 409 "ee/remote-sync/import"
                                               {:branch "main" :expected_branch "stale-branch"})]
              (is (true? (:branch_mismatch resp)))
              (is (= "main" (:current_branch resp)))
              (is (= before (t2/count :model/RemoteSyncTask))
                  "no RemoteSyncTask row is created when the guard fires")))
          (testing "matching expected_branch -> pull proceeds"
            (let [{:keys [task_id] :as resp} (mt/user-http-request :crowberto :post 200 "ee/remote-sync/import"
                                                                   {:branch "main" :expected_branch "main"})]
              (is (=? {:status "success" :task_id int?} resp))
              (wait-for-task-completion task_id)))
          (testing "a branch switch (operational branch != expected_branch) is allowed when expected_branch matches the setting"
            (let [{:keys [task_id] :as resp} (mt/user-http-request :crowberto :post 200 "ee/remote-sync/import"
                                                                   {:branch "feature-branch" :expected_branch "main"})]
              (is (=? {:status "success" :task_id int?} resp))
              (wait-for-task-completion task_id))))))))

(deftest import-creates-audit-log-entry-test
  (testing "POST /api/ee/remote-sync/import records a remote-sync-import audit log entry (#73335)"
    ;; :audit-app is needed for events to actually be recorded to the audit log
    (mt/with-premium-features #{:remote-sync :audit-app}
      (let [mock-main (test-helpers/create-mock-source)]
        (mt/with-temporary-setting-values [remote-sync-url "https://github.com/test/repo.git"
                                           remote-sync-token "test-token"
                                           remote-sync-branch "main"]
          (mt/with-dynamic-fn-redefs [source/source-from-settings (constantly mock-main)]
            (let [before            (t2/count :model/AuditLog :topic "remote-sync-import")
                  {:keys [task_id]} (mt/user-http-request :crowberto :post 200 "ee/remote-sync/import" {:expected_branch "main"})]
              (wait-for-task-completion task_id)
              ;; the audit event publishes after the task's ended_at is set, so poll for the row
              (let [entry (dh/with-retry {:max-retries 10
                                          :delay-ms 200}
                            (u/prog1 (t2/select-one :model/AuditLog :topic "remote-sync-import" {:order-by [[:id :desc]]})
                              (when (<= (t2/count :model/AuditLog :topic "remote-sync-import") before)
                                (throw (ex-info "Audit log entry not written yet" {})))))]
                (testing "attributed to the user who triggered it"
                  (is (= (mt/user->id :crowberto) (:user_id entry))))
                (testing "branch recorded, no :auto flag for manual imports"
                  (is (= "main" (get-in entry [:details :branch])))
                  (is (not (contains? (:details entry) :auto))))))))))))

(deftest import-requires-superuser-test
  (testing "POST /api/ee/remote-sync/import requires superuser permissions"
    (mt/with-temporary-setting-values [remote-sync-enabled true remote-sync-branch "main"]
      (testing "a non-admin cannot pull"
        (is (= "You don't have permissions to do that."
               (mt/user-http-request :rasta :post 403 "ee/remote-sync/import" {:expected_branch "main"}))))
      (testing "a non-admin cannot switch branches (branch != expected_branch)"
        (is (= "You don't have permissions to do that."
               (mt/user-http-request :rasta :post 403 "ee/remote-sync/import"
                                     {:branch "develop" :expected_branch "main"})))))))

(deftest import-errors-when-remote-sync-disabled-test
  (testing "POST /api/ee/remote-sync/import errors when remote sync is disabled"
    (mt/with-temporary-setting-values [remote-sync-url nil]
      (is (= "Remote sync is not configured."
             (mt/user-http-request :crowberto :post 400 "ee/remote-sync/import" {:expected_branch "main"}))))))

(deftest import-handles-network-errors-test
  (testing "POST /api/ee/remote-sync/import handles network errors during import"
    (let [mock-main (test-helpers/create-mock-source :fail-mode :network-error)]
      (mt/with-temporary-setting-values [remote-sync-url "https://github.com/test/repo.git"
                                         remote-sync-token "test-token"
                                         remote-sync-branch "main"]
        (mt/with-dynamic-fn-redefs [source/source-from-settings (constantly mock-main)]
          (let [{:as response :keys [task_id]} (mt/user-http-request :crowberto :post 200 "ee/remote-sync/import" {:expected_branch "main"})
                completed-task (wait-for-task-completion task_id)]
            (is (= "success" (:status response)))
            (is (remote-sync.task/failed? completed-task))))))))

(deftest import-errors-when-task-already-exists-test
  (testing "POST /api/ee/remote-sync/import errors when task already exists"
    (mt/with-temp [:model/RemoteSyncTask _ {:sync_task_type "foo"}]
      (let [mock-source (test-helpers/create-mock-source)]
        (mt/with-temporary-setting-values [remote-sync-url "https://github.com/test/repo.git"
                                           remote-sync-token "test-token"
                                           remote-sync-branch "main"]
          (mt/with-dynamic-fn-redefs [source/source-from-settings (constantly mock-source)]
            (is (= "Remote sync task in progress"
                   (mt/user-http-request :crowberto :post 400 "ee/remote-sync/import" {:expected_branch "main"})))))))))

(deftest import-errors-when-dirty-changes-test
  (testing "POST /api/ee/remote-sync/import errors when dirty changes exist"
    (let [mock-main (test-helpers/create-mock-source)]
      (mt/with-temporary-setting-values [remote-sync-url "https://github.com/test/repo.git"
                                         remote-sync-token "test-token"
                                         remote-sync-branch "main"]
        (t2/insert! :model/RemoteSyncObject {:model_type "Card"
                                             :model_id 1
                                             :model_name "Test Card"
                                             :model_collection_id 1
                                             :status "updated"
                                             :status_changed_at (java.time.OffsetDateTime/now)})
        (mt/with-dynamic-fn-redefs [source/source-from-settings (constantly mock-main)]
          (is (= "There are unsaved changes in the Remote Sync collection which will be overwritten by the import. Force the import to discard these changes."
                 (:message (mt/user-http-request :crowberto :post 400 "ee/remote-sync/import" {:expected_branch "main"}))))
          (testing "But can force an import"
            (let [{:keys [task_id] :as resp} (mt/user-http-request :crowberto :post 200 "ee/remote-sync/import" {:force true :expected_branch "main"})
                  completed-task (wait-for-task-completion task_id)]
              (is (=? {:status "success" :task_id int?} resp))
              (is (remote-sync.task/successful? completed-task)))))))))

(deftest import-merge-keeps-local-changes-test
  (testing "POST /api/ee/remote-sync/import with merge=true does a local-only merge instead of erroring on dirty"
    (mt/with-temp [:model/Collection _ {:is_remote_synced true :name "Test Collection" :location "/"}
                   :model/RemoteSyncTask _ {:sync_task_type "foo"
                                            :ended_at :%now
                                            :version "other-version"}]
      (let [mock-main (test-helpers/create-mock-source)]
        (mt/with-temporary-setting-values [remote-sync-url "https://github.com/test/repo.git"
                                           remote-sync-token "test-token"
                                           remote-sync-branch "main"]
          (t2/insert! :model/RemoteSyncObject {:model_type "Card"
                                               :model_id 1
                                               :model_name "Test Card"
                                               :model_collection_id 1
                                               :status "update"
                                               :status_changed_at (java.time.OffsetDateTime/now)})
          ;; Stub the app-DB reconcile load — its correctness is covered synchronously by impl-test; here we
          ;; only verify the endpoint/flag wiring and that the async task completes (loading into a real
          ;; warehouse DB inside the task thread is slow/racy on the MySQL/MariaDB app-db matrix).
          (mt/with-dynamic-fn-redefs [source/source-from-settings (constantly mock-main)
                                      impl/load-snapshot! (constantly nil)]
            (testing "merge=true succeeds even with unsaved local changes"
              (let [{:keys [task_id]} (mt/user-http-request :crowberto :post 200 "ee/remote-sync/import" {:merge true :expected_branch "main"})
                    completed-task (wait-for-task-completion task_id)]
                (is (remote-sync.task/successful? completed-task))))))))))

(deftest import-merge-noop-when-remote-not-advanced-test
  (testing "POST /import merge=true when the remote has NOT advanced is a no-op success that keeps local dirty (not a spurious 'history was rewritten' conflict)"
    (mt/with-temp [:model/Collection _ {:is_remote_synced true :name "Test Collection" :location "/"}
                   ;; last-synced version == the mock's current version -> not diverged
                   :model/RemoteSyncTask _ {:sync_task_type "foo" :ended_at :%now :version "mock-version"}]
      (let [mock-main (test-helpers/create-mock-source)]
        (mt/with-temporary-setting-values [remote-sync-url "https://github.com/test/repo.git"
                                           remote-sync-token "test-token"
                                           remote-sync-branch "main"]
          (t2/insert! :model/RemoteSyncObject {:model_type "Card"
                                               :model_id 1
                                               :model_name "Test Card"
                                               :model_collection_id 1
                                               :status "update"
                                               :status_changed_at (java.time.OffsetDateTime/now)})
          (mt/with-dynamic-fn-redefs [source/source-from-settings (constantly mock-main)]
            (let [{:keys [task_id]} (mt/user-http-request :crowberto :post 200 "ee/remote-sync/import" {:merge true :expected_branch "main"})
                  completed-task (wait-for-task-completion task_id)]
              (is (remote-sync.task/successful? completed-task)
                  "a merge pull with nothing new on the remote succeeds instead of failing as a history-rewritten conflict")
              (is (= "update" (t2/select-one-fn :status :model/RemoteSyncObject :model_id 1))
                  "the un-pushed local change is left untouched (still dirty)"))))))))

;;; --------------------------- async-import!/async-export! base-snapshot resolution ---------------------------
;;; These drive the async-*! wrappers end-to-end through a version-controllable fake Source, so the real
;;; base-snapshot resolution + 3-way merge run (no hand-built :base-snapshot, no stubbed compute-merge).
;;; load-snapshot! (the slow app-DB reconcile) is stubbed and captured to assert which branch ran.

(defn- card-yaml-fixture []
  {"collections/c1eidaaaaaaaaaaaaaaaa_coll/cards/card1eidaaaaaaaaaaaaa_q.yaml"
   (test-helpers/generate-card-yaml "card1eidaaaaaaaaaaaaa" "Q1" "c1eidaaaaaaaaaaaaaaaa")})

(deftest async-import-not-diverged-noop-test
  (testing "import merge=true with the remote NOT advanced (last-version == current): no-op success, local stays dirty, no reconcile load"
    (mt/with-temporary-setting-values [remote-sync-url    "https://github.com/test/repo.git"
                                       remote-sync-token  "test-token"
                                       remote-sync-branch "main"]
      (let [loaded (atom [])
            src    (test-helpers/versioned-source :current "v1" :trees {"v1" {}})]
        (mt/with-dynamic-fn-redefs [source/source-from-settings  (constantly src)
                                    remote-sync.task/last-version (constantly "v1")
                                    impl/load-snapshot!           (fn [snap & _] (swap! loaded conj (source.p/version snap)) nil)]
          (t2/insert! :model/RemoteSyncObject {:model_type "Card" :model_id 9001 :model_name "Local Card"
                                               :model_collection_id 1 :status "update"
                                               :status_changed_at (java.time.OffsetDateTime/now)})
          (let [{:keys [task_id]} (mt/user-http-request :crowberto :post 200 "ee/remote-sync/import" {:merge true :expected_branch "main"})
                task (wait-for-task-completion task_id)]
            (is (remote-sync.task/successful? task))
            (is (empty? @loaded) "no reconcile load happens when there is nothing to fold in")
            (is (= "update" (t2/select-one-fn :status :model/RemoteSyncObject :model_id 9001))
                "the un-pushed local change is left dirty")))))))

(deftest async-import-diverged-resolves-base-test
  (testing "import merge=true when diverged resolves the base from last-version and folds in a remote-only entity"
    (mt/with-temporary-setting-values [remote-sync-url    "https://github.com/test/repo.git"
                                       remote-sync-token  "test-token"
                                       remote-sync-branch "main"]
      (let [loaded (atom [])
            src    (test-helpers/versioned-source :current "v2" :trees {"v1" {} "v2" (card-yaml-fixture)})]
        (mt/with-dynamic-fn-redefs [source/source-from-settings  (constantly src)
                                    remote-sync.task/last-version (constantly "v1") ; != current -> diverged, base resolvable
                                    ;; simulate load-snapshot!'s contract (run the in-txn finalize: restore-dirty +
                                    ;; set-version) without the slow app-DB reconcile
                                    impl/load-snapshot!           (fn [_snap _ _ & {:keys [finalize!]}]
                                                                    (swap! loaded conj :loaded)
                                                                    (when finalize! (finalize!))
                                                                    nil)]
          (let [{:keys [task_id]} (mt/user-http-request :crowberto :post 200 "ee/remote-sync/import" {:merge true :expected_branch "main"})
                task (wait-for-task-completion task_id)]
            (is (remote-sync.task/successful? task))
            (is (= "v2" (:version task)) "version advances to the remote tip")
            (is (seq @loaded) "the real 3-way merge ran and its result was reconciled into the app DB")))))))

(deftest async-import-base-unreachable-test
  (testing "import merge=true when the base commit is unreachable (orphaned) -> conflict, no reconcile load"
    (mt/with-temporary-setting-values [remote-sync-url    "https://github.com/test/repo.git"
                                       remote-sync-token  "test-token"
                                       remote-sync-branch "main"]
      (let [loaded (atom [])
            src    (test-helpers/versioned-source :current "v2" :trees {"v2" (card-yaml-fixture)})] ; "gone" absent
        (mt/with-dynamic-fn-redefs [source/source-from-settings  (constantly src)
                                    remote-sync.task/last-version (constantly "gone")
                                    impl/load-snapshot!           (fn [_snap & _] (swap! loaded conj :loaded) nil)]
          (let [{:keys [task_id]} (mt/user-http-request :crowberto :post 200 "ee/remote-sync/import" {:merge true :expected_branch "main"})
                task (wait-for-task-completion task_id)]
            (is (remote-sync.task/conflict? task))
            (is (empty? @loaded) "no load on an unresolvable-base conflict")))))))

(deftest async-import-no-prior-sync-test
  (testing "import merge=true with no prior sync (last-version nil) -> conflict (no base to merge against)"
    (mt/with-temporary-setting-values [remote-sync-url    "https://github.com/test/repo.git"
                                       remote-sync-token  "test-token"
                                       remote-sync-branch "main"]
      (let [src (test-helpers/versioned-source :current "v2" :trees {"v2" (card-yaml-fixture)})]
        (mt/with-dynamic-fn-redefs [source/source-from-settings  (constantly src)
                                    remote-sync.task/last-version (constantly nil)
                                    impl/load-snapshot!           (fn [_snap & _] nil)]
          (let [{:keys [task_id]} (mt/user-http-request :crowberto :post 200 "ee/remote-sync/import" {:merge true :expected_branch "main"})
                task (wait-for-task-completion task_id)]
            (is (remote-sync.task/conflict? task))))))))

(deftest async-export-diverged-resolves-base-test
  (testing "export merge=true when diverged resolves the base, runs the real merge, writes, and reconciles"
    (mt/with-temporary-setting-values [remote-sync-type :read-write]
      (mt/with-temp [:model/Collection _ {:is_remote_synced true :name "Test Collection" :location "/"}]
        (mt/with-temporary-setting-values [remote-sync-url    "https://github.com/test/repo.git"
                                           remote-sync-token  "test-token"
                                           remote-sync-branch "main"]
          (let [loaded (atom [])
                src    (test-helpers/versioned-source :current "v2" :trees {"v1" {} "v2" {}})]
            (mt/with-dynamic-fn-redefs [source/source-from-settings  (constantly src)
                                        remote-sync.task/last-version (constantly "v1") ; diverged, base resolvable
                                        impl/load-snapshot!           (fn [_snap _ _ & {:keys [finalize!]}]
                                                                        (swap! loaded conj :loaded)
                                                                        (when finalize! (finalize!))
                                                                        nil)]
              (let [{:keys [task_id]} (mt/user-http-request :crowberto :post 200 "ee/remote-sync/export" {:branch "main" :merge true})
                    task (wait-for-task-completion task_id)]
                (is (remote-sync.task/successful? task))
                (is (seq @loaded) "the merged result is reconciled back into the app DB (the pull half)")))))))))

(deftest async-export-base-unreachable-test
  (testing "export merge=true when the base commit is unreachable -> conflict, nothing written"
    (mt/with-temporary-setting-values [remote-sync-type :read-write]
      (mt/with-temp [:model/Collection _ {:is_remote_synced true :name "Test Collection" :location "/"}]
        (mt/with-temporary-setting-values [remote-sync-url    "https://github.com/test/repo.git"
                                           remote-sync-token  "test-token"
                                           remote-sync-branch "main"]
          (let [loaded (atom [])
                src    (test-helpers/versioned-source :current "v2" :trees {"v2" {}})] ; "gone" absent
            (mt/with-dynamic-fn-redefs [source/source-from-settings  (constantly src)
                                        remote-sync.task/last-version (constantly "gone")
                                        impl/load-snapshot!           (fn [_snap & _] (swap! loaded conj :loaded) nil)]
              (let [{:keys [task_id]} (mt/user-http-request :crowberto :post 200 "ee/remote-sync/export" {:branch "main" :merge true})
                    task (wait-for-task-completion task_id)]
                (is (remote-sync.task/conflict? task))
                (is (empty? @loaded) "no reconcile on an unresolvable-base conflict")))))))))

;;; ------------------------------------------------- Export Endpoint -------------------------------------------------

(deftest export-errors-in-read-only-mode-test
  (testing "POST /api/ee/remote-sync/export errors when in read-only sync mode"
    (mt/with-temporary-setting-values [remote-sync-url "https://github.com/test/repo.git" remote-sync-type :read-only]
      (mt/with-temp [:model/RemoteSyncTask _ {:sync_task_type "foo"}]
        (let [mock-source (test-helpers/create-mock-source)]
          (mt/with-temporary-setting-values [remote-sync-url "https://github.com/test/repo.git"
                                             remote-sync-token "test-token"
                                             remote-sync-branch "main"]
            (mt/with-dynamic-fn-redefs [source/source-from-settings (constantly mock-source)]
              (is (= "Exports are only allowed when remote-sync-type is set to 'read-write'"
                     (mt/user-http-request :crowberto :post 400 "ee/remote-sync/export" {:branch "main"}))))))))))

(deftest export-with-default-settings-test
  (testing "POST /api/ee/remote-sync/export succeeds with default settings"
    (mt/with-temporary-setting-values [remote-sync-type :read-write]
      (mt/with-temp [:model/Collection _ {:is_remote_synced true :name "Test Collection" :location "/"}]
        (let [mock-main (test-helpers/create-mock-source)]
          (mt/with-temporary-setting-values [remote-sync-url "https://github.com/test/repo.git"
                                             remote-sync-token "test-token"
                                             remote-sync-branch "main"]
            (mt/with-dynamic-fn-redefs [source/source-from-settings (constantly mock-main)]
              (let [{:keys [task_id] :as resp} (mt/user-http-request :crowberto :post 200 "ee/remote-sync/export" {:branch "main"})
                    task (wait-for-task-completion task_id)]
                (is (remote-sync.task/successful? task))
                (is (=? {:message string? :task_id int?}
                        resp))))))))))

(deftest export-with-custom-branch-and-message-test
  (testing "POST /api/ee/remote-sync/export succeeds with a non-default branch and message when on that branch"
    (mt/with-temporary-setting-values [remote-sync-type :read-write]
      (mt/with-temp [:model/Collection _ {:is_remote_synced true :name "Test Collection" :location "/"}]
        (let [mock-main (test-helpers/create-mock-source)]
          (mt/with-temporary-setting-values [remote-sync-url "https://github.com/test/repo.git"
                                             remote-sync-token "test-token"
                                             remote-sync-branch "feature-branch"]
            (mt/with-dynamic-fn-redefs [source/source-from-settings (constantly mock-main)]
              (let [{:keys [task_id] :as resp} (mt/user-http-request :crowberto :post 200 "ee/remote-sync/export"
                                                                     {:branch "feature-branch" :message "Custom export message"})
                    task (wait-for-task-completion task_id)]
                (is (=? {:message string? :task_id int?}
                        resp))
                (is (remote-sync.task/successful? task))))))))))

(deftest export-requires-superuser-test
  (testing "POST /api/ee/remote-sync/export requires superuser permissions"
    (mt/with-temporary-setting-values [remote-sync-type :read-write
                                       remote-sync-url "file://repo.git"]
      (is (= "You don't have permissions to do that."
             (mt/user-http-request :rasta :post 403 "ee/remote-sync/export" {:branch "main"}))))))

(deftest export-errors-when-remote-sync-disabled-test
  (testing "POST /api/ee/remote-sync/export errors when remote sync is disabled"
    (mt/with-temporary-setting-values [remote-sync-type :read-write
                                       remote-sync-url nil]
      (is (= "Remote sync is not configured."
             (mt/user-http-request :crowberto :post 400 "ee/remote-sync/export" {:branch "main"}))))))

(deftest export-handles-write-errors-test
  (testing "POST /api/ee/remote-sync/export handles write errors"
    (mt/with-temporary-setting-values [remote-sync-type :read-write]
      (mt/with-temp [:model/Collection {coll-id :id} {:is_remote_synced true :name "Test Collection" :location "/"}]
        ;; A pending create makes the export take the incremental write path, where the error fires.
        (t2/insert! :model/RemoteSyncObject {:model_type "Collection" :model_id coll-id :model_name "Test Collection"
                                             :status "create" :status_changed_at (t/offset-date-time)})
        (let [mock-main (test-helpers/create-mock-source :fail-mode :apply-changes-error)]
          (mt/with-temporary-setting-values [remote-sync-url "https://github.com/test/repo.git"
                                             remote-sync-token "test-token"
                                             remote-sync-branch "main"]
            (mt/with-dynamic-fn-redefs [source/source-from-settings (constantly mock-main)]
              (let [response (mt/user-http-request :crowberto :post 200 "ee/remote-sync/export" {:branch "main"})
                    task (wait-for-task-completion (:task_id response))]
                (is (remote-sync.task/failed? task))))))))))

(deftest export-errors-when-task-already-exists-test
  (testing "POST /api/ee/remote-sync/export errors when task already exists"
    (mt/with-temporary-setting-values [remote-sync-type :read-write]
      (mt/with-temp [:model/RemoteSyncTask _ {:sync_task_type "foo"}]
        (let [mock-source (test-helpers/create-mock-source)]
          (mt/with-temporary-setting-values [remote-sync-url "https://github.com/test/repo.git"
                                             remote-sync-token "test-token"
                                             remote-sync-branch "main"]
            (mt/with-dynamic-fn-redefs [source/source-from-settings (constantly mock-source)]
              (is (= "Remote sync task in progress"
                     (mt/user-http-request :crowberto :post 400 "ee/remote-sync/export" {:branch "main"}))))))))))

(deftest export-merges-if-external-changes-test
  (testing "POST /api/ee/remote-sync/export with merge=true merges when the remote is ahead of the last sync"
    (mt/with-temporary-setting-values [remote-sync-type :read-write]
      (mt/with-temp [:model/Collection _ {:is_remote_synced true :name "Test Collection" :location "/"}
                     :model/RemoteSyncTask _ {:sync_task_type "foo"
                                              :ended_at :%now
                                              :version "other-version"}]
        (let [mock-source (test-helpers/create-mock-source)]
          (mt/with-temporary-setting-values [remote-sync-url "https://github.com/test/repo.git"
                                             remote-sync-token "test-token"
                                             remote-sync-branch "main"]
            ;; Stub the app-DB reconcile load — its correctness is covered synchronously by impl-test; here we
            ;; only verify the endpoint/flag wiring and task completion (loading into a real warehouse DB
            ;; inside the task thread is slow/racy on the MySQL/MariaDB app-db matrix).
            (mt/with-dynamic-fn-redefs [source/source-from-settings (constantly mock-source)
                                        impl/load-snapshot! (constantly nil)]
              (testing "merge=true reconciles non-conflicting remote changes and succeeds"
                (let [response (mt/user-http-request :crowberto :post 200 "ee/remote-sync/export" {:merge true :branch "main"})
                      task     (wait-for-task-completion (:task_id response))]
                  (is (remote-sync.task/successful? task)
                      "non-conflicting remote changes are merged in, so the export succeeds")))
              (testing "without the merge flag a diverged export ends in conflict, not success"
                (mt/with-temp [:model/RemoteSyncTask _ {:sync_task_type "bar"
                                                        :ended_at :%now
                                                        :version "other-version"}]
                  (let [response (mt/user-http-request :crowberto :post 200 "ee/remote-sync/export" {:branch "main"})
                        task     (wait-for-task-completion (:task_id response))]
                    (is (remote-sync.task/conflict? task)
                        "a diverged export without force/merge surfaces a conflict for the UI to resolve"))))
              (testing "Can export when the versions match"
                (mt/with-temp [:model/RemoteSyncTask _ {:sync_task_type "foo"
                                                        :ended_at :%now
                                                        :version "mock-version"}]
                  (mt/user-http-request :crowberto :post 200 "ee/remote-sync/export" {:branch "main"}))))))))))

(deftest export-preflight-test
  (testing "GET /api/ee/remote-sync/export-preflight previews a merge without writing"
    (mt/with-temporary-setting-values [remote-sync-type :read-write]
      (mt/with-temp [:model/Collection _ {:is_remote_synced true :name "Test Collection" :location "/"}]
        (let [mock-source (test-helpers/create-mock-source)]
          (mt/with-temporary-setting-values [remote-sync-url "https://github.com/test/repo.git"
                                             remote-sync-token "test-token"
                                             remote-sync-branch "main"]
            (mt/with-dynamic-fn-redefs [source/source-from-settings (constantly mock-source)]
              (testing "no prior sync -> not diverged"
                (let [resp (mt/user-http-request :crowberto :get 200 "ee/remote-sync/export-preflight?branch=main")]
                  (is (false? (:has_changes resp)))))
              (testing "remote ahead with non-conflicting changes -> clean merge available"
                (mt/with-temp [:model/RemoteSyncTask _ {:sync_task_type "foo"
                                                        :ended_at :%now
                                                        :version "other-version"}]
                  (let [resp (mt/user-http-request :crowberto :get 200 "ee/remote-sync/export-preflight?branch=main")]
                    (is (true? (:has_changes resp)))
                    (is (true? (:clean resp)))
                    (is (= [] (:conflicts resp)))))))))))))

(deftest export-rejects-branch-mismatch-test
  (testing "export and export-preflight reject a requested branch that disagrees with the configured setting (multi-tab CAS guard)"
    (mt/with-temporary-setting-values [remote-sync-type :read-write]
      (mt/with-temp [:model/Collection _ {:is_remote_synced true :name "Test Collection" :location "/"}]
        (let [mock-main (test-helpers/create-mock-source)]
          (mt/with-temporary-setting-values [remote-sync-url    "https://github.com/test/repo.git"
                                             remote-sync-token  "test-token"
                                             remote-sync-branch "main"]
            (mt/with-dynamic-fn-redefs [source/source-from-settings (constantly mock-main)]
              (testing "POST /export -> 409 with a branch_mismatch flag and the current branch"
                (let [before (t2/count :model/RemoteSyncTask)
                      resp   (mt/user-http-request :crowberto :post 409 "ee/remote-sync/export" {:branch "stale-branch"})]
                  (is (true? (:branch_mismatch resp)))
                  (is (= "main" (:current_branch resp)))
                  (is (string? (:message resp)))
                  (is (= before (t2/count :model/RemoteSyncTask))
                      "no RemoteSyncTask row is created when the guard fires")))
              (testing "GET /export-preflight -> 409 with a branch_mismatch flag and the current branch"
                (let [resp (mt/user-http-request :crowberto :get 409 "ee/remote-sync/export-preflight?branch=stale-branch")]
                  (is (true? (:branch_mismatch resp)))
                  (is (= "main" (:current_branch resp))))))))))))

(deftest export-force-if-external-changes-test
  (testing "POST /api/ee/remote-sync/export can force sync when remote is ahead of the last sync"
    (mt/with-temporary-setting-values [remote-sync-type :read-write]
      (mt/with-temp [:model/RemoteSyncTask _ {:sync_task_type "foo"
                                              :ended_at :%now
                                              :version "other-version"}]
        (let [mock-source (test-helpers/create-mock-source)]
          (mt/with-temporary-setting-values [remote-sync-url "https://github.com/test/repo.git"
                                             remote-sync-token "test-token"
                                             remote-sync-branch "main"]
            (mt/with-dynamic-fn-redefs [source/source-from-settings (constantly mock-source)]
              (testing "Can export with force"
                (mt/user-http-request :crowberto :post 200 "ee/remote-sync/export" {:force true :branch "main"})))))))))

(deftest export-after-collection-rename-moves-contents-test
  (testing "GHY-4642: pushing after a synced collection is renamed moves its contents' files under the new collection path"
    (mt/with-temporary-setting-values [remote-sync-type :read-write]
      (mt/with-temp [:model/Collection {coll-id :id} {:name "Collection 2" :location "/"}
                     :model/Card _ {:name "Question 2" :collection_id coll-id}]
        (let [source     (test-helpers/versioned-source :current "v-remote" :trees {"v-remote" {}})
              repo-files #(set (source.p/list-files (source.p/snapshot source)))
              push!      #(wait-for-task-completion
                           (:task_id (mt/user-http-request :crowberto :post 200 "ee/remote-sync/export" {:branch "main"})))]
          (mt/with-temporary-setting-values [remote-sync-url "https://github.com/test/repo.git"
                                             remote-sync-token "test-token"
                                             remote-sync-branch "main"]
            (mt/with-dynamic-fn-redefs [source/source-from-settings (constantly source)
                                        settings/check-and-update-remote-settings! (constantly nil)
                                        impl/finish-remote-config! (constantly nil)]
              (mt/user-http-request :crowberto :put 200 "ee/remote-sync/settings" {:collections {coll-id true}})
              (is (remote-sync.task/successful? (push!)))
              (is (= #{"collections/main/collection_2.yaml"
                       "collections/main/collection_2/question_2.yaml"}
                     (repo-files)))
              (mt/user-http-request :crowberto :put 200 (str "collection/" coll-id) {:name "Marketing Reports"})
              (is (remote-sync.task/successful? (push!)))
              (is (= #{"collections/main/marketing_reports.yaml"
                       "collections/main/marketing_reports/question_2.yaml"}
                     (repo-files))))))))))

(deftest export-includes-model-actions-test
  (testing "GHY-4722: pushing a synced model writes its actions into the model's collection"
    (mt/with-temporary-setting-values [remote-sync-type :read-write]
      (mt/with-actions-enabled
        (mt/with-model-cleanup [:model/Action :model/Card]
          (mt/with-temp [:model/Collection {coll-id :id} {:name "Synced" :location "/"}]
            (let [source     (test-helpers/versioned-source :current "v-remote" :trees {"v-remote" {}})
                  repo-files #(set (source.p/list-files (source.p/snapshot source)))
                  push!      #(wait-for-task-completion
                               (:task_id (mt/user-http-request :crowberto :post 200 "ee/remote-sync/export" {:branch "main"})))]
              (mt/with-temporary-setting-values [remote-sync-url "https://github.com/test/repo.git"
                                                 remote-sync-token "test-token"
                                                 remote-sync-branch "main"]
                (mt/with-dynamic-fn-redefs [source/source-from-settings (constantly source)
                                            settings/check-and-update-remote-settings! (constantly nil)
                                            impl/finish-remote-config! (constantly nil)]
                  (mt/user-http-request :crowberto :put 200 "ee/remote-sync/settings" {:collections {coll-id true}})
                  (is (remote-sync.task/successful? (push!)))
                  (let [model (mt/user-http-request :crowberto :post 200 "card"
                                                    {:name                   "Venues Model"
                                                     :type                   "model"
                                                     :collection_id          coll-id
                                                     :display                "table"
                                                     :visualization_settings {}
                                                     :dataset_query          (mt/mbql-query venues)})]
                    (mt/user-http-request :crowberto :post 200 "action"
                                          {:name     "Create Venue"
                                           :type     "implicit"
                                           :kind     "row/create"
                                           :model_id (:id model)})
                    (mt/user-http-request :crowberto :post 200 "action"
                                          {:name          "Rename Venue"
                                           :type          "query"
                                           :model_id      (:id model)
                                           :database_id   (mt/id)
                                           :dataset_query {:type     "native"
                                                           :database (mt/id)
                                                           :native   {:query "UPDATE venues SET name = 'x' WHERE id = 1"}}
                                           :parameters    []})
                    (is (remote-sync.task/successful? (push!)))
                    (is (every? (repo-files) ["collections/main/synced/create_venue.yaml"
                                              "collections/main/synced/rename_venue.yaml"]))))))))))))

(deftest push-tracks-action-changes-on-pushed-model-test
  (testing "GHY-4722: creating, editing, and deleting an action on an already-pushed model each reach the repo on the next push"
    (mt/with-temporary-setting-values [remote-sync-type :read-write]
      (mt/with-actions-enabled
        (mt/with-model-cleanup [:model/Action :model/Card]
          (mt/with-temp [:model/Collection {coll-id :id} {:name "Synced" :location "/"}]
            (let [source     (test-helpers/versioned-source :current "v-remote" :trees {"v-remote" {}})
                  repo-file  #(source.p/read-file (source.p/snapshot source) %)
                  push!      #(wait-for-task-completion
                               (:task_id (mt/user-http-request :crowberto :post 200 "ee/remote-sync/export" {:branch "main"})))]
              (mt/with-temporary-setting-values [remote-sync-url "https://github.com/test/repo.git"
                                                 remote-sync-token "test-token"
                                                 remote-sync-branch "main"]
                (mt/with-dynamic-fn-redefs [source/source-from-settings (constantly source)
                                            settings/check-and-update-remote-settings! (constantly nil)
                                            impl/finish-remote-config! (constantly nil)]
                  (mt/user-http-request :crowberto :put 200 "ee/remote-sync/settings" {:collections {coll-id true}})
                  (let [model (mt/user-http-request :crowberto :post 200 "card"
                                                    {:name                   "Venues Model"
                                                     :type                   "model"
                                                     :collection_id          coll-id
                                                     :display                "table"
                                                     :visualization_settings {}
                                                     :dataset_query          (mt/mbql-query venues)})]
                    (is (remote-sync.task/successful? (push!)))
                    (is (nil? (repo-file "collections/main/synced/create_venue.yaml")))
                    (let [action (mt/user-http-request :crowberto :post 200 "action"
                                                       {:name     "Create Venue"
                                                        :type     "implicit"
                                                        :kind     "row/create"
                                                        :model_id (:id model)})]
                      (testing "a new action"
                        (is (remote-sync.task/successful? (push!)))
                        (is (some? (repo-file "collections/main/synced/create_venue.yaml"))))
                      (testing "an edited action"
                        (mt/user-http-request :crowberto :put 200 (str "action/" (:id action))
                                              {:description "Adds a venue"})
                        (is (remote-sync.task/successful? (push!)))
                        (is (str/includes? (str (repo-file "collections/main/synced/create_venue.yaml")) "Adds a venue")))
                      (testing "a deleted action"
                        (mt/user-http-request :crowberto :delete 204 (str "action/" (:id action)))
                        (is (remote-sync.task/successful? (push!)))
                        (is (nil? (repo-file "collections/main/synced/create_venue.yaml")))))))))))))))

(defn- do-with-pushed-model!
  "Pushes a model \"Venues Model\" in a synced collection through the API, then calls `f` with a map of:
  `:coll-id`, `:model`, `:model-path` (its YAML path in the repo), `:push!` and `:pull!` (each returns the finished
  task), `:repo-file` (path -> content or nil), `:repo-files` (the set of paths), and `:commit-remote!`, which takes
  `{:upsert {path content} :delete [path]}` and commits it straight to the repo, as another instance's push would."
  [f]
  (mt/with-temporary-setting-values [remote-sync-type :read-write]
    (mt/with-actions-enabled
      (mt/with-model-cleanup [:model/Action :model/Card]
        (mt/with-temp [:model/Collection {coll-id :id} {:name "Synced" :location "/"}]
          (let [source         (test-helpers/versioned-source :current "v-remote" :trees {"v-remote" {}})
                repo-file      #(source.p/read-file (source.p/snapshot source) %)
                repo-files     #(set (source.p/list-files (source.p/snapshot source)))
                push!          #(wait-for-task-completion
                                 (:task_id (mt/user-http-request :crowberto :post 200 "ee/remote-sync/export" {:branch "main"})))
                pull!          #(wait-for-task-completion
                                 (:task_id (mt/user-http-request :crowberto :post 200 "ee/remote-sync/import" {:expected_branch "main"})))
                commit-remote! (fn [{:keys [upsert delete]}]
                                 (let [commit (source.p/open-commit (source.p/snapshot source))]
                                   (doseq [[path content] upsert]
                                     (source.p/stage-upsert! commit {:path path :content content}))
                                   (doseq [path delete]
                                     (source.p/stage-delete! commit path))
                                   (source.p/finish-commit! commit "Another instance's change")))]
            (mt/with-temporary-setting-values [remote-sync-url "https://github.com/test/repo.git"
                                               remote-sync-token "test-token"
                                               remote-sync-branch "main"]
              (mt/with-dynamic-fn-redefs [source/source-from-settings (constantly source)
                                          settings/check-and-update-remote-settings! (constantly nil)
                                          impl/finish-remote-config! (constantly nil)]
                (mt/user-http-request :crowberto :put 200 "ee/remote-sync/settings" {:collections {coll-id true}})
                (let [model (mt/user-http-request :crowberto :post 200 "card"
                                                  {:name                   "Venues Model"
                                                   :type                   "model"
                                                   :collection_id          coll-id
                                                   :display                "table"
                                                   :visualization_settings {}
                                                   :dataset_query          (mt/mbql-query venues)})]
                  (is (remote-sync.task/successful? (push!)))
                  (f {:coll-id        coll-id
                      :model          model
                      :model-path     (some #(when (str/includes? % "venues_model") %) (repo-files))
                      :push!          push!
                      :pull!          pull!
                      :repo-file      repo-file
                      :repo-files     repo-files
                      :commit-remote! commit-remote!}))))))))))

(defn- do-with-pushed-model-actions!
  "Pushes a synced model with an implicit action (`collections/main/synced/create_venue.yaml`) and a query action
  (`collections/main/synced/rename_venue.yaml`) through the API, then calls `f` with the map from [[do-with-pushed-model!]]."
  [f]
  (do-with-pushed-model!
   (fn [{:keys [model push! repo-file] :as ctx}]
     (mt/user-http-request :crowberto :post 200 "action"
                           {:name "Create Venue" :type "implicit" :kind "row/create" :model_id (:id model)})
     (mt/user-http-request :crowberto :post 200 "action"
                           {:name          "Rename Venue"
                            :type          "query"
                            :model_id      (:id model)
                            :database_id   (mt/id)
                            :dataset_query {:type     "native"
                                            :database (mt/id)
                                            :native   {:query "UPDATE venues SET name = 'x' WHERE id = 1"}}
                            :parameters    []})
     (is (remote-sync.task/successful? (push!)))
     (is (some? (repo-file "collections/main/synced/create_venue.yaml")))
     (is (some? (repo-file "collections/main/synced/rename_venue.yaml")))
     (f ctx))))

(deftest push-after-model-becomes-question-removes-actions-test
  (testing "turning a pushed model into a question removes its actions from the repo on the next push"
    (do-with-pushed-model-actions!
     (fn [{:keys [model push! repo-file]}]
       (mt/user-http-request :crowberto :put 200 (str "card/" (:id model)) {:type "question"})
       (is (remote-sync.task/successful? (push!)))
       (is (nil? (repo-file "collections/main/synced/create_venue.yaml")))
       (is (nil? (repo-file "collections/main/synced/rename_venue.yaml")))))))

(deftest push-after-model-query-drops-implicit-actions-test
  (testing "GHY-4722: a model query that no longer supports implicit actions removes them from the repo on the next push"
    (do-with-pushed-model-actions!
     (fn [{:keys [model push! repo-file]}]
       (mt/user-http-request :crowberto :put 200 (str "card/" (:id model))
                             {:dataset_query (mt/mbql-query venues {:filter [:> $price 1]})})
       (is (remote-sync.task/successful? (push!)))
       (is (nil? (repo-file "collections/main/synced/create_venue.yaml")))
       (is (some? (repo-file "collections/main/synced/rename_venue.yaml")) "the query action stays")))))

(deftest push-after-model-deleted-removes-actions-test
  (testing "GHY-4722: deleting a pushed model removes its actions from the repo on the next push"
    (do-with-pushed-model-actions!
     (fn [{:keys [model push! repo-file]}]
       (mt/user-http-request :crowberto :delete 204 (str "card/" (:id model)))
       (is (remote-sync.task/successful? (push!)))
       (is (nil? (repo-file "collections/main/synced/create_venue.yaml")))
       (is (nil? (repo-file "collections/main/synced/rename_venue.yaml")))))))

(defn- insert-untracked-action!
  "Inserts an implicit action \"Local Action\" (`collections/main/synced/local_action.yaml`) on the model with `model-id` straight
  into the app DB, so no event records it in the remote sync ledger. Returns its id."
  [model-id]
  (let [action-id (t2/insert-returning-pk! :model/Action {:name       "Local Action"
                                                          :type       :implicit
                                                          :model_id   model-id
                                                          :created_at :%now
                                                          :updated_at :%now})]
    (t2/insert! :model/ImplicitAction {:action_id action-id :kind "row/create"})
    action-id))

(defn- action-rso-status
  "The status of the ledger row of the Action with `action-id`, or nil if it has none."
  [action-id]
  (t2/select-one-fn :status :model/RemoteSyncObject :model_type "Action" :model_id action-id))

(deftest pull-that-deletes-a-model-keeps-the-ledger-clean-test
  (testing "GHY-4722: a pull that deletes a model with a local, untracked action leaves the instance clean"
    (do-with-pushed-model!
     (fn [{:keys [model model-path pull! commit-remote!]}]
       (insert-untracked-action! (:id model))
       (commit-remote! {:delete [model-path]})
       (let [task (pull!)]
         (is (remote-sync.task/successful? task))
         (is (= "pulled" (get-in task [:outcome :kind]))))
       (is (not (t2/exists? :model/Card :id (:id model))))
       (is (false? (remote-sync.object/dirty?)))
       (is (not (t2/exists? :model/RemoteSyncObject :model_type "Action")))))))

(deftest pull-that-turns-a-model-into-a-question-keeps-the-ledger-clean-test
  (testing "GHY-4722: a pull that turns a model with a local, untracked action into a question leaves the instance clean"
    (do-with-pushed-model!
     (fn [{:keys [model model-path pull! repo-file commit-remote!]}]
       (insert-untracked-action! (:id model))
       (commit-remote! {:upsert {model-path (str/replace (repo-file model-path) "type: model" "type: question")}})
       (let [task (pull!)]
         (is (remote-sync.task/successful? task))
         (is (= "pulled" (get-in task [:outcome :kind]))))
       (is (= :question (t2/select-one-fn :type :model/Card :id (:id model))))
       (is (false? (remote-sync.object/dirty?)))
       (is (not (t2/exists? :model/RemoteSyncObject :model_type "Action")))))))

(deftest push-tracks-deleting-an-action-test
  (testing "deleting a model's action removes its file on the next push"
    (do-with-pushed-model-actions!
     (fn [{:keys [model push! repo-file]}]
       (let [action-id (t2/select-one-pk :model/Action :model_id (:id model) :name "Rename Venue")]
         (is (some? (repo-file "collections/main/synced/rename_venue.yaml")))
         (mt/user-http-request :crowberto :delete 204 (str "action/" action-id))
         (is (remote-sync.task/successful? (push!)))
         (is (nil? (repo-file "collections/main/synced/rename_venue.yaml"))))))))

(deftest turning-sync-on-again-tracks-untracked-actions-test
  (testing "GHY-4722: turning sync on again for a collection tracks an action created while sync was off, and the next push writes it"
    (do-with-pushed-model!
     (fn [{:keys [coll-id model push! repo-file]}]
       (mt/user-http-request :crowberto :put 200 "ee/remote-sync/settings" {:collections {coll-id false}})
       (let [action-id (insert-untracked-action! (:id model))]
         (mt/user-http-request :crowberto :put 200 "ee/remote-sync/settings" {:collections {coll-id true}})
         (is (= "create" (action-rso-status action-id)))
         (is (remote-sync.task/successful? (push!)))
         (is (some? (repo-file "collections/main/synced/local_action.yaml"))))))))

(deftest archiving-a-collection-marks-its-actions-for-deletion-test
  (testing "GHY-4722: archiving the synced collection that holds a model marks its actions for deletion, and the next push removes their files"
    (do-with-pushed-model-actions!
     (fn [{:keys [coll-id model push! repo-file]}]
       (let [action-id (t2/select-one-pk :model/Action :model_id (:id model) :name "Rename Venue")]
         (mt/user-http-request :crowberto :put 200 (str "collection/" coll-id) {:archived true})
         (is (= "delete" (action-rso-status action-id)))
         (is (remote-sync.task/successful? (push!)))
         (is (nil? (repo-file "collections/main/synced/rename_venue.yaml")))
         (is (nil? (repo-file "collections/main/synced/create_venue.yaml"))))))))

(deftest upgrade-backfill-requires-a-push-before-a-pull-test
  (testing "GHY-4722: after the upgrade backfill tracks an existing action, a pull is refused until a push writes the action to the repo"
    (do-with-pushed-model!
     (fn [{:keys [model model-path push! pull! repo-file commit-remote!]}]
       (let [action-id (insert-untracked-action! (:id model))]
         (remote-sync.events/backfill-action-tracking!)
         (is (= "create" (action-rso-status action-id)))
         (testing "a normal pull is refused"
           (is (= "There are unsaved changes in the Remote Sync collection which will be overwritten by the import. Force the import to discard these changes."
                  (:message (mt/user-http-request :crowberto :post 400 "ee/remote-sync/import" {:expected_branch "main"})))))
         (testing "a push writes the action to the repo"
           (is (remote-sync.task/successful? (push!)))
           (is (some? (repo-file "collections/main/synced/local_action.yaml"))))
         (testing "a normal pull after another instance's change keeps the action"
           (commit-remote! {:upsert {model-path (str/replace (repo-file model-path) "name: Venues Model" "name: Renamed Model")}})
           (is (remote-sync.task/successful? (pull!)))
           (is (= "Renamed Model" (t2/select-one-fn :name :model/Card :id (:id model))))
           (is (t2/exists? :model/Action :id action-id))))))))

;;; ------------------------------------------------- Current Task Endpoint -------------------------------------------------

(deftest current-task-requires-superuser-test
  (testing "GET /api/ee/remote-sync/current-task requires superuser permissions (GHY-3804)"
    (is (= "You don't have permissions to do that."
           (mt/user-http-request :rasta :get 403 "ee/remote-sync/current-task")))))

(deftest current-task-returns-nil-when-no-tasks-test
  (testing "GET /api/ee/remote-sync/current-task returns nil when there are no tasks"
    (is (nil? (mt/user-http-request :crowberto :get 204 "ee/remote-sync/current-task")))))

(deftest current-task-returns-active-task-test
  (testing "GET /api/ee/remote-sync/current-task returns the current task when one exists"
    (mt/with-temp [:model/RemoteSyncTask _ {:sync_task_type "export"
                                            :last_progress_report_at :%now
                                            :started_at :%now}]
      (is (=? {:id integer?
               :started_at some?
               :ended_at nil?}
              (mt/user-http-request :crowberto :get 200 "ee/remote-sync/current-task"))))))

(deftest current-task-returns-completed-task-test
  (testing "GET /api/ee/remote-sync/current-task returns completed task after completion"
    (mt/with-temp [:model/RemoteSyncTask {id :id} {:sync_task_type "export"
                                                   :last_progress_report_at :%now
                                                   :started_at :%now}]
      (remote-sync.task/complete-sync-task! id)
      (is (=? {:id integer?
               :started_at some?
               :ended_at some?}
              (mt/user-http-request :crowberto :get 200 "ee/remote-sync/current-task"))))))

(deftest current-task-returns-outcome-test
  (testing "GET /api/ee/remote-sync/current-task returns the structured outcome for a completed task (GHY-3747)"
    (mt/with-temp [:model/RemoteSyncTask {id :id} {:sync_task_type "import"
                                                   :last_progress_report_at :%now
                                                   :started_at :%now}]
      (remote-sync.task/complete-sync-task! id {:kind "pulled" :count 12 :branch "main"})
      (is (=? {:status "successful"
               :ended_at some?
               :outcome {:kind "pulled" :count 12 :branch "main"}}
              (mt/user-http-request :crowberto :get 200 "ee/remote-sync/current-task"))))))

(deftest current-task-returns-error-for-failed-task-test
  (testing "GET /api/ee/remote-sync/current-task returns error message for failed task"
    (mt/with-temp [:model/RemoteSyncTask {id :id} {:sync_task_type "export"
                                                   :last_progress_report_at :%now
                                                   :started_at :%now}]
      (remote-sync.task/fail-sync-task! id "Some error")
      (is (=? {:error_message "Some error"}
              (mt/user-http-request :crowberto :get 200 "ee/remote-sync/current-task"))))))

(deftest current-task-returns-cancelled-status-test
  (testing "GET /api/ee/remote-sync/current-task returns cancelled status for cancelled task"
    (mt/with-temp [:model/RemoteSyncTask {id :id} {:sync_task_type "export"
                                                   :last_progress_report_at :%now
                                                   :started_at :%now}]
      (remote-sync.task/cancel-sync-task! id)
      (is (=? {:cancelled true
               :error_message "Task cancelled"}
              (mt/user-http-request :crowberto :get 200 "ee/remote-sync/current-task"))))))

(deftest current-task-closes-a-stale-open-task-test
  (testing "GET /api/ee/remote-sync/current-task closes an open task whose owner is gone and returns it cancelled"
    (let [old-time (t/minus (t/offset-date-time) (t/hours 1))]
      (mt/with-temp [:model/RemoteSyncTask {id :id} {:sync_task_type "import"
                                                     :started_at old-time
                                                     :last_progress_report_at old-time}]
        (let [first-response (mt/user-http-request :crowberto :get 200 "ee/remote-sync/current-task")]
          (is (=? {:id id
                   :status "cancelled"
                   :cancelled true
                   :ended_at some?
                   :error_message #"^Sync was interrupted.*"}
                  first-response))
          (testing "a second GET returns the same closed row"
            (is (=? (select-keys first-response [:id :status :cancelled :ended_at :error_message])
                    (mt/user-http-request :crowberto :get 200 "ee/remote-sync/current-task")))))))))

(deftest current-task-leaves-a-live-task-running-test
  (testing "GET /api/ee/remote-sync/current-task returns a task with a fresh heartbeat as running even when its progress is stale"
    (mt/with-temp [:model/RemoteSyncTask {id :id} {:sync_task_type "import"
                                                   :started_at (t/minus (t/offset-date-time) (t/hours 1))
                                                   :last_progress_report_at (t/minus (t/offset-date-time) (t/hours 1))
                                                   :last_heartbeat_at :%now}]
      (is (=? {:id id :status "running" :ended_at nil}
              (mt/user-http-request :crowberto :get 200 "ee/remote-sync/current-task"))))))

(deftest current-task-carries-the-trimmed-initiating-user-test
  (testing "GET /api/ee/remote-sync/current-task carries the initiating user as id, first_name, last_name, email only"
    (mt/with-temp [:model/RemoteSyncTask _ {:sync_task_type "import"
                                            :initiated_by (mt/user->id :rasta)
                                            :started_at :%now
                                            :last_progress_report_at :%now}]
      (let [user (:initiated_by_user (mt/user-http-request :crowberto :get 200 "ee/remote-sync/current-task"))]
        (is (= {:id         (mt/user->id :rasta)
                :first_name "Rasta"
                :last_name  "Toucan"
                :email      "[email protected]"}
               user))))))

(deftest current-task-initiating-user-is-nil-for-system-tasks-test
  (testing "GET /api/ee/remote-sync/current-task carries a nil initiating user for a task with no initiator (auto-import)"
    (mt/with-temp [:model/RemoteSyncTask _ {:sync_task_type "import"
                                            :initiated_by nil
                                            :started_at :%now
                                            :last_progress_report_at :%now}]
      (let [response (mt/user-http-request :crowberto :get 200 "ee/remote-sync/current-task")]
        (is (contains? response :initiated_by_user))
        (is (nil? (:initiated_by_user response)))))))

;;; ------------------------------------------------- Cancel Task Endpoint -------------------------------------------------

(deftest cancel-task-carries-the-trimmed-initiating-user-test
  (testing "POST /api/ee/remote-sync/current-task/cancel returns the cancelled task with its trimmed initiating user"
    (mt/with-temp [:model/RemoteSyncTask _ {:sync_task_type "export"
                                            :initiated_by (mt/user->id :rasta)
                                            :started_at :%now
                                            :last_progress_report_at :%now}]
      (is (=? {:status            "cancelled"
               :initiated_by_user {:id (mt/user->id :rasta) :email "[email protected]"}}
              (mt/user-http-request :crowberto :post 200 "ee/remote-sync/current-task/cancel"))))))

(deftest cancel-task-requires-superuser-test
  (testing "POST /api/ee/remote-sync/current-task/cancel requires superuser permissions (GHY-3804)"
    (mt/with-temp [:model/RemoteSyncTask {id :id} {:sync_task_type "export"
                                                   :last_progress_report_at :%now
                                                   :started_at :%now}]
      (is (= "You don't have permissions to do that."
             (mt/user-http-request :rasta :post 403 "ee/remote-sync/current-task/cancel")))
      (testing "task is not cancelled by the non-superuser request"
        (is (not (remote-sync.task/cancelled? (t2/select-one :model/RemoteSyncTask :id id))))))))

(deftest cancel-task-errors-when-no-tasks-test
  (testing "POST /api/ee/remote-sync/current-task/cancel errors when there are no tasks"
    (is (= "No active task to cancel"
           (mt/user-http-request :crowberto :post 400 "ee/remote-sync/current-task/cancel")))))

(deftest cancel-task-errors-when-last-task-completed-test
  (testing "POST /api/ee/remote-sync/current-task/cancel errors when last task completed"
    (mt/with-temp [:model/RemoteSyncTask {id :id} {:sync_task_type "export"
                                                   :last_progress_report_at :%now
                                                   :started_at :%now}]
      (remote-sync.task/complete-sync-task! id)
      (is (= "No active task to cancel"
             (mt/user-http-request :crowberto :post 400 "ee/remote-sync/current-task/cancel"))))))

(deftest cancel-task-errors-when-last-task-errored-test
  (testing "POST /api/ee/remote-sync/current-task/cancel errors when last task errored"
    (mt/with-temp [:model/RemoteSyncTask {id :id} {:sync_task_type "export"
                                                   :last_progress_report_at :%now
                                                   :started_at :%now}]
      (remote-sync.task/fail-sync-task! id "Error msg")
      (is (= "No active task to cancel"
             (mt/user-http-request :crowberto :post 400 "ee/remote-sync/current-task/cancel"))))))

(deftest cancel-active-task-test
  (testing "POST /api/ee/remote-sync/current-task/cancel successfully cancels active task"
    (mt/with-temp [:model/RemoteSyncTask {id :id} {:sync_task_type "export"
                                                   :last_progress_report_at :%now
                                                   :started_at :%now}]
      (is (=? {:id id
               :cancelled true
               :error_message "Task cancelled"}
              (mt/user-http-request :crowberto :post 200 "ee/remote-sync/current-task/cancel")))
      (is (remote-sync.task/cancelled? (t2/select-one :model/RemoteSyncTask :id id))))))

;;; ------------------------------------------------- Is Dirty Endpoint -------------------------------------------------

(deftest is-dirty-returns-false-when-no-changes-test
  (testing "GET /api/ee/remote-sync/is-dirty returns false when no remote-synced collections have changes"
    (test-helpers/with-clean-object
      (mt/with-temp [:model/Collection _ {:name "Remote Collection"
                                          :is_remote_synced true
                                          :entity_id "test-collection-1"
                                          :location "/"}]
        (is (= {:is_dirty false}
               (mt/user-http-request :crowberto :get 200 "ee/remote-sync/is-dirty")))))))

(deftest is-dirty-returns-true-when-changes-exist-test
  (testing "GET /api/ee/remote-sync/is-dirty returns true when any remote-synced collection has changes"
    (test-helpers/with-clean-object
      (mt/with-temp [:model/Collection remote-col {:name "Remote Collection"
                                                   :is_remote_synced true
                                                   :entity_id "test-collection-1"
                                                   :location "/"}
                     :model/Card card {:collection_id (:id remote-col)
                                       :name "Test Card"}
                     :model/RemoteSyncObject _ {:model_type "Card"
                                                :model_id (:id card)
                                                :model_name "Test Card"
                                                :model_collection_id (:id remote-col)
                                                :status "pending"
                                                :status_changed_at (java.time.OffsetDateTime/now)}]
        (is (= {:is_dirty true}
               (mt/user-http-request :crowberto :get 200 "ee/remote-sync/is-dirty")))))))

(deftest is-dirty-requires-superuser-test
  (testing "GET /api/ee/remote-sync/is-dirty requires superuser permissions"
    (is (= "You don't have permissions to do that."
           (mt/user-http-request :rasta :get 403 "ee/remote-sync/is-dirty")))))

;;; ------------------------------------------------- Dirty Models Endpoint -------------------------------------------------

(deftest dirty-returns-empty-list-when-no-dirty-models-test
  (testing "GET /api/ee/remote-sync/dirty returns empty list when no dirty models"
    (test-helpers/with-clean-object
      (mt/with-temp [:model/Collection _ {:name "Remote Collection"
                                          :is_remote_synced true
                                          :entity_id "test-collection-1"
                                          :location "/"}]
        (is (= {:dirty []}
               (mt/user-http-request :crowberto :get 200 "ee/remote-sync/dirty")))))))

(deftest dirty-returns-all-dirty-models-test
  (testing "GET /api/ee/remote-sync/dirty returns all dirty models across remote-synced collections"
    (test-helpers/with-clean-object
      (mt/with-temp [:model/Collection remote-col1 {:name "Remote Collection 1"
                                                    :is_remote_synced true
                                                    :entity_id "test-collection-1"
                                                    :location "/"}
                     :model/Collection remote-col2 {:name "Remote Collection 2"
                                                    :is_remote_synced true
                                                    :entity_id "test-collection-2"
                                                    :location "/"}
                     :model/Card card1 {:collection_id (:id remote-col1)
                                        :name "Card 1"}
                     :model/Card card2 {:collection_id (:id remote-col2)
                                        :name "Card 2"}
                     :model/Dashboard dashboard {:collection_id (:id remote-col1)
                                                 :name "Dashboard 1"}
                     :model/RemoteSyncObject _ {:model_type "card"
                                                :model_id (:id card1)
                                                :model_name "Card 1"
                                                :model_collection_id (:id remote-col1)
                                                :status "pending"
                                                :status_changed_at (java.time.OffsetDateTime/now)}
                     :model/RemoteSyncObject _ {:model_type "card"
                                                :model_id (:id card2)
                                                :model_name "Card 2"
                                                :model_collection_id (:id remote-col2)
                                                :status "pending"
                                                :status_changed_at (java.time.OffsetDateTime/now)}
                     :model/RemoteSyncObject _ {:model_type "dashboard"
                                                :model_id (:id dashboard)
                                                :model_name "Dashboard 1"
                                                :model_collection_id (:id remote-col1)
                                                :status "pending"
                                                :status_changed_at (java.time.OffsetDateTime/now)}]
        (let [response (mt/user-http-request :crowberto :get 200 "ee/remote-sync/dirty")
              dirty-items (:dirty response)]
          (is (= 3 (count dirty-items)))
          (is (= #{"Card 1" "Card 2" "Dashboard 1"}
                 (set (map :name dirty-items))))
          (is (= #{"card" "dashboard"}
                 (set (map :model dirty-items)))))))))

(deftest dirty-returns-nested-collection-models-test
  (testing "GET /api/ee/remote-sync/dirty returns dirty models from nested collections"
    (test-helpers/with-clean-object
      (mt/with-temp [:model/Collection remote-col {:name "Remote Collection"
                                                   :is_remote_synced true
                                                   :entity_id "test-collection-1"
                                                   :location "/"}
                     :model/Collection nested-col {:name "Nested Collection"
                                                   :is_remote_synced true
                                                   :location (str "/" (:id remote-col) "/")}
                     :model/Card nested-card {:collection_id (:id nested-col)
                                              :name "Nested Card"}
                     :model/RemoteSyncObject _ {:model_type "card"
                                                :model_name "Nested Card"
                                                :model_collection_id (:id nested-col)
                                                :model_id (:id nested-card)
                                                :status "pending"
                                                :status_changed_at (java.time.OffsetDateTime/now)}]
        (let [response (mt/user-http-request :crowberto :get 200 "ee/remote-sync/dirty")
              dirty-items (:dirty response)]
          (is (= 1 (count dirty-items)))
          (is (= "Nested Card" (:name (first dirty-items)))))))))

(deftest dirty-deduplicates-items-test
  (testing "GET /api/ee/remote-sync/dirty deduplicates items"
    (test-helpers/with-clean-object
      (mt/with-temp [:model/Collection remote-col {:name "Remote Collection"
                                                   :is_remote_synced true
                                                   :entity_id "test-collection-1"
                                                   :location "/"}
                     :model/Card card {:collection_id (:id remote-col)
                                       :name "Test Card"}
                     :model/RemoteSyncObject _ {:model_type "Card"
                                                :model_id (:id card)
                                                :model_name "Test Card"
                                                :model_collection_id (:id remote-col)
                                                :status "pending"
                                                :status_changed_at (java.time.OffsetDateTime/now)}]
        (let [response (mt/user-http-request :crowberto :get 200 "ee/remote-sync/dirty")
              dirty-items (:dirty response)]
          (is (= 1 (count dirty-items)))
          (is (= "Test Card" (:name (first dirty-items)))))))))

(defn- mark-pushed!
  "Records `model-id` as synced with the file path and content hash of its current serialized form, the state
  a push leaves it in."
  [model-type model-id collection-id]
  (let [{:keys [path content-hash]} (source/row->file-info {:model_type model-type :model_id model-id})]
    (t2/delete! :model/RemoteSyncObject :model_type model-type :model_id model-id)
    (t2/insert! :model/RemoteSyncObject {:model_type          model-type
                                         :model_id            model-id
                                         :model_name          "pushed"
                                         :model_collection_id collection-id
                                         :status              "synced"
                                         :status_changed_at   (t/offset-date-time)
                                         :file_path           path
                                         :content_hash        content-hash})))

(defn- dirty-ids [model]
  (->> (mt/user-http-request :crowberto :get 200 "ee/remote-sync/dirty")
       :dirty
       (filter #(= model (:model %)))
       (map :id)
       set))

(deftest public-link-marks-dashboard-dirty-test
  (testing "GHY-4650: creating or revoking a dashboard's public link lists the dashboard in GET /dirty"
    (test-helpers/with-clean-object
      (mt/with-temporary-setting-values [enable-public-sharing true
                                         remote-sync-type      :read-write]
        (mt/with-temp [:model/Collection coll {:name "Remote Collection" :is_remote_synced true :location "/"}
                       :model/Dashboard dash {:name "Shared Dashboard" :collection_id (:id coll)}]
          (mark-pushed! "Dashboard" (:id dash) (:id coll))
          (is (not (contains? (dirty-ids "dashboard") (:id dash))))
          (testing "create"
            (mt/user-http-request :crowberto :post 200 (format "dashboard/%d/public_link" (:id dash)))
            (is (contains? (dirty-ids "dashboard") (:id dash))))
          (mark-pushed! "Dashboard" (:id dash) (:id coll))
          (testing "revoke"
            (mt/user-http-request :crowberto :delete 204 (format "dashboard/%d/public_link" (:id dash)))
            (is (contains? (dirty-ids "dashboard") (:id dash)))))))))

(deftest public-link-marks-card-dirty-test
  (testing "GHY-4650: creating or revoking a card's public link lists the card in GET /dirty"
    (test-helpers/with-clean-object
      (mt/with-temporary-setting-values [enable-public-sharing true
                                         remote-sync-type      :read-write]
        (mt/with-temp [:model/Collection coll {:name "Remote Collection" :is_remote_synced true :location "/"}
                       :model/Card card {:name "Shared Card" :collection_id (:id coll)}]
          (mark-pushed! "Card" (:id card) (:id coll))
          (is (not (contains? (dirty-ids "card") (:id card))))
          (testing "create"
            (mt/user-http-request :crowberto :post 200 (format "card/%d/public_link" (:id card)))
            (is (contains? (dirty-ids "card") (:id card))))
          (mark-pushed! "Card" (:id card) (:id coll))
          (testing "revoke"
            (mt/user-http-request :crowberto :delete 204 (format "card/%d/public_link" (:id card)))
            (is (contains? (dirty-ids "card") (:id card)))))))))

(defn- fail-first-tracking-write!
  "Calls `thunk` with the remote sync event handler rigged to throw on its first call only."
  [thunk]
  (let [original (mt/original-fn #'spec/determine-status)
        failed?  (atom false)]
    (mt/with-dynamic-fn-redefs [spec/determine-status (fn [& args]
                                                        (if (compare-and-set! failed? false true)
                                                          (throw (ex-info "simulated tracking failure" {}))
                                                          (apply original args)))]
      (thunk))))

(deftest public-link-change-survives-handler-failure-test
  (testing "GHY-4650: when tracking fails, the link change rolls back, so a retry changes the link and marks the item dirty"
    (test-helpers/with-clean-object
      (mt/with-temporary-setting-values [enable-public-sharing true
                                         remote-sync-type      :read-write]
        (doseq [[model endpoint] [["dashboard" "dashboard/%d/public_link"]
                                  ["card"      "card/%d/public_link"]]]
          (testing model
            (mt/with-temp [:model/Collection coll {:name "Remote Collection" :is_remote_synced true :location "/"}
                           (if (= model "dashboard") :model/Dashboard :model/Card) entity {:name "Shared" :collection_id (:id coll)}]
              (let [model-type (if (= model "dashboard") "Dashboard" "Card")
                    url        (format endpoint (:id entity))]
                (testing "create"
                  (mark-pushed! model-type (:id entity) (:id coll))
                  (fail-first-tracking-write!
                   (fn []
                     (mt/user-http-request :crowberto :post 500 url)
                     (mt/user-http-request :crowberto :post 200 url)))
                  (is (contains? (dirty-ids model) (:id entity))))
                (testing "revoke"
                  (mark-pushed! model-type (:id entity) (:id coll))
                  (fail-first-tracking-write!
                   (fn []
                     (mt/user-http-request :crowberto :delete 500 url)
                     (mt/user-http-request :crowberto :delete 204 url)))
                  (is (contains? (dirty-ids model) (:id entity))))))))))))

(deftest public-link-on-read-only-instance-is-not-dirty-test
  (testing "GHY-4650: a public link change on a read-only instance does not mark the synced item dirty, since it cannot be pushed"
    (test-helpers/with-clean-object
      (mt/with-temporary-setting-values [enable-public-sharing true
                                         remote-sync-url       "https://github.com/test/repo.git"
                                         remote-sync-type      :read-only]
        (mt/with-temp [:model/Collection coll {:name "Remote Collection" :is_remote_synced true :location "/"}
                       :model/Dashboard dash {:name "Shared Dashboard" :collection_id (:id coll)}
                       :model/Card card {:name "Shared Card" :collection_id (:id coll)}]
          (mark-pushed! "Dashboard" (:id dash) (:id coll))
          (mark-pushed! "Card" (:id card) (:id coll))
          (mt/user-http-request :crowberto :post 200 (format "dashboard/%d/public_link" (:id dash)))
          (mt/user-http-request :crowberto :post 200 (format "card/%d/public_link" (:id card)))
          (is (not (contains? (dirty-ids "dashboard") (:id dash))))
          (is (not (contains? (dirty-ids "card") (:id card))))
          (mt/user-http-request :crowberto :delete 204 (format "dashboard/%d/public_link" (:id dash)))
          (mt/user-http-request :crowberto :delete 204 (format "card/%d/public_link" (:id card)))
          (is (not (contains? (dirty-ids "dashboard") (:id dash))))
          (is (not (contains? (dirty-ids "card") (:id card)))))))))

(deftest dirty-requires-superuser-test
  (testing "GET /api/ee/remote-sync/dirty requires superuser permissions"
    (is (= "You don't have permissions to do that."
           (mt/user-http-request :rasta :get 403 "ee/remote-sync/dirty")))))

;;; ------------------------------------------------- Settings Endpoint -------------------------------------------------

(deftest settings-update-succeeds-test
  (testing "PUT /api/ee/remote-sync/settings successfully updates settings"
    (let [mock-main (test-helpers/create-mock-source)]
      (mt/with-dynamic-fn-redefs [settings/check-git-settings! (constantly nil)
                                  source/source-from-settings (constantly mock-main)]
        (mt/with-temporary-setting-values [remote-sync-url nil
                                           remote-sync-type :read-write]
          (let [resp (mt/user-http-request :crowberto :put 200 "ee/remote-sync/settings"
                                           {:remote-sync-url "https://github.com/test/repo.git"
                                            :remote-sync-branch "main"})]
            (is (= {:success true} resp))))))))

(deftest settings-update-triggers-import-in-read-only-test
  (testing "PUT /api/ee/remote-sync/settings triggers import when type is read-only"
    (let [mock-main (test-helpers/create-mock-source)]
      (mt/with-dynamic-fn-redefs [settings/check-git-settings! (constantly nil)
                                  source/source-from-settings (constantly mock-main)]
        (mt/with-temporary-setting-values [remote-sync-type :read-only
                                           remote-sync-branch "main"
                                           remote-sync-url "https://github.com/test/repo.git"
                                           remote-sync-token "test-token"]
          (let [response (mt/user-http-request :crowberto :put 200 "ee/remote-sync/settings"
                                               {:remote-sync-url "file://repo.git"
                                                :remote-sync-type :read-only})
                task (wait-for-task-completion (:task_id response))]
            (is (=? {:success true} response))
            (is (remote-sync.task/successful? task))))))))

(deftest settings-rejects-read-write-branch-switch-test
  (testing "GHY-4019: PUT /api/ee/remote-sync/settings guards branch changes"
    (let [mock-main       (test-helpers/create-mock-source)
          reject-message  (str "Switching the remote-sync branch is not allowed here. "
                               "Use the branch switch action, which reconciles synced "
                               "collections and guards against data loss.")]
      (mt/with-dynamic-fn-redefs [settings/check-git-settings!      (constantly nil)
                                  source/source-from-settings       (constantly mock-main)]
        (testing "rejects a branch switch in read-write mode (must use the guarded import)"
          (mt/with-temporary-setting-values [remote-sync-url    "https://github.com/test/repo.git"
                                             remote-sync-token  "test-token"
                                             remote-sync-branch "main"
                                             remote-sync-type   :read-write]
            (is (= reject-message
                   (mt/user-http-request :crowberto :put 400 "ee/remote-sync/settings"
                                         {:remote-sync-branch "develop"})))
            (is (= "main" (settings/remote-sync-branch))
                "the branch setting is left unchanged")))
        (testing "rejects blanking the branch in read-write mode (would otherwise reset the guard)"
          (mt/with-temporary-setting-values [remote-sync-url    "https://github.com/test/repo.git"
                                             remote-sync-token  "test-token"
                                             remote-sync-branch "main"
                                             remote-sync-type   :read-write]
            (is (= reject-message
                   (mt/user-http-request :crowberto :put 400 "ee/remote-sync/settings"
                                         {:remote-sync-branch ""})))
            (is (= "main" (settings/remote-sync-branch))
                "the branch cannot be blanked to bypass the guard, then switched freely")))
        (testing "allows an unchanged branch in read-write mode"
          (mt/with-temporary-setting-values [remote-sync-url    "https://github.com/test/repo.git"
                                             remote-sync-token  "test-token"
                                             remote-sync-branch "main"
                                             remote-sync-type   :read-write]
            (is (=? {:success true}
                    (mt/user-http-request :crowberto :put 200 "ee/remote-sync/settings"
                                          {:remote-sync-branch "main"})))))
        (testing "allows setting the branch during first-time configuration (no current branch)"
          (mt/with-temporary-setting-values [remote-sync-url    "https://github.com/test/repo.git"
                                             remote-sync-token  "test-token"
                                             remote-sync-branch ""
                                             remote-sync-type   :read-write]
            (is (=? {:success true}
                    (mt/user-http-request :crowberto :put 200 "ee/remote-sync/settings"
                                          {:remote-sync-branch "develop"})))
            (is (= "develop" (settings/remote-sync-branch)))))
        (testing "allows a branch change in read-only mode (the change triggers a reconciling import)"
          (mt/with-dynamic-fn-redefs [impl/finish-remote-config! (constantly nil)]
            (mt/with-temporary-setting-values [remote-sync-url    "https://github.com/test/repo.git"
                                               remote-sync-token  "test-token"
                                               remote-sync-branch "main"
                                               remote-sync-type   :read-only]
              (is (=? {:success true}
                      (mt/user-http-request :crowberto :put 200 "ee/remote-sync/settings"
                                            {:remote-sync-branch "develop" :remote-sync-type :read-only})))
              (is (= "develop" (settings/remote-sync-branch))))))))))

(deftest settings-requires-superuser-test
  (testing "PUT /api/ee/remote-sync/settings requires superuser permissions"
    (is (= "You don't have permissions to do that."
           (mt/user-http-request :rasta :put 403 "ee/remote-sync/settings" {})))))

(deftest settings-handles-invalid-settings-test
  (testing "PUT /api/ee/remote-sync/settings handles invalid settings"
    (let [response (mt/user-http-request :crowberto :put 400 "ee/remote-sync/settings"
                                         {:remote-sync-url "asdf://invalid-url"})]
      (is (= "Invalid repository URL: only HTTPS URLs are supported (e.g., https://git-host.example.com/yourcompany/repo.git)" (:error response))))))

(deftest settings-surfaces-clone-failure-message-test
  (testing "PUT /api/ee/remote-sync/settings surfaces a JGit clone-failure as a 400 error message"
    (mt/with-dynamic-fn-redefs [settings/check-git-settings!
                                (fn [_] (throw (ex-info "Failed to clone git repository: URI not supported: file://bad" {})))]
      (mt/with-temporary-setting-values [remote-sync-url nil
                                         remote-sync-type :read-only]
        (let [response (mt/user-http-request :crowberto :put 400 "ee/remote-sync/settings"
                                             {:remote-sync-url "https://github.com/does/not-exist.git"
                                              :remote-sync-type :read-only})]
          (is (re-find #"Failed to clone git repository" (:error response))))))))

(deftest settings-cannot-change-with-dirty-data
  (testing "PUT /api/ee/remote-sync/settings doesn't allow losing dirty data"
    (mt/with-dynamic-fn-redefs [remote-sync.object/dirty? (constantly true)
                                settings/check-and-update-remote-settings! #(throw (Exception. "Should not be called"))]
      (mt/with-temporary-setting-values [remote-sync-url "https://github.com/test/repo.git"
                                         remote-sync-token "test-token"
                                         remote-sync-branch "main"
                                         remote-sync-type :read-write]
        (testing "cannot change to read-only mode"
          (is (= "There are unsaved changes in the Remote Sync collection which will be overwritten switching to read-only mode."
                 (mt/user-http-request :crowberto :put 400 "ee/remote-sync/settings"
                                       {:remote-sync-type :read-only
                                        :remote-sync-branch "main"
                                        :remote-sync-url "https://github.com/test/repo.git"
                                        :remote-sync-token "test-token"}))))))))

(deftest settings-refuses-while-task-running-test
  (testing "PUT /api/ee/remote-sync/settings refuses with 400 when a RemoteSyncTask is active"
    (mt/with-temp [:model/RemoteSyncTask _ {:sync_task_type "import"
                                            :initiated_by   (mt/user->id :rasta)
                                            :started_at     (t/offset-date-time)
                                            :progress       0.0}]
      (let [check-git-call-count (atom 0)]
        (mt/with-dynamic-fn-redefs [settings/check-git-settings! (fn [_] (swap! check-git-call-count inc) true)]
          (mt/with-temporary-setting-values [remote-sync-url    "file://my/repo.git"
                                             remote-sync-token  nil
                                             remote-sync-type   :read-only
                                             remote-sync-branch "main"]
            (let [response (mt/user-http-request :crowberto :put 400 "ee/remote-sync/settings"
                                                 {:remote-sync-url    "file://different.git"
                                                  :remote-sync-type   :read-only
                                                  :remote-sync-branch "feature-x"
                                                  :remote-sync-token  nil})]
              (is (= "Remote sync task in progress" (:message response))
                  "endpoint should return the guard's error message"))
            (is (= "file://my/repo.git" (settings/remote-sync-url))
                "remote-sync-url must remain unchanged when the guard fires")
            (is (= "main" (settings/remote-sync-branch))
                "remote-sync-branch must remain unchanged when the guard fires")
            (is (zero? @check-git-call-count)
                "check-git-settings! must not be called when the guard fires")))))))

;;; ------------------------------------------- Settings Collections Tests -------------------------------------------

(deftest settings-collections-enables-remote-sync-on-single-collection-test
  (testing "PUT /api/ee/remote-sync/settings with collections enables remote sync on a single collection"
    (mt/with-temporary-setting-values [remote-sync-type :read-write]
      (mt/with-temp [:model/Collection {coll-id :id} {:name "Test Collection" :location "/" :is_remote_synced false}]
        (mt/with-dynamic-fn-redefs [settings/check-and-update-remote-settings! (constantly nil)
                                    impl/finish-remote-config! (constantly nil)]
          (let [response (mt/user-http-request :crowberto :put 200 "ee/remote-sync/settings"
                                               {:collections {coll-id true}})]
            (is (= {:success true} response))
            (is (true? (:is_remote_synced (t2/select-one :model/Collection :id coll-id))))))))))

(deftest settings-collections-disables-remote-sync-on-single-collection-test
  (testing "PUT /api/ee/remote-sync/settings with collections disables remote sync on a single collection"
    (mt/with-temporary-setting-values [remote-sync-type :read-write]
      (mt/with-temp [:model/Collection {coll-id :id} {:name "Test Collection" :location "/" :is_remote_synced true}]
        (mt/with-dynamic-fn-redefs [settings/check-and-update-remote-settings! (constantly nil)
                                    impl/finish-remote-config! (constantly nil)]
          (let [response (mt/user-http-request :crowberto :put 200 "ee/remote-sync/settings"
                                               {:collections {coll-id false}})]
            (is (= {:success true} response))
            (is (false? (:is_remote_synced (t2/select-one :model/Collection :id coll-id))))))))))

(deftest settings-collections-enables-multiple-collections-test
  (testing "PUT /api/ee/remote-sync/settings with collections enables remote sync on multiple collections"
    (mt/with-temporary-setting-values [remote-sync-type :read-write]
      (mt/with-temp [:model/Collection {coll1-id :id} {:name "Collection 1" :location "/" :is_remote_synced false}
                     :model/Collection {coll2-id :id} {:name "Collection 2" :location "/" :is_remote_synced false}]
        (mt/with-dynamic-fn-redefs [settings/check-and-update-remote-settings! (constantly nil)
                                    impl/finish-remote-config! (constantly nil)]
          (let [response (mt/user-http-request :crowberto :put 200 "ee/remote-sync/settings"
                                               {:collections {coll1-id true coll2-id true}})]
            (is (= {:success true} response))
            (is (true? (:is_remote_synced (t2/select-one :model/Collection :id coll1-id))))
            (is (true? (:is_remote_synced (t2/select-one :model/Collection :id coll2-id))))))))))

(deftest settings-collections-mixed-enable-disable-test
  (testing "PUT /api/ee/remote-sync/settings with collections handles mixed enable/disable operations"
    (mt/with-temporary-setting-values [remote-sync-type :read-write]
      (mt/with-temp [:model/Collection {coll1-id :id} {:name "Collection 1" :location "/" :is_remote_synced false}
                     :model/Collection {coll2-id :id} {:name "Collection 2" :location "/" :is_remote_synced true}]
        (mt/with-dynamic-fn-redefs [settings/check-and-update-remote-settings! (constantly nil)
                                    impl/finish-remote-config! (constantly nil)]
          (let [response (mt/user-http-request :crowberto :put 200 "ee/remote-sync/settings"
                                               {:collections {coll1-id true coll2-id false}})]
            (is (= {:success true} response))
            (is (true? (:is_remote_synced (t2/select-one :model/Collection :id coll1-id))))
            (is (false? (:is_remote_synced (t2/select-one :model/Collection :id coll2-id))))))))))

(deftest settings-collections-cascades-to-descendants-test
  (testing "PUT /api/ee/remote-sync/settings with collections cascades to descendants"
    (mt/with-temporary-setting-values [remote-sync-type :read-write]
      (mt/with-temp [:model/Collection {parent-id :id} {:name "Parent" :location "/" :is_remote_synced false}
                     :model/Collection {child-id :id} {:name "Child" :location (format "/%d/" parent-id) :is_remote_synced false}
                     :model/Collection {grandchild-id :id} {:name "Grandchild" :location (format "/%d/%d/" parent-id child-id) :is_remote_synced false}]
        (mt/with-dynamic-fn-redefs [settings/check-and-update-remote-settings! (constantly nil)
                                    impl/finish-remote-config! (constantly nil)]
          (let [response (mt/user-http-request :crowberto :put 200 "ee/remote-sync/settings"
                                               {:collections {parent-id true}})]
            (is (= {:success true} response))
            (is (true? (:is_remote_synced (t2/select-one :model/Collection :id parent-id))))
            (is (true? (:is_remote_synced (t2/select-one :model/Collection :id child-id))))
            (is (true? (:is_remote_synced (t2/select-one :model/Collection :id grandchild-id))))))))))

(deftest settings-collections-cascades-disable-to-descendants-test
  (testing "PUT /api/ee/remote-sync/settings with collections cascades disable to descendants"
    (mt/with-temporary-setting-values [remote-sync-type :read-write]
      (mt/with-temp [:model/Collection {parent-id :id} {:name "Parent" :location "/" :is_remote_synced true}
                     :model/Collection {child-id :id} {:name "Child" :location (format "/%d/" parent-id) :is_remote_synced true}
                     :model/Collection {grandchild-id :id} {:name "Grandchild" :location (format "/%d/%d/" parent-id child-id) :is_remote_synced true}]
        (mt/with-dynamic-fn-redefs [settings/check-and-update-remote-settings! (constantly nil)
                                    impl/finish-remote-config! (constantly nil)]
          (let [response (mt/user-http-request :crowberto :put 200 "ee/remote-sync/settings"
                                               {:collections {parent-id false}})]
            (is (= {:success true} response))
            (is (false? (:is_remote_synced (t2/select-one :model/Collection :id parent-id))))
            (is (false? (:is_remote_synced (t2/select-one :model/Collection :id child-id))))
            (is (false? (:is_remote_synced (t2/select-one :model/Collection :id grandchild-id))))))))))

(deftest settings-collections-errors-on-non-remote-synced-dependencies-test
  (testing "PUT /api/ee/remote-sync/settings with collections errors when enabling a collection with non-remote-synced dependencies"
    (mt/with-temporary-setting-values [remote-sync-type :read-write]
      (mt/with-temp [:model/Collection {remote-synced-coll-id :id} {:name "Remote Synced" :location "/" :is_remote_synced false}
                     :model/Collection {regular-coll-id :id} {:name "Regular" :location "/" :is_remote_synced false}
                     :model/Card {source-card-id :id} {:name "Source Card"
                                                       :collection_id regular-coll-id
                                                       :database_id (mt/id)
                                                       :dataset_query (mt/mbql-query venues)}
                     :model/Card _ {:name "Dependent Card"
                                    :collection_id remote-synced-coll-id
                                    :database_id (mt/id)
                                    :dataset_query (mt/mbql-query nil {:source-table (str "card__" source-card-id)})}]
        (mt/with-dynamic-fn-redefs [settings/check-and-update-remote-settings! (constantly nil)
                                    impl/finish-remote-config! (constantly nil)]
          (let [response (mt/user-http-request :crowberto :put 400 "ee/remote-sync/settings"
                                               {:collections {remote-synced-coll-id true}})]
            (is (= "Uses content that is not remote synced." (:error response)))
            (testing "the structured failure survives the endpoint's rewrap, keyed by error_code"
              (is (=? {:error_code "unsynced-dependencies"
                       :errors     {:required [{:remedy       {:type       "collection"
                                                               :collection {:id       regular-coll-id
                                                                            :name     "Regular"
                                                                            :personal false}}
                                                :syncable     true
                                                :blocks       [{:id remote-synced-coll-id :name "Remote Synced"}]
                                                :dependencies [{:model      "card"
                                                                :id         source-card-id
                                                                :name       "Source Card"
                                                                :collection {:id regular-coll-id :name "Regular"}}]}]}}
                      response)))
            (testing "the body is the ex-data itself, not a stacktrace dump"
              (is (nil? (:trace response)))
              (is (nil? (:via response))))))))))

(deftest settings-collections-errors-on-remote-synced-dependents-test
  (testing "PUT /api/ee/remote-sync/settings with collections errors when disabling a collection that has remote-synced dependents"
    (mt/with-temporary-setting-values [remote-sync-type :read-write]
      (mt/with-temp [:model/Collection {coll1-id :id} {:name "Collection 1" :location "/" :is_remote_synced true}
                     :model/Collection {coll2-id :id} {:name "Collection 2" :location "/" :is_remote_synced true}
                     :model/Card {source-card-id :id} {:name "Source Card"
                                                       :collection_id coll1-id
                                                       :database_id (mt/id)
                                                       :dataset_query (mt/mbql-query venues)}
                     :model/Card _ {:name "Dependent Card"
                                    :collection_id coll2-id
                                    :database_id (mt/id)
                                    :dataset_query (mt/mbql-query nil {:source-table (str "card__" source-card-id)})}]
        (mt/with-dynamic-fn-redefs [settings/check-and-update-remote-settings! (constantly nil)
                                    impl/finish-remote-config! (constantly nil)]
          (let [response (mt/user-http-request :crowberto :put 400 "ee/remote-sync/settings"
                                               {:collections {coll1-id false}})]
            (is (= "Used by remote synced content." (:error response)))
            (testing "the structured failure survives the endpoint's rewrap, keyed by error_code"
              (is (=? {:error_code "remote-synced-dependents"
                       :errors     {:collections [{:collection {:id coll1-id :name "Collection 1"}
                                                   :dependents [{:model "card"
                                                                 :name  "Dependent Card"}]}]}}
                      response)))
            (testing "the body is the ex-data itself, not a stacktrace dump"
              (is (nil? (:trace response)))
              (is (nil? (:via response))))))))))

(deftest settings-collections-empty-is-no-op-test
  (testing "PUT /api/ee/remote-sync/settings with empty collections is a no-op"
    (mt/with-temp [:model/Collection {coll-id :id} {:name "Test Collection" :location "/" :is_remote_synced false}]
      (mt/with-dynamic-fn-redefs [settings/check-and-update-remote-settings! (constantly nil)
                                  impl/finish-remote-config! (constantly nil)]
        (let [response (mt/user-http-request :crowberto :put 200 "ee/remote-sync/settings"
                                             {:collections {}})]
          (is (= {:success true} response))
          (is (false? (:is_remote_synced (t2/select-one :model/Collection :id coll-id)))))))))

(deftest settings-collections-requires-superuser-test
  (testing "PUT /api/ee/remote-sync/settings with collections requires superuser permissions"
    (mt/with-temp [:model/Collection {coll-id :id} {:name "Test Collection" :location "/" :is_remote_synced false}]
      (is (= "You don't have permissions to do that."
             (mt/user-http-request :rasta :put 403 "ee/remote-sync/settings"
                                   {:collections {coll-id true}}))))))

(deftest settings-collections-rejects-changes-in-read-only-mode-test
  (testing "PUT /api/ee/remote-sync/settings rejects collection changes when remote-sync-type is read-only"
    (mt/with-temp [:model/Collection {coll-id :id} {:name "Test Collection" :location "/" :is_remote_synced false}
                   :model/Collection {synced-coll-id :id} {:name "Synced Collection" :location "/" :is_remote_synced true}]
      (mt/with-dynamic-fn-redefs [settings/check-and-update-remote-settings! (constantly nil)
                                  impl/finish-remote-config! (constantly nil)]
        (testing "rejects enabling collections when remote-sync-type is explicitly read-only"
          (is (= "Cannot change synced collections when remote-sync-type is read-only."
                 (mt/user-http-request :crowberto :put 400 "ee/remote-sync/settings"
                                       {:remote-sync-type :read-only
                                        :collections {coll-id true}}))))
        (testing "rejects disabling collections when remote-sync-type is explicitly read-only"
          (is (= "Cannot change synced collections when remote-sync-type is read-only."
                 (mt/user-http-request :crowberto :put 400 "ee/remote-sync/settings"
                                       {:remote-sync-type :read-only
                                        :collections {synced-coll-id false}}))))
        (testing "rejects collection changes when remote-sync-type is already read-only (default)"
          (mt/with-temporary-setting-values [remote-sync-url "https://github.com/test/repo.git" remote-sync-type :read-only]
            (is (= "Cannot change synced collections when remote-sync-type is read-only."
                   (mt/user-http-request :crowberto :put 400 "ee/remote-sync/settings"
                                         {:collections {coll-id true}})))))))))

(deftest settings-collections-no-op-skips-read-only-check-test
  (testing "PUT /api/ee/remote-sync/settings skips read-only error when collections have no actual changes"
    (mt/with-temp [:model/Collection {unsynced-id :id} {:name "Unsynced Collection" :location "/" :is_remote_synced false}
                   :model/Collection {synced-id :id} {:name "Synced Collection" :location "/" :is_remote_synced true}]
      (mt/with-dynamic-fn-redefs [settings/check-and-update-remote-settings! (constantly nil)
                                  impl/finish-remote-config! (constantly nil)]
        (testing "sending false for already-unsynced collection in read-only mode succeeds"
          (is (= {:success true}
                 (mt/user-http-request :crowberto :put 200 "ee/remote-sync/settings"
                                       {:remote-sync-type :read-only
                                        :collections {unsynced-id false}}))))
        (testing "sending true for already-synced collection in read-only mode succeeds"
          (is (= {:success true}
                 (mt/user-http-request :crowberto :put 200 "ee/remote-sync/settings"
                                       {:remote-sync-type :read-only
                                        :collections {synced-id true}}))))
        (testing "mix of no-op and real change still rejects in read-only mode"
          (is (= "Cannot change synced collections when remote-sync-type is read-only."
                 (mt/user-http-request :crowberto :put 400 "ee/remote-sync/settings"
                                       {:remote-sync-type :read-only
                                        :collections {synced-id true unsynced-id true}}))))))))

(deftest settings-collections-no-op-skips-bulk-set-test
  (testing "PUT /api/ee/remote-sync/settings skips bulk-set-remote-sync when collections have no actual changes"
    (mt/with-temporary-setting-values [remote-sync-type :read-write]
      (mt/with-temp [:model/Collection {synced-id :id} {:name "Synced Collection" :location "/" :is_remote_synced true}
                     :model/Collection {unsynced-id :id} {:name "Unsynced Collection" :location "/" :is_remote_synced false}]
        (let [bulk-set-called? (atom false)]
          (mt/with-dynamic-fn-redefs [settings/check-and-update-remote-settings! (constantly nil)
                                      impl/finish-remote-config! (constantly nil)
                                      remote-sync.core/bulk-set-remote-sync (fn [& _] (reset! bulk-set-called? true))]
            (testing "no-op collections do not call bulk-set-remote-sync"
              (let [response (mt/user-http-request :crowberto :put 200 "ee/remote-sync/settings"
                                                   {:collections {synced-id true unsynced-id false}})]
                (is (= {:success true} response))
                (is (false? @bulk-set-called?))))))))))

(deftest settings-collections-allows-changes-in-read-write-mode-test
  (testing "PUT /api/ee/remote-sync/settings allows collection changes when remote-sync-type is read-write"
    (mt/with-temp [:model/Collection {coll-id :id} {:name "Test Collection" :location "/" :is_remote_synced false}]
      (mt/with-dynamic-fn-redefs [settings/check-and-update-remote-settings! (constantly nil)
                                  impl/finish-remote-config! (constantly nil)]
        (testing "allows enabling collections when remote-sync-type is explicitly read-write"
          (let [response (mt/user-http-request :crowberto :put 200 "ee/remote-sync/settings"
                                               {:remote-sync-type :read-write
                                                :collections {coll-id true}})]
            (is (= {:success true} response))
            (is (true? (:is_remote_synced (t2/select-one :model/Collection :id coll-id))))))
        (testing "allows disabling collections when remote-sync-type is explicitly read-write"
          (let [response (mt/user-http-request :crowberto :put 200 "ee/remote-sync/settings"
                                               {:remote-sync-type :read-write
                                                :collections {coll-id false}})]
            (is (= {:success true} response))
            (is (false? (:is_remote_synced (t2/select-one :model/Collection :id coll-id))))))
        (testing "allows collection changes when remote-sync-type is already read-write"
          (mt/with-temporary-setting-values [remote-sync-type :read-write]
            (let [response (mt/user-http-request :crowberto :put 200 "ee/remote-sync/settings"
                                                 {:collections {coll-id true}})]
              (is (= {:success true} response))
              (is (true? (:is_remote_synced (t2/select-one :model/Collection :id coll-id)))))))))))

(deftest create-branch
  (testing "POST /api/ee/remote-sync/create-branch creates a new branch"
    (let [mock-source (test-helpers/create-mock-source)]
      (mt/with-temporary-setting-values [remote-sync-url "https://github.com/test/repo.git"
                                         remote-sync-token "test-token"
                                         remote-sync-branch "main"
                                         remote-sync-type :read-write]
        (mt/with-dynamic-fn-redefs [source/source-from-settings (constantly mock-source)]
          (is (= {:status  "success"
                  :message "Branch 'feature-branch' created from 'main'"}
                 (mt/user-http-request :crowberto :post 200 "ee/remote-sync/create-branch"
                                       {:name "feature-branch"})))
          (is (= #{["main" "main-ref"] ["develop" "develop-ref"] ["feature-branch" "feature-branch-ref"]}
                 (set (source.p/branches mock-source))))
          (is (= "feature-branch" (settings/remote-sync-branch))))))))

(deftest stash
  (testing "POST /api/ee/remote-sync/stash"
    (let [mock-source  (test-helpers/create-mock-source)
          export-calls (atom 0)]
      (mt/with-temporary-setting-values [remote-sync-url "https://github.com/test/repo.git"
                                         remote-sync-token "test-token"
                                         remote-sync-branch "main"
                                         remote-sync-type :read-write]
        (mt/with-temp [:model/RemoteSyncObject remote-sync {:model_type          "Card"
                                                            :model_id            1
                                                            :model_name          "Test Card"
                                                            :model_collection_id 1
                                                            :status              "updated"
                                                            :status_changed_at   (java.time.OffsetDateTime/now)}]
          (mt/with-dynamic-fn-redefs [source/source-from-settings (constantly mock-source)
                                      impl/async-export!          (fn [_ _ _ & _opts] (assoc remote-sync :calls (swap! export-calls inc)))]
            (is (=? {:status  "success"
                     :message "Stashing to feature-branch"}
                    (mt/user-http-request :crowberto :post 200 "ee/remote-sync/stash"
                                          {:new_branch "feature-branch"
                                           :message    "Stash message"})))
            (is (= #{["main" "main-ref"] ["develop" "develop-ref"] ["feature-branch" "feature-branch-ref"]}
                   (set (source.p/branches mock-source))))
            (is (= 1 @export-calls))))))))

;;; ------------------------------------------------- Has Remote Changes Endpoint -------------------------------------------------

(deftest has-remote-changes-requires-superuser-test
  (testing "GET /api/ee/remote-sync/has-remote-changes requires superuser permissions"
    (mt/with-temporary-setting-values [remote-sync-url "https://github.com/test/repo.git"]
      (is (= "You don't have permissions to do that."
             (mt/user-http-request :rasta :get 403 "ee/remote-sync/has-remote-changes"))))))

(deftest has-remote-changes-errors-when-not-configured-test
  (testing "GET /api/ee/remote-sync/has-remote-changes errors when remote sync is not configured"
    (mt/with-temporary-setting-values [remote-sync-url nil]
      (is (= "Remote sync is not configured."
             (mt/user-http-request :crowberto :get 400 "ee/remote-sync/has-remote-changes"))))))

(deftest has-remote-changes-returns-true-when-never-imported-test
  (testing "GET /api/ee/remote-sync/has-remote-changes returns true when never imported"
    (let [mock-source (test-helpers/create-mock-source)]
      (mt/with-temporary-setting-values [remote-sync-url "https://github.com/test/repo.git"
                                         remote-sync-token "test-token"
                                         remote-sync-branch "main"]
        (mt/with-dynamic-fn-redefs [source/source-from-settings (constantly mock-source)]
          ;; Clear cache before test
          (impl/invalidate-remote-changes-cache!)
          (let [response (mt/user-http-request :crowberto :get 200 "ee/remote-sync/has-remote-changes")]
            (is (true? (:has_changes response)))
            (is (= "mock-version" (:remote_version response)))
            (is (nil? (:local_version response)))
            (is (= false (:cached response)))))))))

(deftest has-remote-changes-returns-true-when-versions-differ-test
  (testing "GET /api/ee/remote-sync/has-remote-changes returns true when remote version differs from local"
    (let [mock-source (test-helpers/create-mock-source)]
      (mt/with-temporary-setting-values [remote-sync-url "https://github.com/test/repo.git"
                                         remote-sync-token "test-token"
                                         remote-sync-branch "main"]
        (mt/with-temp [:model/RemoteSyncTask _ {:sync_task_type "import"
                                                :ended_at :%now
                                                :version "old-version"}]
          (mt/with-dynamic-fn-redefs [source/source-from-settings (constantly mock-source)]
            ;; Clear cache before test
            (impl/invalidate-remote-changes-cache!)
            (let [response (mt/user-http-request :crowberto :get 200 "ee/remote-sync/has-remote-changes")]
              (is (true? (:has_changes response)))
              (is (= "mock-version" (:remote_version response)))
              (is (= "old-version" (:local_version response)))
              (is (= false (:cached response))))))))))

(deftest has-remote-changes-returns-false-when-versions-match-test
  (testing "GET /api/ee/remote-sync/has-remote-changes returns false when versions match"
    (let [mock-source (test-helpers/create-mock-source)]
      (mt/with-temporary-setting-values [remote-sync-url "https://github.com/test/repo.git"
                                         remote-sync-token "test-token"
                                         remote-sync-branch "main"]
        (mt/with-temp [:model/RemoteSyncTask _ {:sync_task_type "import"
                                                :ended_at :%now
                                                :version "mock-version"}]
          (mt/with-dynamic-fn-redefs [source/source-from-settings (constantly mock-source)]
            ;; Clear cache before test
            (impl/invalidate-remote-changes-cache!)
            (let [response (mt/user-http-request :crowberto :get 200 "ee/remote-sync/has-remote-changes")]
              (is (= false (:has_changes response)))
              (is (= "mock-version" (:remote_version response)))
              (is (= "mock-version" (:local_version response)))
              (is (= false (:cached response))))))))))

(deftest has-remote-changes-uses-cache-test
  (testing "GET /api/ee/remote-sync/has-remote-changes uses cache on subsequent calls"
    (let [mock-source (test-helpers/create-mock-source)
          call-count (atom 0)]
      (mt/with-temporary-setting-values [remote-sync-url "https://github.com/test/repo.git"
                                         remote-sync-token "test-token"
                                         remote-sync-branch "main"
                                         remote-sync-check-changes-cache-ttl-seconds 60]
        (mt/with-temp [:model/RemoteSyncTask _ {:sync_task_type "import"
                                                :ended_at :%now
                                                :version "mock-version"}]
          (mt/with-dynamic-fn-redefs [source/source-from-settings (fn [& _args]
                                                                    (swap! call-count inc)
                                                                    mock-source)]
            ;; Clear cache before test
            (impl/invalidate-remote-changes-cache!)
            ;; First call - should hit the source
            (let [response1 (mt/user-http-request :crowberto :get 200 "ee/remote-sync/has-remote-changes")]
              (is (= false (:cached response1)))
              (is (= 1 @call-count)))
            ;; Second call - should use cache
            (let [response2 (mt/user-http-request :crowberto :get 200 "ee/remote-sync/has-remote-changes")]
              (is (true? (:cached response2)))
              (is (= 1 @call-count)))))))))

(deftest has-remote-changes-force-refresh-bypasses-cache-test
  (testing "GET /api/ee/remote-sync/has-remote-changes?force-refresh=true bypasses cache"
    (let [mock-source (test-helpers/create-mock-source)
          call-count (atom 0)]
      (mt/with-temporary-setting-values [remote-sync-url "https://github.com/test/repo.git"
                                         remote-sync-token "test-token"
                                         remote-sync-branch "main"
                                         remote-sync-check-changes-cache-ttl-seconds 60]
        (mt/with-temp [:model/RemoteSyncTask _ {:sync_task_type "import"
                                                :ended_at :%now
                                                :version "mock-version"}]
          (mt/with-dynamic-fn-redefs [source/source-from-settings (fn [& _args]
                                                                    (swap! call-count inc)
                                                                    mock-source)]
            ;; Clear cache before test
            (impl/invalidate-remote-changes-cache!)
            ;; First call - should hit the source
            (mt/user-http-request :crowberto :get 200 "ee/remote-sync/has-remote-changes")
            (is (= 1 @call-count))
            ;; Second call with force-refresh=true - should bypass cache
            (let [response (mt/user-http-request :crowberto :get 200 "ee/remote-sync/has-remote-changes"
                                                 :force-refresh true)]
              (is (= false (:cached response)))
              (is (= 2 @call-count)))))))))

(deftest has-remote-changes-cache-invalidated-on-branch-change-test
  (testing "GET /api/ee/remote-sync/has-remote-changes invalidates cache when branch changes"
    (let [mock-source (test-helpers/create-mock-source)
          call-count (atom 0)]
      (mt/with-temporary-setting-values [remote-sync-url "https://github.com/test/repo.git"
                                         remote-sync-token "test-token"
                                         remote-sync-branch "main"
                                         remote-sync-check-changes-cache-ttl-seconds 60]
        (mt/with-temp [:model/RemoteSyncTask _ {:sync_task_type "import"
                                                :ended_at :%now
                                                :version "mock-version"}]
          (mt/with-dynamic-fn-redefs [source/source-from-settings (fn [& _args]
                                                                    (swap! call-count inc)
                                                                    mock-source)]
            ;; Clear cache before test
            (impl/invalidate-remote-changes-cache!)
            ;; First call with main branch
            (mt/user-http-request :crowberto :get 200 "ee/remote-sync/has-remote-changes")
            (is (= 1 @call-count))
            ;; Change branch setting
            (mt/with-temporary-setting-values [remote-sync-branch "develop"]
              ;; Should hit source again due to branch change
              (let [response (mt/user-http-request :crowberto :get 200 "ee/remote-sync/has-remote-changes")]
                (is (= false (:cached response)))
                (is (= 2 @call-count))))))))))

(defn- missing-branch-source
  "Source whose snapshot throws the typed :missing-branch ex-info — simulating a
   configured branch that has been deleted upstream."
  [branch]
  (reify source.p/Source
    (branches [_] ["main"])
    (create-branch [_ _ _] nil)
    (default-branch [_] "main")
    (snapshot [_]
      (throw (ex-info (str "Invalid branch: " branch)
                      {:error-type :missing-branch
                       :branch branch})))
    (snapshot-at [_ _version]
      (throw (ex-info (str "Invalid branch: " branch)
                      {:error-type :missing-branch
                       :branch branch})))))

(deftest has-remote-changes-returns-branch-missing-gracefully-test
  (testing "GET /has-remote-changes returns branch_missing=true instead of 500 when the branch has been deleted upstream"
    (mt/with-temporary-setting-values [remote-sync-url "https://github.com/test/repo.git"
                                       remote-sync-token "test-token"
                                       remote-sync-branch "gone"
                                       remote-sync-check-changes-cache-ttl-seconds 60]
      (mt/with-temp [:model/RemoteSyncTask _ {:sync_task_type "import"
                                              :ended_at :%now
                                              :version "some-prior-version"}]
        (mt/with-dynamic-fn-redefs [source/source-from-settings (fn [& _] (missing-branch-source "gone"))]
          (impl/invalidate-remote-changes-cache!)
          (let [response (mt/user-http-request :crowberto :get 200 "ee/remote-sync/has-remote-changes")]
            (is (false? (:has_changes response)))
            (is (true? (:branch_missing response)))
            (is (nil? (:remote_version response)))
            (is (= "some-prior-version" (:local_version response)))
            (is (false? (:cached response)))))))))

(deftest has-remote-changes-does-not-cache-branch-missing-test
  (testing "Branch-missing results are not cached so a subsequent call re-checks"
    (let [snapshot-calls (atom 0)
          ;; First call returns missing-branch; second call succeeds.
          source-fn (fn [& _]
                      (swap! snapshot-calls inc)
                      (if (= 1 @snapshot-calls)
                        (missing-branch-source "gone")
                        (test-helpers/create-mock-source)))]
      (mt/with-temporary-setting-values [remote-sync-url "https://github.com/test/repo.git"
                                         remote-sync-token "test-token"
                                         remote-sync-branch "gone"
                                         remote-sync-check-changes-cache-ttl-seconds 60]
        (mt/with-temp [:model/RemoteSyncTask _ {:sync_task_type "import"
                                                :ended_at :%now
                                                :version "mock-version"}]
          (mt/with-dynamic-fn-redefs [source/source-from-settings source-fn]
            (impl/invalidate-remote-changes-cache!)
            (let [first-response (mt/user-http-request :crowberto :get 200 "ee/remote-sync/has-remote-changes")]
              (is (true? (:branch_missing first-response)))
              (is (false? (:cached first-response))))
            ;; Second call re-hits the source; cache was not populated by the first call.
            (let [second-response (mt/user-http-request :crowberto :get 200 "ee/remote-sync/has-remote-changes")]
              (is (not (:branch_missing second-response)))
              (is (false? (:cached second-response)))
              (is (= 2 @snapshot-calls)))))))))

(deftest has-remote-changes-still-propagates-other-errors-test
  (testing "Non-missing-branch failures still propagate (the graceful handler is :missing-branch-only)"
    (let [failing-source (reify source.p/Source
                           (branches [_] ["main"])
                           (create-branch [_ _ _] nil)
                           (default-branch [_] "main")
                           (snapshot [_] (throw (RuntimeException. "boom")))
                           (snapshot-at [_ _version] (throw (RuntimeException. "boom"))))]
      (mt/with-temporary-setting-values [remote-sync-url "https://github.com/test/repo.git"
                                         remote-sync-token "test-token"
                                         remote-sync-branch "main"
                                         remote-sync-check-changes-cache-ttl-seconds 60]
        (mt/with-dynamic-fn-redefs [source/source-from-settings (fn [& _] failing-source)]
          (impl/invalidate-remote-changes-cache!)
          (is (thrown-with-msg? RuntimeException #"boom"
                                (impl/has-remote-changes?))))))))

;;; ------------------------------------------- Token Preservation Tests -------------------------------------------

(deftest settings-preserves-token-when-switching-to-read-only-test
  (testing "PUT /api/ee/remote-sync/settings preserves token when switching from read-write to read-only"
    (let [mock-source (test-helpers/create-mock-source)]
      (mt/with-dynamic-fn-redefs [settings/check-git-settings! (constantly nil)
                                  source/source-from-settings (constantly mock-source)]
        (mt/with-temporary-setting-values [remote-sync-url "https://github.com/test/repo.git"
                                           remote-sync-token "secret-token-value"
                                           remote-sync-branch "main"
                                           remote-sync-type :read-write]
          (let [{:keys [task_id] :as resp} (mt/user-http-request :crowberto :put 200 "ee/remote-sync/settings"
                                                                 {:remote-sync-type :read-only})]
            (wait-for-task-completion task_id)
            (is (= {:success true :task_id task_id} resp))
            (is (= :read-only (settings/remote-sync-type)))
            (is (= "secret-token-value" (settings/remote-sync-token))
                "Token should be preserved when not included in request")))))))

(deftest settings-preserves-token-when-changing-branch-test
  ;; Read-only mode: a branch change here is a declarative setting whose change triggers a reconciling import
  ;; (in read-write, a branch switch must go through the guarded POST /import instead — GHY-4019).
  (testing "PUT /api/ee/remote-sync/settings preserves token when changing branch"
    (let [mock-source (test-helpers/create-mock-source)]
      (mt/with-dynamic-fn-redefs [settings/check-git-settings! (constantly nil)
                                  source/source-from-settings (constantly mock-source)]
        (mt/with-temporary-setting-values [remote-sync-url "https://github.com/test/repo.git"
                                           remote-sync-token "secret-token-value"
                                           remote-sync-branch "main"
                                           remote-sync-type :read-only]
          (let [{:as resp :keys [task_id]} (mt/user-http-request :crowberto :put 200 "ee/remote-sync/settings"
                                                                 {:remote-sync-branch "develop"})]
            (wait-for-task-completion task_id)
            (is (=? {:success true} resp))
            (is (= "develop" (settings/remote-sync-branch)))
            (is (= "secret-token-value" (settings/remote-sync-token))
                "Token should be preserved when not included in request")))))))

(deftest settings-clears-token-when-explicitly-nil-test
  (testing "PUT /api/ee/remote-sync/settings clears token when explicitly set to nil"
    (let [mock-source (test-helpers/create-mock-source)]
      (mt/with-dynamic-fn-redefs [settings/check-git-settings! (constantly nil)
                                  source/source-from-settings (constantly mock-source)]
        (mt/with-temporary-setting-values [remote-sync-url "https://github.com/test/repo.git"
                                           remote-sync-token "secret-token-value"
                                           remote-sync-branch "main"
                                           remote-sync-type :read-write]
          (let [{:as resp :keys [task_id]} (mt/user-http-request :crowberto :put 200 "ee/remote-sync/settings"
                                                                 {:remote-sync-token nil})]
            (wait-for-task-completion task_id)
            (is (=? {:success true} resp))
            (is (nil? (settings/remote-sync-token))
                "Token should be cleared when explicitly set to nil")))))))

;;; ------------------------------------------- Transforms Setting Tests -------------------------------------------

(deftest settings-updates-transforms-setting-test
  (testing "PUT /api/ee/remote-sync/settings can update remote-sync-transforms setting"
    (let [mock-source (test-helpers/create-mock-source)]
      (mt/with-dynamic-fn-redefs [settings/check-git-settings! (constantly nil)
                                  source/source-from-settings (constantly mock-source)]
        (mt/with-temporary-setting-values [remote-sync-url "https://github.com/test/repo.git"
                                           remote-sync-token "test-token"
                                           remote-sync-branch "main"
                                           remote-sync-type :read-write
                                           remote-sync-transforms false]
          (let [built-in-count (t2/count :model/TransformTag :built_in_type [:not= nil])]
            (testing "can enable transforms sync"
              (let [{:as resp :keys [task_id]} (mt/user-http-request :crowberto :put 200 "ee/remote-sync/settings"
                                                                     {:remote-sync-transforms true})]
                (wait-for-task-completion task_id)
                (is (=? {:success true} resp))
                (is (true? (settings/remote-sync-transforms)))
                (is (= built-in-count (t2/count :model/TransformTag :built_in_type [:not= nil]))
                    "Built-in transform tags should not be deleted by sync")))
            (testing "can disable transforms sync"
              (let [{:as resp :keys [task_id]} (mt/user-http-request :crowberto :put 200 "ee/remote-sync/settings"
                                                                     {:remote-sync-transforms false})]
                (wait-for-task-completion task_id)
                (is (=? {:success true} resp))
                (is (false? (settings/remote-sync-transforms)))
                (is (= built-in-count (t2/count :model/TransformTag :built_in_type [:not= nil]))
                    "Built-in transform tags should not be deleted by sync")))))))))

(deftest settings-preserves-transforms-when-not-specified-test
  (testing "PUT /api/ee/remote-sync/settings preserves transforms setting when not specified"
    (let [mock-source (test-helpers/create-mock-source)]
      (mt/with-dynamic-fn-redefs [settings/check-git-settings! (constantly nil)
                                  source/source-from-settings (constantly mock-source)]
        (mt/with-temporary-setting-values [remote-sync-url "https://github.com/test/repo.git"
                                           remote-sync-token "test-token"
                                           remote-sync-branch "main"
                                           remote-sync-type :read-write
                                           remote-sync-transforms true]
          ;; Toggle an unrelated setting (auto-import) so no branch switch is attempted and no import runs
          ;; that could itself re-toggle remote-sync-transforms; the point is transforms is left untouched.
          (let [resp (mt/user-http-request :crowberto :put 200 "ee/remote-sync/settings"
                                           {:remote-sync-auto-import true})]
            (is (=? {:success true} resp))
            (is (true? (settings/remote-sync-transforms))
                "Transforms setting should be preserved when not included in request")))))))

(deftest dirty-returns-removed-transform-when-setting-disabled-test
  (testing "GET /api/ee/remote-sync/dirty lists a transform pending removal while transforms sync is disabled"
    (test-helpers/with-clean-object
      (mt/with-temporary-setting-values [remote-sync-transforms false]
        (mt/with-temp [:model/Transform {transform-id :id} {:name "Removed Transform"}
                       :model/RemoteSyncObject _ {:model_type        "Transform"
                                                  :model_id          transform-id
                                                  :model_name        "Removed Transform"
                                                  :status            "removed"
                                                  :status_changed_at (t/offset-date-time)}]
          (is (=? [{:id transform-id :model "transform" :name "Removed Transform" :sync_status "removed"}]
                  (:dirty (mt/user-http-request :crowberto :get 200 "ee/remote-sync/dirty")))))))))

(deftest settings-collections-not-marked-synced-when-settings-validation-fails-test
  (testing "PUT /api/ee/remote-sync/settings does not mark collections as synced when settings validation fails"
    (mt/with-temporary-setting-values [remote-sync-type :read-write]
      (mt/with-temp [:model/Collection {coll-id :id} {:name "Test Collection" :location "/" :is_remote_synced false}]
        (mt/with-dynamic-fn-redefs [settings/check-and-update-remote-settings!
                                    (fn [_] (throw (ex-info "Authentication is required" {:status-code 400})))
                                    impl/finish-remote-config! (constantly nil)]
          (mt/user-http-request :crowberto :put 400 "ee/remote-sync/settings"
                                {:remote-sync-url   "https://github.com/test/private-repo.git"
                                 :remote-sync-type  :read-write
                                 :collections       {coll-id true}})
          (is (false? (:is_remote_synced (t2/select-one :model/Collection :id coll-id)))))))))

(defn- do-with-setup-redefs! [thunk]
  (mt/with-dynamic-fn-redefs [settings/check-git-settings! (constantly nil)
                              source/source-from-settings (constantly (test-helpers/create-mock-source))]
    (mt/with-temporary-setting-values [remote-sync-url    nil
                                       remote-sync-branch nil
                                       remote-sync-type   :read-only]
      (collections.tu/with-library-not-synced
        (collections.tu/with-library [{:keys [library data metrics]}]
          (thunk (mapv :id [library data metrics])))))))

(defn- synced-states [collection-ids]
  (t2/select-fn-vec :is_remote_synced :model/Collection :id [:in collection-ids] {:order-by [:id]}))

(deftest settings-syncs-existing-library-test
  (testing "ENG-14338: setting up read-write sync marks an existing Library synced, and existing transforms are tracked"
    (mt/with-temporary-setting-values [remote-sync-custom-collections-enabled false]
      (mt/with-temp [:model/Transform {transform-id :id} {:name "Existing Transform"}]
        (do-with-setup-redefs!
         (fn [library-ids]
           (mt/user-http-request :crowberto :put 200 "ee/remote-sync/settings"
                                 {:remote-sync-url        "https://github.com/test/repo.git"
                                  :remote-sync-branch     "main"
                                  :remote-sync-type       :read-write
                                  :remote-sync-transforms true})
           (is (= [true true true] (synced-states library-ids)))
           (is (contains? (dirty-ids "transform") transform-id))))))))

(deftest settings-syncs-library-when-asked-not-to-test
  (testing "Without custom collections the Library is synced even when the request leaves it unsynced"
    (mt/with-temporary-setting-values [remote-sync-custom-collections-enabled false]
      (do-with-setup-redefs!
       (fn [[library-id :as library-ids]]
         (mt/user-http-request :crowberto :put 200 "ee/remote-sync/settings"
                               {:remote-sync-url    "https://github.com/test/repo.git"
                                :remote-sync-branch "main"
                                :remote-sync-type   :read-write
                                :collections        {library-id false}})
         (is (= [true true true] (synced-states library-ids))))))))

(deftest settings-leaves-library-with-custom-collections-test
  (testing "With custom collections the Library stays unsynced unless the request syncs it"
    (mt/with-temporary-setting-values [remote-sync-custom-collections-enabled true]
      (do-with-setup-redefs!
       (fn [library-ids]
         (mt/user-http-request :crowberto :put 200 "ee/remote-sync/settings"
                               {:remote-sync-url    "https://github.com/test/repo.git"
                                :remote-sync-branch "main"
                                :remote-sync-type   :read-write})
         (is (= [false false false] (synced-states library-ids))))))))

(deftest settings-disable-leaves-library-untracked-test
  (testing "Disabling read-write sync doesn't mark the Library synced or track it on the way out"
    (mt/with-temporary-setting-values [remote-sync-custom-collections-enabled false
                                       remote-sync-url                        "https://github.com/test/repo.git"
                                       remote-sync-branch                     "main"
                                       remote-sync-type                       :read-write]
      (collections.tu/with-library-not-synced
        (collections.tu/with-library [{:keys [library]}]
          (mt/user-http-request :crowberto :put 200 "ee/remote-sync/settings" {:remote-sync-url ""})
          (is (false? (t2/select-one-fn :is_remote_synced :model/Collection :id (:id library))))
          (is (not (t2/exists? :model/RemoteSyncObject :model_type "Collection" :model_id (:id library)))))))))

;; ---------- API-level guard sweep --------------------------------------------------------------
;;
;; Boundary tests verifying that every mutating remote-sync HTTP endpoint surfaces the guard's
;; refusal as a 400 response with `Remote sync task in progress`. Companions to the operation-level
;; tests; this catches the case where an endpoint is edited to bypass the guarded function.

(deftest import-refuses-while-task-running-test
  (testing "POST /api/ee/remote-sync/import returns 400 when a RemoteSyncTask is active"
    (mt/with-temp [:model/RemoteSyncTask _ {:sync_task_type "import"
                                            :initiated_by   (mt/user->id :rasta)
                                            :started_at     (t/offset-date-time)
                                            :progress       0.0}]
      (let [mock-source     (test-helpers/create-mock-source)
            tasks-before    (t2/count :model/RemoteSyncTask)]
        (mt/with-dynamic-fn-redefs [source/source-from-settings (constantly mock-source)]
          (mt/with-temporary-setting-values [remote-sync-url    "https://github.com/test/repo.git"
                                             remote-sync-token  "test-token"
                                             remote-sync-branch "main"
                                             remote-sync-type   :read-write]
            (is (= "Remote sync task in progress"
                   (mt/user-http-request :crowberto :post 400 "ee/remote-sync/import" {:expected_branch "main"})))
            (is (= tasks-before (t2/count :model/RemoteSyncTask))
                "no NEW RemoteSyncTask row should be created when the guard fires")))))))

(deftest export-refuses-while-task-running-test
  (testing "POST /api/ee/remote-sync/export returns 400 when a RemoteSyncTask is active"
    (mt/with-temp [:model/RemoteSyncTask _ {:sync_task_type "import"
                                            :initiated_by   (mt/user->id :rasta)
                                            :started_at     (t/offset-date-time)
                                            :progress       0.0}]
      (let [mock-source  (test-helpers/create-mock-source)
            tasks-before (t2/count :model/RemoteSyncTask)]
        (mt/with-dynamic-fn-redefs [source/source-from-settings (constantly mock-source)]
          (mt/with-temporary-setting-values [remote-sync-url    "https://github.com/test/repo.git"
                                             remote-sync-token  "test-token"
                                             remote-sync-branch "main"
                                             remote-sync-type   :read-write]
            (is (= "Remote sync task in progress"
                   (mt/user-http-request :crowberto :post 400 "ee/remote-sync/export"
                                         {:message "test export" :branch "main"})))
            (is (= tasks-before (t2/count :model/RemoteSyncTask))
                "no NEW RemoteSyncTask row should be created when the guard fires")))))))

(deftest create-branch-refuses-while-task-running-test
  (testing "POST /api/ee/remote-sync/create-branch returns 400 when a RemoteSyncTask is active"
    (mt/with-temp [:model/RemoteSyncTask _ {:sync_task_type "import"
                                            :initiated_by   (mt/user->id :rasta)
                                            :started_at     (t/offset-date-time)
                                            :progress       0.0}]
      (let [mock-source      (test-helpers/create-mock-source)
            initial-branches @(:branches-atom mock-source)]
        (mt/with-dynamic-fn-redefs [source/source-from-settings (constantly mock-source)]
          (mt/with-temporary-setting-values [remote-sync-url    "https://github.com/test/repo.git"
                                             remote-sync-token  "test-token"
                                             remote-sync-branch "main"
                                             remote-sync-type   :read-write]
            ;; The endpoint wraps the guard's exception with "Failed to create branch: ..." in its catch.
            (is (re-find #"Remote sync task in progress"
                         (str (mt/user-http-request :crowberto :post 400 "ee/remote-sync/create-branch"
                                                    {:name "feature-x"}))))
            (is (= "main" (settings/remote-sync-branch))
                "remote-sync-branch must remain unchanged when the guard fires")
            (is (= initial-branches @(:branches-atom mock-source))
                "no new branch should be pushed to the source when the guard fires")))))))

(deftest stash-refuses-while-task-running-test
  (testing "POST /api/ee/remote-sync/stash returns 400 when a RemoteSyncTask is active"
    (mt/with-temp [:model/RemoteSyncTask _ {:sync_task_type "import"
                                            :initiated_by   (mt/user->id :rasta)
                                            :started_at     (t/offset-date-time)
                                            :progress       0.0}]
      (let [mock-source      (test-helpers/create-mock-source)
            initial-branches @(:branches-atom mock-source)
            tasks-before     (t2/count :model/RemoteSyncTask)]
        (mt/with-dynamic-fn-redefs [source/source-from-settings (constantly mock-source)]
          (mt/with-temporary-setting-values [remote-sync-url    "https://github.com/test/repo.git"
                                             remote-sync-token  "test-token"
                                             remote-sync-branch "main"
                                             remote-sync-type   :read-write]
            ;; The endpoint wraps the guard's exception with "Failed to stash changes to branch: ..." in its catch.
            (is (re-find #"Remote sync task in progress"
                         (str (mt/user-http-request :crowberto :post 400 "ee/remote-sync/stash"
                                                    {:new_branch "stash-branch"
                                                     :message    "stash msg"}))))
            (is (= "main" (settings/remote-sync-branch))
                "remote-sync-branch must remain unchanged when the guard fires")
            (is (= initial-branches @(:branches-atom mock-source))
                "no new branch should be pushed to the source when the guard fires")
            (is (= tasks-before (t2/count :model/RemoteSyncTask))
                "no NEW RemoteSyncTask row should be created when the guard fires")))))))

(deftest archiving-a-synced-model-with-actions-test
  (testing "a model in a synced collection can be archived although it has actions, which are deleted with it"
    (mt/with-temporary-setting-values [remote-sync-type :read-write]
      (mt/with-temp [:model/Collection {synced-id :id} {:name "Synced" :is_remote_synced true :location "/"}
                     :model/Card       {model-id :id}  {:type :model :collection_id synced-id
                                                        :dataset_query (mt/mbql-query venues)}
                     :model/Action     {action-id :id} {:type :query :name "Rename" :model_id model-id}]
        (mt/user-http-request :crowberto :put 200 (str "card/" model-id) {:archived true})
        (is (not (t2/exists? :model/Action :id action-id)))))))