From 31a079a7a2c733c9162c7421aea56b53479968a4 Mon Sep 17 00:00:00 2001 From: Eva Marco Date: Thu, 8 Oct 2026 09:55:18 +0200 Subject: [PATCH] :bug: Allow creating tokens when the file is its own tokens source (#12182) Setting the local file back as the tokens source, or unlinking the library that was the source, stored the file's own id in :tokens-source. ensure-tokens-lib only created a tokens-lib when :tokens-source was nil, so a file without a tokens-lib never got one, and every token change was dropped without any error. Now ensure-tokens-lib also creates the tokens-lib when the source is the file itself, which fixes files already in that state, and set-tokens-source removes the attribute instead of storing the file's own id. Closes #12180 AI-assisted-by: claude-opus-5-5 --- common/src/app/common/files/tokens.cljc | 12 +++++++++--- .../test/common_tests/files/tokens_test.cljc | 19 ++++++++++++++++++- .../test/common_tests/logic/token_test.cljc | 4 ++-- 3 files changed, 29 insertions(+), 6 deletions(-) diff --git a/common/src/app/common/files/tokens.cljc b/common/src/app/common/files/tokens.cljc index a9506f938e..34c5b760d6 100644 --- a/common/src/app/common/files/tokens.cljc +++ b/common/src/app/common/files/tokens.cljc @@ -460,10 +460,13 @@ :active-set-ids active-set-ids))) (defn ensure-tokens-lib - "Ensure file-data has a :tokens-lib or :tokens-source, and also a :tokens-status, creating them if necessary." + "Ensure file-data has a :tokens-lib or an external :tokens-source, and also a :tokens-status, + creating them if necessary." [file-data] (cond-> file-data - (nil? (:tokens-source file-data)) + (let [tokens-source (:tokens-source file-data)] + (or (nil? tokens-source) + (= tokens-source (:id file-data)))) (update :tokens-lib #(or % (ctob/make-tokens-lib))) :always @@ -488,9 +491,12 @@ (or (:tokens-source file-data) (:id file-data))) (defn set-tokens-source + "Set the tokens source of the file. Setting it to nil or to the file itself + removes the attribute, so the file uses its own tokens-lib." [file-data tokens-source] (assert (or (nil? tokens-source) (uuid? tokens-source)) "expected nil or valid uuid") - (if (nil? tokens-source) + (if (or (nil? tokens-source) + (= tokens-source (:id file-data))) (dissoc file-data :tokens-source) (assoc file-data :tokens-source tokens-source))) diff --git a/common/test/common_tests/files/tokens_test.cljc b/common/test/common_tests/files/tokens_test.cljc index 93b87a12e1..76da45bd4e 100644 --- a/common/test/common_tests/files/tokens_test.cljc +++ b/common/test/common_tests/files/tokens_test.cljc @@ -254,6 +254,14 @@ (t/is (not (contains? file-data' :tokens-lib))) (t/is (= source-id (:tokens-source file-data'))) (t/is (contains? file-data' :tokens-status)) + (t/is (ctos/tokens-status? (:tokens-status file-data'))))) + + (t/testing "ensure-tokens-lib should add a tokens-lib if the tokens-source is the file itself" + (let [file (thf/sample-file :file1) + file-data (-> (ctf/file-data file) + (assoc :tokens-source (:id file))) + file-data' (cfo/ensure-tokens-lib file-data)] + (t/is (ctob/tokens-lib? (:tokens-lib file-data'))) (t/is (ctos/tokens-status? (:tokens-status file-data')))))) (t/deftest test-get-tokens-lib @@ -293,7 +301,16 @@ file (thf/sample-file :file1) file-data (:data file) file-data' (cfo/set-tokens-source file-data source-id)] - (t/is (= source-id (:tokens-source file-data')))))) + (t/is (= source-id (:tokens-source file-data'))))) + + (t/testing "removes tokens-source when set to the file itself" + (let [source-id (thi/new-id! :tokens-source) + file (thf/sample-file :file1) + file-data (-> (ctf/file-data file) + (assoc :tokens-source source-id)) + file-data' (cfo/set-tokens-source file-data (:id file))] + (t/is (not (contains? file-data' :tokens-source))) + (t/is (= (:id file) (cfo/get-effective-tokens-source file-data')))))) (t/deftest test-effective-tokens-source? (t/testing "no tokens, no tokens-source, ask for file id -> true" diff --git a/common/test/common_tests/logic/token_test.cljc b/common/test/common_tests/logic/token_test.cljc index 4ff3d97a10..20ab2e59fb 100644 --- a/common/test/common_tests/logic/token_test.cljc +++ b/common/test/common_tests/logic/token_test.cljc @@ -146,7 +146,7 @@ (t/is (= (ctos/get-active-set-ids file-tokens-status) (ctos/get-active-set-ids undo-tokens-status))))) - (t/testing "if the library is the same file, the source is set to self and the status is the same one" + (t/testing "if the library is the same file, the source is removed and the status is the same one" (let [set-id (uuid/next) file (tht/sample-file-with-tokens :file-id :file1 @@ -166,7 +166,7 @@ undo-tokens-status (tht/get-tokens-status undo)] ;; Redo: source and status copied from library, replacing the previous status - (t/is (= (:id file) redo-tokens-source)) + (t/is (nil? redo-tokens-source)) (t/is (= (ctos/get-active-theme-ids file-tokens-status) (ctos/get-active-theme-ids redo-tokens-status))) (t/is (= (ctos/get-active-set-ids file-tokens-status)