penpot/backend/test/backend_tests/binfile_jobs_test.clj
Andrey Antukh 29373fff9f
✨ Rework the progress of the import and export dialogs (#12192)
* ✨ 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>
2026-11-02 12:12:39 +01:00

570 lines
26 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 backend-tests.binfile-jobs-test
"The binfile work of a job: the shared adapter that runs it with its
progress reported as job events, and the two job-defs that expose it."
(:require
[app.binfile.common :as bfc]
[app.binfile.jobs :as bfj]
[app.binfile.v1 :as v1]
[app.binfile.v3 :as v3]
[app.common.time :as ct]
[app.common.uuid :as uuid]
[app.db :as db]
[app.jobs :as jobs]
[app.jobs.storage :as js]
[app.rpc.commands.teams :as teams]
[app.storage :as sto]
[app.storage.tmp :as tmp]
[app.util.events :as events]
[backend-tests.helpers :as th]
[clojure.java.io :as jio]
[clojure.test :as t]
[datoteka.fs :as fs]
[datoteka.io :as io]))
(t/use-fixtures :once th/state-init)
(t/use-fixtures :each th/database-reset)
(def ^:private fixture
"A real penpot export, the same fixture the binfile tests read."
"backend_tests/test_files/svg-attrs-camel-case.penpot")
(defn- fixture-path
[]
(-> fixture io/resource jio/file))
(defn- import-fixture!
"A file in the default project of the profile, imported from the
fixture through the core."
[profile]
(-> th/*system*
(assoc ::bfc/project-id (:default-project-id profile))
(assoc ::bfc/profile-id (:id profile))
(assoc ::bfc/team-id (:default-team-id profile))
(assoc ::bfc/input (fixture-path))
(v3/import-files!)))
(defn- job-cfg
"The system cfg as the runner hands it to a handler. The job id is NOT
bound here: the adapter takes it from the context, because the progress
bridge runs on a thread of its own."
[]
th/*system*)
(defn- job-context
[job-id]
(jobs/make-context (jobs/get-job th/*system* job-id)))
(defn- run-export!
"Run an export through the adapter, as the job handler does: the
frozen cfg in, the progress out as job events."
[cfg context file-ids export-type output]
(let [cfg (bfc/export-cfg cfg {:file-ids file-ids
:export-type export-type
:output output})]
(bfj/check-active cfg context)
(bfj/run cfg context v3/export-files!)))
(defn- run-import!
"Run an import through the adapter, as the job handler does: the
frozen cfg in, the progress out as job events. The project touch
stays in the handler, covered by its own tests."
[cfg context {:keys [profile-id project-id team name input version]}]
(bfj/check-active cfg context)
(let [cfg (bfc/import-cfg cfg {:profile-id profile-id
:project-id project-id
:team team
:name name
:input input})]
(bfj/run cfg context
(fn [cfg]
(case (int version)
1 (v1/import-files! cfg)
3 (v3/import-files! cfg)
(throw (ex-info "unsupported binfile version"
{:type :validation
:code :unsupported-version
:version version})))))))
(defn- progress-events
"The progress events of a job, oldest first, with decoded payloads."
[job-id]
(->> (th/db-exec! ["SELECT payload FROM job_event
WHERE job_id = ? AND kind = 'progress'
ORDER BY created_at ASC, id ASC"
job-id])
(mapv #(jobs/decode-progress (:payload %)))))
(defn- export-link-later-package
"A package of one file that uses a shared library, exported so the
library is resolved while importing. The library of the package is gone
afterwards and two replacements share its name, so the import cannot
link it by itself and leaves the file pending resolution."
[profile]
(let [library (th/create-file* 1 {:profile-id (:id profile)
:project-id (:default-project-id profile)
:is-shared true
:name "Icons Library"})
file (th/create-file* 2 {:profile-id (:id profile)
:project-id (:default-project-id profile)
:is-shared false})
output (tmp/tempfile :suffix ".zip")]
(db/insert! th/*system* :file-library-rel
{:file-id (:id file)
:library-file-id (:id library)})
(v3/export-files! (-> th/*system*
(assoc ::bfc/file-ids #{(:id file)})
(assoc ::bfc/export-type :link-later)
(assoc ::bfc/output (io/output-stream output))))
(db/update! th/*system* :file
{:deleted-at (ct/now)}
{:id (:id library)})
(th/create-file* 3 {:profile-id (:id profile)
:project-id (:default-project-id profile)
:is-shared true
:name "Icons Library"})
(th/create-file* 4 {:profile-id (:id profile)
:project-id (:default-project-id profile)
:is-shared true
:name "Icons Library"})
{:library-id (:id library)
:output output}))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; ADAPTER
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(t/deftest export-through-the-adapter-produces-the-artifact
(let [profile (th/create-profile* 1)
file-id (first (:file-ids (import-fixture! profile)))
job-id (th/create-job* :export-binfile {:profile-id (:id profile)})
output (tmp/tempfile* :suffix ".penpot")]
(run-export! (job-cfg) (job-context job-id)
#{file-id} :detach-libraries output)
(t/testing "the artifact is a penpot file with its manifest"
(t/is (fs/exists? output))
(t/is (= [file-id] (mapv :id (:files (v3/get-manifest th/*system* output))))))
(t/testing "the progress of the core became milestones of the job"
(let [events (progress-events job-id)]
(t/is (pos? (count events)))
(t/is (every? keyword? (map :stage events)))
(t/testing "the first one is the file in course, with its position"
(let [event (first events)]
(t/is (= :files (:stage event)))
(t/is (= {:current 1 :total 1} (get-in event [:counters :files])))))
(t/testing "and nothing else: the event is the stage and the counters"
(t/is (every? #(<= (count (keys %)) 2) events)))))))
(t/deftest import-through-the-adapter-imports-the-package
(let [profile (th/create-profile* 1)
team (teams/get-team th/*system*
:profile-id (:id profile)
:project-id (:default-project-id profile))
staged (js/put-resource th/*system* (:id profile)
{:content (sto/content (fixture-path))
:filename "package.penpot"
:mtype "application/zip"})
job-id (th/create-job* :import-binfile {:profile-id (:id profile)
:resource-id (:resource-id staged)})
context (job-context job-id)
result (run-import! (job-cfg) context
{:profile-id (:id profile)
:project-id (:default-project-id profile)
:team team
:name "imported"
:input (js/load-input th/*system* context)
:version 3})]
(t/testing "the package is imported in the destination project"
(t/is (= 1 (count (:file-ids result)))))
(t/testing "the job reported its progress"
(t/is (pos? (count (progress-events job-id)))))))
(t/deftest the-adapter-composes-a-milestone-per-tap
(let [profile (th/create-profile* 1)
file-id (first (:file-ids (import-fixture! profile)))
job-id (th/create-job* :export-binfile {:profile-id (:id profile)})
reports (atom [])]
;; the substrate throttles the durable events, so the whole stream of
;; milestones is only visible with the writes intercepted
(with-redefs [jobs/heartbeat (fn [_cfg & {:as options}]
(swap! reports conj (:progress options))
1)]
(run-export! (job-cfg) (job-context job-id)
#{file-id} :detach-libraries
(tmp/tempfile* :suffix ".penpot")))
(t/testing "the file opens the run with its position"
(let [event (first @reports)]
(t/is (= :files (:stage event)))
(t/is (= {:current 1 :total 1} (get-in event [:counters :files])))))
(t/testing "the stages inside the file carry it as context, with their own counter"
(let [event (first (filter #(= :pages (:stage %)) @reports))]
(t/is (= {:current 1 :total 1} (get-in event [:counters :files])))
(t/is (pos? (get-in event [:counters :pages :current])))
(t/is (pos? (get-in event [:counters :pages :total])))))
(t/testing "a stage outside a file carries no file context"
(let [events (filter #(= :storage-objects (:stage %)) @reports)]
(t/is (pos? (count events)))
(t/is (every? #(nil? (get-in % [:counters :files])) events))))
(t/testing "and every milestone is a stage and its counters, and nothing else"
(t/is (every? #(<= (count (keys %)) 2) @reports)))))
(t/deftest a-tap-carries-the-outer-counters-in-course
(let [profile (th/create-profile* 1)
file-id (first (:file-ids (import-fixture! profile)))
job-id (th/create-job* :export-binfile {:profile-id (:id profile)})
taps (atom [])]
;; the taps themselves, before the adapter turns them into events
(with-redefs [events/tap (fn [_type data] (swap! taps conj data))]
(run-export! (job-cfg) (job-context job-id)
#{file-id} :detach-libraries
(tmp/tempfile* :suffix ".penpot")))
(t/testing "the tap that opens the file carries the outer counters"
(let [tap (first (filter #(= :file (:section %)) @taps))]
(t/is (= {:files {:current 1 :total 1}} (:outer-counters tap)))))
(t/testing "every tap inside the file carries them as context"
(let [inner (filter #(some? (:file-id %)) @taps)]
(t/is (pos? (count inner)))
(t/is (every? #(= {:files {:current 1 :total 1}} (:outer-counters %)) inner))))
(t/testing "a tap outside a file carries no outer counters"
(let [outside (filter #(= :storage-object (:section %)) @taps)]
(t/is (pos? (count outside)))
(t/is (every? #(nil? (:outer-counters %)) outside))))))
(t/deftest a-milestone-merges-outer-counters-with-its-own
(let [milestone @#'bfj/milestone]
(t/testing "outer scopes survive next to the counter of the stage"
(t/is (= {:stage :pages
:counters {:teams {:current 1 :total 1}
:projects {:current 2 :total 2}
:files {:current 1 :total 4}
:pages {:current 2 :total 5}}}
(milestone {:section :page
:current 2 :total 5
:outer-counters {:teams {:current 1 :total 1}
:projects {:current 2 :total 2}
:files {:current 1 :total 4}}}))))
(t/testing "a file tap needs no own counter, the outer one is enough"
(t/is (= {:stage :files
:counters {:files {:current 1 :total 2}}}
(milestone {:section :file
:outer-counters {:files {:current 1 :total 2}}}))))))
(t/deftest entering-a-file-keeps-the-counters-of-the-outer-scopes
(let [cfg {::v3/outer-counters {:teams {:current 1 :total 1}}}
cfg' (@#'v3/with-outer-counter cfg :files {:current 2 :total 4})]
(t/is (= {:teams {:current 1 :total 1}
:files {:current 2 :total 4}}
(::v3/outer-counters cfg')))))
(t/deftest the-import-adapter-composes-the-same-milestones
(let [profile (th/create-profile* 1)
team (teams/get-team th/*system*
:profile-id (:id profile)
:project-id (:default-project-id profile))
staged (js/put-resource th/*system* (:id profile)
{:content (sto/content (fixture-path))
:filename "package.penpot"
:mtype "application/zip"})
job-id (th/create-job* :import-binfile {:profile-id (:id profile)
:resource-id (:resource-id staged)})
context (job-context job-id)
reports (atom [])]
(with-redefs [jobs/heartbeat (fn [_cfg & {:as options}]
(swap! reports conj (:progress options))
1)]
(run-import! (job-cfg) context
{:profile-id (:id profile)
:project-id (:default-project-id profile)
:team team
:name "imported"
:input (js/load-input th/*system* context)
:version 3}))
(t/testing "the run opens with the manifest, which has no units to count"
(let [event (first @reports)]
(t/is (= :manifest (:stage event)))
(t/is (nil? (:counters event)))))
(t/testing "the file carries its position"
(let [event (first (filter #(= :files (:stage %)) @reports))]
(t/is (= {:current 1 :total 1} (get-in event [:counters :files])))))
(t/testing "a stage inside the file carries it as context, with its own counter"
(let [event (first (filter #(= :pages (:stage %)) @reports))]
(t/is (= {:current 1 :total 1} (get-in event [:counters :files])))
(t/is (pos? (get-in event [:counters :pages :current])))
(t/is (pos? (get-in event [:counters :pages :total])))))
(t/testing "and every milestone is a stage and its counters, and nothing else"
(t/is (every? #(<= (count (keys %)) 2) @reports)))))
(t/deftest a-job-that-is-no-longer-active-stops-the-work
(let [profile (th/create-profile* 1)
file-id (first (:file-ids (import-fixture! profile)))
job-id (th/create-job* :export-binfile {:profile-id (:id profile)})
context (job-context job-id)]
(t/testing "a running job is active, and answers with the cfg to thread it"
(t/is (identical? th/*system* (bfj/check-active th/*system* context))))
(t/testing "a cancelled job is not"
(th/db-update! :job {:status "cancelled"} {:id job-id})
(let [cause (try (bfj/check-active th/*system* context) nil
(catch Throwable raised raised))]
(t/is (= :interrupt (th/ex-type cause)))
(t/is (= "cancelled" (:status (ex-data cause))))))
(t/testing "so the export stops at its next progress point"
(let [output (tmp/tempfile* :suffix ".penpot")
cause (try
(run-export! (job-cfg) context
#{file-id} :detach-libraries output)
nil
(catch Throwable raised raised))]
(t/is (= :interrupt (th/ex-type cause)))
(t/is (zero? (count (progress-events job-id))))))
(t/testing "a job that already completed is not active either"
(th/db-update! :job {:status "completed"} {:id job-id})
(let [cause (try (bfj/check-active th/*system* context) nil
(catch Throwable raised raised))]
(t/is (= :interrupt (th/ex-type cause)))))
(t/testing "and neither is a job row that is gone"
(th/db-force-delete :job {:id job-id})
(let [cause (try (bfj/check-active th/*system* context) nil
(catch Throwable raised raised))]
(t/is (= :interrupt (th/ex-type cause)))
(t/is (= "gone" (:status (ex-data cause))))))))
(t/deftest an-interrupted-beat-stops-the-core-at-its-tap
(let [steps (atom [])
context {:id (uuid/next)}
cause (ex-info "the job is no longer active"
{:type :interrupt :code :job-interrupted})]
(with-redefs [jobs/heartbeat (fn [& _] (throw cause))]
(let [raised (try (bfj/run th/*system* context
(fn [_cfg]
(swap! steps conj :before)
;; the sink calls the beat
;; inline, so its interrupt
;; aborts the run here
(events/tap :progress {:section :file})
(swap! steps conj :after)))
nil
(catch Throwable stopped stopped))]
(t/is (= :interrupt (:type (ex-data raised))))
(t/is (= :job-interrupted (:code (ex-data raised))))
(t/is (= [:before] @steps)
"the core stopped at its next tap, the step after never ran")))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; JOB-DEFS
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(t/deftest the-binfile-jobs-follow-the-substrate-contract
(let [defs (::jobs/defs th/*system*)]
(t/testing "the export job produces the resource of a user job"
(let [def (jobs/get-job-def defs :export-binfile)]
(t/is (= :export (::jobs/family def)))
(t/is (= :output (::jobs/resource-role def)))
(t/is (fn? (::jobs/handler def)))))
(t/testing "the import job consumes the resource of the job"
(let [def (jobs/get-job-def defs :import-binfile)]
(t/is (= :import (::jobs/family def)))
(t/is (= :input (::jobs/resource-role def)))
(t/is (fn? (::jobs/handler def)))))
(t/testing "the params are a closed contract"
(let [def (jobs/get-job-def defs :export-binfile)
file-id (uuid/next)]
(t/is (true? ((::jobs/validator def)
{:file-ids #{file-id} :export-type :detach-libraries})))
(t/testing "a job id never travels in params"
(t/is (false? ((::jobs/validator def)
{:file-ids #{file-id} :export-type :detach-libraries
:job-id (uuid/next)}))))))))
(t/deftest the-export-handler-runs-through-the-registry
(let [profile (th/create-profile* 1)
file-id (first (:file-ids (import-fixture! profile)))
job-id (th/create-job* :export-binfile {:profile-id (:id profile)})
result (jobs/invoke (-> th/*system*
(assoc ::jobs/name :export-binfile)
(assoc ::jobs/params {:file-ids #{file-id}
:export-type :detach-libraries})
(assoc ::jobs/context (job-context job-id))))]
(t/testing "the handler answers with the completion envelope of the job"
(t/is (uuid? (:resource-id result)))
(t/is (some? (get-in result [:result :resource-uri]))))
(t/testing "the handler reported progress without knowing the job id"
(t/is (pos? (count (progress-events job-id)))))))
(t/deftest the-import-handler-runs-through-the-registry
(let [profile (th/create-profile* 1)
staged (js/put-resource th/*system* (:id profile)
{:content (sto/content (fixture-path))
:filename "package.penpot"
:mtype "application/zip"})
job-id (th/create-job* :import-binfile {:profile-id (:id profile)
:resource-id (:resource-id staged)})
result (jobs/invoke (-> th/*system*
(assoc ::jobs/name :import-binfile)
(assoc ::jobs/params {:project-id (:default-project-id profile)
:name "imported"
:version 3})
(assoc ::jobs/context (job-context job-id))))]
(t/testing "the package of the job was imported"
(t/is (pos? (count (:file-ids result)))))
(t/testing "the handler reported progress without knowing the job id"
(t/is (pos? (count (progress-events job-id)))))))
(t/deftest the-import-result-keeps-the-type-of-its-ids
"The resolution of an import is a map keyed by the new file id, and it
crosses a plain JSON column: the result schema the job-def declares is
what keeps the keys and the values as uuids, which is what the dashboard
wizard matches against the ids it holds. Without the decoder the substrate
compiles from that schema, the key comes back as a keyword and the entry as
text, and this test fails."
(let [profile (th/create-profile* 1)
{:keys [library-id output]} (export-link-later-package profile)
staged (js/put-resource th/*system* (:id profile)
{:content (sto/content output)
:filename "package.penpot"
:mtype "application/zip"})
job-id (th/create-job* :import-binfile {:profile-id (:id profile)
:resource-id (:resource-id staged)})
result (jobs/invoke (-> th/*system*
(assoc ::jobs/name :import-binfile)
(assoc ::jobs/params {:project-id (:default-project-id profile)
:name "imported"
:version 3})
(assoc ::jobs/context (job-context job-id))))]
(t/testing "the result of the job is stored as the substrate does"
(t/is (= 1 (jobs/complete th/*system* :job-id job-id :result result))))
(let [stored (:result (jobs/get-job th/*system* job-id))
[file-id file-res] (first (:resolution stored))]
(t/testing "the resolution comes back keyed by the new file id"
(t/is (= 1 (count (:resolution stored))))
(t/is (uuid? file-id))
(t/is (= file-id (:id file-res))))
(t/testing "the ids of the entries keep their type"
(t/is (= [file-id] (:file-ids stored)))
(t/is (= [] (:done file-res)))
(t/is (= 1 (count (:pending file-res))))
(let [pending (first (:pending file-res))]
(t/is (= library-id (:id pending)))
(t/is (= 2 (count (:candidates pending))))
(t/is (every? (comp uuid? :id) (:candidates pending)))
(t/is (every? (comp uuid? :project-id) (:candidates pending))))))))
(t/deftest a-package-without-external-libraries-reads-with-an-empty-resolution
(let [profile (th/create-profile* 1)
staged (js/put-resource th/*system* (:id profile)
{:content (sto/content (fixture-path))
:filename "package.penpot"
:mtype "application/zip"})
job-id (th/create-job* :import-binfile {:profile-id (:id profile)
:resource-id (:resource-id staged)})
result (jobs/invoke (-> th/*system*
(assoc ::jobs/name :import-binfile)
(assoc ::jobs/params {:project-id (:default-project-id profile)
:name "imported"
:version 3})
(assoc ::jobs/context (job-context job-id))))]
(t/is (= 1 (jobs/complete th/*system* :job-id job-id :result result)))
(let [stored (:result (jobs/get-job th/*system* job-id))]
(t/testing "a package without external libraries resolves nothing"
(t/is (= {} (:resolution stored))))
(t/testing "and the file ids come back typed"
(t/is (= 1 (count (:file-ids stored))))
(t/is (every? uuid? (:file-ids stored)))))))
(t/deftest a-version-1-result-reads-without-a-resolution
"A version 1 import answers with the file ids and no resolution: the read
path decodes it with the schema of the job-def, which declares the
resolution as optional. Without that schema the file ids come back as text."
(let [profile (th/create-profile* 1)
file-id (uuid/next)
job-id (th/create-job* :import-binfile {:profile-id (:id profile)})]
(t/is (= 1 (jobs/complete th/*system* :job-id job-id
:result {:file-ids [file-id]
:name "imported-v1"
:version 1})))
(let [stored (:result (jobs/get-job th/*system* job-id))]
(t/testing "there is no resolution to decode"
(t/is (nil? (:resolution stored))))
(t/testing "and the file ids come back typed"
(t/is (= [file-id] (:file-ids stored)))
(t/is (= 1 (:version stored)))))))
(t/deftest a-result-of-a-retired-job-def-reads-as-plain-json
"A registry that no longer knows the kind of the row answers with the JSON
the column holds, keywordized keys and uuid as text, instead of raising."
(let [profile (th/create-profile* 1)
file-id (uuid/next)
job-id (th/create-job* :import-binfile {:profile-id (:id profile)})
key (keyword (str file-id))]
(t/is (= 1 (jobs/complete th/*system* :job-id job-id
:result {:file-ids [file-id]
:resolution {file-id {:id file-id
:name "card.penpot"
:done []
:pending []}}
:name "imported"
:version 3})))
(let [stored (:result (jobs/get-job (assoc th/*system* ::jobs/defs {}) job-id))]
(t/testing "the resolution comes back as it is on disk"
(t/is (= [key] (keys (:resolution stored))))
(t/is (= (str file-id) (get-in stored [:resolution key :id]))))
(t/testing "and so do the file ids"
(t/is (= [(str file-id)] (:file-ids stored)))))))