The personal finance app for everyone (by everyone)
Ruby
Stop sparkline timeout badge from replacing loaded charts (#4604)
GFR117bea5dad6e
diff --git a/app/helpers/accounts_helper.rb b/app/helpers/accounts_helper.rbindex 79ee336b00..df868443dd 100644--- a/app/helpers/accounts_helper.rb+++ b/app/helpers/accounts_helper.rb@@ -24,7 +24,7 @@ module AccountsHelper{controller: "turbo-frame-timeout",action: "turbo:before-fetch-request->turbo-frame-timeout#startTimeout " \- "turbo:frame-load->turbo-frame-timeout#clearTimeout",+ "turbo:before-frame-render->turbo-frame-timeout#clearTimeout",turbo_frame_timeout_timeout_value: SPARKLINE_FRAME_TIMEOUT_MS}end@@ -56,7 +56,7 @@ module AccountsHelperend[- family.build_cache_key("account_sidebar_tabs_v5", invalidate_on_data_updates: true),+ family.build_cache_key("account_sidebar_tabs_v6", invalidate_on_data_updates: true),Current.user&.id,shares_version,active_tab,diff --git a/app/javascript/controllers/turbo_frame_timeout_controller.js b/app/javascript/controllers/turbo_frame_timeout_controller.jsindex cd57e7ad6e..ba2587c234 100644--- a/app/javascript/controllers/turbo_frame_timeout_controller.js+++ b/app/javascript/controllers/turbo_frame_timeout_controller.js@@ -7,7 +7,14 @@ import { Controller } from "@hotwired/stimulus"// 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"+// turbo:before-frame-render->turbo-frame-timeout#clearTimeout"+//+// It stops when Turbo is about to render a matching frame, not on+// turbo:frame-load: Turbo inserts the new content several repaints before+// frame-load fires, and a countdown expiring in that gap would overwrite the+// rendered frame with the badge. Responses that bring no matching frame (an+// error, or a sign-in page after a redirect) never render, so they keep+// counting and still end in the badge.export default class extends Controller {static values = { timeout: { type: Number, default: 10000 } }diff --git a/app/views/accounts/_account_sidebar_tabs.html.erb b/app/views/accounts/_account_sidebar_tabs.html.erbindex c29233fcfc..a7fb4dfae3 100644--- a/app/views/accounts/_account_sidebar_tabs.html.erb+++ b/app/views/accounts/_account_sidebar_tabs.html.erb@@ -4,7 +4,7 @@ERB dependency tracker mis-parses as a bogus "Ds/D" template dependencyand then logs "Couldn't find template for digesting: Ds/D" on every cachemiss. The cache key is already manually versioned via- account_sidebar_tabs_cache_key ("account_sidebar_tabs_v5",+ account_sidebar_tabs_cache_key ("account_sidebar_tabs_v6",invalidate_on_data_updates: true), so we opt out of automatic templatedigesting. Bump that version suffix when changing this template or itssidebar dependencies. %>diff --git a/test/system/sidebar_sparklines_test.rb b/test/system/sidebar_sparklines_test.rbindex 4772c8ef96..003acf3f20 100644--- a/test/system/sidebar_sparklines_test.rb+++ b/test/system/sidebar_sparklines_test.rb@@ -21,6 +21,16 @@ class SidebarSparklinesTest < ApplicationSystemTestCase# with a short one and every wait below is proportional to it.SHORT_TIMEOUT_MS = 300+ # A real request may outlive the short window on a busy runner and raise the+ # badge until its response renders over it. The badge checks therefore wait+ # for every fetched frame to finish instead of requiring a response time.+ SETTLE_WAIT_SECONDS = 15++ # Long enough for a sparkline response to arrive inside it, so the tests+ # that use it only fail on what the countdown does with a response it+ # already had.+ RESPONSE_WINDOW_MS = 1500+# 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@@ -40,6 +50,25 @@ class SidebarSparklinesTest < ApplicationSystemTestCaseendAccountsController.prepend(StalledSparklines)+ # Answers sparkline requests with a response that has no sparkline frame:+ # a server error, or a page without the frame like the sign-in page a+ # request is redirected to once the session has expired.+ module BrokenSparklines+ mattr_accessor :mode, default: nil++ def sparkline+ case BrokenSparklines.mode+ when :server_error+ head :internal_server_error+ when :without_frame+ render html: "<!DOCTYPE html><html><head></head><body><p>Sign in</p></body></html>".html_safe, layout: false+ else+ super+ end+ end+ end+ AccountsController.prepend(BrokenSparklines)+setup do@user = users(:family_admin)sign_in @user@@ -58,6 +87,7 @@ class SidebarSparklinesTest < ApplicationSystemTestCase# simply never asked for.assert_selector "#{GROUP_SPARKLINE_FRAME}[complete]", minimum: 1+ wait_for_fetched_frameswait_out_timeout_window# No badge anywhere in the sidebar, group totals included.@@ -75,8 +105,9 @@ class SidebarSparklinesTest < ApplicationSystemTestCaseexpand_first_group- assert_selector "#{ACCOUNT_SPARKLINE_FRAME}[complete] [data-controller='time-series-chart']", minimum: 1+ assert_selector "#{ACCOUNT_SPARKLINE_FRAME}[complete] [data-controller='time-series-chart']", minimum: 1, wait: SETTLE_WAIT_SECONDS+ wait_for_fetched_frameswait_out_timeout_windowassert_no_selector "#{SPARKLINE_FRAME} p", text: BADGE_TEXT, visible: :all@@ -103,6 +134,44 @@ class SidebarSparklinesTest < ApplicationSystemTestCaseassert_selector "#{ACCOUNT_SPARKLINE_FRAME}[complete] [data-controller='time-series-chart'] svg path", minimum: 1end+ # Turbo inserts a response and then waits for repaints before it fires+ # turbo:frame-load. Slowing those repaints down after the insertion lets the+ # window expire inside that gap, where a countdown that only stops on+ # frame-load replaces the sparkline that has just been drawn. The window is+ # long enough for the response itself to arrive in time.+ test "a sparkline that has rendered is not replaced by the timeout badge" do+ stub_const(AccountsHelper, :SPARKLINE_FRAME_TIMEOUT_MS, RESPONSE_WINDOW_MS) do+ visit root_path++ record_account_sparkline_response_times+ page.execute_script(<<~JS)+ const requestAnimationFrame = window.requestAnimationFrame+ document.addEventListener("turbo:before-frame-render", (event) => {+ if (!event.target.id.startsWith("sparkline_account_")) return+ const render = event.detail.render+ event.detail.render = (currentElement, newElement) => {+ render(currentElement, newElement)+ window.requestAnimationFrame = (callback) =>+ setTimeout(() => requestAnimationFrame(callback), #{RESPONSE_WINDOW_MS})+ setTimeout(() => { window.requestAnimationFrame = requestAnimationFrame }, #{RESPONSE_WINDOW_MS * 4})+ }+ })+ JS++ expand_first_group++ assert_selector ACCOUNT_SPARKLINE_FRAME, minimum: 1+ assert_no_selector "#{ACCOUNT_SPARKLINE_FRAME}:not([complete])", wait: SETTLE_WAIT_SECONDS++ # A response that missed the window would let the badge land before the+ # render and be painted over, which proves nothing about the gap.+ assert_account_sparkline_responses_within RESPONSE_WINDOW_MS++ assert_no_selector "#{ACCOUNT_SPARKLINE_FRAME} p", text: BADGE_TEXT, visible: :all+ assert_selector "#{ACCOUNT_SPARKLINE_FRAME} [data-controller='time-series-chart'] svg path", minimum: 1+ end+ end+test "a request that never comes back still raises the timeout badge" dowith_short_sparkline_timeout dovisit root_path@@ -117,6 +186,18 @@ class SidebarSparklinesTest < ApplicationSystemTestCaseendend+ # Only a response that renders the frame ends the wait; one without it is as+ # much a failed sparkline as a request that never returns. Each response has+ # to arrive inside the window, otherwise the badge would be there before it+ # and prove nothing about what the response does to the countdown.+ test "an error response still raises the timeout badge" do+ assert_broken_response_raises_badge :server_error+ end++ test "a response without the sparkline frame still raises the timeout badge" do+ assert_broken_response_raises_badge :without_frame+ end+privatedef with_short_sparkline_timeout(&block)@@ -130,10 +211,56 @@ class SidebarSparklinesTest < ApplicationSystemTestCaseStalledSparklines.enabled = falseend+ def with_broken_sparkline_responses(mode)+ BrokenSparklines.mode = mode+ yield+ ensure+ BrokenSparklines.mode = nil+ end++ def assert_broken_response_raises_badge(mode)+ stub_const(AccountsHelper, :SPARKLINE_FRAME_TIMEOUT_MS, RESPONSE_WINDOW_MS) do+ visit root_path+ record_account_sparkline_response_times++ with_broken_sparkline_responses(mode) do+ expand_first_group++ assert_selector "#{ACCOUNT_SPARKLINE_FRAME} p", text: BADGE_TEXT, minimum: 1, wait: SETTLE_WAIT_SECONDS+ assert_account_sparkline_responses_within RESPONSE_WINDOW_MS+ end+ end+ end++ def record_account_sparkline_response_times+ page.execute_script(<<~JS)+ const requestedAt = {}+ window.sparklineResponseTimes = []+ document.addEventListener("turbo:before-fetch-request", (event) => {+ if (event.target.id.startsWith("sparkline_account_")) requestedAt[event.target.id] = performance.now()+ })+ document.addEventListener("turbo:before-fetch-response", (event) => {+ if (event.target.id in requestedAt) window.sparklineResponseTimes.push(performance.now() - requestedAt[event.target.id])+ })+ JS+ end++ def assert_account_sparkline_responses_within(ms)+ response_times = page.evaluate_script("window.sparklineResponseTimes")+ assert_not_empty response_times+ assert response_times.all? { |time| time < ms }, "Sparkline responses took #{response_times.map(&:round)} ms"+ end+def expand_first_groupfind("#sidebar-scroll details", match: :first).find("summary").clickend+ # Turbo marks a frame `busy` only while its request is in flight, so this+ # waits for real responses without treating unfetched lazy frames as late.+ def wait_for_fetched_frames+ assert_no_selector "#{SPARKLINE_FRAME}[busy]", visible: :all, wait: SETTLE_WAIT_SECONDS+ 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