mirror of
https://github.com/penpot/penpot.git
synced 2026-11-16 23:20:37 +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
276 lines
13 KiB
Clojure
276 lines
13 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.workspace-undo-test
|
|
"Tests for the undo groups of commits and history entries, and for
|
|
forgetting them after a rollback."
|
|
(:require
|
|
[app.common.test-helpers.files :as cthf]
|
|
[app.common.test-helpers.ids-map :as cthi]
|
|
[app.common.uuid :as uuid]
|
|
[app.main.data.changes :as dch]
|
|
[app.main.data.comments :as dcmt]
|
|
[app.main.data.fonts :as df]
|
|
[app.main.data.workspace :as dw]
|
|
[app.main.data.workspace.undo :as dwu]
|
|
[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.state :as ths]
|
|
[potok.v2.core :as ptk]))
|
|
|
|
(t/use-fixtures :each {:before cthi/reset-idmap!})
|
|
|
|
(defn- rename-page
|
|
"Commits a page rename, with `params` for `commit-changes`."
|
|
[page-id name & {:as params}]
|
|
(dch/commit-changes (merge {:redo-changes [{:type :mod-page :id page-id :name name}]
|
|
:undo-changes [{:type :mod-page :id page-id :name "Page 1"}]}
|
|
params)))
|
|
|
|
(defn- run-commits
|
|
"Runs `events` on a store of a sample file and calls `check` with the
|
|
undo groups of the commits made and the final state."
|
|
[done events check]
|
|
(let [file (cthf/sample-file :file1 :page-label :page1)
|
|
store (ths/setup-store file)
|
|
groups (atom [])
|
|
sub (->> (ptk/input-stream store)
|
|
(rx/filter dch/commit?)
|
|
(rx/subs! #(swap! groups conj (:undo-group (deref %)))))]
|
|
(ths/run-store store done (events (cthf/current-page-id file))
|
|
(fn [state]
|
|
(rx/dispose! sub)
|
|
(check @groups state)))))
|
|
|
|
;; Scenario: an action commits twice inside one undo transaction (as applying
|
|
;; a token to several shapes does). Proves: both commits carry the undo group
|
|
;; of the transaction, which its history entry takes too, so a rejected save
|
|
;; reverts the whole Ctrl+Z step, not half of it.
|
|
(t/deftest commits-inside-a-transaction-share-its-undo-group
|
|
(t/async
|
|
done
|
|
(let [tx (js/Symbol)
|
|
tx-group (atom nil)
|
|
spy (ptk/reify ::spy-transaction
|
|
ptk/EffectEvent
|
|
(effect [_ state _]
|
|
(reset! tx-group (get-in state [:workspace-undo :transaction :commit-group]))))]
|
|
(run-commits done
|
|
(fn [page-id]
|
|
[(dwu/start-undo-transaction tx)
|
|
spy
|
|
(rename-page page-id "A")
|
|
(rename-page page-id "B")
|
|
(dwu/commit-undo-transaction tx)])
|
|
(fn [groups _]
|
|
(t/is (some? @tx-group) "the transaction has its own group")
|
|
(t/is (= [@tx-group @tx-group] groups)))))))
|
|
|
|
;; Scenario: commits made with no transaction open. Proves: each one keeps
|
|
;; its own undo group.
|
|
(t/deftest commits-outside-a-transaction-get-their-own-undo-group
|
|
(t/async
|
|
done
|
|
(run-commits done
|
|
(fn [page-id]
|
|
[(rename-page page-id "A")
|
|
(rename-page page-id "B")])
|
|
(fn [groups _]
|
|
(t/is (= 2 (count (distinct groups))))))))
|
|
|
|
;; Scenario: inside a transaction, a commit names its undo group (e.g. a
|
|
;; component update that also writes the library). Proves: the explicit group
|
|
;; is kept.
|
|
(t/deftest an-explicit-undo-group-is-kept-inside-a-transaction
|
|
(t/async
|
|
done
|
|
(let [tx (js/Symbol)
|
|
group (uuid/next)]
|
|
(run-commits done
|
|
(fn [page-id]
|
|
[(dwu/start-undo-transaction tx)
|
|
(rename-page page-id "A" :undo-group group)
|
|
(dwu/commit-undo-transaction tx)])
|
|
(fn [groups _]
|
|
(t/is (= [group] groups)))))))
|
|
|
|
;; Scenario: a rollback forgets undo groups while a transaction is open, then
|
|
;; the transaction commits again. Proves: the later commit keeps the group of
|
|
;; the earlier one, so the transaction stays one rollback unit.
|
|
(t/deftest a-transaction-keeps-its-undo-group-after-forget-undo-groups
|
|
(t/async
|
|
done
|
|
(let [tx (js/Symbol)]
|
|
(run-commits done
|
|
(fn [page-id]
|
|
[(dwu/start-undo-transaction tx)
|
|
(rename-page page-id "A")
|
|
(ptk/reify ::forget-the-transaction-group
|
|
ptk/UpdateEvent
|
|
(update [_ state]
|
|
(let [group (get-in state [:workspace-undo :transaction :commit-group])]
|
|
;; The transaction holds the first commit, as `append-undo` records it
|
|
(->> (assoc-in state [:workspace-undo :transaction :undo-groups] #{group})
|
|
(ptk/update (dwu/forget-undo-groups #{group} nil))))))
|
|
(rename-page page-id "B")
|
|
(dwu/commit-undo-transaction tx)])
|
|
(fn [[first-group second-group :as groups] _]
|
|
(t/is (= 2 (count groups)))
|
|
(t/is (some? first-group))
|
|
(t/is (= first-group second-group)))))))
|
|
|
|
(defn- entry
|
|
[page-id name group]
|
|
{:undo-changes [{:type :mod-page :id page-id :name "Page 1"}]
|
|
:redo-changes [{:type :mod-page :id page-id :name name}]
|
|
:undo-group group
|
|
:tags #{}
|
|
:translation false})
|
|
|
|
;; Scenario: inside one transaction, a commit names another undo group (as a
|
|
;; component update does). Proves: the history entry records both groups,
|
|
;; so a rollback of either one finds it.
|
|
(t/deftest a-transaction-entry-records-every-group-it-holds
|
|
(let [page-id (uuid/next)
|
|
[g1 g2] (repeatedly 2 uuid/next)
|
|
tx (js/Symbol)
|
|
state (reduce #(ptk/update %2 %1)
|
|
{}
|
|
[(dwu/start-undo-transaction tx)
|
|
(dwu/append-undo (entry page-id "A" g1) false)
|
|
(dwu/append-undo (entry page-id "B" g2) false)
|
|
(dwu/commit-undo-transaction tx)])]
|
|
(t/is (= #{g1 g2} (get-in state [:workspace-undo :items 0 :undo-groups])))))
|
|
|
|
;; Scenario: a commit stacks onto the last history entry. Proves: the entry
|
|
;; records the stacked commit's group too.
|
|
(t/deftest a-stacked-commit-adds-its-group-to-the-entry
|
|
(let [page-id (uuid/next)
|
|
[g1 g2] (repeatedly 2 uuid/next)
|
|
state (reduce #(ptk/update %2 %1)
|
|
{}
|
|
[(dwu/append-undo (entry page-id "A" g1) false)
|
|
(dwu/append-undo (entry page-id "B" g2) true)])]
|
|
(t/is (= 1 (count (get-in state [:workspace-undo :items]))))
|
|
(t/is (= #{g1 g2} (get-in state [:workspace-undo :items 0 :undo-groups])))))
|
|
|
|
;; Scenario: the user presses Ctrl+Z on a page rename. Proves: the commit it
|
|
;; makes can be undone (its undo changes redo the rename) and names the index
|
|
;; the history moved from, so a rollback can put both back.
|
|
(t/deftest an-undo-commit-can-be-undone
|
|
(t/async
|
|
done
|
|
(let [file (cthf/sample-file :file1 :page-label :page1)
|
|
page-id (cthf/current-page-id file)
|
|
store (ths/setup-store file)
|
|
commits (atom [])
|
|
sub (->> (ptk/input-stream store)
|
|
(rx/filter dch/commit?)
|
|
(rx/subs! #(swap! commits conj (deref %))))
|
|
item (assoc (entry page-id "A" (uuid/next)) :undo-groups #{})]
|
|
(ths/run-store store done
|
|
[#(assoc % :workspace-undo {:items [item] :index 0})
|
|
dwu/undo]
|
|
(fn [state]
|
|
(rx/dispose! sub)
|
|
(let [[commit] @commits]
|
|
(t/is (= 1 (count @commits)))
|
|
(t/is (= (:undo-changes item) (:redo-changes commit)))
|
|
(t/is (= (:redo-changes item) (:undo-changes commit)))
|
|
(t/is (= 0 (:undo-index commit)))
|
|
(t/is (= -1 (get-in state [:workspace-undo :index])))))))))
|
|
|
|
;; Forgetting the undo groups of a rollback.
|
|
|
|
(defn- entry-of-groups
|
|
"An undo entry holding commits of `groups`, the first one naming it."
|
|
[& groups]
|
|
{:undo-group (first groups) :undo-groups (set groups)})
|
|
|
|
(defn- forget
|
|
"The undo state after forgetting `groups` from `undo-state`."
|
|
[undo-state groups undo-index]
|
|
(:workspace-undo (ptk/update (dwu/forget-undo-groups groups undo-index)
|
|
{:workspace-undo undo-state})))
|
|
|
|
(t/deftest forgetting-undo-groups-cuts-the-history-at-the-first-match
|
|
(let [[g1 g2 g3] (repeatedly 3 uuid/next)
|
|
undo (forget {:items [(entry-of-groups g1) (entry-of-groups g2) (entry-of-groups g3)]
|
|
:index 2
|
|
:transaction {:undo-group g3 :undo-changes [{:type :mod-page}]}}
|
|
#{g2 g3} nil)]
|
|
(t/is (= [g1] (mapv :undo-group (:items undo))))
|
|
(t/is (= 0 (:index undo)))
|
|
(t/is (empty? (get-in undo [:transaction :undo-changes])) "the open transaction is emptied")
|
|
(t/is (= g3 (get-in undo [:transaction :commit-group]))
|
|
"its later commits keep the group of its earlier ones")))
|
|
|
|
(t/deftest forgetting-undo-groups-matches-any-group-an-entry-holds
|
|
(let [[g1 g2 g3] (repeatedly 3 uuid/next)
|
|
undo (forget {:items [(entry-of-groups g1) (entry-of-groups g2 g3)] :index 1}
|
|
#{g3} nil)]
|
|
(t/is (= [g1] (mapv :undo-group (:items undo))))
|
|
(t/is (= 0 (:index undo)))))
|
|
|
|
(t/deftest forgetting-undo-groups-keeps-the-history-without-a-match
|
|
(let [[g1 g2] (repeatedly 2 uuid/next)
|
|
tx {:undo-group g2 :undo-groups #{g2} :undo-changes [{:type :mod-page}]}
|
|
undo (forget {:items [(entry-of-groups g1)] :index 0 :transaction tx}
|
|
#{(uuid/next)} nil)]
|
|
(t/is (= [g1] (mapv :undo-group (:items undo))))
|
|
(t/is (= 0 (:index undo)))
|
|
(t/is (= tx (:transaction undo)) "an unrelated open transaction stays")))
|
|
|
|
(t/deftest forgetting-undo-groups-empties-a-transaction-that-holds-one
|
|
(let [[g1 g2] (repeatedly 2 uuid/next)
|
|
tx {:undo-group g2 :undo-groups #{g2} :undo-changes [{:type :mod-page}]}
|
|
undo (forget {:items [(entry-of-groups g1)] :index 0 :transaction tx}
|
|
#{g2} nil)]
|
|
(t/is (= [g1] (mapv :undo-group (:items undo))) "the history stays")
|
|
(t/is (empty? (get-in undo [:transaction :undo-changes])))
|
|
(t/is (= g2 (get-in undo [:transaction :commit-group])))))
|
|
|
|
(t/deftest forgetting-undo-groups-points-back-to-the-index-of-an-undone-undo
|
|
(let [[g1 g2 g3] (repeatedly 3 uuid/next)
|
|
undo-state {:items [(entry-of-groups g1) (entry-of-groups g2) (entry-of-groups g3)]
|
|
:index 0}]
|
|
(t/is (= 2 (:index (forget undo-state #{(uuid/next)} 2))))
|
|
(t/is (= 0 (:index (forget undo-state #{g2} 2)))
|
|
"a cut history cannot point past its end")))
|
|
|
|
;; Scenario: the workspace opens and then reloads twice (a peer's restore,
|
|
;; the rollback fallback), each reload running `initialize-workspace` again
|
|
;; without leaving the workspace; then one commit is made. Proves: each
|
|
;; reload replaces the workspace streams, so the commit adds one undo entry,
|
|
;; not one per reload.
|
|
(t/deftest ^:async a-workspace-reload-adds-one-undo-entry-per-commit
|
|
(let [file (cthf/sample-file :file1 :page-label :page1)
|
|
team-id (uuid/next)
|
|
store (ths/setup-store file {:renderer :svg})
|
|
loaded (fn [type] (mock/stub (fn [& _] (ptk/data-event type {}))))]
|
|
(await
|
|
(mock/with-mocks*
|
|
{dw/initialize-file (loaded ::file-requested)
|
|
dcmt/retrieve-comment-threads (loaded ::threads-requested)
|
|
dcmt/fetch-profiles (loaded ::profiles-requested)
|
|
df/fetch-fonts (loaded ::fonts-requested)}
|
|
(try
|
|
(ptk/emit! store
|
|
#(assoc % :current-team-id team-id)
|
|
(dw/initialize-workspace team-id (:id file))
|
|
(ptk/event ::dw/reload-current-file)
|
|
(ptk/event ::dw/reload-current-file))
|
|
(await (async/settle))
|
|
(ptk/emit! store (rename-page (cthf/current-page-id file) "Renamed"))
|
|
(await (async/wait-for-store store #(seq (get-in @store [:workspace-undo :items]))
|
|
"the commit reaches the undo history"))
|
|
(await (async/settle))
|
|
(t/is (= 1 (count (get-in @store [:workspace-undo :items]))))
|
|
(finally
|
|
(rx/dispose! store)))))))
|