fix(jobs): delegate recurring-transaction sync gate to Sync.for_family (#1975)

* fix(jobs): delegate recurring-transaction sync gate to Sync.for_family

`IdentifyRecurringTransactionsJob#family_has_incomplete_syncs?` hand-rolled
the list of provider `*_items` associations it polled — plaid, simplefin,
lunchflow, enable_banking, sophtron — missing nine other `Syncable`
provider concerns on `Family`: coinbase, binance, kraken, coinstats,
snaptrade, mercury, brex, indexa_capital, ibkr. When a sync on any of those
nine was in flight, the debounce gate fell through and
`RecurringTransaction::Identifier` ran against a partial dataset; the
follow-up re-enqueue then hit the `find_or_initialize_by` upsert path and
inherited the stale `occurrence_count`. Same drift pattern that bolted
sophtron on as the 5th entry (#591) was already an iteration of.

The maintainers' own `Sync.for_family` (sync.rb:61) already enumerates every
`*_items` association via `Family.reflect_on_all_associations(:has_many)`
filtered by inclusion of `Syncable` — exactly the helper the gate should
delegate to so the list cannot drift again.

- Add `Sync.any_incomplete_for?(family)` class method that wraps
  `for_family(family).incomplete.exists?`.
- Rewrite `family_has_incomplete_syncs?` to delegate. 14 lines → 1.
- New test file `test/jobs/identify_recurring_transactions_job_test.rb`
  covers in-flight Coinbase + Mercury (gate fires), idle (identifier runs),
  missing family, and superseded-by-newer-schedule.
- `test/models/sync_test.rb` gets 2 new tests pinning
  `any_incomplete_for?` against a provider `_items` sync and a
  family-itself sync.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

* test(jobs): stub Rails.cache.read for supersession test (NullStore in test env)

`Rails.cache` is `ActiveSupport::Cache::NullStore` in the Rails test env, so
the previous test's `Rails.cache.write(cache_key, @scheduled_at + 10, ...)`
was a no-op and `Rails.cache.read(cache_key)` returned `nil`. The
supersession short-circuit `return if latest_scheduled && latest_scheduled
> scheduled_at` then fell through, the job proceeded to invoke
`RecurringTransaction::Identifier`, and the Mocha
`.expects(:identify_recurring_patterns).never` failed in CI.

Switch to `Rails.cache.stubs(:read).with(cache_key).returns(...)` — the
same idiom `test/models/provider/twelve_data_test.rb:186-197` already uses
for the cache layer. Add an `assert_nil` on the bare `perform` return so
Minitest's assertion counter sees an explicit assertion (silences the
"missing assertions" warning).

No production-code change. Behavior under test is unchanged; only the test
mechanism for simulating "newer scheduled run already in cache" is fixed.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
galuis116
2026-05-27 15:01:34 -07:00
committed by GitHub
parent 174f7e6be6
commit 2e55bbe294
4 changed files with 94 additions and 14 deletions

View File

@@ -234,6 +234,29 @@ class SyncTest < ActiveSupport::TestCase
assert_equal syncs.map(&:id).sort, Sync.for_family(family).where(id: syncs.map(&:id)).pluck(:id).sort
end
test "any_incomplete_for? fires on a Sync against any Syncable provider item association" do
family = families(:dylan_family)
Sync.for_family(family).incomplete.find_each(&:destroy)
assert_not Sync.any_incomplete_for?(family)
mercury_item = mercury_items(:one)
incomplete = Sync.create!(syncable: mercury_item, status: :pending)
assert Sync.any_incomplete_for?(family),
"any_incomplete_for? should report true for an in-flight Mercury sync"
incomplete.update!(status: :completed)
assert_not Sync.any_incomplete_for?(family)
end
test "any_incomplete_for? fires on a Sync against the family itself" do
family = families(:dylan_family)
Sync.for_family(family).incomplete.find_each(&:destroy)
assert_not Sync.any_incomplete_for?(family)
Sync.create!(syncable: family, status: :syncing)
assert Sync.any_incomplete_for?(family)
end
test "api error payload is present for failed syncs without raw error text" do
sync = Sync.create!(syncable: accounts(:depository), status: :failed)