The personal finance app for everyone (by everyone)
Ruby
Measure SimpleFIN request windows in elapsed time (#5144)
arnaurodondev62645cec3f68
diff --git a/app/models/simplefin_item/importer.rb b/app/models/simplefin_item/importer.rbindex 2869c5fefc..ce246c8855 100644--- a/app/models/simplefin_item/importer.rb+++ b/app/models/simplefin_item/importer.rb@@ -6,6 +6,9 @@ class SimplefinItem::Importer# SimpleFIN Bridge refuses a request whose date range is longer than this# ("Requested date range exceeds recommended range of 45 days").MAX_REQUEST_DAYS = 45+ # The Bridge measures the limit in elapsed time. `n.days` keeps the wall-clock+ # time across a DST change, which makes a window 45 days plus an hour long.+ MAX_REQUEST_SECONDS = MAX_REQUEST_DAYS * 86_400attr_reader :simplefin_item, :simplefin_provider, :sync@@ -400,8 +403,8 @@ class SimplefinItem::Importer# We do NOT use the small initial lookback (7 days) here, because that# would clip the very first chunk to ~1 week and prevent further history.user_start_date = simplefin_item.sync_start_date- implied_max_lookback_days = chunk_size_days * max_requests- default_start_date = implied_max_lookback_days.days.ago+ # Measured in elapsed time like each chunk, so the cap always reaches it.+ default_start_date = Time.current - (MAX_REQUEST_SECONDS * max_requests)target_start_date = user_start_date ? user_start_date.beginning_of_day : default_start_date# Enforce maximum 1-year lookback since SimpleFIN rarely provides more than 90 days@@ -431,7 +434,7 @@ class SimplefinItem::Importerchunk_count += 1# Calculate chunk start date - always use exactly chunk_size_days to stay within limits- chunk_start_date = chunk_end_date - chunk_size_days.days+ chunk_start_date = chunk_end_date - (chunk_size_days * 86_400)# Don't go back further than the target start dateif chunk_start_date < target_start_date@@ -588,7 +591,7 @@ class SimplefinItem::Importerwindows = []window_start = start_datewhile window_start < end_date- window_end = [ window_start + MAX_REQUEST_DAYS.days, end_date ].min+ window_end = [ window_start + MAX_REQUEST_SECONDS, end_date ].minwindows << [ window_start, window_end ]window_start = window_endenddiff --git a/test/models/simplefin_item/importer_test.rb b/test/models/simplefin_item/importer_test.rbindex de6b2d3a15..fd20249816 100644--- a/test/models/simplefin_item/importer_test.rb+++ b/test/models/simplefin_item/importer_test.rb@@ -259,6 +259,54 @@ class SimplefinItem::ImporterTest < ActiveSupport::TestCaseassert_operator ranges.first.last - ranges.last.first, :>=, 120.daysend+ test "the chunked history import stays within the Bridge limit after a fall DST change" do+ Time.use_zone("America/Los_Angeles") do+ travel_to Time.zone.parse("2026-11-15 12:00") do+ link_simplefin_account("sf_dst_1")+ provider, ranges = recording_provider(account_id: "sf_dst_1")++ SimplefinItem::Importer.new(@item, simplefin_provider: provider).send(:import_with_chunked_history)++ assert_operator ranges.size, :>, 1+ ranges.each { |from, to| assert_operator to - from, :<=, 45 * 86_400 }+ end+ end+ end++ test "the default chunked lookback is reachable within the request cap across a DST change" do+ Time.use_zone("America/Los_Angeles") do+ travel_to Time.zone.parse("2026-03-07 12:00") do+ provider, ranges = recording_provider+ Rails.logger.stubs(:info)+ Rails.logger.expects(:info).with(regexp_matches(/reached target start date/)).once+ importer = SimplefinItem::Importer.new(@item, simplefin_provider: provider)+ # Every chunk adds one transaction, so only the cap or the target ends the walk.+ calls = 0+ importer.define_singleton_method(:count_linked_transactions) { (calls += 1) / 2 }++ importer.send(:import_with_chunked_history)++ assert_equal 8, ranges.size+ assert_equal 8 * 45 * 86_400, ranges.first.last - ranges.last.first+ ranges.each { |from, to| assert_operator to - from, :<=, 45 * 86_400 }+ end+ end+ end++ test "a regular sync stays within the Bridge limit after a fall DST change" do+ Time.use_zone("America/Los_Angeles") do+ travel_to Time.zone.parse("2026-11-15 12:00") do+ @item.stubs(:last_synced_at).returns(500.days.ago)+ provider, ranges = recording_provider++ SimplefinItem::Importer.new(@item, simplefin_provider: provider).send(:import_regular_sync)++ assert_operator ranges.size, :>, 1+ ranges.each { |from, to| assert_operator to - from, :<=, 45 * 86_400 }+ end+ end+ end+private# A provider that records each dated request's range. Without an account_id