Andrey Antukh f27bfdf76a
♻️ Reduce the routes, viewer, theme and typography e2e specs (#13059)
* ♻️ Reduce the routes, viewer, theme and typography e2e specs

The five specs of Task 10 (routes, theme, viewer and roles) go from 14
tests to 6, and the two of Task 11 (missing font and add typography)
from 9 to 4. Nine unit tests cover rules that had none: five for the
private legacy-match and four for the viewer frame navigation. No
skipped test and no fixture removed.

The browser keeps the wiring: the known and unknown hash as two steps
of one test, the theme applied live without a reload, the viewer role
walked in two loads with the context menu counted instead of trusted,
and the typography states merged around shared fixtures. The viewer
zoom click comes back as a header step; the arrows e2e becomes unit
tests, with the Next button still clicked by the child-click test. The
missing-font test asserts the renamed, disabled typography button
instead of a hidden one it never described.

WorkspacePage gains reloadWorkspace, which clears the socket registry,
reloads and reopens the mock, and settles on the sitemap page name or
on the canvas when the restored sidebar tab hides it. layerRow matches
rows by anchored text, because a selected row's action buttons can join
its accessible name, and selectLayer accepts click options for the
shift-click steps. The font specs use both and no longer import
MockWebSocketHelper.

The viewer role asserts its two absences by testid: new-project-button
already existed and add-custom-font is new in fonts.cljs, the only
product change, with the visible side covered by dashboard.spec.
routes_test adds the path-params case for legacy-match; viewer_test
asserts the first event and the page id of the frame navigation, and
watch-events delivers asynchronously and handles the nil a watch can
return.

Measured at four workers: 23 to 10 tests; Task 10 from 141.3 to
68.5/72.3 worker-seconds and Task 11 from 150.1 to 109.0/109.3.

Closes #13058

AI-assisted-by: deepseek-v4.1-flash

* 🐛 Let the two reduced e2e steps fail when they should

The typography step selected the next layer with Shift, which in the layers
panel is a range selection: the range also brought in the text with a
typography token, so the button was hidden by the applied token and not by
the mixed values. A mod-click adds only that shape, and two new assertions
keep the token and the asset texts out of the selection.

The logo step clicked 100 px below the logo and asserted the URL, which
potok updates after the click returns, so the assertion matched the old URL
on its first poll. Probing elementFromPoint under the logo answers the
hit-area question directly.

AI-assisted-by: deepseek-v4.1-flash
2026-11-20 17:41:43 +01:00

237 lines
11 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.viewer-test
(:require
[app.common.uuid :as uuid]
[app.main.data.viewer :as dv]
[app.main.router :as rt]
[beicon.v2.core :as rx]
[cljs.test :as t]
[frontend-tests.helpers.async :as async]
[potok.v2.core :as ptk]))
(def ^:private page-id
(uuid/custom 1 1))
(defn- base-state
"Build a minimal viewer state with the given frames and query-params."
[{:keys [frames index]}]
{:route {:params {:query {:page-id (str page-id)
:index (str index)}}}
:viewer {:pages {page-id {:frames frames}}}
:viewer-local {:viewport-size {:width 1000 :height 800}}})
(t/deftest zoom-to-fit-clamps-out-of-bounds-index
(t/testing "index exceeds frame count"
(let [state (base-state {:frames [{:selrect {:width 100 :height 100}}]
:index 1})
result (ptk/update dv/zoom-to-fit state)]
(t/is (= (get-in result [:viewer-local :zoom-type]) :fit))
(t/is (number? (get-in result [:viewer-local :zoom])))))
(t/testing "index is zero with single frame (normal case)"
(let [state (base-state {:frames [{:selrect {:width 100 :height 100}}]
:index 0})
result (ptk/update dv/zoom-to-fit state)]
(t/is (= (get-in result [:viewer-local :zoom-type]) :fit))
(t/is (number? (get-in result [:viewer-local :zoom])))))
(t/testing "index within valid range with multiple frames"
(let [state (base-state {:frames [{:selrect {:width 100 :height 100}}
{:selrect {:width 200 :height 200}}]
:index 1})
result (ptk/update dv/zoom-to-fit state)]
(t/is (= (get-in result [:viewer-local :zoom-type]) :fit))
(t/is (number? (get-in result [:viewer-local :zoom]))))))
(t/deftest zoom-to-fill-clamps-out-of-bounds-index
(t/testing "index exceeds frame count"
(let [state (base-state {:frames [{:selrect {:width 100 :height 100}}]
:index 1})
result (ptk/update dv/zoom-to-fill state)]
(t/is (= (get-in result [:viewer-local :zoom-type]) :fill))
(t/is (number? (get-in result [:viewer-local :zoom])))))
(t/testing "index is zero with single frame (normal case)"
(let [state (base-state {:frames [{:selrect {:width 100 :height 100}}]
:index 0})
result (ptk/update dv/zoom-to-fill state)]
(t/is (= (get-in result [:viewer-local :zoom-type]) :fill))
(t/is (number? (get-in result [:viewer-local :zoom])))))
(t/testing "index within valid range with multiple frames"
(let [state (base-state {:frames [{:selrect {:width 100 :height 100}}
{:selrect {:width 200 :height 200}}]
:index 1})
result (ptk/update dv/zoom-to-fill state)]
(t/is (= (get-in result [:viewer-local :zoom-type]) :fill))
(t/is (number? (get-in result [:viewer-local :zoom]))))))
(defn ^:async watch-events
"Collect the events a watch emits, waiting for its stream to terminate."
[event state]
(let [out (atom [])]
(await (async/observe (or (ptk/watch event state nil) (rx/empty))
:on-next #(swap! out conj %)))
@out))
(defn- zoom-state
"Build a viewer state with the given `:zoom` query param and zoom type."
[zoom-param zoom-type]
{:route {:params {:query (cond-> {:page-id (str page-id) :index "0"}
(some? zoom-param)
(assoc :zoom zoom-param))}}
:viewer-local (cond-> {}
(some? zoom-type)
(assoc :zoom-type zoom-type))})
(t/deftest ^:async update-zoom-querystring-does-not-navigate-when-url-already-matches
(t/testing "zoom type already described by the query string"
(t/is (empty? (await (watch-events dv/update-zoom-querystring
(zoom-state "fit" :fit))))))
(t/testing "no zoom type and no zoom query param"
(t/is (empty? (await (watch-events dv/update-zoom-querystring
(zoom-state nil nil)))))))
(t/deftest ^:async update-zoom-querystring-navigates-when-zoom-changes
(t/testing "zoom type differs from the query string"
(let [events (await (watch-events dv/update-zoom-querystring
(zoom-state "fit" :fill)))
{:keys [id params options]} (some-> (first events) deref)]
(t/is (= 1 (count events)))
(t/is (= :viewer id))
(t/is (= :fill (:zoom params)))
(t/is (true? (::rt/replace options)))))
(t/testing "zoom query param absent, other params preserved"
(let [events (await (watch-events dv/update-zoom-querystring
(zoom-state nil :fit)))
{:keys [params]} (some-> (first events) deref)]
(t/is (= 1 (count events)))
(t/is (= :fit (:zoom params)))
(t/is (= (str page-id) (:page-id params)))
(t/is (= "0" (:index params)))))
(t/testing "zoom type cleared drops the query param"
(let [events (await (watch-events dv/update-zoom-querystring
(zoom-state "fit" nil)))
{:keys [params]} (some-> (first events) deref)]
(t/is (= 1 (count events)))
(t/is (not (contains? params :zoom))))))
(t/deftest ^:async bundle-fetched-with-zoom-fill-url-does-not-navigate
;; Regression test for the React "maximum update depth exceeded"
;; error (2.18.0-RC5): loading the viewer with a URL that already
;; contains `zoom=fill` re-entered the cycle zoom-to-fill →
;; update-zoom-querystring → nav → navigated → zoom-to-fill…,
;; because update-zoom-querystring navigated unconditionally.
;; At HEAD the guard breaks the cycle, so a bundle fetch must not
;; emit any navigation.
(let [state (as-> (base-state {:frames [{:selrect {:width 100 :height 100}}]
:index 0}) state
;; The router stores the same query map under both keys.
(let [query (assoc (rt/get-params state) :zoom "fill")]
(-> state
(assoc-in [:route :params :query] query)
(assoc-in [:route :query-params] query))))]
(let [events (await (watch-events (dv/bundle-fetched
{:file {:id page-id
:data {:pages [page-id]
:pages-index {page-id {:objects {}}}}}
:project {}
:team {:features []}
:share-links []
:libraries []
:users []
:permissions {}
:thumbnails {}})
state))
;; `update-page-position-data` and `go-to-frame-auto` are part
;; of the normal init sequence; the navigation events are the
;; ones that can restart the zoom cycle.
navigations (filter #(= :app.main.router/navigate (ptk/type %)) events)]
(t/is (empty? navigations)))))
(defn- minimal-bundle
[page-ids]
{:file {:id (uuid/custom 2 2)
:data {:pages (vec page-ids)
:pages-index (into {} (map (fn [id] [id {:objects {}}])) page-ids)}}
:project {}
:team {:features []}
:share-links []
:libraries []
:users []
:permissions {}
:thumbnails {}})
(defn- route-without-page-id
[]
(let [query {:file-id (str (uuid/custom 2 2)) :section "interactions" :index "0"}]
{:route {:params {:query query} :query-params query}}))
(t/deftest ^:async initialize-without-page-id-fetches-the-bundle
;; The viewer route passes `:page-id nil` when the URL has none; the
;; params check used to reject it before any fetch.
(let [events (await (watch-events (dv/initialize {:file-id (uuid/custom 2 2)
:page-id nil
:share-id nil})
{}))]
(t/is (= [::dv/fetch-bundle] (map ptk/type (remove nil? events))))))
(t/deftest ^:async bundle-fetched-without-page-id-names-the-first-page
(t/testing "the route is replaced by one naming the first page"
(let [first-page (uuid/custom 3 1)
events (await (watch-events (dv/bundle-fetched
(minimal-bundle [first-page (uuid/custom 3 2)]))
(route-without-page-id)))
{:keys [id params options]} (some-> (first events) deref)]
(t/is (= 1 (count events)))
(t/is (= :viewer id))
(t/is (= first-page (:page-id params)))
(t/is (= "interactions" (:section params)))
(t/is (true? (::rt/replace options)))))
(t/testing "a file without pages keeps the route"
(let [events (await (watch-events (dv/bundle-fetched (minimal-bundle []))
(route-without-page-id)))
navigations (filter #(= :app.main.router/navigate (ptk/type %)) events)]
(t/is (empty? navigations)))))
;; --- Frame navigation ------------------------------------------------------
;; The viewer arrows emit `close-thread` and a navigation to the next or
;; previous index, and nothing at the ends of the sequence.
(t/deftest ^:async select-next-frame-navigates-to-the-next-frame
(let [events (await (watch-events dv/select-next-frame
(base-state {:frames [{} {} {}] :index 1})))
{:keys [id params]} (some-> (second events) deref)]
(t/is (= 2 (count events)))
(t/is (= :app.main.data.comments/close-comment-thread (ptk/type (first events))))
(t/is (= :viewer id))
(t/is (= 2 (:index params)))
(t/is (= (str page-id) (:page-id params)))))
(t/deftest ^:async select-next-frame-stays-on-the-last-frame
(t/is (empty? (await (watch-events dv/select-next-frame
(base-state {:frames [{} {} {}] :index 2}))))))
(t/deftest ^:async select-prev-frame-navigates-to-the-previous-frame
(let [events (await (watch-events dv/select-prev-frame
(base-state {:frames [{} {} {}] :index 1})))
{:keys [id params]} (some-> (second events) deref)]
(t/is (= 2 (count events)))
(t/is (= :app.main.data.comments/close-comment-thread (ptk/type (first events))))
(t/is (= :viewer id))
(t/is (= 0 (:index params)))
(t/is (= (str page-id) (:page-id params)))))
(t/deftest ^:async select-prev-frame-stays-on-the-first-frame
(t/is (empty? (await (watch-events dv/select-prev-frame
(base-state {:frames [{} {} {}] :index 0}))))))