mirror of
https://github.com/penpot/penpot.git
synced 2026-11-09 03:20:57 +00:00
* ✨ Rework the import dialog The import wizard paints a Check/Upload/Import stepper derived from the dialog status, plus a phase title, a description and a progress bar per phase, and its rows follow the new design: folder icon, file name and a size subtitle. While checking, the dialog shows no rows and no spinner, because the phase header already carries the message. The wizard writes the job milestone once, in the header (`jp/current-milestone-text`), since every file of a container reports the same one. A row keeps its name and its size, says "Ready" once its container finished uploading, and shows the error text when it fails; the button that drops a file only appears while the wizard still offers its import step. The `jobs.queued` label now reads "Waiting job slot". The bar measures the whole import: determinate while the containers are uploaded, weighted by their size (`imp/upload-pct`), and animated otherwise, because the stages of a job are not comparable. The phase title counts the files, and `imp/imported-count` counts them done with the job's own counter, so the number moves while a container rebuilds its files one by one. The analysis reads the entries of a version 3 package in one pass: a file shows what it occupies inside its container, its own entries plus the storage objects its media and thumbnails reference, while what the container weighs stays in `:container-size`, the size the upload bar is weighed on. The ready step waits for the end of the analysis (`imp/ready-to-import?`), not for every known file, so Continue never leaves out the files that arrive later. Tests cover the file sizes, the upload percentage, the done counter, the wizard step and the milestone in course. The change also adds `-XX:NativeMemoryTracking=summary` to the backend devenv so `jcmd` shows native memory, and documents the wizard, the promise call styles (`mem:clojure/promesa`) and two CSS pitfalls: a logical `inline-size` wins over an inline physical `width`, and Rumext leaves a plain `:style` map raw, so inline styles leave CLJS as `#js` objects. AI-assisted-by: glm-5.3-flash AI-assisted-by: deepseek-v4.1-flash * ✨ Rework the export dialog Every exported file is its own job, so each row reports its own state: folder icon, name and a size-plus-`.penpot` subtitle; waiting for a worker; running with the milestone of the job and an animated bar; or exported with a tick. A failed row paints red, takes the close icon and shows the hint of the job, or a generic label, through `files-export-modal.export-error`. Success, failure and cancellation replace each other, so a row never reports two outcomes at once. The ids of the messages are canonicalized to string before they match a row, because they cross serialization boundaries. The export result now carries `:size`, so the subtitle says the format until the job produces the artifact and the size once it exists; a transport failure builds a synthetic error, so a nil `ex-data` no longer leaves the row hanging. The header counts the files and says whether the export runs or ended; since every file has its own job, it paints no global bar. Closing changes too: the Close button is gone. While files are in flight the footer offers Cancel export and the X cancels as well, which marks the loading rows cancelled and leaves them in place, so their outcome can be read; once every row reached a terminal state the footer offers Done, which dismisses the dialog. The retry button of the finished design is left out, because the client has no retry mechanism yet. Styles come along: the rows scroll while the header and the footer stay put, and the new phase header, row and bar follow the tokens of the import wizard. The title key becomes `files-export-modal.title`, and the new keys cover the header, the row states and the buttons. AI-assisted-by: muse-spark-1.3 * ♻️ Inline the binfile job adapters into their task handlers The wrappers in app.binfile.jobs only forwarded to the core, so each task handler now composes its own run: frozen cfg, active check, progress bridge and format dispatch. export-cfg freezes the whole export (file-ids, export-type, output) and v3/export-files! takes it as its single argument; ::bfc/ids becomes ::bfc/file-ids. The bridge keeps one job: bfj/run forwards the cfg to its callback, and check-active answers with it to thread the run. AI-assisted-by: muse-spark-1.3 * 🐛 Migrate binfile tests to the single-arg export-files! The refactor made v3/export-files! take a single cfg carrying ::bfc/file-ids and ::bfc/output, but six calls in binfile_test (added by the tokens handling change) still used the old two-arg form with ::bfc/ids. Move them to the new form, as the rest of the file already does. AI-assisted-by: muse-spark-1.3 * 🐛 Fix export artifact expectations to include file size The export flow now reports the artifact size, but the stale expectations dropped it and failed the frontend suite. Include the size in both assertions so they match the emitted message. AI-assisted-by: muse-spark-1.3-contributor * 🐛 Migrate remaining export-files! test callers to the single-arg form rpc_binfile_test and offload_file_data_test still passed the output apart and used ::bfc/ids; move both into the cfg as ::bfc/file-ids/::bfc/output. * 🐛 Hide the downloads hint when no file was exported After Cancel or when every job failed, the export header said "0 files exported · Check your downloads folder."; drop the hint then. * ♻️ Read the object of a container entry once The index of a container asked twice for the object of the same zip entry: once to test it and once as the key of the update. Resolve it once, in the branch that owns it, so the entries of a file never pay for a question that does not apply to them. Addresses a review note on #12192. AI-assisted-by: deepseek-v4.1-flash * ♻️ Never size an import row with its container's weight A row of a version 3 package took the size of the container whenever the container held no entry for its file, while the comment beside it promised the opposite. An unknown size now stays nil, which the row already understands: it paints no subtitle instead of a number that belongs to the whole package. The test carries a file the manifest lists and the container does not hold, and the memory records what the row does then. Addresses a review note on #12192. AI-assisted-by: deepseek-v4.1-flash * 🐛 Retry pool stats read until snapshot is consistent Hikari exposes each pool counter through a separate MXBean call, so a borrow or return between the reads can leave one snapshot off by one. The test read once and failed in CI with total 2 vs active + idle 3. Read up to 20 times with a 10ms pause until total equals active + idle, then assert on the stable snapshot. The invariant stays strict; only the transient race is tolerated. AI-assisted-by: muse-spark-1.3-contributor --------- Co-authored-by: Elena Torro <elenatorro@gmail.com>
350 lines
14 KiB
Clojure
350 lines
14 KiB
Clojure
;; This Source Code Form is subject to the terms of the Mozilla Public
|
|
;; License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
;; file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
;;
|
|
;; Copyright (c) KALEIDOS SUBSIDIARY SL
|
|
|
|
(ns frontend-tests.data.exports-files-test
|
|
"Exporting files as jobs (one file, one job and one answer, with the progress
|
|
of the job reported while it runs), and opening the `.penpot` download dialog
|
|
(the pending changes are sent first, and the dialog is told when some are
|
|
still not saved)."
|
|
(:require
|
|
[app.common.uuid :as uuid]
|
|
[app.main.data.exports.files :as fexp]
|
|
[app.main.data.modal :as modal]
|
|
[app.main.data.persistence :as dps]
|
|
[app.main.repo :as rp]
|
|
[app.main.store :as st]
|
|
[app.util.websocket :as ws]
|
|
[beicon.v2.core :as rx]
|
|
[cljs.test :as t :include-macros true]
|
|
[frontend-tests.helpers.async :as hva]
|
|
[frontend-tests.helpers.mock :as mock]
|
|
[potok.v2.core :as ptk]))
|
|
|
|
(defn- file
|
|
[name]
|
|
{:id (uuid/next)
|
|
:name name
|
|
:project-id (uuid/next)
|
|
:is-shared false})
|
|
|
|
(defn- job
|
|
[job-id status & {:as extra}]
|
|
(merge {:id job-id :status status :name "export-binfile"} extra))
|
|
|
|
(defn- event
|
|
[job-id kind payload]
|
|
{:type :job-event
|
|
:job-id job-id
|
|
:kind kind
|
|
:payload payload})
|
|
|
|
(defn- message
|
|
[payload]
|
|
{:type :message :payload payload})
|
|
|
|
(defn- fake-server
|
|
"The backend of the export: the creation answers with a fresh job the
|
|
test drives, and the read answers the row of that job."
|
|
[calls rows]
|
|
(mock/stub
|
|
(fn [id params]
|
|
(case id
|
|
:create-export-binfile-job
|
|
(let [job-id (uuid/next)]
|
|
(swap! calls conj {:cmd id :params params :job-id job-id})
|
|
(swap! rows assoc job-id (job job-id "running"))
|
|
(rx/of {:id job-id}))
|
|
|
|
:get-job
|
|
(do
|
|
(swap! calls conj {:cmd id :params params})
|
|
(rx/of (get @rows (:id params))))
|
|
|
|
(rx/of nil)))))
|
|
|
|
(def ^:private created?
|
|
(comp #{:create-export-binfile-job} :cmd))
|
|
|
|
(defn- answered?
|
|
"True when the file already has its answer: the artifact or an error."
|
|
[results file-id]
|
|
(some (fn [result]
|
|
(and (= file-id (:file-id result))
|
|
(or (:uri result) (:error result))))
|
|
results))
|
|
|
|
(t/deftest ^:async a-file-is-exported-as-its-own-job
|
|
(let [ws-stream (rx/subject)
|
|
calls (atom [])
|
|
rows (atom {})
|
|
seen (atom [])
|
|
target (file "My file")]
|
|
|
|
(await
|
|
(mock/with-mocks*
|
|
{st/state (atom {:ws-conn ws-stream})
|
|
rp/cmd! (fake-server calls rows)
|
|
ws/get-rcv-stream (mock/stub (fn [_] ws-stream))}
|
|
|
|
(let [done (hva/observe (fexp/export-files :files [target] :type :detach-libraries)
|
|
{:on-next #(swap! seen conj %)})
|
|
job-id (:job-id (first @calls))]
|
|
|
|
(t/testing "the file is exported by a job of its own"
|
|
(t/is (= :create-export-binfile-job (:cmd (first @calls))))
|
|
(t/is (= {:params {:file-ids #{(:id target)}
|
|
:export-type :detach-libraries}}
|
|
(:params (first @calls)))))
|
|
|
|
(t/testing "the milestone of the job is reported while it runs"
|
|
(rx/push! ws-stream
|
|
(message (event job-id :progress
|
|
{:stage :pages
|
|
:counters {:files {:current 1 :total 1}
|
|
:pages {:current 1 :total 2}}})))
|
|
(await (hva/wait-for #(some :progress @seen) "the milestone"))
|
|
|
|
(t/is (= {:file-id (:id target)
|
|
:progress {:stage :pages
|
|
:counters {:files {:current 1 :total 1}
|
|
:pages {:current 1 :total 2}}}}
|
|
(last @seen))))
|
|
|
|
(t/testing "the artifact of the job answers when it is over"
|
|
(swap! rows assoc job-id
|
|
(job job-id "completed"
|
|
:result {:resource-uri "http://assets/export"
|
|
:filename "export.penpot"
|
|
:mtype "application/zip"
|
|
:size 120}))
|
|
(rx/push! ws-stream (message (event job-id :end {:outcome "completed"})))
|
|
(await done)
|
|
|
|
(t/is (= {:file-id (:id target)
|
|
:uri "http://assets/export"
|
|
:filename "My file"
|
|
:size 120}
|
|
(last @seen)))))))))
|
|
|
|
(t/deftest ^:async a-failure-fails-only-its-own-file
|
|
(let [ws-stream (rx/subject)
|
|
calls (atom [])
|
|
rows (atom {})
|
|
seen (atom [])
|
|
one (file "First")
|
|
other (file "Second")]
|
|
|
|
(await
|
|
(mock/with-mocks*
|
|
{st/state (atom {:ws-conn ws-stream})
|
|
rp/cmd! (fake-server calls rows)
|
|
ws/get-rcv-stream (mock/stub (fn [_] ws-stream))}
|
|
|
|
(let [done (hva/observe (fexp/export-files :files [one other] :type :detach-libraries)
|
|
{:on-next #(swap! seen conj %)})]
|
|
|
|
(t/testing "the first file answers with its artifact"
|
|
(let [job-id (:job-id (first (filter created? @calls)))]
|
|
(swap! rows assoc job-id
|
|
(job job-id "completed" :result {:resource-uri "http://assets/first"}))
|
|
(rx/push! ws-stream (message (event job-id :end {:outcome "completed"})))
|
|
(await (hva/wait-for #(answered? @seen (:id one)) "the first answer"))))
|
|
|
|
(t/testing "and the second one fails on its own"
|
|
(await (hva/wait-for #(= 2 (count (filter created? @calls))) "the second job"))
|
|
(let [job-id (:job-id (second (filter created? @calls)))]
|
|
(swap! rows assoc job-id
|
|
(job job-id "failed" :error {:type :internal
|
|
:code :boom
|
|
:hint "boom"}))
|
|
(rx/push! ws-stream (message (event job-id :end {:outcome "failed"})))
|
|
(await done)))
|
|
|
|
(t/testing "the failure does not touch the file that worked"
|
|
(t/is (= {:file-id (:id one)
|
|
:uri "http://assets/first"
|
|
:filename "First"
|
|
:size nil}
|
|
(first (filter #(and (= (:id one) (:file-id %))
|
|
(:uri %))
|
|
@seen)))))
|
|
|
|
(t/is (= {:file-id (:id other)
|
|
:error {:type :internal :code :boom :hint "boom"}}
|
|
(last @seen))))))))
|
|
|
|
(t/deftest ^:async the-created-jobs-reach-the-caller-tagged-with-their-file
|
|
(let [ws-stream (rx/subject)
|
|
calls (atom [])
|
|
rows (atom {})
|
|
seen-jobs (atom [])
|
|
one (file "First")
|
|
other (file "Second")]
|
|
|
|
(await
|
|
(mock/with-mocks*
|
|
{st/state (atom {:ws-conn ws-stream})
|
|
rp/cmd! (fake-server calls rows)
|
|
ws/get-rcv-stream (mock/stub (fn [_] ws-stream))}
|
|
|
|
(let [done (hva/observe (fexp/export-files :files [one other]
|
|
:type :detach-libraries
|
|
:on-job #(swap! seen-jobs conj %))
|
|
{:on-next (constantly nil)})]
|
|
|
|
(await (hva/wait-for #(= 1 (count (filter created? @calls))) "the first job"))
|
|
|
|
(t/testing "the server recorded the creation with its job id"
|
|
(let [creates (filterv created? @calls)]
|
|
(t/is (= 1 (count creates)))
|
|
(t/is (= :create-export-binfile-job (:cmd (first creates))))
|
|
(t/is (uuid? (:job-id (first creates))))))
|
|
|
|
(t/testing "the caller learns that same job tagged with its file"
|
|
(t/is (= [{:job-id (:job-id (first (filterv created? @calls)))
|
|
:file-id (:id one)}]
|
|
@seen-jobs)))
|
|
|
|
;; files export one after the other: the second job is only
|
|
;; created once the first one is over
|
|
(let [{job-1 :job-id} (first @seen-jobs)]
|
|
(swap! rows assoc job-1 (job job-1 "completed" :result {:resource-uri "http://assets/first"}))
|
|
(rx/push! ws-stream (message (event job-1 :end {:outcome "completed"}))))
|
|
|
|
(await (hva/wait-for #(= 2 (count (filter created? @calls))) "the second job"))
|
|
|
|
(t/testing "the caller learns the second job too, tagged and in order"
|
|
(t/is (= 2 (count @seen-jobs)))
|
|
(t/is (= (:job-id (second (filterv created? @calls))) (:job-id (second @seen-jobs))))
|
|
(t/is (= (:id other) (:file-id (second @seen-jobs)))))
|
|
|
|
(let [{job-2 :job-id} (second @seen-jobs)]
|
|
(swap! rows assoc job-2 (job job-2 "completed" :result {:resource-uri "http://assets/second"}))
|
|
(rx/push! ws-stream (message (event job-2 :end {:outcome "completed"})))
|
|
(await done)))))))
|
|
|
|
(t/deftest ^:async every-file-is-queued-until-its-first-milestone
|
|
(let [ws-stream (rx/subject)
|
|
calls (atom [])
|
|
rows (atom {})
|
|
seen (atom [])
|
|
one (file "First")
|
|
other (file "Second")]
|
|
|
|
(await
|
|
(mock/with-mocks*
|
|
{st/state (atom {:ws-conn ws-stream})
|
|
rp/cmd! (fake-server calls rows)
|
|
ws/get-rcv-stream (mock/stub (fn [_] ws-stream))}
|
|
|
|
(let [done (hva/observe (fexp/export-files :files [one other] :type :detach-libraries)
|
|
{:on-next #(swap! seen conj %)})]
|
|
|
|
(t/testing "every file queues up front, before any of its jobs exists"
|
|
(await (hva/wait-for #(= 2 (count (filter :queued @seen))) "the queued messages"))
|
|
(t/is (= [{:file-id (:id one) :queued true}
|
|
{:file-id (:id other) :queued true}]
|
|
(vec (filter :queued @seen)))))
|
|
|
|
(t/testing "a worker picking a job up is not a milestone and ends nothing"
|
|
(let [job-id (:job-id (first (filter created? @calls)))]
|
|
(rx/push! ws-stream (message (event job-id :start {:attempt 1})))
|
|
(t/is (not-any? :started @seen))))
|
|
|
|
;; finish the first job so the second one is created and the run ends
|
|
(let [job-1 (:job-id (first (filter created? @calls)))]
|
|
(swap! rows assoc job-1
|
|
(job job-1 "completed" :result {:resource-uri "http://assets/first"}))
|
|
(rx/push! ws-stream (message (event job-1 :end {:outcome "completed"}))))
|
|
|
|
(await (hva/wait-for #(= 2 (count (filter created? @calls))) "the second job"))
|
|
|
|
(let [job-2 (:job-id (second (filter created? @calls)))]
|
|
(swap! rows assoc job-2
|
|
(job job-2 "completed" :result {:resource-uri "http://assets/second"}))
|
|
(rx/push! ws-stream (message (event job-2 :end {:outcome "completed"})))
|
|
(await done)))))))
|
|
|
|
(t/deftest ^:async a-cancelled-job-marks-the-file-cancelled-without-artifact
|
|
(let [ws-stream (rx/subject)
|
|
calls (atom [])
|
|
rows (atom {})
|
|
seen (atom [])
|
|
target (file "My file")]
|
|
|
|
(await
|
|
(mock/with-mocks*
|
|
{st/state (atom {:ws-conn ws-stream})
|
|
rp/cmd! (fake-server calls rows)
|
|
ws/get-rcv-stream (mock/stub (fn [_] ws-stream))}
|
|
|
|
(let [done (hva/observe (fexp/export-files :files [target] :type :detach-libraries)
|
|
{:on-next #(swap! seen conj %)})
|
|
job-id (:job-id (first @calls))]
|
|
|
|
(t/testing "the cancelled outcome is an explicit message, never an artifact"
|
|
(swap! rows assoc job-id (job job-id "cancelled"))
|
|
(rx/push! ws-stream (message (event job-id :end {:outcome "cancelled"})))
|
|
(await done)
|
|
(t/is (= {:file-id (:id target) :cancelled true} (last @seen)))
|
|
(t/is (not-any? :uri @seen))))))))
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
;; DOWNLOAD DIALOG
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
(defn- file-param
|
|
[file-id]
|
|
{:id file-id :name "File" :project-id (uuid/next) :is-shared false})
|
|
|
|
(defn- with-queued-change
|
|
"Adds to `state` a save queue that still holds a change of `file-id`."
|
|
[state file-id]
|
|
(let [commit-id (uuid/next)]
|
|
(assoc state :persistence {:queue (conj #queue [] commit-id)
|
|
:index {commit-id {:id commit-id :file-id file-id}}})))
|
|
|
|
(defn- ^:async dialog-props
|
|
"Opens the download dialog for `file-id` from `state` and returns the
|
|
props the dialog is shown with, plus the file ids the save wait got."
|
|
[state file-id]
|
|
(let [store (ptk/store {:state state :on-error #(t/is false (str %))})
|
|
waited (atom nil)]
|
|
(await
|
|
(mock/with-mocks*
|
|
{rp/cmd! (mock/stub (fn [_ _] (rx/of false)))
|
|
dps/force-persist-files-and-wait (mock/stub (fn [file-ids _]
|
|
(reset! waited file-ids)
|
|
(rx/empty)))}
|
|
(ptk/emit! store (fexp/open-export-dialog [(file-param file-id)]))
|
|
(await (hva/wait-for #(some? (get-in @store [::modal/modal :props]))
|
|
"the dialog opens"))))
|
|
(assoc (get-in @store [::modal/modal :props]) ::waited @waited)))
|
|
|
|
;; Scenario: the user downloads a file whose changes are all saved. Proves:
|
|
;; the pending changes of that file are sent first, and the dialog shows no
|
|
;; warning.
|
|
(t/deftest ^:async download-of-a-saved-file-has-no-warning
|
|
(let [file-id (uuid/next)
|
|
props (await (dialog-props {} file-id))]
|
|
(t/is (= #{file-id} (::waited props)) "the save wait covers the downloaded file")
|
|
(t/is (false? (:unsaved props)))))
|
|
|
|
;; Scenario: the file still has a change the server does not have after the
|
|
;; wait. Proves: the dialog is told, so it can warn that the download will
|
|
;; not include it.
|
|
(t/deftest ^:async download-of-a-file-with-unsaved-changes-warns
|
|
(let [file-id (uuid/next)
|
|
props (await (dialog-props (with-queued-change {} file-id) file-id))]
|
|
(t/is (true? (:unsaved props)))))
|
|
|
|
;; Scenario: another file has unsaved changes. Proves: only the downloaded
|
|
;; file's changes count.
|
|
(t/deftest ^:async unsaved-changes-of-another-file-do-not-warn
|
|
(let [file-id (uuid/next)
|
|
props (await (dialog-props (with-queued-change {} (uuid/next)) file-id))]
|
|
(t/is (false? (:unsaved props)))))
|