mirror of
https://github.com/we-promise/sure.git
synced 2026-04-07 14:31:25 +00:00
* Add tests and logic for Simplefin account balance normalization - Introduced `SimplefinAccountProcessorTest` to verify balance normalization logic. - Updated `SimplefinAccount::Processor` to invert negative balances for liability accounts (credit cards and loans) while keeping asset balances unchanged. - Added comments to clarify balance conventions and sign normalization rules. * Refactor balances-only sync logic and improve tests for edge cases - Updated `SimplefinItem::Importer` and `SimplefinItem::Syncer` to ensure `last_synced_at` remains nil during balances-only runs, preserving chunked-history behavior for full syncs. - Introduced additional comments to clarify balances-only implications and syncing logic. - Added test case in `SimplefinAccountProcessorTest` to verify correct handling of overpayment for credit card liabilities. - Refined balance normalization in `SimplefinAccount::Processor` to always invert liability balances for consistency. --------- Co-authored-by: Josh Waldrep <joshua.waldrep5+github@gmail.com>
85 lines
2.3 KiB
Ruby
85 lines
2.3 KiB
Ruby
require "test_helper"
|
|
|
|
class SimplefinAccountProcessorTest < ActiveSupport::TestCase
|
|
setup do
|
|
@family = families(:dylan_family)
|
|
@item = SimplefinItem.create!(
|
|
family: @family,
|
|
name: "SimpleFIN",
|
|
access_url: "https://example.com/token"
|
|
)
|
|
end
|
|
|
|
test "inverts negative balance for credit card liabilities" do
|
|
sfin_acct = SimplefinAccount.create!(
|
|
simplefin_item: @item,
|
|
name: "Chase Credit",
|
|
account_id: "cc_1",
|
|
currency: "USD",
|
|
account_type: "credit",
|
|
current_balance: BigDecimal("-123.45")
|
|
)
|
|
|
|
acct = accounts(:credit_card)
|
|
acct.update!(simplefin_account: sfin_acct)
|
|
|
|
SimplefinAccount::Processor.new(sfin_acct).send(:process_account!)
|
|
|
|
assert_equal BigDecimal("123.45"), acct.reload.balance
|
|
end
|
|
|
|
test "does not invert balance for asset accounts (depository)" do
|
|
sfin_acct = SimplefinAccount.create!(
|
|
simplefin_item: @item,
|
|
name: "Checking",
|
|
account_id: "dep_1",
|
|
currency: "USD",
|
|
account_type: "checking",
|
|
current_balance: BigDecimal("1000.00")
|
|
)
|
|
|
|
acct = accounts(:depository)
|
|
acct.update!(simplefin_account: sfin_acct)
|
|
|
|
SimplefinAccount::Processor.new(sfin_acct).send(:process_account!)
|
|
|
|
assert_equal BigDecimal("1000.00"), acct.reload.balance
|
|
end
|
|
|
|
test "inverts negative balance for loan liabilities" do
|
|
sfin_acct = SimplefinAccount.create!(
|
|
simplefin_item: @item,
|
|
name: "Mortgage",
|
|
account_id: "loan_1",
|
|
currency: "USD",
|
|
account_type: "mortgage",
|
|
current_balance: BigDecimal("-50000")
|
|
)
|
|
|
|
acct = accounts(:loan)
|
|
acct.update!(simplefin_account: sfin_acct)
|
|
|
|
SimplefinAccount::Processor.new(sfin_acct).send(:process_account!)
|
|
|
|
assert_equal BigDecimal("50000"), acct.reload.balance
|
|
end
|
|
|
|
test "positive provider balance (overpayment) becomes negative for credit card liabilities" do
|
|
sfin_acct = SimplefinAccount.create!(
|
|
simplefin_item: @item,
|
|
name: "Chase Credit",
|
|
account_id: "cc_overpay",
|
|
currency: "USD",
|
|
account_type: "credit",
|
|
current_balance: BigDecimal("75.00") # provider sends positive for overpayment
|
|
)
|
|
|
|
acct = accounts(:credit_card)
|
|
acct.update!(simplefin_account: sfin_acct)
|
|
|
|
SimplefinAccount::Processor.new(sfin_acct).send(:process_account!)
|
|
|
|
assert_equal BigDecimal("-75.00"), acct.reload.balance
|
|
end
|
|
end
|