penpot/frontend/test/frontend_tests/data/workspace_compare_test.cljs
Juan de la Cruz 936939fd75
✨ Compare a branch with main on the canvas (#13201)
* ✨ Add per-profile review marks to branch comparisons

A reviewer goes through the changes of a branch one by one, and the
comparison has to remember which ones they have already seen.

Add the `file_branch_change_review` table and two RPC methods,
`get-branch-change-reviews` and `set-branch-change-review`. A mark
belongs to a branch, a profile, a direction and a change, and keeps the
fingerprint of the change at the time it was marked, so the client can
ask for a new review when the change moves again.

Signed-off-by: elhombretecla <delacruzgarciajuan@gmail.com>

* ✨ Compare a branch with main on the canvas

The comparison and the conflicts of a branch opened in two modals, away
from the design they talk about. Move both to a compare mode on the
canvas of the branch itself.

The left sidebar lists the changes, in either direction, and the right
one shows the change chosen. The canvas marks every change where it is,
or only the one chosen, and a bar goes through them one at a time. The
conflicts of a merge or an update are resolved in the same mode, with a
popover beside the layer in conflict.

The file is never touched: the workspace is read-only while the mode
lasts, and what the canvas shows that the branch does not hold (the
update, the choices made, main itself) is drawn by the WASM render
alone. Main is put together from the changes of both directions, and
can be seen beside the branch or by holding M.

Each direction keeps its own selection of changes, and the changes
marked as reviewed are stored per profile.

The marks and the bar of the design reviews become shared components.
That bar and the bar of the review now center on the canvas the rulers
leave, as the bar of the branch does.

Signed-off-by: elhombretecla <delacruzgarciajuan@gmail.com>

* ✨ Add end-to-end tests for the compare mode

Cover entering and leaving the mode, a merge with a change left out, an
update from main and the three kinds of conflict. The tests of the
reviews and of the merged branches follow the comparison to the canvas.

Signed-off-by: elhombretecla <delacruzgarciajuan@gmail.com>

* 💄 Move the WebGL rendering label to the bottom of the canvas

The top right corner of the canvas now holds the name of the branch
while it is compared with main, and the label was drawn under it.

Signed-off-by: elhombretecla <delacruzgarciajuan@gmail.com>

* ✨ Make the canvas answer for the changes in the compare mode

While a branch is compared with main both sidebars talk about changes,
not about layers, yet the canvas still selected any layer. Make it
answer for the layers with a change or a conflict alone.

The pointer on such a layer marks it and shows its row in the list, a
click chooses it and a double click zooms to it. A click anywhere else
lets the choice go, and the canvas marks every change again. The row
under the pointer marks its layer too. No layer is selected, outlined
or dragged over; pan and zoom stay as they are, and a commenting tool
takes the canvas back while it is in use.

The bar that goes through the changes shows with a change chosen only.

The canvas no longer draws its projection again on every render, only
when what it shows changes.

Signed-off-by: elhombretecla <delacruzgarciajuan@gmail.com>

* 💄 Keep the compare navigator grey outside a design review

The bar that goes through the changes of a comparison had the blue
border of a design review even with no review open. Give it the grey of
the bar at the bottom of the canvas, and keep the blue inside a review.

Signed-off-by: elhombretecla <delacruzgarciajuan@gmail.com>

* ✨ Line main up with the change chosen in the side by side view

Main beside the branch showed the same spot of the page, so a layer the
branch moved fell out of the half of main once the canvas went to it.
The side by side view now has two modes: linked, as it was, and aligned
to the change, where main is moved by how far the layer chosen moved,
so both versions sit at the middle of their halves. Zoom and pan stay
linked, and main slides to the next change as it is chosen.

The mode is picked in a submenu of the view, or with the A key, and the
browser keeps the last one for the next comparison. The note on the
canvas says how far main is moved.

Main put a moved group, path or boolean at the place the branch has:
the diff leaves out the box of a layer, and it was only built again for
the layers whose box is their position and size. Their mark in main now
stands where they are.

Signed-off-by: elhombretecla <delacruzgarciajuan@gmail.com>

* 💄 Name the side by side view alone in the view menu

The control of the view said "Side by side - aligned" with main lined up
with the change chosen, which the note under the toolbar already says.
It names the view alone now, in both modes.

Signed-off-by: elhombretecla <delacruzgarciajuan@gmail.com>

* 🐛 Fit the whole page in both halves of the side by side view

Fitting the whole page, and zooming from the menu or to 100%, took the
middle of the canvas as the middle of the page, but beside main that is
the divider: both halves showed the page cut on the left, out of its
middle.

While main is beside the branch these zooms now work in the half of the
branch: the page fits in it and stands at its middle, and a zoom with no
pointer turns on that middle. Fitting the whole page takes in main too,
which can reach further than the branch. Fitting the change chosen goes
through the same fit, which every fit of the workspace now shares.

Signed-off-by: elhombretecla <delacruzgarciajuan@gmail.com>

* 🐛 Zoom the canvas with the wheel over the marks of the changes

The wheel with ctrl or cmd held only zoomed the canvas over the canvas
itself or over the comments. Over the tags of the changes, which take
the pointer, the browser zoomed the whole page instead. It zooms the
canvas over anything drawn on top of it now: marks, tags and bars.

The note that says how main stands beside the branch takes the look of
the other notes of the canvas, in the primary colour.

Signed-off-by: elhombretecla <delacruzgarciajuan@gmail.com>

* 🐛 Fix the layers, bars and badges of the compare and review modes

- The comments go over the marks of the changes, so a thread or a task
  opened from a bubble is no longer covered by them.
- How far the review of a comparison is reads at the size of the rest
  of its bar, in Spanish too.
- The way out of a comparison, back from its conflicts and out of a
  review are icon buttons, with what they do in a tooltip.
- Beside the branch, main keeps the frame of the canvas, rounded
  corners included: its half covered it.
- The tab of the other direction counts its changes as soon as the
  comparison opens: they come with every diff, but only the tab shown
  was counted.
- The read-only badge is a quiet grey one with a lock, in the pages
  panel and over the changes of a comparison.

Signed-off-by: elhombretecla <delacruzgarciajuan@gmail.com>

* 🌐 Translate branching and design reviews into Spanish

The Spanish catalog lacked 418 keys, nearly all of them of branching,
comparing, conflicts, merges and design reviews, so those buttons, menus,
messages and tooltips showed in English. Add them all, with the terms
the catalog already uses.

Drop 61 keys of both catalogs that nothing reads any more, left over
from screens that are gone, and spell "colors" as the rest of the
English catalog does. The Spanish word list takes the valid words the
glued-word check flagged.

Signed-off-by: elhombretecla <delacruzgarciajuan@gmail.com>

* 🐛 Find the way out of a review by its name in the e2e test

The test looked for "Exit review" in the menu of the review bar, but the
way out sits on the bar itself, and it is an icon button now, with its
name in a tooltip. Click it on the bar, by its name.

Signed-off-by: elhombretecla <delacruzgarciajuan@gmail.com>

* ✨ Rework the detail of a change in the compare mode

The property changes and the buttons of a change stand on the
background the inspect tab gives its blocks, over the whole width of
the sidebar and as one block when they follow each other. The buttons
go one over the other, as wide as the sidebar.

Under a change or a conflict, the comments that wait for an answer and
the tasks still open on its layer give its context, inside a design
review and outside it. They replace the tab of the comments of the
review the detail had inside one.

The bar of a design review says "In review", on one line: a long title
is cut short, whole on hover. The arrows of the bar that goes through
the changes hold the place in the list between them, with no gap.

Signed-off-by: elhombretecla <delacruzgarciajuan@gmail.com>

* ♻️ Bring the branching styles in line with the design system

Review every stylesheet of branching and design reviews against the
design system rules. None of them depends on the deprecated styles; a
few details did not follow the rules:

- colors that change with a state go through a local custom property
  (the branch counts, the caption of a preview card, the actions of the
  branch switcher and the next step of a task);
- the alias of the canvas frame border resolves to its DS token;
- the panels of the compare mode scroll with overflow-y, as
  overflow-block is not well supported yet;
- unused imports are gone, and a z-index and a variable that are not DS
  tokens say so in a TODO.

The generated CSS stays the same, except the overflow and two
backgrounds that render as before.

Signed-off-by: elhombretecla <delacruzgarciajuan@gmail.com>

* ♻️ Reuse the design system components in the branching UI

Where the branching and design review UI built its own control, it uses
the one of the design system, keeping what the screens look like:

- the multi-line fields use the DS text area, as high as before, and
  their labels name them;
- renaming a branch uses the DS input;
- the filter of comments and tasks is a DS menu, as wide as its button;
- the read-only badge is a DS tag, and its own component is gone.

The design system had nothing for a floating panel that is not a menu,
so it gets a popover: it only closes on a click out of it or on Escape
and gives the focus back, and the caller draws the panel. The branch
switcher and the branches of a dashboard card use it instead of the
legacy dropdown, unchanged on screen.

The verdicts of a review and its reviewers read as radios and as
checkboxes. The preview card of the design system, unused since the
old conflicts dialog went away, is deleted.

Signed-off-by: elhombretecla <delacruzgarciajuan@gmail.com>

---------

Signed-off-by: elhombretecla <delacruzgarciajuan@gmail.com>
2026-11-20 16:24:27 +01:00

608 lines
28 KiB
Clojure

;; This Source Code Form is subject to the terms of the Mozilla Public
;; License, v. 2.0. If a copy of the MPL was not distributed with this
;; file, You can obtain one at http://mozilla.org/MPL/2.0/.
;;
;; Copyright (c) KALEIDOS SUBSIDIARY SL
(ns frontend-tests.data.workspace-compare-test
"The compare mode: it opens on the branch alone, makes the workspace
read-only while it lasts, leaves nothing behind, and applies the
conflict choices with what the attempt that returned them carried."
(:require
[app.common.geom.rect :as grc]
[app.common.types.shape :as cts]
[app.common.uuid :as uuid]
[app.main.data.changes :as dch]
[app.main.data.workspace.branches :as dwb]
[app.main.data.workspace.compare :as dwcp]
[app.main.data.workspace.zoom :as dwz]
[app.main.repo :as rp]
[app.util.storage :as storage]
[beicon.v2.core :as rx]
[cljs.test :as t :include-macros true]
[frontend-tests.helpers.async :as async]
[frontend-tests.helpers.mock :as mock]
[potok.v2.core :as ptk]))
(def ^:private branch-file-id (uuid/next))
(def ^:private main-file-id (uuid/next))
(def ^:private branch
{:id (uuid/next) :name "feature-login" :branch-file-id branch-file-id
:source-file-id main-file-id :ahead 2})
(defn- make-store
[state]
(ptk/store {:state state
:on-error (fn [cause] (t/is false (str cause)))}))
(defn- on-branch
[]
(make-store {:current-file-id branch-file-id}))
(defn- cmd-stub
"Fake `rp/cmd!` that records every call into `calls` and never answers,
so a diff stays loading and a merge or an update stays in flight."
[calls]
(mock/stub (fn [cmd params]
(swap! calls conj [cmd params])
(rx/subject))))
(defn- last-call
[calls cmd]
(->> @calls (filter #(= cmd (first %))) (last) (second)))
(t/deftest ^:async the-mode-opens-on-the-branch-and-fetches-its-diff
(let [calls (atom [])]
(await
(mock/with-mocks* {rp/cmd! (cmd-stub calls)}
(let [store (on-branch)]
(ptk/emit! store (dwcp/enter branch))
(await (async/wait-for #(some? (last-call calls :get-branch-diff))
"the diff is asked for"))
(t/is (dwcp/comparing? @store))
(t/is (= {:id (:id branch) :name "feature-login" :branch-file-id branch-file-id}
(get-in @store [:workspace-compare :branch]))
"the mode keeps what names the branch, not its counts")
(t/is (= :compare (get-in @store [:workspace-compare :phase])))
(t/is (= {:branch-id (:id branch) :direction :branch->main}
(last-call calls :get-branch-diff)))
(rx/dispose! store))))))
(t/deftest ^:async the-mode-does-not-open-with-main-open
(let [calls (atom [])]
(await
(mock/with-mocks* {rp/cmd! (cmd-stub calls)}
(let [store (make-store {:current-file-id main-file-id})]
(ptk/emit! store (dwcp/enter branch))
(await (async/settle))
(t/is (not (dwcp/comparing? @store)))
(t/is (empty? @calls) "nothing is compared")
(rx/dispose! store))))))
(t/deftest ^:async leaving-the-mode-leaves-nothing-behind
(let [calls (atom [])]
(await
(mock/with-mocks* {rp/cmd! (cmd-stub calls)}
(let [store (on-branch)]
(ptk/emit! store (dwcp/enter branch))
(ptk/emit! store (dwb/toggle-diff-excluded #{"color//a/1"}))
(ptk/emit! store (dwb/open-conflict-resolutions {:branch branch
:mode :merge
:conflicts [{:id (uuid/next)}]}))
(ptk/emit! store (dwcp/exit))
(await (async/wait-for-store store
#(nil? (:workspace-branch-conflicts @store))
"the attempt is dropped"))
(t/is (not (dwcp/comparing? @store)))
(t/is (= #{} (dwb/diff-excluded (:workspace-branch-diff @store)))
"the selection is dropped")
(rx/dispose! store))))))
(t/deftest ^:async the-direction-refetches-and-starts-the-list-over
(let [calls (atom [])]
(await
(mock/with-mocks* {rp/cmd! (cmd-stub calls)}
(let [store (on-branch)]
(ptk/emit! store (dwcp/enter branch))
(ptk/emit! store (dwcp/set-filter :library))
(ptk/emit! store (dwcp/select "color//a/1" {:travel false}))
(ptk/emit! store (dwcp/set-direction :main->branch))
(await (async/wait-for #(= :main->branch (:direction (last-call calls :get-branch-diff)))
"the other direction is asked for"))
(t/is (= {:selected nil :filter :all :phase :compare}
(select-keys (:workspace-compare @store) [:selected :filter :phase])))
(rx/dispose! store))))))
(t/deftest ^:async main-is-known-by-the-changes-of-both-directions
(let [calls (atom [])
change {:kind :shape :status :modified :id (uuid/next)}
clash {:kind :shape :status :conflict :id (uuid/next)}
answers (mock/stub (fn [cmd params]
(swap! calls conj [cmd params])
(if (and (= :get-branch-diff cmd)
(= :main->branch (:direction params)))
(rx/of {:changes [change] :conflicts [clash]})
(rx/subject))))]
(await
(mock/with-mocks* {rp/cmd! answers}
(let [store (on-branch)
stopper (rx/subject)]
(ptk/emit! store (dwcp/watch-events stopper))
(ptk/emit! store (dwcp/enter branch))
(await (async/wait-for #(some? (get-in @store [:workspace-compare :other-changes]))
"the other direction answers"))
(t/is (= [:branch->main :main->branch]
(->> @calls
(filter #(= :get-branch-diff (first %)))
(mapv (comp :direction second))))
"the diff listed, then its other direction")
(t/is (= [change] (get-in @store [:workspace-compare :other-changes])))
(t/is (= [clash] (get-in @store [:workspace-compare :other-conflicts]))
"with its conflicts, which its tab counts")
(ptk/emit! store (dwcp/set-direction :main->branch))
(t/is (nil? (get-in @store [:workspace-compare :other-changes]))
"the ones of the direction left do not stay")
(t/is (nil? (get-in @store [:workspace-compare :other-conflicts])))
(rx/push! stopper :stop)
(rx/dispose! store))))))
(t/deftest the-panels-keep-what-the-user-set
(let [store (make-store {:current-file-id branch-file-id
:workspace-compare {:branch branch :phase :compare
:hidden false :collapsed #{}}})]
(ptk/emit! store (dwcp/toggle-group "library"))
(t/is (= #{"library"} (get-in @store [:workspace-compare :collapsed])))
(ptk/emit! store (dwcp/toggle-group "library"))
(t/is (= #{} (get-in @store [:workspace-compare :collapsed])))
(ptk/emit! store (dwcp/toggle-hidden))
(t/is (true? (get-in @store [:workspace-compare :hidden])))
(ptk/emit! store (dwcp/set-view :highlight))
(t/is (false? (get-in @store [:workspace-compare :hidden]))
"picking a view shows the changes again")
(ptk/emit! store (dwcp/set-count :main->branch 4))
(t/is (= {:main->branch 4} (get-in @store [:workspace-compare :counts])))
(rx/dispose! store)))
(t/deftest choosing-the-change-chosen-lets-it-go
(let [store (make-store {:current-file-id branch-file-id
:workspace-compare {:phase :compare :selected nil}})]
(ptk/emit! store (dwcp/toggle-selected "shape/p/a"))
(t/is (= "shape/p/a" (get-in @store [:workspace-compare :selected])))
(ptk/emit! store (dwcp/toggle-selected "shape/p/b"))
(t/is (= "shape/p/b" (get-in @store [:workspace-compare :selected])))
(ptk/emit! store (dwcp/toggle-selected "shape/p/b"))
(t/is (nil? (get-in @store [:workspace-compare :selected])))
(rx/dispose! store)))
(t/deftest the-pointer-is-followed-on-the-list-and-on-the-canvas
(let [store (make-store {:workspace-compare {:phase :compare :selected "shape/p/a"}})
idle (make-store {})
mode #(select-keys (:workspace-compare @store) [:hovered :hovered-from :selected])]
(ptk/emit! store (dwcp/set-hovered "shape/p/b" :canvas))
(t/is (= {:hovered "shape/p/b" :hovered-from :canvas :selected "shape/p/a"} (mode))
"the choice stays")
(ptk/emit! store (dwcp/set-hovered "shape/p/c" :list))
(ptk/emit! store (dwcp/set-hovered nil :canvas))
(t/is (= "shape/p/c" (:hovered (mode)))
"the canvas does not let go the row the pointer reached")
(ptk/emit! store (dwcp/set-hovered nil :list))
(t/is (nil? (:hovered (mode))))
(ptk/emit! idle (dwcp/set-hovered "shape/p/b" :list))
(t/is (not (contains? @idle :workspace-compare)) "outside the mode nothing is kept")
(rx/dispose! store)
(rx/dispose! idle)))
(t/deftest a-click-on-the-canvas-chooses-the-change-of-its-layer
(let [store (make-store {:workspace-compare {:phase :compare :selected "shape/p/a"}})
selected #(get-in @store [:workspace-compare :selected])]
(ptk/emit! store (dwcp/set-hovered "shape/p/b" :canvas))
(ptk/emit! store (dwcp/pick-hovered))
(t/is (= "shape/p/b" (selected)))
(ptk/emit! store (dwcp/set-hovered "shape/p/c" :list))
(ptk/emit! store (dwcp/pick-hovered))
(t/is (nil? (selected)) "a row under the pointer is no layer under it")
(ptk/emit! store (dwcp/select "shape/p/a" {:travel false}))
(ptk/emit! store (dwcp/set-hovered nil :list))
(ptk/emit! store (dwcp/pick-hovered))
(t/is (nil? (selected)) "the background lets the choice go")
(rx/dispose! store)))
(t/deftest the-layer-chosen-is-fitted-in-the-room-the-view-leaves
(let [page-id (uuid/next)
shape-id (uuid/next)
change {:kind :shape :status :modified :id shape-id :page-id page-id
:key "layer"}
state (fn [view]
{:current-file-id branch-file-id
:current-page-id page-id
:files {branch-file-id
{:data {:pages-index
{page-id {:objects {shape-id {:id shape-id
:selrect {:x 100 :y 100
:width 200 :height 100}}}}}}}}
:workspace-local {:vport {:width 1000 :height 500}
:vbox {:x 0 :y 0 :width 1000 :height 500}
:zoom 1}
:workspace-branch-diff {:diff {:changes [change]}}
:workspace-compare {:phase :compare :view view
:selected (dwcp/item-key change)}})
fit (fn [view]
(let [store (make-store (state view))]
(ptk/emit! store (dwcp/fit-selected))
(let [local (:workspace-local @store)]
(rx/dispose! store)
local)))
whole (fit :highlight)
half (fit :side)
center (fn [{:keys [vbox]} at]
(+ (:x vbox) (* at (:width vbox))))]
(t/is (> (:zoom whole) 1) "the layer fills the canvas")
(t/is (= 200 (center whole 0.5)) "in its middle")
(t/is (< (:zoom half) (:zoom whole)) "half the canvas holds less")
(t/is (= 200 (center half 0.75)) "in the middle of the half of the branch")
(t/is (= (/ 1000 (:zoom half)) (get-in half [:vbox :width])))))
;;; --- The side mode ---
(def ^:private side-mode-key
:app.main.data.workspace.compare/side-mode)
(defn- keeping-side-mode
"Run `f` with `stored` as the side mode the browser keeps (nil for
none), and leave what the browser kept before as it was."
[stored f]
(let [prev (get @storage/user side-mode-key)]
(if (some? stored)
(swap! storage/user assoc side-mode-key stored)
(swap! storage/user dissoc side-mode-key))
(try
(f)
(finally
(if (some? prev)
(swap! storage/user assoc side-mode-key prev)
(swap! storage/user dissoc side-mode-key))))))
(defn- side-store
[view]
(make-store {:current-file-id branch-file-id
:workspace-compare {:branch branch :phase :compare
:view view :side-mode :linked}}))
(defn- side-mode
[store]
(get-in @store [:workspace-compare :side-mode]))
(t/deftest ^:async the-side-mode-starts-linked-and-comes-back-as-picked
(let [calls (atom [])
prev (get @storage/user side-mode-key)]
(swap! storage/user dissoc side-mode-key)
(await
(mock/with-mocks* {rp/cmd! (cmd-stub calls)}
(let [store (on-branch)]
(ptk/emit! store (dwcp/enter branch))
(t/is (= :linked (side-mode store)) "linked with nothing kept")
(ptk/emit! store (dwcp/set-side-mode :aligned))
(ptk/emit! store (dwcp/exit))
(ptk/emit! store (dwcp/enter branch))
(await (async/wait-for #(some? (last-call calls :get-branch-diff))
"the diff is asked for"))
(t/is (= :aligned (side-mode store)) "the one picked last")
(rx/dispose! store))))
(if (some? prev)
(swap! storage/user assoc side-mode-key prev)
(swap! storage/user dissoc side-mode-key))))
(t/deftest ^:async the-side-mode-kept-must-be-one
(let [calls (atom [])
prev (get @storage/user side-mode-key)]
(swap! storage/user assoc side-mode-key :sideways)
(await
(mock/with-mocks* {rp/cmd! (cmd-stub calls)}
(let [store (on-branch)]
(ptk/emit! store (dwcp/enter branch))
(await (async/wait-for #(some? (last-call calls :get-branch-diff))
"the diff is asked for"))
(t/is (= :linked (side-mode store)))
(rx/dispose! store))))
(if (some? prev)
(swap! storage/user assoc side-mode-key prev)
(swap! storage/user dissoc side-mode-key))))
(t/deftest ^:async resolving-takes-the-side-mode-kept
(let [calls (atom [])
prev (get @storage/user side-mode-key)]
(swap! storage/user assoc side-mode-key :aligned)
(await
(mock/with-mocks* {rp/cmd! (cmd-stub calls)}
(let [store (on-branch)]
(ptk/emit! store (dwb/open-conflict-resolutions {:branch branch
:mode :merge
:conflicts [{:id (uuid/next)}]}))
(ptk/emit! store (dwcp/start-resolve))
(await (async/wait-for #(some? (last-call calls :get-branch-diff))
"the diff is asked for"))
(t/is (= :aligned (side-mode store)))
(rx/dispose! store))))
(if (some? prev)
(swap! storage/user assoc side-mode-key prev)
(swap! storage/user dissoc side-mode-key))))
(t/deftest the-side-mode-is-kept-in-the-browser
(keeping-side-mode
nil
(fn []
(let [store (side-store :side)]
(ptk/emit! store (dwcp/set-side-mode :aligned))
(t/is (= :aligned (side-mode store)))
(t/is (= :aligned (get @storage/user side-mode-key)))
(rx/dispose! store)))))
(t/deftest ^:async the-key-flips-the-side-mode-beside-the-branch-alone
(let [prev (get @storage/user side-mode-key)
side (side-store :side)
flat (side-store :highlight)]
(ptk/emit! side (dwcp/toggle-side-mode))
(await (async/wait-for-store side #(= :aligned (side-mode side)) "it flips"))
(ptk/emit! side (dwcp/toggle-side-mode))
(await (async/wait-for-store side #(= :linked (side-mode side)) "and back"))
(ptk/emit! flat (dwcp/toggle-side-mode))
(await (async/settle))
(t/is (= :linked (side-mode flat)) "highlighting, it does nothing")
(rx/dispose! side)
(rx/dispose! flat)
(if (some? prev)
(swap! storage/user assoc side-mode-key prev)
(swap! storage/user dissoc side-mode-key))))
(t/deftest another-view-keeps-the-side-mode
(let [store (make-store {:current-file-id branch-file-id
:workspace-compare {:branch branch :phase :compare
:view :side :side-mode :aligned}})]
(ptk/emit! store (dwcp/set-view :highlight))
(ptk/emit! store (dwcp/set-view :side))
(t/is (= :aligned (side-mode store)))
(rx/dispose! store)))
;;; --- Zoom with main beside the branch ---
(defn- zoom-state
"A page with a board at 100,100 (200 by 100) on a viewport of 1000 by
500, with the compare mode in `view` when there is one."
[view local]
(let [page-id (uuid/next)
board-id (uuid/next)]
(cond-> {:current-file-id branch-file-id
:current-page-id page-id
:files {branch-file-id
{:data {:pages-index
{page-id
{:objects
{uuid/zero {:id uuid/zero :type :frame :parent-id uuid/zero
:shapes [board-id]}
board-id (cts/setup-rect {:id board-id :type :frame
:parent-id uuid/zero
:x 100 :y 100
:width 200 :height 100})}}}}}}
:workspace-local (merge {:vport {:width 1000 :height 500}
:vbox (grc/make-rect 0 0 1000 500)
:zoom 1}
local)}
(some? view)
(assoc :workspace-compare {:phase :compare :view view}))))
(defn- zoom-with
[state event]
(let [store (make-store state)]
(ptk/emit! store event)
(let [local (:workspace-local @store)]
(rx/dispose! store)
local)))
(defn- vbox-at
"The point of the page at `at` of the width of the viewbox."
[{:keys [vbox]} at]
(+ (:x vbox) (* at (:width vbox))))
(t/deftest the-whole-page-is-fitted-in-the-room-the-view-leaves
(let [whole (zoom-with (zoom-state nil nil) dwz/zoom-to-fit-all)
half (zoom-with (zoom-state :side nil) dwz/zoom-to-fit-all)]
(t/is (= 200 (vbox-at whole 0.5)) "in the middle of the canvas")
(t/is (= 200 (vbox-at half 0.75)) "in the middle of the half of the branch")
(t/is (< (:zoom half) (:zoom whole)) "half the canvas holds less")
(t/is (= 200 (vbox-at (zoom-with (zoom-state :highlight nil) dwz/zoom-to-fit-all) 0.5))
"highlighting, the canvas is not split")))
(t/deftest the-whole-page-takes-in-main-beside-the-branch
(let [state (assoc-in (zoom-state :side nil) [:workspace-compare :main-extent]
(grc/make-rect 1000 100 200 100))
local (zoom-with state dwz/zoom-to-fit-all)]
(t/is (= 650 (vbox-at local 0.75)) "the middle of the board on both sides")))
(t/deftest the-zoom-turns-on-the-middle-of-the-room-the-view-leaves
(let [local {:zoom 2 :vbox (grc/make-rect 0 0 500 250)}
whole (zoom-with (zoom-state nil local) dwz/reset-zoom)
half (zoom-with (zoom-state :side local) dwz/reset-zoom)
more (zoom-with (zoom-state :side local) (dwz/increase-zoom nil))]
(t/is (= 1 (:zoom half)))
(t/is (= 250 (vbox-at whole 0.5)) "the middle of the canvas stays")
(t/is (= 375 (vbox-at half 0.75)) "the middle of the half of the branch stays")
(t/is (= 375 (vbox-at more 0.75)) "zooming in from the menu too")))
(t/deftest the-events-do-nothing-outside-the-mode
(let [store (make-store {:current-file-id branch-file-id})]
(keeping-side-mode
nil
(fn []
(ptk/emit! store (dwcp/set-side-mode :aligned))
(ptk/emit! store (dwcp/toggle-side-mode))
(t/is (nil? (get @storage/user side-mode-key)) "nothing is kept")))
(ptk/emit! store (dwcp/toggle-hidden))
(ptk/emit! store (dwcp/set-filter :library))
(ptk/emit! store (dwcp/toggle-group "library"))
(ptk/emit! store (dwcp/set-count :main->branch 4))
(t/is (not (dwcp/comparing? @store)) "none of them creates the mode")
(rx/dispose! store)))
;;; --- Entries ---
(t/deftest an-entry-is-known-by-its-change-key
(t/is (= "shape/p/1" (dwcp/item-key {:key "shape/p/1" :kind :shape :id 1})))
(t/is (= "token:brand" (dwcp/item-key {:kind :token :id "brand"}))
"an entry without a change key falls back to its kind and id"))
(t/deftest a-layer-is-placed-by-the-open-file-or-by-the-diff
(let [id (uuid/next)
rect {:x 1 :y 2 :width 3 :height 4}
objects {id {:id id :selrect rect}}]
(t/is (= rect (dwcp/item-rect objects {:kind :shape :id id})))
(t/is (= rect (dwcp/item-rect {} {:kind :shape :id id :shape {:selrect rect}}))
"a layer the open file lacks comes whole in the diff")
(t/is (= rect (dwcp/item-rect {} {:kind :shape :id id :shape rect}))
"its own position and size do when it carries no selrect")
(t/is (nil? (dwcp/item-rect objects {:kind :color :id id})))
(t/is (nil? (dwcp/item-rect {} {:kind :shape :id id})))))
(t/deftest the-conflicts-of-an-attempt-win-over-the-ones-of-the-diff
(let [branch-id (uuid/next)
diff-st {:diff {:conflicts [{:id :from-diff}]}}]
(t/is (= [{:id :from-attempt}]
(dwcp/get-conflicts {:branch-id branch-id :conflicts [{:id :from-attempt}]}
diff-st branch-id)))
(t/is (= [{:id :from-diff}]
(dwcp/get-conflicts {:branch-id (uuid/next) :conflicts [{:id :other}]}
diff-st branch-id))
"the attempt of another branch does not count")
(t/is (= [{:id :from-diff}] (dwcp/get-conflicts nil diff-st branch-id)))))
;;; --- Read-only ---
(t/deftest ^:async no-edit-is-committed-while-comparing
(let [state {:current-file-id branch-file-id
:permissions {:can-edit true}
:files {branch-file-id {:id branch-file-id :revn 1 :vern 0}}
:workspace-compare {:branch branch :phase :compare}}
store (make-store state)
commits (atom [])
sub (->> (ptk/input-stream store)
(rx/filter dch/commit?)
(rx/subs! #(swap! commits conj %)))]
(ptk/emit! store (dch/commit-changes {:redo-changes [{:type :mod-obj}]
:undo-changes []
:file-id branch-file-id}))
(await (async/settle))
(t/is (empty? @commits) "the comparison refuses the edit")
(rx/dispose! sub)
(rx/dispose! store)))
;;; --- Resolving conflicts ---
(t/deftest ^:async resolving-starts-in-the-direction-of-the-attempt
(let [calls (atom [])]
(await
(mock/with-mocks* {rp/cmd! (cmd-stub calls)}
(let [store (on-branch)]
(ptk/emit! store (dwb/open-conflict-resolutions {:branch branch
:mode :update
:conflicts [{:id (uuid/next)}]}))
(ptk/emit! store (dwcp/start-resolve))
(await (async/wait-for #(some? (last-call calls :get-branch-diff))
"the diff is asked for"))
(t/is (= :resolve (get-in @store [:workspace-compare :phase])))
(t/is (= (:id branch) (get-in @store [:workspace-compare :branch :id])))
(t/is (= :main->branch (:direction (last-call calls :get-branch-diff)))
"an update resolves main's changes")
(rx/dispose! store))))))
(t/deftest ^:async conflicts-of-another-file-wait-for-their-branch
(let [calls (atom [])]
(await
(mock/with-mocks* {rp/cmd! (cmd-stub calls)}
(let [store (make-store {:current-file-id main-file-id})]
(ptk/emit! store (dwb/open-conflict-resolutions {:branch branch
:mode :merge
:conflicts [{:id (uuid/next)}]}))
(ptk/emit! store (dwcp/start-resolve))
(await (async/wait-for-store store
#(true? (get-in @store [:workspace-branch-conflicts :pending]))
"the conflicts wait"))
(t/is (not (dwcp/comparing? @store)) "the mode waits for the branch file")
(rx/dispose! store))))))
(t/deftest going-back-keeps-the-choices-and-drops-the-attempt
(let [conflict-id (uuid/next)
store (make-store {:current-file-id branch-file-id
:workspace-compare {:branch branch :phase :resolve :selected "x"}
:workspace-branch-diff {:branch-id (:id branch)
:resolutions {conflict-id :main}}
:workspace-branch-conflicts {:branch-id (:id branch)
:conflicts [{:id conflict-id}]}})]
(ptk/emit! store (dwcp/back-to-compare))
(t/is (= {:phase :compare :selected nil}
(select-keys (:workspace-compare @store) [:phase :selected])))
(t/is (nil? (:workspace-branch-conflicts @store)))
(t/is (= {conflict-id :main} (get-in @store [:workspace-branch-diff :resolutions])))
(rx/dispose! store)))
(defn- resolving
"A store that resolves one conflict of the branch, with a choice made
for it and for a conflict that is no longer there."
[conflict-id diff-st attempt]
(make-store {:current-file-id branch-file-id
:workspace-compare {:branch (select-keys branch [:id :name :branch-file-id])
:phase :resolve}
:workspace-branch-diff
(merge {:branch-id (:id branch)
:status :loaded
:diff {:conflicts [{:id conflict-id}] :meta {:main-revn 9}}
:resolutions {conflict-id :branch (uuid/next) :main}}
diff-st)
:workspace-branch-conflicts attempt}))
(t/deftest ^:async applying-a-merge-sends-what-the-attempt-carried
(let [calls (atom [])
conflict-id (uuid/next)]
(await
(mock/with-mocks* {rp/cmd! (cmd-stub calls)}
(let [store (resolving conflict-id
{:direction :branch->main
:excluded {:branch->main #{"from-the-comparison"}}}
{:branch-id (:id branch)
:mode :merge
:conflicts [{:id conflict-id}]
:main-revn 7
:excluded #{"from-the-attempt"}
:comment "Ships the header"})]
(ptk/emit! store (dwcp/apply-resolutions))
(await (async/wait-for #(some? (last-call calls :merge-file-branch))
"the merge is asked for"))
(t/is (= {:branch-id (:id branch)
:resolutions {conflict-id :branch}
:excluded #{"from-the-attempt"}
:comment "Ships the header"
:expected-main-revn 9}
(last-call calls :merge-file-branch))
"only the choices of its conflicts travel, with its selection and comment")
(rx/dispose! store))))))
(t/deftest ^:async applying-an-update-from-the-comparison-sends-its-selection
(let [calls (atom [])
conflict-id (uuid/next)]
(await
(mock/with-mocks* {rp/cmd! (cmd-stub calls)}
(let [store (resolving conflict-id
{:direction :main->branch
:excluded {:main->branch #{"left-out"}
:branch->main #{"other-direction"}}}
nil)]
(ptk/emit! store (dwcp/apply-resolutions))
(await (async/wait-for #(some? (last-call calls :update-branch-from-main))
"the update is asked for"))
(t/is (= {:branch-id (:id branch)
:resolutions {conflict-id :branch}
:excluded #{"left-out"}
:expected-main-revn 9}
(last-call calls :update-branch-from-main)))
(t/is (nil? (last-call calls :merge-file-branch)))
(rx/dispose! store))))))