The personal finance app for everyone (by everyone)
Ruby
Fix the false Timeout badge on sidebar sparklines (#3576)
zeishrfd5314a37efc
diff --git a/app/helpers/accounts_helper.rb b/app/helpers/accounts_helper.rbindex ffb8480d3b..1e12be4bcc 100644--- a/app/helpers/accounts_helper.rb+++ b/app/helpers/accounts_helper.rb@@ -9,6 +9,26 @@ module AccountsHelpersync_account_path(account)end+ # How long a sidebar sparkline frame may spend fetching before+ # `turbo_frame_timeout_controller` replaces it with a warning badge.+ SPARKLINE_FRAME_TIMEOUT_MS = 10_000++ # Shared by the group-level and the per-account sparkline frames so the two+ # call sites cannot drift apart.+ #+ # Those frames are `loading: "lazy"`: Turbo fetches them when they scroll+ # into view, which can be long after they render, or never, so the countdown+ # has to time the request rather than the element. The frames declare when it+ # starts and when it stops.+ def sparkline_frame_data+ {+ controller: "turbo-frame-timeout",+ action: "turbo:before-fetch-request->turbo-frame-timeout#startTimeout " \+ "turbo:frame-load->turbo-frame-timeout#clearTimeout",+ turbo_frame_timeout_timeout_value: SPARKLINE_FRAME_TIMEOUT_MS+ }+ end+# Returns the account id segment from `/accounts/<id>(/...)?`, or nil.# Used as a cache-key component so the sidebar's active-link styling is# correct without busting the cache for every unrelated path change.diff --git a/app/javascript/controllers/turbo_frame_timeout_controller.js b/app/javascript/controllers/turbo_frame_timeout_controller.jsindex a81b3dde91..cd57e7ad6e 100644--- a/app/javascript/controllers/turbo_frame_timeout_controller.js+++ b/app/javascript/controllers/turbo_frame_timeout_controller.js@@ -1,22 +1,37 @@import { Controller } from "@hotwired/stimulus"// Connects to data-controller="turbo-frame-timeout"+//+// The countdown times a request, not the element. A frame with loading="lazy"+// is only fetched once it scrolls into view, which can be long after this+// controller connects, or never, so the frame declares when to start:+//+// data-action="turbo:before-fetch-request->turbo-frame-timeout#startTimeout+// turbo:frame-load->turbo-frame-timeout#clearTimeout"export default class extends Controller {static values = { timeout: { type: Number, default: 10000 } }connect() {- this.timeoutId = setTimeout(() => {- this.handleTimeout()- }, this.timeoutValue)-- // Listen for successful frame loads to clear timeout- this.element.addEventListener("turbo:frame-load", this.clearTimeout.bind(this))+ // Turbo keeps a frame busy for the length of its request. An eager frame+ // can start fetching before Stimulus connects, and the attribute is what+ // tells us the event has already been and gone.+ if (this.element.hasAttribute("busy")) {+ this.startTimeout()+ }}disconnect() {this.clearTimeout()}+ startTimeout() {+ this.clearTimeout()++ this.timeoutId = setTimeout(() => {+ this.handleTimeout()+ }, this.timeoutValue)+ }+clearTimeout() {if (this.timeoutId) {clearTimeout(this.timeoutId)@@ -39,4 +54,4 @@ export default class extends Controller {</div>`}-}\ No newline at end of file+}diff --git a/app/views/accounts/_accountable_group.html.erb b/app/views/accounts/_accountable_group.html.erbindex efcc44f0f4..ce44bc98b8 100644--- a/app/views/accounts/_accountable_group.html.erb+++ b/app/views/accounts/_accountable_group.html.erb@@ -13,7 +13,10 @@<div class="ml-auto text-right grow"><%= tag.p format_money(account_group.total_money), class: "text-sm font-medium text-primary privacy-sensitive" %>- <%= turbo_frame_tag "#{account_group.key}_sparkline", src: accountable_sparkline_path(account_group.key), loading: "lazy", data: { controller: "turbo-frame-timeout", turbo_frame_timeout_timeout_value: 10000 } do %>+ <%= turbo_frame_tag "#{account_group.key}_sparkline",+ src: accountable_sparkline_path(account_group.key),+ loading: "lazy",+ data: sparkline_frame_data do %><div class="flex items-center w-8 h-4 ml-auto"><div class="w-6 h-px bg-loader"></div></div>@@ -44,7 +47,10 @@<div class="ml-auto text-right grow h-10"><%= tag.p format_money(account.balance_money), class: "text-sm font-medium text-primary whitespace-nowrap privacy-sensitive" %>- <%= turbo_frame_tag dom_id(account, :sparkline), src: sparkline_account_path(account), loading: "lazy", data: { controller: "turbo-frame-timeout", turbo_frame_timeout_timeout_value: 10000 } do %>+ <%= turbo_frame_tag dom_id(account, :sparkline),+ src: sparkline_account_path(account),+ loading: "lazy",+ data: sparkline_frame_data do %><div class="flex items-center w-8 h-4 ml-auto"><div class="w-6 h-px bg-loader"></div></div>diff --git a/test/system/sidebar_sparklines_test.rb b/test/system/sidebar_sparklines_test.rbnew file mode 100644index 0000000000..aa39237884--- /dev/null+++ b/test/system/sidebar_sparklines_test.rb@@ -0,0 +1,122 @@+require "application_system_test_case"++# The sidebar sparkline frames are `loading: "lazy"` and most of them sit+# inside a collapsed account group, so Turbo never fetches them. The timeout+# countdown therefore has to time the request rather than the element+# connecting — otherwise every unexpanded row claims it timed out, ten seconds+# into any page load, without a single request having been made.+class SidebarSparklinesTest < ApplicationSystemTestCase+ SPARKLINE_FRAME = "#sidebar-scroll turbo-frame[data-controller='turbo-frame-timeout']"+ BADGE_TEXT = "Timeout"++ # The group total in the <summary> renders the same chart as the rows inside+ # the disclosure and is on screen from the start, so "a sparkline loaded" has+ # to say which one: the rows are `dom_id(account, :sparkline)`, the total is+ # "#{account_group.key}_sparkline".+ ACCOUNT_SPARKLINE_FRAME = "#{SPARKLINE_FRAME}[id^='sparkline_account_']"+ GROUP_SPARKLINE_FRAME = "#{SPARKLINE_FRAME}[id$='_sparkline']"++ # The shipped window is ten seconds. Outliving it for real would add most of+ # a minute to the system suite for a cosmetic badge, so the frames render+ # with a short one and every wait below is proportional to it.+ SHORT_TIMEOUT_MS = 300++ # Holds sparkline responses open for as long as a test needs one pending.+ # The wait has to happen inside the server thread, which the test cannot+ # reach, so a process-wide flag stands in for it; clearing the flag releases+ # the request instead of leaving it in flight past the test.+ #+ # It wraps the whole action rather than the series it renders: the browser+ # outlives a single test, so an account whose sparkline an earlier test+ # already fetched answers the conditional GET with a 304 without ever+ # computing one.+ module StalledSparklines+ mattr_accessor :enabled, default: false++ def sparkline+ sleep 0.05 while StalledSparklines.enabled+ super+ end+ end+ AccountsController.prepend(StalledSparklines)++ setup do+ @user = users(:family_admin)+ sign_in @user+ end++ test "a frame that is never fetched keeps its placeholder instead of a timeout badge" do+ with_short_sparkline_timeout do+ visit root_path++ # Nothing is expanded on the dashboard: no account page is active, so+ # every group renders closed and the rows inside it stay off screen.+ assert_no_selector "#sidebar-scroll details[open]"++ # The group's own sparkline sits in the <summary>, so it is on screen and+ # Turbo does fetch it: the page is working, the collapsed rows below are+ # simply never asked for.+ assert_selector "#{GROUP_SPARKLINE_FRAME}[complete]", minimum: 1++ wait_out_timeout_window++ # No badge anywhere in the sidebar, group totals included.+ assert_no_selector "#{SPARKLINE_FRAME} p", text: BADGE_TEXT, visible: :all++ # The rows nobody asked for still hold their loading placeholder, which+ # is an honest description of what they are doing.+ assert_selector "#{ACCOUNT_SPARKLINE_FRAME} .bg-loader", visible: :all, minimum: 1+ end+ end++ test "expanding a group loads its sparklines" do+ with_short_sparkline_timeout do+ visit root_path++ expand_first_group++ assert_selector "#{ACCOUNT_SPARKLINE_FRAME}[complete] [data-controller='time-series-chart']", minimum: 1++ wait_out_timeout_window++ assert_no_selector "#{SPARKLINE_FRAME} p", text: BADGE_TEXT, visible: :all+ end+ end++ test "a request that never comes back still raises the timeout badge" do+ with_short_sparkline_timeout do+ visit root_path++ with_stalled_sparkline_responses do+ expand_first_group++ # On the rows that were stalled, not on a group total that loaded long+ # before the stall was in place.+ assert_selector "#{ACCOUNT_SPARKLINE_FRAME} p", text: BADGE_TEXT, minimum: 1+ end+ end+ end++ private++ def with_short_sparkline_timeout(&block)+ stub_const(AccountsHelper, :SPARKLINE_FRAME_TIMEOUT_MS, SHORT_TIMEOUT_MS, &block)+ end++ def with_stalled_sparkline_responses+ StalledSparklines.enabled = true+ yield+ ensure+ StalledSparklines.enabled = false+ end++ def expand_first_group+ find("#sidebar-scroll details", match: :first).find("summary").click+ end++ # Sleeping is the assertion here: the badge is an absence, and an absence+ # only means anything once the window it would have fired in has passed.+ def wait_out_timeout_window+ sleep (SHORT_TIMEOUT_MS * 3) / 1000.0+ end+end