mirror of
https://github.com/penpot/penpot.git
synced 2026-11-17 07:30:35 +00:00
* ♻️ Wait on signals instead of event-loop turns `wait-for` counted its budget in event-loop turns (50 x `setTimeout 0`, about 54 ms idle), so a delivery that travelled through the RxJS async scheduler could arrive after the wait gave up. It now accepts a `:signal`: the predicate is re-checked on every emission and the wait resolves on the event that changes the state. The ceiling (5000 ms) only bounds failure when a signal is given, and stays a poll budget for the waits that have nothing to listen to. `wait-for-store` wraps the store's input stream, which notifies its subscribers after applying the update. The five explicit `:max-ticks` sites move to `:timeout-ms` in the same change, so the old option never lives in a state where destructuring silently ignores it. New `frontend-tests.helpers.async-test` pins the contract: a late delivery resolves, a store signal resolves on the event without a poll, an already-true predicate does not yield, a never-true predicate records one failure within the ceiling, and a resolved wait stops listening. AI-assisted-by: deepseek-v4.1-flash * ♻️ Wait for the persistence requests on their own stream The persistence tests waited for `(= N (count @requests))` by polling the atom at every event-loop turn. The mocked transport now pushes each request into a `requests$` subject, and those 59 waits pass it as `:signal`, so they resolve on the request that makes the count true. The two predicates that mix the request count with the store state merge both signals. The wait that flaked (`the other file's edit is sent next`, in the rollback test) is one of these: it no longer depends on a turn budget, and it cannot lose the delivery. AI-assisted-by: deepseek-v4.1-flash * ♻️ Wait for the store event in the store backed tests 153 waits whose predicate reads the store now go through `wait-for-store`, so they resolve on the store event that changes the state instead of polling at every event-loop turn. The count is 153, not the 162 the plan estimated: nine of them read the store through a helper (`fill-color`, `shape-name`, `copy-children`) rather than `@store`. The five sites that carried an explicit ceiling drop it, since with an event the ceiling only bounds failure. Resolving on the event exposes the places where the poll was really a pause for a side effect to advance: - `tokens-source-test` emitted `unlink-file-from-library`, a watch that chains a second `rp/cmd!` after an async hop. The store signal fires before the chain runs (the event itself dissocs the library), so the mock scope closed in flight and the second call reached the real HTTP layer. The test now waits for that second call, through the mock. - `persistence-rollback-test` drives the persistence pipeline step by step; two of its waits are the pause before the next emit, not an effect, so they keep the poll. AI-assisted-by: deepseek-v4.1-flash * 📚 Document the wait contract and what still polls `mem:frontend/testing` described `wait-for` as an immediate check plus a bounded poll. It now waits on a signal, and the ceiling only bounds failure; `wait-for-store` and the persistence `requests$` subject are how a wait resolves on the event that changes the state. The waits with nothing to listen to keep the poll, and the memory names them. The two traps that cost a red suite are recorded too: a wait that means "the side effect advanced" has no event and keeps the poll, and a store-state wait can close the mock scope before the flow it observes ends, so the chain's last RPC call has to be awaited through the mock. AI-assisted-by: deepseek-v4.1-flash * ♻️ Give the wait wrapper a single signal `wait-for-store` built its options with `assoc`, so it replaced the `:signal` a caller passed. The two waits whose predicate reads both the store and the request counter relied on a merged signal that was being discarded; they now pass it to `wait-for` themselves, and the wrapper takes no options. That merge was the safety net for a request counted after the store event, and it was inert. The helper also reports why a wait failed (ceiling, ended signal, or an error with its cause) instead of calling all three a timeout, counts the predicate evaluations in the late-delivery case to prove the signal path does not poll, reads the clock through `app.common.time` in the helper and the test, and the test's turn probe cancels its timer. The copyright header of `helpers/async.cljs` stays as the previous commit left it, matching the majority spelling. AI-assisted-by: deepseek-v4.1-flash * ♻️ Give the cycle counter a signal too Two waits in the retry tests read the store and the `cycles` atom, which the `slow-cycle-timer` double fills inside the persistence watch. They passed only the store signal, so a counter change could not wake them: the same latent race the previous commit removed from the request count. The double now also emits each new cycle into a `cycles$` subject, and both waits pass the merged signal to `wait-for`. The public `wait-for` docstring names the three failure reasons, and the message for a failed signal bounds the cause text: the exception message when there is one, otherwise the printed value inside a try, cut at 500 characters. AI-assisted-by: deepseek-v4.1-flash * ♻️ Stop the error handlers flooding the test log Four tests in `main-errors-test` run a real error handler end to end. Those handlers print the cause with `ex/print-throwable` before notifying, which is what a developer wants in the app; in a test it only writes console noise. One of them replays 10,000 offline errors, so a run carried 10,006 dumps: 190,010 of its 192,682 lines, 13.8 MB, well past the ~4 MB where GitHub truncates a step log and takes the summary and any failing assertion with it. The tests stub `ex/print-throwable` with a no-op for the duration of their mock scope. The handlers still run, the assertions do not change, and the 10,000-error loop still proves the governor emits once. A full run now logs 2,583 lines and 384 KB. The single block that remains comes from the re-entrancy guard, which prints its own notice on purpose. AI-assisted-by: deepseek-v4.1-flash * ♻️ Silence the deliberate WASM panic in its test `get-svg-markup` logs a WASM panic with `console.error` before returning empty, and `get-svg-markup-swallows-wasm-errors` sends two panics through it on purpose. That was two 35-line dumps in which the exception printed its own `data`, `arr` and protocol masks, and it reads like a failure that the suite is hiding. The test replaces `(.-error js/console)` for its body and restores it in a `finally`. The assertions and the helper stay as they are: the same panic answered with a different stack would still show up, because only this test silences the console. AI-assisted-by: deepseek-v4.1-flash * 🐛 Use real timer for wait-poll ceiling wait-poll read the app clock (ct/now), so tests freezing that clock never reached the deadline and hung instead of failing. Track expiry with a real setTimeout flag, pass timeout-ms through wait-signal and wait-for, and cover the frozen-clock case with a regression test. AI-assisted-by: muse-spark-1.3-contributor
210 lines
9.9 KiB
Clojure
210 lines
9.9 KiB
Clojure
;; This Source Code Form is subject to the terms of the Mozilla Public
|
|
;; License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
;; file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
;;
|
|
;; Copyright (c) KALEIDOS SUBSIDIARY SL
|
|
|
|
(ns frontend-tests.data.persistence-lock-test
|
|
"Tests for the commits made while saves fail and the workspace is
|
|
locked (`app.main.data.persistence.lock`): user edits are refused, the
|
|
undo of rolled-back commits and derived data still commit."
|
|
(:require
|
|
[app.common.test-helpers.components :as cthc]
|
|
[app.common.test-helpers.compositions :as ctho]
|
|
[app.common.test-helpers.files :as cthf]
|
|
[app.common.test-helpers.ids-map :as cthi]
|
|
[app.common.test-helpers.shapes :as cths]
|
|
[app.common.uuid :as uuid]
|
|
[app.main.data.changes :as dch]
|
|
[app.main.data.persistence :as dps]
|
|
[app.main.data.persistence.lock :as plk]
|
|
[app.main.data.workspace :as dw]
|
|
[app.main.data.workspace.libraries :as dwl]
|
|
[app.main.data.workspace.undo :as dwu]
|
|
[app.main.repo :as rp]
|
|
[app.render-wasm.api :as wasm.api]
|
|
[beicon.v2.core :as rx]
|
|
[cljs.test :as t :include-macros true]
|
|
[frontend-tests.helpers.async :as async]
|
|
[frontend-tests.helpers.mock :as mock]
|
|
[frontend-tests.helpers.persistence :as thp]
|
|
[frontend-tests.helpers.state :as ths]
|
|
[potok.v2.core :as ptk]))
|
|
|
|
(t/use-fixtures :each {:before cthi/reset-idmap!})
|
|
|
|
(defn- server-error
|
|
"A save answer that fails for good: an unexpected server error."
|
|
[]
|
|
(rp/handle-response {:status 500
|
|
:body {:type :server-error :code :unhandled}
|
|
:uri "https://penpot.example.com/api/main/methods/update-file"}))
|
|
|
|
(defn- answers
|
|
"A transport that gives each save, in order, the next of `responses`
|
|
(functions returning the answer's observable)."
|
|
[& responses]
|
|
(let [pending (atom responses)]
|
|
(fn [_ _]
|
|
(let [respond (first @pending)]
|
|
(swap! pending rest)
|
|
(respond)))))
|
|
|
|
(defn- saved
|
|
[revn]
|
|
#(rx/of {:revn revn}))
|
|
|
|
(def ^:private lock
|
|
"The lock a failed save leaves, set by hand."
|
|
#(assoc-in % [:workspace-global :persistence-lock] {:read-only? false}))
|
|
|
|
(defn- record-commits
|
|
"Records, for each commit on `store`, whether the workspace was locked
|
|
when it was made. Answers the subscription and the atom."
|
|
[store]
|
|
(let [made (atom [])
|
|
sub (->> (ptk/input-stream store)
|
|
(rx/filter dch/commit?)
|
|
(rx/subs! (fn [_] (swap! made conj (plk/locked? @store)))))]
|
|
[sub made]))
|
|
|
|
;; Scenario: a save fails for good and the workspace locks; the user then
|
|
;; renames the shape through some UI the lock did not hide. Proves: the
|
|
;; edit makes no commit, so nothing queues behind the failed save and the
|
|
;; shape keeps its name.
|
|
(t/deftest ^:async an-edit-while-locked-makes-no-commit
|
|
(await
|
|
(thp/with-persistence
|
|
(^:async fn [{:keys [file-id store]}]
|
|
(let [page-id (uuid/next)
|
|
shape-id (uuid/next)
|
|
[sub made] (record-commits store)]
|
|
(ptk/emit! store
|
|
(thp/open-file file-id page-id (thp/file-data page-id shape-id "B"))
|
|
(thp/rename-commit file-id page-id shape-id "A" "B")
|
|
::dps/force-persist)
|
|
(await (async/wait-for-store store #(plk/locked? @store) "the failed save locks the workspace"))
|
|
(reset! made [])
|
|
(ptk/emit! store
|
|
(dch/commit-changes {:redo-changes [(thp/rename page-id shape-id "C")]
|
|
:undo-changes [(thp/rename page-id shape-id "B")]})
|
|
::dps/force-persist)
|
|
(await (async/settle))
|
|
(rx/dispose! sub)
|
|
(t/is (empty? @made) "no commit is made")
|
|
(t/is (= "B" (thp/shape-name store file-id page-id shape-id)))
|
|
(t/is (empty? (get-in @store [:persistence :buffer])))
|
|
(t/is (= 1 (count (get-in @store [:persistence :queue])))
|
|
"the queue holds only the failed edit")))
|
|
(answers server-error))))
|
|
|
|
;; Scenario: the user presses Ctrl+Z while the workspace is locked.
|
|
;; Proves: the history stays where it was, since its commit would be
|
|
;; refused after the index moved.
|
|
(t/deftest ^:async undo-and-redo-do-nothing-while-locked
|
|
(let [file (-> (cthf/sample-file :file1)
|
|
(ctho/add-rect :rect1 :name "A"))
|
|
store (ths/setup-store file {:renderer :svg})
|
|
page-id (cthf/current-page-id file)
|
|
rect-id (cthi/id :rect1)
|
|
rename (fn [from to]
|
|
(dch/commit-changes {:redo-changes [(thp/rename page-id rect-id to)]
|
|
:undo-changes [(thp/rename page-id rect-id from)]}))
|
|
group (uuid/next)
|
|
entry {:undo-changes [(thp/rename page-id rect-id "A")]
|
|
:redo-changes [(thp/rename page-id rect-id "B")]
|
|
:undo-group group
|
|
:undo-groups #{group}
|
|
:tags #{}}
|
|
name-of #(:name (cths/get-shape (ths/get-file-from-state @store) :rect1))]
|
|
(ptk/emit! store (rename "A" "B") #(assoc % :workspace-undo {:items [entry] :index 0}))
|
|
(await (async/wait-for #(= "B" (name-of)) "the shape is renamed"))
|
|
(ptk/emit! store lock dwu/undo)
|
|
(await (async/settle))
|
|
(t/is (= "B" (name-of)) "undo changes nothing")
|
|
(t/is (= 0 (get-in @store [:workspace-undo :index])))
|
|
(ptk/emit! store #(update % :workspace-global dissoc :persistence-lock) dwu/undo)
|
|
(await (async/wait-for #(= "A" (name-of)) "undo works once unlocked"))
|
|
(ptk/emit! store lock dwu/redo)
|
|
(await (async/settle))
|
|
(t/is (= "A" (name-of)) "redo changes nothing")
|
|
(t/is (= -1 (get-in @store [:workspace-undo :index])))))
|
|
|
|
;; Scenario: one action renames a shape in a library and in this file;
|
|
;; the library's save lands, this file's fails for good and the workspace
|
|
;; locks. Edit rights come back with a role change, which releases the
|
|
;; failed edit while the lock still holds. Proves: the undo of the
|
|
;; library's saved rename still commits and is saved, so the library goes
|
|
;; back with the released edit.
|
|
(t/deftest ^:async a-release-undoes-saved-commits-while-locked
|
|
(await
|
|
(thp/with-persistence
|
|
(^:async fn [{:keys [file-id store requests]}]
|
|
(let [lib-id (uuid/next)
|
|
page-id (uuid/next)
|
|
shape-id (uuid/next)
|
|
lshape-id (uuid/next)
|
|
group (uuid/next)
|
|
linked (ptk/data-event ::dch/commit
|
|
(assoc @(thp/rename-commit lib-id page-id lshape-id "X" "Y")
|
|
:undo-group group))
|
|
commit (ptk/data-event ::dch/commit
|
|
(assoc @(thp/rename-commit file-id page-id shape-id "A" "B")
|
|
:undo-group group))
|
|
[sub made] (record-commits store)]
|
|
(ptk/emit! store (thp/open-file-with-library file-id lib-id page-id
|
|
(thp/file-data page-id shape-id "B")
|
|
(thp/file-data page-id lshape-id "Y")))
|
|
(ptk/emit! store linked commit ::dps/force-persist)
|
|
(await (async/wait-for-store store #(plk/locked? @store) "this file's failed save locks the workspace"))
|
|
(reset! made [])
|
|
(ptk/emit! store (ptk/data-event :app.main.data.common/change-team-role))
|
|
(await (async/wait-for #(seq @made) "the library's rename is undone"))
|
|
(t/is (= [true] @made) "the undo commits while the lock holds")
|
|
(t/is (= "X" (thp/shape-name store lib-id page-id lshape-id)))
|
|
(t/is (= "A" (thp/shape-name store file-id page-id shape-id)))
|
|
(ptk/emit! store ::dps/force-persist)
|
|
(await (async/wait-for-store store #(= :saved (get-in @store [:persistence :status]))
|
|
"the undo is saved"))
|
|
(rx/dispose! sub)
|
|
(t/is (= 3 (count @requests)))
|
|
(t/is (= lib-id (get-in (nth @requests 2) [1 :id])))
|
|
(t/is (not (plk/locked? @store)))))
|
|
(answers (saved 1) server-error (saved 2)))))
|
|
|
|
;; Scenario: texts without layout data are measured while the workspace is
|
|
;; locked, as on a reload during the lock. Proves: the derived layout still
|
|
;; commits, so the text and its layout data stay in step.
|
|
(t/deftest ^:async text-layout-data-commits-while-locked
|
|
(let [file (-> (cthf/sample-file :file1)
|
|
(ctho/add-text :text1 "hello"))
|
|
store (ths/setup-store file {:renderer :svg})
|
|
position-data [{:x 0 :y 0 :width 80 :height 16 :fills [] :text "hello"}]
|
|
[sub made] (record-commits store)]
|
|
(await
|
|
(mock/with-mocks*
|
|
{wasm.api/calculate-position-data (mock/stub (fn [_] position-data))}
|
|
(ptk/emit! store lock (dw/update-page-position-data))
|
|
(await (async/wait-for #(seq @made) "the layout data is committed"))))
|
|
(rx/dispose! sub)
|
|
(t/is (= [true] @made))
|
|
(t/is (= position-data
|
|
(:position-data (cths/get-shape (ths/get-file-from-state @store) :text1))))))
|
|
|
|
;; Scenario: a main component changes while the workspace is locked, by the
|
|
;; undo of a rolled-back commit. Proves: its modification time still
|
|
;; commits, so other files see the component changed again.
|
|
(t/deftest ^:async a-component-touch-commits-while-locked
|
|
(let [file (-> (cthf/sample-file :file1)
|
|
(ctho/add-simple-component :component1 :main-root :main-child))
|
|
comp-id (cthi/id :component1)
|
|
;; No modification time yet, so the touch shows whatever the clock
|
|
file (update-in file [:data :components comp-id] dissoc :modified-at)
|
|
store (ths/setup-store file {:renderer :svg})
|
|
[sub made] (record-commits store)]
|
|
(ptk/emit! store lock (dwl/touch-component comp-id))
|
|
(await (async/wait-for #(seq @made) "the touch is committed"))
|
|
(rx/dispose! sub)
|
|
(t/is (= [true] @made))
|
|
(t/is (some? (:modified-at (cthc/get-component (ths/get-file-from-state @store) :component1))))))
|