diff --git a/frontend/src/app/main/store.cljs b/frontend/src/app/main/store.cljs index 9ecfc39284..dc389033ae 100644 --- a/frontend/src/app/main/store.cljs +++ b/frontend/src/app/main/store.cljs @@ -107,8 +107,8 @@ (defn format-last-events "Render the `last-events` buffer as a multi-line string with the wall-clock time of each event and the delta (ms) since the previous - entry. The first entry has no delta. Useful for embedding in error - reports." + entry. The delta column is right-padded to 10 chars so the event + names align. Useful for embedding in error reports." ([] (format-last-events @last-events)) ([events] (let [lines @@ -117,13 +117,14 @@ out (transient [])] (if xs (let [{:keys [name t]} (first xs) - iso (ct/format-inst t :iso) - tail (if prev-t - (str " (+" (ct/diff-ms prev-t t) "ms)") - "")] + iso (ct/format-inst t :iso) + delta (if prev-t + (str "(+" (ct/diff-ms prev-t t) "ms)") + "(+0ms)") + delta-pad (str/pad delta {:length 10 :type :right})] (recur t (next xs) - (conj! out (str iso tail " " name)))) + (conj! out (str iso " " delta-pad " " name)))) (persistent! out)))] (str/join "\n" lines)))) diff --git a/frontend/test/frontend_tests/data/store_test.cljs b/frontend/test/frontend_tests/data/store_test.cljs new file mode 100644 index 0000000000..56d29fec85 --- /dev/null +++ b/frontend/test/frontend_tests/data/store_test.cljs @@ -0,0 +1,49 @@ +;; 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 INC Sucursal en España SL + +(ns frontend-tests.data.store-test + "Unit tests for app.main.store. + Tests cover: + - format-last-events – empty, single, multi, column alignment" + (:require + [app.main.store :as st] + [cljs.test :as t :include-macros true] + [cuerdas.core :as str])) + +(t/deftest format-last-events-empty + (t/testing "empty events produce empty string" + (t/is (= "" (st/format-last-events []))))) + +(t/deftest format-last-events-single + (t/testing "a single event shows (+0ms) and its name" + (let [result (st/format-last-events [{:name ":test/event" :t (js/Date. 1000)}])] + (t/is (str/includes? result "(+0ms)")) + (t/is (str/includes? result ":test/event")) + (t/is (= 1 (count (str/split result "\n"))))))) + +(t/deftest format-last-events-multi + (t/testing "multiple events show correct deltas and event names" + (let [events [{:name ":event/a" :t (js/Date. 0)} + {:name ":event/b" :t (js/Date. 500)} + {:name ":event/c" :t (js/Date. 2500)}] + lines (str/split (st/format-last-events events) "\n")] + (t/is (= 3 (count lines))) + (t/is (some #(str/includes? % ":event/a") lines)) + (t/is (some #(str/includes? % ":event/b") lines)) + (t/is (some #(str/includes? % ":event/c") lines)) + (t/is (str/includes? (nth lines 0) "(+0ms)")) + (t/is (str/includes? (nth lines 1) "(+500ms)")) + (t/is (str/includes? (nth lines 2) "(+2000ms)"))))) + +(t/deftest format-last-events-alignment + (t/testing "event names start at the same column across all lines" + (let [events [{:name ":evt-a" :t (js/Date. 0)} + {:name ":evt-b" :t (js/Date. 500)}] + lines (str/split (st/format-last-events events) "\n") + col-a (.indexOf (nth lines 0) ":evt-a") + col-b (.indexOf (nth lines 1) ":evt-b")] + (t/is (pos? col-a)) + (t/is (= col-a col-b))))) diff --git a/frontend/test/frontend_tests/runner.cljs b/frontend/test/frontend_tests/runner.cljs index cda245671d..450f6978bd 100644 --- a/frontend/test/frontend_tests/runner.cljs +++ b/frontend/test/frontend_tests/runner.cljs @@ -9,6 +9,7 @@ [frontend-tests.copy-as-svg-test] [frontend-tests.data.nitrate-test] [frontend-tests.data.repo-test] + [frontend-tests.data.store-test] [frontend-tests.data.uploads-test] [frontend-tests.data.viewer-test] [frontend-tests.data.workspace-colors-test] @@ -86,6 +87,7 @@ 'frontend-tests.copy-as-svg-test 'frontend-tests.data.nitrate-test 'frontend-tests.data.repo-test + 'frontend-tests.data.store-test 'frontend-tests.errors-test 'frontend-tests.main-errors-test 'frontend-tests.data.uploads-test