diff --git a/.serena/memories/frontend/testing.md b/.serena/memories/frontend/testing.md index 37f5304395..b16acf8b6d 100644 --- a/.serena/memories/frontend/testing.md +++ b/.serena/memories/frontend/testing.md @@ -17,7 +17,8 @@ Frontend testing is async-first: everything essentially asynchronous is modeled - `mock/with-mocks` (callback style, legacy compat): installs with `set!` so mocks survive async boundaries; bodies run deferred past the current tick via `asap`, so only done-chained (`t/async`) contexts are allowed; `done'` restores and completes exactly once (twice only warns; never calling it stalls the run and leaks the mocks). Prefer `mock/with-mocks*` for new tests. - `mock/with-mocks*` (direction): body forms wrapped in a generated `^:async` fn, evaluates to a promise — `await` it, `await` nested scopes too, no `done` in test code. Rejections and non-promise returns report as `:error` via `run-mocked`. - `mock/stub` wraps fns for arities 0-6 (the `:esm` test build dispatches multi-arity vars as `cljs$core$IFn$_invoke$arity$N`); when the mocked var is variadic-defined (or called with more than 6 args), the call compiles to variadic dispatch, so use a plain variadic `fn` — the stub does not forward variadic. A mock must not call the mocked var again (self-delegation inside a multi-arity function recurses). -- Helpers in `frontend-tests.helpers.async`: `->promise` (single-value observable → promise; beicon has no `to-promise`), `await-response` (subscribe→push→await, atomic), `settle`, `wait-for` (immediate check + bounded poll, fails instead of hanging), `observe` (stream → termination promise; asserts provided, timeout rejects). +- Helpers in `frontend-tests.helpers.async`: `->promise` (single-value observable → promise; beicon has no `to-promise`), `await-response` (subscribe→push→await, atomic), `settle`, `wait-for`, `wait-for-store`, `observe` (stream → termination promise; asserts provided, timeout rejects). +- `wait-for` waits on a **signal**, not on turns: `:signal` (an observable) re-checks the predicate on every emission and resolves on the event that changes the state; `:timeout-ms` (default 5000) only bounds failure then. Without a signal it polls against a real-time deadline — 50 turns were ~54 ms idle, and turns are not a unit of time. `wait-for-store` passes `ptk/input-stream` as the signal (the store notifies after applying the update, so the predicate sees the new state); the persistence fixture exposes `requests$` for `(= N (count @requests))`. A wait that resolves on an event consumes no turn. The waits with nothing to listen to stay on the poll: test atoms filled outside the store (`@cycles`, `@reports`, `@done`), `@failures`, and predicates over JS objects or closures. - Fixtures return promises (`with-watchdog`, `with-persistence`); `await` them from `^:async` tests. `main_errors` and `fonts` are fully migrated (no legacy `with-mocks` left). - Valid `^:async` placements: `mem:clojure/idioms`. @@ -37,8 +38,11 @@ To assert over emitted event sequences, observe termination: subscribe through ` - `st/emit!` doubles collect heterogeneous events: audit `DataEvent`s deref, toast reify-objects do not — discriminate with `ptk/type` (total, never throws), never blind `deref`. - Subscription order decides delivery order: never resolve settlement from a pre-subscribed branch racing the pipeline. - Auto-answering mocks lose deadline expressiveness (can't time answers), need teardown timer-cancellation, and post-teardown deliveries hit real implementations — explicit pushes + async delivery + `wait-for` won on every axis. +- A wait that observes an effect the test asserts takes a signal. A wait that means "the side effect advanced before the next emit" has no event to listen to: keep the poll (or an explicit `settle`) and say so, because a signal would resolve inside the same `emit!` and the next step would overtake the pipeline (`persistence-rollback-test` drove its four emits this way). +- With `rp/cmd!` mocked, a store-state wait can resolve before the flow that changes it ends, and the mock scope then closes in flight: the next call hits the real HTTP layer (malformed URL from a nil `cf/public-uri`) and the error is reported on a later test. `unlink-file-from-library` dissocs the library before its async chain, so the only proof the chain ran inside the mock is its second RPC call; wait for that, through the mock. - Teardown belongs to the terminal continuation, never to `finally`-around-triggers (it would dispose in-flight flows). - A body that awaits must be `(^:async fn …)` even if the rest is sync; sync sequences are atomic vs the event loop. +- `app.main.errors` prints every handled cause with `ex/print-throwable` before notifying, and the app logger does not gate it. A test that runs a real handler end to end stubs that var (`main-errors-test/quiet-throwable`): without it, the 10 000-error governor test alone wrote 190 k of the 192 k lines of a run log (13,8 MB), and GitHub truncates a step log past ~4 MB, cutting the summary and any failing assertion. Same for a production `console.error` a test provokes on purpose: `code-gen-markup-svg-wasm-test` silences `(.-error js/console)` in a `try/finally` while two WASM panics go through `get-svg-markup` (2 blocks, 70 lines, and the exception dumps its own `data`/`arr`). - The `:esm` test build cannot load the CommonJS `sax` module behind `tubax`, so SVG parsing fails in tests (`dwm/valid-svg-string?` answers false, `dwm/svg->clj` errors): mock both with the parsed data. From `frontend/`: diff --git a/frontend/test/frontend_tests/data/dashboard_test.cljs b/frontend/test/frontend_tests/data/dashboard_test.cljs index 8bd965eca5..a6328acfdd 100644 --- a/frontend/test/frontend_tests/data/dashboard_test.cljs +++ b/frontend/test/frontend_tests/data/dashboard_test.cljs @@ -252,7 +252,7 @@ (t/is (= [[:wait #{file-id}] [:duplicate-file]] @calls)) (t/is (nil? (:notification @store)) "nothing is told before the copy exists") (rx/push! answer {:id (uuid/next)}) - (await (async/wait-for #(some? (:notification @store)) "the user is told")) + (await (async/wait-for-store store #(some? (:notification @store)) "the user is told")) (await (async/settle)) (t/is (= "translated:dashboard.duplicate-unsaved-changes" (get-in @store [:notification :content]))) @@ -276,7 +276,7 @@ (await (async/wait-for #(= 1 @sent) "the copy is requested")) (t/is (nil? (:notification @store))) (rx/push! answer {:id (uuid/next)}) - (await (async/wait-for #(some? (:notification @store)) "the user is told")) + (await (async/wait-for-store store #(some? (:notification @store)) "the user is told")) (t/is (= "translated:dashboard.success-duplicate-file" (get-in @store [:notification :content]))) (t/is (= :success (get-in @store [:notification :level]))))))) diff --git a/frontend/test/frontend_tests/data/exports_assets_test.cljs b/frontend/test/frontend_tests/data/exports_assets_test.cljs index 18277c2f37..53965531d5 100644 --- a/frontend/test/frontend_tests/data/exports_assets_test.cljs +++ b/frontend/test/frontend_tests/data/exports_assets_test.cljs @@ -300,7 +300,7 @@ wasm.api/export-target (mock/stub (constantly :too-large)) i18n/tr (mock/stub #(str "translated:" %))} (ptk/emit! store (de/request-simple-export {:export one})) - (await (async/wait-for #(some? (:notification @store)) "the toast shows")) + (await (async/wait-for-store store #(some? (:notification @store)) "the toast shows")) (t/is (empty? @cmd-calls) "no exporter RPC") (t/is (= "translated:errors.export-too-large" (get-in @store [:notification :content]))) @@ -418,14 +418,14 @@ (reset! downloaded [filename mtype uri]))) st/ongoing-tasks (atom #{})} (ptk/emit! store (de/request-multiple-export {:exports exports :name "page"})) - (await (async/wait-for #(= "running" (get-in @store [:export :status])) - "export is running")) + (await (async/wait-for-store store #(= "running" (get-in @store [:export :status])) + "export is running")) (rx/push! worker-bus {:type :progress :done 1 :total 2}) - (await (async/wait-for #(= 1 (get-in @store [:export :progress])) - "first progress lands")) + (await (async/wait-for-store store #(= 1 (get-in @store [:export :progress])) + "first progress lands")) (ptk/emit! store (de/cancel-export)) - (await (async/wait-for #(= "cancelled" (get-in @store [:export :status])) - "export is cancelled")) + (await (async/wait-for-store store #(= "cancelled" (get-in @store [:export :status])) + "export is cancelled")) (rx/push! worker-bus {:type :progress :done 2 :total 2}) (rx/push! worker-bus {:type :done :filename "page.zip" diff --git a/frontend/test/frontend_tests/data/persistence_lock_test.cljs b/frontend/test/frontend_tests/data/persistence_lock_test.cljs index 41ec6d38f9..7e908d9ab2 100644 --- a/frontend/test/frontend_tests/data/persistence_lock_test.cljs +++ b/frontend/test/frontend_tests/data/persistence_lock_test.cljs @@ -83,7 +83,7 @@ (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 #(plk/locked? @store) "the failed save locks the workspace")) + (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")] @@ -156,7 +156,7 @@ (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 #(plk/locked? @store) "this file's failed save locks the workspace")) + (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")) @@ -164,8 +164,8 @@ (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 #(= :saved (get-in @store [:persistence :status])) - "the undo is saved")) + (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]))) diff --git a/frontend/test/frontend_tests/data/persistence_retry_test.cljs b/frontend/test/frontend_tests/data/persistence_retry_test.cljs index 1f79811018..dcab8e593c 100644 --- a/frontend/test/frontend_tests/data/persistence_retry_test.cljs +++ b/frontend/test/frontend_tests/data/persistence_retry_test.cljs @@ -50,11 +50,11 @@ (mock/with-mocks* {rx/timer (mock/stub instant-backoff)} (ptk/emit! store (local-commit file-id) ::dps/force-persist) - (await (async/wait-for #(and (= :error (get-in @store [:persistence :status])) - (= :invalid-save-response - (get-in @store [:persistence :error :code])) - (= 1 (count (get-in @store [:persistence :queue])))) - "invalid response fails the save")) + (await (async/wait-for-store store #(and (= :error (get-in @store [:persistence :status])) + (= :invalid-save-response + (get-in @store [:persistence :error :code])) + (= 1 (count (get-in @store [:persistence :queue])))) + "invalid response fails the save")) (t/is (= :error (get-in @store [:persistence :status]))) (t/is (= :invalid-save-response (get-in @store [:persistence :error :code]))) (t/is (= 1 (count (get-in @store [:persistence :queue])))) @@ -97,9 +97,9 @@ (mock/with-mocks* {rx/timer (mock/stub instant-backoff)} (ptk/emit! store (local-commit file-id) ::dps/force-persist) - (await (async/wait-for #(and (= :saved (get-in @store [:persistence :status])) - (empty? (get-in @store [:persistence :queue]))) - "retry saves the file")) + (await (async/wait-for-store store #(and (= :saved (get-in @store [:persistence :status])) + (empty? (get-in @store [:persistence :queue]))) + "retry saves the file")) (t/is (= :saved (get-in @store [:persistence :status]))) (t/is (empty? (get-in @store [:persistence :queue]))) (t/is (= 2 (count @requests)) "failed send plus one retry") @@ -124,9 +124,9 @@ (mock/with-mocks* {rx/timer (mock/stub instant-backoff)} (ptk/emit! store (local-commit file-id) ::dps/force-persist) - (await (async/wait-for #(and (= :saved (get-in @store [:persistence :status])) - (empty? (get-in @store [:persistence :queue]))) - (str "a " status " is retried and the save lands"))) + (await (async/wait-for-store store #(and (= :saved (get-in @store [:persistence :status])) + (empty? (get-in @store [:persistence :queue]))) + (str "a " status " is retried and the save lands"))) (t/is (= 2 (count @requests)) "the proxy error plus one retry") (t/is (empty? @failures) "a retried proxy error never reaches the failure toast")))) (fn [_ _] @@ -158,9 +158,9 @@ (mock/with-mocks* {rx/timer (mock/stub instant-backoff)} (ptk/emit! store (local-commit file-id) ::dps/force-persist) - (await (async/wait-for #(and (= :saved (get-in @store [:persistence :status])) - (empty? (get-in @store [:persistence :queue]))) - (str "a busy " (:status response) " is retried and the save lands"))) + (await (async/wait-for-store store #(and (= :saved (get-in @store [:persistence :status])) + (empty? (get-in @store [:persistence :queue]))) + (str "a busy " (:status response) " is retried and the save lands"))) (t/is (= 2 (count @requests)) "the busy answer plus one retry") (t/is (empty? @failures) "a retried busy answer never reaches the failure toast")))) (fn [_ _] @@ -197,7 +197,7 @@ hints (atom [])] (await (with-persistence - (^:async fn [{:keys [file-id requests store]}] + (^:async fn [{:keys [file-id requests requests$ store]}] (await (mock/with-mocks* {rx/timer (mock/stub (fn [ms] @@ -209,13 +209,13 @@ (js/Promise.resolve nil)) i18n/tr (fn ([key] (str "translated:" key)) ([key & _] (str "translated:" key)))} (ptk/emit! store (local-commit file-id) ::dps/force-persist) - (await (async/wait-for #(= 1 (get-in @store [:persistence :attempts])) - "the first answer starts the retries")) + (await (async/wait-for-store store #(= 1 (get-in @store [:persistence :attempts])) + "the first answer starts the retries")) (t/is (= "translated:errors.save-retrying-busy" (get-in @store [:notification :content]))) (doseq [n [2 3 4]] (rx/push! timer-s :tick) - (await (async/wait-for #(= n (count @requests)) "the retry is sent"))) + (await (async/wait-for #(= n (count @requests)) "the retry is sent" {:signal requests$}))) (await (async/wait-for #(seq @hints) "the slow-cycle notice")) (t/is (= ["translated:errors.save-still-retrying-busy"] @hints)) (rx/end! timer-s)))) @@ -234,8 +234,8 @@ (mock/with-mocks* {rx/timer (mock/stub instant-backoff)} (ptk/emit! store (local-commit file-id) ::dps/force-persist) - (await (async/wait-for #(= :error (get-in @store [:persistence :status])) - "the save fails")) + (await (async/wait-for-store store #(= :error (get-in @store [:persistence :status])) + "the save fails")) (t/is (= :server-error (get-in @store [:persistence :error :cause-type]))) (t/is (= 1 (count @requests)) "no retry")))) (fn [_ _] @@ -251,14 +251,17 @@ (let [delays (atom [])] (await (with-persistence - (^:async fn [{:keys [file-id failures requests store]}] + (^:async fn [{:keys [file-id failures requests requests$ store]}] (await (mock/with-mocks* {rx/timer (mock/stub (fn [ms] (swap! delays conj ms) (instant-backoff ms)))} (ptk/emit! store (local-commit file-id) ::dps/force-persist) + ;; The predicate reads both the store and the request counter, so it + ;; passes the merged signal; the wrapper only provides the store one. (await (async/wait-for #(and (= :error (get-in @store [:persistence :status])) (= 4 (count @requests))) - "retries exhaust into terminal error")) + "retries exhaust into terminal error" + {:signal (rx/merge requests$ (ptk/input-stream store))})) (t/is (= :error (get-in @store [:persistence :status]))) (t/is (= :save-failed (get-in @store [:persistence :error :code]))) (t/is (= 1 (count (get-in @store [:persistence :queue])))) @@ -277,20 +280,20 @@ timer-s (rx/subject)] (await (with-persistence - (^:async fn [{:keys [file-id requests store]}] + (^:async fn [{:keys [file-id requests requests$ store]}] (await (mock/with-mocks* {rx/timer (mock/stub (fn [ms] (swap! delays conj ms) timer-s))} ;; Phase 1 — first send fails transiently; the retry pends on ;; the hand-fired timer. (ptk/emit! store (local-commit file-id) ::dps/force-persist) - (await (async/wait-for #(= :retrying (get-in @store [:persistence :status])) - "transient failure retries")) + (await (async/wait-for-store store #(= :retrying (get-in @store [:persistence :status])) + "transient failure retries")) (t/is (= 1 (count @requests))) ;; Phase 2 — the online signal resends the head under the live ;; episode (status stays :retrying), hanging in flight. (ptk/emit! store (#'dps/resume-on-online)) - (await (async/wait-for #(= 2 (count @requests)) "online resends")) + (await (async/wait-for #(= 2 (count @requests)) "online resends" {:signal requests$})) (t/is (= :retrying (get-in @store [:persistence :status]))) (t/is (= 1 (get-in @store [:persistence :attempts]))) ;; Phase 3 — the pending timer fires into the in-flight request: @@ -318,8 +321,8 @@ (mock/with-mocks* {rx/timer (mock/stub (fn [_] (rx/subject)))} (ptk/emit! store (local-commit file-id) ::dps/force-persist) - (await (async/wait-for #(= :retrying (get-in @store [:persistence :status])) - "transient failure retries")) + (await (async/wait-for-store store #(= :retrying (get-in @store [:persistence :status])) + "transient failure retries")) (t/is (= 1 (count @requests))) (ptk/emit! store (#'dps/persist-commit (peek (get-in @store [:persistence :queue])) @@ -348,13 +351,13 @@ ;; Phase 1 — first send fails transiently; the retry pends on ;; the hand-fired timer. (ptk/emit! store (local-commit file-id) ::dps/force-persist) - (await (async/wait-for #(= :retrying (get-in @store [:persistence :status])) - "transient failure retries")) + (await (async/wait-for-store store #(= :retrying (get-in @store [:persistence :status])) + "transient failure retries")) (t/is (= 1 (count @requests))) ;; Phase 2 — a new edit joins the queue without resending. (ptk/emit! store (local-commit file-id) ::dps/force-persist) - (await (async/wait-for #(= 2 (count (get-in @store [:persistence :queue]))) - "second edit is queued")) + (await (async/wait-for-store store #(= 2 (count (get-in @store [:persistence :queue]))) + "second edit is queued")) (await (async/settle)) (t/is (= 1 (count @requests)) "the edit does not resend the head") (t/is (= 1 (get-in @store [:persistence :attempts])) "no attempt is spent") @@ -362,9 +365,9 @@ (t/is (= :retrying (get-in @store [:persistence :status]))) ;; Phase 3 — the timer fires: the head saves, then the new edit. (rx/push! timer-s :tick) - (await (async/wait-for #(and (= :saved (get-in @store [:persistence :status])) - (empty? (get-in @store [:persistence :queue]))) - "both edits save")) + (await (async/wait-for-store store #(and (= :saved (get-in @store [:persistence :status])) + (empty? (get-in @store [:persistence :queue]))) + "both edits save")) (t/is (= 3 (count @requests)) "failed send, retry, then the new edit") (let [[first-id retry-id edit-id] (map (comp :commit-id second) @requests)] (t/is (= first-id retry-id) "the retry resends the head") @@ -391,17 +394,17 @@ {rx/timer (mock/stub (fn [_] timer-s)) i18n/tr (mock/stub #(str "translated:" %))} (ptk/emit! store (local-commit file-id) ::dps/force-persist) - (await (async/wait-for #(= 1 (get-in @store [:persistence :attempts])) - "first attempt fails into retrying")) + (await (async/wait-for-store store #(= 1 (get-in @store [:persistence :attempts])) + "first attempt fails into retrying")) (let [notice (get @store :notification)] (t/is (map? notice) "a single notice is visible, never stacked")) (t/is (= :persistence-reconnecting (get-in @store [:notification :tag]))) (t/is (= "translated:errors.save-retrying" (get-in @store [:notification :content]))) (rx/push! timer-s :tick) - (await (async/wait-for #(and (= :saved (get-in @store [:persistence :status])) - (nil? (get @store :notification))) - "save hides the notice")) + (await (async/wait-for-store store #(and (= :saved (get-in @store [:persistence :status])) + (nil? (get @store :notification))) + "save hides the notice")) (t/is (= :saved (get-in @store [:persistence :status]))) (t/is (nil? (get @store :notification)) "recovery is silent") (rx/end! timer-s)))) @@ -421,8 +424,8 @@ (mock/with-mocks* {rx/timer (mock/stub instant-backoff)} (ptk/emit! store (local-commit file-id) ::dps/force-persist) - (await (async/wait-for #(= :error (get-in @store [:persistence :status])) - "retries exhaust into terminal error")) + (await (async/wait-for-store store #(= :error (get-in @store [:persistence :status])) + "retries exhaust into terminal error")) (t/is (= :error (get-in @store [:persistence :status]))) (t/is (nil? (get @store :notification)) "the terminal path hides the reconnect notice")))) @@ -440,23 +443,23 @@ second-s (atom nil)] (await (with-persistence - (^:async fn [{:keys [file-id requests store]}] + (^:async fn [{:keys [file-id requests requests$ store]}] (await (mock/with-mocks* {rx/timer (mock/stub (fn [_] timer-s))} (ptk/emit! store (local-commit file-id) ::dps/force-persist) - (await (async/wait-for #(= :retrying (get-in @store [:persistence :status])) - "transient failure retries")) + (await (async/wait-for-store store #(= :retrying (get-in @store [:persistence :status])) + "transient failure retries")) (t/is (= 1 (count @requests))) (ptk/emit! store (#'dps/resume-on-online)) - (await (async/wait-for #(= 2 (count @requests)) "online resends")) + (await (async/wait-for #(= 2 (count @requests)) "online resends" {:signal requests$})) (t/is (= 2 (count @requests))) (t/is (= :retrying (get-in @store [:persistence :status]))) (t/is (= 1 (get-in @store [:persistence :attempts]))) (rx/push! @second-s {:revn 1}) - (await (async/wait-for #(and (= :saved (get-in @store [:persistence :status])) - (empty? (get-in @store [:persistence :queue]))) - "resumed save lands")) + (await (async/wait-for-store store #(and (= :saved (get-in @store [:persistence :status])) + (empty? (get-in @store [:persistence :queue]))) + "resumed save lands")) (t/is (= :saved (get-in @store [:persistence :status]))) (ptk/emit! store (#'dps/resume-on-online)) (await (async/settle)) @@ -485,20 +488,20 @@ timer-s (rx/subject)] (await (with-persistence - (^:async fn [{:keys [file-id requests store]}] + (^:async fn [{:keys [file-id requests requests$ store]}] (await (mock/with-mocks* {rx/timer (mock/stub (fn [_] timer-s))} ;; Phase 1 — first send fails transiently; the retry pends on ;; the hand-fired timer. (ptk/emit! store (local-commit file-id) ::dps/force-persist) - (await (async/wait-for #(= :retrying (get-in @store [:persistence :status])) - "transient failure retries")) + (await (async/wait-for-store store #(= :retrying (get-in @store [:persistence :status])) + "transient failure retries")) (t/is (= 1 (count @requests))) ;; Phase 2 — the retry timer fires and resends the head, ;; hanging in flight. (rx/push! timer-s :tick) - (await (async/wait-for #(= 2 (count @requests)) "retry resends")) + (await (async/wait-for #(= 2 (count @requests)) "retry resends" {:signal requests$})) (t/is (= :retrying (get-in @store [:persistence :status]))) ;; Phase 3 — online arrives mid-flight: silent, never a third send. (ptk/emit! store (#'dps/resume-on-online)) @@ -558,10 +561,10 @@ ;; Phase 2 — the save lands, resetting the stall clock; a new ;; edit stalls again, over a governor window later. (rx/push! response {:revn 1}) - (await (async/wait-for #(= :saved (get-in @store [:persistence :status])) - "first save lands")) + (await (async/wait-for-store store #(= :saved (get-in @store [:persistence :status])) + "first save lands")) (ptk/emit! store (local-commit file-id) ::dps/force-persist) - (await (async/wait-for #(= 2 (count @requests)) "second edit is sent")) + (await (async/wait-for #(= 2 (count @requests)) "second edit is sent" {:signal requests$})) (reset! clock (+ 300001 errors/report-window-ms 300001)) (rx/push! ticks :tick) (await (async/wait-for #(= 2 (count (audit-events @events))) @@ -594,9 +597,9 @@ (swap! timeouts conj ms) (timeout 0 with ob)))} (ptk/emit! store (local-commit file-id) ::dps/force-persist) - (await (async/wait-for #(and (= :saved (get-in @store [:persistence :status])) - (empty? (get-in @store [:persistence :queue]))) - "the retry saves the file")) + (await (async/wait-for-store store #(and (= :saved (get-in @store [:persistence :status])) + (empty? (get-in @store [:persistence :queue]))) + "the retry saves the file")) (t/is (= 2 (count @requests)) "hung send plus one retry") (t/is (apply = (map (comp :commit-id second) @requests)) "both sends carry the same commit id") @@ -612,12 +615,15 @@ (defn- slow-cycle-timer "Timer double: the backoff delays fire at once, and every slow cycle waits - on a subject collected in `cycles`, so the test fires them one by one." - [cycles] + on a subject collected in `cycles`, so the test fires them one by one. When + `cycles$` is given, each new subject is also emitted there, so a wait can + resolve on the cycle being armed instead of polling the atom." + [cycles & [cycles$]] (fn [ms] (if (= ms dps/slow-retry-delay-ms) (let [cycle-s (rx/subject)] (swap! cycles conj cycle-s) + (when (some? cycles$) (rx/push! cycles$ cycle-s)) cycle-s) (rx/of :tick)))) @@ -626,18 +632,22 @@ ;; again, and saves once the network is back. Proves: a session outlasting ;; the backoff still recovers on its own. (t/deftest ^:async a-spent-backoff-keeps-trying-on-a-slow-cycle - (let [calls (atom 0) - cycles (atom [])] + (let [calls (atom 0) + cycles (atom []) + cycles$ (rx/subject)] (await (with-persistence (^:async fn [{:keys [file-id failures requests store]}] (await (mock/with-mocks* - {rx/timer (mock/stub (slow-cycle-timer cycles))} + {rx/timer (mock/stub (slow-cycle-timer cycles cycles$))} (ptk/emit! store (local-commit file-id) ::dps/force-persist) + ;; The predicate reads both the store and the cycle counter, so it + ;; passes the merged signal; the wrapper only provides the store one. (await (async/wait-for #(and (= :error (get-in @store [:persistence :status])) (= 1 (count @cycles))) - "the spent backoff arms the slow cycle")) + "the spent backoff arms the slow cycle" + {:signal (rx/merge cycles$ (ptk/input-stream store))})) (t/is (= 4 (count @requests))) (t/is (= 1 (count @failures))) @@ -652,9 +662,9 @@ (ptk/emit! store #(assoc % :notification {:tag errors/persistence-failed-tag :level :error})) (rx/push! (last @cycles) :tick) - (await (async/wait-for #(and (= :saved (get-in @store [:persistence :status])) - (empty? (get-in @store [:persistence :queue]))) - "the slow attempt saves")) + (await (async/wait-for-store store #(and (= :saved (get-in @store [:persistence :status])) + (empty? (get-in @store [:persistence :queue]))) + "the slow attempt saves")) (t/is (= 6 (count @requests))) (t/is (apply = (map (comp :commit-id second) @requests)) "every attempt carries the same commit id") @@ -675,17 +685,20 @@ (t/deftest ^:async the-slow-cycle-keeps-the-workspace-locked-until-a-save-lands (let [calls (atom 0) cycles (atom []) + cycles$ (rx/subject) attempt-s (rx/subject)] (await (with-persistence (^:async fn [{:keys [file-id store]}] (await (mock/with-mocks* - {rx/timer (mock/stub (slow-cycle-timer cycles))} + {rx/timer (mock/stub (slow-cycle-timer cycles cycles$))} (ptk/emit! store (local-commit file-id) ::dps/force-persist) + ;; Same as above: the predicate also reads the cycle counter. (await (async/wait-for #(and (= 1 (count @cycles)) (some? (get-in @store [:workspace-global :persistence-lock]))) - "the spent backoff locks the workspace")) + "the spent backoff locks the workspace" + {:signal (rx/merge cycles$ (ptk/input-stream store))})) (t/is (true? (get-in @store [:workspace-global :read-only?]))) (t/is (dps/save-retrying? (:persistence @store))) @@ -697,8 +710,8 @@ "a slow attempt in flight keeps the lock") (rx/push! attempt-s {:revn 1}) - (await (async/wait-for #(nil? (get-in @store [:workspace-global :persistence-lock])) - "the landed save lifts the lock")) + (await (async/wait-for-store store #(nil? (get-in @store [:workspace-global :persistence-lock])) + "the landed save lifts the lock")) (t/is (false? (get-in @store [:workspace-global :read-only?])))))) (fn [_ _] (if (< (swap! calls inc) 5) @@ -725,9 +738,9 @@ (t/is (= 1 (count (get-in @store [:persistence :queue])))) (rx/push! (last @cycles) :tick) - (await (async/wait-for #(and (= :saved (get-in @store [:persistence :status])) - (empty? (get-in @store [:persistence :queue]))) - (str status " recovers on the slow cycle"))) + (await (async/wait-for-store store #(and (= :saved (get-in @store [:persistence :status])) + (empty? (get-in @store [:persistence :queue]))) + (str status " recovers on the slow cycle"))) (t/is (= 5 (count @requests))) (t/is (apply = (map (comp :commit-id second) @requests)) "the slow cycle saves the held commit")))) @@ -747,8 +760,8 @@ (mock/with-mocks* {rx/timer (mock/stub (slow-cycle-timer cycles))} (ptk/emit! store (local-commit file-id) ::dps/force-persist) - (await (async/wait-for #(= :error (get-in @store [:persistence :status])) - "the save fails")) + (await (async/wait-for-store store #(= :error (get-in @store [:persistence :status])) + "the save fails")) (await (async/settle)) (t/is (= 1 (count @requests))) (t/is (empty? @cycles))))) @@ -773,8 +786,8 @@ {rx/timer (mock/stub (slow-cycle-timer cycles)) ct/now (mock/stub #(ct/inst @clock))} (ptk/emit! store (local-commit file-id) ::dps/force-persist) - (await (async/wait-for #(= :error (get-in @store [:persistence :status])) - (str slow-count " slow failures spend the backoff"))) + (await (async/wait-for-store store #(= :error (get-in @store [:persistence :status])) + (str slow-count " slow failures spend the backoff"))) (await (async/settle)) (t/is (= (if armed? 4 slow-count) (count @requests)) "the backoff sends no attempt past the limit") @@ -784,8 +797,8 @@ (when-not armed? (ptk/emit! store (ptk/data-event :app.main.data.common/change-team-role)) - (await (async/wait-for #(empty? (get-in @store [:persistence :queue])) - "recovery releases the held edit")) + (await (async/wait-for-store store #(empty? (get-in @store [:persistence :queue])) + "recovery releases the held edit")) (t/is (= slow-count (count @requests)) "the released edit is not sent again"))))) (fn [_ _] (when (<= (swap! calls inc) slow-count) @@ -799,7 +812,7 @@ cycles (atom [])] (await (with-persistence - (^:async fn [{:keys [file-id requests store]}] + (^:async fn [{:keys [file-id requests requests$ store]}] (await (mock/with-mocks* {rx/timer (mock/stub (slow-cycle-timer cycles)) @@ -809,9 +822,9 @@ (reset! clock (+ dps/retry-give-up-ms 1000)) (rx/push! (last @cycles) :tick) - (await (async/wait-for #(= 5 (count @requests)) "the last slow attempt")) - (await (async/wait-for #(= :error (get-in @store [:persistence :status])) - "the attempt fails")) + (await (async/wait-for #(= 5 (count @requests)) "the last slow attempt" {:signal requests$})) + (await (async/wait-for-store store #(= :error (get-in @store [:persistence :status])) + "the attempt fails")) (await (async/settle)) (t/is (= 1 (count @cycles)) "no further cycle is armed") (t/is (= 1 (count (get-in @store [:persistence :queue])))) @@ -829,7 +842,7 @@ cycles (atom [])] (await (with-persistence - (^:async fn [{:keys [file-id requests store]}] + (^:async fn [{:keys [file-id requests requests$ store]}] (await (mock/with-mocks* {rx/timer (mock/stub (slow-cycle-timer cycles)) @@ -843,9 +856,9 @@ (t/is (dps/save-retrying? (:persistence @store))) (rx/push! (last @cycles) :tick) (await (async/wait-for #(= (+ 4 dps/max-slow-failures) (count @requests)) - "the last slow attempt is sent")) - (await (async/wait-for #(= :error (get-in @store [:persistence :status])) - "the last slow attempt fails")) + "the last slow attempt is sent" {:signal requests$})) + (await (async/wait-for-store store #(= :error (get-in @store [:persistence :status])) + "the last slow attempt fails")) (await (async/settle)) (t/is (= dps/max-slow-failures (count @cycles)) "no further cycle is armed") (t/is (nil? (get-in @store [:persistence :recovering]))) @@ -873,9 +886,9 @@ (await (async/wait-for #(= 1 (count @cycles)) "the slow cycle is armed")) (ptk/emit! store (ptk/data-event :app.main.data.workspace/finalize-workspace)) (ptk/emit! store (ptk/data-event :app.main.data.common/change-team-role)) - (await (async/wait-for #(and (= :saved (get-in @store [:persistence :status])) - (empty? (get-in @store [:persistence :queue]))) - "recovery saves the held edit")) + (await (async/wait-for-store store #(and (= :saved (get-in @store [:persistence :status])) + (empty? (get-in @store [:persistence :queue]))) + "recovery saves the held edit")) (t/is (= 5 (count @requests))) (t/is (apply = (map (comp :commit-id second) @requests)) "recovery sends the same commit, not a released queue")))) @@ -902,9 +915,9 @@ (ptk/emit! store #(assoc-in % [:permissions :can-edit] false) (ptk/data-event :app.main.data.common/change-team-role)) - (await (async/wait-for #(= :save-permission-denied - (get-in @store [:persistence :error :code])) - "the viewer cannot send the edit")) + (await (async/wait-for-store store #(= :save-permission-denied + (get-in @store [:persistence :error :code])) + "the viewer cannot send the edit")) (ptk/emit! store (ptk/data-event :app.main.data.common/change-team-role)) (await (async/settle)) (t/is (= :error (get-in @store [:persistence :status]))) @@ -935,8 +948,8 @@ (await (async/wait-for #(= 1 (count @timers)) "the failure schedules a retry")) (ptk/emit! store (ptk/data-event :app.main.data.workspace/finalize-workspace)) - (await (async/wait-for #(get-in @store [:persistence :suspended]) - "leaving the workspace suspends retries")) + (await (async/wait-for-store store #(get-in @store [:persistence :suspended]) + "leaving the workspace suspends retries")) (rx/push! (first @timers) :tick) (await (async/settle)) (t/is (= 1 (count @requests)) "the pending retry stays silent") @@ -944,9 +957,9 @@ (t/is (= 1 (count (get-in @store [:persistence :queue])))) (ptk/emit! store (ptk/data-event :app.main.data.workspace/workspace-initialized)) - (await (async/wait-for #(and (= :saved (get-in @store [:persistence :status])) - (empty? (get-in @store [:persistence :queue]))) - "opening a file saves the held edit")) + (await (async/wait-for-store store #(and (= :saved (get-in @store [:persistence :status])) + (empty? (get-in @store [:persistence :queue]))) + "opening a file saves the held edit")) (t/is (= 2 (count @requests))) (t/is (apply = (map (comp :commit-id second) @requests)) "the same commit is sent again") @@ -966,19 +979,19 @@ first-s (rx/subject)] (await (with-persistence - (^:async fn [{:keys [file-id requests store]}] + (^:async fn [{:keys [file-id requests requests$ store]}] (await (mock/with-mocks* {rx/timer (mock/stub (fn [ms] (swap! timers conj ms) (rx/subject)))} (ptk/emit! store (local-commit file-id) ::dps/force-persist) - (await (async/wait-for #(= 1 (count @requests)) "the edit is sent")) + (await (async/wait-for #(= 1 (count @requests)) "the edit is sent" {:signal requests$})) (ptk/emit! store (ptk/data-event :app.main.data.workspace/finalize-workspace)) - (await (async/wait-for #(get-in @store [:persistence :suspended]) - "leaving the workspace suspends retries")) + (await (async/wait-for-store store #(get-in @store [:persistence :suspended]) + "leaving the workspace suspends retries")) (rx/error! first-s (ex-info "offline" {:type :offline})) - (await (async/wait-for #(= :retrying (get-in @store [:persistence :status])) - "the failure keeps the edit queued")) + (await (async/wait-for-store store #(= :retrying (get-in @store [:persistence :status])) + "the failure keeps the edit queued")) (await (async/settle)) (t/is (empty? @timers) "no backoff is scheduled") @@ -987,9 +1000,9 @@ (t/is (= 1 (count @requests)) "the online event does not resend") (ptk/emit! store (ptk/data-event :app.main.data.workspace/workspace-initialized)) - (await (async/wait-for #(and (= :saved (get-in @store [:persistence :status])) - (empty? (get-in @store [:persistence :queue]))) - "opening a file saves the held edit")) + (await (async/wait-for-store store #(and (= :saved (get-in @store [:persistence :status])) + (empty? (get-in @store [:persistence :queue]))) + "opening a file saves the held edit")) (t/is (= 2 (count @requests)))))) (fn [_ _] (if (= 1 (swap! calls inc)) @@ -1006,27 +1019,27 @@ last-s (rx/subject)] (await (with-persistence - (^:async fn [{:keys [file-id requests store]}] + (^:async fn [{:keys [file-id requests requests$ store]}] (await (mock/with-mocks* {rx/timer (mock/stub (slow-cycle-timer cycles))} (ptk/emit! store (local-commit file-id) ::dps/force-persist) - (await (async/wait-for #(= 4 (count @requests)) "the last retry is sent")) + (await (async/wait-for #(= 4 (count @requests)) "the last retry is sent" {:signal requests$})) (ptk/emit! store (ptk/data-event :app.main.data.workspace/finalize-workspace)) - (await (async/wait-for #(get-in @store [:persistence :suspended]) - "leaving the workspace suspends retries")) + (await (async/wait-for-store store #(get-in @store [:persistence :suspended]) + "leaving the workspace suspends retries")) (rx/error! last-s (ex-info "offline" {:type :offline})) - (await (async/wait-for #(= :error (get-in @store [:persistence :status])) - "the spent backoff halts the queue")) + (await (async/wait-for-store store #(= :error (get-in @store [:persistence :status])) + "the spent backoff halts the queue")) (await (async/settle)) (t/is (empty? @cycles) "no slow cycle is armed") (t/is (= 1 (count (get-in @store [:persistence :queue])))) (ptk/emit! store (ptk/data-event :app.main.data.workspace/workspace-initialized)) - (await (async/wait-for #(and (= :saved (get-in @store [:persistence :status])) - (empty? (get-in @store [:persistence :queue]))) - "opening a file saves the held edit")) + (await (async/wait-for-store store #(and (= :saved (get-in @store [:persistence :status])) + (empty? (get-in @store [:persistence :queue]))) + "opening a file saves the held edit")) (t/is (= 5 (count @requests)))))) (fn [_ _] (let [n (swap! calls inc)] diff --git a/frontend/test/frontend_tests/data/persistence_rollback_test.cljs b/frontend/test/frontend_tests/data/persistence_rollback_test.cljs index 3efff3cb85..69a971ce21 100644 --- a/frontend/test/frontend_tests/data/persistence_rollback_test.cljs +++ b/frontend/test/frontend_tests/data/persistence_rollback_test.cljs @@ -66,7 +66,7 @@ (t/deftest ^:async a-rejected-save-undoes-the-rejected-commit-and-every-later-one (await (thp/with-persistence - (^:async fn [{:keys [file-id response requests store failures]}] + (^:async fn [{:keys [file-id response requests requests$ store failures]}] (let [page-id (uuid/next) shape-id (uuid/next) other-id (uuid/next) @@ -84,8 +84,11 @@ :index 3}) (assoc-in [:workspace-local :selected] #{shape-id}))) (ptk/emit! store commit1 commit2 ::dps/force-persist) - (await (async/wait-for #(= 1 (count @requests)) "the first two commits are sent")) + (await (async/wait-for #(= 1 (count @requests)) "the first two commits are sent" {:signal requests$})) (ptk/emit! store commit3 other ::dps/force-persist) + ;; This test drives the pipeline step by step: these two waits are the + ;; pause that lets the persistence side effect advance before the next + ;; emit, not an effect to observe, so they keep the poll. (await (async/wait-for #(= 3 (count (get-in @store [:persistence :queue]))) "the third commit and the other file's edit are queued")) (ptk/emit! store commit4) @@ -93,7 +96,7 @@ "the fourth commit is buffered")) (rx/push! response (thp/rejection (:id @commit2))) - (await (async/wait-for #(= 2 (count @requests)) "the other file's edit is sent next")) + (await (async/wait-for #(= 2 (count @requests)) "the other file's edit is sent next" {:signal requests$})) (t/is (= "B" (thp/shape-name store file-id page-id shape-id)) "the file holds the commits the server kept") @@ -107,8 +110,8 @@ (t/is (= 1 (count @failures)) "the user is told once") (t/is (= other-id (get-in (second @requests) [1 :id]))) (rx/push! response {:revn 1}) - (await (async/wait-for #(= :saved (get-in @store [:persistence :status])) - "the queue drains"))))))) + (await (async/wait-for-store store #(= :saved (get-in @store [:persistence :status])) + "the queue drains"))))))) ;; Scenario: an edit is buffered while a save is in flight and the save comes ;; back rejected, so the rollback drops the edit; the 200 ms pending notice @@ -118,19 +121,19 @@ (t/deftest ^:async a-pending-notice-after-a-rollback-leaves-the-status-saved (await (thp/with-persistence - (^:async fn [{:keys [file-id response requests store]}] + (^:async fn [{:keys [file-id response requests requests$ store]}] (let [page-id (uuid/next) shape-id (uuid/next) commit1 (thp/rename-commit file-id page-id shape-id "A" "B") commit2 (thp/rename-commit file-id page-id shape-id "B" "C")] (ptk/emit! store (thp/open-file file-id page-id (thp/file-data page-id shape-id "C"))) (ptk/emit! store commit1 ::dps/force-persist) - (await (async/wait-for #(= 1 (count @requests)) "the first commit is sent")) + (await (async/wait-for #(= 1 (count @requests)) "the first commit is sent" {:signal requests$})) (ptk/emit! store commit2) (rx/push! response (thp/rejection (:id @commit1))) - (await (async/wait-for #(and (= :saved (get-in @store [:persistence :status])) - (empty? (get-in @store [:persistence :buffer]))) - "the rollback drops both commits")) + (await (async/wait-for-store store #(and (= :saved (get-in @store [:persistence :status])) + (empty? (get-in @store [:persistence :buffer]))) + "the rollback drops both commits")) ;; The notice the dropped edit scheduled (ptk/emit! store (#'dps/update-status :pending)) (await (async/settle)) @@ -145,24 +148,24 @@ (t/deftest ^:async a-rollback-keeps-a-peer-change-to-the-same-attribute (await (thp/with-persistence - (^:async fn [{:keys [file-id response requests store]}] + (^:async fn [{:keys [file-id response requests requests$ store]}] (let [page-id (uuid/next) shape-id (uuid/next) commit1 (thp/rename-commit file-id page-id shape-id "A" "B") commit2 (thp/attr-commit file-id page-id shape-id :opacity 0.5 0.9)] (ptk/emit! store (thp/open-file file-id page-id (thp/file-data page-id shape-id "B" :opacity 0.9))) (ptk/emit! store commit1 commit2) - (await (async/wait-for #(= 2 (count (get-in @store [:persistence :buffer]))) - "both edits are buffered")) + (await (async/wait-for-store store #(= 2 (count (get-in @store [:persistence :buffer]))) + "both edits are buffered")) (ptk/emit! store (thp/remote-commit file-id 1 [(thp/set-attr page-id shape-id :opacity 0.3)])) (await (async/settle)) (t/is (= 0.9 (thp/shape-attr store file-id page-id shape-id :opacity)) "the local edit stays on top of the peer's while pending") (ptk/emit! store ::dps/force-persist) - (await (async/wait-for #(= 1 (count @requests)) "the edits are sent")) + (await (async/wait-for #(= 1 (count @requests)) "the edits are sent" {:signal requests$})) (rx/push! response (thp/rejection (:id @commit2))) - (await (async/wait-for #(= :saved (get-in @store [:persistence :status])) - "the rollback settles")) + (await (async/wait-for-store store #(= :saved (get-in @store [:persistence :status])) + "the rollback settles")) (t/is (= 0.3 (thp/shape-attr store file-id page-id shape-id :opacity)) "the peer's opacity, as on the server") (t/is (= "B" (thp/shape-name store file-id page-id shape-id)) @@ -177,7 +180,7 @@ (t/deftest ^:async a-rollback-ignores-peer-changes-older-than-the-pending-commits (await (thp/with-persistence - (^:async fn [{:keys [file-id response requests store]}] + (^:async fn [{:keys [file-id response requests requests$ store]}] (let [page-id (uuid/next) shape-id (uuid/next) commit0 (thp/attr-commit file-id page-id shape-id :opacity 0.5 0.7) @@ -185,22 +188,22 @@ commit2 (thp/rename-commit file-id page-id shape-id "B" "C")] (ptk/emit! store (thp/open-file file-id page-id (thp/file-data page-id shape-id "A" :opacity 0.7))) (ptk/emit! store commit0) - (await (async/wait-for #(seq (get-in @store [:persistence :buffer])) "the edit is buffered")) + (await (async/wait-for-store store #(seq (get-in @store [:persistence :buffer])) "the edit is buffered")) (ptk/emit! store (thp/remote-commit file-id 1 [(thp/set-attr page-id shape-id :opacity 0.3)])) (ptk/emit! store ::dps/force-persist) - (await (async/wait-for #(= 1 (count @requests)) "the opacity edit is sent")) + (await (async/wait-for #(= 1 (count @requests)) "the opacity edit is sent" {:signal requests$})) (rx/push! response {:revn 1}) - (await (async/wait-for #(= :saved (get-in @store [:persistence :status])) - "the opacity edit is saved")) + (await (async/wait-for-store store #(= :saved (get-in @store [:persistence :status])) + "the opacity edit is saved")) (t/is (empty? (get-in @store [:persistence :remote-changes file-id])) "nothing pending, nothing kept") (ptk/emit! store #(assoc-in % [:files file-id :data :pages-index page-id :objects shape-id :name] "C") commit1 commit2 ::dps/force-persist) - (await (async/wait-for #(= 2 (count @requests)) "the renames are sent")) + (await (async/wait-for #(= 2 (count @requests)) "the renames are sent" {:signal requests$})) (rx/push! response (thp/rejection (:id @commit2))) - (await (async/wait-for #(= "B" (thp/shape-name store file-id page-id shape-id)) - "the rejected rename is undone")) + (await (async/wait-for-store store #(= "B" (thp/shape-name store file-id page-id shape-id)) + "the rejected rename is undone")) (t/is (= 0.7 (thp/shape-attr store file-id page-id shape-id :opacity)) "the saved opacity edit stays")))))) @@ -216,7 +219,7 @@ (t/deftest ^:async a-rejection-reverts-the-pending-commits-of-the-same-action-in-other-files (await (thp/with-persistence - (^:async fn [{:keys [file-id response requests store]}] + (^:async fn [{:keys [file-id response requests requests$ store]}] (let [lib-id (uuid/next) page-id (uuid/next) shape-id (uuid/next) @@ -231,9 +234,9 @@ (thp/file-data page-id shape-id "C") (thp/file-data page-id lshape-id "W"))) (ptk/emit! store commit1 commit2 before linked after ::dps/force-persist) - (await (async/wait-for #(= 1 (count @requests)) "this file's save is sent")) + (await (async/wait-for #(= 1 (count @requests)) "this file's save is sent" {:signal requests$})) (rx/push! response (thp/rejection (:id @commit2))) - (await (async/wait-for #(= 2 (count @requests)) "the library's save is sent next")) + (await (async/wait-for #(= 2 (count @requests)) "the library's save is sent next" {:signal requests$})) (t/is (= "B" (thp/shape-name store file-id page-id shape-id))) (t/is (= "Y" (thp/shape-name store lib-id page-id lshape-id)) "the library goes back to the edit made before the action") @@ -241,8 +244,8 @@ (t/is (= [[(thp/rename page-id lshape-id "Y")]] (sent-changes requests 1)) "only the library's edit made before the action is saved") (rx/push! response {:revn 1}) - (await (async/wait-for #(= :saved (get-in @store [:persistence :status])) - "the queue drains"))))))) + (await (async/wait-for-store store #(= :saved (get-in @store [:persistence :status])) + "the queue drains"))))))) ;; Scenario: one action makes a commit in a library and one in this file. ;; The library's is saved first; then this file's save comes back with the @@ -251,7 +254,7 @@ (t/deftest ^:async a-rejection-undoes-the-saved-commits-of-the-same-action-with-a-new-commit (await (thp/with-persistence - (^:async fn [{:keys [file-id response requests store]}] + (^:async fn [{:keys [file-id response requests requests$ store]}] (let [lib-id (uuid/next) page-id (uuid/next) shape-id (uuid/next) @@ -263,22 +266,22 @@ (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 #(= 1 (count @requests)) "the library's save is sent first")) + (await (async/wait-for #(= 1 (count @requests)) "the library's save is sent first" {:signal requests$})) (t/is (= lib-id (get-in (first @requests) [1 :id]))) (rx/push! response {:revn 1}) - (await (async/wait-for #(= 2 (count @requests)) "this file's save is sent")) + (await (async/wait-for #(= 2 (count @requests)) "this file's save is sent" {:signal requests$})) (rx/push! response (thp/rejection (:id @commit))) - (await (async/wait-for #(seq (get-in @store [:persistence :buffer])) - "a new commit undoes the library's")) + (await (async/wait-for-store store #(seq (get-in @store [:persistence :buffer])) + "a new commit undoes the library's")) (ptk/emit! store ::dps/force-persist) - (await (async/wait-for #(= 3 (count @requests)) "the new commit is sent")) + (await (async/wait-for #(= 3 (count @requests)) "the new commit is sent" {:signal requests$})) (t/is (= lib-id (get-in (nth @requests 2) [1 :id]))) (t/is (= [[(thp/rename page-id lshape-id "X")]] (sent-changes requests 2))) (t/is (= "A" (thp/shape-name store file-id page-id shape-id))) (t/is (= "X" (thp/shape-name store lib-id page-id lshape-id))) (rx/push! response {:revn 2}) - (await (async/wait-for #(= :saved (get-in @store [:persistence :status])) - "the queue drains"))))))) + (await (async/wait-for-store store #(= :saved (get-in @store [:persistence :status])) + "the queue drains"))))))) ;; Scenario: as above, while the user has an undo transaction open. Proves: ;; the undo commit gets its own group, not the transaction's, so a later @@ -286,7 +289,7 @@ (t/deftest ^:async the-undo-of-a-saved-commit-stays-out-of-an-open-transaction (await (thp/with-persistence - (^:async fn [{:keys [file-id response requests store]}] + (^:async fn [{:keys [file-id response requests requests$ store]}] (let [lib-id (uuid/next) page-id (uuid/next) shape-id (uuid/next) @@ -298,21 +301,21 @@ (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 #(= 1 (count @requests)) "the library's save is sent first")) + (await (async/wait-for #(= 1 (count @requests)) "the library's save is sent first" {:signal requests$})) (rx/push! response {:revn 1}) - (await (async/wait-for #(= 2 (count @requests)) "this file's save is sent")) + (await (async/wait-for #(= 2 (count @requests)) "this file's save is sent" {:signal requests$})) (ptk/emit! store (dwu/start-undo-transaction (js/Symbol) :timeout 0)) (let [tx-group (get-in @store [:workspace-undo :transaction :commit-group])] (rx/push! response (thp/rejection (:id @commit))) - (await (async/wait-for #(seq (get-in @store [:persistence :buffer])) - "a new commit undoes the library's")) + (await (async/wait-for-store store #(seq (get-in @store [:persistence :buffer])) + "a new commit undoes the library's")) (t/is (some? tx-group)) (t/is (not= tx-group (get-in @store [:persistence :buffer 0 :undo-group]))) (ptk/emit! store ::dps/force-persist) - (await (async/wait-for #(= 3 (count @requests)) "the new commit is sent")) + (await (async/wait-for #(= 3 (count @requests)) "the new commit is sent" {:signal requests$})) (rx/push! response {:revn 2}) - (await (async/wait-for #(= :saved (get-in @store [:persistence :status])) - "the queue drains")))))))) + (await (async/wait-for-store store #(= :saved (get-in @store [:persistence :status])) + "the queue drains")))))))) ;; Scenario: as above, but the library is no longer loaded when this file's ;; save comes back. Proves: the undo of the library's saved commit is still @@ -320,7 +323,7 @@ (t/deftest ^:async a-rejection-queues-the-undo-of-a-saved-commit-of-a-file-not-loaded (await (thp/with-persistence - (^:async fn [{:keys [file-id response requests store]}] + (^:async fn [{:keys [file-id response requests requests$ store]}] (let [lib-id (uuid/next) page-id (uuid/next) shape-id (uuid/next) @@ -332,20 +335,20 @@ (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 #(= 1 (count @requests)) "the library's save is sent first")) + (await (async/wait-for #(= 1 (count @requests)) "the library's save is sent first" {:signal requests$})) (rx/push! response {:revn 1}) - (await (async/wait-for #(= 2 (count @requests)) "this file's save is sent")) + (await (async/wait-for #(= 2 (count @requests)) "this file's save is sent" {:signal requests$})) (ptk/emit! store #(update % :files dissoc lib-id)) (rx/push! response (thp/rejection (:id @commit))) - (await (async/wait-for #(= 3 (count @requests)) "the undo is queued and sent")) + (await (async/wait-for #(= 3 (count @requests)) "the undo is queued and sent" {:signal requests$})) (t/is (= lib-id (get-in (nth @requests 2) [1 :id]))) (t/is (= [[(thp/rename page-id lshape-id "X")]] (sent-changes requests 2))) (t/is (empty? (get-in @store [:persistence :buffer]))) (t/is (nil? (get-in @store [:files lib-id])) "no local data is made up") (t/is (= "A" (thp/shape-name store file-id page-id shape-id))) (rx/push! response {:revn 2}) - (await (async/wait-for #(= :saved (get-in @store [:persistence :status])) - "the queue drains"))))))) + (await (async/wait-for-store store #(= :saved (get-in @store [:persistence :status])) + "the queue drains"))))))) ;; Scenario: one action makes two commits in this file and the save keeps ;; the first and rejects the second. Proves: the kept commit of the action @@ -353,7 +356,7 @@ (t/deftest ^:async a-rejection-undoes-the-kept-commits-of-the-same-action (await (thp/with-persistence - (^:async fn [{:keys [file-id response requests store]}] + (^:async fn [{:keys [file-id response requests requests$ store]}] (let [page-id (uuid/next) shape-id (uuid/next) group (uuid/next) @@ -361,18 +364,18 @@ commit2 (grouped (thp/attr-commit file-id page-id shape-id :opacity 1 0.5) group)] (ptk/emit! store (thp/open-file file-id page-id (thp/file-data page-id shape-id "B" :opacity 0.5))) (ptk/emit! store commit1 commit2 ::dps/force-persist) - (await (async/wait-for #(= 1 (count @requests)) "the action is sent")) + (await (async/wait-for #(= 1 (count @requests)) "the action is sent" {:signal requests$})) (rx/push! response (thp/rejection (:id @commit2))) - (await (async/wait-for #(seq (get-in @store [:persistence :buffer])) - "a new commit undoes the kept one")) + (await (async/wait-for-store store #(seq (get-in @store [:persistence :buffer])) + "a new commit undoes the kept one")) (ptk/emit! store ::dps/force-persist) - (await (async/wait-for #(= 2 (count @requests)) "the new commit is sent")) + (await (async/wait-for #(= 2 (count @requests)) "the new commit is sent" {:signal requests$})) (t/is (= [[(thp/rename page-id shape-id "A")]] (sent-changes requests 1))) (t/is (= "A" (thp/shape-name store file-id page-id shape-id))) (t/is (= 1 (thp/shape-attr store file-id page-id shape-id :opacity))) (rx/push! response {:revn 1}) - (await (async/wait-for #(= :saved (get-in @store [:persistence :status])) - "the queue drains"))))))) + (await (async/wait-for-store store #(= :saved (get-in @store [:persistence :status])) + "the queue drains"))))))) ;; Scenario: applying a token to five shapes makes three commits inside one ;; undo transaction, all sent in one save; the server keeps the first two @@ -382,7 +385,7 @@ (t/deftest ^:async a-rejection-reverts-every-commit-of-an-undo-transaction (await (thp/with-persistence - (^:async fn [{:keys [file-id response requests store]}] + (^:async fn [{:keys [file-id response requests requests$ store]}] (let [page-id (uuid/next) shape-ids (into [] (repeatedly 5 uuid/next)) shapes (map #(cts/setup-shape {:id % :type :rect :name "R" :opacity 1 @@ -416,14 +419,14 @@ (apply-to (subvec shape-ids 4 5)) (dwu/commit-undo-transaction tx-id) ::dps/force-persist) - (await (async/wait-for #(= 1 (count @requests)) "the action is sent")) + (await (async/wait-for #(= 1 (count @requests)) "the action is sent" {:signal requests$})) (let [groups (get-in (first @requests) [1 :changes-with-metadata])] (t/is (= 3 (count groups)) "the save carries the three commits") (rx/push! response (thp/rejection (:commit-id (nth groups 2))))) - (await (async/wait-for #(seq (get-in @store [:persistence :buffer])) - "a new commit undoes the kept ones")) + (await (async/wait-for-store store #(seq (get-in @store [:persistence :buffer])) + "a new commit undoes the kept ones")) (ptk/emit! store ::dps/force-persist) - (await (async/wait-for #(= 2 (count @requests)) "the new commit is sent")) + (await (async/wait-for #(= 2 (count @requests)) "the new commit is sent" {:signal requests$})) (doseq [id shape-ids] (t/is (= 1 (thp/shape-attr store file-id page-id id :opacity))) (t/is (nil? (thp/shape-attr store file-id page-id id :applied-tokens)))) @@ -436,8 +439,8 @@ (get-in (second @requests) [1 :changes-with-metadata]))) "the new commit puts the kept opacity back on the server") (rx/push! response {:revn 1}) - (await (async/wait-for #(= :saved (get-in @store [:persistence :status])) - "the queue drains"))))))) + (await (async/wait-for-store store #(= :saved (get-in @store [:persistence :status])) + "the queue drains"))))))) ;; UI state after a rollback. @@ -447,7 +450,7 @@ (t/deftest ^:async a-rollback-leaves-the-edition-of-a-removed-shape (await (thp/with-persistence - (^:async fn [{:keys [file-id response requests store]}] + (^:async fn [{:keys [file-id response requests requests$ store]}] (let [page-id (uuid/next) shape-id (uuid/next) text-id (uuid/next) @@ -458,10 +461,10 @@ (thp/add-shape page-id text))) #(assoc-in % [:workspace-local :edition] text-id)) (ptk/emit! store commit ::dps/force-persist) - (await (async/wait-for #(= 1 (count @requests)) "the new text is sent")) + (await (async/wait-for #(= 1 (count @requests)) "the new text is sent" {:signal requests$})) (rx/push! response (thp/rejection (:id @commit))) - (await (async/wait-for #(nil? (get-in @store [:workspace-local :edition])) - "the rollback leaves edition mode")) + (await (async/wait-for-store store #(nil? (get-in @store [:workspace-local :edition])) + "the rollback leaves edition mode")) (t/is (nil? (thp/shape-attr store file-id page-id text-id :id)))))))) ;; Scenario: the user edits the nodes of a saved path and the rejected commit @@ -472,7 +475,7 @@ (t/deftest ^:async a-rollback-leaves-the-edition-of-a-changed-path (await (thp/with-persistence - (^:async fn [{:keys [file-id response requests store]}] + (^:async fn [{:keys [file-id response requests requests$ store]}] (let [page-id (uuid/next) shape-id (uuid/next) commit (thp/rename-commit file-id page-id shape-id "A" "B")] @@ -485,10 +488,10 @@ (assoc-in [:workspace-drawing :object] {:id shape-id :content [:rejected]}))) (ptk/emit! store commit ::dps/force-persist) - (await (async/wait-for #(= 1 (count @requests)) "the edit is sent")) + (await (async/wait-for #(= 1 (count @requests)) "the edit is sent" {:signal requests$})) (rx/push! response (thp/rejection (:id @commit))) - (await (async/wait-for #(nil? (get-in @store [:workspace-local :edition])) - "the rollback leaves edition mode")) + (await (async/wait-for-store store #(nil? (get-in @store [:workspace-local :edition])) + "the rollback leaves edition mode")) (t/is (= "A" (thp/shape-name store file-id page-id shape-id))) (t/is (nil? (get-in @store [:workspace-local :edit-path shape-id])) "the editor's state is dropped") @@ -502,7 +505,7 @@ (t/deftest ^:async a-rollback-keeps-the-edition-of-an-unchanged-shape (await (thp/with-persistence - (^:async fn [{:keys [file-id response requests store]}] + (^:async fn [{:keys [file-id response requests requests$ store]}] (let [page-id (uuid/next) shape-id (uuid/next) other-id (uuid/next) @@ -514,10 +517,10 @@ (thp/add-shape page-id other))) #(assoc-in % [:workspace-local :edition] other-id)) (ptk/emit! store commit ::dps/force-persist) - (await (async/wait-for #(= 1 (count @requests)) "the edit is sent")) + (await (async/wait-for #(= 1 (count @requests)) "the edit is sent" {:signal requests$})) (rx/push! response (thp/rejection (:id @commit))) - (await (async/wait-for #(= "A" (thp/shape-name store file-id page-id shape-id)) - "the rollback lands")) + (await (async/wait-for-store store #(= "A" (thp/shape-name store file-id page-id shape-id)) + "the rollback lands")) (await (async/settle)) (t/is (= other-id (get-in @store [:workspace-local :edition])))))))) @@ -527,7 +530,7 @@ (t/deftest ^:async a-rollback-closes-the-color-picker-of-a-removed-shape (await (thp/with-persistence - (^:async fn [{:keys [file-id response requests store]}] + (^:async fn [{:keys [file-id response requests requests$ store]}] (let [page-id (uuid/next) shape-id (uuid/next) rect-id (uuid/next) @@ -540,10 +543,10 @@ (assoc-in [:workspace-local :selected] #{rect-id}) (assoc ::modal/modal {:id (uuid/next) :type :colorpicker :props {}}))) (ptk/emit! store commit ::dps/force-persist) - (await (async/wait-for #(= 1 (count @requests)) "the new rectangle is sent")) + (await (async/wait-for #(= 1 (count @requests)) "the new rectangle is sent" {:signal requests$})) (rx/push! response (thp/rejection (:id @commit))) - (await (async/wait-for #(empty? (get-in @store [:workspace-local :selected])) - "the rollback deselects the removed shape")) + (await (async/wait-for-store store #(empty? (get-in @store [:workspace-local :selected])) + "the rollback deselects the removed shape")) (t/is (nil? (::modal/modal @store)) "the color picker is closed")))))) ;; Scenario: the user undoes a rename with Ctrl+Z and the server rejects the @@ -552,7 +555,7 @@ (t/deftest ^:async a-rejected-undo-puts-the-change-and-the-history-back (await (thp/with-persistence - (^:async fn [{:keys [file-id response requests store]}] + (^:async fn [{:keys [file-id response requests requests$ store]}] (let [page-id (uuid/next) shape-id (uuid/next) item (thp/undo-entry (thp/rename-commit file-id page-id shape-id "A" "B"))] @@ -560,19 +563,19 @@ (thp/open-file file-id page-id (thp/file-data page-id shape-id "B")) #(assoc % :workspace-undo {:items [item] :index 0})) (ptk/emit! store dwu/undo) - (await (async/wait-for #(= "A" (thp/shape-name store file-id page-id shape-id)) - "the rename is undone")) + (await (async/wait-for-store store #(= "A" (thp/shape-name store file-id page-id shape-id)) + "the rename is undone")) (t/is (= -1 (get-in @store [:workspace-undo :index]))) (ptk/emit! store ::dps/force-persist) - (await (async/wait-for #(= 1 (count @requests)) "the undo is sent")) + (await (async/wait-for #(= 1 (count @requests)) "the undo is sent" {:signal requests$})) (rx/push! response (thp/rejection (get-in (first @requests) [1 :changes-with-metadata 0 :commit-id]))) - (await (async/wait-for #(= "B" (thp/shape-name store file-id page-id shape-id)) - "the rename is back")) + (await (async/wait-for-store store #(= "B" (thp/shape-name store file-id page-id shape-id)) + "the rename is back")) (t/is (= [item] (get-in @store [:workspace-undo :items])) "the history stays") (t/is (= 0 (get-in @store [:workspace-undo :index])) "it points at the rename again") - (await (async/wait-for #(= :saved (get-in @store [:persistence :status])) - "the rollback settles"))))))) + (await (async/wait-for-store store #(= :saved (get-in @store [:persistence :status])) + "the rollback settles"))))))) ;; Version restores. @@ -598,7 +601,7 @@ (try (await (thp/with-persistence - (^:async fn [{:keys [file-id response requests store]}] + (^:async fn [{:keys [file-id response requests requests$ store]}] (let [page-id (uuid/next) shape-id (uuid/next) ;; A change type with no handler, so its undo throws @@ -614,7 +617,7 @@ (thp/shape-name store file-id page-id shape-id)]))))] (ptk/emit! store (thp/open-file file-id page-id (thp/file-data page-id shape-id "B"))) (ptk/emit! store commit ::dps/force-persist) - (await (async/wait-for #(= 1 (count @requests)) "the rename is sent")) + (await (async/wait-for #(= 1 (count @requests)) "the rename is sent" {:signal requests$})) (rx/push! response (thp/rejection (:id @commit))) (await (async/wait-for #(= 1 @reloads) "the rollback reloads the file")) (await (async/settle)) @@ -639,7 +642,7 @@ (t/deftest ^:async an-answer-from-before-a-restore-leaves-the-reloaded-file-alone (await (thp/with-persistence - (^:async fn [{:keys [file-id response requests store failures]}] + (^:async fn [{:keys [file-id response requests requests$ store failures]}] (let [page-id (uuid/next) shape-id (uuid/next) commit1 (thp/rename-commit file-id page-id shape-id "A" "B") @@ -649,10 +652,10 @@ (thp/open-file file-id page-id (thp/file-data page-id shape-id "D")) #(assoc-in % [:files file-id :vern] 0)) (ptk/emit! store commit1 commit2 ::dps/force-persist) - (await (async/wait-for #(= 1 (count @requests)) "the first two renames are sent")) + (await (async/wait-for #(= 1 (count @requests)) "the first two renames are sent" {:signal requests$})) (ptk/emit! store commit3 ::dps/force-persist) - (await (async/wait-for #(= 2 (count (get-in @store [:persistence :queue]))) - "the third rename is queued")) + (await (async/wait-for-store store #(= 2 (count (get-in @store [:persistence :queue]))) + "the third rename is queued")) (ptk/emit! store (restore-version file-id (thp/file-data page-id shape-id "Restored"))) (ptk/emit! store (thp/remote-commit file-id 1 [(thp/set-attr page-id shape-id :opacity 0.5)])) @@ -660,9 +663,9 @@ (t/is (= "Restored" (thp/shape-name store file-id page-id shape-id)) "a peer's change does not replay the renames") (rx/push! response (thp/rejection (:id @commit2))) - (await (async/wait-for #(and (= :saved (get-in @store [:persistence :status])) - (empty? (get-in @store [:persistence :queue]))) - "the queue drains")) + (await (async/wait-for-store store #(and (= :saved (get-in @store [:persistence :status])) + (empty? (get-in @store [:persistence :queue]))) + "the queue drains")) (t/is (= "Restored" (thp/shape-name store file-id page-id shape-id)) "the file keeps the restored version") (t/is (= 0.5 (thp/shape-attr store file-id page-id shape-id :opacity))) @@ -677,20 +680,20 @@ (t/deftest ^:async a-save-refused-after-a-restore-is-dropped (await (thp/with-persistence - (^:async fn [{:keys [file-id response requests store failures]}] + (^:async fn [{:keys [file-id response requests requests$ store failures]}] (let [page-id (uuid/next) shape-id (uuid/next)] (ptk/emit! store (thp/open-file file-id page-id (thp/file-data page-id shape-id "B")) #(assoc-in % [:files file-id :vern] 0)) (ptk/emit! store (thp/rename-commit file-id page-id shape-id "A" "B") ::dps/force-persist) - (await (async/wait-for #(= 1 (count @requests)) "the rename is sent")) + (await (async/wait-for #(= 1 (count @requests)) "the rename is sent" {:signal requests$})) (ptk/emit! store (restore-version file-id (thp/file-data page-id shape-id "Restored"))) (rx/error! response (ex-info "A different version has been restored for the file." {:type :validation :code :vern-conflict})) - (await (async/wait-for #(and (= :saved (get-in @store [:persistence :status])) - (empty? (get-in @store [:persistence :queue]))) - "the refused save is dropped")) + (await (async/wait-for-store store #(and (= :saved (get-in @store [:persistence :status])) + (empty? (get-in @store [:persistence :queue]))) + "the refused save is dropped")) (t/is (empty? @failures)) (t/is (nil? (get-in @store [:workspace-global :persistence-lock]))) (t/is (= "Restored" (thp/shape-name store file-id page-id shape-id)))))))) @@ -783,7 +786,7 @@ [prepare answer] (let [flashes (atom [])] (-> (thp/with-persistence - (^:async fn [{:keys [file-id response requests store]}] + (^:async fn [{:keys [file-id response requests requests$ store]}] (await (mock/with-mocks* {errors/flash (fn [& {:as params}] @@ -795,7 +798,7 @@ commit1 (thp/rename-commit file-id page-id shape-id "A" "B")] (ptk/emit! store (thp/open-file file-id page-id (thp/file-data page-id shape-id "B"))) (ptk/emit! store commit1 ::dps/force-persist) - (await (async/wait-for #(= 1 (count @requests)) "the rename is sent")) + (await (async/wait-for #(= 1 (count @requests)) "the rename is sent" {:signal requests$})) (ptk/emit! store #(prepare % file-id)) (rx/push! response (answer (thp/rejection (:id @commit1)))) (await (async/wait-for #(seq @flashes) "the notice is flashed"))))))) diff --git a/frontend/test/frontend_tests/data/persistence_test.cljs b/frontend/test/frontend_tests/data/persistence_test.cljs index 544a41ee03..9f23fb317f 100644 --- a/frontend/test/frontend_tests/data/persistence_test.cljs +++ b/frontend/test/frontend_tests/data/persistence_test.cljs @@ -366,8 +366,8 @@ ;; Phase 1 — two commits share the first request; its landing starts the second. (reset! clock 290000) (rx/push! response {:revn 1}) - (await (async/wait-for #(= 1 (count (get-in @store [:persistence :queue]))) - "first save lands, second request starts")) + (await (async/wait-for-store store #(= 1 (count (get-in @store [:persistence :queue]))) + "first save lands, second request starts")) (reset! clock 300001) (rx/push! ticks :tick) (await (async/settle)) @@ -380,9 +380,9 @@ (t/is (= 1 (count @reports)) "The second request has now stalled") (rx/push! response {:revn 2}) - (await (async/wait-for #(and (= :saved (get-in @store [:persistence :status])) - (empty? (get-in @store [:persistence :queue]))) - "second save lands")) + (await (async/wait-for-store store #(and (= :saved (get-in @store [:persistence :status])) + (empty? (get-in @store [:persistence :queue]))) + "second save lands")) (t/is (= :saved (get-in @store [:persistence :status]))) ;; Phase 3 — answering saves the file: the tick stays silent. (reset! clock 1000000) @@ -471,7 +471,7 @@ timer-s (rx/subject)] (await (with-watchdog - (^:async fn [{:keys [clock ticks reports causes requests store file-id]}] + (^:async fn [{:keys [clock ticks reports causes requests requests$ store file-id]}] (await (mock/with-mocks* {rx/timer (mock/stub (fn [_] timer-s)) @@ -485,12 +485,12 @@ ;; Phase 1 — first send fails transiently; the episode parks ;; on the hand-fired timer. (ptk/emit! store (local-commit file-id) ::dps/force-persist) - (await (async/wait-for #(= :retrying (get-in @store [:persistence :status])) - "transient failure retries")) + (await (async/wait-for-store store #(= :retrying (get-in @store [:persistence :status])) + "transient failure retries")) (t/is (= 1 (count @requests))) ;; Phase 2 — the timer fires and the resend goes out, hanging. (rx/push! timer-s :tick) - (await (async/wait-for #(= 2 (count @requests)) "retry resends")) + (await (async/wait-for #(= 2 (count @requests)) "retry resends" {:signal requests$})) (t/is (= :retrying (get-in @store [:persistence :status]))) ;; Phase 3 — past the deadline the watchdog reports the stall ;; once, keeping the queued edit. @@ -537,9 +537,9 @@ (ptk/emit! store (local-commit file-id) #(assoc-in % [:permissions :can-edit] false) ::dps/force-persist) - (await (async/wait-for #(and (= :error (get-in @store [:persistence :status])) - (= 1 (count (get-in @store [:persistence :queue])))) - "permission loss fails the save")) + (await (async/wait-for-store store #(and (= :error (get-in @store [:persistence :status])) + (= 1 (count (get-in @store [:persistence :queue])))) + "permission loss fails the save")) (t/is (= :error (get-in @store [:persistence :status]))) (t/is (= 1 (count (get-in @store [:persistence :queue]))) "the undeliverable edit is retained, so waiters keep rejecting") @@ -557,18 +557,18 @@ (ptk/emit! store (local-commit file-id) #(assoc-in % [:permissions :can-edit] false) ::dps/force-persist) - (await (async/wait-for #(and (= :error (get-in @store [:persistence :status])) - (= 1 (count (get-in @store [:persistence :queue])))) - "the save fails terminally")) + (await (async/wait-for-store store #(and (= :error (get-in @store [:persistence :status])) + (= 1 (count (get-in @store [:persistence :queue])))) + "the save fails terminally")) ;; Edit rights come back: a team-role change is one of the two real ;; recovery triggers (the other is opening a file, whose event does ;; real work and is not usable as a bare signal here). (ptk/emit! store #(assoc-in % [:permissions :can-edit] true) (ptk/data-event :app.main.data.common/change-team-role)) - (await (async/wait-for #(and (= :saved (get-in @store [:persistence :status])) - (empty? (get-in @store [:persistence :queue]))) - "re-entering the file settles the queue")) + (await (async/wait-for-store store #(and (= :saved (get-in @store [:persistence :status])) + (empty? (get-in @store [:persistence :queue]))) + "re-entering the file settles the queue")) (t/is (nil? (get-in @store [:persistence :error])) "the halt is cleared once the user comes back") (t/is (empty? @requests) @@ -594,8 +594,8 @@ commit #(assoc-in % [:permissions :can-edit] false) ::dps/force-persist) - (await (async/wait-for #(= :error (get-in @store [:persistence :status])) - "lost permission fails the save")) + (await (async/wait-for-store store #(= :error (get-in @store [:persistence :status])) + "lost permission fails the save")) (ptk/emit! store (ptk/data-event :app.main.data.common/change-team-role)) (await (async/settle)) @@ -608,9 +608,9 @@ (ptk/emit! store #(assoc-in % [:permissions :can-edit] true) (ptk/data-event :app.main.data.common/change-team-role)) - (await (async/wait-for #(and (= :saved (get-in @store [:persistence :status])) - (empty? (get-in @store [:persistence :queue]))) - "edit rights back release the edit")) + (await (async/wait-for-store store #(and (= :saved (get-in @store [:persistence :status])) + (empty? (get-in @store [:persistence :queue]))) + "edit rights back release the edit")) (t/is (= "A" (thp/shape-name store file-id page-id shape-id)) "the file shows the server's data") (t/is (empty? (get-in @store [:workspace-undo :items])) @@ -636,20 +636,20 @@ (ptk/emit! store #(assoc-in % [:workspace-global :read-only?] true) (local-commit file-id) ::dps/force-persist) - (await (async/wait-for #(some? (get-in @store [:workspace-global :persistence-lock])) - "the failed save locks the workspace")) + (await (async/wait-for-store store #(some? (get-in @store [:workspace-global :persistence-lock])) + "the failed save locks the workspace")) (t/is (true? (get-in @store [:workspace-global :read-only?]))) (t/is (true? (get-in @store [:workspace-global :persistence-lock :read-only?]))) (ptk/emit! store (dwc/set-workspace-read-only false)) - (await (async/wait-for #(false? (get-in @store [:workspace-global :persistence-lock :read-only?])) - "the requested mode is kept for later")) + (await (async/wait-for-store store #(false? (get-in @store [:workspace-global :persistence-lock :read-only?])) + "the requested mode is kept for later")) (t/is (true? (get-in @store [:workspace-global :read-only?])) "the workspace stays read-only while saves fail") (ptk/emit! store (ptk/data-event :app.main.data.common/change-team-role)) - (await (async/wait-for #(nil? (get-in @store [:workspace-global :persistence-lock])) - "recovery lifts the lock")) + (await (async/wait-for-store store #(nil? (get-in @store [:workspace-global :persistence-lock])) + "recovery lifts the lock")) (t/is (false? (get-in @store [:workspace-global :read-only?])) "the workspace takes the mode the user asked for")) server-error))) @@ -665,8 +665,8 @@ (ptk/emit! store (local-commit file-id) #(assoc-in % [:permissions :can-edit] false) ::dps/force-persist) - (await (async/wait-for #(= :error (get-in @store [:persistence :status])) - "lost permission fails the save")) + (await (async/wait-for-store store #(= :error (get-in @store [:persistence :status])) + "lost permission fails the save")) (await (async/settle)) (t/is (nil? (get-in @store [:workspace-global :persistence-lock]))) (t/is (nil? (get-in @store [:workspace-global :read-only?]))) @@ -680,18 +680,18 @@ (with-persistence (^:async fn [{:keys [file-id store]}] (ptk/emit! store (local-commit file-id) ::dps/force-persist) - (await (async/wait-for #(some? (get-in @store [:workspace-global :persistence-lock])) - "the failed save locks the workspace")) + (await (async/wait-for-store store #(some? (get-in @store [:workspace-global :persistence-lock])) + "the failed save locks the workspace")) (ptk/emit! store (drw/context-lost) (ptk/data-event :app.main.data.common/change-team-role)) - (await (async/wait-for #(nil? (get-in @store [:workspace-global :persistence-lock])) - "recovery lifts the save lock")) + (await (async/wait-for-store store #(nil? (get-in @store [:workspace-global :persistence-lock])) + "recovery lifts the save lock")) (t/is (true? (get-in @store [:workspace-global :read-only?])) "the lost context keeps the workspace read-only") (ptk/emit! store (drw/context-restored)) - (await (async/wait-for #(false? (get-in @store [:workspace-global :read-only?])) - "the restored context brings back the first mode"))) + (await (async/wait-for-store store #(false? (get-in @store [:workspace-global :read-only?])) + "the restored context brings back the first mode"))) server-error))) ;; Scenario: two files have pending edits and the first send fails @@ -703,7 +703,7 @@ sends (atom 0)] (await (with-persistence - (^:async fn [{:keys [requests store]}] + (^:async fn [{:keys [requests requests$ store]}] (ptk/emit! store #(assoc % :files (into (:files %) (map (fn [id] [id {:id id :revn 0}])) @@ -711,17 +711,17 @@ (local-commit (nth files 0)) (local-commit (nth files 1)) ::dps/force-persist) - (await (async/wait-for #(seq @requests) "the first file is sent")) + (await (async/wait-for #(seq @requests) "the first file is sent" {:signal requests$})) (let [failed-file (get-in (second (first @requests)) [:id])] - (await (async/wait-for #(and (= :error (get-in @store [:persistence :status])) - (not (empty? (get-in @store [:persistence :queue])))) - "the terminal failure retains the queue")) + (await (async/wait-for-store store #(and (= :error (get-in @store [:persistence :status])) + (not (empty? (get-in @store [:persistence :queue])))) + "the terminal failure retains the queue")) ;; The user acts again: recovery releases the failed file and drains ;; the rest. (ptk/emit! store (ptk/data-event :app.main.data.common/change-team-role)) - (await (async/wait-for #(and (= :saved (get-in @store [:persistence :status])) - (empty? (get-in @store [:persistence :queue]))) - "the released file frees the rest of the queue")) + (await (async/wait-for-store store #(and (= :saved (get-in @store [:persistence :status])) + (empty? (get-in @store [:persistence :queue]))) + "the released file frees the rest of the queue")) (t/is (= 2 (count @requests)) "the other file was saved after the failed one was released") (t/is (= 1 (count (filter #(= failed-file (get-in (second %) [:id])) @requests))) @@ -747,8 +747,8 @@ i18n/tr (mock/stub #(str "translated:" %))} (ptk/emit! store (local-commit file-id) ::dps/force-persist) (.error response (ex-info "Raw transport details" {:type cause-type})) - (await (async/wait-for #(= :error (get-in @store [:persistence :status])) - "failed request errors the save")) + (await (async/wait-for-store store #(= :error (get-in @store [:persistence :status])) + "failed request errors the save")) (t/is (= :error (get-in @store [:persistence :status]))) (let [data (get-in @store [:persistence :error])] (ptk/handle-error (assoc data ::errors/instance (ex-info "Save failed" data)))) @@ -857,8 +857,8 @@ :index {} :run-id (uuid/next) :status :saving}) (dps/initialize-persistence)) - (await (async/wait-for #(= :error (get-in @store [:persistence :status])) - "dangling commit errors instead of skipping")) + (await (async/wait-for-store store #(= :error (get-in @store [:persistence :status])) + "dangling commit errors instead of skipping")) (t/is (= :error (get-in @store [:persistence :status]))) (t/is (= [id] (vec (get-in @store [:persistence :queue])))) (t/is (empty? @requests))))))) @@ -870,7 +870,7 @@ (t/deftest ^:async initialization-recovers-an-unsent-commit-with-a-dangling-run-id (await (with-persistence - (^:async fn [{:keys [file-id response requests store]}] + (^:async fn [{:keys [file-id response requests requests$ store]}] (let [commit (pcm/queue-entry [@(local-commit file-id)]) id (:id commit)] (ptk/emit! store @@ -878,12 +878,12 @@ :index {id commit} :run-id (uuid/next) :status :saving}) (dps/initialize-persistence)) - (await (async/wait-for #(= 1 (count @requests)) "dangling commit is sent")) + (await (async/wait-for #(= 1 (count @requests)) "dangling commit is sent" {:signal requests$})) (t/is (= 1 (count @requests))) (rx/push! response {:revn 1}) - (await (async/wait-for #(and (= :saved (get-in @store [:persistence :status])) - (empty? (get-in @store [:persistence :queue]))) - "recovered save lands")) + (await (async/wait-for-store store #(and (= :saved (get-in @store [:persistence :status])) + (empty? (get-in @store [:persistence :queue]))) + "recovered save lands")) (t/is (= :saved (get-in @store [:persistence :status]))) (t/is (empty? (get-in @store [:persistence :queue])))))))) @@ -892,15 +892,15 @@ reinitializes persistence: the pending request must not be sent again." [interrupt] (with-persistence - (^:async fn [{:keys [file-id response requests store]}] + (^:async fn [{:keys [file-id response requests requests$ store]}] (apply ptk/emit! store (local-commit file-id) ::dps/force-persist interrupt) (ptk/emit! store (dps/initialize-persistence)) - (await (async/wait-for #(= 1 (count @requests)) "only the active request is sent")) + (await (async/wait-for #(= 1 (count @requests)) "only the active request is sent" {:signal requests$})) (t/is (= 1 (count @requests))) (rx/push! response {:revn 1}) - (await (async/wait-for #(and (= :saved (get-in @store [:persistence :status])) - (empty? (get-in @store [:persistence :queue]))) - "active request completes")) + (await (async/wait-for-store store #(and (= :saved (get-in @store [:persistence :status])) + (empty? (get-in @store [:persistence :queue]))) + "active request completes")) (t/is (= :saved (get-in @store [:persistence :status]))) (t/is (empty? (get-in @store [:persistence :queue])))))) @@ -925,15 +925,15 @@ (ptk/emit! store (local-commit file-id) #(assoc-in % [:permissions :can-edit] false) ::dps/force-persist) - (await (async/wait-for #(and (= :error (get-in @store [:persistence :status])) - (= 1 (count (get-in @store [:persistence :queue])))) - "permission loss fails the save")) + (await (async/wait-for-store store #(and (= :error (get-in @store [:persistence :status])) + (= 1 (count (get-in @store [:persistence :queue])))) + "permission loss fails the save")) ;; Permission comes back and a file is opened again. (ptk/emit! store #(assoc-in % [:permissions :can-edit] true) (ptk/data-event :app.main.data.common/change-team-role)) - (await (async/wait-for #(and (= :saved (get-in @store [:persistence :status])) - (empty? (get-in @store [:persistence :queue]))) - "recovery releases the undeliverable edit")) + (await (async/wait-for-store store #(and (= :saved (get-in @store [:persistence :status])) + (empty? (get-in @store [:persistence :queue]))) + "recovery releases the undeliverable edit")) (t/is (empty? @requests) "the undeliverable edit is never replayed, not even once the permission is back") @@ -950,9 +950,9 @@ (^:async fn [{:keys [file-id response store]}] (ptk/emit! store (local-commit file-id) ::dps/force-persist) (.error response (ex-info "Validation failed" {:type :validation})) - (await (async/wait-for #(and (= :error (get-in @store [:persistence :status])) - (= 1 (count (get-in @store [:persistence :queue])))) - "the save fails terminally")) + (await (async/wait-for-store store #(and (= :error (get-in @store [:persistence :status])) + (= 1 (count (get-in @store [:persistence :queue])))) + "the save fails terminally")) (ptk/emit! store (dps/initialize-persistence)) (await (async/settle)) (t/is (= :error (get-in @store [:persistence :status])) @@ -971,13 +971,13 @@ (ptk/emit! store (local-commit file-id) ::dps/force-persist (ptk/data-event ::dps/error)) (rx/push! response {:revn 1}) - (await (async/wait-for #(= 1 (count (get-in @store [:persistence :queue]))) - "acknowledged commit stays queued")) + (await (async/wait-for-store store #(= 1 (count (get-in @store [:persistence :queue]))) + "acknowledged commit stays queued")) (t/is (= 1 (count (get-in @store [:persistence :queue])))) (ptk/emit! store (dps/initialize-persistence)) - (await (async/wait-for #(and (= :saved (get-in @store [:persistence :status])) - (empty? (get-in @store [:persistence :queue]))) - "recovery completes without resending")) + (await (async/wait-for-store store #(and (= :saved (get-in @store [:persistence :status])) + (empty? (get-in @store [:persistence :queue]))) + "recovery completes without resending")) (t/is (= 1 (count @requests)) "The acknowledged changes must not be sent again") (t/is (= :saved (get-in @store [:persistence :status]))) (t/is (empty? (get-in @store [:persistence :queue]))))))) @@ -989,19 +989,19 @@ (t/deftest ^:async recovery-sends-again-a-save-with-an-unknown-outcome (await (with-persistence - (^:async fn [{:keys [file-id response requests store]}] + (^:async fn [{:keys [file-id response requests requests$ store]}] (let [commit (assoc (pcm/queue-entry [@(local-commit file-id)]) ::dps/request-id (uuid/next)) id (:id commit)] (ptk/emit! store #(assoc % :persistence {:queue (conj #queue [] id) :index {id commit} :status :saving}) (dps/initialize-persistence)) - (await (async/wait-for #(= 1 (count @requests)) "the commit is sent again")) + (await (async/wait-for #(= 1 (count @requests)) "the commit is sent again" {:signal requests$})) (t/is (= id (:commit-id (second (first @requests))))) (rx/push! response {:revn 1}) - (await (async/wait-for #(and (= :saved (get-in @store [:persistence :status])) - (empty? (get-in @store [:persistence :queue]))) - "the resent commit saves")) + (await (async/wait-for-store store #(and (= :saved (get-in @store [:persistence :status])) + (empty? (get-in @store [:persistence :queue]))) + "the resent commit saves")) (t/is (= 1 (count @requests)))))))) ;; Scenario: an edit is queued before persistence even initializes. On @@ -1011,18 +1011,21 @@ (t/deftest ^:async initialization-flushes-buffered-edits-without-duplicating-them (await (with-persistence - (^:async fn [{:keys [file-id response requests store]}] + (^:async fn [{:keys [file-id response requests requests$ store]}] (ptk/emit! store (local-commit file-id) (dps/initialize-persistence) ::dps/force-persist) + ;; The predicate reads both the store and the request counter, so it + ;; passes the merged signal; the wrapper only provides the store one. (await (async/wait-for #(and (= 1 (count @requests)) (= 1 (count (get-in @store [:persistence :queue])))) - "buffered edit is sent once")) + "buffered edit is sent once" + {:signal (rx/merge requests$ (ptk/input-stream store))})) (t/is (= 1 (count @requests))) (t/is (= 1 (count (get-in @store [:persistence :queue])))) (rx/push! response {:revn 1}) - (await (async/wait-for #(= :saved (get-in @store [:persistence :status])) - "buffered edit saves")) + (await (async/wait-for-store store #(= :saved (get-in @store [:persistence :status])) + "buffered edit saves")) (t/is (= :saved (get-in @store [:persistence :status]))))))) ;; Scenario: the transport answers synchronously (immediate observable). @@ -1033,9 +1036,9 @@ (with-persistence (^:async fn [{:keys [file-id store]}] (ptk/emit! store (local-commit file-id) ::dps/force-persist) - (await (async/wait-for #(and (= :saved (get-in @store [:persistence :status])) - (empty? (get-in @store [:persistence :queue]))) - "synchronous save completes")) + (await (async/wait-for-store store #(and (= :saved (get-in @store [:persistence :status])) + (empty? (get-in @store [:persistence :queue]))) + "synchronous save completes")) (t/is (= :saved (get-in @store [:persistence :status]))) (t/is (empty? (get-in @store [:persistence :queue])))) (fn [_ _] (rx/of {:revn 1}))))) @@ -1049,12 +1052,12 @@ (t/deftest ^:async unsaved-edits-keep-the-leave-page-guard (await (with-persistence - (^:async fn [{:keys [file-id response requests store tasks]}] + (^:async fn [{:keys [file-id response requests requests$ store tasks]}] (ptk/emit! store (local-commit file-id)) (await (async/wait-for #(contains? @tasks :persistence) "a buffered edit turns the guard on")) (ptk/emit! store ::dps/force-persist) - (await (async/wait-for #(seq @requests) "the edit is sent")) + (await (async/wait-for #(seq @requests) "the edit is sent" {:signal requests$})) (t/is (contains? @tasks :persistence) "the guard stays on while the save is in flight") (rx/push! response {:revn 1}) (await (async/wait-for #(not (contains? @tasks :persistence)) @@ -1068,17 +1071,17 @@ (t/deftest ^:async edits-buffered-during-a-save-keep-the-leave-page-guard (await (with-persistence - (^:async fn [{:keys [file-id response requests store tasks]}] + (^:async fn [{:keys [file-id response requests requests$ store tasks]}] (ptk/emit! store (local-commit file-id) ::dps/force-persist) - (await (async/wait-for #(= 1 (count @requests)) "the first edit is sent")) + (await (async/wait-for #(= 1 (count @requests)) "the first edit is sent" {:signal requests$})) (ptk/emit! store (local-commit file-id)) (rx/push! response {:revn 1}) - (await (async/wait-for #(empty? (get-in @store [:persistence :queue])) - "the first save lands")) + (await (async/wait-for-store store #(empty? (get-in @store [:persistence :queue])) + "the first save lands")) (await (async/settle)) (t/is (contains? @tasks :persistence) "the buffered edit keeps the guard on") (ptk/emit! store ::dps/force-persist) - (await (async/wait-for #(= 2 (count @requests)) "the buffered edit is sent")) + (await (async/wait-for #(= 2 (count @requests)) "the buffered edit is sent" {:signal requests$})) (rx/push! response {:revn 2}) (await (async/wait-for #(not (contains? @tasks :persistence)) "the second save turns the guard off")) @@ -1095,15 +1098,15 @@ (ptk/emit! store (local-commit file-id) #(assoc-in % [:permissions :can-edit] false) ::dps/force-persist) - (await (async/wait-for #(= :error (get-in @store [:persistence :status])) - "the save fails terminally")) + (await (async/wait-for-store store #(= :error (get-in @store [:persistence :status])) + "the save fails terminally")) (await (async/settle)) (t/is (contains? @tasks :persistence) "the retained edit keeps the guard on") (ptk/emit! store #(assoc-in % [:permissions :can-edit] true) (ptk/data-event :app.main.data.common/change-team-role)) - (await (async/wait-for #(empty? (get-in @store [:persistence :queue])) - "recovery releases the edit")) + (await (async/wait-for-store store #(empty? (get-in @store [:persistence :queue])) + "recovery releases the edit")) (await (async/wait-for #(not (contains? @tasks :persistence)) "the release turns the guard off")) (t/is (= :saved (get-in @store [:persistence :status]))))))) @@ -1118,13 +1121,13 @@ {dps/leave-signals (fn [] leave-s)} (await (with-persistence - (^:async fn [{:keys [file-id requests store]}] + (^:async fn [{:keys [file-id requests requests$ store]}] (ptk/emit! store (local-commit file-id)) (await (async/settle)) (t/is (empty? @requests) "the edit waits in the buffer") (rx/push! leave-s :hidden) (await (async/wait-for #(= 1 (count @requests)) - "leaving sends the edit at once")) + "leaving sends the edit at once" {:signal requests$})) (t/is (= 1 (count @requests)))))))))) ;; Scenario: nothing is buffered when the user leaves. Proves: a leave @@ -1163,13 +1166,13 @@ (let [done (atom false)] (await (with-persistence - (^:async fn [{:keys [file-id response requests store]}] + (^:async fn [{:keys [file-id response requests requests$ store]}] (await (mock/with-mocks* {refs/persistence (l/derived :persistence store)} (ptk/emit! store (local-commit file-id)) (wait-files-saved store #{file-id} 60000 done) - (await (async/wait-for #(= 1 (count @requests)) "the edit is sent at once")) + (await (async/wait-for #(= 1 (count @requests)) "the edit is sent at once" {:signal requests$})) (await (async/settle)) (t/is (false? @done) "the wait holds while the save is in flight") (rx/push! response {:revn 1}) @@ -1182,12 +1185,12 @@ (let [done (atom false)] (await (with-persistence - (^:async fn [{:keys [file-id requests store]}] + (^:async fn [{:keys [file-id requests requests$ store]}] (await (mock/with-mocks* {refs/persistence (l/derived :persistence store)} (ptk/emit! store (local-commit file-id) ::dps/force-persist) - (await (async/wait-for #(= 1 (count @requests)) "the other file's save hangs")) + (await (async/wait-for #(= 1 (count @requests)) "the other file's save hangs" {:signal requests$})) (wait-files-saved store #{(uuid/next)} 60000 done) (await (async/wait-for #(true? @done) "the wait ends at once"))))))))) @@ -1226,8 +1229,8 @@ (^:async fn [{:keys [file-id store]}] (let [events (save-failed-events store)] (ptk/emit! store (local-commit file-id) ::dps/force-persist) - (await (async/wait-for #(= :error (get-in @store [:persistence :status])) - "the save fails")) + (await (async/wait-for-store store #(= :error (get-in @store [:persistence :status])) + "the save fails")) (await (async/wait-for #(seq @events) "the event is emitted")) (await (async/settle)) (t/is (= 1 (count @events))) @@ -1248,23 +1251,23 @@ (t/deftest ^:async buffered-commits-stay-in-the-store-until-queued (await (with-persistence - (^:async fn [{:keys [file-id response requests store]}] + (^:async fn [{:keys [file-id response requests requests$ store]}] (let [commit (local-commit file-id)] (ptk/emit! store commit) - (await (async/wait-for #(seq (get-in @store [:persistence :buffer])) - "the edit is buffered")) + (await (async/wait-for-store store #(seq (get-in @store [:persistence :buffer])) + "the edit is buffered")) (t/is (= [(:id @commit)] (mapv :id (get-in @store [:persistence :buffer])))) (t/is (empty? (get-in @store [:persistence :queue]))) (ptk/emit! store ::dps/force-persist) - (await (async/wait-for #(seq @requests) "the flush sends the edit")) + (await (async/wait-for #(seq @requests) "the flush sends the edit" {:signal requests$})) (t/is (empty? (get-in @store [:persistence :buffer]))) (t/is (= [(:id @commit)] (->> (get-in @store [:persistence :queue]) (mapcat #(get-in @store [:persistence :index % :commits])) (mapv :id)))) (rx/push! response {:revn 1}) - (await (async/wait-for #(= :saved (get-in @store [:persistence :status])) - "the save lands"))))))) + (await (async/wait-for-store store #(= :saved (get-in @store [:persistence :status])) + "the save lands"))))))) ;; Scenario: an edit waits in the buffer when another user's change to the ;; same attribute arrives. The server will apply the remote change first and @@ -1279,8 +1282,8 @@ local (thp/rename-commit file-id page-id shape-id "Original" "Local")] (ptk/emit! store (thp/open-file file-id page-id (thp/file-data page-id shape-id "Local"))) (ptk/emit! store local) - (await (async/wait-for #(seq (get-in @store [:persistence :buffer])) - "the local edit is buffered")) + (await (async/wait-for-store store #(seq (get-in @store [:persistence :buffer])) + "the local edit is buffered")) (ptk/emit! store (thp/remote-commit file-id 1 [(thp/rename page-id shape-id "Remote")])) (await (async/settle)) (t/is (= "Local" (thp/shape-name store file-id page-id shape-id)))))))) @@ -1299,8 +1302,8 @@ (thp/file-data page-id shape-id "A") (thp/file-data page-id shape-id "Y"))) (ptk/emit! store (thp/rename-commit lib-id page-id shape-id "X" "Y")) - (await (async/wait-for #(seq (get-in @store [:persistence :buffer])) - "the library edit is buffered")) + (await (async/wait-for-store store #(seq (get-in @store [:persistence :buffer])) + "the library edit is buffered")) (ptk/emit! store (thp/remote-commit file-id 1 [(thp/set-attr page-id shape-id :opacity 0.3)])) (await (async/settle)) (t/is (= 0.3 (thp/shape-attr store file-id page-id shape-id :opacity))) @@ -1362,7 +1365,7 @@ (t/deftest ^:async a-repair-answered-before-its-message-goes-under-the-saved-commit (await (with-persistence - (^:async fn [{:keys [file-id response requests store]}] + (^:async fn [{:keys [file-id response requests requests$ store]}] (let [session-id (uuid/next) page-id (uuid/next) shape-id (uuid/next) @@ -1372,18 +1375,18 @@ repair [(set-root-children page-id [shape-id])]] (open-with-new-child store file-id session-id page-id shape-id child) (ptk/emit! store commit ::dps/force-persist) - (await (async/wait-for #(= 1 (count @requests)) "the new child is sent")) + (await (async/wait-for #(= 1 (count @requests)) "the new child is sent" {:signal requests$})) (rx/push! response {:revn 0 :lagged [(lagged-row file-id 1 repair-session-id repair) (lagged-row file-id 2 session-id (:redo-changes @commit))]}) - (await (async/wait-for #(get-in @store [:persistence :index (:id @commit) ::dps/awaiting]) - "the saved commit waits for the repair")) + (await (async/wait-for-store store #(get-in @store [:persistence :index (:id @commit) ::dps/awaiting]) + "the saved commit waits for the repair")) (t/is (= [(:id @commit)] (vec (get-in @store [:persistence :queue])))) (ptk/emit! store (thp/remote-commit file-id 1 repair)) - (await (async/wait-for #(and (= :saved (get-in @store [:persistence :status])) - (empty? (get-in @store [:persistence :queue]))) - "the saved commit is acknowledged once the repair arrives")) + (await (async/wait-for-store store #(and (= :saved (get-in @store [:persistence :status])) + (empty? (get-in @store [:persistence :queue]))) + "the saved commit is acknowledged once the repair arrives")) (t/is (= [shape-id (:id child)] (root-children store file-id page-id)) "the repair goes under the new child, as on the server")))))) @@ -1392,7 +1395,7 @@ (t/deftest ^:async a-save-answer-does-not-wait-for-changes-already-received (await (with-persistence - (^:async fn [{:keys [file-id response requests store]}] + (^:async fn [{:keys [file-id response requests requests$ store]}] (let [session-id (uuid/next) page-id (uuid/next) shape-id (uuid/next) @@ -1402,14 +1405,14 @@ repair [(set-root-children page-id [shape-id])]] (open-with-new-child store file-id session-id page-id shape-id child) (ptk/emit! store commit ::dps/force-persist) - (await (async/wait-for #(= 1 (count @requests)) "the new child is sent")) + (await (async/wait-for #(= 1 (count @requests)) "the new child is sent" {:signal requests$})) (ptk/emit! store (thp/remote-commit file-id 1 repair)) (rx/push! response {:revn 0 :lagged [(lagged-row file-id 1 repair-session-id repair) (lagged-row file-id 2 session-id (:redo-changes @commit))]}) - (await (async/wait-for #(and (= :saved (get-in @store [:persistence :status])) - (empty? (get-in @store [:persistence :queue]))) - "the save lands at once")) + (await (async/wait-for-store store #(and (= :saved (get-in @store [:persistence :status])) + (empty? (get-in @store [:persistence :queue]))) + "the save lands at once")) (t/is (= [shape-id (:id child)] (root-children store file-id page-id)))))))) ;; Scenario: the save answer lists a repair whose message never reaches the @@ -1426,7 +1429,7 @@ (try (await (with-persistence - (^:async fn [{:keys [file-id response requests store]}] + (^:async fn [{:keys [file-id response requests requests$ store]}] (await (mock/with-mocks* {rx/timer (mock/stub (fn [ms] @@ -1441,13 +1444,13 @@ commit (add-child-commit file-id page-id child)] (open-with-new-child store file-id session-id page-id shape-id child) (ptk/emit! store commit ::dps/force-persist) - (await (async/wait-for #(= 1 (count @requests)) "the new child is sent")) + (await (async/wait-for #(= 1 (count @requests)) "the new child is sent" {:signal requests$})) (rx/push! response {:revn 0 :lagged [(lagged-row file-id 1 repair-session-id [(set-root-children page-id [shape-id])]) (lagged-row file-id 2 session-id (:redo-changes @commit))]}) - (await (async/wait-for #(get-in @store [:persistence :index (:id @commit) ::dps/awaiting]) - "the saved commit waits for the repair")) + (await (async/wait-for-store store #(get-in @store [:persistence :index (:id @commit) ::dps/awaiting]) + "the saved commit waits for the repair")) (t/is (zero? @reloads)) (rx/push! timeout-s :tick) (await (async/wait-for #(= 1 @reloads) "the file reloads")) @@ -1465,19 +1468,19 @@ (t/deftest ^:async a-save-sends-each-commit-as-its-own-group (await (with-persistence - (^:async fn [{:keys [file-id response requests store]}] + (^:async fn [{:keys [file-id response requests requests$ store]}] (let [commit1 (local-commit file-id) commit2 (local-commit file-id)] (ptk/emit! store commit1 commit2 ::dps/force-persist) - (await (async/wait-for #(= 1 (count @requests)) "both edits go in one save")) + (await (async/wait-for #(= 1 (count @requests)) "both edits go in one save" {:signal requests$})) (let [[_ params] (first @requests)] (t/is (not (contains? params :changes))) (t/is (= [{:commit-id (:id @commit1) :changes (:redo-changes @commit1)} {:commit-id (:id @commit2) :changes (:redo-changes @commit2)}] (:changes-with-metadata params)))) (rx/push! response {:revn 1}) - (await (async/wait-for #(= :saved (get-in @store [:persistence :status])) - "the save lands"))))))) + (await (async/wait-for-store store #(= :saved (get-in @store [:persistence :status])) + "the save lands"))))))) ;; Scenario: a save is in flight while a later edit waits in the buffer, and ;; something waits for the edits to be saved (as a library sync does before @@ -1487,20 +1490,20 @@ (t/deftest ^:async waiting-for-the-save-covers-the-buffered-edits (await (with-persistence - (^:async fn [{:keys [file-id response requests store]}] + (^:async fn [{:keys [file-id response requests requests$ store]}] (let [released? #(dps/terminal-status? (:persistence @store))] (ptk/emit! store (local-commit file-id) ::dps/force-persist) - (await (async/wait-for #(= 1 (count @requests)) "the first edit is sent")) + (await (async/wait-for #(= 1 (count @requests)) "the first edit is sent" {:signal requests$})) (ptk/emit! store (local-commit file-id)) - (await (async/wait-for #(seq (get-in @store [:persistence :buffer])) - "the second edit waits in the buffer")) + (await (async/wait-for-store store #(seq (get-in @store [:persistence :buffer])) + "the second edit waits in the buffer")) (rx/push! response {:revn 1}) - (await (async/wait-for #(and (= :saved (get-in @store [:persistence :status])) - (empty? (get-in @store [:persistence :queue]))) - "the first save lands")) + (await (async/wait-for-store store #(and (= :saved (get-in @store [:persistence :status])) + (empty? (get-in @store [:persistence :queue]))) + "the first save lands")) (t/is (seq (get-in @store [:persistence :buffer]))) (t/is (not (released?)) "the buffered edit is not saved yet") (ptk/emit! store ::dps/force-persist) - (await (async/wait-for #(= 2 (count @requests)) "the buffered edit is sent")) + (await (async/wait-for #(= 2 (count @requests)) "the buffered edit is sent" {:signal requests$})) (rx/push! response {:revn 2}) (await (async/wait-for released? "the waiters are released"))))))) diff --git a/frontend/test/frontend_tests/data/workspace_undo_test.cljs b/frontend/test/frontend_tests/data/workspace_undo_test.cljs index 1794b13a90..830f5c94e9 100644 --- a/frontend/test/frontend_tests/data/workspace_undo_test.cljs +++ b/frontend/test/frontend_tests/data/workspace_undo_test.cljs @@ -267,8 +267,8 @@ (ptk/event ::dw/reload-current-file)) (await (async/settle)) (ptk/emit! store (rename-page (cthf/current-page-id file) "Renamed")) - (await (async/wait-for #(seq (get-in @store [:workspace-undo :items])) - "the commit reaches the undo history")) + (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 diff --git a/frontend/test/frontend_tests/helpers/async.cljs b/frontend/test/frontend_tests/helpers/async.cljs index 1e52a49c25..1af96d9b27 100644 --- a/frontend/test/frontend_tests/helpers/async.cljs +++ b/frontend/test/frontend_tests/helpers/async.cljs @@ -2,7 +2,7 @@ ;; 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 INC Sucursal en España SL +;; Copyright (c) KALEIDOS SUBSIDIARY SL (ns frontend-tests.helpers.async "Async-first plumbing for ClojureScript tests. @@ -18,7 +18,8 @@ scheduled before it." (:require [beicon.v2.core :as rx] - [cljs.test :as t :include-macros true])) + [cljs.test :as t :include-macros true] + [potok.v2.core :as ptk])) (defn ->promise "Coerces a single-value observable into a js/Promise that resolves @@ -78,15 +79,111 @@ (when on-complete (on-complete)) (settle resolve nil)))))))) +(def ^:private default-timeout-ms + "Ceiling of a wait, in real milliseconds. With a signal it only bounds + the failure: a wait that resolves on an event never reaches it. Without + a signal it is the poll budget." + 5000) + +(defn- ^:async poll + [pred msg expired?] + (cond + (pred) nil + @expired? (t/is false (str "Timed out waiting for: " msg)) + :else (do (await (settle)) + (await (poll pred msg expired?))))) + +(defn- ^:async wait-poll + [pred msg timeout-ms] + (let [expired? (atom false) + timer (js/setTimeout #(reset! expired? true) timeout-ms)] + (await (poll pred msg expired?)) + (js/clearTimeout timer))) + +(defn- cause-text + "Readable, bounded text for a signal error: its message when it has one, + otherwise the printed value inside a `try` (a cyclic value would not print), + and never more than 500 characters." + [cause] + (let [text (or (ex-message cause) + (try + (pr-str cause) + (catch :default _ "#")))] + (if (> (count text) 500) + (str (subs text 0 500) "…") + text))) + +(defn- wait-signal + "Resolves when `pred` holds, re-checking it on every emission of `signal`. + + The timeout only bounds failure: it records the failure and resolves, so + the caller never hangs. A signal that ends or fails without the predicate + holding is reported with its own reason. The subscription is disposed on + every path." + [pred msg signal timeout-ms] + (js/Promise. + (fn [resolve] + (let [done? (atom false) + sub* (atom nil) + timer* (atom nil) + stop (fn [] + (when-let [s @sub*] (rx/dispose! s)) + (when-let [t @timer*] (js/clearTimeout t))) + fail (fn [text] + (when-not @done? + (reset! done? true) + (stop) + (t/is false text) + (resolve nil))) + finish (fn [] + (when-not @done? + (reset! done? true) + (stop) + (resolve nil)))] + (reset! timer* + (js/setTimeout #(fail (str "Timed out waiting for: " msg)) + timeout-ms)) + (reset! sub* + (rx/subs! + (fn [_] (when (pred) (finish))) + (fn [cause] + (if (pred) + (finish) + (fail (str "Signal failed while waiting for: " msg ": " + (cause-text cause))))) + (fn [] + (if (pred) + (finish) + (fail (str "Signal ended while waiting for: " msg)))) + signal)) + ;; A source that emits while subscribing already settled the wait. + (when @done? + (rx/dispose! @sub*)) + nil)))) + (defn ^:async wait-for - "Resolves once `pred` holds, checking immediately and then once per - macrotask (bounded: records a failure instead of hanging). Await it - before asserting effects of triggers — it holds whether the effect - lands synchronously or not." - [pred msg & [{:keys [max-ticks] :or {max-ticks 50}}]] - (if (pred) - nil - (if (<= max-ticks 0) - (t/is false (str "Timed out waiting for: " msg)) - (do (await (settle)) - (await (wait-for pred msg {:max-ticks (dec max-ticks)})))))) + "Resolves once `pred` holds, checking immediately and then re-checking it + on every emission of `:signal` (or once per macrotask when no signal is + given), up to `:timeout-ms` of real time (default 5000). + + With a signal the ceiling bounds failure only: the wait resolves on the + event that changes the state, not after a number of event-loop turns. + Without one it is a poll budget, and turns are not a unit of time (50 + turns are ~54 ms idle and much less ground under load). Records a single + failure instead of hanging: `Timed out waiting for: ` when the ceiling + expires, or the signal's own reason when it ends or fails (see + `wait-signal`)." + [pred msg & [{:keys [signal timeout-ms] + :or {timeout-ms default-timeout-ms}}]] + (when-not (pred) + (if (some? signal) + (await (wait-signal pred msg signal timeout-ms)) + (await (wait-poll pred msg timeout-ms))))) + +(defn wait-for-store + "Resolves once `pred` holds over `store`, re-checking it on every store + event. The store notifies its subscribers after applying the update, so the + predicate already sees the new state. One signal only: a predicate that also + reads another source passes a merged signal to `wait-for` itself." + [store pred msg] + (wait-for pred msg {:signal (ptk/input-stream store)})) diff --git a/frontend/test/frontend_tests/helpers/async_test.cljs b/frontend/test/frontend_tests/helpers/async_test.cljs new file mode 100644 index 0000000000..869fffdd58 --- /dev/null +++ b/frontend/test/frontend_tests/helpers/async_test.cljs @@ -0,0 +1,178 @@ +;; 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.helpers.async-test + "Contract of the wait primitives: a wait with a signal resolves on the + event that changes the state, not on an event-loop budget, and the + ceiling only bounds the failure." + (:require + [app.common.time :as ct] + [beicon.v2.core :as rx] + [cljs.test :as t :include-macros true] + [clojure.string :as str] + [frontend-tests.helpers.async :as async] + [frontend-tests.helpers.mock :as mock] + [potok.v2.core :as ptk])) + +;; The old budget: 50 event-loop turns. A delivery after it used to be lost. +(def ^:private delivery-after-ticks 100) + +(def ^:private reported + "Messages of the failures recorded in a throwaway test environment, so the + failure reasons can be asserted without failing the run." + (atom [])) + +(defmethod t/report [::silent :fail] + ;; The never-true case asserts that the helper records a failure: count it + ;; in the throwaway environment and keep the message, but do not print it. + [m] + (swap! reported conj (:message m)) + (t/inc-report-counter! :fail)) + +(defn- deliver-after! + "Calls `f` once the event loop has turned `n` times, one per macrotask." + [n f] + (if (pos? n) + (js/setTimeout #(deliver-after! (dec n) f) 0) + (f))) + +(defn- turn-probe + "Schedules one macrotask and returns `:ran?`, a reader for whether it + already ran, and `:stop`, which cancels it. A wait that polls always + consumes at least one macrotask, so a resolved probe still false proves the + wait did not poll." + [] + (let [ran (atom false) + id (js/setTimeout #(reset! ran true) 0)] + {:ran? (fn [] @ran) + :stop (fn [] (js/clearTimeout id))})) + +(defn- inc-counter + [] + (ptk/reify ::inc-counter + ptk/UpdateEvent + (update [_ state] + (update state :counter inc)))) + +(t/deftest ^:async a-signal-resolves-a-late-delivery + ;; The delivery lands after the old 50-turn budget. Without a signal the + ;; wait used to give up first; with one it resolves when the event arrives. + ;; Counting the predicate evaluations separates the two paths: a poll + ;; evaluates it once per turn, a signal exactly twice. + (let [ready (atom false) + evals (atom 0) + sig (rx/subject)] + (deliver-after! delivery-after-ticks + #(do (reset! ready true) + (rx/push! sig :ready))) + (await (async/wait-for #(do (swap! evals inc) (true? @ready)) + "the late delivery" + {:signal sig})) + (t/is (= 2 @evals) "one initial check and one signal emission") + (t/is (true? @ready)))) + +(t/deftest ^:async a-store-signal-resolves-after-the-update + ;; The store notifies its subscribers after applying the update, so the + ;; predicate is already true when the wait re-checks it. + (let [store (ptk/store {:state {:counter 0}}) + {:keys [ran? stop]} (turn-probe)] + (try + (let [done (async/wait-for-store store + #(= 1 (:counter @store)) + "the update")] + (ptk/emit! store (inc-counter)) + (await done) + (t/is (= 1 (:counter @store))) + (t/is (false? (ran?)) "resolved on the event, not on a poll")) + (finally + (stop) + (rx/dispose! store))))) + +(t/deftest ^:async a-late-predicate-without-signal + ;; Same late delivery, with nothing to listen to: the ceiling has to be + ;; time, not turns, for the wait to survive it. + (let [ready (atom false)] + (deliver-after! delivery-after-ticks #(reset! ready true)) + (await (async/wait-for #(true? @ready) "the late delivery")) + (t/is (true? @ready)))) + +(t/deftest ^:async a-never-true-predicate-records-one-failure + (let [failures (atom nil) + started (inst-ms (ct/now))] + (binding [t/*current-env* (t/empty-env ::silent)] + (await (async/wait-for (constantly false) "the impossible" {:timeout-ms 100})) + (reset! failures (get-in (t/get-current-env) [:report-counters :fail]))) + (t/is (= 1 @failures) "one failure, not one per turn") + ;; A safety net at 10x the 100 ms ceiling, so a hung wait fails instead of + ;; stalling the run; it does not measure precision. + (t/is (< (- (inst-ms (ct/now)) started) 1000) "bounded by the ceiling"))) + +(t/deftest ^:async a-frozen-clock-still-times-out-in-real-time + ;; The watchdog fixtures freeze `ct/now`: the ceiling must be real time, + ;; so a never-true wait still fails instead of hanging on a frozen clock. + (let [failures (atom nil) + started (js/Date.now)] + (binding [t/*current-env* (t/empty-env ::silent)] + (await (mock/with-mocks* + {ct/now (mock/stub (fn [] (ct/inst 0)))} + (await (async/wait-for (constantly false) + "the impossible on a frozen clock" + {:timeout-ms 100})))) + (reset! failures (get-in (t/get-current-env) [:report-counters :fail]))) + (t/is (= 1 @failures) "one failure, not one per turn") + (t/is (< (- (js/Date.now) started) 1000) "bounded by the ceiling"))) + +(t/deftest ^:async an-immediate-predicate-does-not-yield + (let [{:keys [ran? stop]} (turn-probe)] + (try + (await (async/wait-for (constantly true) "the immediate")) + (t/is (false? (ran?)) "an already-true predicate does not wait") + (finally + (stop))))) + +(t/deftest ^:async a-resolved-wait-disposes-its-signal + (let [evals (atom 0) + sig (rx/subject) + pred #(do (swap! evals inc) (>= @evals 2)) + done (async/wait-for pred "the second check" {:signal sig})] + (rx/push! sig :first) + (await done) + (let [before @evals] + (rx/push! sig :second) + (t/is (= before @evals) "a resolved wait stops listening")))) + +(t/deftest ^:async a-signal-that-ends-records-why + (reset! reported []) + (let [sig (rx/subject)] + (binding [t/*current-env* (t/empty-env ::silent)] + (let [done (async/wait-for (constantly false) "the closed signal" {:signal sig})] + (rx/end! sig) + (await done)))) + (t/is (= ["Signal ended while waiting for: the closed signal"] @reported) + "an ended signal is not reported as a timeout")) + +(t/deftest ^:async a-signal-that-errors-records-why + (reset! reported []) + (let [sig (rx/subject)] + (binding [t/*current-env* (t/empty-env ::silent)] + (let [done (async/wait-for (constantly false) "the broken signal" {:signal sig})] + (rx/error! sig (ex-info "boom" {:where :test})) + (await done)))) + (t/is (some #(str/includes? % "Signal failed while waiting for: the broken signal") + @reported) + "an errored signal is not reported as a timeout")) + +(t/deftest ^:async a-signal-error-bounds-a-long-cause + (reset! reported []) + (let [sig (rx/subject) + cause (apply str (repeat 600 "x"))] + (binding [t/*current-env* (t/empty-env ::silent)] + (let [done (async/wait-for (constantly false) "the long cause" {:signal sig})] + (rx/error! sig cause) + (await done)))) + (let [text (first @reported)] + (t/is (str/includes? text "Signal failed while waiting for: the long cause")) + (t/is (<= (count text) 600) "the cause text is bounded"))) diff --git a/frontend/test/frontend_tests/helpers/persistence.cljs b/frontend/test/frontend_tests/helpers/persistence.cljs index 70e3db744a..f7f4da7fbf 100644 --- a/frontend/test/frontend_tests/helpers/persistence.cljs +++ b/frontend/test/frontend_tests/helpers/persistence.cljs @@ -50,18 +50,21 @@ (`observe-on :async`): await each effect — via `wait-for` — before asserting it." [f & [respond]] - (let [file-id (uuid/next) - response (rx/subject) - failures (atom []) - requests (atom []) - tasks (atom #{}) - store (ptk/store {:state {:current-file-id file-id - :permissions {:can-edit true} - :files {file-id {:id file-id :revn 0}}} - :on-error #(t/is false (str %))})] + (let [file-id (uuid/next) + response (rx/subject) + failures (atom []) + requests (atom []) + requests$ (rx/subject) + tasks (atom #{}) + store (ptk/store {:state {:current-file-id file-id + :permissions {:can-edit true} + :files {file-id {:id file-id :revn 0}}} + :on-error #(t/is false (str %))})] (mock/with-mocks* {rp/cmd! (mock/stub (fn [cmd params] - (swap! requests conj [cmd params]) + (let [entry [cmd params]] + (swap! requests conj entry) + (rx/push! requests$ entry)) (if respond (respond cmd params) (->> response (rx/take 1) (rx/observe-on :async))))) @@ -73,9 +76,11 @@ (try (ptk/emit! store (dps/initialize-persistence)) (await (f {:file-id file-id :response response :failures failures - :requests requests :store store :tasks tasks})) + :requests requests :requests$ requests$ :store store + :tasks tasks})) (finally (rx/dispose! store) + (rx/end! requests$) (rx/end! response)))))) (defn set-attr diff --git a/frontend/test/frontend_tests/logic/comp_watcher_test.cljs b/frontend/test/frontend_tests/logic/comp_watcher_test.cljs index b9767480ba..51696f2397 100644 --- a/frontend/test/frontend_tests/logic/comp_watcher_test.cljs +++ b/frontend/test/frontend_tests/logic/comp_watcher_test.cljs @@ -87,8 +87,8 @@ #(assoc % :fills [{:fill-color "#ff0000" :fill-opacity 1}]))) - (await (async/wait-for #(empty? (copy-children store)) - "the copy to lose its rect")) + (await (async/wait-for-store store #(empty? (copy-children store)) + "the copy to lose its rect")) (let [file' (ths/get-file-from-state @store)] (t/is (= (cthi/id :outside) diff --git a/frontend/test/frontend_tests/logic/drag_on_save_failure_test.cljs b/frontend/test/frontend_tests/logic/drag_on_save_failure_test.cljs index 7ccf1c9d9c..fb336a2a9e 100644 --- a/frontend/test/frontend_tests/logic/drag_on_save_failure_test.cljs +++ b/frontend/test/frontend_tests/logic/drag_on_save_failure_test.cljs @@ -64,7 +64,7 @@ #(assoc-in % [:workspace-local :selected] (d/ordered-set (:id rect))) (start rect)) (rx/push! ms/mouse-position (gpt/point 160 260)) - (await (async/wait-for #(seq (:workspace-modifiers @store)) "the drag shows a preview")) + (await (async/wait-for-store store #(seq (:workspace-modifiers @store)) "the drag shows a preview")) (ptk/emit! store stop) (await (async/settle)) (finally @@ -147,8 +147,8 @@ (await (move store)) (ptk/emit! store stop) ;; A box drawing ends 100 ms after its stop - (await (async/wait-for #(nil? (get-in @store [:workspace-drawing :object])) - "the drawing ends" {:max-ticks 500})) + (await (async/wait-for-store store #(nil? (get-in @store [:workspace-drawing :object])) + "the drawing ends")) (await (async/settle)) (finally (rx/push! ms/mouse-position nil) @@ -158,21 +158,21 @@ (defn- ^:async drag-box [store] (rx/push! ms/mouse-position (gpt/point 160 180)) - (await (async/wait-for #(< 50 (get-in @store [:workspace-drawing :object :width] 0)) - "the box follows the pointer"))) + (await (async/wait-for-store store #(< 50 (get-in @store [:workspace-drawing :object :width] 0)) + "the box follows the pointer"))) (defn- ^:async drag-curve [store] (doseq [x [110 130 150 170]] (rx/push! ms/mouse-position (gpt/point x (+ x 20))) - (await (async/wait-for #(some (fn [p] (= x (:x p))) - (get-in @store [:workspace-drawing :object ::dwdc/points])) - "the curve takes the point")))) + (await (async/wait-for-store store #(some (fn [p] (= x (:x p))) + (get-in @store [:workspace-drawing :object ::dwdc/points])) + "the curve takes the point")))) (defn- ^:async click-path "Stands for the pen clicks: the drawn path gets two nodes." [store] - (await (async/wait-for #(some? (get-in @store [:workspace-drawing :object])) "the pen starts")) + (await (async/wait-for-store store #(some? (get-in @store [:workspace-drawing :object])) "the pen starts")) (ptk/emit! store #(assoc-in % [:workspace-drawing :object :content] (path/content [{:command :move-to :params {:x 100 :y 100}} {:command :line-to :params {:x 180 :y 160}}])))) diff --git a/frontend/test/frontend_tests/main_errors_test.cljs b/frontend/test/frontend_tests/main_errors_test.cljs index 4f59e70b95..e58317b322 100644 --- a/frontend/test/frontend_tests/main_errors_test.cljs +++ b/frontend/test/frontend_tests/main_errors_test.cljs @@ -23,6 +23,7 @@ - save failure notification – sticky toast carrying the error report - delegated save failures – causes handled by the handler for their type" (:require + [app.common.exceptions :as ex] [app.common.uuid :as uuid] [app.main.data.event :as ev] [app.main.data.persistence :as dps] @@ -43,6 +44,15 @@ [frontend-tests.helpers.mock :as mock] [potok.v2.core :as ptk])) +(defn- quiet-throwable + "Console stub for the tests that 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 floods the + run log, and one of them replays the same error thousands of times. No + test asserts the printed text, so the stub changes nothing but the noise." + [& _] + nil) + ;; --------------------------------------------------------------------------- ;; stale-asset-error? ;; --------------------------------------------------------------------------- @@ -247,7 +257,8 @@ cause (ex-info "http error" {:type type :hint "http error"})] (await (mock/with-mocks* - {st/emit! (mock/stub (fn [& emitted] (swap! events into emitted))) + {ex/print-throwable quiet-throwable + st/emit! (mock/stub (fn [& emitted] (swap! events into emitted))) rt/get-current-href (constantly "https://penpot.example.com/#/workspace") tm/schedule (mock/stub (fn [f] (f))) st/format-last-events (mock/stub (fn [& _] (throw (ex-info "must not be called" {}))))} @@ -303,7 +314,8 @@ cause (ex-info "http error" {:type :offline :hint "http error"})] (await (mock/with-mocks* - {st/emit! (mock/stub (fn [& emitted] (swap! events into emitted))) + {ex/print-throwable quiet-throwable + st/emit! (mock/stub (fn [& emitted] (swap! events into emitted))) rt/get-current-href (constantly "https://penpot.example.com/#/workspace") tm/schedule (mock/stub (fn [f] (mock/asap f))) st/format-last-events (mock/stub (fn [& _] (throw (ex-info "must not be called" {}))))} @@ -1060,7 +1072,8 @@ (t/testing "a version restored elsewhere reloads the file instead of notifying" (let [events (atom []) scheduled (atom [])] - (with-redefs [st/emit! (mock/stub (fn [& emitted] (swap! events into emitted))) + (with-redefs [ex/print-throwable quiet-throwable + st/emit! (mock/stub (fn [& emitted] (swap! events into emitted))) tm/schedule (mock/stub (fn [callback] (swap! scheduled conj callback)))] (errors/flash-persistence (ex-info "A different version has been restored" @@ -1138,7 +1151,8 @@ :uri "https://design.penpot.app/api/main/methods/get-teams"})] (await (mock/with-mocks* - {st/emit! (mock/stub (fn [& emitted] (swap! events into emitted))) + {ex/print-throwable quiet-throwable + st/emit! (mock/stub (fn [& emitted] (swap! events into emitted))) rt/get-current-href (constantly "https://penpot.example.com/#/workspace") rt/assign-exception (fn [error] (swap! assigned conj error) error) tm/schedule (mock/stub (fn [f] (f))) diff --git a/frontend/test/frontend_tests/plugins/creation_parent_test.cljs b/frontend/test/frontend_tests/plugins/creation_parent_test.cljs index 6fe12ebc82..7d94361d91 100644 --- a/frontend/test/frontend_tests/plugins/creation_parent_test.cljs +++ b/frontend/test/frontend_tests/plugins/creation_parent_test.cljs @@ -79,7 +79,7 @@ context ""))) id (obj/get created "$id")] - (await (async/wait-for #(some? (get-in @store (conj path id))) "created shape committed")) + (await (async/wait-for-store store #(some? (get-in @store (conj path id))) "created shape committed")) (let [created-shape (get-in @store (conj path id))] (t/is (= uuid/zero (:parent-id created-shape)) (str creation " / " layout)) (t/is (= uuid/zero (:frame-id created-shape)) (str creation " / " layout)) @@ -90,19 +90,19 @@ (t/is (= 1 (count (filter #{:create-file-media-object-from-url} (map first @uploads)))))) (ptk/emit! store dwu/undo) - (await (async/wait-for #(nil? (get-in @store (conj path id))) "creation undone")) + (await (async/wait-for-store store #(nil? (get-in @store (conj path id))) "creation undone")) (t/is (nil? (get-in @store (conj path id)))) (t/is (empty? (get-in @store (into path [(thi/id :board) :shapes])))) (ptk/emit! store dwu/redo) - (await (async/wait-for #(some? (get-in @store (conj path id))) "creation redone")) + (await (async/wait-for-store store #(some? (get-in @store (conj path id))) "creation redone")) (set! (.-x created) 1000) (set! (.-y created) 1200) - (await (async/wait-for #(= [1000 1200] ((juxt :x :y) (get-in @store (conj path id)))) "position updated")) + (await (async/wait-for-store store #(= [1000 1200] ((juxt :x :y) (get-in @store (conj path id)))) "position updated")) (t/is (= [1000 1200] ((juxt :x :y) (get-in @store (conj path id))))) (let [^js page (.-currentPage context) ^js board (.getShapeById page (str (thi/id :board)))] (.appendChild board created)) - (await (async/wait-for #(= (thi/id :board) (get-in @store (into path [id :parent-id]))) "explicit parent applied")) + (await (async/wait-for-store store #(= (thi/id :board) (get-in @store (into path [id :parent-id]))) "explicit parent applied")) (t/is (= (thi/id :board) (get-in @store (into path [id :parent-id])))))))))) (t/deftest ^:async interactive-creation-keeps-coordinate-based-parents @@ -143,16 +143,16 @@ position nil)}] (ptk/emit! store (get events creation))) (let [id (if (= creation :component) @id-ref svg-id)] - (await (async/wait-for #(some? (get-in @store (conj path id))) "interactive shape committed")) + (await (async/wait-for-store store #(some? (get-in @store (conj path id))) "interactive shape committed")) (t/is (= parent (get-in @store (into path [id :parent-id])))) (t/is (= parent (get-in @store (into path [id :frame-id])))) (t/is (= #{id} (set (get-in @store [:workspace-local :selected])))) (when (= creation :component) - (await (async/wait-for #(= :move (get-in @store [:workspace-local :transform])) "interactive move started")) + (await (async/wait-for-store store #(= :move (get-in @store [:workspace-local :transform])) "interactive move started")) (t/is (= :move (get-in @store [:workspace-local :transform]))) (rx/push! ms/mouse-position position) (await (async/settle)) (ptk/emit! store :interrupt) - (await (async/wait-for #(nil? (get-in @store [:workspace-local :transform])) "interactive move finished")) + (await (async/wait-for-store store #(nil? (get-in @store [:workspace-local :transform])) "interactive move finished")) (t/is (nil? (get-in @store [:workspace-local :transform]))) (t/is (= parent (get-in @store (into path [id :parent-id]))))))))))) diff --git a/frontend/test/frontend_tests/plugins/library_test.cljs b/frontend/test/frontend_tests/plugins/library_test.cljs index 1e798f7a78..9df7e4e5ff 100644 --- a/frontend/test/frontend_tests/plugins/library_test.cljs +++ b/frontend/test/frontend_tests/plugins/library_test.cljs @@ -77,8 +77,8 @@ component (.component copy)] (t/is (true? (get-in @store [:files (:id file) :data :components (thi/id :restorable) :deleted]))) (.restore component) - (await (async/wait-for #(not (get-in @store [:files (:id file) :data :components (thi/id :restorable) :deleted])) - "component restored")) + (await (async/wait-for-store store #(not (get-in @store [:files (:id file) :data :components (thi/id :restorable) :deleted])) + "component restored")) (.restore component) (await (async/settle)) (t/is (= (thi/id :main) diff --git a/frontend/test/frontend_tests/plugins/tokens_test.cljs b/frontend/test/frontend_tests/plugins/tokens_test.cljs index a8e5033cef..34c81dde12 100644 --- a/frontend/test/frontend_tests/plugins/tokens_test.cljs +++ b/frontend/test/frontend_tests/plugins/tokens_test.cljs @@ -154,8 +154,8 @@ (mock/with-mocks* {st/state store st/stream (ptk/input-stream store)} (t/is (= (str (:id file)) (.. context -tokensSource -id))) (set! (.-tokensSource context) source-proxy) - (await (async/wait-for #(= (:id source) (get-in @store [:files (:id file) :data :tokens-source])) - "library token source")) + (await (async/wait-for-store store #(= (:id source) (get-in @store [:files (:id file) :data :tokens-source])) + "library token source")) (t/is (= (str (:id source)) (.. context -tokensSource -id))) (t/is (= (datafy (ctht/get-tokens-status source)) (datafy (get-in @store [:files (:id file) :data :tokens-status])))) @@ -175,8 +175,8 @@ (await (mock/with-mocks* {st/state store st/stream (ptk/input-stream store)} (set! (.-tokensSource context) (.-local context)) - (await (async/wait-for #(nil? (get-in @store [:files (:id file) :data :tokens-source])) - "local token source")) + (await (async/wait-for-store store #(nil? (get-in @store [:files (:id file) :data :tokens-source])) + "local token source")) (let [^js catalog (.. context -local -tokens)] (t/is (= (str (:id file)) (.. context -tokensSource -id))) (t/is (true? (.-isEditableTokens catalog))) @@ -194,16 +194,16 @@ (mock/with-mocks* {st/state store st/stream (ptk/input-stream store)} (st/emit! (tohs/watch-undo-stack)) (set! (.-tokensSource context) source-proxy) - (await (async/wait-for #(seq (get-in @store [:workspace-undo :items])) - "token source undo entry")) + (await (async/wait-for-store store #(seq (get-in @store [:workspace-undo :items])) + "token source undo entry")) (st/emit! dwu/undo) - (await (async/wait-for #(nil? (get-in @store [:files (:id file) :data :tokens-source])) - "undo token source")) + (await (async/wait-for-store store #(nil? (get-in @store [:files (:id file) :data :tokens-source])) + "undo token source")) (t/is (= (datafy (ctht/get-tokens-status file)) (datafy (get-in @store [:files (:id file) :data :tokens-status])))) (st/emit! dwu/redo) - (await (async/wait-for #(= (:id source) (get-in @store [:files (:id file) :data :tokens-source])) - "redo token source")) + (await (async/wait-for-store store #(= (:id source) (get-in @store [:files (:id file) :data :tokens-source])) + "redo token source")) (t/is (= (datafy (ctht/get-tokens-status source)) (datafy (get-in @store [:files (:id file) :data :tokens-status])))))))) @@ -379,13 +379,13 @@ ^js theme (aget (.-themes catalog) 0)] (st/emit! (tohs/watch-undo-stack)) (.toggleActive theme) - (await (async/wait-for #(seq (get-in @store [:workspace-undo :items])) - "theme toggle undo entry")) + (await (async/wait-for-store store #(seq (get-in @store [:workspace-undo :items])) + "theme toggle undo entry")) (t/is (false? (.-active theme))) (st/emit! dwu/undo) - (await (async/wait-for #(= (datafy status) - (datafy (get-in @store [:files (:id file) :data :tokens-status]))) - "undo external theme toggle")) + (await (async/wait-for-store store #(= (datafy status) + (datafy (get-in @store [:files (:id file) :data :tokens-status]))) + "undo external theme toggle")) (t/is (tokens-unchanged? store source))))))) (t/deftest ^:async external-token-source-allows-applying-tokens-to-shapes @@ -413,10 +413,10 @@ (.applyToSelected token #js ["fill"]) (doseq [label [:rect1 :rect2 :rect3]] (let [shape-path [:files (:id file) :data :pages-index page-id :objects (cthi/id label)]] - (await (async/wait-for #(= "color.primary" (get-in @store (conj shape-path :applied-tokens :fill))) - (str "apply external token to " label))) - (await (async/wait-for #(= "#ff0000" (get-in @store (conj shape-path :fills 0 :fill-color))) - (str "resolve external token on " label))))) + (await (async/wait-for-store store #(= "color.primary" (get-in @store (conj shape-path :applied-tokens :fill))) + (str "apply external token to " label))) + (await (async/wait-for-store store #(= "#ff0000" (get-in @store (conj shape-path :fills 0 :fill-color))) + (str "resolve external token on " label))))) (t/is (= (ctht/get-tokens-lib file) (get-in @store [:files (:id file) :data :tokens-lib]))) (t/is (= (datafy (ctht/get-tokens-status file)) (datafy (get-in @store [:files (:id file) :data :tokens-status])))) @@ -728,9 +728,9 @@ ;; token and resolves to a uniform width, which the strokes setter accepts. (t/testing "the uniform strokeWidth alias is still accepted" (.applyToken ellipse token #js ["strokeWidth"]) - (await (async/wait-for - #(= "width.sm" (get-in @store (conj path :stroke-width-top))) - "uniform stroke width token applied")) + (await (async/wait-for-store store + #(= "width.sm" (get-in @store (conj path :stroke-width-top))) + "uniform stroke width token applied")) (t/is (= "width.sm" (get-in @store (conj path :stroke-width-top))))))) (t/deftest token-attr?-rejects-unknown-input diff --git a/frontend/test/frontend_tests/runner.cljs b/frontend/test/frontend_tests/runner.cljs index a081b888db..6abd1aebfc 100644 --- a/frontend/test/frontend_tests/runner.cljs +++ b/frontend/test/frontend_tests/runner.cljs @@ -60,6 +60,7 @@ [frontend-tests.errors-test] [frontend-tests.fonts-test] [frontend-tests.helpers-shapes-test] + [frontend-tests.helpers.async-test] [frontend-tests.logic.bool-move-test] [frontend-tests.logic.comp-remove-swap-slots-test] [frontend-tests.logic.comp-watcher-test] @@ -269,6 +270,7 @@ 'frontend-tests.errors-test 'frontend-tests.fonts-test 'frontend-tests.helpers-shapes-test + 'frontend-tests.helpers.async-test 'frontend-tests.logic.bool-move-test 'frontend-tests.logic.comp-remove-swap-slots-test 'frontend-tests.logic.comp-watcher-test diff --git a/frontend/test/frontend_tests/tokens/logic/tokens_source_test.cljs b/frontend/test/frontend_tests/tokens/logic/tokens_source_test.cljs index 6813fd1da4..08b77f5073 100644 --- a/frontend/test/frontend_tests/tokens/logic/tokens_source_test.cljs +++ b/frontend/test/frontend_tests/tokens/logic/tokens_source_test.cljs @@ -80,9 +80,8 @@ "Set `library` as tokens source and wait for the canvas to use it." [store library] (ptk/emit! store (dwtl/set-tokens-source (:id library))) - (await (async/wait-for #(= "#00ff00" (fill-color store)) - "the fill uses the library value of `red`" - {:max-ticks 200}))) + (await (async/wait-for-store store #(= "#00ff00" (fill-color store)) + "the fill uses the library value of `red`"))) (t/deftest ^:async switching-tokens-source-updates-the-canvas (let [library (setup-library) @@ -92,9 +91,8 @@ (t/is (= "#7b2222" (fill-color store))) (ptk/emit! store (dwtl/set-tokens-source (:id library))) - (await (async/wait-for #(= "#00ff00" (fill-color store)) - "the fill uses the library value of `red`" - {:max-ticks 200})) + (await (async/wait-for-store store #(= "#00ff00" (fill-color store)) + "the fill uses the library value of `red`")) (t/is (= "#00ff00" (fill-color store)) "Setting the library as tokens source resolves `red` from the library") @@ -102,9 +100,8 @@ (await (async/settle)) (t/is (contains? (active-set-ids store) (cthi/id :local-set)) "Going back to the local tokens keeps the local set active") - (await (async/wait-for #(= "#7b2222" (fill-color store)) - "the fill uses the local value of `red` again" - {:max-ticks 200})) + (await (async/wait-for-store store #(= "#7b2222" (fill-color store)) + "the fill uses the local value of `red` again")) (t/is (= "#7b2222" (fill-color store)) "Going back to the local tokens resolves `red` from the file again") @@ -116,16 +113,26 @@ store (ths/setup-store file {:libraries [library]})] (await (use-library-tokens store library)) - (await - (mock/with-mocks* - {rp/cmd! (mock/stub (fn [_ _] (rx/observe-on :async (rx/of []))))} - ;; The same events the libraries dialog emits on unlink - (ptk/emit! store - (dwl/unlink-file-from-library (:id file) (:id library)) - (dwtl/set-tokens-source (:id file))) - (await (async/wait-for #(= "#7b2222" (fill-color store)) - "the fill uses the local value of `red` again" - {:max-ticks 200})))) + (let [calls (atom []) + calls$ (rx/subject)] + (await + (mock/with-mocks* + {rp/cmd! (mock/stub (fn [cmd _] + (swap! calls conj cmd) + (rx/push! calls$ cmd) + (rx/observe-on :async (rx/of []))))} + ;; The same events the libraries dialog emits on unlink + (ptk/emit! store + (dwl/unlink-file-from-library (:id file) (:id library)) + (dwtl/set-tokens-source (:id file))) + ;; The unlink event dissocs the library before its watch runs, so the + ;; store cannot tell when the chain ended. The chain's second call + ;; (`get-file-libraries`) is the proof that it ran inside the mock; + ;; wait for it, and keep the store signal for the token resolution. + (await (async/wait-for #(and (= "#7b2222" (fill-color store)) + (some #{:get-file-libraries} @calls)) + "the unlink flow completes and the local tokens resolve" + {:signal (rx/merge calls$ (ptk/input-stream store))}))))) (t/is (nil? (tokens-source store))) (t/is (contains? (active-set-ids store) (cthi/id :local-set)) diff --git a/frontend/test/frontend_tests/util/code_gen_markup_svg_wasm_test.cljs b/frontend/test/frontend_tests/util/code_gen_markup_svg_wasm_test.cljs index 62df62eb59..83e981636f 100644 --- a/frontend/test/frontend_tests/util/code_gen_markup_svg_wasm_test.cljs +++ b/frontend/test/frontend_tests/util/code_gen_markup_svg_wasm_test.cljs @@ -55,12 +55,20 @@ :fn "_render_shape_svg"}))) (t/deftest get-svg-markup-swallows-wasm-errors - (with-redefs [wasm.api/initialized? (constantly true) - wasm.api/render-shape-svg (fn [_ _] (wasm-panic))] - (t/is (= "" (svg.wasm/get-svg-markup [(uuid/next)])))) - (with-redefs [wasm.api/initialized? (constantly true) - wasm.api/render-shapes-svg (fn [_ _] (wasm-panic))] - (t/is (= "" (svg.wasm/get-svg-markup [(uuid/next) (uuid/next)]))))) + ;; The helper logs the non-fatal failure with `console.error` and returns + ;; empty. This test provokes two panics on purpose, so the dumped exception + ;; is noise: silence it for the body, then restore it. + (let [console-error (.-error js/console)] + (set! (.-error js/console) (fn [& _] nil)) + (try + (with-redefs [wasm.api/initialized? (constantly true) + wasm.api/render-shape-svg (fn [_ _] (wasm-panic))] + (t/is (= "" (svg.wasm/get-svg-markup [(uuid/next)])))) + (with-redefs [wasm.api/initialized? (constantly true) + wasm.api/render-shapes-svg (fn [_ _] (wasm-panic))] + (t/is (= "" (svg.wasm/get-svg-markup [(uuid/next) (uuid/next)])))) + (finally + (set! (.-error js/console) console-error))))) (t/deftest get-formatted-svg-markup-safe-on-empty (with-redefs [wasm.api/initialized? (constantly false)]